inode.c 26 KB

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