inode.c 35 KB

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