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