inode.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  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, mapping, index, 0);
  380. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  381. /*
  382. * If page is mapped, it was faulted in after being
  383. * unmapped in caller. Unmap (again) now after taking
  384. * the fault mutex. The mutex will prevent faults
  385. * until we finish removing the page.
  386. *
  387. * This race can only happen in the hole punch case.
  388. * Getting here in a truncate operation is a bug.
  389. */
  390. if (unlikely(page_mapped(page))) {
  391. BUG_ON(truncate_op);
  392. i_mmap_lock_write(mapping);
  393. hugetlb_vmdelete_list(&mapping->i_mmap,
  394. index * pages_per_huge_page(h),
  395. (index + 1) * pages_per_huge_page(h));
  396. i_mmap_unlock_write(mapping);
  397. }
  398. lock_page(page);
  399. /*
  400. * We must free the huge page and remove from page
  401. * cache (remove_huge_page) BEFORE removing the
  402. * region/reserve map (hugetlb_unreserve_pages). In
  403. * rare out of memory conditions, removal of the
  404. * region/reserve map could fail. Correspondingly,
  405. * the subpool and global reserve usage count can need
  406. * to be adjusted.
  407. */
  408. VM_BUG_ON(PagePrivate(page));
  409. remove_huge_page(page);
  410. freed++;
  411. if (!truncate_op) {
  412. if (unlikely(hugetlb_unreserve_pages(inode,
  413. index, index + 1, 1)))
  414. hugetlb_fix_reserve_counts(inode);
  415. }
  416. unlock_page(page);
  417. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  418. }
  419. huge_pagevec_release(&pvec);
  420. cond_resched();
  421. }
  422. if (truncate_op)
  423. (void)hugetlb_unreserve_pages(inode, start, LONG_MAX, freed);
  424. }
  425. static void hugetlbfs_evict_inode(struct inode *inode)
  426. {
  427. struct resv_map *resv_map;
  428. remove_inode_hugepages(inode, 0, LLONG_MAX);
  429. resv_map = (struct resv_map *)inode->i_mapping->private_data;
  430. /* root inode doesn't have the resv_map, so we should check it */
  431. if (resv_map)
  432. resv_map_release(&resv_map->refs);
  433. clear_inode(inode);
  434. }
  435. static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
  436. {
  437. pgoff_t pgoff;
  438. struct address_space *mapping = inode->i_mapping;
  439. struct hstate *h = hstate_inode(inode);
  440. BUG_ON(offset & ~huge_page_mask(h));
  441. pgoff = offset >> PAGE_SHIFT;
  442. i_size_write(inode, offset);
  443. i_mmap_lock_write(mapping);
  444. if (!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))
  445. hugetlb_vmdelete_list(&mapping->i_mmap, pgoff, 0);
  446. i_mmap_unlock_write(mapping);
  447. remove_inode_hugepages(inode, offset, LLONG_MAX);
  448. return 0;
  449. }
  450. static long hugetlbfs_punch_hole(struct inode *inode, loff_t offset, loff_t len)
  451. {
  452. struct hstate *h = hstate_inode(inode);
  453. loff_t hpage_size = huge_page_size(h);
  454. loff_t hole_start, hole_end;
  455. /*
  456. * For hole punch round up the beginning offset of the hole and
  457. * round down the end.
  458. */
  459. hole_start = round_up(offset, hpage_size);
  460. hole_end = round_down(offset + len, hpage_size);
  461. if (hole_end > hole_start) {
  462. struct address_space *mapping = inode->i_mapping;
  463. struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
  464. inode_lock(inode);
  465. /* protected by i_mutex */
  466. if (info->seals & F_SEAL_WRITE) {
  467. inode_unlock(inode);
  468. return -EPERM;
  469. }
  470. i_mmap_lock_write(mapping);
  471. if (!RB_EMPTY_ROOT(&mapping->i_mmap.rb_root))
  472. hugetlb_vmdelete_list(&mapping->i_mmap,
  473. hole_start >> PAGE_SHIFT,
  474. hole_end >> PAGE_SHIFT);
  475. i_mmap_unlock_write(mapping);
  476. remove_inode_hugepages(inode, hole_start, hole_end);
  477. inode_unlock(inode);
  478. }
  479. return 0;
  480. }
  481. static long hugetlbfs_fallocate(struct file *file, int mode, loff_t offset,
  482. loff_t len)
  483. {
  484. struct inode *inode = file_inode(file);
  485. struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
  486. struct address_space *mapping = inode->i_mapping;
  487. struct hstate *h = hstate_inode(inode);
  488. struct vm_area_struct pseudo_vma;
  489. struct mm_struct *mm = current->mm;
  490. loff_t hpage_size = huge_page_size(h);
  491. unsigned long hpage_shift = huge_page_shift(h);
  492. pgoff_t start, index, end;
  493. int error;
  494. u32 hash;
  495. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  496. return -EOPNOTSUPP;
  497. if (mode & FALLOC_FL_PUNCH_HOLE)
  498. return hugetlbfs_punch_hole(inode, offset, len);
  499. /*
  500. * Default preallocate case.
  501. * For this range, start is rounded down and end is rounded up
  502. * as well as being converted to page offsets.
  503. */
  504. start = offset >> hpage_shift;
  505. end = (offset + len + hpage_size - 1) >> hpage_shift;
  506. inode_lock(inode);
  507. /* We need to check rlimit even when FALLOC_FL_KEEP_SIZE */
  508. error = inode_newsize_ok(inode, offset + len);
  509. if (error)
  510. goto out;
  511. if ((info->seals & F_SEAL_GROW) && offset + len > inode->i_size) {
  512. error = -EPERM;
  513. goto out;
  514. }
  515. /*
  516. * Initialize a pseudo vma as this is required by the huge page
  517. * allocation routines. If NUMA is configured, use page index
  518. * as input to create an allocation policy.
  519. */
  520. vma_init(&pseudo_vma, mm);
  521. pseudo_vma.vm_flags = (VM_HUGETLB | VM_MAYSHARE | VM_SHARED);
  522. pseudo_vma.vm_file = file;
  523. for (index = start; index < end; index++) {
  524. /*
  525. * This is supposed to be the vaddr where the page is being
  526. * faulted in, but we have no vaddr here.
  527. */
  528. struct page *page;
  529. unsigned long addr;
  530. int avoid_reserve = 0;
  531. cond_resched();
  532. /*
  533. * fallocate(2) manpage permits EINTR; we may have been
  534. * interrupted because we are using up too much memory.
  535. */
  536. if (signal_pending(current)) {
  537. error = -EINTR;
  538. break;
  539. }
  540. /* Set numa allocation policy based on index */
  541. hugetlb_set_vma_policy(&pseudo_vma, inode, index);
  542. /* addr is the offset within the file (zero based) */
  543. addr = index * hpage_size;
  544. /* mutex taken here, fault path and hole punch */
  545. hash = hugetlb_fault_mutex_hash(h, mapping, index, addr);
  546. mutex_lock(&hugetlb_fault_mutex_table[hash]);
  547. /* See if already present in mapping to avoid alloc/free */
  548. page = find_get_page(mapping, index);
  549. if (page) {
  550. put_page(page);
  551. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  552. hugetlb_drop_vma_policy(&pseudo_vma);
  553. continue;
  554. }
  555. /* Allocate page and add to page cache */
  556. page = alloc_huge_page(&pseudo_vma, addr, avoid_reserve);
  557. hugetlb_drop_vma_policy(&pseudo_vma);
  558. if (IS_ERR(page)) {
  559. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  560. error = PTR_ERR(page);
  561. goto out;
  562. }
  563. clear_huge_page(page, addr, pages_per_huge_page(h));
  564. __SetPageUptodate(page);
  565. error = huge_add_to_page_cache(page, mapping, index);
  566. if (unlikely(error)) {
  567. put_page(page);
  568. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  569. goto out;
  570. }
  571. mutex_unlock(&hugetlb_fault_mutex_table[hash]);
  572. set_page_huge_active(page);
  573. /*
  574. * unlock_page because locked by add_to_page_cache()
  575. * put_page() due to reference from alloc_huge_page()
  576. */
  577. unlock_page(page);
  578. put_page(page);
  579. }
  580. if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > inode->i_size)
  581. i_size_write(inode, offset + len);
  582. inode->i_ctime = current_time(inode);
  583. out:
  584. inode_unlock(inode);
  585. return error;
  586. }
  587. static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
  588. {
  589. struct inode *inode = d_inode(dentry);
  590. struct hstate *h = hstate_inode(inode);
  591. int error;
  592. unsigned int ia_valid = attr->ia_valid;
  593. struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
  594. BUG_ON(!inode);
  595. error = setattr_prepare(dentry, attr);
  596. if (error)
  597. return error;
  598. if (ia_valid & ATTR_SIZE) {
  599. loff_t oldsize = inode->i_size;
  600. loff_t newsize = attr->ia_size;
  601. if (newsize & ~huge_page_mask(h))
  602. return -EINVAL;
  603. /* protected by i_mutex */
  604. if ((newsize < oldsize && (info->seals & F_SEAL_SHRINK)) ||
  605. (newsize > oldsize && (info->seals & F_SEAL_GROW)))
  606. return -EPERM;
  607. error = hugetlb_vmtruncate(inode, newsize);
  608. if (error)
  609. return error;
  610. }
  611. setattr_copy(inode, attr);
  612. mark_inode_dirty(inode);
  613. return 0;
  614. }
  615. static struct inode *hugetlbfs_get_root(struct super_block *sb,
  616. struct hugetlbfs_config *config)
  617. {
  618. struct inode *inode;
  619. inode = new_inode(sb);
  620. if (inode) {
  621. inode->i_ino = get_next_ino();
  622. inode->i_mode = S_IFDIR | config->mode;
  623. inode->i_uid = config->uid;
  624. inode->i_gid = config->gid;
  625. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  626. inode->i_op = &hugetlbfs_dir_inode_operations;
  627. inode->i_fop = &simple_dir_operations;
  628. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  629. inc_nlink(inode);
  630. lockdep_annotate_inode_mutex_key(inode);
  631. }
  632. return inode;
  633. }
  634. /*
  635. * Hugetlbfs is not reclaimable; therefore its i_mmap_rwsem will never
  636. * be taken from reclaim -- unlike regular filesystems. This needs an
  637. * annotation because huge_pmd_share() does an allocation under hugetlb's
  638. * i_mmap_rwsem.
  639. */
  640. static struct lock_class_key hugetlbfs_i_mmap_rwsem_key;
  641. static struct inode *hugetlbfs_get_inode(struct super_block *sb,
  642. struct inode *dir,
  643. umode_t mode, dev_t dev)
  644. {
  645. struct inode *inode;
  646. struct resv_map *resv_map = NULL;
  647. /*
  648. * Reserve maps are only needed for inodes that can have associated
  649. * page allocations.
  650. */
  651. if (S_ISREG(mode) || S_ISLNK(mode)) {
  652. resv_map = resv_map_alloc();
  653. if (!resv_map)
  654. return NULL;
  655. }
  656. inode = new_inode(sb);
  657. if (inode) {
  658. struct hugetlbfs_inode_info *info = HUGETLBFS_I(inode);
  659. inode->i_ino = get_next_ino();
  660. inode_init_owner(inode, dir, mode);
  661. lockdep_set_class(&inode->i_mapping->i_mmap_rwsem,
  662. &hugetlbfs_i_mmap_rwsem_key);
  663. inode->i_mapping->a_ops = &hugetlbfs_aops;
  664. inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
  665. inode->i_mapping->private_data = resv_map;
  666. info->seals = F_SEAL_SEAL;
  667. switch (mode & S_IFMT) {
  668. default:
  669. init_special_inode(inode, mode, dev);
  670. break;
  671. case S_IFREG:
  672. inode->i_op = &hugetlbfs_inode_operations;
  673. inode->i_fop = &hugetlbfs_file_operations;
  674. break;
  675. case S_IFDIR:
  676. inode->i_op = &hugetlbfs_dir_inode_operations;
  677. inode->i_fop = &simple_dir_operations;
  678. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  679. inc_nlink(inode);
  680. break;
  681. case S_IFLNK:
  682. inode->i_op = &page_symlink_inode_operations;
  683. inode_nohighmem(inode);
  684. break;
  685. }
  686. lockdep_annotate_inode_mutex_key(inode);
  687. } else {
  688. if (resv_map)
  689. kref_put(&resv_map->refs, resv_map_release);
  690. }
  691. return inode;
  692. }
  693. /*
  694. * File creation. Allocate an inode, and we're done..
  695. */
  696. static int hugetlbfs_mknod(struct inode *dir,
  697. struct dentry *dentry, umode_t mode, dev_t dev)
  698. {
  699. struct inode *inode;
  700. int error = -ENOSPC;
  701. inode = hugetlbfs_get_inode(dir->i_sb, dir, mode, dev);
  702. if (inode) {
  703. dir->i_ctime = dir->i_mtime = current_time(dir);
  704. d_instantiate(dentry, inode);
  705. dget(dentry); /* Extra count - pin the dentry in core */
  706. error = 0;
  707. }
  708. return error;
  709. }
  710. static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  711. {
  712. int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
  713. if (!retval)
  714. inc_nlink(dir);
  715. return retval;
  716. }
  717. static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  718. {
  719. return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
  720. }
  721. static int hugetlbfs_symlink(struct inode *dir,
  722. struct dentry *dentry, const char *symname)
  723. {
  724. struct inode *inode;
  725. int error = -ENOSPC;
  726. inode = hugetlbfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
  727. if (inode) {
  728. int l = strlen(symname)+1;
  729. error = page_symlink(inode, symname, l);
  730. if (!error) {
  731. d_instantiate(dentry, inode);
  732. dget(dentry);
  733. } else
  734. iput(inode);
  735. }
  736. dir->i_ctime = dir->i_mtime = current_time(dir);
  737. return error;
  738. }
  739. /*
  740. * mark the head page dirty
  741. */
  742. static int hugetlbfs_set_page_dirty(struct page *page)
  743. {
  744. struct page *head = compound_head(page);
  745. SetPageDirty(head);
  746. return 0;
  747. }
  748. static int hugetlbfs_migrate_page(struct address_space *mapping,
  749. struct page *newpage, struct page *page,
  750. enum migrate_mode mode)
  751. {
  752. int rc;
  753. rc = migrate_huge_page_move_mapping(mapping, newpage, page);
  754. if (rc != MIGRATEPAGE_SUCCESS)
  755. return rc;
  756. /*
  757. * page_private is subpool pointer in hugetlb pages. Transfer to
  758. * new page. PagePrivate is not associated with page_private for
  759. * hugetlb pages and can not be set here as only page_huge_active
  760. * pages can be migrated.
  761. */
  762. if (page_private(page)) {
  763. set_page_private(newpage, page_private(page));
  764. set_page_private(page, 0);
  765. }
  766. if (mode != MIGRATE_SYNC_NO_COPY)
  767. migrate_page_copy(newpage, page);
  768. else
  769. migrate_page_states(newpage, page);
  770. return MIGRATEPAGE_SUCCESS;
  771. }
  772. static int hugetlbfs_error_remove_page(struct address_space *mapping,
  773. struct page *page)
  774. {
  775. struct inode *inode = mapping->host;
  776. pgoff_t index = page->index;
  777. remove_huge_page(page);
  778. if (unlikely(hugetlb_unreserve_pages(inode, index, index + 1, 1)))
  779. hugetlb_fix_reserve_counts(inode);
  780. return 0;
  781. }
  782. /*
  783. * Display the mount options in /proc/mounts.
  784. */
  785. static int hugetlbfs_show_options(struct seq_file *m, struct dentry *root)
  786. {
  787. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(root->d_sb);
  788. struct hugepage_subpool *spool = sbinfo->spool;
  789. unsigned long hpage_size = huge_page_size(sbinfo->hstate);
  790. unsigned hpage_shift = huge_page_shift(sbinfo->hstate);
  791. char mod;
  792. if (!uid_eq(sbinfo->uid, GLOBAL_ROOT_UID))
  793. seq_printf(m, ",uid=%u",
  794. from_kuid_munged(&init_user_ns, sbinfo->uid));
  795. if (!gid_eq(sbinfo->gid, GLOBAL_ROOT_GID))
  796. seq_printf(m, ",gid=%u",
  797. from_kgid_munged(&init_user_ns, sbinfo->gid));
  798. if (sbinfo->mode != 0755)
  799. seq_printf(m, ",mode=%o", sbinfo->mode);
  800. if (sbinfo->max_inodes != -1)
  801. seq_printf(m, ",nr_inodes=%lu", sbinfo->max_inodes);
  802. hpage_size /= 1024;
  803. mod = 'K';
  804. if (hpage_size >= 1024) {
  805. hpage_size /= 1024;
  806. mod = 'M';
  807. }
  808. seq_printf(m, ",pagesize=%lu%c", hpage_size, mod);
  809. if (spool) {
  810. if (spool->max_hpages != -1)
  811. seq_printf(m, ",size=%llu",
  812. (unsigned long long)spool->max_hpages << hpage_shift);
  813. if (spool->min_hpages != -1)
  814. seq_printf(m, ",min_size=%llu",
  815. (unsigned long long)spool->min_hpages << hpage_shift);
  816. }
  817. return 0;
  818. }
  819. static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  820. {
  821. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
  822. struct hstate *h = hstate_inode(d_inode(dentry));
  823. buf->f_type = HUGETLBFS_MAGIC;
  824. buf->f_bsize = huge_page_size(h);
  825. if (sbinfo) {
  826. spin_lock(&sbinfo->stat_lock);
  827. /* If no limits set, just report 0 for max/free/used
  828. * blocks, like simple_statfs() */
  829. if (sbinfo->spool) {
  830. long free_pages;
  831. spin_lock(&sbinfo->spool->lock);
  832. buf->f_blocks = sbinfo->spool->max_hpages;
  833. free_pages = sbinfo->spool->max_hpages
  834. - sbinfo->spool->used_hpages;
  835. buf->f_bavail = buf->f_bfree = free_pages;
  836. spin_unlock(&sbinfo->spool->lock);
  837. buf->f_files = sbinfo->max_inodes;
  838. buf->f_ffree = sbinfo->free_inodes;
  839. }
  840. spin_unlock(&sbinfo->stat_lock);
  841. }
  842. buf->f_namelen = NAME_MAX;
  843. return 0;
  844. }
  845. static void hugetlbfs_put_super(struct super_block *sb)
  846. {
  847. struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
  848. if (sbi) {
  849. sb->s_fs_info = NULL;
  850. if (sbi->spool)
  851. hugepage_put_subpool(sbi->spool);
  852. kfree(sbi);
  853. }
  854. }
  855. static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  856. {
  857. if (sbinfo->free_inodes >= 0) {
  858. spin_lock(&sbinfo->stat_lock);
  859. if (unlikely(!sbinfo->free_inodes)) {
  860. spin_unlock(&sbinfo->stat_lock);
  861. return 0;
  862. }
  863. sbinfo->free_inodes--;
  864. spin_unlock(&sbinfo->stat_lock);
  865. }
  866. return 1;
  867. }
  868. static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  869. {
  870. if (sbinfo->free_inodes >= 0) {
  871. spin_lock(&sbinfo->stat_lock);
  872. sbinfo->free_inodes++;
  873. spin_unlock(&sbinfo->stat_lock);
  874. }
  875. }
  876. static struct kmem_cache *hugetlbfs_inode_cachep;
  877. static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
  878. {
  879. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
  880. struct hugetlbfs_inode_info *p;
  881. if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
  882. return NULL;
  883. p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
  884. if (unlikely(!p)) {
  885. hugetlbfs_inc_free_inodes(sbinfo);
  886. return NULL;
  887. }
  888. /*
  889. * Any time after allocation, hugetlbfs_destroy_inode can be called
  890. * for the inode. mpol_free_shared_policy is unconditionally called
  891. * as part of hugetlbfs_destroy_inode. So, initialize policy here
  892. * in case of a quick call to destroy.
  893. *
  894. * Note that the policy is initialized even if we are creating a
  895. * private inode. This simplifies hugetlbfs_destroy_inode.
  896. */
  897. mpol_shared_policy_init(&p->policy, NULL);
  898. return &p->vfs_inode;
  899. }
  900. static void hugetlbfs_i_callback(struct rcu_head *head)
  901. {
  902. struct inode *inode = container_of(head, struct inode, i_rcu);
  903. kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
  904. }
  905. static void hugetlbfs_destroy_inode(struct inode *inode)
  906. {
  907. hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
  908. mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
  909. call_rcu(&inode->i_rcu, hugetlbfs_i_callback);
  910. }
  911. static const struct address_space_operations hugetlbfs_aops = {
  912. .write_begin = hugetlbfs_write_begin,
  913. .write_end = hugetlbfs_write_end,
  914. .set_page_dirty = hugetlbfs_set_page_dirty,
  915. .migratepage = hugetlbfs_migrate_page,
  916. .error_remove_page = hugetlbfs_error_remove_page,
  917. };
  918. static void init_once(void *foo)
  919. {
  920. struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
  921. inode_init_once(&ei->vfs_inode);
  922. }
  923. const struct file_operations hugetlbfs_file_operations = {
  924. .read_iter = hugetlbfs_read_iter,
  925. .mmap = hugetlbfs_file_mmap,
  926. .fsync = noop_fsync,
  927. .get_unmapped_area = hugetlb_get_unmapped_area,
  928. .llseek = default_llseek,
  929. .fallocate = hugetlbfs_fallocate,
  930. };
  931. static const struct inode_operations hugetlbfs_dir_inode_operations = {
  932. .create = hugetlbfs_create,
  933. .lookup = simple_lookup,
  934. .link = simple_link,
  935. .unlink = simple_unlink,
  936. .symlink = hugetlbfs_symlink,
  937. .mkdir = hugetlbfs_mkdir,
  938. .rmdir = simple_rmdir,
  939. .mknod = hugetlbfs_mknod,
  940. .rename = simple_rename,
  941. .setattr = hugetlbfs_setattr,
  942. };
  943. static const struct inode_operations hugetlbfs_inode_operations = {
  944. .setattr = hugetlbfs_setattr,
  945. };
  946. static const struct super_operations hugetlbfs_ops = {
  947. .alloc_inode = hugetlbfs_alloc_inode,
  948. .destroy_inode = hugetlbfs_destroy_inode,
  949. .evict_inode = hugetlbfs_evict_inode,
  950. .statfs = hugetlbfs_statfs,
  951. .put_super = hugetlbfs_put_super,
  952. .show_options = hugetlbfs_show_options,
  953. };
  954. enum hugetlbfs_size_type { NO_SIZE, SIZE_STD, SIZE_PERCENT };
  955. /*
  956. * Convert size option passed from command line to number of huge pages
  957. * in the pool specified by hstate. Size option could be in bytes
  958. * (val_type == SIZE_STD) or percentage of the pool (val_type == SIZE_PERCENT).
  959. */
  960. static long
  961. hugetlbfs_size_to_hpages(struct hstate *h, unsigned long long size_opt,
  962. enum hugetlbfs_size_type val_type)
  963. {
  964. if (val_type == NO_SIZE)
  965. return -1;
  966. if (val_type == SIZE_PERCENT) {
  967. size_opt <<= huge_page_shift(h);
  968. size_opt *= h->max_huge_pages;
  969. do_div(size_opt, 100);
  970. }
  971. size_opt >>= huge_page_shift(h);
  972. return size_opt;
  973. }
  974. static int
  975. hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
  976. {
  977. char *p, *rest;
  978. substring_t args[MAX_OPT_ARGS];
  979. int option;
  980. unsigned long long max_size_opt = 0, min_size_opt = 0;
  981. enum hugetlbfs_size_type max_val_type = NO_SIZE, min_val_type = NO_SIZE;
  982. if (!options)
  983. return 0;
  984. while ((p = strsep(&options, ",")) != NULL) {
  985. int token;
  986. if (!*p)
  987. continue;
  988. token = match_token(p, tokens, args);
  989. switch (token) {
  990. case Opt_uid:
  991. if (match_int(&args[0], &option))
  992. goto bad_val;
  993. pconfig->uid = make_kuid(current_user_ns(), option);
  994. if (!uid_valid(pconfig->uid))
  995. goto bad_val;
  996. break;
  997. case Opt_gid:
  998. if (match_int(&args[0], &option))
  999. goto bad_val;
  1000. pconfig->gid = make_kgid(current_user_ns(), option);
  1001. if (!gid_valid(pconfig->gid))
  1002. goto bad_val;
  1003. break;
  1004. case Opt_mode:
  1005. if (match_octal(&args[0], &option))
  1006. goto bad_val;
  1007. pconfig->mode = option & 01777U;
  1008. break;
  1009. case Opt_size: {
  1010. /* memparse() will accept a K/M/G without a digit */
  1011. if (!isdigit(*args[0].from))
  1012. goto bad_val;
  1013. max_size_opt = memparse(args[0].from, &rest);
  1014. max_val_type = SIZE_STD;
  1015. if (*rest == '%')
  1016. max_val_type = SIZE_PERCENT;
  1017. break;
  1018. }
  1019. case Opt_nr_inodes:
  1020. /* memparse() will accept a K/M/G without a digit */
  1021. if (!isdigit(*args[0].from))
  1022. goto bad_val;
  1023. pconfig->nr_inodes = memparse(args[0].from, &rest);
  1024. break;
  1025. case Opt_pagesize: {
  1026. unsigned long ps;
  1027. ps = memparse(args[0].from, &rest);
  1028. pconfig->hstate = size_to_hstate(ps);
  1029. if (!pconfig->hstate) {
  1030. pr_err("Unsupported page size %lu MB\n",
  1031. ps >> 20);
  1032. return -EINVAL;
  1033. }
  1034. break;
  1035. }
  1036. case Opt_min_size: {
  1037. /* memparse() will accept a K/M/G without a digit */
  1038. if (!isdigit(*args[0].from))
  1039. goto bad_val;
  1040. min_size_opt = memparse(args[0].from, &rest);
  1041. min_val_type = SIZE_STD;
  1042. if (*rest == '%')
  1043. min_val_type = SIZE_PERCENT;
  1044. break;
  1045. }
  1046. default:
  1047. pr_err("Bad mount option: \"%s\"\n", p);
  1048. return -EINVAL;
  1049. break;
  1050. }
  1051. }
  1052. /*
  1053. * Use huge page pool size (in hstate) to convert the size
  1054. * options to number of huge pages. If NO_SIZE, -1 is returned.
  1055. */
  1056. pconfig->max_hpages = hugetlbfs_size_to_hpages(pconfig->hstate,
  1057. max_size_opt, max_val_type);
  1058. pconfig->min_hpages = hugetlbfs_size_to_hpages(pconfig->hstate,
  1059. min_size_opt, min_val_type);
  1060. /*
  1061. * If max_size was specified, then min_size must be smaller
  1062. */
  1063. if (max_val_type > NO_SIZE &&
  1064. pconfig->min_hpages > pconfig->max_hpages) {
  1065. pr_err("minimum size can not be greater than maximum size\n");
  1066. return -EINVAL;
  1067. }
  1068. return 0;
  1069. bad_val:
  1070. pr_err("Bad value '%s' for mount option '%s'\n", args[0].from, p);
  1071. return -EINVAL;
  1072. }
  1073. static int
  1074. hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
  1075. {
  1076. int ret;
  1077. struct hugetlbfs_config config;
  1078. struct hugetlbfs_sb_info *sbinfo;
  1079. config.max_hpages = -1; /* No limit on size by default */
  1080. config.nr_inodes = -1; /* No limit on number of inodes by default */
  1081. config.uid = current_fsuid();
  1082. config.gid = current_fsgid();
  1083. config.mode = 0755;
  1084. config.hstate = &default_hstate;
  1085. config.min_hpages = -1; /* No default minimum size */
  1086. ret = hugetlbfs_parse_options(data, &config);
  1087. if (ret)
  1088. return ret;
  1089. sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
  1090. if (!sbinfo)
  1091. return -ENOMEM;
  1092. sb->s_fs_info = sbinfo;
  1093. sbinfo->hstate = config.hstate;
  1094. spin_lock_init(&sbinfo->stat_lock);
  1095. sbinfo->max_inodes = config.nr_inodes;
  1096. sbinfo->free_inodes = config.nr_inodes;
  1097. sbinfo->spool = NULL;
  1098. sbinfo->uid = config.uid;
  1099. sbinfo->gid = config.gid;
  1100. sbinfo->mode = config.mode;
  1101. /*
  1102. * Allocate and initialize subpool if maximum or minimum size is
  1103. * specified. Any needed reservations (for minimim size) are taken
  1104. * taken when the subpool is created.
  1105. */
  1106. if (config.max_hpages != -1 || config.min_hpages != -1) {
  1107. sbinfo->spool = hugepage_new_subpool(config.hstate,
  1108. config.max_hpages,
  1109. config.min_hpages);
  1110. if (!sbinfo->spool)
  1111. goto out_free;
  1112. }
  1113. sb->s_maxbytes = MAX_LFS_FILESIZE;
  1114. sb->s_blocksize = huge_page_size(config.hstate);
  1115. sb->s_blocksize_bits = huge_page_shift(config.hstate);
  1116. sb->s_magic = HUGETLBFS_MAGIC;
  1117. sb->s_op = &hugetlbfs_ops;
  1118. sb->s_time_gran = 1;
  1119. sb->s_root = d_make_root(hugetlbfs_get_root(sb, &config));
  1120. if (!sb->s_root)
  1121. goto out_free;
  1122. return 0;
  1123. out_free:
  1124. kfree(sbinfo->spool);
  1125. kfree(sbinfo);
  1126. return -ENOMEM;
  1127. }
  1128. static struct dentry *hugetlbfs_mount(struct file_system_type *fs_type,
  1129. int flags, const char *dev_name, void *data)
  1130. {
  1131. return mount_nodev(fs_type, flags, data, hugetlbfs_fill_super);
  1132. }
  1133. static struct file_system_type hugetlbfs_fs_type = {
  1134. .name = "hugetlbfs",
  1135. .mount = hugetlbfs_mount,
  1136. .kill_sb = kill_litter_super,
  1137. };
  1138. static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];
  1139. static int can_do_hugetlb_shm(void)
  1140. {
  1141. kgid_t shm_group;
  1142. shm_group = make_kgid(&init_user_ns, sysctl_hugetlb_shm_group);
  1143. return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
  1144. }
  1145. static int get_hstate_idx(int page_size_log)
  1146. {
  1147. struct hstate *h = hstate_sizelog(page_size_log);
  1148. if (!h)
  1149. return -1;
  1150. return h - hstates;
  1151. }
  1152. /*
  1153. * Note that size should be aligned to proper hugepage size in caller side,
  1154. * otherwise hugetlb_reserve_pages reserves one less hugepages than intended.
  1155. */
  1156. struct file *hugetlb_file_setup(const char *name, size_t size,
  1157. vm_flags_t acctflag, struct user_struct **user,
  1158. int creat_flags, int page_size_log)
  1159. {
  1160. struct inode *inode;
  1161. struct vfsmount *mnt;
  1162. int hstate_idx;
  1163. struct file *file;
  1164. hstate_idx = get_hstate_idx(page_size_log);
  1165. if (hstate_idx < 0)
  1166. return ERR_PTR(-ENODEV);
  1167. *user = NULL;
  1168. mnt = hugetlbfs_vfsmount[hstate_idx];
  1169. if (!mnt)
  1170. return ERR_PTR(-ENOENT);
  1171. if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
  1172. *user = current_user();
  1173. if (user_shm_lock(size, *user)) {
  1174. task_lock(current);
  1175. pr_warn_once("%s (%d): Using mlock ulimits for SHM_HUGETLB is deprecated\n",
  1176. current->comm, current->pid);
  1177. task_unlock(current);
  1178. } else {
  1179. *user = NULL;
  1180. return ERR_PTR(-EPERM);
  1181. }
  1182. }
  1183. file = ERR_PTR(-ENOSPC);
  1184. inode = hugetlbfs_get_inode(mnt->mnt_sb, NULL, S_IFREG | S_IRWXUGO, 0);
  1185. if (!inode)
  1186. goto out;
  1187. if (creat_flags == HUGETLB_SHMFS_INODE)
  1188. inode->i_flags |= S_PRIVATE;
  1189. inode->i_size = size;
  1190. clear_nlink(inode);
  1191. if (hugetlb_reserve_pages(inode, 0,
  1192. size >> huge_page_shift(hstate_inode(inode)), NULL,
  1193. acctflag))
  1194. file = ERR_PTR(-ENOMEM);
  1195. else
  1196. file = alloc_file_pseudo(inode, mnt, name, O_RDWR,
  1197. &hugetlbfs_file_operations);
  1198. if (!IS_ERR(file))
  1199. return file;
  1200. iput(inode);
  1201. out:
  1202. if (*user) {
  1203. user_shm_unlock(size, *user);
  1204. *user = NULL;
  1205. }
  1206. return file;
  1207. }
  1208. static int __init init_hugetlbfs_fs(void)
  1209. {
  1210. struct hstate *h;
  1211. int error;
  1212. int i;
  1213. if (!hugepages_supported()) {
  1214. pr_info("disabling because there are no supported hugepage sizes\n");
  1215. return -ENOTSUPP;
  1216. }
  1217. error = -ENOMEM;
  1218. hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
  1219. sizeof(struct hugetlbfs_inode_info),
  1220. 0, SLAB_ACCOUNT, init_once);
  1221. if (hugetlbfs_inode_cachep == NULL)
  1222. goto out2;
  1223. error = register_filesystem(&hugetlbfs_fs_type);
  1224. if (error)
  1225. goto out;
  1226. i = 0;
  1227. for_each_hstate(h) {
  1228. char buf[50];
  1229. unsigned ps_kb = 1U << (h->order + PAGE_SHIFT - 10);
  1230. snprintf(buf, sizeof(buf), "pagesize=%uK", ps_kb);
  1231. hugetlbfs_vfsmount[i] = kern_mount_data(&hugetlbfs_fs_type,
  1232. buf);
  1233. if (IS_ERR(hugetlbfs_vfsmount[i])) {
  1234. pr_err("Cannot mount internal hugetlbfs for "
  1235. "page size %uK", ps_kb);
  1236. error = PTR_ERR(hugetlbfs_vfsmount[i]);
  1237. hugetlbfs_vfsmount[i] = NULL;
  1238. }
  1239. i++;
  1240. }
  1241. /* Non default hstates are optional */
  1242. if (!IS_ERR_OR_NULL(hugetlbfs_vfsmount[default_hstate_idx]))
  1243. return 0;
  1244. out:
  1245. kmem_cache_destroy(hugetlbfs_inode_cachep);
  1246. out2:
  1247. return error;
  1248. }
  1249. fs_initcall(init_hugetlbfs_fs)