inode.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /*
  2. * hugetlbpage-backed filesystem. Based on ramfs.
  3. *
  4. * William Irwin, 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->f_path.dentry->d_inode;
  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 | VM_DONTDUMP;
  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. truncate_hugepages(inode, 0);
  322. clear_inode(inode);
  323. }
  324. static inline void
  325. hugetlb_vmtruncate_list(struct rb_root *root, pgoff_t pgoff)
  326. {
  327. struct vm_area_struct *vma;
  328. vma_interval_tree_foreach(vma, root, pgoff, ULONG_MAX) {
  329. unsigned long v_offset;
  330. /*
  331. * Can the expression below overflow on 32-bit arches?
  332. * No, because the interval tree returns us only those vmas
  333. * which overlap the truncated area starting at pgoff,
  334. * and no vma on a 32-bit arch can span beyond the 4GB.
  335. */
  336. if (vma->vm_pgoff < pgoff)
  337. v_offset = (pgoff - vma->vm_pgoff) << PAGE_SHIFT;
  338. else
  339. v_offset = 0;
  340. unmap_hugepage_range(vma, vma->vm_start + v_offset,
  341. vma->vm_end, NULL);
  342. }
  343. }
  344. static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
  345. {
  346. pgoff_t pgoff;
  347. struct address_space *mapping = inode->i_mapping;
  348. struct hstate *h = hstate_inode(inode);
  349. BUG_ON(offset & ~huge_page_mask(h));
  350. pgoff = offset >> PAGE_SHIFT;
  351. i_size_write(inode, offset);
  352. mutex_lock(&mapping->i_mmap_mutex);
  353. if (!RB_EMPTY_ROOT(&mapping->i_mmap))
  354. hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
  355. mutex_unlock(&mapping->i_mmap_mutex);
  356. truncate_hugepages(inode, offset);
  357. return 0;
  358. }
  359. static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
  360. {
  361. struct inode *inode = dentry->d_inode;
  362. struct hstate *h = hstate_inode(inode);
  363. int error;
  364. unsigned int ia_valid = attr->ia_valid;
  365. BUG_ON(!inode);
  366. error = inode_change_ok(inode, attr);
  367. if (error)
  368. return error;
  369. if (ia_valid & ATTR_SIZE) {
  370. error = -EINVAL;
  371. if (attr->ia_size & ~huge_page_mask(h))
  372. return -EINVAL;
  373. error = hugetlb_vmtruncate(inode, attr->ia_size);
  374. if (error)
  375. return error;
  376. }
  377. setattr_copy(inode, attr);
  378. mark_inode_dirty(inode);
  379. return 0;
  380. }
  381. static struct inode *hugetlbfs_get_root(struct super_block *sb,
  382. struct hugetlbfs_config *config)
  383. {
  384. struct inode *inode;
  385. inode = new_inode(sb);
  386. if (inode) {
  387. struct hugetlbfs_inode_info *info;
  388. inode->i_ino = get_next_ino();
  389. inode->i_mode = S_IFDIR | config->mode;
  390. inode->i_uid = config->uid;
  391. inode->i_gid = config->gid;
  392. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  393. info = HUGETLBFS_I(inode);
  394. mpol_shared_policy_init(&info->policy, NULL);
  395. inode->i_op = &hugetlbfs_dir_inode_operations;
  396. inode->i_fop = &simple_dir_operations;
  397. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  398. inc_nlink(inode);
  399. lockdep_annotate_inode_mutex_key(inode);
  400. }
  401. return inode;
  402. }
  403. static struct inode *hugetlbfs_get_inode(struct super_block *sb,
  404. struct inode *dir,
  405. umode_t mode, dev_t dev)
  406. {
  407. struct inode *inode;
  408. inode = new_inode(sb);
  409. if (inode) {
  410. struct hugetlbfs_inode_info *info;
  411. inode->i_ino = get_next_ino();
  412. inode_init_owner(inode, dir, mode);
  413. inode->i_mapping->a_ops = &hugetlbfs_aops;
  414. inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
  415. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  416. INIT_LIST_HEAD(&inode->i_mapping->private_list);
  417. info = HUGETLBFS_I(inode);
  418. /*
  419. * The policy is initialized here even if we are creating a
  420. * private inode because initialization simply creates an
  421. * an empty rb tree and calls spin_lock_init(), later when we
  422. * call mpol_free_shared_policy() it will just return because
  423. * the rb tree will still be empty.
  424. */
  425. mpol_shared_policy_init(&info->policy, NULL);
  426. switch (mode & S_IFMT) {
  427. default:
  428. init_special_inode(inode, mode, dev);
  429. break;
  430. case S_IFREG:
  431. inode->i_op = &hugetlbfs_inode_operations;
  432. inode->i_fop = &hugetlbfs_file_operations;
  433. break;
  434. case S_IFDIR:
  435. inode->i_op = &hugetlbfs_dir_inode_operations;
  436. inode->i_fop = &simple_dir_operations;
  437. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  438. inc_nlink(inode);
  439. break;
  440. case S_IFLNK:
  441. inode->i_op = &page_symlink_inode_operations;
  442. break;
  443. }
  444. lockdep_annotate_inode_mutex_key(inode);
  445. }
  446. return inode;
  447. }
  448. /*
  449. * File creation. Allocate an inode, and we're done..
  450. */
  451. static int hugetlbfs_mknod(struct inode *dir,
  452. struct dentry *dentry, umode_t mode, dev_t dev)
  453. {
  454. struct inode *inode;
  455. int error = -ENOSPC;
  456. inode = hugetlbfs_get_inode(dir->i_sb, dir, mode, dev);
  457. if (inode) {
  458. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  459. d_instantiate(dentry, inode);
  460. dget(dentry); /* Extra count - pin the dentry in core */
  461. error = 0;
  462. }
  463. return error;
  464. }
  465. static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  466. {
  467. int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
  468. if (!retval)
  469. inc_nlink(dir);
  470. return retval;
  471. }
  472. static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool excl)
  473. {
  474. return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
  475. }
  476. static int hugetlbfs_symlink(struct inode *dir,
  477. struct dentry *dentry, const char *symname)
  478. {
  479. struct inode *inode;
  480. int error = -ENOSPC;
  481. inode = hugetlbfs_get_inode(dir->i_sb, dir, S_IFLNK|S_IRWXUGO, 0);
  482. if (inode) {
  483. int l = strlen(symname)+1;
  484. error = page_symlink(inode, symname, l);
  485. if (!error) {
  486. d_instantiate(dentry, inode);
  487. dget(dentry);
  488. } else
  489. iput(inode);
  490. }
  491. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  492. return error;
  493. }
  494. /*
  495. * mark the head page dirty
  496. */
  497. static int hugetlbfs_set_page_dirty(struct page *page)
  498. {
  499. struct page *head = compound_head(page);
  500. SetPageDirty(head);
  501. return 0;
  502. }
  503. static int hugetlbfs_migrate_page(struct address_space *mapping,
  504. struct page *newpage, struct page *page,
  505. enum migrate_mode mode)
  506. {
  507. int rc;
  508. rc = migrate_huge_page_move_mapping(mapping, newpage, page);
  509. if (rc != MIGRATEPAGE_SUCCESS)
  510. return rc;
  511. migrate_page_copy(newpage, page);
  512. return MIGRATEPAGE_SUCCESS;
  513. }
  514. static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  515. {
  516. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
  517. struct hstate *h = hstate_inode(dentry->d_inode);
  518. buf->f_type = HUGETLBFS_MAGIC;
  519. buf->f_bsize = huge_page_size(h);
  520. if (sbinfo) {
  521. spin_lock(&sbinfo->stat_lock);
  522. /* If no limits set, just report 0 for max/free/used
  523. * blocks, like simple_statfs() */
  524. if (sbinfo->spool) {
  525. long free_pages;
  526. spin_lock(&sbinfo->spool->lock);
  527. buf->f_blocks = sbinfo->spool->max_hpages;
  528. free_pages = sbinfo->spool->max_hpages
  529. - sbinfo->spool->used_hpages;
  530. buf->f_bavail = buf->f_bfree = free_pages;
  531. spin_unlock(&sbinfo->spool->lock);
  532. buf->f_files = sbinfo->max_inodes;
  533. buf->f_ffree = sbinfo->free_inodes;
  534. }
  535. spin_unlock(&sbinfo->stat_lock);
  536. }
  537. buf->f_namelen = NAME_MAX;
  538. return 0;
  539. }
  540. static void hugetlbfs_put_super(struct super_block *sb)
  541. {
  542. struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
  543. if (sbi) {
  544. sb->s_fs_info = NULL;
  545. if (sbi->spool)
  546. hugepage_put_subpool(sbi->spool);
  547. kfree(sbi);
  548. }
  549. }
  550. static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  551. {
  552. if (sbinfo->free_inodes >= 0) {
  553. spin_lock(&sbinfo->stat_lock);
  554. if (unlikely(!sbinfo->free_inodes)) {
  555. spin_unlock(&sbinfo->stat_lock);
  556. return 0;
  557. }
  558. sbinfo->free_inodes--;
  559. spin_unlock(&sbinfo->stat_lock);
  560. }
  561. return 1;
  562. }
  563. static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  564. {
  565. if (sbinfo->free_inodes >= 0) {
  566. spin_lock(&sbinfo->stat_lock);
  567. sbinfo->free_inodes++;
  568. spin_unlock(&sbinfo->stat_lock);
  569. }
  570. }
  571. static struct kmem_cache *hugetlbfs_inode_cachep;
  572. static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
  573. {
  574. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
  575. struct hugetlbfs_inode_info *p;
  576. if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
  577. return NULL;
  578. p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
  579. if (unlikely(!p)) {
  580. hugetlbfs_inc_free_inodes(sbinfo);
  581. return NULL;
  582. }
  583. return &p->vfs_inode;
  584. }
  585. static void hugetlbfs_i_callback(struct rcu_head *head)
  586. {
  587. struct inode *inode = container_of(head, struct inode, i_rcu);
  588. kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
  589. }
  590. static void hugetlbfs_destroy_inode(struct inode *inode)
  591. {
  592. hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
  593. mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
  594. call_rcu(&inode->i_rcu, hugetlbfs_i_callback);
  595. }
  596. static const struct address_space_operations hugetlbfs_aops = {
  597. .write_begin = hugetlbfs_write_begin,
  598. .write_end = hugetlbfs_write_end,
  599. .set_page_dirty = hugetlbfs_set_page_dirty,
  600. .migratepage = hugetlbfs_migrate_page,
  601. };
  602. static void init_once(void *foo)
  603. {
  604. struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
  605. inode_init_once(&ei->vfs_inode);
  606. }
  607. const struct file_operations hugetlbfs_file_operations = {
  608. .read = hugetlbfs_read,
  609. .mmap = hugetlbfs_file_mmap,
  610. .fsync = noop_fsync,
  611. .get_unmapped_area = hugetlb_get_unmapped_area,
  612. .llseek = default_llseek,
  613. };
  614. static const struct inode_operations hugetlbfs_dir_inode_operations = {
  615. .create = hugetlbfs_create,
  616. .lookup = simple_lookup,
  617. .link = simple_link,
  618. .unlink = simple_unlink,
  619. .symlink = hugetlbfs_symlink,
  620. .mkdir = hugetlbfs_mkdir,
  621. .rmdir = simple_rmdir,
  622. .mknod = hugetlbfs_mknod,
  623. .rename = simple_rename,
  624. .setattr = hugetlbfs_setattr,
  625. };
  626. static const struct inode_operations hugetlbfs_inode_operations = {
  627. .setattr = hugetlbfs_setattr,
  628. };
  629. static const struct super_operations hugetlbfs_ops = {
  630. .alloc_inode = hugetlbfs_alloc_inode,
  631. .destroy_inode = hugetlbfs_destroy_inode,
  632. .evict_inode = hugetlbfs_evict_inode,
  633. .statfs = hugetlbfs_statfs,
  634. .put_super = hugetlbfs_put_super,
  635. .show_options = generic_show_options,
  636. };
  637. static int
  638. hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
  639. {
  640. char *p, *rest;
  641. substring_t args[MAX_OPT_ARGS];
  642. int option;
  643. unsigned long long size = 0;
  644. enum { NO_SIZE, SIZE_STD, SIZE_PERCENT } setsize = NO_SIZE;
  645. if (!options)
  646. return 0;
  647. while ((p = strsep(&options, ",")) != NULL) {
  648. int token;
  649. if (!*p)
  650. continue;
  651. token = match_token(p, tokens, args);
  652. switch (token) {
  653. case Opt_uid:
  654. if (match_int(&args[0], &option))
  655. goto bad_val;
  656. pconfig->uid = make_kuid(current_user_ns(), option);
  657. if (!uid_valid(pconfig->uid))
  658. goto bad_val;
  659. break;
  660. case Opt_gid:
  661. if (match_int(&args[0], &option))
  662. goto bad_val;
  663. pconfig->gid = make_kgid(current_user_ns(), option);
  664. if (!gid_valid(pconfig->gid))
  665. goto bad_val;
  666. break;
  667. case Opt_mode:
  668. if (match_octal(&args[0], &option))
  669. goto bad_val;
  670. pconfig->mode = option & 01777U;
  671. break;
  672. case Opt_size: {
  673. /* memparse() will accept a K/M/G without a digit */
  674. if (!isdigit(*args[0].from))
  675. goto bad_val;
  676. size = memparse(args[0].from, &rest);
  677. setsize = SIZE_STD;
  678. if (*rest == '%')
  679. setsize = SIZE_PERCENT;
  680. break;
  681. }
  682. case Opt_nr_inodes:
  683. /* memparse() will accept a K/M/G without a digit */
  684. if (!isdigit(*args[0].from))
  685. goto bad_val;
  686. pconfig->nr_inodes = memparse(args[0].from, &rest);
  687. break;
  688. case Opt_pagesize: {
  689. unsigned long ps;
  690. ps = memparse(args[0].from, &rest);
  691. pconfig->hstate = size_to_hstate(ps);
  692. if (!pconfig->hstate) {
  693. printk(KERN_ERR
  694. "hugetlbfs: Unsupported page size %lu MB\n",
  695. ps >> 20);
  696. return -EINVAL;
  697. }
  698. break;
  699. }
  700. default:
  701. printk(KERN_ERR "hugetlbfs: Bad mount option: \"%s\"\n",
  702. p);
  703. return -EINVAL;
  704. break;
  705. }
  706. }
  707. /* Do size after hstate is set up */
  708. if (setsize > NO_SIZE) {
  709. struct hstate *h = pconfig->hstate;
  710. if (setsize == SIZE_PERCENT) {
  711. size <<= huge_page_shift(h);
  712. size *= h->max_huge_pages;
  713. do_div(size, 100);
  714. }
  715. pconfig->nr_blocks = (size >> huge_page_shift(h));
  716. }
  717. return 0;
  718. bad_val:
  719. printk(KERN_ERR "hugetlbfs: Bad value '%s' for mount option '%s'\n",
  720. args[0].from, p);
  721. return -EINVAL;
  722. }
  723. static int
  724. hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
  725. {
  726. int ret;
  727. struct hugetlbfs_config config;
  728. struct hugetlbfs_sb_info *sbinfo;
  729. save_mount_options(sb, data);
  730. config.nr_blocks = -1; /* No limit on size by default */
  731. config.nr_inodes = -1; /* No limit on number of inodes by default */
  732. config.uid = current_fsuid();
  733. config.gid = current_fsgid();
  734. config.mode = 0755;
  735. config.hstate = &default_hstate;
  736. ret = hugetlbfs_parse_options(data, &config);
  737. if (ret)
  738. return ret;
  739. sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
  740. if (!sbinfo)
  741. return -ENOMEM;
  742. sb->s_fs_info = sbinfo;
  743. sbinfo->hstate = config.hstate;
  744. spin_lock_init(&sbinfo->stat_lock);
  745. sbinfo->max_inodes = config.nr_inodes;
  746. sbinfo->free_inodes = config.nr_inodes;
  747. sbinfo->spool = NULL;
  748. if (config.nr_blocks != -1) {
  749. sbinfo->spool = hugepage_new_subpool(config.nr_blocks);
  750. if (!sbinfo->spool)
  751. goto out_free;
  752. }
  753. sb->s_maxbytes = MAX_LFS_FILESIZE;
  754. sb->s_blocksize = huge_page_size(config.hstate);
  755. sb->s_blocksize_bits = huge_page_shift(config.hstate);
  756. sb->s_magic = HUGETLBFS_MAGIC;
  757. sb->s_op = &hugetlbfs_ops;
  758. sb->s_time_gran = 1;
  759. sb->s_root = d_make_root(hugetlbfs_get_root(sb, &config));
  760. if (!sb->s_root)
  761. goto out_free;
  762. return 0;
  763. out_free:
  764. if (sbinfo->spool)
  765. kfree(sbinfo->spool);
  766. kfree(sbinfo);
  767. return -ENOMEM;
  768. }
  769. static struct dentry *hugetlbfs_mount(struct file_system_type *fs_type,
  770. int flags, const char *dev_name, void *data)
  771. {
  772. return mount_nodev(fs_type, flags, data, hugetlbfs_fill_super);
  773. }
  774. static struct file_system_type hugetlbfs_fs_type = {
  775. .name = "hugetlbfs",
  776. .mount = hugetlbfs_mount,
  777. .kill_sb = kill_litter_super,
  778. };
  779. static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];
  780. static int can_do_hugetlb_shm(void)
  781. {
  782. kgid_t shm_group;
  783. shm_group = make_kgid(&init_user_ns, sysctl_hugetlb_shm_group);
  784. return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
  785. }
  786. static int get_hstate_idx(int page_size_log)
  787. {
  788. struct hstate *h;
  789. if (!page_size_log)
  790. return default_hstate_idx;
  791. h = size_to_hstate(1 << page_size_log);
  792. if (!h)
  793. return -1;
  794. return h - hstates;
  795. }
  796. struct file *hugetlb_file_setup(const char *name, unsigned long addr,
  797. size_t size, vm_flags_t acctflag,
  798. struct user_struct **user,
  799. int creat_flags, int page_size_log)
  800. {
  801. int error = -ENOMEM;
  802. struct file *file;
  803. struct inode *inode;
  804. struct path path;
  805. struct dentry *root;
  806. struct qstr quick_string;
  807. struct hstate *hstate;
  808. unsigned long num_pages;
  809. int hstate_idx;
  810. hstate_idx = get_hstate_idx(page_size_log);
  811. if (hstate_idx < 0)
  812. return ERR_PTR(-ENODEV);
  813. *user = NULL;
  814. if (!hugetlbfs_vfsmount[hstate_idx])
  815. return ERR_PTR(-ENOENT);
  816. if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
  817. *user = current_user();
  818. if (user_shm_lock(size, *user)) {
  819. task_lock(current);
  820. printk_once(KERN_WARNING
  821. "%s (%d): Using mlock ulimits for SHM_HUGETLB is deprecated\n",
  822. current->comm, current->pid);
  823. task_unlock(current);
  824. } else {
  825. *user = NULL;
  826. return ERR_PTR(-EPERM);
  827. }
  828. }
  829. root = hugetlbfs_vfsmount[hstate_idx]->mnt_root;
  830. quick_string.name = name;
  831. quick_string.len = strlen(quick_string.name);
  832. quick_string.hash = 0;
  833. path.dentry = d_alloc(root, &quick_string);
  834. if (!path.dentry)
  835. goto out_shm_unlock;
  836. path.mnt = mntget(hugetlbfs_vfsmount[hstate_idx]);
  837. error = -ENOSPC;
  838. inode = hugetlbfs_get_inode(root->d_sb, NULL, S_IFREG | S_IRWXUGO, 0);
  839. if (!inode)
  840. goto out_dentry;
  841. hstate = hstate_inode(inode);
  842. size += addr & ~huge_page_mask(hstate);
  843. num_pages = ALIGN(size, huge_page_size(hstate)) >>
  844. huge_page_shift(hstate);
  845. error = -ENOMEM;
  846. if (hugetlb_reserve_pages(inode, 0, num_pages, NULL, acctflag))
  847. goto out_inode;
  848. d_instantiate(path.dentry, inode);
  849. inode->i_size = size;
  850. clear_nlink(inode);
  851. error = -ENFILE;
  852. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  853. &hugetlbfs_file_operations);
  854. if (!file)
  855. goto out_dentry; /* inode is already attached */
  856. return file;
  857. out_inode:
  858. iput(inode);
  859. out_dentry:
  860. path_put(&path);
  861. out_shm_unlock:
  862. if (*user) {
  863. user_shm_unlock(size, *user);
  864. *user = NULL;
  865. }
  866. return ERR_PTR(error);
  867. }
  868. static int __init init_hugetlbfs_fs(void)
  869. {
  870. struct hstate *h;
  871. int error;
  872. int i;
  873. error = bdi_init(&hugetlbfs_backing_dev_info);
  874. if (error)
  875. return error;
  876. error = -ENOMEM;
  877. hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
  878. sizeof(struct hugetlbfs_inode_info),
  879. 0, 0, init_once);
  880. if (hugetlbfs_inode_cachep == NULL)
  881. goto out2;
  882. error = register_filesystem(&hugetlbfs_fs_type);
  883. if (error)
  884. goto out;
  885. i = 0;
  886. for_each_hstate(h) {
  887. char buf[50];
  888. unsigned ps_kb = 1U << (h->order + PAGE_SHIFT - 10);
  889. snprintf(buf, sizeof(buf), "pagesize=%uK", ps_kb);
  890. hugetlbfs_vfsmount[i] = kern_mount_data(&hugetlbfs_fs_type,
  891. buf);
  892. if (IS_ERR(hugetlbfs_vfsmount[i])) {
  893. pr_err("hugetlb: Cannot mount internal hugetlbfs for "
  894. "page size %uK", ps_kb);
  895. error = PTR_ERR(hugetlbfs_vfsmount[i]);
  896. hugetlbfs_vfsmount[i] = NULL;
  897. }
  898. i++;
  899. }
  900. /* Non default hstates are optional */
  901. if (!IS_ERR_OR_NULL(hugetlbfs_vfsmount[default_hstate_idx]))
  902. return 0;
  903. out:
  904. kmem_cache_destroy(hugetlbfs_inode_cachep);
  905. out2:
  906. bdi_destroy(&hugetlbfs_backing_dev_info);
  907. return error;
  908. }
  909. static void __exit exit_hugetlbfs_fs(void)
  910. {
  911. struct hstate *h;
  912. int i;
  913. /*
  914. * Make sure all delayed rcu free inodes are flushed before we
  915. * destroy cache.
  916. */
  917. rcu_barrier();
  918. kmem_cache_destroy(hugetlbfs_inode_cachep);
  919. i = 0;
  920. for_each_hstate(h)
  921. kern_unmount(hugetlbfs_vfsmount[i++]);
  922. unregister_filesystem(&hugetlbfs_fs_type);
  923. bdi_destroy(&hugetlbfs_backing_dev_info);
  924. }
  925. module_init(init_hugetlbfs_fs)
  926. module_exit(exit_hugetlbfs_fs)
  927. MODULE_LICENSE("GPL");