dax.c 32 KB

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