inode.c 23 KB

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