inode.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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 | 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. MODULE_ALIAS_FS("hugetlbfs");
  780. static struct vfsmount *hugetlbfs_vfsmount[HUGE_MAX_HSTATE];
  781. static int can_do_hugetlb_shm(void)
  782. {
  783. kgid_t shm_group;
  784. shm_group = make_kgid(&init_user_ns, sysctl_hugetlb_shm_group);
  785. return capable(CAP_IPC_LOCK) || in_group_p(shm_group);
  786. }
  787. static int get_hstate_idx(int page_size_log)
  788. {
  789. struct hstate *h;
  790. if (!page_size_log)
  791. return default_hstate_idx;
  792. h = size_to_hstate(1 << page_size_log);
  793. if (!h)
  794. return -1;
  795. return h - hstates;
  796. }
  797. static char *hugetlb_dname(struct dentry *dentry, char *buffer, int buflen)
  798. {
  799. return dynamic_dname(dentry, buffer, buflen, "/%s (deleted)",
  800. dentry->d_name.name);
  801. }
  802. static struct dentry_operations anon_ops = {
  803. .d_dname = hugetlb_dname
  804. };
  805. struct file *hugetlb_file_setup(const char *name, unsigned long addr,
  806. size_t size, vm_flags_t acctflag,
  807. struct user_struct **user,
  808. int creat_flags, int page_size_log)
  809. {
  810. struct file *file = ERR_PTR(-ENOMEM);
  811. struct inode *inode;
  812. struct path path;
  813. struct super_block *sb;
  814. struct qstr quick_string;
  815. struct hstate *hstate;
  816. unsigned long num_pages;
  817. int hstate_idx;
  818. hstate_idx = get_hstate_idx(page_size_log);
  819. if (hstate_idx < 0)
  820. return ERR_PTR(-ENODEV);
  821. *user = NULL;
  822. if (!hugetlbfs_vfsmount[hstate_idx])
  823. return ERR_PTR(-ENOENT);
  824. if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
  825. *user = current_user();
  826. if (user_shm_lock(size, *user)) {
  827. task_lock(current);
  828. printk_once(KERN_WARNING
  829. "%s (%d): Using mlock ulimits for SHM_HUGETLB is deprecated\n",
  830. current->comm, current->pid);
  831. task_unlock(current);
  832. } else {
  833. *user = NULL;
  834. return ERR_PTR(-EPERM);
  835. }
  836. }
  837. sb = hugetlbfs_vfsmount[hstate_idx]->mnt_sb;
  838. quick_string.name = name;
  839. quick_string.len = strlen(quick_string.name);
  840. quick_string.hash = 0;
  841. path.dentry = d_alloc_pseudo(sb, &quick_string);
  842. if (!path.dentry)
  843. goto out_shm_unlock;
  844. d_set_d_op(path.dentry, &anon_ops);
  845. path.mnt = mntget(hugetlbfs_vfsmount[hstate_idx]);
  846. file = ERR_PTR(-ENOSPC);
  847. inode = hugetlbfs_get_inode(sb, NULL, S_IFREG | S_IRWXUGO, 0);
  848. if (!inode)
  849. goto out_dentry;
  850. hstate = hstate_inode(inode);
  851. size += addr & ~huge_page_mask(hstate);
  852. num_pages = ALIGN(size, huge_page_size(hstate)) >>
  853. huge_page_shift(hstate);
  854. file = ERR_PTR(-ENOMEM);
  855. if (hugetlb_reserve_pages(inode, 0, num_pages, NULL, acctflag))
  856. goto out_inode;
  857. d_instantiate(path.dentry, inode);
  858. inode->i_size = size;
  859. clear_nlink(inode);
  860. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  861. &hugetlbfs_file_operations);
  862. if (IS_ERR(file))
  863. goto out_dentry; /* inode is already attached */
  864. return file;
  865. out_inode:
  866. iput(inode);
  867. out_dentry:
  868. path_put(&path);
  869. out_shm_unlock:
  870. if (*user) {
  871. user_shm_unlock(size, *user);
  872. *user = NULL;
  873. }
  874. return file;
  875. }
  876. static int __init init_hugetlbfs_fs(void)
  877. {
  878. struct hstate *h;
  879. int error;
  880. int i;
  881. error = bdi_init(&hugetlbfs_backing_dev_info);
  882. if (error)
  883. return error;
  884. error = -ENOMEM;
  885. hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
  886. sizeof(struct hugetlbfs_inode_info),
  887. 0, 0, init_once);
  888. if (hugetlbfs_inode_cachep == NULL)
  889. goto out2;
  890. error = register_filesystem(&hugetlbfs_fs_type);
  891. if (error)
  892. goto out;
  893. i = 0;
  894. for_each_hstate(h) {
  895. char buf[50];
  896. unsigned ps_kb = 1U << (h->order + PAGE_SHIFT - 10);
  897. snprintf(buf, sizeof(buf), "pagesize=%uK", ps_kb);
  898. hugetlbfs_vfsmount[i] = kern_mount_data(&hugetlbfs_fs_type,
  899. buf);
  900. if (IS_ERR(hugetlbfs_vfsmount[i])) {
  901. pr_err("hugetlb: Cannot mount internal hugetlbfs for "
  902. "page size %uK", ps_kb);
  903. error = PTR_ERR(hugetlbfs_vfsmount[i]);
  904. hugetlbfs_vfsmount[i] = NULL;
  905. }
  906. i++;
  907. }
  908. /* Non default hstates are optional */
  909. if (!IS_ERR_OR_NULL(hugetlbfs_vfsmount[default_hstate_idx]))
  910. return 0;
  911. out:
  912. kmem_cache_destroy(hugetlbfs_inode_cachep);
  913. out2:
  914. bdi_destroy(&hugetlbfs_backing_dev_info);
  915. return error;
  916. }
  917. static void __exit exit_hugetlbfs_fs(void)
  918. {
  919. struct hstate *h;
  920. int i;
  921. /*
  922. * Make sure all delayed rcu free inodes are flushed before we
  923. * destroy cache.
  924. */
  925. rcu_barrier();
  926. kmem_cache_destroy(hugetlbfs_inode_cachep);
  927. i = 0;
  928. for_each_hstate(h)
  929. kern_unmount(hugetlbfs_vfsmount[i++]);
  930. unregister_filesystem(&hugetlbfs_fs_type);
  931. bdi_destroy(&hugetlbfs_backing_dev_info);
  932. }
  933. module_init(init_hugetlbfs_fs)
  934. module_exit(exit_hugetlbfs_fs)
  935. MODULE_LICENSE("GPL");