inode.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018
  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. .llseek = default_llseek,
  585. };
  586. static const struct inode_operations hugetlbfs_dir_inode_operations = {
  587. .create = hugetlbfs_create,
  588. .lookup = simple_lookup,
  589. .link = simple_link,
  590. .unlink = simple_unlink,
  591. .symlink = hugetlbfs_symlink,
  592. .mkdir = hugetlbfs_mkdir,
  593. .rmdir = simple_rmdir,
  594. .mknod = hugetlbfs_mknod,
  595. .rename = simple_rename,
  596. .setattr = hugetlbfs_setattr,
  597. };
  598. static const struct inode_operations hugetlbfs_inode_operations = {
  599. .setattr = hugetlbfs_setattr,
  600. };
  601. static const struct super_operations hugetlbfs_ops = {
  602. .alloc_inode = hugetlbfs_alloc_inode,
  603. .destroy_inode = hugetlbfs_destroy_inode,
  604. .evict_inode = hugetlbfs_evict_inode,
  605. .statfs = hugetlbfs_statfs,
  606. .put_super = hugetlbfs_put_super,
  607. .show_options = generic_show_options,
  608. };
  609. static int
  610. hugetlbfs_parse_options(char *options, struct hugetlbfs_config *pconfig)
  611. {
  612. char *p, *rest;
  613. substring_t args[MAX_OPT_ARGS];
  614. int option;
  615. unsigned long long size = 0;
  616. enum { NO_SIZE, SIZE_STD, SIZE_PERCENT } setsize = NO_SIZE;
  617. if (!options)
  618. return 0;
  619. while ((p = strsep(&options, ",")) != NULL) {
  620. int token;
  621. if (!*p)
  622. continue;
  623. token = match_token(p, tokens, args);
  624. switch (token) {
  625. case Opt_uid:
  626. if (match_int(&args[0], &option))
  627. goto bad_val;
  628. pconfig->uid = option;
  629. break;
  630. case Opt_gid:
  631. if (match_int(&args[0], &option))
  632. goto bad_val;
  633. pconfig->gid = option;
  634. break;
  635. case Opt_mode:
  636. if (match_octal(&args[0], &option))
  637. goto bad_val;
  638. pconfig->mode = option & 01777U;
  639. break;
  640. case Opt_size: {
  641. /* memparse() will accept a K/M/G without a digit */
  642. if (!isdigit(*args[0].from))
  643. goto bad_val;
  644. size = memparse(args[0].from, &rest);
  645. setsize = SIZE_STD;
  646. if (*rest == '%')
  647. setsize = SIZE_PERCENT;
  648. break;
  649. }
  650. case Opt_nr_inodes:
  651. /* memparse() will accept a K/M/G without a digit */
  652. if (!isdigit(*args[0].from))
  653. goto bad_val;
  654. pconfig->nr_inodes = memparse(args[0].from, &rest);
  655. break;
  656. case Opt_pagesize: {
  657. unsigned long ps;
  658. ps = memparse(args[0].from, &rest);
  659. pconfig->hstate = size_to_hstate(ps);
  660. if (!pconfig->hstate) {
  661. printk(KERN_ERR
  662. "hugetlbfs: Unsupported page size %lu MB\n",
  663. ps >> 20);
  664. return -EINVAL;
  665. }
  666. break;
  667. }
  668. default:
  669. printk(KERN_ERR "hugetlbfs: Bad mount option: \"%s\"\n",
  670. p);
  671. return -EINVAL;
  672. break;
  673. }
  674. }
  675. /* Do size after hstate is set up */
  676. if (setsize > NO_SIZE) {
  677. struct hstate *h = pconfig->hstate;
  678. if (setsize == SIZE_PERCENT) {
  679. size <<= huge_page_shift(h);
  680. size *= h->max_huge_pages;
  681. do_div(size, 100);
  682. }
  683. pconfig->nr_blocks = (size >> huge_page_shift(h));
  684. }
  685. return 0;
  686. bad_val:
  687. printk(KERN_ERR "hugetlbfs: Bad value '%s' for mount option '%s'\n",
  688. args[0].from, p);
  689. return -EINVAL;
  690. }
  691. static int
  692. hugetlbfs_fill_super(struct super_block *sb, void *data, int silent)
  693. {
  694. struct inode * inode;
  695. struct dentry * root;
  696. int ret;
  697. struct hugetlbfs_config config;
  698. struct hugetlbfs_sb_info *sbinfo;
  699. save_mount_options(sb, data);
  700. config.nr_blocks = -1; /* No limit on size by default */
  701. config.nr_inodes = -1; /* No limit on number of inodes by default */
  702. config.uid = current_fsuid();
  703. config.gid = current_fsgid();
  704. config.mode = 0755;
  705. config.hstate = &default_hstate;
  706. ret = hugetlbfs_parse_options(data, &config);
  707. if (ret)
  708. return ret;
  709. sbinfo = kmalloc(sizeof(struct hugetlbfs_sb_info), GFP_KERNEL);
  710. if (!sbinfo)
  711. return -ENOMEM;
  712. sb->s_fs_info = sbinfo;
  713. sbinfo->hstate = config.hstate;
  714. spin_lock_init(&sbinfo->stat_lock);
  715. sbinfo->max_blocks = config.nr_blocks;
  716. sbinfo->free_blocks = config.nr_blocks;
  717. sbinfo->max_inodes = config.nr_inodes;
  718. sbinfo->free_inodes = config.nr_inodes;
  719. sb->s_maxbytes = MAX_LFS_FILESIZE;
  720. sb->s_blocksize = huge_page_size(config.hstate);
  721. sb->s_blocksize_bits = huge_page_shift(config.hstate);
  722. sb->s_magic = HUGETLBFS_MAGIC;
  723. sb->s_op = &hugetlbfs_ops;
  724. sb->s_time_gran = 1;
  725. inode = hugetlbfs_get_inode(sb, config.uid, config.gid,
  726. S_IFDIR | config.mode, 0);
  727. if (!inode)
  728. goto out_free;
  729. root = d_alloc_root(inode);
  730. if (!root) {
  731. iput(inode);
  732. goto out_free;
  733. }
  734. sb->s_root = root;
  735. return 0;
  736. out_free:
  737. kfree(sbinfo);
  738. return -ENOMEM;
  739. }
  740. int hugetlb_get_quota(struct address_space *mapping, long delta)
  741. {
  742. int ret = 0;
  743. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
  744. if (sbinfo->free_blocks > -1) {
  745. spin_lock(&sbinfo->stat_lock);
  746. if (sbinfo->free_blocks - delta >= 0)
  747. sbinfo->free_blocks -= delta;
  748. else
  749. ret = -ENOMEM;
  750. spin_unlock(&sbinfo->stat_lock);
  751. }
  752. return ret;
  753. }
  754. void hugetlb_put_quota(struct address_space *mapping, long delta)
  755. {
  756. struct hugetlbfs_sb_info *sbinfo = HUGETLBFS_SB(mapping->host->i_sb);
  757. if (sbinfo->free_blocks > -1) {
  758. spin_lock(&sbinfo->stat_lock);
  759. sbinfo->free_blocks += delta;
  760. spin_unlock(&sbinfo->stat_lock);
  761. }
  762. }
  763. static int hugetlbfs_get_sb(struct file_system_type *fs_type,
  764. int flags, const char *dev_name, void *data, struct vfsmount *mnt)
  765. {
  766. return get_sb_nodev(fs_type, flags, data, hugetlbfs_fill_super, mnt);
  767. }
  768. static struct file_system_type hugetlbfs_fs_type = {
  769. .name = "hugetlbfs",
  770. .get_sb = hugetlbfs_get_sb,
  771. .kill_sb = kill_litter_super,
  772. };
  773. static struct vfsmount *hugetlbfs_vfsmount;
  774. static int can_do_hugetlb_shm(void)
  775. {
  776. return capable(CAP_IPC_LOCK) || in_group_p(sysctl_hugetlb_shm_group);
  777. }
  778. struct file *hugetlb_file_setup(const char *name, size_t size, int acctflag,
  779. struct user_struct **user, int creat_flags)
  780. {
  781. int error = -ENOMEM;
  782. struct file *file;
  783. struct inode *inode;
  784. struct path path;
  785. struct dentry *root;
  786. struct qstr quick_string;
  787. *user = NULL;
  788. if (!hugetlbfs_vfsmount)
  789. return ERR_PTR(-ENOENT);
  790. if (creat_flags == HUGETLB_SHMFS_INODE && !can_do_hugetlb_shm()) {
  791. *user = current_user();
  792. if (user_shm_lock(size, *user)) {
  793. WARN_ONCE(1,
  794. "Using mlock ulimits for SHM_HUGETLB deprecated\n");
  795. } else {
  796. *user = NULL;
  797. return ERR_PTR(-EPERM);
  798. }
  799. }
  800. root = hugetlbfs_vfsmount->mnt_root;
  801. quick_string.name = name;
  802. quick_string.len = strlen(quick_string.name);
  803. quick_string.hash = 0;
  804. path.dentry = d_alloc(root, &quick_string);
  805. if (!path.dentry)
  806. goto out_shm_unlock;
  807. path.mnt = mntget(hugetlbfs_vfsmount);
  808. error = -ENOSPC;
  809. inode = hugetlbfs_get_inode(root->d_sb, current_fsuid(),
  810. current_fsgid(), S_IFREG | S_IRWXUGO, 0);
  811. if (!inode)
  812. goto out_dentry;
  813. error = -ENOMEM;
  814. if (hugetlb_reserve_pages(inode, 0,
  815. size >> huge_page_shift(hstate_inode(inode)), NULL,
  816. acctflag))
  817. goto out_inode;
  818. d_instantiate(path.dentry, inode);
  819. inode->i_size = size;
  820. inode->i_nlink = 0;
  821. error = -ENFILE;
  822. file = alloc_file(&path, FMODE_WRITE | FMODE_READ,
  823. &hugetlbfs_file_operations);
  824. if (!file)
  825. goto out_dentry; /* inode is already attached */
  826. return file;
  827. out_inode:
  828. iput(inode);
  829. out_dentry:
  830. path_put(&path);
  831. out_shm_unlock:
  832. if (*user) {
  833. user_shm_unlock(size, *user);
  834. *user = NULL;
  835. }
  836. return ERR_PTR(error);
  837. }
  838. static int __init init_hugetlbfs_fs(void)
  839. {
  840. int error;
  841. struct vfsmount *vfsmount;
  842. error = bdi_init(&hugetlbfs_backing_dev_info);
  843. if (error)
  844. return error;
  845. hugetlbfs_inode_cachep = kmem_cache_create("hugetlbfs_inode_cache",
  846. sizeof(struct hugetlbfs_inode_info),
  847. 0, 0, init_once);
  848. if (hugetlbfs_inode_cachep == NULL)
  849. goto out2;
  850. error = register_filesystem(&hugetlbfs_fs_type);
  851. if (error)
  852. goto out;
  853. vfsmount = kern_mount(&hugetlbfs_fs_type);
  854. if (!IS_ERR(vfsmount)) {
  855. hugetlbfs_vfsmount = vfsmount;
  856. return 0;
  857. }
  858. error = PTR_ERR(vfsmount);
  859. out:
  860. if (error)
  861. kmem_cache_destroy(hugetlbfs_inode_cachep);
  862. out2:
  863. bdi_destroy(&hugetlbfs_backing_dev_info);
  864. return error;
  865. }
  866. static void __exit exit_hugetlbfs_fs(void)
  867. {
  868. kmem_cache_destroy(hugetlbfs_inode_cachep);
  869. unregister_filesystem(&hugetlbfs_fs_type);
  870. bdi_destroy(&hugetlbfs_backing_dev_info);
  871. }
  872. module_init(init_hugetlbfs_fs)
  873. module_exit(exit_hugetlbfs_fs)
  874. MODULE_LICENSE("GPL");