inode.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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. mutex_lock(&inode->i_mutex);
  207. /* validate length */
  208. if (len == 0)
  209. goto out;
  210. isize = i_size_read(inode);
  211. if (!isize)
  212. goto out;
  213. end_index = (isize - 1) >> huge_page_shift(h);
  214. for (;;) {
  215. struct page *page;
  216. unsigned long nr, ret;
  217. int ra;
  218. /* nr is the maximum number of bytes to copy from this page */
  219. nr = huge_page_size(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. }
  228. nr = nr - offset;
  229. /* Find the page */
  230. page = find_get_page(mapping, index);
  231. if (unlikely(page == NULL)) {
  232. /*
  233. * We have a HOLE, zero out the user-buffer for the
  234. * length of the hole or request.
  235. */
  236. ret = len < nr ? len : nr;
  237. if (clear_user(buf, ret))
  238. ra = -EFAULT;
  239. else
  240. ra = 0;
  241. } else {
  242. /*
  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. }
  248. if (ra < 0) {
  249. if (retval == 0)
  250. retval = ra;
  251. if (page)
  252. page_cache_release(page);
  253. goto out;
  254. }
  255. offset += ret;
  256. retval += ret;
  257. len -= ret;
  258. index += offset >> huge_page_shift(h);
  259. offset &= ~huge_page_mask(h);
  260. if (page)
  261. page_cache_release(page);
  262. /* short read or no more work */
  263. if ((ret != nr) || (len == 0))
  264. break;
  265. }
  266. out:
  267. *ppos = ((loff_t)index << huge_page_shift(h)) + offset;
  268. mutex_unlock(&inode->i_mutex);
  269. return retval;
  270. }
  271. static int hugetlbfs_write_begin(struct file *file,
  272. struct address_space *mapping,
  273. loff_t pos, unsigned len, unsigned flags,
  274. struct page **pagep, void **fsdata)
  275. {
  276. return -EINVAL;
  277. }
  278. static int hugetlbfs_write_end(struct file *file, struct address_space *mapping,
  279. loff_t pos, unsigned len, unsigned copied,
  280. struct page *page, void *fsdata)
  281. {
  282. BUG();
  283. return -EINVAL;
  284. }
  285. static void truncate_huge_page(struct page *page)
  286. {
  287. cancel_dirty_page(page, /* No IO accounting for huge pages? */0);
  288. ClearPageUptodate(page);
  289. delete_from_page_cache(page);
  290. }
  291. static void truncate_hugepages(struct inode *inode, loff_t lstart)
  292. {
  293. struct hstate *h = hstate_inode(inode);
  294. struct address_space *mapping = &inode->i_data;
  295. const pgoff_t start = lstart >> huge_page_shift(h);
  296. struct pagevec pvec;
  297. pgoff_t next;
  298. int i, freed = 0;
  299. pagevec_init(&pvec, 0);
  300. next = start;
  301. while (1) {
  302. if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  303. if (next == start)
  304. break;
  305. next = start;
  306. continue;
  307. }
  308. for (i = 0; i < pagevec_count(&pvec); ++i) {
  309. struct page *page = pvec.pages[i];
  310. lock_page(page);
  311. if (page->index > next)
  312. next = page->index;
  313. ++next;
  314. truncate_huge_page(page);
  315. unlock_page(page);
  316. freed++;
  317. }
  318. huge_pagevec_release(&pvec);
  319. }
  320. BUG_ON(!lstart && mapping->nrpages);
  321. hugetlb_unreserve_pages(inode, start, freed);
  322. }
  323. static void hugetlbfs_evict_inode(struct inode *inode)
  324. {
  325. truncate_hugepages(inode, 0);
  326. end_writeback(inode);
  327. }
  328. static inline void
  329. hugetlb_vmtruncate_list(struct prio_tree_root *root, pgoff_t pgoff)
  330. {
  331. struct vm_area_struct *vma;
  332. struct prio_tree_iter iter;
  333. vma_prio_tree_foreach(vma, &iter, root, pgoff, ULONG_MAX) {
  334. unsigned long v_offset;
  335. /*
  336. * Can the expression below overflow on 32-bit arches?
  337. * No, because the prio_tree returns us only those vmas
  338. * which overlap the truncated area starting at pgoff,
  339. * and no vma on a 32-bit arch can span beyond the 4GB.
  340. */
  341. if (vma->vm_pgoff < pgoff)
  342. v_offset = (pgoff - vma->vm_pgoff) << PAGE_SHIFT;
  343. else
  344. v_offset = 0;
  345. __unmap_hugepage_range(vma,
  346. vma->vm_start + v_offset, vma->vm_end, NULL);
  347. }
  348. }
  349. static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
  350. {
  351. pgoff_t pgoff;
  352. struct address_space *mapping = inode->i_mapping;
  353. struct hstate *h = hstate_inode(inode);
  354. BUG_ON(offset & ~huge_page_mask(h));
  355. pgoff = offset >> PAGE_SHIFT;
  356. i_size_write(inode, offset);
  357. mutex_lock(&mapping->i_mmap_mutex);
  358. if (!prio_tree_empty(&mapping->i_mmap))
  359. hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
  360. mutex_unlock(&mapping->i_mmap_mutex);
  361. truncate_hugepages(inode, offset);
  362. return 0;
  363. }
  364. static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
  365. {
  366. struct inode *inode = dentry->d_inode;
  367. struct hstate *h = hstate_inode(inode);
  368. int error;
  369. unsigned int ia_valid = attr->ia_valid;
  370. BUG_ON(!inode);
  371. error = inode_change_ok(inode, attr);
  372. if (error)
  373. return error;
  374. if (ia_valid & ATTR_SIZE) {
  375. error = -EINVAL;
  376. if (attr->ia_size & ~huge_page_mask(h))
  377. return -EINVAL;
  378. error = hugetlb_vmtruncate(inode, attr->ia_size);
  379. if (error)
  380. return error;
  381. }
  382. setattr_copy(inode, attr);
  383. mark_inode_dirty(inode);
  384. return 0;
  385. }
  386. static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
  387. gid_t gid, int mode, dev_t dev)
  388. {
  389. struct inode *inode;
  390. inode = new_inode(sb);
  391. if (inode) {
  392. struct hugetlbfs_inode_info *info;
  393. inode->i_ino = get_next_ino();
  394. inode->i_mode = mode;
  395. inode->i_uid = uid;
  396. inode->i_gid = gid;
  397. inode->i_mapping->a_ops = &hugetlbfs_aops;
  398. inode->i_mapping->backing_dev_info =&hugetlbfs_backing_dev_info;
  399. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  400. INIT_LIST_HEAD(&inode->i_mapping->private_list);
  401. info = HUGETLBFS_I(inode);
  402. /*
  403. * The policy is initialized here even if we are creating a
  404. * private inode because initialization simply creates an
  405. * an empty rb tree and calls spin_lock_init(), later when we
  406. * call mpol_free_shared_policy() it will just return because
  407. * the rb tree will still be empty.
  408. */
  409. mpol_shared_policy_init(&info->policy, NULL);
  410. switch (mode & S_IFMT) {
  411. default:
  412. init_special_inode(inode, mode, dev);
  413. break;
  414. case S_IFREG:
  415. inode->i_op = &hugetlbfs_inode_operations;
  416. inode->i_fop = &hugetlbfs_file_operations;
  417. break;
  418. case S_IFDIR:
  419. inode->i_op = &hugetlbfs_dir_inode_operations;
  420. inode->i_fop = &simple_dir_operations;
  421. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  422. inc_nlink(inode);
  423. break;
  424. case S_IFLNK:
  425. inode->i_op = &page_symlink_inode_operations;
  426. break;
  427. }
  428. }
  429. return inode;
  430. }
  431. /*
  432. * File creation. Allocate an inode, and we're done..
  433. */
  434. static int hugetlbfs_mknod(struct inode *dir,
  435. struct dentry *dentry, int mode, dev_t dev)
  436. {
  437. struct inode *inode;
  438. int error = -ENOSPC;
  439. gid_t gid;
  440. if (dir->i_mode & S_ISGID) {
  441. gid = dir->i_gid;
  442. if (S_ISDIR(mode))
  443. mode |= S_ISGID;
  444. } else {
  445. gid = current_fsgid();
  446. }
  447. inode = hugetlbfs_get_inode(dir->i_sb, current_fsuid(), gid, mode, dev);
  448. if (inode) {
  449. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  450. d_instantiate(dentry, inode);
  451. dget(dentry); /* Extra count - pin the dentry in core */
  452. error = 0;
  453. }
  454. return error;
  455. }
  456. static int hugetlbfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  457. {
  458. int retval = hugetlbfs_mknod(dir, dentry, mode | S_IFDIR, 0);
  459. if (!retval)
  460. inc_nlink(dir);
  461. return retval;
  462. }
  463. static int hugetlbfs_create(struct inode *dir, struct dentry *dentry, int mode, struct nameidata *nd)
  464. {
  465. return hugetlbfs_mknod(dir, dentry, mode | S_IFREG, 0);
  466. }
  467. static int hugetlbfs_symlink(struct inode *dir,
  468. struct dentry *dentry, const char *symname)
  469. {
  470. struct inode *inode;
  471. int error = -ENOSPC;
  472. gid_t gid;
  473. if (dir->i_mode & S_ISGID)
  474. gid = dir->i_gid;
  475. else
  476. gid = current_fsgid();
  477. inode = hugetlbfs_get_inode(dir->i_sb, current_fsuid(),
  478. gid, S_IFLNK|S_IRWXUGO, 0);
  479. if (inode) {
  480. int l = strlen(symname)+1;
  481. error = page_symlink(inode, symname, l);
  482. if (!error) {
  483. d_instantiate(dentry, inode);
  484. dget(dentry);
  485. } else
  486. iput(inode);
  487. }
  488. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  489. return error;
  490. }
  491. /*
  492. * mark the head page dirty
  493. */
  494. static int hugetlbfs_set_page_dirty(struct page *page)
  495. {
  496. struct page *head = compound_head(page);
  497. SetPageDirty(head);
  498. return 0;
  499. }
  500. static int hugetlbfs_migrate_page(struct address_space *mapping,
  501. struct page *newpage, struct page *page)
  502. {
  503. int rc;
  504. rc = migrate_huge_page_move_mapping(mapping, newpage, page);
  505. if (rc)
  506. return rc;
  507. migrate_page_copy(newpage, page);
  508. return 0;
  509. }
  510. static int hugetlbfs_statfs(struct dentry *dentry, struct kstatfs *buf)
  511. {
  512. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(dentry->d_sb);
  513. struct hstate *h = hstate_inode(dentry->d_inode);
  514. buf->f_type = HUGETLBFS_MAGIC;
  515. buf->f_bsize = huge_page_size(h);
  516. if (sbinfo) {
  517. spin_lock(&sbinfo->stat_lock);
  518. /* If no limits set, just report 0 for max/free/used
  519. * blocks, like simple_statfs() */
  520. if (sbinfo->max_blocks >= 0) {
  521. buf->f_blocks = sbinfo->max_blocks;
  522. buf->f_bavail = buf->f_bfree = sbinfo->free_blocks;
  523. buf->f_files = sbinfo->max_inodes;
  524. buf->f_ffree = sbinfo->free_inodes;
  525. }
  526. spin_unlock(&sbinfo->stat_lock);
  527. }
  528. buf->f_namelen = NAME_MAX;
  529. return 0;
  530. }
  531. static void hugetlbfs_put_super(struct super_block *sb)
  532. {
  533. struct hugetlbfs_sb_info *sbi = HUGETLBFS_SB(sb);
  534. if (sbi) {
  535. sb->s_fs_info = NULL;
  536. kfree(sbi);
  537. }
  538. }
  539. static inline int hugetlbfs_dec_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  540. {
  541. if (sbinfo->free_inodes >= 0) {
  542. spin_lock(&sbinfo->stat_lock);
  543. if (unlikely(!sbinfo->free_inodes)) {
  544. spin_unlock(&sbinfo->stat_lock);
  545. return 0;
  546. }
  547. sbinfo->free_inodes--;
  548. spin_unlock(&sbinfo->stat_lock);
  549. }
  550. return 1;
  551. }
  552. static void hugetlbfs_inc_free_inodes(struct hugetlbfs_sb_info *sbinfo)
  553. {
  554. if (sbinfo->free_inodes >= 0) {
  555. spin_lock(&sbinfo->stat_lock);
  556. sbinfo->free_inodes++;
  557. spin_unlock(&sbinfo->stat_lock);
  558. }
  559. }
  560. static struct kmem_cache *hugetlbfs_inode_cachep;
  561. static struct inode *hugetlbfs_alloc_inode(struct super_block *sb)
  562. {
  563. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(sb);
  564. struct hugetlbfs_inode_info *p;
  565. if (unlikely(!hugetlbfs_dec_free_inodes(sbinfo)))
  566. return NULL;
  567. p = kmem_cache_alloc(hugetlbfs_inode_cachep, GFP_KERNEL);
  568. if (unlikely(!p)) {
  569. hugetlbfs_inc_free_inodes(sbinfo);
  570. return NULL;
  571. }
  572. return &p->vfs_inode;
  573. }
  574. static void hugetlbfs_i_callback(struct rcu_head *head)
  575. {
  576. struct inode *inode = container_of(head, struct inode, i_rcu);
  577. INIT_LIST_HEAD(&inode->i_dentry);
  578. kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
  579. }
  580. static void hugetlbfs_destroy_inode(struct inode *inode)
  581. {
  582. hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
  583. mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
  584. call_rcu(&inode->i_rcu, hugetlbfs_i_callback);
  585. }
  586. static const struct address_space_operations hugetlbfs_aops = {
  587. .write_begin = hugetlbfs_write_begin,
  588. .write_end = hugetlbfs_write_end,
  589. .set_page_dirty = hugetlbfs_set_page_dirty,
  590. .migratepage = hugetlbfs_migrate_page,
  591. };
  592. static void init_once(void *foo)
  593. {
  594. struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
  595. inode_init_once(&ei->vfs_inode);
  596. }
  597. const struct file_operations hugetlbfs_file_operations = {
  598. .read = hugetlbfs_read,
  599. .mmap = hugetlbfs_file_mmap,
  600. .fsync = noop_fsync,
  601. .get_unmapped_area = hugetlb_get_unmapped_area,
  602. .llseek = default_llseek,
  603. };
  604. static const struct inode_operations hugetlbfs_dir_inode_operations = {
  605. .create = hugetlbfs_create,
  606. .lookup = simple_lookup,
  607. .link = simple_link,
  608. .unlink = simple_unlink,
  609. .symlink = hugetlbfs_symlink,
  610. .mkdir = hugetlbfs_mkdir,
  611. .rmdir = simple_rmdir,
  612. .mknod = hugetlbfs_mknod,
  613. .rename = simple_rename,
  614. .setattr = hugetlbfs_setattr,
  615. };
  616. static const struct inode_operations hugetlbfs_inode_operations = {
  617. .setattr = hugetlbfs_setattr,
  618. };
  619. static const struct super_operations hugetlbfs_ops = {
  620. .alloc_inode = hugetlbfs_alloc_inode,
  621. .destroy_inode = hugetlbfs_destroy_inode,
  622. .evict_inode = hugetlbfs_evict_inode,
  623. .statfs = hugetlbfs_statfs,
  624. .put_super = hugetlbfs_put_super,
  625. .show_options = generic_show_options,
  626. };
  627. static int
  628. hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
  629. {
  630. char *p, *rest;
  631. substring_t args[MAX_OPT_ARGS];
  632. int option;
  633. unsigned long long size = 0;
  634. enum { NO_SIZE, SIZE_STD, SIZE_PERCENT } setsize = NO_SIZE;
  635. if (!options)
  636. return 0;
  637. while ((p = strsep(&options, ",")) != NULL) {
  638. int token;
  639. if (!*p)
  640. continue;
  641. token = match_token(p, tokens, args);
  642. switch (token) {
  643. case Opt_uid:
  644. if (match_int(&args[0], &option))
  645. goto bad_val;
  646. pconfig->uid = option;
  647. break;
  648. case Opt_gid:
  649. if (match_int(&args[0], &option))
  650. goto bad_val;
  651. pconfig->gid = option;
  652. break;
  653. case Opt_mode:
  654. if (match_octal(&args[0], &option))
  655. goto bad_val;
  656. pconfig->mode = option & 01777U;
  657. break;
  658. case Opt_size: {
  659. /* memparse() will accept a K/M/G without a digit */
  660. if (!isdigit(*args[0].from))
  661. goto bad_val;
  662. size = memparse(args[0].from, &rest);
  663. setsize = SIZE_STD;
  664. if (*rest == '%')
  665. setsize = SIZE_PERCENT;
  666. break;
  667. }
  668. case Opt_nr_inodes:
  669. /* memparse() will accept a K/M/G without a digit */
  670. if (!isdigit(*args[0].from))
  671. goto bad_val;
  672. pconfig->nr_inodes = memparse(args[0].from, &rest);
  673. break;
  674. case Opt_pagesize: {
  675. unsigned long ps;
  676. ps = memparse(args[0].from, &rest);
  677. pconfig->hstate = size_to_hstate(ps);
  678. if (!pconfig->hstate) {
  679. printk(KERN_ERR
  680. "hugetlbfs: Unsupported page size %lu MB\n",
  681. ps >> 20);
  682. return -EINVAL;
  683. }
  684. break;
  685. }
  686. default:
  687. printk(KERN_ERR "hugetlbfs: Bad mount option: \"%s\"\n",
  688. p);
  689. return -EINVAL;
  690. break;
  691. }
  692. }
  693. /* Do size after hstate is set up */
  694. if (setsize > NO_SIZE) {
  695. struct hstate *h = pconfig->hstate;
  696. if (setsize == SIZE_PERCENT) {
  697. size <<= huge_page_shift(h);
  698. size *= h->max_huge_pages;
  699. do_div(size, 100);
  700. }
  701. pconfig->nr_blocks = (size >> huge_page_shift(h));
  702. }
  703. return 0;
  704. bad_val:
  705. printk(KERN_ERR "hugetlbfs: Bad value '%s' for mount option '%s'\n",
  706. args[0].from, p);
  707. return -EINVAL;
  708. }
  709. static int
  710. hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
  711. {
  712. struct inode * inode;
  713. struct dentry * root;
  714. int ret;
  715. struct hugetlbfs_config config;
  716. struct hugetlbfs_sb_info *sbinfo;
  717. save_mount_options(sb, data);
  718. config.nr_blocks = -1; /* No limit on size by default */
  719. config.nr_inodes = -1; /* No limit on number of inodes by default */
  720. config.uid = current_fsuid();
  721. config.gid = current_fsgid();
  722. config.mode = 0755;
  723. config.hstate = &default_hstate;
  724. ret = hugetlbfs_parse_options(data, &config);
  725. if (ret)
  726. return ret;
  727. sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
  728. if (!sbinfo)
  729. return -ENOMEM;
  730. sb->s_fs_info = sbinfo;
  731. sbinfo->hstate = config.hstate;
  732. spin_lock_init(&sbinfo->stat_lock);
  733. sbinfo->max_blocks = config.nr_blocks;
  734. sbinfo->free_blocks = config.nr_blocks;
  735. sbinfo->max_inodes = config.nr_inodes;
  736. sbinfo->free_inodes = config.nr_inodes;
  737. sb->s_maxbytes = MAX_LFS_FILESIZE;
  738. sb->s_blocksize = huge_page_size(config.hstate);
  739. sb->s_blocksize_bits = huge_page_shift(config.hstate);
  740. sb->s_magic = HUGETLBFS_MAGIC;
  741. sb->s_op = &hugetlbfs_ops;
  742. sb->s_time_gran = 1;
  743. inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
  744. S_IFDIR | config.mode, 0);
  745. if (!inode)
  746. goto out_free;
  747. root = d_alloc_root(inode);
  748. if (!root) {
  749. iput(inode);
  750. goto out_free;
  751. }
  752. sb->s_root = root;
  753. return 0;
  754. out_free:
  755. kfree(sbinfo);
  756. return -ENOMEM;
  757. }
  758. int hugetlb_get_quota(struct address_space *mapping, long delta)
  759. {
  760. int ret = 0;
  761. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
  762. if (sbinfo->free_blocks > -1) {
  763. spin_lock(&sbinfo->stat_lock);
  764. if (sbinfo->free_blocks - delta >= 0)
  765. sbinfo->free_blocks -= delta;
  766. else
  767. ret = -ENOMEM;
  768. spin_unlock(&sbinfo->stat_lock);
  769. }
  770. return ret;
  771. }
  772. void hugetlb_put_quota(struct address_space *mapping, long delta)
  773. {
  774. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
  775. if (sbinfo->free_blocks > -1) {
  776. spin_lock(&sbinfo->stat_lock);
  777. sbinfo->free_blocks += delta;
  778. spin_unlock(&sbinfo->stat_lock);
  779. }
  780. }
  781. static struct dentry *hugetlbfs_mount(struct file_system_type *fs_type,
  782. int flags, const char *dev_name, void *data)
  783. {
  784. return mount_nodev(fs_type, flags, data, hugetlbfs_fill_super);
  785. }
  786. static struct file_system_type hugetlbfs_fs_type = {
  787. .name = "hugetlbfs",
  788. .mount = hugetlbfs_mount,
  789. .kill_sb = kill_litter_super,
  790. };
  791. static struct vfsmount *hugetlbfs_vfsmount;
  792. static int can_do_hugetlb_shm(void)
  793. {
  794. return capable(CAP_IPC_LOCK) || in_group_p(sysctl_hugetlb_shm_group);
  795. }
  796. struct file *hugetlb_file_setup(const char *name, size_t size,
  797. vm_flags_t acctflag,
  798. struct user_struct **user, int creat_flags)
  799. {
  800. int error = -ENOMEM;
  801. struct file *file;
  802. struct inode *inode;
  803. struct path path;
  804. struct dentry *root;
  805. struct qstr quick_string;
  806. *user = NULL;
  807. if (!hugetlbfs_vfsmount)
  808. return ERR_PTR(-ENOENT);
  809. if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
  810. *user = current_user();
  811. if (user_shm_lock(size, *user)) {
  812. printk_once(KERN_WARNING "Using mlock ulimits for SHM_HUGETLB is deprecated\n");
  813. } else {
  814. *user = NULL;
  815. return ERR_PTR(-EPERM);
  816. }
  817. }
  818. root = hugetlbfs_vfsmount->mnt_root;
  819. quick_string.name = name;
  820. quick_string.len = strlen(quick_string.name);
  821. quick_string.hash = 0;
  822. path.dentry = d_alloc(root, &quick_string);
  823. if (!path.dentry)
  824. goto out_shm_unlock;
  825. path.mnt = mntget(hugetlbfs_vfsmount);
  826. error = -ENOSPC;
  827. inode = hugetlbfs_get_inode(root->d_sb, current_fsuid(),
  828. current_fsgid(), S_IFREG | S_IRWXUGO, 0);
  829. if (!inode)
  830. goto out_dentry;
  831. error = -ENOMEM;
  832. if (hugetlb_reserve_pages(inode, 0,
  833. size >> huge_page_shift(hstate_inode(inode)), NULL,
  834. acctflag))
  835. goto out_inode;
  836. d_instantiate(path.dentry, inode);
  837. inode->i_size = size;
  838. inode->i_nlink = 0;
  839. error = -ENFILE;
  840. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  841. &hugetlbfs_file_operations);
  842. if (!file)
  843. goto out_dentry; /* inode is already attached */
  844. return file;
  845. out_inode:
  846. iput(inode);
  847. out_dentry:
  848. path_put(&path);
  849. out_shm_unlock:
  850. if (*user) {
  851. user_shm_unlock(size, *user);
  852. *user = NULL;
  853. }
  854. return ERR_PTR(error);
  855. }
  856. static int __init init_hugetlbfs_fs(void)
  857. {
  858. int error;
  859. struct vfsmount *vfsmount;
  860. error = bdi_init(&hugetlbfs_backing_dev_info);
  861. if (error)
  862. return error;
  863. hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
  864. sizeof(struct hugetlbfs_inode_info),
  865. 0, 0, init_once);
  866. if (hugetlbfs_inode_cachep == NULL)
  867. goto out2;
  868. error = register_filesystem(&hugetlbfs_fs_type);
  869. if (error)
  870. goto out;
  871. vfsmount = kern_mount(&hugetlbfs_fs_type);
  872. if (!IS_ERR(vfsmount)) {
  873. hugetlbfs_vfsmount = vfsmount;
  874. return 0;
  875. }
  876. error = PTR_ERR(vfsmount);
  877. out:
  878. if (error)
  879. kmem_cache_destroy(hugetlbfs_inode_cachep);
  880. out2:
  881. bdi_destroy(&hugetlbfs_backing_dev_info);
  882. return error;
  883. }
  884. static void __exit exit_hugetlbfs_fs(void)
  885. {
  886. kmem_cache_destroy(hugetlbfs_inode_cachep);
  887. kern_unmount(hugetlbfs_vfsmount);
  888. unregister_filesystem(&hugetlbfs_fs_type);
  889. bdi_destroy(&hugetlbfs_backing_dev_info);
  890. }
  891. module_init(init_hugetlbfs_fs)
  892. module_exit(exit_hugetlbfs_fs)
  893. MODULE_LICENSE("GPL");