inode.c 36 KB

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