inode.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /*
  2. * hugetlbpage-backed filesystem. Based on ramfs.
  3. *
  4. * Nadia Yvette Chambers, 2002
  5. *
  6. * Copyright (C) 2002 Linus Torvalds.
  7. * License: GPL
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/thread_info.h>
  11. #include <asm/current.h>
  12. #include <linux/sched/signal.h> /* remove ASAP */
  13. #include <linux/falloc.h>
  14. #include <linux/fs.h>
  15. #include <linux/mount.h>
  16. #include <linux/file.h>
  17. #include <linux/kernel.h>
  18. #include <linux/writeback.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/highmem.h>
  21. #include <linux/init.h>
  22. #include <linux/string.h>
  23. #include <linux/capability.h>
  24. #include <linux/ctype.h>
  25. #include <linux/backing-dev.h>
  26. #include <linux/hugetlb.h>
  27. #include <linux/pagevec.h>
  28. #include <linux/parser.h>
  29. #include <linux/mman.h>
  30. #include <linux/slab.h>
  31. #include <linux/dnotify.h>
  32. #include <linux/statfs.h>
  33. #include <linux/security.h>
  34. #include <linux/magic.h>
  35. #include <linux/migrate.h>
  36. #include <linux/uio.h>
  37. #include <linux/uaccess.h>
  38. static const struct super_operations hugetlbfs_ops;
  39. static const struct address_space_operations hugetlbfs_aops;
  40. const struct file_operations hugetlbfs_file_operations;
  41. static const struct inode_operations hugetlbfs_dir_inode_operations;
  42. static const struct inode_operations hugetlbfs_inode_operations;
  43. struct hugetlbfs_config {
  44. struct hstate *hstate;
  45. long max_hpages;
  46. long nr_inodes;
  47. long min_hpages;
  48. kuid_t uid;
  49. kgid_t gid;
  50. umode_t mode;
  51. };
  52. int sysctl_hugetlb_shm_group;
  53. enum {
  54. Opt_size, Opt_nr_inodes,
  55. Opt_mode, Opt_uid, Opt_gid,
  56. Opt_pagesize, Opt_min_size,
  57. Opt_err,
  58. };
  59. static const match_table_t tokens = {
  60. {Opt_size, "size=%s"},
  61. {Opt_nr_inodes, "nr_inodes=%s"},
  62. {Opt_mode, "mode=%o"},
  63. {Opt_uid, "uid=%u"},
  64. {Opt_gid, "gid=%u"},
  65. {Opt_pagesize, "pagesize=%s"},
  66. {Opt_min_size, "min_size=%s"},
  67. {Opt_err, NULL},
  68. };
  69. #ifdef CONFIG_NUMA
  70. static inline void hugetlb_set_vma_policy(struct vm_area_struct *vma,
  71. struct inode *inode, pgoff_t index)
  72. {
  73. vma->vm_policy = mpol_shared_policy_lookup(&HUGETLBFS_I(inode)->policy,
  74. index);
  75. }
  76. static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma)
  77. {
  78. mpol_cond_put(vma->vm_policy);
  79. }
  80. #else
  81. static inline void hugetlb_set_vma_policy(struct vm_area_struct *vma,
  82. struct inode *inode, pgoff_t index)
  83. {
  84. }
  85. static inline void hugetlb_drop_vma_policy(struct vm_area_struct *vma)
  86. {
  87. }
  88. #endif
  89. static void huge_pagevec_release(struct pagevec *pvec)
  90. {
  91. int i;
  92. for (i = 0; i < pagevec_count(pvec); ++i)
  93. put_page(pvec->pages[i]);
  94. pagevec_reinit(pvec);
  95. }
  96. /*
  97. * Mask used when checking the page offset value passed in via system
  98. * calls. This value will be converted to a loff_t which is signed.
  99. * Therefore, we want to check the upper PAGE_SHIFT + 1 bits of the
  100. * value. The extra bit (- 1 in the shift value) is to take the sign
  101. * bit into account.
  102. */
  103. #define PGOFF_LOFFT_MAX \
  104. (((1UL << (PAGE_SHIFT + 1)) - 1) << (BITS_PER_LONG - (PAGE_SHIFT + 1)))
  105. static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
  106. {
  107. struct inode *inode = file_inode(file);
  108. loff_t len, vma_len;
  109. int ret;
  110. struct hstate *h = hstate_file(file);
  111. /*
  112. * vma address alignment (but not the pgoff alignment) has
  113. * already been checked by prepare_hugepage_range. If you add
  114. * any error returns here, do so after setting VM_HUGETLB, so
  115. * is_vm_hugetlb_page tests below unmap_region go the right
  116. * way when do_mmap_pgoff unwinds (may be important on powerpc
  117. * and ia64).
  118. */
  119. vma->vm_flags |= VM_HUGETLB | VM_DONTEXPAND;
  120. vma->vm_ops = &hugetlb_vm_ops;
  121. /*
  122. * page based offset in vm_pgoff could be sufficiently large to
  123. * overflow a loff_t when converted to byte offset. This can
  124. * only happen on architectures where sizeof(loff_t) ==
  125. * sizeof(unsigned long). So, only check in those instances.
  126. */
  127. if (sizeof(unsigned long) == sizeof(loff_t)) {
  128. if (vma->vm_pgoff & PGOFF_LOFFT_MAX)
  129. return -EINVAL;
  130. }
  131. /* must be huge page aligned */
  132. if (vma->vm_pgoff & (~huge_page_mask(h) >> PAGE_SHIFT))
  133. return -EINVAL;
  134. vma_len = (loff_t)(vma->vm_end - vma->vm_start);
  135. len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  136. /* check for overflow */
  137. if (len < vma_len)
  138. return -EINVAL;
  139. inode_lock(inode);
  140. file_accessed(file);
  141. ret = -ENOMEM;
  142. if (hugetlb_reserve_pages(inode,
  143. vma->vm_pgoff >> huge_page_order(h),
  144. len >> huge_page_shift(h), vma,
  145. vma->vm_flags))
  146. goto out;
  147. ret = 0;
  148. if (vma->vm_flags & VM_WRITE && inode->i_size < len)
  149. i_size_write(inode, len);
  150. out:
  151. inode_unlock(inode);
  152. return ret;
  153. }
  154. /*
  155. * Called under down_write(mmap_sem).
  156. */
  157. #ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
  158. static unsigned long
  159. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  160. unsigned long len, unsigned long pgoff, unsigned long flags)
  161. {
  162. struct mm_struct *mm = current->mm;
  163. struct vm_area_struct *vma;
  164. struct hstate *h = hstate_file(file);
  165. struct vm_unmapped_area_info info;
  166. if (len & ~huge_page_mask(h))
  167. return -EINVAL;
  168. if (len > TASK_SIZE)
  169. return -ENOMEM;
  170. if (flags & MAP_FIXED) {
  171. if (prepare_hugepage_range(file, addr, len))
  172. return -EINVAL;
  173. return addr;
  174. }
  175. if (addr) {
  176. addr = ALIGN(addr, huge_page_size(h));
  177. vma = find_vma(mm, addr);
  178. if (TASK_SIZE - len >= addr &&
  179. (!vma || addr + len <= vm_start_gap(vma)))
  180. return addr;
  181. }
  182. info.flags = 0;
  183. info.length = len;
  184. info.low_limit = TASK_UNMAPPED_BASE;
  185. info.high_limit = TASK_SIZE;
  186. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  187. info.align_offset = 0;
  188. return vm_unmapped_area(&info);
  189. }
  190. #endif
  191. static size_t
  192. hugetlbfs_read_actor(struct page *page, unsigned long offset,
  193. struct iov_iter *to, unsigned long size)
  194. {
  195. size_t copied = 0;
  196. int i, chunksize;
  197. /* Find which 4k chunk and offset with in that chunk */
  198. i = offset >> PAGE_SHIFT;
  199. offset = offset & ~PAGE_MASK;
  200. while (size) {
  201. size_t n;
  202. chunksize = PAGE_SIZE;
  203. if (offset)
  204. chunksize -= offset;
  205. if (chunksize > size)
  206. chunksize = size;
  207. n = copy_page_to_iter(&page[i], offset, chunksize, to);
  208. copied += n;
  209. if (n != chunksize)
  210. return copied;
  211. offset = 0;
  212. size -= chunksize;
  213. i++;
  214. }
  215. return copied;
  216. }
  217. /*
  218. * Support for read() - Find the page attached to f_mapping and copy out the
  219. * data. Its *very* similar to do_generic_mapping_read(), we can't use that
  220. * since it has PAGE_SIZE assumptions.
  221. */
  222. static ssize_t hugetlbfs_read_iter(struct kiocb *iocb, struct iov_iter *to)
  223. {
  224. struct file *file = iocb->ki_filp;
  225. struct hstate *h = hstate_file(file);
  226. struct address_space *mapping = file->f_mapping;
  227. struct inode *inode = mapping->host;
  228. unsigned long index = iocb->ki_pos >> huge_page_shift(h);
  229. unsigned long offset = iocb->ki_pos & ~huge_page_mask(h);
  230. unsigned long end_index;
  231. loff_t isize;
  232. ssize_t retval = 0;
  233. while (iov_iter_count(to)) {
  234. struct page *page;
  235. size_t nr, copied;
  236. /* nr is the maximum number of bytes to copy from this page */
  237. nr = huge_page_size(h);
  238. isize = i_size_read(inode);
  239. if (!isize)
  240. break;
  241. end_index = (isize - 1) >> huge_page_shift(h);
  242. if (index > end_index)
  243. break;
  244. if (index == end_index) {
  245. nr = ((isize - 1) & ~huge_page_mask(h)) + 1;
  246. if (nr <= offset)
  247. break;
  248. }
  249. nr = nr - offset;
  250. /* Find the page */
  251. page = find_lock_page(mapping, index);
  252. if (unlikely(page == NULL)) {
  253. /*
  254. * We have a HOLE, zero out the user-buffer for the
  255. * length of the hole or request.
  256. */
  257. copied = iov_iter_zero(nr, to);
  258. } else {
  259. unlock_page(page);
  260. /*
  261. * We have the page, copy it to user space buffer.
  262. */
  263. copied = hugetlbfs_read_actor(page, offset, to, nr);
  264. put_page(page);
  265. }
  266. offset += copied;
  267. retval += copied;
  268. if (copied != nr && iov_iter_count(to)) {
  269. if (!retval)
  270. retval = -EFAULT;
  271. break;
  272. }
  273. index += offset >> huge_page_shift(h);
  274. offset &= ~huge_page_mask(h);
  275. }
  276. iocb->ki_pos = ((loff_t)index << huge_page_shift(h)) + offset;
  277. return retval;
  278. }
  279. static int hugetlbfs_write_begin(struct file *file,
  280. struct address_space *mapping,
  281. loff_t pos, unsigned len, unsigned flags,
  282. struct page **pagep, void **fsdata)
  283. {
  284. return -EINVAL;
  285. }
  286. static int hugetlbfs_write_end(struct file *file, struct address_space *mapping,
  287. loff_t pos, unsigned len, unsigned copied,
  288. struct page *page, void *fsdata)
  289. {
  290. BUG();
  291. return -EINVAL;
  292. }
  293. static void remove_huge_page(struct page *page)
  294. {
  295. ClearPageDirty(page);
  296. ClearPageUptodate(page);
  297. delete_from_page_cache(page);
  298. }
  299. static void
  300. hugetlb_vmdelete_list(struct rb_root_cached *root, pgoff_t start, pgoff_t end)
  301. {
  302. struct vm_area_struct *vma;
  303. /*
  304. * end == 0 indicates that the entire range after
  305. * start should be unmapped.
  306. */
  307. vma_interval_tree_foreach(vma, root, start, end ? end : ULONG_MAX) {
  308. unsigned long v_offset;
  309. unsigned long v_end;
  310. /*
  311. * Can the expression below overflow on 32-bit arches?
  312. * No, because the interval tree returns us only those vmas
  313. * which overlap the truncated area starting at pgoff,
  314. * and no vma on a 32-bit arch can span beyond the 4GB.
  315. */
  316. if (vma->vm_pgoff < start)
  317. v_offset = (start - vma->vm_pgoff) << PAGE_SHIFT;
  318. else
  319. v_offset = 0;
  320. if (!end)
  321. v_end = vma->vm_end;
  322. else {
  323. v_end = ((end - vma->vm_pgoff) << PAGE_SHIFT)
  324. + vma->vm_start;
  325. if (v_end > vma->vm_end)
  326. v_end = vma->vm_end;
  327. }
  328. unmap_hugepage_range(vma, vma->vm_start + v_offset, v_end,
  329. NULL);
  330. }
  331. }
  332. /*
  333. * remove_inode_hugepages handles two distinct cases: truncation and hole
  334. * punch. There are subtle differences in operation for each case.
  335. *
  336. * truncation is indicated by end of range being LLONG_MAX
  337. * In this case, we first scan the range and release found pages.
  338. * After releasing pages, hugetlb_unreserve_pages cleans up region/reserv
  339. * maps and global counts. Page faults can not race with truncation
  340. * in this routine. hugetlb_no_page() prevents page faults in the
  341. * truncated range. It checks i_size before allocation, and again after
  342. * with the page table lock for the page held. The same lock must be
  343. * acquired to unmap a page.
  344. * hole punch is indicated if end is not LLONG_MAX
  345. * In the hole punch case we scan the range and release found pages.
  346. * Only when releasing a page is the associated region/reserv map
  347. * deleted. The region/reserv map for ranges without associated
  348. * pages are not modified. Page faults can race with hole punch.
  349. * This is indicated if we find a mapped page.
  350. * Note: If the passed end of range value is beyond the end of file, but
  351. * not LLONG_MAX this routine still performs a hole punch operation.
  352. */
  353. static void remove_inode_hugepages(struct inode *inode, loff_t lstart,
  354. loff_t lend)
  355. {
  356. struct hstate *h = hstate_inode(inode);
  357. struct address_space *mapping = &inode->i_data;
  358. const pgoff_t start = lstart >> huge_page_shift(h);
  359. const pgoff_t end = lend >> huge_page_shift(h);
  360. struct vm_area_struct pseudo_vma;
  361. struct pagevec pvec;
  362. pgoff_t next, index;
  363. int i, freed = 0;
  364. bool truncate_op = (lend == LLONG_MAX);
  365. vma_init(&pseudo_vma, current->mm);
  366. pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
  367. pagevec_init(&pvec);
  368. next = start;
  369. while (next < end) {
  370. /*
  371. * When no more pages are found, we are done.
  372. */
  373. if (!pagevec_lookup_range(&pvec, mapping, &next, end - 1))
  374. break;
  375. for (i = 0; i < pagevec_count(&pvec); ++i) {
  376. struct page *page = pvec.pages[i];
  377. u32 hash;
  378. index = page->index;
  379. hash = hugetlb_fault_mutex_hash(h, current->mm,
  380. &pseudo_vma,
  381. mapping, index, 0);
  382. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  383. /*
  384. * If page is mapped, it was faulted in after being
  385. * unmapped in caller. Unmap (again) now after taking
  386. * the fault mutex. The mutex will prevent faults
  387. * until we finish removing the page.
  388. *
  389. * This race can only happen in the hole punch case.
  390. * Getting here in a truncate operation is a bug.
  391. */
  392. if (unlikely(page_mapped(page))) {
  393. BUG_ON(truncate_op);
  394. i_mmap_lock_write(mapping);
  395. hugetlb_vmdelete_list(&mapping->i_mmap,
  396. index * pages_per_huge_page(h),
  397. (index + 1) * pages_per_huge_page(h));
  398. i_mmap_unlock_write(mapping);
  399. }
  400. lock_page(page);
  401. /*
  402. * We must free the huge page and remove from page
  403. * cache (remove_huge_page) BEFORE removing the
  404. * region/reserve map (hugetlb_unreserve_pages). In
  405. * rare out of memory conditions, removal of the
  406. * region/reserve map could fail. Correspondingly,
  407. * the subpool and global reserve usage count can need
  408. * to be adjusted.
  409. */
  410. VM_BUG_ON(PagePrivate(page));
  411. remove_huge_page(page);
  412. freed++;
  413. if (!truncate_op) {
  414. if (unlikely(hugetlb_unreserve_pages(inode,
  415. index, index + 1, 1)))
  416. hugetlb_fix_reserve_counts(inode);
  417. }
  418. unlock_page(page);
  419. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  420. }
  421. huge_pagevec_release(&pvec);
  422. cond_resched();
  423. }
  424. if (truncate_op)
  425. (void)hugetlb_unreserve_pages(inode, start, LONG_MAX, freed);
  426. }
  427. static void hugetlbfs_evict_inode(struct inode *inode)
  428. {
  429. struct resv_map *resv_map;
  430. remove_inode_hugepages(inode, 0, LLONG_MAX);
  431. resv_map = (struct resv_map *)inode->i_mapping->private_data;
  432. /* root inode doesn't have the resv_map, so we should check it */
  433. if (resv_map)
  434. resv_map_release(&resv_map->refs);
  435. clear_inode(inode);
  436. }
  437. static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
  438. {
  439. pgoff_t pgoff;
  440. struct address_space *mapping = inode->i_mapping;
  441. struct hstate *h = hstate_inode(inode);
  442. BUG_ON(offset & ~huge_page_mask(h));
  443. pgoff = offset >> PAGE_SHIFT;
  444. i_size_write(inode, offset);
  445. i_mmap_lock_write(mapping);
  446. if (!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))
  447. hugetlb_vmdelete_list(&mapping->i_mmap, pgoff, 0);
  448. i_mmap_unlock_write(mapping);
  449. remove_inode_hugepages(inode, offset, LLONG_MAX);
  450. return 0;
  451. }
  452. static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
  453. {
  454. struct hstate *h = hstate_inode(inode);
  455. loff_t hpage_size = huge_page_size(h);
  456. loff_t hole_start, hole_end;
  457. /*
  458. * For hole punch round up the beginning offset of the hole and
  459. * round down the end.
  460. */
  461. hole_start = round_up(offset, hpage_size);
  462. hole_end = round_down(offset + len, hpage_size);
  463. if (hole_end > hole_start) {
  464. struct address_space *mapping = inode->i_mapping;
  465. struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
  466. inode_lock(inode);
  467. /* protected by i_mutex */
  468. if (info->seals & F_SEAL_WRITE) {
  469. inode_unlock(inode);
  470. return -EPERM;
  471. }
  472. i_mmap_lock_write(mapping);
  473. if (!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))
  474. hugetlb_vmdelete_list(&mapping->i_mmap,
  475. hole_start >> PAGE_SHIFT,
  476. hole_end >> PAGE_SHIFT);
  477. i_mmap_unlock_write(mapping);
  478. remove_inode_hugepages(inode, hole_start, hole_end);
  479. inode_unlock(inode);
  480. }
  481. return 0;
  482. }
  483. static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
  484. loff_t len)
  485. {
  486. struct inode *inode = file_inode(file);
  487. struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
  488. struct address_space *mapping = inode->i_mapping;
  489. struct hstate *h = hstate_inode(inode);
  490. struct vm_area_struct pseudo_vma;
  491. struct mm_struct *mm = current->mm;
  492. loff_t hpage_size = huge_page_size(h);
  493. unsigned long hpage_shift = huge_page_shift(h);
  494. pgoff_t start, index, end;
  495. int error;
  496. u32 hash;
  497. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  498. return -EOPNOTSUPP;
  499. if (mode & FALLOC_FL_PUNCH_HOLE)
  500. return hugetlbfs_punch_hole(inode, offset, len);
  501. /*
  502. * Default preallocate case.
  503. * For this range, start is rounded down and end is rounded up
  504. * as well as being converted to page offsets.
  505. */
  506. start = offset >> hpage_shift;
  507. end = (offset + len + hpage_size - 1) >> hpage_shift;
  508. inode_lock(inode);
  509. /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
  510. error = inode_newsize_ok(inode, offset + len);
  511. if (error)
  512. goto out;
  513. if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
  514. error = -EPERM;
  515. goto out;
  516. }
  517. /*
  518. * Initialize a pseudo vma as this is required by the huge page
  519. * allocation routines. If NUMA is configured, use page index
  520. * as input to create an allocation policy.
  521. */
  522. vma_init(&pseudo_vma, mm);
  523. pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
  524. pseudo_vma.vm_file = file;
  525. for (index = start; index < end; index++) {
  526. /*
  527. * This is supposed to be the vaddr where the page is being
  528. * faulted in, but we have no vaddr here.
  529. */
  530. struct page *page;
  531. unsigned long addr;
  532. int avoid_reserve = 0;
  533. cond_resched();
  534. /*
  535. * fallocate(2) manpage permits EINTR; we may have been
  536. * interrupted because we are using up too much memory.
  537. */
  538. if (signal_pending(current)) {
  539. error = -EINTR;
  540. break;
  541. }
  542. /* Set numa allocation policy based on index */
  543. hugetlb_set_vma_policy(&pseudo_vma, inode, index);
  544. /* addr is the offset within the file (zero based) */
  545. addr = index * hpage_size;
  546. /* mutex taken here, fault path and hole punch */
  547. hash = hugetlb_fault_mutex_hash(h, mm, &pseudo_vma, mapping,
  548. index, addr);
  549. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  550. /* See if already present in mapping to avoid alloc/free */
  551. page = find_get_page(mapping, index);
  552. if (page) {
  553. put_page(page);
  554. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  555. hugetlb_drop_vma_policy(&pseudo_vma);
  556. continue;
  557. }
  558. /* Allocate page and add to page cache */
  559. page = alloc_huge_page(&pseudo_vma, addr, avoid_reserve);
  560. hugetlb_drop_vma_policy(&pseudo_vma);
  561. if (IS_ERR(page)) {
  562. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  563. error = PTR_ERR(page);
  564. goto out;
  565. }
  566. clear_huge_page(page, addr, pages_per_huge_page(h));
  567. __SetPageUptodate(page);
  568. error = huge_add_to_page_cache(page, mapping, index);
  569. if (unlikely(error)) {
  570. put_page(page);
  571. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  572. goto out;
  573. }
  574. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  575. /*
  576. * unlock_page because locked by add_to_page_cache()
  577. * page_put due to reference from alloc_huge_page()
  578. */
  579. unlock_page(page);
  580. put_page(page);
  581. }
  582. if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
  583. i_size_write(inode, offset + len);
  584. inode->i_ctime = current_time(inode);
  585. out:
  586. inode_unlock(inode);
  587. return error;
  588. }
  589. static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
  590. {
  591. struct inode *inode = d_inode(dentry);
  592. struct hstate *h = hstate_inode(inode);
  593. int error;
  594. unsigned int ia_valid = attr->ia_valid;
  595. struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
  596. BUG_ON(!inode);
  597. error = setattr_prepare(dentry, attr);
  598. if (error)
  599. return error;
  600. if (ia_valid & ATTR_SIZE) {
  601. loff_t oldsize = inode->i_size;
  602. loff_t newsize = attr->ia_size;
  603. if (newsize & ~huge_page_mask(h))
  604. return -EINVAL;
  605. /* protected by i_mutex */
  606. if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
  607. (newsize > oldsize && (info->seals & F_SEAL_GROW)))
  608. return -EPERM;
  609. error = hugetlb_vmtruncate(inode, newsize);
  610. if (error)
  611. return error;
  612. }
  613. setattr_copy(inode, attr);
  614. mark_inode_dirty(inode);
  615. return 0;
  616. }
  617. static struct inode *hugetlbfs_get_root(struct super_block *sb,
  618. struct hugetlbfs_config *config)
  619. {
  620. struct inode *inode;
  621. inode = new_inode(sb);
  622. if (inode) {
  623. inode->i_ino = get_next_ino();
  624. inode->i_mode = S_IFDIR | config->mode;
  625. inode->i_uid = config->uid;
  626. inode->i_gid = config->gid;
  627. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  628. inode->i_op = &hugetlbfs_dir_inode_operations;
  629. inode->i_fop = &simple_dir_operations;
  630. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  631. inc_nlink(inode);
  632. lockdep_annotate_inode_mutex_key(inode);
  633. }
  634. return inode;
  635. }
  636. /*
  637. * Hugetlbfs is not reclaimable; therefore its i_mmap_rwsem will never
  638. * be taken from reclaim -- unlike regular filesystems. This needs an
  639. * annotation because huge_pmd_share() does an allocation under hugetlb's
  640. * i_mmap_rwsem.
  641. */
  642. static struct lock_class_key hugetlbfs_i_mmap_rwsem_key;
  643. static struct inode *hugetlbfs_get_inode(struct super_block *sb,
  644. struct inode *dir,
  645. umode_t mode, dev_t dev)
  646. {
  647. struct inode *inode;
  648. struct resv_map *resv_map;
  649. resv_map = resv_map_alloc();
  650. if (!resv_map)
  651. return NULL;
  652. inode = new_inode(sb);
  653. if (inode) {
  654. struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
  655. inode->i_ino = get_next_ino();
  656. inode_init_owner(inode, dir, mode);
  657. lockdep_set_class(&inode->i_mapping->i_mmap_rwsem,
  658. &hugetlbfs_i_mmap_rwsem_key);
  659. inode->i_mapping->a_ops = &hugetlbfs_aops;
  660. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  661. inode->i_mapping->private_data = resv_map;
  662. info->seals = F_SEAL_SEAL;
  663. switch (mode & S_IFMT) {
  664. default:
  665. init_special_inode(inode, mode, dev);
  666. break;
  667. case S_IFREG:
  668. inode->i_op = &hugetlbfs_inode_operations;
  669. inode->i_fop = &hugetlbfs_file_operations;
  670. break;
  671. case S_IFDIR:
  672. inode->i_op = &hugetlbfs_dir_inode_operations;
  673. inode->i_fop = &simple_dir_operations;
  674. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  675. inc_nlink(inode);
  676. break;
  677. case S_IFLNK:
  678. inode->i_op = &page_symlink_inode_operations;
  679. inode_nohighmem(inode);
  680. break;
  681. }
  682. lockdep_annotate_inode_mutex_key(inode);
  683. } else
  684. kref_put(&resv_map->refs, resv_map_release);
  685. return inode;
  686. }
  687. /*
  688. * File creation. Allocate an inode, and we're done..
  689. */
  690. static int hugetlbfs_mknod(struct inode *dir,
  691. struct dentry *dentry, umode_t mode, dev_t dev)
  692. {
  693. struct inode *inode;
  694. int error = -ENOSPC;
  695. inode = hugetlbfs_get_inode(dir->i_sb, dir, mode, dev);
  696. if (inode) {
  697. dir->i_ctime = dir->i_mtime = current_time(dir);
  698. d_instantiate(dentry, inode);
  699. dget(dentry); /* Extra count - pin the dentry in core */
  700. error = 0;
  701. }
  702. return error;
  703. }
  704. static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  705. {
  706. int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
  707. if (!retval)
  708. inc_nlink(dir);
  709. return retval;
  710. }
  711. static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  712. {
  713. return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
  714. }
  715. static int hugetlbfs_symlink(struct inode *dir,
  716. struct dentry *dentry, const char *symname)
  717. {
  718. struct inode *inode;
  719. int error = -ENOSPC;
  720. inode = hugetlbfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
  721. if (inode) {
  722. int l = strlen(symname)+1;
  723. error = page_symlink(inode, symname, l);
  724. if (!error) {
  725. d_instantiate(dentry, inode);
  726. dget(dentry);
  727. } else
  728. iput(inode);
  729. }
  730. dir->i_ctime = dir->i_mtime = current_time(dir);
  731. return error;
  732. }
  733. /*
  734. * mark the head page dirty
  735. */
  736. static int hugetlbfs_set_page_dirty(struct page *page)
  737. {
  738. struct page *head = compound_head(page);
  739. SetPageDirty(head);
  740. return 0;
  741. }
  742. static int hugetlbfs_migrate_page(struct address_space *mapping,
  743. struct page *newpage, struct page *page,
  744. enum migrate_mode mode)
  745. {
  746. int rc;
  747. rc = migrate_huge_page_move_mapping(mapping, newpage, page);
  748. if (rc != MIGRATEPAGE_SUCCESS)
  749. return rc;
  750. if (mode != MIGRATE_SYNC_NO_COPY)
  751. migrate_page_copy(newpage, page);
  752. else
  753. migrate_page_states(newpage, page);
  754. return MIGRATEPAGE_SUCCESS;
  755. }
  756. static int hugetlbfs_error_remove_page(struct address_space *mapping,
  757. struct page *page)
  758. {
  759. struct inode *inode = mapping->host;
  760. pgoff_t index = page->index;
  761. remove_huge_page(page);
  762. if (unlikely(hugetlb_unreserve_pages(inode, index, index + 1, 1)))
  763. hugetlb_fix_reserve_counts(inode);
  764. return 0;
  765. }
  766. /*
  767. * Display the mount options in /proc/mounts.
  768. */
  769. static int hugetlbfs_show_options(struct seq_file *m, struct dentry *root)
  770. {
  771. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(root->d_sb);
  772. struct hugepage_subpool *spool = sbinfo->spool;
  773. unsigned long hpage_size = huge_page_size(sbinfo->hstate);
  774. unsigned hpage_shift = huge_page_shift(sbinfo->hstate);
  775. char mod;
  776. if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
  777. seq_printf(m, ",uid=%u",
  778. from_kuid_munged(&init_user_ns, sbinfo->uid));
  779. if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
  780. seq_printf(m, ",gid=%u",
  781. from_kgid_munged(&init_user_ns, sbinfo->gid));
  782. if (sbinfo->mode != 0755)
  783. seq_printf(m, ",mode=%o", sbinfo->mode);
  784. if (sbinfo->max_inodes != -1)
  785. seq_printf(m, ",nr_inodes=%lu", sbinfo->max_inodes);
  786. hpage_size /= 1024;
  787. mod = 'K';
  788. if (hpage_size >= 1024) {
  789. hpage_size /= 1024;
  790. mod = 'M';
  791. }
  792. seq_printf(m, ",pagesize=%lu%c", hpage_size, mod);
  793. if (spool) {
  794. if (spool->max_hpages != -1)
  795. seq_printf(m, ",size=%llu",
  796. (unsigned long long)spool->max_hpages << hpage_shift);
  797. if (spool->min_hpages != -1)
  798. seq_printf(m, ",min_size=%llu",
  799. (unsigned long long)spool->min_hpages << hpage_shift);
  800. }
  801. return 0;
  802. }
  803. static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  804. {
  805. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
  806. struct hstate *h = hstate_inode(d_inode(dentry));
  807. buf->f_type = HUGETLBFS_MAGIC;
  808. buf->f_bsize = huge_page_size(h);
  809. if (sbinfo) {
  810. spin_lock(&sbinfo->stat_lock);
  811. /* If no limits set, just report 0 for max/free/used
  812. * blocks, like simple_statfs() */
  813. if (sbinfo->spool) {
  814. long free_pages;
  815. spin_lock(&sbinfo->spool->lock);
  816. buf->f_blocks = sbinfo->spool->max_hpages;
  817. free_pages = sbinfo->spool->max_hpages
  818. - sbinfo->spool->used_hpages;
  819. buf->f_bavail = buf->f_bfree = free_pages;
  820. spin_unlock(&sbinfo->spool->lock);
  821. buf->f_files = sbinfo->max_inodes;
  822. buf->f_ffree = sbinfo->free_inodes;
  823. }
  824. spin_unlock(&sbinfo->stat_lock);
  825. }
  826. buf->f_namelen = NAME_MAX;
  827. return 0;
  828. }
  829. static void hugetlbfs_put_super(struct super_block *sb)
  830. {
  831. struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
  832. if (sbi) {
  833. sb->s_fs_info = NULL;
  834. if (sbi->spool)
  835. hugepage_put_subpool(sbi->spool);
  836. kfree(sbi);
  837. }
  838. }
  839. static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  840. {
  841. if (sbinfo->free_inodes >= 0) {
  842. spin_lock(&sbinfo->stat_lock);
  843. if (unlikely(!sbinfo->free_inodes)) {
  844. spin_unlock(&sbinfo->stat_lock);
  845. return 0;
  846. }
  847. sbinfo->free_inodes--;
  848. spin_unlock(&sbinfo->stat_lock);
  849. }
  850. return 1;
  851. }
  852. static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  853. {
  854. if (sbinfo->free_inodes >= 0) {
  855. spin_lock(&sbinfo->stat_lock);
  856. sbinfo->free_inodes++;
  857. spin_unlock(&sbinfo->stat_lock);
  858. }
  859. }
  860. static struct kmem_cache *hugetlbfs_inode_cachep;
  861. static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
  862. {
  863. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
  864. struct hugetlbfs_inode_info *p;
  865. if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
  866. return NULL;
  867. p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
  868. if (unlikely(!p)) {
  869. hugetlbfs_inc_free_inodes(sbinfo);
  870. return NULL;
  871. }
  872. /*
  873. * Any time after allocation, hugetlbfs_destroy_inode can be called
  874. * for the inode. mpol_free_shared_policy is unconditionally called
  875. * as part of hugetlbfs_destroy_inode. So, initialize policy here
  876. * in case of a quick call to destroy.
  877. *
  878. * Note that the policy is initialized even if we are creating a
  879. * private inode. This simplifies hugetlbfs_destroy_inode.
  880. */
  881. mpol_shared_policy_init(&p->policy, NULL);
  882. return &p->vfs_inode;
  883. }
  884. static void hugetlbfs_i_callback(struct rcu_head *head)
  885. {
  886. struct inode *inode = container_of(head, struct inode, i_rcu);
  887. kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
  888. }
  889. static void hugetlbfs_destroy_inode(struct inode *inode)
  890. {
  891. hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
  892. mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
  893. call_rcu(&inode->i_rcu, hugetlbfs_i_callback);
  894. }
  895. static const struct address_space_operations hugetlbfs_aops = {
  896. .write_begin = hugetlbfs_write_begin,
  897. .write_end = hugetlbfs_write_end,
  898. .set_page_dirty = hugetlbfs_set_page_dirty,
  899. .migratepage = hugetlbfs_migrate_page,
  900. .error_remove_page = hugetlbfs_error_remove_page,
  901. };
  902. static void init_once(void *foo)
  903. {
  904. struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
  905. inode_init_once(&ei->vfs_inode);
  906. }
  907. const struct file_operations hugetlbfs_file_operations = {
  908. .read_iter = hugetlbfs_read_iter,
  909. .mmap = hugetlbfs_file_mmap,
  910. .fsync = noop_fsync,
  911. .get_unmapped_area = hugetlb_get_unmapped_area,
  912. .llseek = default_llseek,
  913. .fallocate = hugetlbfs_fallocate,
  914. };
  915. static const struct inode_operations hugetlbfs_dir_inode_operations = {
  916. .create = hugetlbfs_create,
  917. .lookup = simple_lookup,
  918. .link = simple_link,
  919. .unlink = simple_unlink,
  920. .symlink = hugetlbfs_symlink,
  921. .mkdir = hugetlbfs_mkdir,
  922. .rmdir = simple_rmdir,
  923. .mknod = hugetlbfs_mknod,
  924. .rename = simple_rename,
  925. .setattr = hugetlbfs_setattr,
  926. };
  927. static const struct inode_operations hugetlbfs_inode_operations = {
  928. .setattr = hugetlbfs_setattr,
  929. };
  930. static const struct super_operations hugetlbfs_ops = {
  931. .alloc_inode = hugetlbfs_alloc_inode,
  932. .destroy_inode = hugetlbfs_destroy_inode,
  933. .evict_inode = hugetlbfs_evict_inode,
  934. .statfs = hugetlbfs_statfs,
  935. .put_super = hugetlbfs_put_super,
  936. .show_options = hugetlbfs_show_options,
  937. };
  938. enum hugetlbfs_size_type { NO_SIZE, SIZE_STD, SIZE_PERCENT };
  939. /*
  940. * Convert size option passed from command line to number of huge pages
  941. * in the pool specified by hstate. Size option could be in bytes
  942. * (val_type == SIZE_STD) or percentage of the pool (val_type == SIZE_PERCENT).
  943. */
  944. static long
  945. hugetlbfs_size_to_hpages(struct hstate *h, unsigned long long size_opt,
  946. enum hugetlbfs_size_type val_type)
  947. {
  948. if (val_type == NO_SIZE)
  949. return -1;
  950. if (val_type == SIZE_PERCENT) {
  951. size_opt <<= huge_page_shift(h);
  952. size_opt *= h->max_huge_pages;
  953. do_div(size_opt, 100);
  954. }
  955. size_opt >>= huge_page_shift(h);
  956. return size_opt;
  957. }
  958. static int
  959. hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
  960. {
  961. char *p, *rest;
  962. substring_t args[MAX_OPT_ARGS];
  963. int option;
  964. unsigned long long max_size_opt = 0, min_size_opt = 0;
  965. enum hugetlbfs_size_type max_val_type = NO_SIZE, min_val_type = NO_SIZE;
  966. if (!options)
  967. return 0;
  968. while ((p = strsep(&options, ",")) != NULL) {
  969. int token;
  970. if (!*p)
  971. continue;
  972. token = match_token(p, tokens, args);
  973. switch (token) {
  974. case Opt_uid:
  975. if (match_int(&args[0], &option))
  976. goto bad_val;
  977. pconfig->uid = make_kuid(current_user_ns(), option);
  978. if (!uid_valid(pconfig->uid))
  979. goto bad_val;
  980. break;
  981. case Opt_gid:
  982. if (match_int(&args[0], &option))
  983. goto bad_val;
  984. pconfig->gid = make_kgid(current_user_ns(), option);
  985. if (!gid_valid(pconfig->gid))
  986. goto bad_val;
  987. break;
  988. case Opt_mode:
  989. if (match_octal(&args[0], &option))
  990. goto bad_val;
  991. pconfig->mode = option & 01777U;
  992. break;
  993. case Opt_size: {
  994. /* memparse() will accept a K/M/G without a digit */
  995. if (!isdigit(*args[0].from))
  996. goto bad_val;
  997. max_size_opt = memparse(args[0].from, &rest);
  998. max_val_type = SIZE_STD;
  999. if (*rest == '%')
  1000. max_val_type = SIZE_PERCENT;
  1001. break;
  1002. }
  1003. case Opt_nr_inodes:
  1004. /* memparse() will accept a K/M/G without a digit */
  1005. if (!isdigit(*args[0].from))
  1006. goto bad_val;
  1007. pconfig->nr_inodes = memparse(args[0].from, &rest);
  1008. break;
  1009. case Opt_pagesize: {
  1010. unsigned long ps;
  1011. ps = memparse(args[0].from, &rest);
  1012. pconfig->hstate = size_to_hstate(ps);
  1013. if (!pconfig->hstate) {
  1014. pr_err("Unsupported page size %lu MB\n",
  1015. ps >> 20);
  1016. return -EINVAL;
  1017. }
  1018. break;
  1019. }
  1020. case Opt_min_size: {
  1021. /* memparse() will accept a K/M/G without a digit */
  1022. if (!isdigit(*args[0].from))
  1023. goto bad_val;
  1024. min_size_opt = memparse(args[0].from, &rest);
  1025. min_val_type = SIZE_STD;
  1026. if (*rest == '%')
  1027. min_val_type = SIZE_PERCENT;
  1028. break;
  1029. }
  1030. default:
  1031. pr_err("Bad mount option: \"%s\"\n", p);
  1032. return -EINVAL;
  1033. break;
  1034. }
  1035. }
  1036. /*
  1037. * Use huge page pool size (in hstate) to convert the size
  1038. * options to number of huge pages. If NO_SIZE, -1 is returned.
  1039. */
  1040. pconfig->max_hpages = hugetlbfs_size_to_hpages(pconfig->hstate,
  1041. max_size_opt, max_val_type);
  1042. pconfig->min_hpages = hugetlbfs_size_to_hpages(pconfig->hstate,
  1043. min_size_opt, min_val_type);
  1044. /*
  1045. * If max_size was specified, then min_size must be smaller
  1046. */
  1047. if (max_val_type > NO_SIZE &&
  1048. pconfig->min_hpages > pconfig->max_hpages) {
  1049. pr_err("minimum size can not be greater than maximum size\n");
  1050. return -EINVAL;
  1051. }
  1052. return 0;
  1053. bad_val:
  1054. pr_err("Bad value '%s' for mount option '%s'\n", args[0].from, p);
  1055. return -EINVAL;
  1056. }
  1057. static int
  1058. hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
  1059. {
  1060. int ret;
  1061. struct hugetlbfs_config config;
  1062. struct hugetlbfs_sb_info *sbinfo;
  1063. config.max_hpages = -1; /* No limit on size by default */
  1064. config.nr_inodes = -1; /* No limit on number of inodes by default */
  1065. config.uid = current_fsuid();
  1066. config.gid = current_fsgid();
  1067. config.mode = 0755;
  1068. config.hstate = &default_hstate;
  1069. config.min_hpages = -1; /* No default minimum size */
  1070. ret = hugetlbfs_parse_options(data, &config);
  1071. if (ret)
  1072. return ret;
  1073. sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
  1074. if (!sbinfo)
  1075. return -ENOMEM;
  1076. sb->s_fs_info = sbinfo;
  1077. sbinfo->hstate = config.hstate;
  1078. spin_lock_init(&sbinfo->stat_lock);
  1079. sbinfo->max_inodes = config.nr_inodes;
  1080. sbinfo->free_inodes = config.nr_inodes;
  1081. sbinfo->spool = NULL;
  1082. sbinfo->uid = config.uid;
  1083. sbinfo->gid = config.gid;
  1084. sbinfo->mode = config.mode;
  1085. /*
  1086. * Allocate and initialize subpool if maximum or minimum size is
  1087. * specified. Any needed reservations (for minimim size) are taken
  1088. * taken when the subpool is created.
  1089. */
  1090. if (config.max_hpages != -1 || config.min_hpages != -1) {
  1091. sbinfo->spool = hugepage_new_subpool(config.hstate,
  1092. config.max_hpages,
  1093. config.min_hpages);
  1094. if (!sbinfo->spool)
  1095. goto out_free;
  1096. }
  1097. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1098. sb->s_blocksize = huge_page_size(config.hstate);
  1099. sb->s_blocksize_bits = huge_page_shift(config.hstate);
  1100. sb->s_magic = HUGETLBFS_MAGIC;
  1101. sb->s_op = &hugetlbfs_ops;
  1102. sb->s_time_gran = 1;
  1103. sb->s_root = d_make_root(hugetlbfs_get_root(sb, &config));
  1104. if (!sb->s_root)
  1105. goto out_free;
  1106. return 0;
  1107. out_free:
  1108. kfree(sbinfo->spool);
  1109. kfree(sbinfo);
  1110. return -ENOMEM;
  1111. }
  1112. static struct dentry *hugetlbfs_mount(struct file_system_type *fs_type,
  1113. int flags, const char *dev_name, void *data)
  1114. {
  1115. return mount_nodev(fs_type, flags, data, hugetlbfs_fill_super);
  1116. }
  1117. static struct file_system_type hugetlbfs_fs_type = {
  1118. .name = "hugetlbfs",
  1119. .mount = hugetlbfs_mount,
  1120. .kill_sb = kill_litter_super,
  1121. };
  1122. static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];
  1123. static int can_do_hugetlb_shm(void)
  1124. {
  1125. kgid_t shm_group;
  1126. shm_group = make_kgid(&init_user_ns, sysctl_hugetlb_shm_group);
  1127. return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
  1128. }
  1129. static int get_hstate_idx(int page_size_log)
  1130. {
  1131. struct hstate *h = hstate_sizelog(page_size_log);
  1132. if (!h)
  1133. return -1;
  1134. return h - hstates;
  1135. }
  1136. /*
  1137. * Note that size should be aligned to proper hugepage size in caller side,
  1138. * otherwise hugetlb_reserve_pages reserves one less hugepages than intended.
  1139. */
  1140. struct file *hugetlb_file_setup(const char *name, size_t size,
  1141. vm_flags_t acctflag, struct user_struct **user,
  1142. int creat_flags, int page_size_log)
  1143. {
  1144. struct inode *inode;
  1145. struct vfsmount *mnt;
  1146. int hstate_idx;
  1147. struct file *file;
  1148. hstate_idx = get_hstate_idx(page_size_log);
  1149. if (hstate_idx < 0)
  1150. return ERR_PTR(-ENODEV);
  1151. *user = NULL;
  1152. mnt = hugetlbfs_vfsmount[hstate_idx];
  1153. if (!mnt)
  1154. return ERR_PTR(-ENOENT);
  1155. if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
  1156. *user = current_user();
  1157. if (user_shm_lock(size, *user)) {
  1158. task_lock(current);
  1159. pr_warn_once("%s (%d): Using mlock ulimits for SHM_HUGETLB is deprecated\n",
  1160. current->comm, current->pid);
  1161. task_unlock(current);
  1162. } else {
  1163. *user = NULL;
  1164. return ERR_PTR(-EPERM);
  1165. }
  1166. }
  1167. file = ERR_PTR(-ENOSPC);
  1168. inode = hugetlbfs_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0);
  1169. if (!inode)
  1170. goto out;
  1171. if (creat_flags == HUGETLB_SHMFS_INODE)
  1172. inode->i_flags |= S_PRIVATE;
  1173. inode->i_size = size;
  1174. clear_nlink(inode);
  1175. if (hugetlb_reserve_pages(inode, 0,
  1176. size >> huge_page_shift(hstate_inode(inode)), NULL,
  1177. acctflag))
  1178. file = ERR_PTR(-ENOMEM);
  1179. else
  1180. file = alloc_file_pseudo(inode, mnt, name, O_RDWR,
  1181. &hugetlbfs_file_operations);
  1182. if (!IS_ERR(file))
  1183. return file;
  1184. iput(inode);
  1185. out:
  1186. if (*user) {
  1187. user_shm_unlock(size, *user);
  1188. *user = NULL;
  1189. }
  1190. return file;
  1191. }
  1192. static int __init init_hugetlbfs_fs(void)
  1193. {
  1194. struct hstate *h;
  1195. int error;
  1196. int i;
  1197. if (!hugepages_supported()) {
  1198. pr_info("disabling because there are no supported hugepage sizes\n");
  1199. return -ENOTSUPP;
  1200. }
  1201. error = -ENOMEM;
  1202. hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
  1203. sizeof(struct hugetlbfs_inode_info),
  1204. 0, SLAB_ACCOUNT, init_once);
  1205. if (hugetlbfs_inode_cachep == NULL)
  1206. goto out2;
  1207. error = register_filesystem(&hugetlbfs_fs_type);
  1208. if (error)
  1209. goto out;
  1210. i = 0;
  1211. for_each_hstate(h) {
  1212. char buf[50];
  1213. unsigned ps_kb = 1U << (h->order + PAGE_SHIFT - 10);
  1214. snprintf(buf, sizeof(buf), "pagesize=%uK", ps_kb);
  1215. hugetlbfs_vfsmount[i] = kern_mount_data(&hugetlbfs_fs_type,
  1216. buf);
  1217. if (IS_ERR(hugetlbfs_vfsmount[i])) {
  1218. pr_err("Cannot mount internal hugetlbfs for "
  1219. "page size %uK", ps_kb);
  1220. error = PTR_ERR(hugetlbfs_vfsmount[i]);
  1221. hugetlbfs_vfsmount[i] = NULL;
  1222. }
  1223. i++;
  1224. }
  1225. /* Non default hstates are optional */
  1226. if (!IS_ERR_OR_NULL(hugetlbfs_vfsmount[default_hstate_idx]))
  1227. return 0;
  1228. out:
  1229. kmem_cache_destroy(hugetlbfs_inode_cachep);
  1230. out2:
  1231. return error;
  1232. }
  1233. fs_initcall(init_hugetlbfs_fs)