inode.c 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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. static struct backing_dev_info hugetlbfs_backing_dev_info = {
  41. .name = "hugetlbfs",
  42. .ra_pages = 0, /* No readahead */
  43. .capabilities = BDI_CAP_NO_ACCT_AND_WRITEBACK,
  44. };
  45. int sysctl_hugetlb_shm_group;
  46. enum {
  47. Opt_size, Opt_nr_inodes,
  48. Opt_mode, Opt_uid, Opt_gid,
  49. Opt_pagesize,
  50. Opt_err,
  51. };
  52. static const match_table_t tokens = {
  53. {Opt_size, "size=%s"},
  54. {Opt_nr_inodes, "nr_inodes=%s"},
  55. {Opt_mode, "mode=%o"},
  56. {Opt_uid, "uid=%u"},
  57. {Opt_gid, "gid=%u"},
  58. {Opt_pagesize, "pagesize=%s"},
  59. {Opt_err, NULL},
  60. };
  61. static void huge_pagevec_release(struct pagevec *pvec)
  62. {
  63. int i;
  64. for (i = 0; i < pagevec_count(pvec); ++i)
  65. put_page(pvec->pages[i]);
  66. pagevec_reinit(pvec);
  67. }
  68. static int hugetlbfs_file_mmap(struct file *file, struct vm_area_struct *vma)
  69. {
  70. struct inode *inode = file->f_path.dentry->d_inode;
  71. loff_t len, vma_len;
  72. int ret;
  73. struct hstate *h = hstate_file(file);
  74. /*
  75. * vma address alignment (but not the pgoff alignment) has
  76. * already been checked by prepare_hugepage_range. If you add
  77. * any error returns here, do so after setting VM_HUGETLB, so
  78. * is_vm_hugetlb_page tests below unmap_region go the right
  79. * way when do_mmap_pgoff unwinds (may be important on powerpc
  80. * and ia64).
  81. */
  82. vma->vm_flags |= VM_HUGETLB | VM_RESERVED;
  83. vma->vm_ops = &hugetlb_vm_ops;
  84. if (vma->vm_pgoff & ~(huge_page_mask(h) >> PAGE_SHIFT))
  85. return -EINVAL;
  86. vma_len = (loff_t)(vma->vm_end - vma->vm_start);
  87. mutex_lock(&inode->i_mutex);
  88. file_accessed(file);
  89. ret = -ENOMEM;
  90. len = vma_len + ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  91. if (hugetlb_reserve_pages(inode,
  92. vma->vm_pgoff >> huge_page_order(h),
  93. len >> huge_page_shift(h), vma,
  94. vma->vm_flags))
  95. goto out;
  96. ret = 0;
  97. hugetlb_prefault_arch_hook(vma->vm_mm);
  98. if (vma->vm_flags & VM_WRITE && inode->i_size < len)
  99. inode->i_size = len;
  100. out:
  101. mutex_unlock(&inode->i_mutex);
  102. return ret;
  103. }
  104. /*
  105. * Called under down_write(mmap_sem).
  106. */
  107. #ifndef HAVE_ARCH_HUGETLB_UNMAPPED_AREA
  108. static unsigned long
  109. hugetlb_get_unmapped_area(struct file *file, unsigned long addr,
  110. unsigned long len, unsigned long pgoff, unsigned long flags)
  111. {
  112. struct mm_struct *mm = current->mm;
  113. struct vm_area_struct *vma;
  114. unsigned long start_addr;
  115. struct hstate *h = hstate_file(file);
  116. if (len & ~huge_page_mask(h))
  117. return -EINVAL;
  118. if (len > TASK_SIZE)
  119. return -ENOMEM;
  120. if (flags & MAP_FIXED) {
  121. if (prepare_hugepage_range(file, addr, len))
  122. return -EINVAL;
  123. return addr;
  124. }
  125. if (addr) {
  126. addr = ALIGN(addr, huge_page_size(h));
  127. vma = find_vma(mm, addr);
  128. if (TASK_SIZE - len >= addr &&
  129. (!vma || addr + len <= vma->vm_start))
  130. return addr;
  131. }
  132. start_addr = mm->free_area_cache;
  133. if (len <= mm->cached_hole_size)
  134. start_addr = TASK_UNMAPPED_BASE;
  135. full_search:
  136. addr = ALIGN(start_addr, huge_page_size(h));
  137. for (vma = find_vma(mm, addr); ; vma = vma->vm_next) {
  138. /* At this point: (!vma || addr < vma->vm_end). */
  139. if (TASK_SIZE - len < addr) {
  140. /*
  141. * Start a new search - just in case we missed
  142. * some holes.
  143. */
  144. if (start_addr != TASK_UNMAPPED_BASE) {
  145. start_addr = TASK_UNMAPPED_BASE;
  146. goto full_search;
  147. }
  148. return -ENOMEM;
  149. }
  150. if (!vma || addr + len <= vma->vm_start)
  151. return addr;
  152. addr = ALIGN(vma->vm_end, huge_page_size(h));
  153. }
  154. }
  155. #endif
  156. static int
  157. hugetlbfs_read_actor(struct page *page, unsigned long offset,
  158. char __user *buf, unsigned long count,
  159. unsigned long size)
  160. {
  161. char *kaddr;
  162. unsigned long left, copied = 0;
  163. int i, chunksize;
  164. if (size > count)
  165. size = count;
  166. /* Find which 4k chunk and offset with in that chunk */
  167. i = offset >> PAGE_CACHE_SHIFT;
  168. offset = offset & ~PAGE_CACHE_MASK;
  169. while (size) {
  170. chunksize = PAGE_CACHE_SIZE;
  171. if (offset)
  172. chunksize -= offset;
  173. if (chunksize > size)
  174. chunksize = size;
  175. kaddr = kmap(&page[i]);
  176. left = __copy_to_user(buf, kaddr + offset, chunksize);
  177. kunmap(&page[i]);
  178. if (left) {
  179. copied += (chunksize - left);
  180. break;
  181. }
  182. offset = 0;
  183. size -= chunksize;
  184. buf += chunksize;
  185. copied += chunksize;
  186. i++;
  187. }
  188. return copied ? copied : -EFAULT;
  189. }
  190. /*
  191. * Support for read() - Find the page attached to f_mapping and copy out the
  192. * data. Its *very* similar to do_generic_mapping_read(), we can't use that
  193. * since it has PAGE_CACHE_SIZE assumptions.
  194. */
  195. static ssize_t hugetlbfs_read(struct file *filp, char __user *buf,
  196. size_t len, loff_t *ppos)
  197. {
  198. struct hstate *h = hstate_file(filp);
  199. struct address_space *mapping = filp->f_mapping;
  200. struct inode *inode = mapping->host;
  201. unsigned long index = *ppos >> huge_page_shift(h);
  202. unsigned long offset = *ppos & ~huge_page_mask(h);
  203. unsigned long end_index;
  204. loff_t isize;
  205. ssize_t retval = 0;
  206. /* validate length */
  207. if (len == 0)
  208. goto out;
  209. for (;;) {
  210. struct page *page;
  211. unsigned long nr, ret;
  212. int ra;
  213. /* nr is the maximum number of bytes to copy from this page */
  214. nr = huge_page_size(h);
  215. isize = i_size_read(inode);
  216. if (!isize)
  217. goto out;
  218. end_index = (isize - 1) >> huge_page_shift(h);
  219. if (index >= end_index) {
  220. if (index > end_index)
  221. goto out;
  222. nr = ((isize - 1) & ~huge_page_mask(h)) + 1;
  223. if (nr <= offset)
  224. goto out;
  225. }
  226. nr = nr - offset;
  227. /* Find the page */
  228. page = find_lock_page(mapping, index);
  229. if (unlikely(page == NULL)) {
  230. /*
  231. * We have a HOLE, zero out the user-buffer for the
  232. * length of the hole or request.
  233. */
  234. ret = len < nr ? len : nr;
  235. if (clear_user(buf, ret))
  236. ra = -EFAULT;
  237. else
  238. ra = 0;
  239. } else {
  240. unlock_page(page);
  241. /*
  242. * We have the page, copy it to user space buffer.
  243. */
  244. ra = hugetlbfs_read_actor(page, offset, buf, len, nr);
  245. ret = ra;
  246. page_cache_release(page);
  247. }
  248. if (ra < 0) {
  249. if (retval == 0)
  250. retval = ra;
  251. goto out;
  252. }
  253. offset += ret;
  254. retval += ret;
  255. len -= ret;
  256. index += offset >> huge_page_shift(h);
  257. offset &= ~huge_page_mask(h);
  258. /* short read or no more work */
  259. if ((ret != nr) || (len == 0))
  260. break;
  261. }
  262. out:
  263. *ppos = ((loff_t)index << huge_page_shift(h)) + offset;
  264. return retval;
  265. }
  266. static int hugetlbfs_write_begin(struct file *file,
  267. struct address_space *mapping,
  268. loff_t pos, unsigned len, unsigned flags,
  269. struct page **pagep, void **fsdata)
  270. {
  271. return -EINVAL;
  272. }
  273. static int hugetlbfs_write_end(struct file *file, struct address_space *mapping,
  274. loff_t pos, unsigned len, unsigned copied,
  275. struct page *page, void *fsdata)
  276. {
  277. BUG();
  278. return -EINVAL;
  279. }
  280. static void truncate_huge_page(struct page *page)
  281. {
  282. cancel_dirty_page(page, /* No IO accounting for huge pages? */0);
  283. ClearPageUptodate(page);
  284. delete_from_page_cache(page);
  285. }
  286. static void truncate_hugepages(struct inode *inode, loff_t lstart)
  287. {
  288. struct hstate *h = hstate_inode(inode);
  289. struct address_space *mapping = &inode->i_data;
  290. const pgoff_t start = lstart >> huge_page_shift(h);
  291. struct pagevec pvec;
  292. pgoff_t next;
  293. int i, freed = 0;
  294. pagevec_init(&pvec, 0);
  295. next = start;
  296. while (1) {
  297. if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  298. if (next == start)
  299. break;
  300. next = start;
  301. continue;
  302. }
  303. for (i = 0; i < pagevec_count(&pvec); ++i) {
  304. struct page *page = pvec.pages[i];
  305. lock_page(page);
  306. if (page->index > next)
  307. next = page->index;
  308. ++next;
  309. truncate_huge_page(page);
  310. unlock_page(page);
  311. freed++;
  312. }
  313. huge_pagevec_release(&pvec);
  314. }
  315. BUG_ON(!lstart && mapping->nrpages);
  316. hugetlb_unreserve_pages(inode, start, freed);
  317. }
  318. static void hugetlbfs_evict_inode(struct inode *inode)
  319. {
  320. truncate_hugepages(inode, 0);
  321. end_writeback(inode);
  322. }
  323. static inline void
  324. hugetlb_vmtruncate_list(struct prio_tree_root *root, pgoff_t pgoff)
  325. {
  326. struct vm_area_struct *vma;
  327. struct prio_tree_iter iter;
  328. vma_prio_tree_foreach(vma, &iter, root, pgoff, ULONG_MAX) {
  329. unsigned long v_offset;
  330. /*
  331. * Can the expression below overflow on 32-bit arches?
  332. * No, because the prio_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,
  341. vma->vm_start + v_offset, 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 (!prio_tree_empty(&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_inode(struct super_block *sb, uid_t uid,
  382. gid_t gid, int mode, dev_t dev)
  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 = mode;
  390. inode->i_uid = uid;
  391. inode->i_gid = gid;
  392. inode->i_mapping->a_ops = &hugetlbfs_aops;
  393. inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
  394. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  395. INIT_LIST_HEAD(&inode->i_mapping->private_list);
  396. info = HUGETLBFS_I(inode);
  397. /*
  398. * The policy is initialized here even if we are creating a
  399. * private inode because initialization simply creates an
  400. * an empty rb tree and calls spin_lock_init(), later when we
  401. * call mpol_free_shared_policy() it will just return because
  402. * the rb tree will still be empty.
  403. */
  404. mpol_shared_policy_init(&info->policy, NULL);
  405. switch (mode & S_IFMT) {
  406. default:
  407. init_special_inode(inode, mode, dev);
  408. break;
  409. case S_IFREG:
  410. inode->i_op = &hugetlbfs_inode_operations;
  411. inode->i_fop = &hugetlbfs_file_operations;
  412. break;
  413. case S_IFDIR:
  414. inode->i_op = &hugetlbfs_dir_inode_operations;
  415. inode->i_fop = &simple_dir_operations;
  416. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  417. inc_nlink(inode);
  418. break;
  419. case S_IFLNK:
  420. inode->i_op = &page_symlink_inode_operations;
  421. break;
  422. }
  423. }
  424. return inode;
  425. }
  426. /*
  427. * File creation. Allocate an inode, and we're done..
  428. */
  429. static int hugetlbfs_mknod(struct inode *dir,
  430. struct dentry *dentry, int mode, dev_t dev)
  431. {
  432. struct inode *inode;
  433. int error = -ENOSPC;
  434. gid_t gid;
  435. if (dir->i_mode & S_ISGID) {
  436. gid = dir->i_gid;
  437. if (S_ISDIR(mode))
  438. mode |= S_ISGID;
  439. } else {
  440. gid = current_fsgid();
  441. }
  442. inode = hugetlbfs_get_inode(dir->i_sb, current_fsuid(), gid, mode, dev);
  443. if (inode) {
  444. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  445. d_instantiate(dentry, inode);
  446. dget(dentry); /* Extra count - pin the dentry in core */
  447. error = 0;
  448. }
  449. return error;
  450. }
  451. static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  452. {
  453. int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
  454. if (!retval)
  455. inc_nlink(dir);
  456. return retval;
  457. }
  458. static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
  459. {
  460. return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
  461. }
  462. static int hugetlbfs_symlink(struct inode *dir,
  463. struct dentry *dentry, const char *symname)
  464. {
  465. struct inode *inode;
  466. int error = -ENOSPC;
  467. gid_t gid;
  468. if (dir->i_mode & S_ISGID)
  469. gid = dir->i_gid;
  470. else
  471. gid = current_fsgid();
  472. inode = hugetlbfs_get_inode(dir->i_sb, current_fsuid(),
  473. gid, S_IFLNK|S_IRWXUGO, 0);
  474. if (inode) {
  475. int l = strlen(symname)+1;
  476. error = page_symlink(inode, symname, l);
  477. if (!error) {
  478. d_instantiate(dentry, inode);
  479. dget(dentry);
  480. } else
  481. iput(inode);
  482. }
  483. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  484. return error;
  485. }
  486. /*
  487. * mark the head page dirty
  488. */
  489. static int hugetlbfs_set_page_dirty(struct page *page)
  490. {
  491. struct page *head = compound_head(page);
  492. SetPageDirty(head);
  493. return 0;
  494. }
  495. static int hugetlbfs_migrate_page(struct address_space *mapping,
  496. struct page *newpage, struct page *page)
  497. {
  498. int rc;
  499. rc = migrate_huge_page_move_mapping(mapping, newpage, page);
  500. if (rc)
  501. return rc;
  502. migrate_page_copy(newpage, page);
  503. return 0;
  504. }
  505. static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  506. {
  507. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
  508. struct hstate *h = hstate_inode(dentry->d_inode);
  509. buf->f_type = HUGETLBFS_MAGIC;
  510. buf->f_bsize = huge_page_size(h);
  511. if (sbinfo) {
  512. spin_lock(&sbinfo->stat_lock);
  513. /* If no limits set, just report 0 for max/free/used
  514. * blocks, like simple_statfs() */
  515. if (sbinfo->max_blocks >= 0) {
  516. buf->f_blocks = sbinfo->max_blocks;
  517. buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
  518. buf->f_files = sbinfo->max_inodes;
  519. buf->f_ffree = sbinfo->free_inodes;
  520. }
  521. spin_unlock(&sbinfo->stat_lock);
  522. }
  523. buf->f_namelen = NAME_MAX;
  524. return 0;
  525. }
  526. static void hugetlbfs_put_super(struct super_block *sb)
  527. {
  528. struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
  529. if (sbi) {
  530. sb->s_fs_info = NULL;
  531. kfree(sbi);
  532. }
  533. }
  534. static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  535. {
  536. if (sbinfo->free_inodes >= 0) {
  537. spin_lock(&sbinfo->stat_lock);
  538. if (unlikely(!sbinfo->free_inodes)) {
  539. spin_unlock(&sbinfo->stat_lock);
  540. return 0;
  541. }
  542. sbinfo->free_inodes--;
  543. spin_unlock(&sbinfo->stat_lock);
  544. }
  545. return 1;
  546. }
  547. static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  548. {
  549. if (sbinfo->free_inodes >= 0) {
  550. spin_lock(&sbinfo->stat_lock);
  551. sbinfo->free_inodes++;
  552. spin_unlock(&sbinfo->stat_lock);
  553. }
  554. }
  555. static struct kmem_cache *hugetlbfs_inode_cachep;
  556. static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
  557. {
  558. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
  559. struct hugetlbfs_inode_info *p;
  560. if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
  561. return NULL;
  562. p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
  563. if (unlikely(!p)) {
  564. hugetlbfs_inc_free_inodes(sbinfo);
  565. return NULL;
  566. }
  567. return &p->vfs_inode;
  568. }
  569. static void hugetlbfs_i_callback(struct rcu_head *head)
  570. {
  571. struct inode *inode = container_of(head, struct inode, i_rcu);
  572. INIT_LIST_HEAD(&inode->i_dentry);
  573. kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
  574. }
  575. static void hugetlbfs_destroy_inode(struct inode *inode)
  576. {
  577. hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
  578. mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
  579. call_rcu(&inode->i_rcu, hugetlbfs_i_callback);
  580. }
  581. static const struct address_space_operations hugetlbfs_aops = {
  582. .write_begin = hugetlbfs_write_begin,
  583. .write_end = hugetlbfs_write_end,
  584. .set_page_dirty = hugetlbfs_set_page_dirty,
  585. .migratepage = hugetlbfs_migrate_page,
  586. };
  587. static void init_once(void *foo)
  588. {
  589. struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
  590. inode_init_once(&ei->vfs_inode);
  591. }
  592. const struct file_operations hugetlbfs_file_operations = {
  593. .read = hugetlbfs_read,
  594. .mmap = hugetlbfs_file_mmap,
  595. .fsync = noop_fsync,
  596. .get_unmapped_area = hugetlb_get_unmapped_area,
  597. .llseek = default_llseek,
  598. };
  599. static const struct inode_operations hugetlbfs_dir_inode_operations = {
  600. .create = hugetlbfs_create,
  601. .lookup = simple_lookup,
  602. .link = simple_link,
  603. .unlink = simple_unlink,
  604. .symlink = hugetlbfs_symlink,
  605. .mkdir = hugetlbfs_mkdir,
  606. .rmdir = simple_rmdir,
  607. .mknod = hugetlbfs_mknod,
  608. .rename = simple_rename,
  609. .setattr = hugetlbfs_setattr,
  610. };
  611. static const struct inode_operations hugetlbfs_inode_operations = {
  612. .setattr = hugetlbfs_setattr,
  613. };
  614. static const struct super_operations hugetlbfs_ops = {
  615. .alloc_inode = hugetlbfs_alloc_inode,
  616. .destroy_inode = hugetlbfs_destroy_inode,
  617. .evict_inode = hugetlbfs_evict_inode,
  618. .statfs = hugetlbfs_statfs,
  619. .put_super = hugetlbfs_put_super,
  620. .show_options = generic_show_options,
  621. };
  622. static int
  623. hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
  624. {
  625. char *p, *rest;
  626. substring_t args[MAX_OPT_ARGS];
  627. int option;
  628. unsigned long long size = 0;
  629. enum { NO_SIZE, SIZE_STD, SIZE_PERCENT } setsize = NO_SIZE;
  630. if (!options)
  631. return 0;
  632. while ((p = strsep(&options, ",")) != NULL) {
  633. int token;
  634. if (!*p)
  635. continue;
  636. token = match_token(p, tokens, args);
  637. switch (token) {
  638. case Opt_uid:
  639. if (match_int(&args[0], &option))
  640. goto bad_val;
  641. pconfig->uid = option;
  642. break;
  643. case Opt_gid:
  644. if (match_int(&args[0], &option))
  645. goto bad_val;
  646. pconfig->gid = option;
  647. break;
  648. case Opt_mode:
  649. if (match_octal(&args[0], &option))
  650. goto bad_val;
  651. pconfig->mode = option & 01777U;
  652. break;
  653. case Opt_size: {
  654. /* memparse() will accept a K/M/G without a digit */
  655. if (!isdigit(*args[0].from))
  656. goto bad_val;
  657. size = memparse(args[0].from, &rest);
  658. setsize = SIZE_STD;
  659. if (*rest == '%')
  660. setsize = SIZE_PERCENT;
  661. break;
  662. }
  663. case Opt_nr_inodes:
  664. /* memparse() will accept a K/M/G without a digit */
  665. if (!isdigit(*args[0].from))
  666. goto bad_val;
  667. pconfig->nr_inodes = memparse(args[0].from, &rest);
  668. break;
  669. case Opt_pagesize: {
  670. unsigned long ps;
  671. ps = memparse(args[0].from, &rest);
  672. pconfig->hstate = size_to_hstate(ps);
  673. if (!pconfig->hstate) {
  674. printk(KERN_ERR
  675. "hugetlbfs: Unsupported page size %lu MB\n",
  676. ps >> 20);
  677. return -EINVAL;
  678. }
  679. break;
  680. }
  681. default:
  682. printk(KERN_ERR "hugetlbfs: Bad mount option: \"%s\"\n",
  683. p);
  684. return -EINVAL;
  685. break;
  686. }
  687. }
  688. /* Do size after hstate is set up */
  689. if (setsize > NO_SIZE) {
  690. struct hstate *h = pconfig->hstate;
  691. if (setsize == SIZE_PERCENT) {
  692. size <<= huge_page_shift(h);
  693. size *= h->max_huge_pages;
  694. do_div(size, 100);
  695. }
  696. pconfig->nr_blocks = (size >> huge_page_shift(h));
  697. }
  698. return 0;
  699. bad_val:
  700. printk(KERN_ERR "hugetlbfs: Bad value '%s' for mount option '%s'\n",
  701. args[0].from, p);
  702. return -EINVAL;
  703. }
  704. static int
  705. hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
  706. {
  707. struct inode * inode;
  708. struct dentry * root;
  709. int ret;
  710. struct hugetlbfs_config config;
  711. struct hugetlbfs_sb_info *sbinfo;
  712. save_mount_options(sb, data);
  713. config.nr_blocks = -1; /* No limit on size by default */
  714. config.nr_inodes = -1; /* No limit on number of inodes by default */
  715. config.uid = current_fsuid();
  716. config.gid = current_fsgid();
  717. config.mode = 0755;
  718. config.hstate = &default_hstate;
  719. ret = hugetlbfs_parse_options(data, &config);
  720. if (ret)
  721. return ret;
  722. sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
  723. if (!sbinfo)
  724. return -ENOMEM;
  725. sb->s_fs_info = sbinfo;
  726. sbinfo->hstate = config.hstate;
  727. spin_lock_init(&sbinfo->stat_lock);
  728. sbinfo->max_blocks = config.nr_blocks;
  729. sbinfo->free_blocks = config.nr_blocks;
  730. sbinfo->max_inodes = config.nr_inodes;
  731. sbinfo->free_inodes = config.nr_inodes;
  732. sb->s_maxbytes = MAX_LFS_FILESIZE;
  733. sb->s_blocksize = huge_page_size(config.hstate);
  734. sb->s_blocksize_bits = huge_page_shift(config.hstate);
  735. sb->s_magic = HUGETLBFS_MAGIC;
  736. sb->s_op = &hugetlbfs_ops;
  737. sb->s_time_gran = 1;
  738. inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
  739. S_IFDIR | config.mode, 0);
  740. if (!inode)
  741. goto out_free;
  742. root = d_alloc_root(inode);
  743. if (!root) {
  744. iput(inode);
  745. goto out_free;
  746. }
  747. sb->s_root = root;
  748. return 0;
  749. out_free:
  750. kfree(sbinfo);
  751. return -ENOMEM;
  752. }
  753. int hugetlb_get_quota(struct address_space *mapping, long delta)
  754. {
  755. int ret = 0;
  756. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
  757. if (sbinfo->free_blocks > -1) {
  758. spin_lock(&sbinfo->stat_lock);
  759. if (sbinfo->free_blocks - delta >= 0)
  760. sbinfo->free_blocks -= delta;
  761. else
  762. ret = -ENOMEM;
  763. spin_unlock(&sbinfo->stat_lock);
  764. }
  765. return ret;
  766. }
  767. void hugetlb_put_quota(struct address_space *mapping, long delta)
  768. {
  769. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
  770. if (sbinfo->free_blocks > -1) {
  771. spin_lock(&sbinfo->stat_lock);
  772. sbinfo->free_blocks += delta;
  773. spin_unlock(&sbinfo->stat_lock);
  774. }
  775. }
  776. static struct dentry *hugetlbfs_mount(struct file_system_type *fs_type,
  777. int flags, const char *dev_name, void *data)
  778. {
  779. return mount_nodev(fs_type, flags, data, hugetlbfs_fill_super);
  780. }
  781. static struct file_system_type hugetlbfs_fs_type = {
  782. .name = "hugetlbfs",
  783. .mount = hugetlbfs_mount,
  784. .kill_sb = kill_litter_super,
  785. };
  786. static struct vfsmount *hugetlbfs_vfsmount;
  787. static int can_do_hugetlb_shm(void)
  788. {
  789. return capable(CAP_IPC_LOCK) || in_group_p(sysctl_hugetlb_shm_group);
  790. }
  791. struct file *hugetlb_file_setup(const char *name, size_t size,
  792. vm_flags_t acctflag,
  793. struct user_struct **user, int creat_flags)
  794. {
  795. int error = -ENOMEM;
  796. struct file *file;
  797. struct inode *inode;
  798. struct path path;
  799. struct dentry *root;
  800. struct qstr quick_string;
  801. *user = NULL;
  802. if (!hugetlbfs_vfsmount)
  803. return ERR_PTR(-ENOENT);
  804. if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
  805. *user = current_user();
  806. if (user_shm_lock(size, *user)) {
  807. printk_once(KERN_WARNING "Using mlock ulimits for SHM_HUGETLB is deprecated\n");
  808. } else {
  809. *user = NULL;
  810. return ERR_PTR(-EPERM);
  811. }
  812. }
  813. root = hugetlbfs_vfsmount->mnt_root;
  814. quick_string.name = name;
  815. quick_string.len = strlen(quick_string.name);
  816. quick_string.hash = 0;
  817. path.dentry = d_alloc(root, &quick_string);
  818. if (!path.dentry)
  819. goto out_shm_unlock;
  820. path.mnt = mntget(hugetlbfs_vfsmount);
  821. error = -ENOSPC;
  822. inode = hugetlbfs_get_inode(root->d_sb, current_fsuid(),
  823. current_fsgid(), S_IFREG | S_IRWXUGO, 0);
  824. if (!inode)
  825. goto out_dentry;
  826. error = -ENOMEM;
  827. if (hugetlb_reserve_pages(inode, 0,
  828. size >> huge_page_shift(hstate_inode(inode)), NULL,
  829. acctflag))
  830. goto out_inode;
  831. d_instantiate(path.dentry, inode);
  832. inode->i_size = size;
  833. inode->i_nlink = 0;
  834. error = -ENFILE;
  835. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  836. &hugetlbfs_file_operations);
  837. if (!file)
  838. goto out_dentry; /* inode is already attached */
  839. return file;
  840. out_inode:
  841. iput(inode);
  842. out_dentry:
  843. path_put(&path);
  844. out_shm_unlock:
  845. if (*user) {
  846. user_shm_unlock(size, *user);
  847. *user = NULL;
  848. }
  849. return ERR_PTR(error);
  850. }
  851. static int __init init_hugetlbfs_fs(void)
  852. {
  853. int error;
  854. struct vfsmount *vfsmount;
  855. error = bdi_init(&hugetlbfs_backing_dev_info);
  856. if (error)
  857. return error;
  858. hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
  859. sizeof(struct hugetlbfs_inode_info),
  860. 0, 0, init_once);
  861. if (hugetlbfs_inode_cachep == NULL)
  862. goto out2;
  863. error = register_filesystem(&hugetlbfs_fs_type);
  864. if (error)
  865. goto out;
  866. vfsmount = kern_mount(&hugetlbfs_fs_type);
  867. if (!IS_ERR(vfsmount)) {
  868. hugetlbfs_vfsmount = vfsmount;
  869. return 0;
  870. }
  871. error = PTR_ERR(vfsmount);
  872. out:
  873. if (error)
  874. kmem_cache_destroy(hugetlbfs_inode_cachep);
  875. out2:
  876. bdi_destroy(&hugetlbfs_backing_dev_info);
  877. return error;
  878. }
  879. static void __exit exit_hugetlbfs_fs(void)
  880. {
  881. kmem_cache_destroy(hugetlbfs_inode_cachep);
  882. unregister_filesystem(&hugetlbfs_fs_type);
  883. bdi_destroy(&hugetlbfs_backing_dev_info);
  884. }
  885. module_init(init_hugetlbfs_fs)
  886. module_exit(exit_hugetlbfs_fs)
  887. MODULE_LICENSE("GPL");