inode.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101
  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/fs.h>
  14. #include <linux/mount.h>
  15. #include <linux/file.h>
  16. #include <linux/kernel.h>
  17. #include <linux/writeback.h>
  18. #include <linux/pagemap.h>
  19. #include <linux/highmem.h>
  20. #include <linux/init.h>
  21. #include <linux/string.h>
  22. #include <linux/capability.h>
  23. #include <linux/ctype.h>
  24. #include <linux/backing-dev.h>
  25. #include <linux/hugetlb.h>
  26. #include <linux/pagevec.h>
  27. #include <linux/parser.h>
  28. #include <linux/mman.h>
  29. #include <linux/slab.h>
  30. #include <linux/dnotify.h>
  31. #include <linux/statfs.h>
  32. #include <linux/security.h>
  33. #include <linux/magic.h>
  34. #include <linux/migrate.h>
  35. #include <asm/uaccess.h>
  36. static const struct super_operations hugetlbfs_ops;
  37. static const struct address_space_operations hugetlbfs_aops;
  38. const struct file_operations hugetlbfs_file_operations;
  39. static const struct inode_operations hugetlbfs_dir_inode_operations;
  40. static const struct inode_operations hugetlbfs_inode_operations;
  41. struct hugetlbfs_config {
  42. kuid_t uid;
  43. kgid_t gid;
  44. umode_t mode;
  45. long nr_blocks;
  46. long nr_inodes;
  47. struct hstate *hstate;
  48. };
  49. struct hugetlbfs_inode_info {
  50. struct shared_policy policy;
  51. struct inode vfs_inode;
  52. };
  53. static inline struct hugetlbfs_inode_info *HUGETLBFS_I(struct inode *inode)
  54. {
  55. return container_of(inode, struct hugetlbfs_inode_info, vfs_inode);
  56. }
  57. static struct backing_dev_info hugetlbfs_backing_dev_info = {
  58. .name = "hugetlbfs",
  59. .ra_pages = 0, /* No readahead */
  60. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
  61. };
  62. int sysctl_hugetlb_shm_group;
  63. enum {
  64. Opt_size, Opt_nr_inodes,
  65. Opt_mode, Opt_uid, Opt_gid,
  66. Opt_pagesize,
  67. Opt_err,
  68. };
  69. static const match_table_t tokens = {
  70. {Opt_size, "size=%s"},
  71. {Opt_nr_inodes, "nr_inodes=%s"},
  72. {Opt_mode, "mode=%o"},
  73. {Opt_uid, "uid=%u"},
  74. {Opt_gid, "gid=%u"},
  75. {Opt_pagesize, "pagesize=%s"},
  76. {Opt_err, NULL},
  77. };
  78. static void huge_pagevec_release(struct pagevec *pvec)
  79. {
  80. int i;
  81. for (i = 0; i < pagevec_count(pvec); ++i)
  82. put_page(pvec->pages[i]);
  83. pagevec_reinit(pvec);
  84. }
  85. static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
  86. {
  87. struct inode *inode = file_inode(file);
  88. loff_t len, vma_len;
  89. int ret;
  90. struct hstate *h = hstate_file(file);
  91. /*
  92. * vma address alignment (but not the pgoff alignment) has
  93. * already been checked by prepare_hugepage_range. If you add
  94. * any error returns here, do so after setting VM_HUGETLB, so
  95. * is_vm_hugetlb_page tests below unmap_region go the right
  96. * way when do_mmap_pgoff unwinds (may be important on powerpc
  97. * and ia64).
  98. */
  99. vma->vm_flags |= VM_HUGETLB | VM_DONTEXPAND;
  100. vma->vm_ops = &hugetlb_vm_ops;
  101. if (vma->vm_pgoff & (~huge_page_mask(h) >> PAGE_SHIFT))
  102. return -EINVAL;
  103. vma_len = (loff_t)(vma->vm_end - vma->vm_start);
  104. mutex_lock(&inode->i_mutex);
  105. file_accessed(file);
  106. ret = -ENOMEM;
  107. len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  108. if (hugetlb_reserve_pages(inode,
  109. vma->vm_pgoff >> huge_page_order(h),
  110. len >> huge_page_shift(h), vma,
  111. vma->vm_flags))
  112. goto out;
  113. ret = 0;
  114. hugetlb_prefault_arch_hook(vma->vm_mm);
  115. if (vma->vm_flags & VM_WRITE && inode->i_size < len)
  116. inode->i_size = len;
  117. out:
  118. mutex_unlock(&inode->i_mutex);
  119. return ret;
  120. }
  121. /*
  122. * Called under down_write(mmap_sem).
  123. */
  124. #ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
  125. static unsigned long
  126. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  127. unsigned long len, unsigned long pgoff, unsigned long flags)
  128. {
  129. struct mm_struct *mm = current->mm;
  130. struct vm_area_struct *vma;
  131. struct hstate *h = hstate_file(file);
  132. struct vm_unmapped_area_info info;
  133. if (len & ~huge_page_mask(h))
  134. return -EINVAL;
  135. if (len > TASK_SIZE)
  136. return -ENOMEM;
  137. if (flags & MAP_FIXED) {
  138. if (prepare_hugepage_range(file, addr, len))
  139. return -EINVAL;
  140. return addr;
  141. }
  142. if (addr) {
  143. addr = ALIGN(addr, huge_page_size(h));
  144. vma = find_vma(mm, addr);
  145. if (TASK_SIZE - len >= addr &&
  146. (!vma || addr + len <= vma->vm_start))
  147. return addr;
  148. }
  149. info.flags = 0;
  150. info.length = len;
  151. info.low_limit = TASK_UNMAPPED_BASE;
  152. info.high_limit = TASK_SIZE;
  153. info.align_mask = PAGE_MASK & ~huge_page_mask(h);
  154. info.align_offset = 0;
  155. return vm_unmapped_area(&info);
  156. }
  157. #endif
  158. static int
  159. hugetlbfs_read_actor(struct page *page, unsigned long offset,
  160. char __user *buf, unsigned long count,
  161. unsigned long size)
  162. {
  163. char *kaddr;
  164. unsigned long left, copied = 0;
  165. int i, chunksize;
  166. if (size > count)
  167. size = count;
  168. /* Find which 4k chunk and offset with in that chunk */
  169. i = offset >> PAGE_CACHE_SHIFT;
  170. offset = offset & ~PAGE_CACHE_MASK;
  171. while (size) {
  172. chunksize = PAGE_CACHE_SIZE;
  173. if (offset)
  174. chunksize -= offset;
  175. if (chunksize > size)
  176. chunksize = size;
  177. kaddr = kmap(&page[i]);
  178. left = __copy_to_user(buf, kaddr + offset, chunksize);
  179. kunmap(&page[i]);
  180. if (left) {
  181. copied += (chunksize - left);
  182. break;
  183. }
  184. offset = 0;
  185. size -= chunksize;
  186. buf += chunksize;
  187. copied += chunksize;
  188. i++;
  189. }
  190. return copied ? copied : -EFAULT;
  191. }
  192. /*
  193. * Support for read() - Find the page attached to f_mapping and copy out the
  194. * data. Its *very* similar to do_generic_mapping_read(), we can't use that
  195. * since it has PAGE_CACHE_SIZE assumptions.
  196. */
  197. static ssize_t hugetlbfs_read(struct file *filp, char __user *buf,
  198. size_t len, loff_t *ppos)
  199. {
  200. struct hstate *h = hstate_file(filp);
  201. struct address_space *mapping = filp->f_mapping;
  202. struct inode *inode = mapping->host;
  203. unsigned long index = *ppos >> huge_page_shift(h);
  204. unsigned long offset = *ppos & ~huge_page_mask(h);
  205. unsigned long end_index;
  206. loff_t isize;
  207. ssize_t retval = 0;
  208. /* validate length */
  209. if (len == 0)
  210. goto out;
  211. for (;;) {
  212. struct page *page;
  213. unsigned long nr, ret;
  214. int ra;
  215. /* nr is the maximum number of bytes to copy from this page */
  216. nr = huge_page_size(h);
  217. isize = i_size_read(inode);
  218. if (!isize)
  219. goto out;
  220. end_index = (isize - 1) >> huge_page_shift(h);
  221. if (index >= end_index) {
  222. if (index > end_index)
  223. goto out;
  224. nr = ((isize - 1) & ~huge_page_mask(h)) + 1;
  225. if (nr <= offset)
  226. goto out;
  227. }
  228. nr = nr - offset;
  229. /* Find the page */
  230. page = find_lock_page(mapping, index);
  231. if (unlikely(page == NULL)) {
  232. /*
  233. * We have a HOLE, zero out the user-buffer for the
  234. * length of the hole or request.
  235. */
  236. ret = len < nr ? len : nr;
  237. if (clear_user(buf, ret))
  238. ra = -EFAULT;
  239. else
  240. ra = 0;
  241. } else {
  242. unlock_page(page);
  243. /*
  244. * We have the page, copy it to user space buffer.
  245. */
  246. ra = hugetlbfs_read_actor(page, offset, buf, len, nr);
  247. ret = ra;
  248. page_cache_release(page);
  249. }
  250. if (ra < 0) {
  251. if (retval == 0)
  252. retval = ra;
  253. goto out;
  254. }
  255. offset += ret;
  256. retval += ret;
  257. len -= ret;
  258. index += offset >> huge_page_shift(h);
  259. offset &= ~huge_page_mask(h);
  260. /* short read or no more work */
  261. if ((ret != nr) || (len == 0))
  262. break;
  263. }
  264. out:
  265. *ppos = ((loff_t)index << huge_page_shift(h)) + offset;
  266. return retval;
  267. }
  268. static int hugetlbfs_write_begin(struct file *file,
  269. struct address_space *mapping,
  270. loff_t pos, unsigned len, unsigned flags,
  271. struct page **pagep, void **fsdata)
  272. {
  273. return -EINVAL;
  274. }
  275. static int hugetlbfs_write_end(struct file *file, struct address_space *mapping,
  276. loff_t pos, unsigned len, unsigned copied,
  277. struct page *page, void *fsdata)
  278. {
  279. BUG();
  280. return -EINVAL;
  281. }
  282. static void truncate_huge_page(struct page *page)
  283. {
  284. cancel_dirty_page(page, /* No IO accounting for huge pages? */0);
  285. ClearPageUptodate(page);
  286. delete_from_page_cache(page);
  287. }
  288. static void truncate_hugepages(struct inode *inode, loff_t lstart)
  289. {
  290. struct hstate *h = hstate_inode(inode);
  291. struct address_space *mapping = &inode->i_data;
  292. const pgoff_t start = lstart >> huge_page_shift(h);
  293. struct pagevec pvec;
  294. pgoff_t next;
  295. int i, freed = 0;
  296. pagevec_init(&pvec, 0);
  297. next = start;
  298. while (1) {
  299. if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  300. if (next == start)
  301. break;
  302. next = start;
  303. continue;
  304. }
  305. for (i = 0; i < pagevec_count(&pvec); ++i) {
  306. struct page *page = pvec.pages[i];
  307. lock_page(page);
  308. if (page->index > next)
  309. next = page->index;
  310. ++next;
  311. truncate_huge_page(page);
  312. unlock_page(page);
  313. freed++;
  314. }
  315. huge_pagevec_release(&pvec);
  316. }
  317. BUG_ON(!lstart && mapping->nrpages);
  318. hugetlb_unreserve_pages(inode, start, freed);
  319. }
  320. static void hugetlbfs_evict_inode(struct inode *inode)
  321. {
  322. struct resv_map *resv_map;
  323. truncate_hugepages(inode, 0);
  324. resv_map = (struct resv_map *)inode->i_mapping->private_data;
  325. /* root inode doesn't have the resv_map, so we should check it */
  326. if (resv_map)
  327. resv_map_release(&resv_map->refs);
  328. clear_inode(inode);
  329. }
  330. static inline void
  331. hugetlb_vmtruncate_list(struct rb_root *root, pgoff_t pgoff)
  332. {
  333. struct vm_area_struct *vma;
  334. vma_interval_tree_foreach(vma, root, pgoff, ULONG_MAX) {
  335. unsigned long v_offset;
  336. /*
  337. * Can the expression below overflow on 32-bit arches?
  338. * No, because the interval tree returns us only those vmas
  339. * which overlap the truncated area starting at pgoff,
  340. * and no vma on a 32-bit arch can span beyond the 4GB.
  341. */
  342. if (vma->vm_pgoff < pgoff)
  343. v_offset = (pgoff - vma->vm_pgoff) << PAGE_SHIFT;
  344. else
  345. v_offset = 0;
  346. unmap_hugepage_range(vma, vma->vm_start + v_offset,
  347. vma->vm_end, NULL);
  348. }
  349. }
  350. static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
  351. {
  352. pgoff_t pgoff;
  353. struct address_space *mapping = inode->i_mapping;
  354. struct hstate *h = hstate_inode(inode);
  355. BUG_ON(offset & ~huge_page_mask(h));
  356. pgoff = offset >> PAGE_SHIFT;
  357. i_size_write(inode, offset);
  358. mutex_lock(&mapping->i_mmap_mutex);
  359. if (!RB_EMPTY_ROOT(&mapping->i_mmap))
  360. hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
  361. mutex_unlock(&mapping->i_mmap_mutex);
  362. truncate_hugepages(inode, offset);
  363. return 0;
  364. }
  365. static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
  366. {
  367. struct inode *inode = dentry->d_inode;
  368. struct hstate *h = hstate_inode(inode);
  369. int error;
  370. unsigned int ia_valid = attr->ia_valid;
  371. BUG_ON(!inode);
  372. error = inode_change_ok(inode, attr);
  373. if (error)
  374. return error;
  375. if (ia_valid & ATTR_SIZE) {
  376. error = -EINVAL;
  377. if (attr->ia_size & ~huge_page_mask(h))
  378. return -EINVAL;
  379. error = hugetlb_vmtruncate(inode, attr->ia_size);
  380. if (error)
  381. return error;
  382. }
  383. setattr_copy(inode, attr);
  384. mark_inode_dirty(inode);
  385. return 0;
  386. }
  387. static struct inode *hugetlbfs_get_root(struct super_block *sb,
  388. struct hugetlbfs_config *config)
  389. {
  390. struct inode *inode;
  391. inode = new_inode(sb);
  392. if (inode) {
  393. struct hugetlbfs_inode_info *info;
  394. inode->i_ino = get_next_ino();
  395. inode->i_mode = S_IFDIR | config->mode;
  396. inode->i_uid = config->uid;
  397. inode->i_gid = config->gid;
  398. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  399. info = HUGETLBFS_I(inode);
  400. mpol_shared_policy_init(&info->policy, NULL);
  401. inode->i_op = &hugetlbfs_dir_inode_operations;
  402. inode->i_fop = &simple_dir_operations;
  403. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  404. inc_nlink(inode);
  405. lockdep_annotate_inode_mutex_key(inode);
  406. }
  407. return inode;
  408. }
  409. /*
  410. * Hugetlbfs is not reclaimable; therefore its i_mmap_mutex will never
  411. * be taken from reclaim -- unlike regular filesystems. This needs an
  412. * annotation because huge_pmd_share() does an allocation under
  413. * i_mmap_mutex.
  414. */
  415. static struct lock_class_key hugetlbfs_i_mmap_mutex_key;
  416. static struct inode *hugetlbfs_get_inode(struct super_block *sb,
  417. struct inode *dir,
  418. umode_t mode, dev_t dev)
  419. {
  420. struct inode *inode;
  421. struct resv_map *resv_map;
  422. resv_map = resv_map_alloc();
  423. if (!resv_map)
  424. return NULL;
  425. inode = new_inode(sb);
  426. if (inode) {
  427. struct hugetlbfs_inode_info *info;
  428. inode->i_ino = get_next_ino();
  429. inode_init_owner(inode, dir, mode);
  430. lockdep_set_class(&inode->i_mapping->i_mmap_mutex,
  431. &hugetlbfs_i_mmap_mutex_key);
  432. inode->i_mapping->a_ops = &hugetlbfs_aops;
  433. inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
  434. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  435. inode->i_mapping->private_data = resv_map;
  436. info = HUGETLBFS_I(inode);
  437. /*
  438. * The policy is initialized here even if we are creating a
  439. * private inode because initialization simply creates an
  440. * an empty rb tree and calls spin_lock_init(), later when we
  441. * call mpol_free_shared_policy() it will just return because
  442. * the rb tree will still be empty.
  443. */
  444. mpol_shared_policy_init(&info->policy, NULL);
  445. switch (mode & S_IFMT) {
  446. default:
  447. init_special_inode(inode, mode, dev);
  448. break;
  449. case S_IFREG:
  450. inode->i_op = &hugetlbfs_inode_operations;
  451. inode->i_fop = &hugetlbfs_file_operations;
  452. break;
  453. case S_IFDIR:
  454. inode->i_op = &hugetlbfs_dir_inode_operations;
  455. inode->i_fop = &simple_dir_operations;
  456. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  457. inc_nlink(inode);
  458. break;
  459. case S_IFLNK:
  460. inode->i_op = &page_symlink_inode_operations;
  461. break;
  462. }
  463. lockdep_annotate_inode_mutex_key(inode);
  464. } else
  465. kref_put(&resv_map->refs, resv_map_release);
  466. return inode;
  467. }
  468. /*
  469. * File creation. Allocate an inode, and we're done..
  470. */
  471. static int hugetlbfs_mknod(struct inode *dir,
  472. struct dentry *dentry, umode_t mode, dev_t dev)
  473. {
  474. struct inode *inode;
  475. int error = -ENOSPC;
  476. inode = hugetlbfs_get_inode(dir->i_sb, dir, mode, dev);
  477. if (inode) {
  478. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  479. d_instantiate(dentry, inode);
  480. dget(dentry); /* Extra count - pin the dentry in core */
  481. error = 0;
  482. }
  483. return error;
  484. }
  485. static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  486. {
  487. int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
  488. if (!retval)
  489. inc_nlink(dir);
  490. return retval;
  491. }
  492. static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  493. {
  494. return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
  495. }
  496. static int hugetlbfs_symlink(struct inode *dir,
  497. struct dentry *dentry, const char *symname)
  498. {
  499. struct inode *inode;
  500. int error = -ENOSPC;
  501. inode = hugetlbfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
  502. if (inode) {
  503. int l = strlen(symname)+1;
  504. error = page_symlink(inode, symname, l);
  505. if (!error) {
  506. d_instantiate(dentry, inode);
  507. dget(dentry);
  508. } else
  509. iput(inode);
  510. }
  511. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  512. return error;
  513. }
  514. /*
  515. * mark the head page dirty
  516. */
  517. static int hugetlbfs_set_page_dirty(struct page *page)
  518. {
  519. struct page *head = compound_head(page);
  520. SetPageDirty(head);
  521. return 0;
  522. }
  523. static int hugetlbfs_migrate_page(struct address_space *mapping,
  524. struct page *newpage, struct page *page,
  525. enum migrate_mode mode)
  526. {
  527. int rc;
  528. rc = migrate_huge_page_move_mapping(mapping, newpage, page);
  529. if (rc != MIGRATEPAGE_SUCCESS)
  530. return rc;
  531. migrate_page_copy(newpage, page);
  532. return MIGRATEPAGE_SUCCESS;
  533. }
  534. static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  535. {
  536. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
  537. struct hstate *h = hstate_inode(dentry->d_inode);
  538. buf->f_type = HUGETLBFS_MAGIC;
  539. buf->f_bsize = huge_page_size(h);
  540. if (sbinfo) {
  541. spin_lock(&sbinfo->stat_lock);
  542. /* If no limits set, just report 0 for max/free/used
  543. * blocks, like simple_statfs() */
  544. if (sbinfo->spool) {
  545. long free_pages;
  546. spin_lock(&sbinfo->spool->lock);
  547. buf->f_blocks = sbinfo->spool->max_hpages;
  548. free_pages = sbinfo->spool->max_hpages
  549. - sbinfo->spool->used_hpages;
  550. buf->f_bavail = buf->f_bfree = free_pages;
  551. spin_unlock(&sbinfo->spool->lock);
  552. buf->f_files = sbinfo->max_inodes;
  553. buf->f_ffree = sbinfo->free_inodes;
  554. }
  555. spin_unlock(&sbinfo->stat_lock);
  556. }
  557. buf->f_namelen = NAME_MAX;
  558. return 0;
  559. }
  560. static void hugetlbfs_put_super(struct super_block *sb)
  561. {
  562. struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
  563. if (sbi) {
  564. sb->s_fs_info = NULL;
  565. if (sbi->spool)
  566. hugepage_put_subpool(sbi->spool);
  567. kfree(sbi);
  568. }
  569. }
  570. static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  571. {
  572. if (sbinfo->free_inodes >= 0) {
  573. spin_lock(&sbinfo->stat_lock);
  574. if (unlikely(!sbinfo->free_inodes)) {
  575. spin_unlock(&sbinfo->stat_lock);
  576. return 0;
  577. }
  578. sbinfo->free_inodes--;
  579. spin_unlock(&sbinfo->stat_lock);
  580. }
  581. return 1;
  582. }
  583. static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  584. {
  585. if (sbinfo->free_inodes >= 0) {
  586. spin_lock(&sbinfo->stat_lock);
  587. sbinfo->free_inodes++;
  588. spin_unlock(&sbinfo->stat_lock);
  589. }
  590. }
  591. static struct kmem_cache *hugetlbfs_inode_cachep;
  592. static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
  593. {
  594. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
  595. struct hugetlbfs_inode_info *p;
  596. if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
  597. return NULL;
  598. p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
  599. if (unlikely(!p)) {
  600. hugetlbfs_inc_free_inodes(sbinfo);
  601. return NULL;
  602. }
  603. return &p->vfs_inode;
  604. }
  605. static void hugetlbfs_i_callback(struct rcu_head *head)
  606. {
  607. struct inode *inode = container_of(head, struct inode, i_rcu);
  608. kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
  609. }
  610. static void hugetlbfs_destroy_inode(struct inode *inode)
  611. {
  612. hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
  613. mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
  614. call_rcu(&inode->i_rcu, hugetlbfs_i_callback);
  615. }
  616. static const struct address_space_operations hugetlbfs_aops = {
  617. .write_begin = hugetlbfs_write_begin,
  618. .write_end = hugetlbfs_write_end,
  619. .set_page_dirty = hugetlbfs_set_page_dirty,
  620. .migratepage = hugetlbfs_migrate_page,
  621. };
  622. static void init_once(void *foo)
  623. {
  624. struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
  625. inode_init_once(&ei->vfs_inode);
  626. }
  627. const struct file_operations hugetlbfs_file_operations = {
  628. .read = hugetlbfs_read,
  629. .mmap = hugetlbfs_file_mmap,
  630. .fsync = noop_fsync,
  631. .get_unmapped_area = hugetlb_get_unmapped_area,
  632. .llseek = default_llseek,
  633. };
  634. static const struct inode_operations hugetlbfs_dir_inode_operations = {
  635. .create = hugetlbfs_create,
  636. .lookup = simple_lookup,
  637. .link = simple_link,
  638. .unlink = simple_unlink,
  639. .symlink = hugetlbfs_symlink,
  640. .mkdir = hugetlbfs_mkdir,
  641. .rmdir = simple_rmdir,
  642. .mknod = hugetlbfs_mknod,
  643. .rename = simple_rename,
  644. .setattr = hugetlbfs_setattr,
  645. };
  646. static const struct inode_operations hugetlbfs_inode_operations = {
  647. .setattr = hugetlbfs_setattr,
  648. };
  649. static const struct super_operations hugetlbfs_ops = {
  650. .alloc_inode = hugetlbfs_alloc_inode,
  651. .destroy_inode = hugetlbfs_destroy_inode,
  652. .evict_inode = hugetlbfs_evict_inode,
  653. .statfs = hugetlbfs_statfs,
  654. .put_super = hugetlbfs_put_super,
  655. .show_options = generic_show_options,
  656. };
  657. static int
  658. hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
  659. {
  660. char *p, *rest;
  661. substring_t args[MAX_OPT_ARGS];
  662. int option;
  663. unsigned long long size = 0;
  664. enum { NO_SIZE, SIZE_STD, SIZE_PERCENT } setsize = NO_SIZE;
  665. if (!options)
  666. return 0;
  667. while ((p = strsep(&options, ",")) != NULL) {
  668. int token;
  669. if (!*p)
  670. continue;
  671. token = match_token(p, tokens, args);
  672. switch (token) {
  673. case Opt_uid:
  674. if (match_int(&args[0], &option))
  675. goto bad_val;
  676. pconfig->uid = make_kuid(current_user_ns(), option);
  677. if (!uid_valid(pconfig->uid))
  678. goto bad_val;
  679. break;
  680. case Opt_gid:
  681. if (match_int(&args[0], &option))
  682. goto bad_val;
  683. pconfig->gid = make_kgid(current_user_ns(), option);
  684. if (!gid_valid(pconfig->gid))
  685. goto bad_val;
  686. break;
  687. case Opt_mode:
  688. if (match_octal(&args[0], &option))
  689. goto bad_val;
  690. pconfig->mode = option & 01777U;
  691. break;
  692. case Opt_size: {
  693. /* memparse() will accept a K/M/G without a digit */
  694. if (!isdigit(*args[0].from))
  695. goto bad_val;
  696. size = memparse(args[0].from, &rest);
  697. setsize = SIZE_STD;
  698. if (*rest == '%')
  699. setsize = SIZE_PERCENT;
  700. break;
  701. }
  702. case Opt_nr_inodes:
  703. /* memparse() will accept a K/M/G without a digit */
  704. if (!isdigit(*args[0].from))
  705. goto bad_val;
  706. pconfig->nr_inodes = memparse(args[0].from, &rest);
  707. break;
  708. case Opt_pagesize: {
  709. unsigned long ps;
  710. ps = memparse(args[0].from, &rest);
  711. pconfig->hstate = size_to_hstate(ps);
  712. if (!pconfig->hstate) {
  713. pr_err("Unsupported page size %lu MB\n",
  714. ps >> 20);
  715. return -EINVAL;
  716. }
  717. break;
  718. }
  719. default:
  720. pr_err("Bad mount option: \"%s\"\n", p);
  721. return -EINVAL;
  722. break;
  723. }
  724. }
  725. /* Do size after hstate is set up */
  726. if (setsize > NO_SIZE) {
  727. struct hstate *h = pconfig->hstate;
  728. if (setsize == SIZE_PERCENT) {
  729. size <<= huge_page_shift(h);
  730. size *= h->max_huge_pages;
  731. do_div(size, 100);
  732. }
  733. pconfig->nr_blocks = (size >> huge_page_shift(h));
  734. }
  735. return 0;
  736. bad_val:
  737. pr_err("Bad value '%s' for mount option '%s'\n", args[0].from, p);
  738. return -EINVAL;
  739. }
  740. static int
  741. hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
  742. {
  743. int ret;
  744. struct hugetlbfs_config config;
  745. struct hugetlbfs_sb_info *sbinfo;
  746. save_mount_options(sb, data);
  747. config.nr_blocks = -1; /* No limit on size by default */
  748. config.nr_inodes = -1; /* No limit on number of inodes by default */
  749. config.uid = current_fsuid();
  750. config.gid = current_fsgid();
  751. config.mode = 0755;
  752. config.hstate = &default_hstate;
  753. ret = hugetlbfs_parse_options(data, &config);
  754. if (ret)
  755. return ret;
  756. sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
  757. if (!sbinfo)
  758. return -ENOMEM;
  759. sb->s_fs_info = sbinfo;
  760. sbinfo->hstate = config.hstate;
  761. spin_lock_init(&sbinfo->stat_lock);
  762. sbinfo->max_inodes = config.nr_inodes;
  763. sbinfo->free_inodes = config.nr_inodes;
  764. sbinfo->spool = NULL;
  765. if (config.nr_blocks != -1) {
  766. sbinfo->spool = hugepage_new_subpool(config.nr_blocks);
  767. if (!sbinfo->spool)
  768. goto out_free;
  769. }
  770. sb->s_maxbytes = MAX_LFS_FILESIZE;
  771. sb->s_blocksize = huge_page_size(config.hstate);
  772. sb->s_blocksize_bits = huge_page_shift(config.hstate);
  773. sb->s_magic = HUGETLBFS_MAGIC;
  774. sb->s_op = &hugetlbfs_ops;
  775. sb->s_time_gran = 1;
  776. sb->s_root = d_make_root(hugetlbfs_get_root(sb, &config));
  777. if (!sb->s_root)
  778. goto out_free;
  779. return 0;
  780. out_free:
  781. if (sbinfo->spool)
  782. kfree(sbinfo->spool);
  783. kfree(sbinfo);
  784. return -ENOMEM;
  785. }
  786. static struct dentry *hugetlbfs_mount(struct file_system_type *fs_type,
  787. int flags, const char *dev_name, void *data)
  788. {
  789. return mount_nodev(fs_type, flags, data, hugetlbfs_fill_super);
  790. }
  791. static struct file_system_type hugetlbfs_fs_type = {
  792. .name = "hugetlbfs",
  793. .mount = hugetlbfs_mount,
  794. .kill_sb = kill_litter_super,
  795. };
  796. MODULE_ALIAS_FS("hugetlbfs");
  797. static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];
  798. static int can_do_hugetlb_shm(void)
  799. {
  800. kgid_t shm_group;
  801. shm_group = make_kgid(&init_user_ns, sysctl_hugetlb_shm_group);
  802. return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
  803. }
  804. static int get_hstate_idx(int page_size_log)
  805. {
  806. struct hstate *h = hstate_sizelog(page_size_log);
  807. if (!h)
  808. return -1;
  809. return h - hstates;
  810. }
  811. static const struct dentry_operations anon_ops = {
  812. .d_dname = simple_dname
  813. };
  814. /*
  815. * Note that size should be aligned to proper hugepage size in caller side,
  816. * otherwise hugetlb_reserve_pages reserves one less hugepages than intended.
  817. */
  818. struct file *hugetlb_file_setup(const char *name, size_t size,
  819. vm_flags_t acctflag, struct user_struct **user,
  820. int creat_flags, int page_size_log)
  821. {
  822. struct file *file = ERR_PTR(-ENOMEM);
  823. struct inode *inode;
  824. struct path path;
  825. struct super_block *sb;
  826. struct qstr quick_string;
  827. int hstate_idx;
  828. hstate_idx = get_hstate_idx(page_size_log);
  829. if (hstate_idx < 0)
  830. return ERR_PTR(-ENODEV);
  831. *user = NULL;
  832. if (!hugetlbfs_vfsmount[hstate_idx])
  833. return ERR_PTR(-ENOENT);
  834. if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
  835. *user = current_user();
  836. if (user_shm_lock(size, *user)) {
  837. task_lock(current);
  838. pr_warn_once("%s (%d): Using mlock ulimits for SHM_HUGETLB is deprecated\n",
  839. current->comm, current->pid);
  840. task_unlock(current);
  841. } else {
  842. *user = NULL;
  843. return ERR_PTR(-EPERM);
  844. }
  845. }
  846. sb = hugetlbfs_vfsmount[hstate_idx]->mnt_sb;
  847. quick_string.name = name;
  848. quick_string.len = strlen(quick_string.name);
  849. quick_string.hash = 0;
  850. path.dentry = d_alloc_pseudo(sb, &quick_string);
  851. if (!path.dentry)
  852. goto out_shm_unlock;
  853. d_set_d_op(path.dentry, &anon_ops);
  854. path.mnt = mntget(hugetlbfs_vfsmount[hstate_idx]);
  855. file = ERR_PTR(-ENOSPC);
  856. inode = hugetlbfs_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0);
  857. if (!inode)
  858. goto out_dentry;
  859. file = ERR_PTR(-ENOMEM);
  860. if (hugetlb_reserve_pages(inode, 0,
  861. size >> huge_page_shift(hstate_inode(inode)), NULL,
  862. acctflag))
  863. goto out_inode;
  864. d_instantiate(path.dentry, inode);
  865. inode->i_size = size;
  866. clear_nlink(inode);
  867. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  868. &hugetlbfs_file_operations);
  869. if (IS_ERR(file))
  870. goto out_dentry; /* inode is already attached */
  871. return file;
  872. out_inode:
  873. iput(inode);
  874. out_dentry:
  875. path_put(&path);
  876. out_shm_unlock:
  877. if (*user) {
  878. user_shm_unlock(size, *user);
  879. *user = NULL;
  880. }
  881. return file;
  882. }
  883. static int __init init_hugetlbfs_fs(void)
  884. {
  885. struct hstate *h;
  886. int error;
  887. int i;
  888. if (!hugepages_supported()) {
  889. pr_info("disabling because there are no supported hugepage sizes\n");
  890. return -ENOTSUPP;
  891. }
  892. error = bdi_init(&hugetlbfs_backing_dev_info);
  893. if (error)
  894. return error;
  895. error = -ENOMEM;
  896. hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
  897. sizeof(struct hugetlbfs_inode_info),
  898. 0, 0, init_once);
  899. if (hugetlbfs_inode_cachep == NULL)
  900. goto out2;
  901. error = register_filesystem(&hugetlbfs_fs_type);
  902. if (error)
  903. goto out;
  904. i = 0;
  905. for_each_hstate(h) {
  906. char buf[50];
  907. unsigned ps_kb = 1U << (h->order + PAGE_SHIFT - 10);
  908. snprintf(buf, sizeof(buf), "pagesize=%uK", ps_kb);
  909. hugetlbfs_vfsmount[i] = kern_mount_data(&hugetlbfs_fs_type,
  910. buf);
  911. if (IS_ERR(hugetlbfs_vfsmount[i])) {
  912. pr_err("Cannot mount internal hugetlbfs for "
  913. "page size %uK", ps_kb);
  914. error = PTR_ERR(hugetlbfs_vfsmount[i]);
  915. hugetlbfs_vfsmount[i] = NULL;
  916. }
  917. i++;
  918. }
  919. /* Non default hstates are optional */
  920. if (!IS_ERR_OR_NULL(hugetlbfs_vfsmount[default_hstate_idx]))
  921. return 0;
  922. out:
  923. kmem_cache_destroy(hugetlbfs_inode_cachep);
  924. out2:
  925. bdi_destroy(&hugetlbfs_backing_dev_info);
  926. return error;
  927. }
  928. static void __exit exit_hugetlbfs_fs(void)
  929. {
  930. struct hstate *h;
  931. int i;
  932. /*
  933. * Make sure all delayed rcu free inodes are flushed before we
  934. * destroy cache.
  935. */
  936. rcu_barrier();
  937. kmem_cache_destroy(hugetlbfs_inode_cachep);
  938. i = 0;
  939. for_each_hstate(h)
  940. kern_unmount(hugetlbfs_vfsmount[i++]);
  941. unregister_filesystem(&hugetlbfs_fs_type);
  942. bdi_destroy(&hugetlbfs_backing_dev_info);
  943. }
  944. module_init(init_hugetlbfs_fs)
  945. module_exit(exit_hugetlbfs_fs)
  946. MODULE_LICENSE("GPL");