inode.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. remove_from_page_cache(page);
  290. put_page(page);
  291. }
  292. static void truncate_hugepages(struct inode *inode, loff_t lstart)
  293. {
  294. struct hstate *h = hstate_inode(inode);
  295. struct address_space *mapping = &inode->i_data;
  296. const pgoff_t start = lstart >> huge_page_shift(h);
  297. struct pagevec pvec;
  298. pgoff_t next;
  299. int i, freed = 0;
  300. pagevec_init(&pvec, 0);
  301. next = start;
  302. while (1) {
  303. if (!pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) {
  304. if (next == start)
  305. break;
  306. next = start;
  307. continue;
  308. }
  309. for (i = 0; i < pagevec_count(&pvec); ++i) {
  310. struct page *page = pvec.pages[i];
  311. lock_page(page);
  312. if (page->index > next)
  313. next = page->index;
  314. ++next;
  315. truncate_huge_page(page);
  316. unlock_page(page);
  317. freed++;
  318. }
  319. huge_pagevec_release(&pvec);
  320. }
  321. BUG_ON(!lstart && mapping->nrpages);
  322. hugetlb_unreserve_pages(inode, start, freed);
  323. }
  324. static void hugetlbfs_evict_inode(struct inode *inode)
  325. {
  326. truncate_hugepages(inode, 0);
  327. end_writeback(inode);
  328. }
  329. static inline void
  330. hugetlb_vmtruncate_list(struct prio_tree_root *root, pgoff_t pgoff)
  331. {
  332. struct vm_area_struct *vma;
  333. struct prio_tree_iter iter;
  334. vma_prio_tree_foreach(vma, &iter, root, pgoff, ULONG_MAX) {
  335. unsigned long v_offset;
  336. /*
  337. * Can the expression below overflow on 32-bit arches?
  338. * No, because the prio_tree returns us only those vmas
  339. * which overlap the truncated area starting at pgoff,
  340. * and no vma on a 32-bit arch can span beyond the 4GB.
  341. */
  342. if (vma->vm_pgoff < pgoff)
  343. v_offset = (pgoff - vma->vm_pgoff) << PAGE_SHIFT;
  344. else
  345. v_offset = 0;
  346. __unmap_hugepage_range(vma,
  347. vma->vm_start + v_offset, vma->vm_end, NULL);
  348. }
  349. }
  350. static int hugetlb_vmtruncate(struct inode *inode, loff_t offset)
  351. {
  352. pgoff_t pgoff;
  353. struct address_space *mapping = inode->i_mapping;
  354. struct hstate *h = hstate_inode(inode);
  355. BUG_ON(offset & ~huge_page_mask(h));
  356. pgoff = offset >> PAGE_SHIFT;
  357. i_size_write(inode, offset);
  358. spin_lock(&mapping->i_mmap_lock);
  359. if (!prio_tree_empty(&mapping->i_mmap))
  360. hugetlb_vmtruncate_list(&mapping->i_mmap, pgoff);
  361. spin_unlock(&mapping->i_mmap_lock);
  362. truncate_hugepages(inode, offset);
  363. return 0;
  364. }
  365. static int hugetlbfs_setattr(struct dentry *dentry, struct iattr *attr)
  366. {
  367. struct inode *inode = dentry->d_inode;
  368. struct hstate *h = hstate_inode(inode);
  369. int error;
  370. unsigned int ia_valid = attr->ia_valid;
  371. BUG_ON(!inode);
  372. error = inode_change_ok(inode, attr);
  373. if (error)
  374. return error;
  375. if (ia_valid & ATTR_SIZE) {
  376. error = -EINVAL;
  377. if (attr->ia_size & ~huge_page_mask(h))
  378. return -EINVAL;
  379. error = hugetlb_vmtruncate(inode, attr->ia_size);
  380. if (error)
  381. return error;
  382. }
  383. setattr_copy(inode, attr);
  384. mark_inode_dirty(inode);
  385. return 0;
  386. }
  387. static struct inode *hugetlbfs_get_inode(struct super_block *sb, uid_t uid,
  388. gid_t gid, int mode, dev_t dev)
  389. {
  390. struct inode *inode;
  391. inode = new_inode(sb);
  392. if (inode) {
  393. struct hugetlbfs_inode_info *info;
  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_destroy_inode(struct inode *inode)
  575. {
  576. hugetlbfs_inc_free_inodes(HUGETLBFS_SB(inode->i_sb));
  577. mpol_free_shared_policy(&HUGETLBFS_I(inode)->policy);
  578. kmem_cache_free(hugetlbfs_inode_cachep, HUGETLBFS_I(inode));
  579. }
  580. static const struct address_space_operations hugetlbfs_aops = {
  581. .write_begin = hugetlbfs_write_begin,
  582. .write_end = hugetlbfs_write_end,
  583. .set_page_dirty = hugetlbfs_set_page_dirty,
  584. .migratepage = hugetlbfs_migrate_page,
  585. };
  586. static void init_once(void *foo)
  587. {
  588. struct hugetlbfs_inode_info *ei = (struct hugetlbfs_inode_info *)foo;
  589. inode_init_once(&ei->vfs_inode);
  590. }
  591. const struct file_operations hugetlbfs_file_operations = {
  592. .read = hugetlbfs_read,
  593. .mmap = hugetlbfs_file_mmap,
  594. .fsync = noop_fsync,
  595. .get_unmapped_area = hugetlb_get_unmapped_area,
  596. };
  597. static const struct inode_operations hugetlbfs_dir_inode_operations = {
  598. .create = hugetlbfs_create,
  599. .lookup = simple_lookup,
  600. .link = simple_link,
  601. .unlink = simple_unlink,
  602. .symlink = hugetlbfs_symlink,
  603. .mkdir = hugetlbfs_mkdir,
  604. .rmdir = simple_rmdir,
  605. .mknod = hugetlbfs_mknod,
  606. .rename = simple_rename,
  607. .setattr = hugetlbfs_setattr,
  608. };
  609. static const struct inode_operations hugetlbfs_inode_operations = {
  610. .setattr = hugetlbfs_setattr,
  611. };
  612. static const struct super_operations hugetlbfs_ops = {
  613. .alloc_inode = hugetlbfs_alloc_inode,
  614. .destroy_inode = hugetlbfs_destroy_inode,
  615. .evict_inode = hugetlbfs_evict_inode,
  616. .statfs = hugetlbfs_statfs,
  617. .put_super = hugetlbfs_put_super,
  618. .show_options = generic_show_options,
  619. };
  620. static int
  621. hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
  622. {
  623. char *p, *rest;
  624. substring_t args[MAX_OPT_ARGS];
  625. int option;
  626. unsigned long long size = 0;
  627. enum { NO_SIZE, SIZE_STD, SIZE_PERCENT } setsize = NO_SIZE;
  628. if (!options)
  629. return 0;
  630. while ((p = strsep(&options, ",")) != NULL) {
  631. int token;
  632. if (!*p)
  633. continue;
  634. token = match_token(p, tokens, args);
  635. switch (token) {
  636. case Opt_uid:
  637. if (match_int(&args[0], &option))
  638. goto bad_val;
  639. pconfig->uid = option;
  640. break;
  641. case Opt_gid:
  642. if (match_int(&args[0], &option))
  643. goto bad_val;
  644. pconfig->gid = option;
  645. break;
  646. case Opt_mode:
  647. if (match_octal(&args[0], &option))
  648. goto bad_val;
  649. pconfig->mode = option & 01777U;
  650. break;
  651. case Opt_size: {
  652. /* memparse() will accept a K/M/G without a digit */
  653. if (!isdigit(*args[0].from))
  654. goto bad_val;
  655. size = memparse(args[0].from, &rest);
  656. setsize = SIZE_STD;
  657. if (*rest == '%')
  658. setsize = SIZE_PERCENT;
  659. break;
  660. }
  661. case Opt_nr_inodes:
  662. /* memparse() will accept a K/M/G without a digit */
  663. if (!isdigit(*args[0].from))
  664. goto bad_val;
  665. pconfig->nr_inodes = memparse(args[0].from, &rest);
  666. break;
  667. case Opt_pagesize: {
  668. unsigned long ps;
  669. ps = memparse(args[0].from, &rest);
  670. pconfig->hstate = size_to_hstate(ps);
  671. if (!pconfig->hstate) {
  672. printk(KERN_ERR
  673. "hugetlbfs: Unsupported page size %lu MB\n",
  674. ps >> 20);
  675. return -EINVAL;
  676. }
  677. break;
  678. }
  679. default:
  680. printk(KERN_ERR "hugetlbfs: Bad mount option: \"%s\"\n",
  681. p);
  682. return -EINVAL;
  683. break;
  684. }
  685. }
  686. /* Do size after hstate is set up */
  687. if (setsize > NO_SIZE) {
  688. struct hstate *h = pconfig->hstate;
  689. if (setsize == SIZE_PERCENT) {
  690. size <<= huge_page_shift(h);
  691. size *= h->max_huge_pages;
  692. do_div(size, 100);
  693. }
  694. pconfig->nr_blocks = (size >> huge_page_shift(h));
  695. }
  696. return 0;
  697. bad_val:
  698. printk(KERN_ERR "hugetlbfs: Bad value '%s' for mount option '%s'\n",
  699. args[0].from, p);
  700. return -EINVAL;
  701. }
  702. static int
  703. hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
  704. {
  705. struct inode * inode;
  706. struct dentry * root;
  707. int ret;
  708. struct hugetlbfs_config config;
  709. struct hugetlbfs_sb_info *sbinfo;
  710. save_mount_options(sb, data);
  711. config.nr_blocks = -1; /* No limit on size by default */
  712. config.nr_inodes = -1; /* No limit on number of inodes by default */
  713. config.uid = current_fsuid();
  714. config.gid = current_fsgid();
  715. config.mode = 0755;
  716. config.hstate = &default_hstate;
  717. ret = hugetlbfs_parse_options(data, &config);
  718. if (ret)
  719. return ret;
  720. sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
  721. if (!sbinfo)
  722. return -ENOMEM;
  723. sb->s_fs_info = sbinfo;
  724. sbinfo->hstate = config.hstate;
  725. spin_lock_init(&sbinfo->stat_lock);
  726. sbinfo->max_blocks = config.nr_blocks;
  727. sbinfo->free_blocks = config.nr_blocks;
  728. sbinfo->max_inodes = config.nr_inodes;
  729. sbinfo->free_inodes = config.nr_inodes;
  730. sb->s_maxbytes = MAX_LFS_FILESIZE;
  731. sb->s_blocksize = huge_page_size(config.hstate);
  732. sb->s_blocksize_bits = huge_page_shift(config.hstate);
  733. sb->s_magic = HUGETLBFS_MAGIC;
  734. sb->s_op = &hugetlbfs_ops;
  735. sb->s_time_gran = 1;
  736. inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
  737. S_IFDIR | config.mode, 0);
  738. if (!inode)
  739. goto out_free;
  740. root = d_alloc_root(inode);
  741. if (!root) {
  742. iput(inode);
  743. goto out_free;
  744. }
  745. sb->s_root = root;
  746. return 0;
  747. out_free:
  748. kfree(sbinfo);
  749. return -ENOMEM;
  750. }
  751. int hugetlb_get_quota(struct address_space *mapping, long delta)
  752. {
  753. int ret = 0;
  754. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
  755. if (sbinfo->free_blocks > -1) {
  756. spin_lock(&sbinfo->stat_lock);
  757. if (sbinfo->free_blocks - delta >= 0)
  758. sbinfo->free_blocks -= delta;
  759. else
  760. ret = -ENOMEM;
  761. spin_unlock(&sbinfo->stat_lock);
  762. }
  763. return ret;
  764. }
  765. void hugetlb_put_quota(struct address_space *mapping, long delta)
  766. {
  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. sbinfo->free_blocks += delta;
  771. spin_unlock(&sbinfo->stat_lock);
  772. }
  773. }
  774. static int hugetlbfs_get_sb(struct file_system_type *fs_type,
  775. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  776. {
  777. return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super, mnt);
  778. }
  779. static struct file_system_type hugetlbfs_fs_type = {
  780. .name = "hugetlbfs",
  781. .get_sb = hugetlbfs_get_sb,
  782. .kill_sb = kill_litter_super,
  783. };
  784. static struct vfsmount *hugetlbfs_vfsmount;
  785. static int can_do_hugetlb_shm(void)
  786. {
  787. return capable(CAP_IPC_LOCK) || in_group_p(sysctl_hugetlb_shm_group);
  788. }
  789. struct file *hugetlb_file_setup(const char *name, size_t size, int acctflag,
  790. struct user_struct **user, int creat_flags)
  791. {
  792. int error = -ENOMEM;
  793. struct file *file;
  794. struct inode *inode;
  795. struct path path;
  796. struct dentry *root;
  797. struct qstr quick_string;
  798. *user = NULL;
  799. if (!hugetlbfs_vfsmount)
  800. return ERR_PTR(-ENOENT);
  801. if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
  802. *user = current_user();
  803. if (user_shm_lock(size, *user)) {
  804. WARN_ONCE(1,
  805. "Using mlock ulimits for SHM_HUGETLB deprecated\n");
  806. } else {
  807. *user = NULL;
  808. return ERR_PTR(-EPERM);
  809. }
  810. }
  811. root = hugetlbfs_vfsmount->mnt_root;
  812. quick_string.name = name;
  813. quick_string.len = strlen(quick_string.name);
  814. quick_string.hash = 0;
  815. path.dentry = d_alloc(root, &quick_string);
  816. if (!path.dentry)
  817. goto out_shm_unlock;
  818. path.mnt = mntget(hugetlbfs_vfsmount);
  819. error = -ENOSPC;
  820. inode = hugetlbfs_get_inode(root->d_sb, current_fsuid(),
  821. current_fsgid(), S_IFREG | S_IRWXUGO, 0);
  822. if (!inode)
  823. goto out_dentry;
  824. error = -ENOMEM;
  825. if (hugetlb_reserve_pages(inode, 0,
  826. size >> huge_page_shift(hstate_inode(inode)), NULL,
  827. acctflag))
  828. goto out_inode;
  829. d_instantiate(path.dentry, inode);
  830. inode->i_size = size;
  831. inode->i_nlink = 0;
  832. error = -ENFILE;
  833. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  834. &hugetlbfs_file_operations);
  835. if (!file)
  836. goto out_dentry; /* inode is already attached */
  837. return file;
  838. out_inode:
  839. iput(inode);
  840. out_dentry:
  841. path_put(&path);
  842. out_shm_unlock:
  843. if (*user) {
  844. user_shm_unlock(size, *user);
  845. *user = NULL;
  846. }
  847. return ERR_PTR(error);
  848. }
  849. static int __init init_hugetlbfs_fs(void)
  850. {
  851. int error;
  852. struct vfsmount *vfsmount;
  853. error = bdi_init(&hugetlbfs_backing_dev_info);
  854. if (error)
  855. return error;
  856. hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
  857. sizeof(struct hugetlbfs_inode_info),
  858. 0, 0, init_once);
  859. if (hugetlbfs_inode_cachep == NULL)
  860. goto out2;
  861. error = register_filesystem(&hugetlbfs_fs_type);
  862. if (error)
  863. goto out;
  864. vfsmount = kern_mount(&hugetlbfs_fs_type);
  865. if (!IS_ERR(vfsmount)) {
  866. hugetlbfs_vfsmount = vfsmount;
  867. return 0;
  868. }
  869. error = PTR_ERR(vfsmount);
  870. out:
  871. if (error)
  872. kmem_cache_destroy(hugetlbfs_inode_cachep);
  873. out2:
  874. bdi_destroy(&hugetlbfs_backing_dev_info);
  875. return error;
  876. }
  877. static void __exit exit_hugetlbfs_fs(void)
  878. {
  879. kmem_cache_destroy(hugetlbfs_inode_cachep);
  880. unregister_filesystem(&hugetlbfs_fs_type);
  881. bdi_destroy(&hugetlbfs_backing_dev_info);
  882. }
  883. module_init(init_hugetlbfs_fs)
  884. module_exit(exit_hugetlbfs_fs)
  885. MODULE_LICENSE("GPL");