dax.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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/pagevec.h>
  27. #include <linux/pmem.h>
  28. #include <linux/sched.h>
  29. #include <linux/uio.h>
  30. #include <linux/vmstat.h>
  31. #include <linux/pfn_t.h>
  32. #include <linux/sizes.h>
  33. static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
  34. {
  35. struct request_queue *q = bdev->bd_queue;
  36. long rc = -EIO;
  37. dax->addr = (void __pmem *) ERR_PTR(-EIO);
  38. if (blk_queue_enter(q, true) != 0)
  39. return rc;
  40. rc = bdev_direct_access(bdev, dax);
  41. if (rc < 0) {
  42. dax->addr = (void __pmem *) ERR_PTR(rc);
  43. blk_queue_exit(q);
  44. return rc;
  45. }
  46. return rc;
  47. }
  48. static void dax_unmap_atomic(struct block_device *bdev,
  49. const struct blk_dax_ctl *dax)
  50. {
  51. if (IS_ERR(dax->addr))
  52. return;
  53. blk_queue_exit(bdev->bd_queue);
  54. }
  55. /*
  56. * dax_clear_blocks() is called from within transaction context from XFS,
  57. * and hence this means the stack from this point must follow GFP_NOFS
  58. * semantics for all operations.
  59. */
  60. int dax_clear_blocks(struct inode *inode, sector_t block, long _size)
  61. {
  62. struct block_device *bdev = inode->i_sb->s_bdev;
  63. struct blk_dax_ctl dax = {
  64. .sector = block << (inode->i_blkbits - 9),
  65. .size = _size,
  66. };
  67. might_sleep();
  68. do {
  69. long count, sz;
  70. count = dax_map_atomic(bdev, &dax);
  71. if (count < 0)
  72. return count;
  73. sz = min_t(long, count, SZ_128K);
  74. clear_pmem(dax.addr, sz);
  75. dax.size -= sz;
  76. dax.sector += sz / 512;
  77. dax_unmap_atomic(bdev, &dax);
  78. cond_resched();
  79. } while (dax.size);
  80. wmb_pmem();
  81. return 0;
  82. }
  83. EXPORT_SYMBOL_GPL(dax_clear_blocks);
  84. /* the clear_pmem() calls are ordered by a wmb_pmem() in the caller */
  85. static void dax_new_buf(void __pmem *addr, unsigned size, unsigned first,
  86. loff_t pos, loff_t end)
  87. {
  88. loff_t final = end - pos + first; /* The final byte of the buffer */
  89. if (first > 0)
  90. clear_pmem(addr, first);
  91. if (final < size)
  92. clear_pmem(addr + final, size - final);
  93. }
  94. static bool buffer_written(struct buffer_head *bh)
  95. {
  96. return buffer_mapped(bh) && !buffer_unwritten(bh);
  97. }
  98. /*
  99. * When ext4 encounters a hole, it returns without modifying the buffer_head
  100. * which means that we can't trust b_size. To cope with this, we set b_state
  101. * to 0 before calling get_block and, if any bit is set, we know we can trust
  102. * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is
  103. * and would save us time calling get_block repeatedly.
  104. */
  105. static bool buffer_size_valid(struct buffer_head *bh)
  106. {
  107. return bh->b_state != 0;
  108. }
  109. static sector_t to_sector(const struct buffer_head *bh,
  110. const struct inode *inode)
  111. {
  112. sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
  113. return sector;
  114. }
  115. static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
  116. loff_t start, loff_t end, get_block_t get_block,
  117. struct buffer_head *bh)
  118. {
  119. loff_t pos = start, max = start, bh_max = start;
  120. bool hole = false, need_wmb = false;
  121. struct block_device *bdev = NULL;
  122. int rw = iov_iter_rw(iter), rc;
  123. long map_len = 0;
  124. struct blk_dax_ctl dax = {
  125. .addr = (void __pmem *) ERR_PTR(-EIO),
  126. };
  127. if (rw == READ)
  128. end = min(end, i_size_read(inode));
  129. while (pos < end) {
  130. size_t len;
  131. if (pos == max) {
  132. unsigned blkbits = inode->i_blkbits;
  133. long page = pos >> PAGE_SHIFT;
  134. sector_t block = page << (PAGE_SHIFT - blkbits);
  135. unsigned first = pos - (block << blkbits);
  136. long size;
  137. if (pos == bh_max) {
  138. bh->b_size = PAGE_ALIGN(end - pos);
  139. bh->b_state = 0;
  140. rc = get_block(inode, block, bh, rw == WRITE);
  141. if (rc)
  142. break;
  143. if (!buffer_size_valid(bh))
  144. bh->b_size = 1 << blkbits;
  145. bh_max = pos - first + bh->b_size;
  146. bdev = bh->b_bdev;
  147. } else {
  148. unsigned done = bh->b_size -
  149. (bh_max - (pos - first));
  150. bh->b_blocknr += done >> blkbits;
  151. bh->b_size -= done;
  152. }
  153. hole = rw == READ && !buffer_written(bh);
  154. if (hole) {
  155. size = bh->b_size - first;
  156. } else {
  157. dax_unmap_atomic(bdev, &dax);
  158. dax.sector = to_sector(bh, inode);
  159. dax.size = bh->b_size;
  160. map_len = dax_map_atomic(bdev, &dax);
  161. if (map_len < 0) {
  162. rc = map_len;
  163. break;
  164. }
  165. if (buffer_unwritten(bh) || buffer_new(bh)) {
  166. dax_new_buf(dax.addr, map_len, first,
  167. pos, end);
  168. need_wmb = true;
  169. }
  170. dax.addr += first;
  171. size = map_len - first;
  172. }
  173. max = min(pos + size, end);
  174. }
  175. if (iov_iter_rw(iter) == WRITE) {
  176. len = copy_from_iter_pmem(dax.addr, max - pos, iter);
  177. need_wmb = true;
  178. } else if (!hole)
  179. len = copy_to_iter((void __force *) dax.addr, max - pos,
  180. iter);
  181. else
  182. len = iov_iter_zero(max - pos, iter);
  183. if (!len) {
  184. rc = -EFAULT;
  185. break;
  186. }
  187. pos += len;
  188. if (!IS_ERR(dax.addr))
  189. dax.addr += len;
  190. }
  191. if (need_wmb)
  192. wmb_pmem();
  193. dax_unmap_atomic(bdev, &dax);
  194. return (pos == start) ? rc : pos - start;
  195. }
  196. /**
  197. * dax_do_io - Perform I/O to a DAX file
  198. * @iocb: The control block for this I/O
  199. * @inode: The file which the I/O is directed at
  200. * @iter: The addresses to do I/O from or to
  201. * @pos: The file offset where the I/O starts
  202. * @get_block: The filesystem method used to translate file offsets to blocks
  203. * @end_io: A filesystem callback for I/O completion
  204. * @flags: See below
  205. *
  206. * This function uses the same locking scheme as do_blockdev_direct_IO:
  207. * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
  208. * caller for writes. For reads, we take and release the i_mutex ourselves.
  209. * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
  210. * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
  211. * is in progress.
  212. */
  213. ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
  214. struct iov_iter *iter, loff_t pos, get_block_t get_block,
  215. dio_iodone_t end_io, int flags)
  216. {
  217. struct buffer_head bh;
  218. ssize_t retval = -EINVAL;
  219. loff_t end = pos + iov_iter_count(iter);
  220. memset(&bh, 0, sizeof(bh));
  221. bh.b_bdev = inode->i_sb->s_bdev;
  222. if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) {
  223. struct address_space *mapping = inode->i_mapping;
  224. inode_lock(inode);
  225. retval = filemap_write_and_wait_range(mapping, pos, end - 1);
  226. if (retval) {
  227. inode_unlock(inode);
  228. goto out;
  229. }
  230. }
  231. /* Protects against truncate */
  232. if (!(flags & DIO_SKIP_DIO_COUNT))
  233. inode_dio_begin(inode);
  234. retval = dax_io(inode, iter, pos, end, get_block, &bh);
  235. if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
  236. inode_unlock(inode);
  237. if ((retval > 0) && end_io)
  238. end_io(iocb, pos, retval, bh.b_private);
  239. if (!(flags & DIO_SKIP_DIO_COUNT))
  240. inode_dio_end(inode);
  241. out:
  242. return retval;
  243. }
  244. EXPORT_SYMBOL_GPL(dax_do_io);
  245. /*
  246. * The user has performed a load from a hole in the file. Allocating
  247. * a new page in the file would cause excessive storage usage for
  248. * workloads with sparse files. We allocate a page cache page instead.
  249. * We'll kick it out of the page cache if it's ever written to,
  250. * otherwise it will simply fall out of the page cache under memory
  251. * pressure without ever having been dirtied.
  252. */
  253. static int dax_load_hole(struct address_space *mapping, struct page *page,
  254. struct vm_fault *vmf)
  255. {
  256. unsigned long size;
  257. struct inode *inode = mapping->host;
  258. if (!page)
  259. page = find_or_create_page(mapping, vmf->pgoff,
  260. GFP_KERNEL | __GFP_ZERO);
  261. if (!page)
  262. return VM_FAULT_OOM;
  263. /* Recheck i_size under page lock to avoid truncate race */
  264. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  265. if (vmf->pgoff >= size) {
  266. unlock_page(page);
  267. page_cache_release(page);
  268. return VM_FAULT_SIGBUS;
  269. }
  270. vmf->page = page;
  271. return VM_FAULT_LOCKED;
  272. }
  273. static int copy_user_bh(struct page *to, struct inode *inode,
  274. struct buffer_head *bh, unsigned long vaddr)
  275. {
  276. struct blk_dax_ctl dax = {
  277. .sector = to_sector(bh, inode),
  278. .size = bh->b_size,
  279. };
  280. struct block_device *bdev = bh->b_bdev;
  281. void *vto;
  282. if (dax_map_atomic(bdev, &dax) < 0)
  283. return PTR_ERR(dax.addr);
  284. vto = kmap_atomic(to);
  285. copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
  286. kunmap_atomic(vto);
  287. dax_unmap_atomic(bdev, &dax);
  288. return 0;
  289. }
  290. #define NO_SECTOR -1
  291. #define DAX_PMD_INDEX(page_index) (page_index & (PMD_MASK >> PAGE_CACHE_SHIFT))
  292. static int dax_radix_entry(struct address_space *mapping, pgoff_t index,
  293. sector_t sector, bool pmd_entry, bool dirty)
  294. {
  295. struct radix_tree_root *page_tree = &mapping->page_tree;
  296. pgoff_t pmd_index = DAX_PMD_INDEX(index);
  297. int type, error = 0;
  298. void *entry;
  299. WARN_ON_ONCE(pmd_entry && !dirty);
  300. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  301. spin_lock_irq(&mapping->tree_lock);
  302. entry = radix_tree_lookup(page_tree, pmd_index);
  303. if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD) {
  304. index = pmd_index;
  305. goto dirty;
  306. }
  307. entry = radix_tree_lookup(page_tree, index);
  308. if (entry) {
  309. type = RADIX_DAX_TYPE(entry);
  310. if (WARN_ON_ONCE(type != RADIX_DAX_PTE &&
  311. type != RADIX_DAX_PMD)) {
  312. error = -EIO;
  313. goto unlock;
  314. }
  315. if (!pmd_entry || type == RADIX_DAX_PMD)
  316. goto dirty;
  317. /*
  318. * We only insert dirty PMD entries into the radix tree. This
  319. * means we don't need to worry about removing a dirty PTE
  320. * entry and inserting a clean PMD entry, thus reducing the
  321. * range we would flush with a follow-up fsync/msync call.
  322. */
  323. radix_tree_delete(&mapping->page_tree, index);
  324. mapping->nrexceptional--;
  325. }
  326. if (sector == NO_SECTOR) {
  327. /*
  328. * This can happen during correct operation if our pfn_mkwrite
  329. * fault raced against a hole punch operation. If this
  330. * happens the pte that was hole punched will have been
  331. * unmapped and the radix tree entry will have been removed by
  332. * the time we are called, but the call will still happen. We
  333. * will return all the way up to wp_pfn_shared(), where the
  334. * pte_same() check will fail, eventually causing page fault
  335. * to be retried by the CPU.
  336. */
  337. goto unlock;
  338. }
  339. error = radix_tree_insert(page_tree, index,
  340. RADIX_DAX_ENTRY(sector, pmd_entry));
  341. if (error)
  342. goto unlock;
  343. mapping->nrexceptional++;
  344. dirty:
  345. if (dirty)
  346. radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
  347. unlock:
  348. spin_unlock_irq(&mapping->tree_lock);
  349. return error;
  350. }
  351. static int dax_writeback_one(struct block_device *bdev,
  352. struct address_space *mapping, pgoff_t index, void *entry)
  353. {
  354. struct radix_tree_root *page_tree = &mapping->page_tree;
  355. int type = RADIX_DAX_TYPE(entry);
  356. struct radix_tree_node *node;
  357. struct blk_dax_ctl dax;
  358. void **slot;
  359. int ret = 0;
  360. spin_lock_irq(&mapping->tree_lock);
  361. /*
  362. * Regular page slots are stabilized by the page lock even
  363. * without the tree itself locked. These unlocked entries
  364. * need verification under the tree lock.
  365. */
  366. if (!__radix_tree_lookup(page_tree, index, &node, &slot))
  367. goto unlock;
  368. if (*slot != entry)
  369. goto unlock;
  370. /* another fsync thread may have already written back this entry */
  371. if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
  372. goto unlock;
  373. if (WARN_ON_ONCE(type != RADIX_DAX_PTE && type != RADIX_DAX_PMD)) {
  374. ret = -EIO;
  375. goto unlock;
  376. }
  377. dax.sector = RADIX_DAX_SECTOR(entry);
  378. dax.size = (type == RADIX_DAX_PMD ? PMD_SIZE : PAGE_SIZE);
  379. spin_unlock_irq(&mapping->tree_lock);
  380. /*
  381. * We cannot hold tree_lock while calling dax_map_atomic() because it
  382. * eventually calls cond_resched().
  383. */
  384. ret = dax_map_atomic(bdev, &dax);
  385. if (ret < 0)
  386. return ret;
  387. if (WARN_ON_ONCE(ret < dax.size)) {
  388. ret = -EIO;
  389. goto unmap;
  390. }
  391. wb_cache_pmem(dax.addr, dax.size);
  392. spin_lock_irq(&mapping->tree_lock);
  393. radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
  394. spin_unlock_irq(&mapping->tree_lock);
  395. unmap:
  396. dax_unmap_atomic(bdev, &dax);
  397. return ret;
  398. unlock:
  399. spin_unlock_irq(&mapping->tree_lock);
  400. return ret;
  401. }
  402. /*
  403. * Flush the mapping to the persistent domain within the byte range of [start,
  404. * end]. This is required by data integrity operations to ensure file data is
  405. * on persistent storage prior to completion of the operation.
  406. */
  407. int dax_writeback_mapping_range(struct address_space *mapping, loff_t start,
  408. loff_t end)
  409. {
  410. struct inode *inode = mapping->host;
  411. struct block_device *bdev = inode->i_sb->s_bdev;
  412. pgoff_t start_index, end_index, pmd_index;
  413. pgoff_t indices[PAGEVEC_SIZE];
  414. struct pagevec pvec;
  415. bool done = false;
  416. int i, ret = 0;
  417. void *entry;
  418. if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
  419. return -EIO;
  420. start_index = start >> PAGE_CACHE_SHIFT;
  421. end_index = end >> PAGE_CACHE_SHIFT;
  422. pmd_index = DAX_PMD_INDEX(start_index);
  423. rcu_read_lock();
  424. entry = radix_tree_lookup(&mapping->page_tree, pmd_index);
  425. rcu_read_unlock();
  426. /* see if the start of our range is covered by a PMD entry */
  427. if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD)
  428. start_index = pmd_index;
  429. tag_pages_for_writeback(mapping, start_index, end_index);
  430. pagevec_init(&pvec, 0);
  431. while (!done) {
  432. pvec.nr = find_get_entries_tag(mapping, start_index,
  433. PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
  434. pvec.pages, indices);
  435. if (pvec.nr == 0)
  436. break;
  437. for (i = 0; i < pvec.nr; i++) {
  438. if (indices[i] > end_index) {
  439. done = true;
  440. break;
  441. }
  442. ret = dax_writeback_one(bdev, mapping, indices[i],
  443. pvec.pages[i]);
  444. if (ret < 0)
  445. return ret;
  446. }
  447. }
  448. wmb_pmem();
  449. return 0;
  450. }
  451. EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
  452. static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
  453. struct vm_area_struct *vma, struct vm_fault *vmf)
  454. {
  455. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  456. struct address_space *mapping = inode->i_mapping;
  457. struct block_device *bdev = bh->b_bdev;
  458. struct blk_dax_ctl dax = {
  459. .sector = to_sector(bh, inode),
  460. .size = bh->b_size,
  461. };
  462. pgoff_t size;
  463. int error;
  464. i_mmap_lock_read(mapping);
  465. /*
  466. * Check truncate didn't happen while we were allocating a block.
  467. * If it did, this block may or may not be still allocated to the
  468. * file. We can't tell the filesystem to free it because we can't
  469. * take i_mutex here. In the worst case, the file still has blocks
  470. * allocated past the end of the file.
  471. */
  472. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  473. if (unlikely(vmf->pgoff >= size)) {
  474. error = -EIO;
  475. goto out;
  476. }
  477. if (dax_map_atomic(bdev, &dax) < 0) {
  478. error = PTR_ERR(dax.addr);
  479. goto out;
  480. }
  481. if (buffer_unwritten(bh) || buffer_new(bh)) {
  482. clear_pmem(dax.addr, PAGE_SIZE);
  483. wmb_pmem();
  484. }
  485. dax_unmap_atomic(bdev, &dax);
  486. error = dax_radix_entry(mapping, vmf->pgoff, dax.sector, false,
  487. vmf->flags & FAULT_FLAG_WRITE);
  488. if (error)
  489. goto out;
  490. error = vm_insert_mixed(vma, vaddr, dax.pfn);
  491. out:
  492. i_mmap_unlock_read(mapping);
  493. return error;
  494. }
  495. /**
  496. * __dax_fault - handle a page fault on a DAX file
  497. * @vma: The virtual memory area where the fault occurred
  498. * @vmf: The description of the fault
  499. * @get_block: The filesystem method used to translate file offsets to blocks
  500. * @complete_unwritten: The filesystem method used to convert unwritten blocks
  501. * to written so the data written to them is exposed. This is required for
  502. * required by write faults for filesystems that will return unwritten
  503. * extent mappings from @get_block, but it is optional for reads as
  504. * dax_insert_mapping() will always zero unwritten blocks. If the fs does
  505. * not support unwritten extents, the it should pass NULL.
  506. *
  507. * When a page fault occurs, filesystems may call this helper in their
  508. * fault handler for DAX files. __dax_fault() assumes the caller has done all
  509. * the necessary locking for the page fault to proceed successfully.
  510. */
  511. int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  512. get_block_t get_block, dax_iodone_t complete_unwritten)
  513. {
  514. struct file *file = vma->vm_file;
  515. struct address_space *mapping = file->f_mapping;
  516. struct inode *inode = mapping->host;
  517. struct page *page;
  518. struct buffer_head bh;
  519. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  520. unsigned blkbits = inode->i_blkbits;
  521. sector_t block;
  522. pgoff_t size;
  523. int error;
  524. int major = 0;
  525. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  526. if (vmf->pgoff >= size)
  527. return VM_FAULT_SIGBUS;
  528. memset(&bh, 0, sizeof(bh));
  529. block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
  530. bh.b_bdev = inode->i_sb->s_bdev;
  531. bh.b_size = PAGE_SIZE;
  532. repeat:
  533. page = find_get_page(mapping, vmf->pgoff);
  534. if (page) {
  535. if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
  536. page_cache_release(page);
  537. return VM_FAULT_RETRY;
  538. }
  539. if (unlikely(page->mapping != mapping)) {
  540. unlock_page(page);
  541. page_cache_release(page);
  542. goto repeat;
  543. }
  544. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  545. if (unlikely(vmf->pgoff >= size)) {
  546. /*
  547. * We have a struct page covering a hole in the file
  548. * from a read fault and we've raced with a truncate
  549. */
  550. error = -EIO;
  551. goto unlock_page;
  552. }
  553. }
  554. error = get_block(inode, block, &bh, 0);
  555. if (!error && (bh.b_size < PAGE_SIZE))
  556. error = -EIO; /* fs corruption? */
  557. if (error)
  558. goto unlock_page;
  559. if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) {
  560. if (vmf->flags & FAULT_FLAG_WRITE) {
  561. error = get_block(inode, block, &bh, 1);
  562. count_vm_event(PGMAJFAULT);
  563. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  564. major = VM_FAULT_MAJOR;
  565. if (!error && (bh.b_size < PAGE_SIZE))
  566. error = -EIO;
  567. if (error)
  568. goto unlock_page;
  569. } else {
  570. return dax_load_hole(mapping, page, vmf);
  571. }
  572. }
  573. if (vmf->cow_page) {
  574. struct page *new_page = vmf->cow_page;
  575. if (buffer_written(&bh))
  576. error = copy_user_bh(new_page, inode, &bh, vaddr);
  577. else
  578. clear_user_highpage(new_page, vaddr);
  579. if (error)
  580. goto unlock_page;
  581. vmf->page = page;
  582. if (!page) {
  583. i_mmap_lock_read(mapping);
  584. /* Check we didn't race with truncate */
  585. size = (i_size_read(inode) + PAGE_SIZE - 1) >>
  586. PAGE_SHIFT;
  587. if (vmf->pgoff >= size) {
  588. i_mmap_unlock_read(mapping);
  589. error = -EIO;
  590. goto out;
  591. }
  592. }
  593. return VM_FAULT_LOCKED;
  594. }
  595. /* Check we didn't race with a read fault installing a new page */
  596. if (!page && major)
  597. page = find_lock_page(mapping, vmf->pgoff);
  598. if (page) {
  599. unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
  600. PAGE_CACHE_SIZE, 0);
  601. delete_from_page_cache(page);
  602. unlock_page(page);
  603. page_cache_release(page);
  604. page = NULL;
  605. }
  606. /*
  607. * If we successfully insert the new mapping over an unwritten extent,
  608. * we need to ensure we convert the unwritten extent. If there is an
  609. * error inserting the mapping, the filesystem needs to leave it as
  610. * unwritten to prevent exposure of the stale underlying data to
  611. * userspace, but we still need to call the completion function so
  612. * the private resources on the mapping buffer can be released. We
  613. * indicate what the callback should do via the uptodate variable, same
  614. * as for normal BH based IO completions.
  615. */
  616. error = dax_insert_mapping(inode, &bh, vma, vmf);
  617. if (buffer_unwritten(&bh)) {
  618. if (complete_unwritten)
  619. complete_unwritten(&bh, !error);
  620. else
  621. WARN_ON_ONCE(!(vmf->flags & FAULT_FLAG_WRITE));
  622. }
  623. out:
  624. if (error == -ENOMEM)
  625. return VM_FAULT_OOM | major;
  626. /* -EBUSY is fine, somebody else faulted on the same PTE */
  627. if ((error < 0) && (error != -EBUSY))
  628. return VM_FAULT_SIGBUS | major;
  629. return VM_FAULT_NOPAGE | major;
  630. unlock_page:
  631. if (page) {
  632. unlock_page(page);
  633. page_cache_release(page);
  634. }
  635. goto out;
  636. }
  637. EXPORT_SYMBOL(__dax_fault);
  638. /**
  639. * dax_fault - handle a page fault on a DAX file
  640. * @vma: The virtual memory area where the fault occurred
  641. * @vmf: The description of the fault
  642. * @get_block: The filesystem method used to translate file offsets to blocks
  643. *
  644. * When a page fault occurs, filesystems may call this helper in their
  645. * fault handler for DAX files.
  646. */
  647. int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  648. get_block_t get_block, dax_iodone_t complete_unwritten)
  649. {
  650. int result;
  651. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  652. if (vmf->flags & FAULT_FLAG_WRITE) {
  653. sb_start_pagefault(sb);
  654. file_update_time(vma->vm_file);
  655. }
  656. result = __dax_fault(vma, vmf, get_block, complete_unwritten);
  657. if (vmf->flags & FAULT_FLAG_WRITE)
  658. sb_end_pagefault(sb);
  659. return result;
  660. }
  661. EXPORT_SYMBOL_GPL(dax_fault);
  662. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  663. /*
  664. * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
  665. * more often than one might expect in the below function.
  666. */
  667. #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
  668. static void __dax_dbg(struct buffer_head *bh, unsigned long address,
  669. const char *reason, const char *fn)
  670. {
  671. if (bh) {
  672. char bname[BDEVNAME_SIZE];
  673. bdevname(bh->b_bdev, bname);
  674. pr_debug("%s: %s addr: %lx dev %s state %lx start %lld "
  675. "length %zd fallback: %s\n", fn, current->comm,
  676. address, bname, bh->b_state, (u64)bh->b_blocknr,
  677. bh->b_size, reason);
  678. } else {
  679. pr_debug("%s: %s addr: %lx fallback: %s\n", fn,
  680. current->comm, address, reason);
  681. }
  682. }
  683. #define dax_pmd_dbg(bh, address, reason) __dax_dbg(bh, address, reason, "dax_pmd")
  684. int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
  685. pmd_t *pmd, unsigned int flags, get_block_t get_block,
  686. dax_iodone_t complete_unwritten)
  687. {
  688. struct file *file = vma->vm_file;
  689. struct address_space *mapping = file->f_mapping;
  690. struct inode *inode = mapping->host;
  691. struct buffer_head bh;
  692. unsigned blkbits = inode->i_blkbits;
  693. unsigned long pmd_addr = address & PMD_MASK;
  694. bool write = flags & FAULT_FLAG_WRITE;
  695. struct block_device *bdev;
  696. pgoff_t size, pgoff;
  697. sector_t block;
  698. int error, result = 0;
  699. bool alloc = false;
  700. /* dax pmd mappings require pfn_t_devmap() */
  701. if (!IS_ENABLED(CONFIG_FS_DAX_PMD))
  702. return VM_FAULT_FALLBACK;
  703. /* Fall back to PTEs if we're going to COW */
  704. if (write && !(vma->vm_flags & VM_SHARED)) {
  705. split_huge_pmd(vma, pmd, address);
  706. dax_pmd_dbg(NULL, address, "cow write");
  707. return VM_FAULT_FALLBACK;
  708. }
  709. /* If the PMD would extend outside the VMA */
  710. if (pmd_addr < vma->vm_start) {
  711. dax_pmd_dbg(NULL, address, "vma start unaligned");
  712. return VM_FAULT_FALLBACK;
  713. }
  714. if ((pmd_addr + PMD_SIZE) > vma->vm_end) {
  715. dax_pmd_dbg(NULL, address, "vma end unaligned");
  716. return VM_FAULT_FALLBACK;
  717. }
  718. pgoff = linear_page_index(vma, pmd_addr);
  719. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  720. if (pgoff >= size)
  721. return VM_FAULT_SIGBUS;
  722. /* If the PMD would cover blocks out of the file */
  723. if ((pgoff | PG_PMD_COLOUR) >= size) {
  724. dax_pmd_dbg(NULL, address,
  725. "offset + huge page size > file size");
  726. return VM_FAULT_FALLBACK;
  727. }
  728. memset(&bh, 0, sizeof(bh));
  729. bh.b_bdev = inode->i_sb->s_bdev;
  730. block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
  731. bh.b_size = PMD_SIZE;
  732. if (get_block(inode, block, &bh, 0) != 0)
  733. return VM_FAULT_SIGBUS;
  734. if (!buffer_mapped(&bh) && write) {
  735. if (get_block(inode, block, &bh, 1) != 0)
  736. return VM_FAULT_SIGBUS;
  737. alloc = true;
  738. }
  739. bdev = bh.b_bdev;
  740. /*
  741. * If the filesystem isn't willing to tell us the length of a hole,
  742. * just fall back to PTEs. Calling get_block 512 times in a loop
  743. * would be silly.
  744. */
  745. if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE) {
  746. dax_pmd_dbg(&bh, address, "allocated block too small");
  747. return VM_FAULT_FALLBACK;
  748. }
  749. /*
  750. * If we allocated new storage, make sure no process has any
  751. * zero pages covering this hole
  752. */
  753. if (alloc) {
  754. loff_t lstart = pgoff << PAGE_SHIFT;
  755. loff_t lend = lstart + PMD_SIZE - 1; /* inclusive */
  756. truncate_pagecache_range(inode, lstart, lend);
  757. }
  758. i_mmap_lock_read(mapping);
  759. /*
  760. * If a truncate happened while we were allocating blocks, we may
  761. * leave blocks allocated to the file that are beyond EOF. We can't
  762. * take i_mutex here, so just leave them hanging; they'll be freed
  763. * when the file is deleted.
  764. */
  765. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  766. if (pgoff >= size) {
  767. result = VM_FAULT_SIGBUS;
  768. goto out;
  769. }
  770. if ((pgoff | PG_PMD_COLOUR) >= size) {
  771. dax_pmd_dbg(&bh, address,
  772. "offset + huge page size > file size");
  773. goto fallback;
  774. }
  775. if (!write && !buffer_mapped(&bh) && buffer_uptodate(&bh)) {
  776. spinlock_t *ptl;
  777. pmd_t entry;
  778. struct page *zero_page = get_huge_zero_page();
  779. if (unlikely(!zero_page)) {
  780. dax_pmd_dbg(&bh, address, "no zero page");
  781. goto fallback;
  782. }
  783. ptl = pmd_lock(vma->vm_mm, pmd);
  784. if (!pmd_none(*pmd)) {
  785. spin_unlock(ptl);
  786. dax_pmd_dbg(&bh, address, "pmd already present");
  787. goto fallback;
  788. }
  789. dev_dbg(part_to_dev(bdev->bd_part),
  790. "%s: %s addr: %lx pfn: <zero> sect: %llx\n",
  791. __func__, current->comm, address,
  792. (unsigned long long) to_sector(&bh, inode));
  793. entry = mk_pmd(zero_page, vma->vm_page_prot);
  794. entry = pmd_mkhuge(entry);
  795. set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
  796. result = VM_FAULT_NOPAGE;
  797. spin_unlock(ptl);
  798. } else {
  799. struct blk_dax_ctl dax = {
  800. .sector = to_sector(&bh, inode),
  801. .size = PMD_SIZE,
  802. };
  803. long length = dax_map_atomic(bdev, &dax);
  804. if (length < 0) {
  805. result = VM_FAULT_SIGBUS;
  806. goto out;
  807. }
  808. if (length < PMD_SIZE) {
  809. dax_pmd_dbg(&bh, address, "dax-length too small");
  810. dax_unmap_atomic(bdev, &dax);
  811. goto fallback;
  812. }
  813. if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR) {
  814. dax_pmd_dbg(&bh, address, "pfn unaligned");
  815. dax_unmap_atomic(bdev, &dax);
  816. goto fallback;
  817. }
  818. if (!pfn_t_devmap(dax.pfn)) {
  819. dax_unmap_atomic(bdev, &dax);
  820. dax_pmd_dbg(&bh, address, "pfn not in memmap");
  821. goto fallback;
  822. }
  823. if (buffer_unwritten(&bh) || buffer_new(&bh)) {
  824. clear_pmem(dax.addr, PMD_SIZE);
  825. wmb_pmem();
  826. count_vm_event(PGMAJFAULT);
  827. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  828. result |= VM_FAULT_MAJOR;
  829. }
  830. dax_unmap_atomic(bdev, &dax);
  831. /*
  832. * For PTE faults we insert a radix tree entry for reads, and
  833. * leave it clean. Then on the first write we dirty the radix
  834. * tree entry via the dax_pfn_mkwrite() path. This sequence
  835. * allows the dax_pfn_mkwrite() call to be simpler and avoid a
  836. * call into get_block() to translate the pgoff to a sector in
  837. * order to be able to create a new radix tree entry.
  838. *
  839. * The PMD path doesn't have an equivalent to
  840. * dax_pfn_mkwrite(), though, so for a read followed by a
  841. * write we traverse all the way through __dax_pmd_fault()
  842. * twice. This means we can just skip inserting a radix tree
  843. * entry completely on the initial read and just wait until
  844. * the write to insert a dirty entry.
  845. */
  846. if (write) {
  847. error = dax_radix_entry(mapping, pgoff, dax.sector,
  848. true, true);
  849. if (error) {
  850. dax_pmd_dbg(&bh, address,
  851. "PMD radix insertion failed");
  852. goto fallback;
  853. }
  854. }
  855. dev_dbg(part_to_dev(bdev->bd_part),
  856. "%s: %s addr: %lx pfn: %lx sect: %llx\n",
  857. __func__, current->comm, address,
  858. pfn_t_to_pfn(dax.pfn),
  859. (unsigned long long) dax.sector);
  860. result |= vmf_insert_pfn_pmd(vma, address, pmd,
  861. dax.pfn, write);
  862. }
  863. out:
  864. i_mmap_unlock_read(mapping);
  865. if (buffer_unwritten(&bh))
  866. complete_unwritten(&bh, !(result & VM_FAULT_ERROR));
  867. return result;
  868. fallback:
  869. count_vm_event(THP_FAULT_FALLBACK);
  870. result = VM_FAULT_FALLBACK;
  871. goto out;
  872. }
  873. EXPORT_SYMBOL_GPL(__dax_pmd_fault);
  874. /**
  875. * dax_pmd_fault - handle a PMD fault on a DAX file
  876. * @vma: The virtual memory area where the fault occurred
  877. * @vmf: The description of the fault
  878. * @get_block: The filesystem method used to translate file offsets to blocks
  879. *
  880. * When a page fault occurs, filesystems may call this helper in their
  881. * pmd_fault handler for DAX files.
  882. */
  883. int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
  884. pmd_t *pmd, unsigned int flags, get_block_t get_block,
  885. dax_iodone_t complete_unwritten)
  886. {
  887. int result;
  888. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  889. if (flags & FAULT_FLAG_WRITE) {
  890. sb_start_pagefault(sb);
  891. file_update_time(vma->vm_file);
  892. }
  893. result = __dax_pmd_fault(vma, address, pmd, flags, get_block,
  894. complete_unwritten);
  895. if (flags & FAULT_FLAG_WRITE)
  896. sb_end_pagefault(sb);
  897. return result;
  898. }
  899. EXPORT_SYMBOL_GPL(dax_pmd_fault);
  900. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  901. /**
  902. * dax_pfn_mkwrite - handle first write to DAX page
  903. * @vma: The virtual memory area where the fault occurred
  904. * @vmf: The description of the fault
  905. */
  906. int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  907. {
  908. struct file *file = vma->vm_file;
  909. /*
  910. * We pass NO_SECTOR to dax_radix_entry() because we expect that a
  911. * RADIX_DAX_PTE entry already exists in the radix tree from a
  912. * previous call to __dax_fault(). We just want to look up that PTE
  913. * entry using vmf->pgoff and make sure the dirty tag is set. This
  914. * saves us from having to make a call to get_block() here to look
  915. * up the sector.
  916. */
  917. dax_radix_entry(file->f_mapping, vmf->pgoff, NO_SECTOR, false, true);
  918. return VM_FAULT_NOPAGE;
  919. }
  920. EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
  921. /**
  922. * dax_zero_page_range - zero a range within a page of a DAX file
  923. * @inode: The file being truncated
  924. * @from: The file offset that is being truncated to
  925. * @length: The number of bytes to zero
  926. * @get_block: The filesystem method used to translate file offsets to blocks
  927. *
  928. * This function can be called by a filesystem when it is zeroing part of a
  929. * page in a DAX file. This is intended for hole-punch operations. If
  930. * you are truncating a file, the helper function dax_truncate_page() may be
  931. * more convenient.
  932. *
  933. * We work in terms of PAGE_CACHE_SIZE here for commonality with
  934. * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
  935. * took care of disposing of the unnecessary blocks. Even if the filesystem
  936. * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
  937. * since the file might be mmapped.
  938. */
  939. int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
  940. get_block_t get_block)
  941. {
  942. struct buffer_head bh;
  943. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  944. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  945. int err;
  946. /* Block boundary? Nothing to do */
  947. if (!length)
  948. return 0;
  949. BUG_ON((offset + length) > PAGE_CACHE_SIZE);
  950. memset(&bh, 0, sizeof(bh));
  951. bh.b_bdev = inode->i_sb->s_bdev;
  952. bh.b_size = PAGE_CACHE_SIZE;
  953. err = get_block(inode, index, &bh, 0);
  954. if (err < 0)
  955. return err;
  956. if (buffer_written(&bh)) {
  957. struct block_device *bdev = bh.b_bdev;
  958. struct blk_dax_ctl dax = {
  959. .sector = to_sector(&bh, inode),
  960. .size = PAGE_CACHE_SIZE,
  961. };
  962. if (dax_map_atomic(bdev, &dax) < 0)
  963. return PTR_ERR(dax.addr);
  964. clear_pmem(dax.addr + offset, length);
  965. wmb_pmem();
  966. dax_unmap_atomic(bdev, &dax);
  967. }
  968. return 0;
  969. }
  970. EXPORT_SYMBOL_GPL(dax_zero_page_range);
  971. /**
  972. * dax_truncate_page - handle a partial page being truncated in a DAX file
  973. * @inode: The file being truncated
  974. * @from: The file offset that is being truncated to
  975. * @get_block: The filesystem method used to translate file offsets to blocks
  976. *
  977. * Similar to block_truncate_page(), this function can be called by a
  978. * filesystem when it is truncating a DAX file to handle the partial page.
  979. *
  980. * We work in terms of PAGE_CACHE_SIZE here for commonality with
  981. * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
  982. * took care of disposing of the unnecessary blocks. Even if the filesystem
  983. * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
  984. * since the file might be mmapped.
  985. */
  986. int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
  987. {
  988. unsigned length = PAGE_CACHE_ALIGN(from) - from;
  989. return dax_zero_page_range(inode, from, length, get_block);
  990. }
  991. EXPORT_SYMBOL_GPL(dax_truncate_page);