dax.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. /*
  2. * fs/dax.c - Direct Access filesystem code
  3. * Copyright (c) 2013-2014 Intel Corporation
  4. * Author: Matthew Wilcox <matthew.r.wilcox@intel.com>
  5. * Author: Ross Zwisler <ross.zwisler@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms and conditions of the GNU General Public License,
  9. * version 2, as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  14. * more details.
  15. */
  16. #include <linux/atomic.h>
  17. #include <linux/blkdev.h>
  18. #include <linux/buffer_head.h>
  19. #include <linux/dax.h>
  20. #include <linux/fs.h>
  21. #include <linux/genhd.h>
  22. #include <linux/highmem.h>
  23. #include <linux/memcontrol.h>
  24. #include <linux/mm.h>
  25. #include <linux/mutex.h>
  26. #include <linux/pmem.h>
  27. #include <linux/sched.h>
  28. #include <linux/uio.h>
  29. #include <linux/vmstat.h>
  30. #include <linux/sizes.h>
  31. static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
  32. {
  33. struct request_queue *q = bdev->bd_queue;
  34. long rc = -EIO;
  35. dax->addr = (void __pmem *) ERR_PTR(-EIO);
  36. if (blk_queue_enter(q, true) != 0)
  37. return rc;
  38. rc = bdev_direct_access(bdev, dax);
  39. if (rc < 0) {
  40. dax->addr = (void __pmem *) ERR_PTR(rc);
  41. blk_queue_exit(q);
  42. return rc;
  43. }
  44. return rc;
  45. }
  46. static void dax_unmap_atomic(struct block_device *bdev,
  47. const struct blk_dax_ctl *dax)
  48. {
  49. if (IS_ERR(dax->addr))
  50. return;
  51. blk_queue_exit(bdev->bd_queue);
  52. }
  53. /*
  54. * dax_clear_blocks() is called from within transaction context from XFS,
  55. * and hence this means the stack from this point must follow GFP_NOFS
  56. * semantics for all operations.
  57. */
  58. int dax_clear_blocks(struct inode *inode, sector_t block, long _size)
  59. {
  60. struct block_device *bdev = inode->i_sb->s_bdev;
  61. struct blk_dax_ctl dax = {
  62. .sector = block << (inode->i_blkbits - 9),
  63. .size = _size,
  64. };
  65. might_sleep();
  66. do {
  67. long count, sz;
  68. count = dax_map_atomic(bdev, &dax);
  69. if (count < 0)
  70. return count;
  71. sz = min_t(long, count, SZ_128K);
  72. clear_pmem(dax.addr, sz);
  73. dax.size -= sz;
  74. dax.sector += sz / 512;
  75. dax_unmap_atomic(bdev, &dax);
  76. cond_resched();
  77. } while (dax.size);
  78. wmb_pmem();
  79. return 0;
  80. }
  81. EXPORT_SYMBOL_GPL(dax_clear_blocks);
  82. /* the clear_pmem() calls are ordered by a wmb_pmem() in the caller */
  83. static void dax_new_buf(void __pmem *addr, unsigned size, unsigned first,
  84. loff_t pos, loff_t end)
  85. {
  86. loff_t final = end - pos + first; /* The final byte of the buffer */
  87. if (first > 0)
  88. clear_pmem(addr, first);
  89. if (final < size)
  90. clear_pmem(addr + final, size - final);
  91. }
  92. static bool buffer_written(struct buffer_head *bh)
  93. {
  94. return buffer_mapped(bh) && !buffer_unwritten(bh);
  95. }
  96. /*
  97. * When ext4 encounters a hole, it returns without modifying the buffer_head
  98. * which means that we can't trust b_size. To cope with this, we set b_state
  99. * to 0 before calling get_block and, if any bit is set, we know we can trust
  100. * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is
  101. * and would save us time calling get_block repeatedly.
  102. */
  103. static bool buffer_size_valid(struct buffer_head *bh)
  104. {
  105. return bh->b_state != 0;
  106. }
  107. static sector_t to_sector(const struct buffer_head *bh,
  108. const struct inode *inode)
  109. {
  110. sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
  111. return sector;
  112. }
  113. static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
  114. loff_t start, loff_t end, get_block_t get_block,
  115. struct buffer_head *bh)
  116. {
  117. loff_t pos = start, max = start, bh_max = start;
  118. bool hole = false, need_wmb = false;
  119. struct block_device *bdev = NULL;
  120. int rw = iov_iter_rw(iter), rc;
  121. long map_len = 0;
  122. struct blk_dax_ctl dax = {
  123. .addr = (void __pmem *) ERR_PTR(-EIO),
  124. };
  125. if (rw == READ)
  126. end = min(end, i_size_read(inode));
  127. while (pos < end) {
  128. size_t len;
  129. if (pos == max) {
  130. unsigned blkbits = inode->i_blkbits;
  131. long page = pos >> PAGE_SHIFT;
  132. sector_t block = page << (PAGE_SHIFT - blkbits);
  133. unsigned first = pos - (block << blkbits);
  134. long size;
  135. if (pos == bh_max) {
  136. bh->b_size = PAGE_ALIGN(end - pos);
  137. bh->b_state = 0;
  138. rc = get_block(inode, block, bh, rw == WRITE);
  139. if (rc)
  140. break;
  141. if (!buffer_size_valid(bh))
  142. bh->b_size = 1 << blkbits;
  143. bh_max = pos - first + bh->b_size;
  144. bdev = bh->b_bdev;
  145. } else {
  146. unsigned done = bh->b_size -
  147. (bh_max - (pos - first));
  148. bh->b_blocknr += done >> blkbits;
  149. bh->b_size -= done;
  150. }
  151. hole = rw == READ && !buffer_written(bh);
  152. if (hole) {
  153. size = bh->b_size - first;
  154. } else {
  155. dax_unmap_atomic(bdev, &dax);
  156. dax.sector = to_sector(bh, inode);
  157. dax.size = bh->b_size;
  158. map_len = dax_map_atomic(bdev, &dax);
  159. if (map_len < 0) {
  160. rc = map_len;
  161. break;
  162. }
  163. if (buffer_unwritten(bh) || buffer_new(bh)) {
  164. dax_new_buf(dax.addr, map_len, first,
  165. pos, end);
  166. need_wmb = true;
  167. }
  168. dax.addr += first;
  169. size = map_len - first;
  170. }
  171. max = min(pos + size, end);
  172. }
  173. if (iov_iter_rw(iter) == WRITE) {
  174. len = copy_from_iter_pmem(dax.addr, max - pos, iter);
  175. need_wmb = true;
  176. } else if (!hole)
  177. len = copy_to_iter((void __force *) dax.addr, max - pos,
  178. iter);
  179. else
  180. len = iov_iter_zero(max - pos, iter);
  181. if (!len) {
  182. rc = -EFAULT;
  183. break;
  184. }
  185. pos += len;
  186. if (!IS_ERR(dax.addr))
  187. dax.addr += len;
  188. }
  189. if (need_wmb)
  190. wmb_pmem();
  191. dax_unmap_atomic(bdev, &dax);
  192. return (pos == start) ? rc : pos - start;
  193. }
  194. /**
  195. * dax_do_io - Perform I/O to a DAX file
  196. * @iocb: The control block for this I/O
  197. * @inode: The file which the I/O is directed at
  198. * @iter: The addresses to do I/O from or to
  199. * @pos: The file offset where the I/O starts
  200. * @get_block: The filesystem method used to translate file offsets to blocks
  201. * @end_io: A filesystem callback for I/O completion
  202. * @flags: See below
  203. *
  204. * This function uses the same locking scheme as do_blockdev_direct_IO:
  205. * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
  206. * caller for writes. For reads, we take and release the i_mutex ourselves.
  207. * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
  208. * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
  209. * is in progress.
  210. */
  211. ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
  212. struct iov_iter *iter, loff_t pos, get_block_t get_block,
  213. dio_iodone_t end_io, int flags)
  214. {
  215. struct buffer_head bh;
  216. ssize_t retval = -EINVAL;
  217. loff_t end = pos + iov_iter_count(iter);
  218. memset(&bh, 0, sizeof(bh));
  219. if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) {
  220. struct address_space *mapping = inode->i_mapping;
  221. mutex_lock(&inode->i_mutex);
  222. retval = filemap_write_and_wait_range(mapping, pos, end - 1);
  223. if (retval) {
  224. mutex_unlock(&inode->i_mutex);
  225. goto out;
  226. }
  227. }
  228. /* Protects against truncate */
  229. if (!(flags & DIO_SKIP_DIO_COUNT))
  230. inode_dio_begin(inode);
  231. retval = dax_io(inode, iter, pos, end, get_block, &bh);
  232. if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
  233. mutex_unlock(&inode->i_mutex);
  234. if ((retval > 0) && end_io)
  235. end_io(iocb, pos, retval, bh.b_private);
  236. if (!(flags & DIO_SKIP_DIO_COUNT))
  237. inode_dio_end(inode);
  238. out:
  239. return retval;
  240. }
  241. EXPORT_SYMBOL_GPL(dax_do_io);
  242. /*
  243. * The user has performed a load from a hole in the file. Allocating
  244. * a new page in the file would cause excessive storage usage for
  245. * workloads with sparse files. We allocate a page cache page instead.
  246. * We'll kick it out of the page cache if it's ever written to,
  247. * otherwise it will simply fall out of the page cache under memory
  248. * pressure without ever having been dirtied.
  249. */
  250. static int dax_load_hole(struct address_space *mapping, struct page *page,
  251. struct vm_fault *vmf)
  252. {
  253. unsigned long size;
  254. struct inode *inode = mapping->host;
  255. if (!page)
  256. page = find_or_create_page(mapping, vmf->pgoff,
  257. GFP_KERNEL | __GFP_ZERO);
  258. if (!page)
  259. return VM_FAULT_OOM;
  260. /* Recheck i_size under page lock to avoid truncate race */
  261. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  262. if (vmf->pgoff >= size) {
  263. unlock_page(page);
  264. page_cache_release(page);
  265. return VM_FAULT_SIGBUS;
  266. }
  267. vmf->page = page;
  268. return VM_FAULT_LOCKED;
  269. }
  270. static int copy_user_bh(struct page *to, struct inode *inode,
  271. struct buffer_head *bh, unsigned long vaddr)
  272. {
  273. struct blk_dax_ctl dax = {
  274. .sector = to_sector(bh, inode),
  275. .size = bh->b_size,
  276. };
  277. struct block_device *bdev = bh->b_bdev;
  278. void *vto;
  279. if (dax_map_atomic(bdev, &dax) < 0)
  280. return PTR_ERR(dax.addr);
  281. vto = kmap_atomic(to);
  282. copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
  283. kunmap_atomic(vto);
  284. dax_unmap_atomic(bdev, &dax);
  285. return 0;
  286. }
  287. static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
  288. struct vm_area_struct *vma, struct vm_fault *vmf)
  289. {
  290. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  291. struct address_space *mapping = inode->i_mapping;
  292. struct block_device *bdev = bh->b_bdev;
  293. struct blk_dax_ctl dax = {
  294. .sector = to_sector(bh, inode),
  295. .size = bh->b_size,
  296. };
  297. pgoff_t size;
  298. int error;
  299. i_mmap_lock_read(mapping);
  300. /*
  301. * Check truncate didn't happen while we were allocating a block.
  302. * If it did, this block may or may not be still allocated to the
  303. * file. We can't tell the filesystem to free it because we can't
  304. * take i_mutex here. In the worst case, the file still has blocks
  305. * allocated past the end of the file.
  306. */
  307. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  308. if (unlikely(vmf->pgoff >= size)) {
  309. error = -EIO;
  310. goto out;
  311. }
  312. if (dax_map_atomic(bdev, &dax) < 0) {
  313. error = PTR_ERR(dax.addr);
  314. goto out;
  315. }
  316. if (buffer_unwritten(bh) || buffer_new(bh)) {
  317. clear_pmem(dax.addr, PAGE_SIZE);
  318. wmb_pmem();
  319. }
  320. dax_unmap_atomic(bdev, &dax);
  321. error = vm_insert_mixed(vma, vaddr, dax.pfn);
  322. out:
  323. i_mmap_unlock_read(mapping);
  324. return error;
  325. }
  326. /**
  327. * __dax_fault - handle a page fault on a DAX file
  328. * @vma: The virtual memory area where the fault occurred
  329. * @vmf: The description of the fault
  330. * @get_block: The filesystem method used to translate file offsets to blocks
  331. * @complete_unwritten: The filesystem method used to convert unwritten blocks
  332. * to written so the data written to them is exposed. This is required for
  333. * required by write faults for filesystems that will return unwritten
  334. * extent mappings from @get_block, but it is optional for reads as
  335. * dax_insert_mapping() will always zero unwritten blocks. If the fs does
  336. * not support unwritten extents, the it should pass NULL.
  337. *
  338. * When a page fault occurs, filesystems may call this helper in their
  339. * fault handler for DAX files. __dax_fault() assumes the caller has done all
  340. * the necessary locking for the page fault to proceed successfully.
  341. */
  342. int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  343. get_block_t get_block, dax_iodone_t complete_unwritten)
  344. {
  345. struct file *file = vma->vm_file;
  346. struct address_space *mapping = file->f_mapping;
  347. struct inode *inode = mapping->host;
  348. struct page *page;
  349. struct buffer_head bh;
  350. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  351. unsigned blkbits = inode->i_blkbits;
  352. sector_t block;
  353. pgoff_t size;
  354. int error;
  355. int major = 0;
  356. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  357. if (vmf->pgoff >= size)
  358. return VM_FAULT_SIGBUS;
  359. memset(&bh, 0, sizeof(bh));
  360. block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
  361. bh.b_size = PAGE_SIZE;
  362. repeat:
  363. page = find_get_page(mapping, vmf->pgoff);
  364. if (page) {
  365. if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
  366. page_cache_release(page);
  367. return VM_FAULT_RETRY;
  368. }
  369. if (unlikely(page->mapping != mapping)) {
  370. unlock_page(page);
  371. page_cache_release(page);
  372. goto repeat;
  373. }
  374. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  375. if (unlikely(vmf->pgoff >= size)) {
  376. /*
  377. * We have a struct page covering a hole in the file
  378. * from a read fault and we've raced with a truncate
  379. */
  380. error = -EIO;
  381. goto unlock_page;
  382. }
  383. }
  384. error = get_block(inode, block, &bh, 0);
  385. if (!error && (bh.b_size < PAGE_SIZE))
  386. error = -EIO; /* fs corruption? */
  387. if (error)
  388. goto unlock_page;
  389. if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) {
  390. if (vmf->flags & FAULT_FLAG_WRITE) {
  391. error = get_block(inode, block, &bh, 1);
  392. count_vm_event(PGMAJFAULT);
  393. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  394. major = VM_FAULT_MAJOR;
  395. if (!error && (bh.b_size < PAGE_SIZE))
  396. error = -EIO;
  397. if (error)
  398. goto unlock_page;
  399. } else {
  400. return dax_load_hole(mapping, page, vmf);
  401. }
  402. }
  403. if (vmf->cow_page) {
  404. struct page *new_page = vmf->cow_page;
  405. if (buffer_written(&bh))
  406. error = copy_user_bh(new_page, inode, &bh, vaddr);
  407. else
  408. clear_user_highpage(new_page, vaddr);
  409. if (error)
  410. goto unlock_page;
  411. vmf->page = page;
  412. if (!page) {
  413. i_mmap_lock_read(mapping);
  414. /* Check we didn't race with truncate */
  415. size = (i_size_read(inode) + PAGE_SIZE - 1) >>
  416. PAGE_SHIFT;
  417. if (vmf->pgoff >= size) {
  418. i_mmap_unlock_read(mapping);
  419. error = -EIO;
  420. goto out;
  421. }
  422. }
  423. return VM_FAULT_LOCKED;
  424. }
  425. /* Check we didn't race with a read fault installing a new page */
  426. if (!page && major)
  427. page = find_lock_page(mapping, vmf->pgoff);
  428. if (page) {
  429. unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
  430. PAGE_CACHE_SIZE, 0);
  431. delete_from_page_cache(page);
  432. unlock_page(page);
  433. page_cache_release(page);
  434. }
  435. /*
  436. * If we successfully insert the new mapping over an unwritten extent,
  437. * we need to ensure we convert the unwritten extent. If there is an
  438. * error inserting the mapping, the filesystem needs to leave it as
  439. * unwritten to prevent exposure of the stale underlying data to
  440. * userspace, but we still need to call the completion function so
  441. * the private resources on the mapping buffer can be released. We
  442. * indicate what the callback should do via the uptodate variable, same
  443. * as for normal BH based IO completions.
  444. */
  445. error = dax_insert_mapping(inode, &bh, vma, vmf);
  446. if (buffer_unwritten(&bh)) {
  447. if (complete_unwritten)
  448. complete_unwritten(&bh, !error);
  449. else
  450. WARN_ON_ONCE(!(vmf->flags & FAULT_FLAG_WRITE));
  451. }
  452. out:
  453. if (error == -ENOMEM)
  454. return VM_FAULT_OOM | major;
  455. /* -EBUSY is fine, somebody else faulted on the same PTE */
  456. if ((error < 0) && (error != -EBUSY))
  457. return VM_FAULT_SIGBUS | major;
  458. return VM_FAULT_NOPAGE | major;
  459. unlock_page:
  460. if (page) {
  461. unlock_page(page);
  462. page_cache_release(page);
  463. }
  464. goto out;
  465. }
  466. EXPORT_SYMBOL(__dax_fault);
  467. /**
  468. * dax_fault - handle a page fault on a DAX file
  469. * @vma: The virtual memory area where the fault occurred
  470. * @vmf: The description of the fault
  471. * @get_block: The filesystem method used to translate file offsets to blocks
  472. *
  473. * When a page fault occurs, filesystems may call this helper in their
  474. * fault handler for DAX files.
  475. */
  476. int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  477. get_block_t get_block, dax_iodone_t complete_unwritten)
  478. {
  479. int result;
  480. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  481. if (vmf->flags & FAULT_FLAG_WRITE) {
  482. sb_start_pagefault(sb);
  483. file_update_time(vma->vm_file);
  484. }
  485. result = __dax_fault(vma, vmf, get_block, complete_unwritten);
  486. if (vmf->flags & FAULT_FLAG_WRITE)
  487. sb_end_pagefault(sb);
  488. return result;
  489. }
  490. EXPORT_SYMBOL_GPL(dax_fault);
  491. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  492. /*
  493. * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
  494. * more often than one might expect in the below function.
  495. */
  496. #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
  497. int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
  498. pmd_t *pmd, unsigned int flags, get_block_t get_block,
  499. dax_iodone_t complete_unwritten)
  500. {
  501. struct file *file = vma->vm_file;
  502. struct address_space *mapping = file->f_mapping;
  503. struct inode *inode = mapping->host;
  504. struct buffer_head bh;
  505. unsigned blkbits = inode->i_blkbits;
  506. unsigned long pmd_addr = address & PMD_MASK;
  507. bool write = flags & FAULT_FLAG_WRITE;
  508. struct block_device *bdev;
  509. pgoff_t size, pgoff;
  510. sector_t block;
  511. int result = 0;
  512. /* dax pmd mappings are broken wrt gup and fork */
  513. if (!IS_ENABLED(CONFIG_FS_DAX_PMD))
  514. return VM_FAULT_FALLBACK;
  515. /* Fall back to PTEs if we're going to COW */
  516. if (write && !(vma->vm_flags & VM_SHARED))
  517. return VM_FAULT_FALLBACK;
  518. /* If the PMD would extend outside the VMA */
  519. if (pmd_addr < vma->vm_start)
  520. return VM_FAULT_FALLBACK;
  521. if ((pmd_addr + PMD_SIZE) > vma->vm_end)
  522. return VM_FAULT_FALLBACK;
  523. pgoff = linear_page_index(vma, pmd_addr);
  524. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  525. if (pgoff >= size)
  526. return VM_FAULT_SIGBUS;
  527. /* If the PMD would cover blocks out of the file */
  528. if ((pgoff | PG_PMD_COLOUR) >= size)
  529. return VM_FAULT_FALLBACK;
  530. memset(&bh, 0, sizeof(bh));
  531. block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
  532. bh.b_size = PMD_SIZE;
  533. if (get_block(inode, block, &bh, write) != 0)
  534. return VM_FAULT_SIGBUS;
  535. bdev = bh.b_bdev;
  536. i_mmap_lock_read(mapping);
  537. /*
  538. * If the filesystem isn't willing to tell us the length of a hole,
  539. * just fall back to PTEs. Calling get_block 512 times in a loop
  540. * would be silly.
  541. */
  542. if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE)
  543. goto fallback;
  544. /*
  545. * If we allocated new storage, make sure no process has any
  546. * zero pages covering this hole
  547. */
  548. if (buffer_new(&bh)) {
  549. i_mmap_unlock_read(mapping);
  550. unmap_mapping_range(mapping, pgoff << PAGE_SHIFT, PMD_SIZE, 0);
  551. i_mmap_lock_read(mapping);
  552. }
  553. /*
  554. * If a truncate happened while we were allocating blocks, we may
  555. * leave blocks allocated to the file that are beyond EOF. We can't
  556. * take i_mutex here, so just leave them hanging; they'll be freed
  557. * when the file is deleted.
  558. */
  559. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  560. if (pgoff >= size) {
  561. result = VM_FAULT_SIGBUS;
  562. goto out;
  563. }
  564. if ((pgoff | PG_PMD_COLOUR) >= size)
  565. goto fallback;
  566. if (!write && !buffer_mapped(&bh) && buffer_uptodate(&bh)) {
  567. spinlock_t *ptl;
  568. pmd_t entry;
  569. struct page *zero_page = get_huge_zero_page();
  570. if (unlikely(!zero_page))
  571. goto fallback;
  572. ptl = pmd_lock(vma->vm_mm, pmd);
  573. if (!pmd_none(*pmd)) {
  574. spin_unlock(ptl);
  575. goto fallback;
  576. }
  577. entry = mk_pmd(zero_page, vma->vm_page_prot);
  578. entry = pmd_mkhuge(entry);
  579. set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
  580. result = VM_FAULT_NOPAGE;
  581. spin_unlock(ptl);
  582. } else {
  583. struct blk_dax_ctl dax = {
  584. .sector = to_sector(&bh, inode),
  585. .size = PMD_SIZE,
  586. };
  587. long length = dax_map_atomic(bdev, &dax);
  588. if (length < 0) {
  589. result = VM_FAULT_SIGBUS;
  590. goto out;
  591. }
  592. if ((length < PMD_SIZE) || (dax.pfn & PG_PMD_COLOUR)) {
  593. dax_unmap_atomic(bdev, &dax);
  594. goto fallback;
  595. }
  596. /*
  597. * TODO: teach vmf_insert_pfn_pmd() to support
  598. * 'pte_special' for pmds
  599. */
  600. if (pfn_valid(dax.pfn)) {
  601. dax_unmap_atomic(bdev, &dax);
  602. goto fallback;
  603. }
  604. if (buffer_unwritten(&bh) || buffer_new(&bh)) {
  605. clear_pmem(dax.addr, PMD_SIZE);
  606. wmb_pmem();
  607. count_vm_event(PGMAJFAULT);
  608. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  609. result |= VM_FAULT_MAJOR;
  610. }
  611. dax_unmap_atomic(bdev, &dax);
  612. result |= vmf_insert_pfn_pmd(vma, address, pmd, dax.pfn, write);
  613. }
  614. out:
  615. i_mmap_unlock_read(mapping);
  616. if (buffer_unwritten(&bh))
  617. complete_unwritten(&bh, !(result & VM_FAULT_ERROR));
  618. return result;
  619. fallback:
  620. count_vm_event(THP_FAULT_FALLBACK);
  621. result = VM_FAULT_FALLBACK;
  622. goto out;
  623. }
  624. EXPORT_SYMBOL_GPL(__dax_pmd_fault);
  625. /**
  626. * dax_pmd_fault - handle a PMD fault on a DAX file
  627. * @vma: The virtual memory area where the fault occurred
  628. * @vmf: The description of the fault
  629. * @get_block: The filesystem method used to translate file offsets to blocks
  630. *
  631. * When a page fault occurs, filesystems may call this helper in their
  632. * pmd_fault handler for DAX files.
  633. */
  634. int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
  635. pmd_t *pmd, unsigned int flags, get_block_t get_block,
  636. dax_iodone_t complete_unwritten)
  637. {
  638. int result;
  639. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  640. if (flags & FAULT_FLAG_WRITE) {
  641. sb_start_pagefault(sb);
  642. file_update_time(vma->vm_file);
  643. }
  644. result = __dax_pmd_fault(vma, address, pmd, flags, get_block,
  645. complete_unwritten);
  646. if (flags & FAULT_FLAG_WRITE)
  647. sb_end_pagefault(sb);
  648. return result;
  649. }
  650. EXPORT_SYMBOL_GPL(dax_pmd_fault);
  651. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  652. /**
  653. * dax_pfn_mkwrite - handle first write to DAX page
  654. * @vma: The virtual memory area where the fault occurred
  655. * @vmf: The description of the fault
  656. *
  657. */
  658. int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  659. {
  660. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  661. sb_start_pagefault(sb);
  662. file_update_time(vma->vm_file);
  663. sb_end_pagefault(sb);
  664. return VM_FAULT_NOPAGE;
  665. }
  666. EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
  667. /**
  668. * dax_zero_page_range - zero a range within a page of a DAX file
  669. * @inode: The file being truncated
  670. * @from: The file offset that is being truncated to
  671. * @length: The number of bytes to zero
  672. * @get_block: The filesystem method used to translate file offsets to blocks
  673. *
  674. * This function can be called by a filesystem when it is zeroing part of a
  675. * page in a DAX file. This is intended for hole-punch operations. If
  676. * you are truncating a file, the helper function dax_truncate_page() may be
  677. * more convenient.
  678. *
  679. * We work in terms of PAGE_CACHE_SIZE here for commonality with
  680. * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
  681. * took care of disposing of the unnecessary blocks. Even if the filesystem
  682. * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
  683. * since the file might be mmapped.
  684. */
  685. int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
  686. get_block_t get_block)
  687. {
  688. struct buffer_head bh;
  689. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  690. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  691. int err;
  692. /* Block boundary? Nothing to do */
  693. if (!length)
  694. return 0;
  695. BUG_ON((offset + length) > PAGE_CACHE_SIZE);
  696. memset(&bh, 0, sizeof(bh));
  697. bh.b_size = PAGE_CACHE_SIZE;
  698. err = get_block(inode, index, &bh, 0);
  699. if (err < 0)
  700. return err;
  701. if (buffer_written(&bh)) {
  702. struct block_device *bdev = bh.b_bdev;
  703. struct blk_dax_ctl dax = {
  704. .sector = to_sector(&bh, inode),
  705. .size = PAGE_CACHE_SIZE,
  706. };
  707. if (dax_map_atomic(bdev, &dax) < 0)
  708. return PTR_ERR(dax.addr);
  709. clear_pmem(dax.addr + offset, length);
  710. wmb_pmem();
  711. dax_unmap_atomic(bdev, &dax);
  712. }
  713. return 0;
  714. }
  715. EXPORT_SYMBOL_GPL(dax_zero_page_range);
  716. /**
  717. * dax_truncate_page - handle a partial page being truncated in a DAX file
  718. * @inode: The file being truncated
  719. * @from: The file offset that is being truncated to
  720. * @get_block: The filesystem method used to translate file offsets to blocks
  721. *
  722. * Similar to block_truncate_page(), this function can be called by a
  723. * filesystem when it is truncating a DAX file to handle the partial page.
  724. *
  725. * We work in terms of PAGE_CACHE_SIZE here for commonality with
  726. * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
  727. * took care of disposing of the unnecessary blocks. Even if the filesystem
  728. * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
  729. * since the file might be mmapped.
  730. */
  731. int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
  732. {
  733. unsigned length = PAGE_CACHE_ALIGN(from) - from;
  734. return dax_zero_page_range(inode, from, length, get_block);
  735. }
  736. EXPORT_SYMBOL_GPL(dax_truncate_page);