inode.c 37 KB

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