inode.c 34 KB

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