inode.c 37 KB

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