inode.c 35 KB

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