inode.c 24 KB

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