dax.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256
  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. #include <linux/iomap.h>
  34. #include "internal.h"
  35. /* We choose 4096 entries - same as per-zone page wait tables */
  36. #define DAX_WAIT_TABLE_BITS 12
  37. #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
  38. static wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
  39. static int __init init_dax_wait_table(void)
  40. {
  41. int i;
  42. for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
  43. init_waitqueue_head(wait_table + i);
  44. return 0;
  45. }
  46. fs_initcall(init_dax_wait_table);
  47. static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
  48. {
  49. struct request_queue *q = bdev->bd_queue;
  50. long rc = -EIO;
  51. dax->addr = ERR_PTR(-EIO);
  52. if (blk_queue_enter(q, true) != 0)
  53. return rc;
  54. rc = bdev_direct_access(bdev, dax);
  55. if (rc < 0) {
  56. dax->addr = ERR_PTR(rc);
  57. blk_queue_exit(q);
  58. return rc;
  59. }
  60. return rc;
  61. }
  62. static void dax_unmap_atomic(struct block_device *bdev,
  63. const struct blk_dax_ctl *dax)
  64. {
  65. if (IS_ERR(dax->addr))
  66. return;
  67. blk_queue_exit(bdev->bd_queue);
  68. }
  69. static int dax_is_pmd_entry(void *entry)
  70. {
  71. return (unsigned long)entry & RADIX_DAX_PMD;
  72. }
  73. static int dax_is_pte_entry(void *entry)
  74. {
  75. return !((unsigned long)entry & RADIX_DAX_PMD);
  76. }
  77. static int dax_is_zero_entry(void *entry)
  78. {
  79. return (unsigned long)entry & RADIX_DAX_HZP;
  80. }
  81. static int dax_is_empty_entry(void *entry)
  82. {
  83. return (unsigned long)entry & RADIX_DAX_EMPTY;
  84. }
  85. struct page *read_dax_sector(struct block_device *bdev, sector_t n)
  86. {
  87. struct page *page = alloc_pages(GFP_KERNEL, 0);
  88. struct blk_dax_ctl dax = {
  89. .size = PAGE_SIZE,
  90. .sector = n & ~((((int) PAGE_SIZE) / 512) - 1),
  91. };
  92. long rc;
  93. if (!page)
  94. return ERR_PTR(-ENOMEM);
  95. rc = dax_map_atomic(bdev, &dax);
  96. if (rc < 0)
  97. return ERR_PTR(rc);
  98. memcpy_from_pmem(page_address(page), dax.addr, PAGE_SIZE);
  99. dax_unmap_atomic(bdev, &dax);
  100. return page;
  101. }
  102. /*
  103. * DAX radix tree locking
  104. */
  105. struct exceptional_entry_key {
  106. struct address_space *mapping;
  107. pgoff_t entry_start;
  108. };
  109. struct wait_exceptional_entry_queue {
  110. wait_queue_t wait;
  111. struct exceptional_entry_key key;
  112. };
  113. static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
  114. pgoff_t index, void *entry, struct exceptional_entry_key *key)
  115. {
  116. unsigned long hash;
  117. /*
  118. * If 'entry' is a PMD, align the 'index' that we use for the wait
  119. * queue to the start of that PMD. This ensures that all offsets in
  120. * the range covered by the PMD map to the same bit lock.
  121. */
  122. if (dax_is_pmd_entry(entry))
  123. index &= ~((1UL << (PMD_SHIFT - PAGE_SHIFT)) - 1);
  124. key->mapping = mapping;
  125. key->entry_start = index;
  126. hash = hash_long((unsigned long)mapping ^ index, DAX_WAIT_TABLE_BITS);
  127. return wait_table + hash;
  128. }
  129. static int wake_exceptional_entry_func(wait_queue_t *wait, unsigned int mode,
  130. int sync, void *keyp)
  131. {
  132. struct exceptional_entry_key *key = keyp;
  133. struct wait_exceptional_entry_queue *ewait =
  134. container_of(wait, struct wait_exceptional_entry_queue, wait);
  135. if (key->mapping != ewait->key.mapping ||
  136. key->entry_start != ewait->key.entry_start)
  137. return 0;
  138. return autoremove_wake_function(wait, mode, sync, NULL);
  139. }
  140. /*
  141. * Check whether the given slot is locked. The function must be called with
  142. * mapping->tree_lock held
  143. */
  144. static inline int slot_locked(struct address_space *mapping, void **slot)
  145. {
  146. unsigned long entry = (unsigned long)
  147. radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
  148. return entry & RADIX_DAX_ENTRY_LOCK;
  149. }
  150. /*
  151. * Mark the given slot is locked. The function must be called with
  152. * mapping->tree_lock held
  153. */
  154. static inline void *lock_slot(struct address_space *mapping, void **slot)
  155. {
  156. unsigned long entry = (unsigned long)
  157. radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
  158. entry |= RADIX_DAX_ENTRY_LOCK;
  159. radix_tree_replace_slot(slot, (void *)entry);
  160. return (void *)entry;
  161. }
  162. /*
  163. * Mark the given slot is unlocked. The function must be called with
  164. * mapping->tree_lock held
  165. */
  166. static inline void *unlock_slot(struct address_space *mapping, void **slot)
  167. {
  168. unsigned long entry = (unsigned long)
  169. radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
  170. entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
  171. radix_tree_replace_slot(slot, (void *)entry);
  172. return (void *)entry;
  173. }
  174. /*
  175. * Lookup entry in radix tree, wait for it to become unlocked if it is
  176. * exceptional entry and return it. The caller must call
  177. * put_unlocked_mapping_entry() when he decided not to lock the entry or
  178. * put_locked_mapping_entry() when he locked the entry and now wants to
  179. * unlock it.
  180. *
  181. * The function must be called with mapping->tree_lock held.
  182. */
  183. static void *get_unlocked_mapping_entry(struct address_space *mapping,
  184. pgoff_t index, void ***slotp)
  185. {
  186. void *entry, **slot;
  187. struct wait_exceptional_entry_queue ewait;
  188. wait_queue_head_t *wq;
  189. init_wait(&ewait.wait);
  190. ewait.wait.func = wake_exceptional_entry_func;
  191. for (;;) {
  192. entry = __radix_tree_lookup(&mapping->page_tree, index, NULL,
  193. &slot);
  194. if (!entry || !radix_tree_exceptional_entry(entry) ||
  195. !slot_locked(mapping, slot)) {
  196. if (slotp)
  197. *slotp = slot;
  198. return entry;
  199. }
  200. wq = dax_entry_waitqueue(mapping, index, entry, &ewait.key);
  201. prepare_to_wait_exclusive(wq, &ewait.wait,
  202. TASK_UNINTERRUPTIBLE);
  203. spin_unlock_irq(&mapping->tree_lock);
  204. schedule();
  205. finish_wait(wq, &ewait.wait);
  206. spin_lock_irq(&mapping->tree_lock);
  207. }
  208. }
  209. static void put_locked_mapping_entry(struct address_space *mapping,
  210. pgoff_t index, void *entry)
  211. {
  212. if (!radix_tree_exceptional_entry(entry)) {
  213. unlock_page(entry);
  214. put_page(entry);
  215. } else {
  216. dax_unlock_mapping_entry(mapping, index);
  217. }
  218. }
  219. /*
  220. * Called when we are done with radix tree entry we looked up via
  221. * get_unlocked_mapping_entry() and which we didn't lock in the end.
  222. */
  223. static void put_unlocked_mapping_entry(struct address_space *mapping,
  224. pgoff_t index, void *entry)
  225. {
  226. if (!radix_tree_exceptional_entry(entry))
  227. return;
  228. /* We have to wake up next waiter for the radix tree entry lock */
  229. dax_wake_mapping_entry_waiter(mapping, index, entry, false);
  230. }
  231. /*
  232. * Find radix tree entry at given index. If it points to a page, return with
  233. * the page locked. If it points to the exceptional entry, return with the
  234. * radix tree entry locked. If the radix tree doesn't contain given index,
  235. * create empty exceptional entry for the index and return with it locked.
  236. *
  237. * When requesting an entry with size RADIX_DAX_PMD, grab_mapping_entry() will
  238. * either return that locked entry or will return an error. This error will
  239. * happen if there are any 4k entries (either zero pages or DAX entries)
  240. * within the 2MiB range that we are requesting.
  241. *
  242. * We always favor 4k entries over 2MiB entries. There isn't a flow where we
  243. * evict 4k entries in order to 'upgrade' them to a 2MiB entry. A 2MiB
  244. * insertion will fail if it finds any 4k entries already in the tree, and a
  245. * 4k insertion will cause an existing 2MiB entry to be unmapped and
  246. * downgraded to 4k entries. This happens for both 2MiB huge zero pages as
  247. * well as 2MiB empty entries.
  248. *
  249. * The exception to this downgrade path is for 2MiB DAX PMD entries that have
  250. * real storage backing them. We will leave these real 2MiB DAX entries in
  251. * the tree, and PTE writes will simply dirty the entire 2MiB DAX entry.
  252. *
  253. * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
  254. * persistent memory the benefit is doubtful. We can add that later if we can
  255. * show it helps.
  256. */
  257. static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index,
  258. unsigned long size_flag)
  259. {
  260. bool pmd_downgrade = false; /* splitting 2MiB entry into 4k entries? */
  261. void *entry, **slot;
  262. restart:
  263. spin_lock_irq(&mapping->tree_lock);
  264. entry = get_unlocked_mapping_entry(mapping, index, &slot);
  265. if (entry) {
  266. if (size_flag & RADIX_DAX_PMD) {
  267. if (!radix_tree_exceptional_entry(entry) ||
  268. dax_is_pte_entry(entry)) {
  269. put_unlocked_mapping_entry(mapping, index,
  270. entry);
  271. entry = ERR_PTR(-EEXIST);
  272. goto out_unlock;
  273. }
  274. } else { /* trying to grab a PTE entry */
  275. if (radix_tree_exceptional_entry(entry) &&
  276. dax_is_pmd_entry(entry) &&
  277. (dax_is_zero_entry(entry) ||
  278. dax_is_empty_entry(entry))) {
  279. pmd_downgrade = true;
  280. }
  281. }
  282. }
  283. /* No entry for given index? Make sure radix tree is big enough. */
  284. if (!entry || pmd_downgrade) {
  285. int err;
  286. if (pmd_downgrade) {
  287. /*
  288. * Make sure 'entry' remains valid while we drop
  289. * mapping->tree_lock.
  290. */
  291. entry = lock_slot(mapping, slot);
  292. }
  293. spin_unlock_irq(&mapping->tree_lock);
  294. err = radix_tree_preload(
  295. mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
  296. if (err) {
  297. if (pmd_downgrade)
  298. put_locked_mapping_entry(mapping, index, entry);
  299. return ERR_PTR(err);
  300. }
  301. /*
  302. * Besides huge zero pages the only other thing that gets
  303. * downgraded are empty entries which don't need to be
  304. * unmapped.
  305. */
  306. if (pmd_downgrade && dax_is_zero_entry(entry))
  307. unmap_mapping_range(mapping,
  308. (index << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0);
  309. spin_lock_irq(&mapping->tree_lock);
  310. if (pmd_downgrade) {
  311. radix_tree_delete(&mapping->page_tree, index);
  312. mapping->nrexceptional--;
  313. dax_wake_mapping_entry_waiter(mapping, index, entry,
  314. true);
  315. }
  316. entry = dax_radix_locked_entry(0, size_flag | RADIX_DAX_EMPTY);
  317. err = __radix_tree_insert(&mapping->page_tree, index,
  318. dax_radix_order(entry), entry);
  319. radix_tree_preload_end();
  320. if (err) {
  321. spin_unlock_irq(&mapping->tree_lock);
  322. /*
  323. * Someone already created the entry? This is a
  324. * normal failure when inserting PMDs in a range
  325. * that already contains PTEs. In that case we want
  326. * to return -EEXIST immediately.
  327. */
  328. if (err == -EEXIST && !(size_flag & RADIX_DAX_PMD))
  329. goto restart;
  330. /*
  331. * Our insertion of a DAX PMD entry failed, most
  332. * likely because it collided with a PTE sized entry
  333. * at a different index in the PMD range. We haven't
  334. * inserted anything into the radix tree and have no
  335. * waiters to wake.
  336. */
  337. return ERR_PTR(err);
  338. }
  339. /* Good, we have inserted empty locked entry into the tree. */
  340. mapping->nrexceptional++;
  341. spin_unlock_irq(&mapping->tree_lock);
  342. return entry;
  343. }
  344. /* Normal page in radix tree? */
  345. if (!radix_tree_exceptional_entry(entry)) {
  346. struct page *page = entry;
  347. get_page(page);
  348. spin_unlock_irq(&mapping->tree_lock);
  349. lock_page(page);
  350. /* Page got truncated? Retry... */
  351. if (unlikely(page->mapping != mapping)) {
  352. unlock_page(page);
  353. put_page(page);
  354. goto restart;
  355. }
  356. return page;
  357. }
  358. entry = lock_slot(mapping, slot);
  359. out_unlock:
  360. spin_unlock_irq(&mapping->tree_lock);
  361. return entry;
  362. }
  363. /*
  364. * We do not necessarily hold the mapping->tree_lock when we call this
  365. * function so it is possible that 'entry' is no longer a valid item in the
  366. * radix tree. This is okay because all we really need to do is to find the
  367. * correct waitqueue where tasks might be waiting for that old 'entry' and
  368. * wake them.
  369. */
  370. void dax_wake_mapping_entry_waiter(struct address_space *mapping,
  371. pgoff_t index, void *entry, bool wake_all)
  372. {
  373. struct exceptional_entry_key key;
  374. wait_queue_head_t *wq;
  375. wq = dax_entry_waitqueue(mapping, index, entry, &key);
  376. /*
  377. * Checking for locked entry and prepare_to_wait_exclusive() happens
  378. * under mapping->tree_lock, ditto for entry handling in our callers.
  379. * So at this point all tasks that could have seen our entry locked
  380. * must be in the waitqueue and the following check will see them.
  381. */
  382. if (waitqueue_active(wq))
  383. __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
  384. }
  385. void dax_unlock_mapping_entry(struct address_space *mapping, pgoff_t index)
  386. {
  387. void *entry, **slot;
  388. spin_lock_irq(&mapping->tree_lock);
  389. entry = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
  390. if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry) ||
  391. !slot_locked(mapping, slot))) {
  392. spin_unlock_irq(&mapping->tree_lock);
  393. return;
  394. }
  395. unlock_slot(mapping, slot);
  396. spin_unlock_irq(&mapping->tree_lock);
  397. dax_wake_mapping_entry_waiter(mapping, index, entry, false);
  398. }
  399. /*
  400. * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
  401. * entry to get unlocked before deleting it.
  402. */
  403. int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
  404. {
  405. void *entry;
  406. spin_lock_irq(&mapping->tree_lock);
  407. entry = get_unlocked_mapping_entry(mapping, index, NULL);
  408. /*
  409. * This gets called from truncate / punch_hole path. As such, the caller
  410. * must hold locks protecting against concurrent modifications of the
  411. * radix tree (usually fs-private i_mmap_sem for writing). Since the
  412. * caller has seen exceptional entry for this index, we better find it
  413. * at that index as well...
  414. */
  415. if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry))) {
  416. spin_unlock_irq(&mapping->tree_lock);
  417. return 0;
  418. }
  419. radix_tree_delete(&mapping->page_tree, index);
  420. mapping->nrexceptional--;
  421. spin_unlock_irq(&mapping->tree_lock);
  422. dax_wake_mapping_entry_waiter(mapping, index, entry, true);
  423. return 1;
  424. }
  425. /*
  426. * The user has performed a load from a hole in the file. Allocating
  427. * a new page in the file would cause excessive storage usage for
  428. * workloads with sparse files. We allocate a page cache page instead.
  429. * We'll kick it out of the page cache if it's ever written to,
  430. * otherwise it will simply fall out of the page cache under memory
  431. * pressure without ever having been dirtied.
  432. */
  433. static int dax_load_hole(struct address_space *mapping, void *entry,
  434. struct vm_fault *vmf)
  435. {
  436. struct page *page;
  437. /* Hole page already exists? Return it... */
  438. if (!radix_tree_exceptional_entry(entry)) {
  439. vmf->page = entry;
  440. return VM_FAULT_LOCKED;
  441. }
  442. /* This will replace locked radix tree entry with a hole page */
  443. page = find_or_create_page(mapping, vmf->pgoff,
  444. vmf->gfp_mask | __GFP_ZERO);
  445. if (!page) {
  446. put_locked_mapping_entry(mapping, vmf->pgoff, entry);
  447. return VM_FAULT_OOM;
  448. }
  449. vmf->page = page;
  450. return VM_FAULT_LOCKED;
  451. }
  452. static int copy_user_dax(struct block_device *bdev, sector_t sector, size_t size,
  453. struct page *to, unsigned long vaddr)
  454. {
  455. struct blk_dax_ctl dax = {
  456. .sector = sector,
  457. .size = size,
  458. };
  459. void *vto;
  460. if (dax_map_atomic(bdev, &dax) < 0)
  461. return PTR_ERR(dax.addr);
  462. vto = kmap_atomic(to);
  463. copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
  464. kunmap_atomic(vto);
  465. dax_unmap_atomic(bdev, &dax);
  466. return 0;
  467. }
  468. /*
  469. * By this point grab_mapping_entry() has ensured that we have a locked entry
  470. * of the appropriate size so we don't have to worry about downgrading PMDs to
  471. * PTEs. If we happen to be trying to insert a PTE and there is a PMD
  472. * already in the tree, we will skip the insertion and just dirty the PMD as
  473. * appropriate.
  474. */
  475. static void *dax_insert_mapping_entry(struct address_space *mapping,
  476. struct vm_fault *vmf,
  477. void *entry, sector_t sector,
  478. unsigned long flags)
  479. {
  480. struct radix_tree_root *page_tree = &mapping->page_tree;
  481. int error = 0;
  482. bool hole_fill = false;
  483. void *new_entry;
  484. pgoff_t index = vmf->pgoff;
  485. if (vmf->flags & FAULT_FLAG_WRITE)
  486. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  487. /* Replacing hole page with block mapping? */
  488. if (!radix_tree_exceptional_entry(entry)) {
  489. hole_fill = true;
  490. /*
  491. * Unmap the page now before we remove it from page cache below.
  492. * The page is locked so it cannot be faulted in again.
  493. */
  494. unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
  495. PAGE_SIZE, 0);
  496. error = radix_tree_preload(vmf->gfp_mask & ~__GFP_HIGHMEM);
  497. if (error)
  498. return ERR_PTR(error);
  499. } else if (dax_is_zero_entry(entry) && !(flags & RADIX_DAX_HZP)) {
  500. /* replacing huge zero page with PMD block mapping */
  501. unmap_mapping_range(mapping,
  502. (vmf->pgoff << PAGE_SHIFT) & PMD_MASK, PMD_SIZE, 0);
  503. }
  504. spin_lock_irq(&mapping->tree_lock);
  505. new_entry = dax_radix_locked_entry(sector, flags);
  506. if (hole_fill) {
  507. __delete_from_page_cache(entry, NULL);
  508. /* Drop pagecache reference */
  509. put_page(entry);
  510. error = __radix_tree_insert(page_tree, index,
  511. dax_radix_order(new_entry), new_entry);
  512. if (error) {
  513. new_entry = ERR_PTR(error);
  514. goto unlock;
  515. }
  516. mapping->nrexceptional++;
  517. } else if (dax_is_zero_entry(entry) || dax_is_empty_entry(entry)) {
  518. /*
  519. * Only swap our new entry into the radix tree if the current
  520. * entry is a zero page or an empty entry. If a normal PTE or
  521. * PMD entry is already in the tree, we leave it alone. This
  522. * means that if we are trying to insert a PTE and the
  523. * existing entry is a PMD, we will just leave the PMD in the
  524. * tree and dirty it if necessary.
  525. */
  526. void **slot;
  527. void *ret;
  528. ret = __radix_tree_lookup(page_tree, index, NULL, &slot);
  529. WARN_ON_ONCE(ret != entry);
  530. radix_tree_replace_slot(slot, new_entry);
  531. }
  532. if (vmf->flags & FAULT_FLAG_WRITE)
  533. radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
  534. unlock:
  535. spin_unlock_irq(&mapping->tree_lock);
  536. if (hole_fill) {
  537. radix_tree_preload_end();
  538. /*
  539. * We don't need hole page anymore, it has been replaced with
  540. * locked radix tree entry now.
  541. */
  542. if (mapping->a_ops->freepage)
  543. mapping->a_ops->freepage(entry);
  544. unlock_page(entry);
  545. put_page(entry);
  546. }
  547. return new_entry;
  548. }
  549. static int dax_writeback_one(struct block_device *bdev,
  550. struct address_space *mapping, pgoff_t index, void *entry)
  551. {
  552. struct radix_tree_root *page_tree = &mapping->page_tree;
  553. struct radix_tree_node *node;
  554. struct blk_dax_ctl dax;
  555. void **slot;
  556. int ret = 0;
  557. spin_lock_irq(&mapping->tree_lock);
  558. /*
  559. * Regular page slots are stabilized by the page lock even
  560. * without the tree itself locked. These unlocked entries
  561. * need verification under the tree lock.
  562. */
  563. if (!__radix_tree_lookup(page_tree, index, &node, &slot))
  564. goto unlock;
  565. if (*slot != entry)
  566. goto unlock;
  567. /* another fsync thread may have already written back this entry */
  568. if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
  569. goto unlock;
  570. if (WARN_ON_ONCE(dax_is_empty_entry(entry) ||
  571. dax_is_zero_entry(entry))) {
  572. ret = -EIO;
  573. goto unlock;
  574. }
  575. /*
  576. * Even if dax_writeback_mapping_range() was given a wbc->range_start
  577. * in the middle of a PMD, the 'index' we are given will be aligned to
  578. * the start index of the PMD, as will the sector we pull from
  579. * 'entry'. This allows us to flush for PMD_SIZE and not have to
  580. * worry about partial PMD writebacks.
  581. */
  582. dax.sector = dax_radix_sector(entry);
  583. dax.size = PAGE_SIZE << dax_radix_order(entry);
  584. spin_unlock_irq(&mapping->tree_lock);
  585. /*
  586. * We cannot hold tree_lock while calling dax_map_atomic() because it
  587. * eventually calls cond_resched().
  588. */
  589. ret = dax_map_atomic(bdev, &dax);
  590. if (ret < 0)
  591. return ret;
  592. if (WARN_ON_ONCE(ret < dax.size)) {
  593. ret = -EIO;
  594. goto unmap;
  595. }
  596. wb_cache_pmem(dax.addr, dax.size);
  597. spin_lock_irq(&mapping->tree_lock);
  598. radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
  599. spin_unlock_irq(&mapping->tree_lock);
  600. unmap:
  601. dax_unmap_atomic(bdev, &dax);
  602. return ret;
  603. unlock:
  604. spin_unlock_irq(&mapping->tree_lock);
  605. return ret;
  606. }
  607. /*
  608. * Flush the mapping to the persistent domain within the byte range of [start,
  609. * end]. This is required by data integrity operations to ensure file data is
  610. * on persistent storage prior to completion of the operation.
  611. */
  612. int dax_writeback_mapping_range(struct address_space *mapping,
  613. struct block_device *bdev, struct writeback_control *wbc)
  614. {
  615. struct inode *inode = mapping->host;
  616. pgoff_t start_index, end_index;
  617. pgoff_t indices[PAGEVEC_SIZE];
  618. struct pagevec pvec;
  619. bool done = false;
  620. int i, ret = 0;
  621. if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
  622. return -EIO;
  623. if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
  624. return 0;
  625. start_index = wbc->range_start >> PAGE_SHIFT;
  626. end_index = wbc->range_end >> PAGE_SHIFT;
  627. tag_pages_for_writeback(mapping, start_index, end_index);
  628. pagevec_init(&pvec, 0);
  629. while (!done) {
  630. pvec.nr = find_get_entries_tag(mapping, start_index,
  631. PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
  632. pvec.pages, indices);
  633. if (pvec.nr == 0)
  634. break;
  635. for (i = 0; i < pvec.nr; i++) {
  636. if (indices[i] > end_index) {
  637. done = true;
  638. break;
  639. }
  640. ret = dax_writeback_one(bdev, mapping, indices[i],
  641. pvec.pages[i]);
  642. if (ret < 0)
  643. return ret;
  644. }
  645. }
  646. return 0;
  647. }
  648. EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
  649. static int dax_insert_mapping(struct address_space *mapping,
  650. struct block_device *bdev, sector_t sector, size_t size,
  651. void **entryp, struct vm_area_struct *vma, struct vm_fault *vmf)
  652. {
  653. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  654. struct blk_dax_ctl dax = {
  655. .sector = sector,
  656. .size = size,
  657. };
  658. void *ret;
  659. void *entry = *entryp;
  660. if (dax_map_atomic(bdev, &dax) < 0)
  661. return PTR_ERR(dax.addr);
  662. dax_unmap_atomic(bdev, &dax);
  663. ret = dax_insert_mapping_entry(mapping, vmf, entry, dax.sector, 0);
  664. if (IS_ERR(ret))
  665. return PTR_ERR(ret);
  666. *entryp = ret;
  667. return vm_insert_mixed(vma, vaddr, dax.pfn);
  668. }
  669. /**
  670. * dax_pfn_mkwrite - handle first write to DAX page
  671. * @vma: The virtual memory area where the fault occurred
  672. * @vmf: The description of the fault
  673. */
  674. int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  675. {
  676. struct file *file = vma->vm_file;
  677. struct address_space *mapping = file->f_mapping;
  678. void *entry;
  679. pgoff_t index = vmf->pgoff;
  680. spin_lock_irq(&mapping->tree_lock);
  681. entry = get_unlocked_mapping_entry(mapping, index, NULL);
  682. if (!entry || !radix_tree_exceptional_entry(entry))
  683. goto out;
  684. radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY);
  685. put_unlocked_mapping_entry(mapping, index, entry);
  686. out:
  687. spin_unlock_irq(&mapping->tree_lock);
  688. return VM_FAULT_NOPAGE;
  689. }
  690. EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
  691. static bool dax_range_is_aligned(struct block_device *bdev,
  692. unsigned int offset, unsigned int length)
  693. {
  694. unsigned short sector_size = bdev_logical_block_size(bdev);
  695. if (!IS_ALIGNED(offset, sector_size))
  696. return false;
  697. if (!IS_ALIGNED(length, sector_size))
  698. return false;
  699. return true;
  700. }
  701. int __dax_zero_page_range(struct block_device *bdev, sector_t sector,
  702. unsigned int offset, unsigned int length)
  703. {
  704. struct blk_dax_ctl dax = {
  705. .sector = sector,
  706. .size = PAGE_SIZE,
  707. };
  708. if (dax_range_is_aligned(bdev, offset, length)) {
  709. sector_t start_sector = dax.sector + (offset >> 9);
  710. return blkdev_issue_zeroout(bdev, start_sector,
  711. length >> 9, GFP_NOFS, true);
  712. } else {
  713. if (dax_map_atomic(bdev, &dax) < 0)
  714. return PTR_ERR(dax.addr);
  715. clear_pmem(dax.addr + offset, length);
  716. dax_unmap_atomic(bdev, &dax);
  717. }
  718. return 0;
  719. }
  720. EXPORT_SYMBOL_GPL(__dax_zero_page_range);
  721. #ifdef CONFIG_FS_IOMAP
  722. static sector_t dax_iomap_sector(struct iomap *iomap, loff_t pos)
  723. {
  724. return iomap->blkno + (((pos & PAGE_MASK) - iomap->offset) >> 9);
  725. }
  726. static loff_t
  727. dax_iomap_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  728. struct iomap *iomap)
  729. {
  730. struct iov_iter *iter = data;
  731. loff_t end = pos + length, done = 0;
  732. ssize_t ret = 0;
  733. if (iov_iter_rw(iter) == READ) {
  734. end = min(end, i_size_read(inode));
  735. if (pos >= end)
  736. return 0;
  737. if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
  738. return iov_iter_zero(min(length, end - pos), iter);
  739. }
  740. if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
  741. return -EIO;
  742. while (pos < end) {
  743. unsigned offset = pos & (PAGE_SIZE - 1);
  744. struct blk_dax_ctl dax = { 0 };
  745. ssize_t map_len;
  746. dax.sector = dax_iomap_sector(iomap, pos);
  747. dax.size = (length + offset + PAGE_SIZE - 1) & PAGE_MASK;
  748. map_len = dax_map_atomic(iomap->bdev, &dax);
  749. if (map_len < 0) {
  750. ret = map_len;
  751. break;
  752. }
  753. dax.addr += offset;
  754. map_len -= offset;
  755. if (map_len > end - pos)
  756. map_len = end - pos;
  757. if (iov_iter_rw(iter) == WRITE)
  758. map_len = copy_from_iter_pmem(dax.addr, map_len, iter);
  759. else
  760. map_len = copy_to_iter(dax.addr, map_len, iter);
  761. dax_unmap_atomic(iomap->bdev, &dax);
  762. if (map_len <= 0) {
  763. ret = map_len ? map_len : -EFAULT;
  764. break;
  765. }
  766. pos += map_len;
  767. length -= map_len;
  768. done += map_len;
  769. }
  770. return done ? done : ret;
  771. }
  772. /**
  773. * dax_iomap_rw - Perform I/O to a DAX file
  774. * @iocb: The control block for this I/O
  775. * @iter: The addresses to do I/O from or to
  776. * @ops: iomap ops passed from the file system
  777. *
  778. * This function performs read and write operations to directly mapped
  779. * persistent memory. The callers needs to take care of read/write exclusion
  780. * and evicting any page cache pages in the region under I/O.
  781. */
  782. ssize_t
  783. dax_iomap_rw(struct kiocb *iocb, struct iov_iter *iter,
  784. struct iomap_ops *ops)
  785. {
  786. struct address_space *mapping = iocb->ki_filp->f_mapping;
  787. struct inode *inode = mapping->host;
  788. loff_t pos = iocb->ki_pos, ret = 0, done = 0;
  789. unsigned flags = 0;
  790. if (iov_iter_rw(iter) == WRITE)
  791. flags |= IOMAP_WRITE;
  792. /*
  793. * Yes, even DAX files can have page cache attached to them: A zeroed
  794. * page is inserted into the pagecache when we have to serve a write
  795. * fault on a hole. It should never be dirtied and can simply be
  796. * dropped from the pagecache once we get real data for the page.
  797. *
  798. * XXX: This is racy against mmap, and there's nothing we can do about
  799. * it. We'll eventually need to shift this down even further so that
  800. * we can check if we allocated blocks over a hole first.
  801. */
  802. if (mapping->nrpages) {
  803. ret = invalidate_inode_pages2_range(mapping,
  804. pos >> PAGE_SHIFT,
  805. (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT);
  806. WARN_ON_ONCE(ret);
  807. }
  808. while (iov_iter_count(iter)) {
  809. ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
  810. iter, dax_iomap_actor);
  811. if (ret <= 0)
  812. break;
  813. pos += ret;
  814. done += ret;
  815. }
  816. iocb->ki_pos += done;
  817. return done ? done : ret;
  818. }
  819. EXPORT_SYMBOL_GPL(dax_iomap_rw);
  820. /**
  821. * dax_iomap_fault - handle a page fault on a DAX file
  822. * @vma: The virtual memory area where the fault occurred
  823. * @vmf: The description of the fault
  824. * @ops: iomap ops passed from the file system
  825. *
  826. * When a page fault occurs, filesystems may call this helper in their fault
  827. * or mkwrite handler for DAX files. Assumes the caller has done all the
  828. * necessary locking for the page fault to proceed successfully.
  829. */
  830. int dax_iomap_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  831. struct iomap_ops *ops)
  832. {
  833. struct address_space *mapping = vma->vm_file->f_mapping;
  834. struct inode *inode = mapping->host;
  835. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  836. loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
  837. sector_t sector;
  838. struct iomap iomap = { 0 };
  839. unsigned flags = IOMAP_FAULT;
  840. int error, major = 0;
  841. int locked_status = 0;
  842. void *entry;
  843. /*
  844. * Check whether offset isn't beyond end of file now. Caller is supposed
  845. * to hold locks serializing us with truncate / punch hole so this is
  846. * a reliable test.
  847. */
  848. if (pos >= i_size_read(inode))
  849. return VM_FAULT_SIGBUS;
  850. entry = grab_mapping_entry(mapping, vmf->pgoff, 0);
  851. if (IS_ERR(entry)) {
  852. error = PTR_ERR(entry);
  853. goto out;
  854. }
  855. if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
  856. flags |= IOMAP_WRITE;
  857. /*
  858. * Note that we don't bother to use iomap_apply here: DAX required
  859. * the file system block size to be equal the page size, which means
  860. * that we never have to deal with more than a single extent here.
  861. */
  862. error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
  863. if (error)
  864. goto unlock_entry;
  865. if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
  866. error = -EIO; /* fs corruption? */
  867. goto finish_iomap;
  868. }
  869. sector = dax_iomap_sector(&iomap, pos);
  870. if (vmf->cow_page) {
  871. switch (iomap.type) {
  872. case IOMAP_HOLE:
  873. case IOMAP_UNWRITTEN:
  874. clear_user_highpage(vmf->cow_page, vaddr);
  875. break;
  876. case IOMAP_MAPPED:
  877. error = copy_user_dax(iomap.bdev, sector, PAGE_SIZE,
  878. vmf->cow_page, vaddr);
  879. break;
  880. default:
  881. WARN_ON_ONCE(1);
  882. error = -EIO;
  883. break;
  884. }
  885. if (error)
  886. goto finish_iomap;
  887. if (!radix_tree_exceptional_entry(entry)) {
  888. vmf->page = entry;
  889. locked_status = VM_FAULT_LOCKED;
  890. } else {
  891. vmf->entry = entry;
  892. locked_status = VM_FAULT_DAX_LOCKED;
  893. }
  894. goto finish_iomap;
  895. }
  896. switch (iomap.type) {
  897. case IOMAP_MAPPED:
  898. if (iomap.flags & IOMAP_F_NEW) {
  899. count_vm_event(PGMAJFAULT);
  900. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  901. major = VM_FAULT_MAJOR;
  902. }
  903. error = dax_insert_mapping(mapping, iomap.bdev, sector,
  904. PAGE_SIZE, &entry, vma, vmf);
  905. break;
  906. case IOMAP_UNWRITTEN:
  907. case IOMAP_HOLE:
  908. if (!(vmf->flags & FAULT_FLAG_WRITE)) {
  909. locked_status = dax_load_hole(mapping, entry, vmf);
  910. break;
  911. }
  912. /*FALLTHRU*/
  913. default:
  914. WARN_ON_ONCE(1);
  915. error = -EIO;
  916. break;
  917. }
  918. finish_iomap:
  919. if (ops->iomap_end) {
  920. if (error) {
  921. /* keep previous error */
  922. ops->iomap_end(inode, pos, PAGE_SIZE, 0, flags,
  923. &iomap);
  924. } else {
  925. error = ops->iomap_end(inode, pos, PAGE_SIZE,
  926. PAGE_SIZE, flags, &iomap);
  927. }
  928. }
  929. unlock_entry:
  930. if (!locked_status || error)
  931. put_locked_mapping_entry(mapping, vmf->pgoff, entry);
  932. out:
  933. if (error == -ENOMEM)
  934. return VM_FAULT_OOM | major;
  935. /* -EBUSY is fine, somebody else faulted on the same PTE */
  936. if (error < 0 && error != -EBUSY)
  937. return VM_FAULT_SIGBUS | major;
  938. if (locked_status) {
  939. WARN_ON_ONCE(error); /* -EBUSY from ops->iomap_end? */
  940. return locked_status;
  941. }
  942. return VM_FAULT_NOPAGE | major;
  943. }
  944. EXPORT_SYMBOL_GPL(dax_iomap_fault);
  945. #ifdef CONFIG_FS_DAX_PMD
  946. /*
  947. * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
  948. * more often than one might expect in the below functions.
  949. */
  950. #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
  951. static int dax_pmd_insert_mapping(struct vm_area_struct *vma, pmd_t *pmd,
  952. struct vm_fault *vmf, unsigned long address,
  953. struct iomap *iomap, loff_t pos, bool write, void **entryp)
  954. {
  955. struct address_space *mapping = vma->vm_file->f_mapping;
  956. struct block_device *bdev = iomap->bdev;
  957. struct blk_dax_ctl dax = {
  958. .sector = dax_iomap_sector(iomap, pos),
  959. .size = PMD_SIZE,
  960. };
  961. long length = dax_map_atomic(bdev, &dax);
  962. void *ret;
  963. if (length < 0) /* dax_map_atomic() failed */
  964. return VM_FAULT_FALLBACK;
  965. if (length < PMD_SIZE)
  966. goto unmap_fallback;
  967. if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR)
  968. goto unmap_fallback;
  969. if (!pfn_t_devmap(dax.pfn))
  970. goto unmap_fallback;
  971. dax_unmap_atomic(bdev, &dax);
  972. ret = dax_insert_mapping_entry(mapping, vmf, *entryp, dax.sector,
  973. RADIX_DAX_PMD);
  974. if (IS_ERR(ret))
  975. return VM_FAULT_FALLBACK;
  976. *entryp = ret;
  977. return vmf_insert_pfn_pmd(vma, address, pmd, dax.pfn, write);
  978. unmap_fallback:
  979. dax_unmap_atomic(bdev, &dax);
  980. return VM_FAULT_FALLBACK;
  981. }
  982. static int dax_pmd_load_hole(struct vm_area_struct *vma, pmd_t *pmd,
  983. struct vm_fault *vmf, unsigned long address,
  984. struct iomap *iomap, void **entryp)
  985. {
  986. struct address_space *mapping = vma->vm_file->f_mapping;
  987. unsigned long pmd_addr = address & PMD_MASK;
  988. struct page *zero_page;
  989. spinlock_t *ptl;
  990. pmd_t pmd_entry;
  991. void *ret;
  992. zero_page = mm_get_huge_zero_page(vma->vm_mm);
  993. if (unlikely(!zero_page))
  994. return VM_FAULT_FALLBACK;
  995. ret = dax_insert_mapping_entry(mapping, vmf, *entryp, 0,
  996. RADIX_DAX_PMD | RADIX_DAX_HZP);
  997. if (IS_ERR(ret))
  998. return VM_FAULT_FALLBACK;
  999. *entryp = ret;
  1000. ptl = pmd_lock(vma->vm_mm, pmd);
  1001. if (!pmd_none(*pmd)) {
  1002. spin_unlock(ptl);
  1003. return VM_FAULT_FALLBACK;
  1004. }
  1005. pmd_entry = mk_pmd(zero_page, vma->vm_page_prot);
  1006. pmd_entry = pmd_mkhuge(pmd_entry);
  1007. set_pmd_at(vma->vm_mm, pmd_addr, pmd, pmd_entry);
  1008. spin_unlock(ptl);
  1009. return VM_FAULT_NOPAGE;
  1010. }
  1011. int dax_iomap_pmd_fault(struct vm_area_struct *vma, unsigned long address,
  1012. pmd_t *pmd, unsigned int flags, struct iomap_ops *ops)
  1013. {
  1014. struct address_space *mapping = vma->vm_file->f_mapping;
  1015. unsigned long pmd_addr = address & PMD_MASK;
  1016. bool write = flags & FAULT_FLAG_WRITE;
  1017. unsigned int iomap_flags = (write ? IOMAP_WRITE : 0) | IOMAP_FAULT;
  1018. struct inode *inode = mapping->host;
  1019. int result = VM_FAULT_FALLBACK;
  1020. struct iomap iomap = { 0 };
  1021. pgoff_t max_pgoff, pgoff;
  1022. struct vm_fault vmf;
  1023. void *entry;
  1024. loff_t pos;
  1025. int error;
  1026. /* Fall back to PTEs if we're going to COW */
  1027. if (write && !(vma->vm_flags & VM_SHARED))
  1028. goto fallback;
  1029. /* If the PMD would extend outside the VMA */
  1030. if (pmd_addr < vma->vm_start)
  1031. goto fallback;
  1032. if ((pmd_addr + PMD_SIZE) > vma->vm_end)
  1033. goto fallback;
  1034. /*
  1035. * Check whether offset isn't beyond end of file now. Caller is
  1036. * supposed to hold locks serializing us with truncate / punch hole so
  1037. * this is a reliable test.
  1038. */
  1039. pgoff = linear_page_index(vma, pmd_addr);
  1040. max_pgoff = (i_size_read(inode) - 1) >> PAGE_SHIFT;
  1041. if (pgoff > max_pgoff)
  1042. return VM_FAULT_SIGBUS;
  1043. /* If the PMD would extend beyond the file size */
  1044. if ((pgoff | PG_PMD_COLOUR) > max_pgoff)
  1045. goto fallback;
  1046. /*
  1047. * grab_mapping_entry() will make sure we get a 2M empty entry, a DAX
  1048. * PMD or a HZP entry. If it can't (because a 4k page is already in
  1049. * the tree, for instance), it will return -EEXIST and we just fall
  1050. * back to 4k entries.
  1051. */
  1052. entry = grab_mapping_entry(mapping, pgoff, RADIX_DAX_PMD);
  1053. if (IS_ERR(entry))
  1054. goto fallback;
  1055. /*
  1056. * Note that we don't use iomap_apply here. We aren't doing I/O, only
  1057. * setting up a mapping, so really we're using iomap_begin() as a way
  1058. * to look up our filesystem block.
  1059. */
  1060. pos = (loff_t)pgoff << PAGE_SHIFT;
  1061. error = ops->iomap_begin(inode, pos, PMD_SIZE, iomap_flags, &iomap);
  1062. if (error)
  1063. goto unlock_entry;
  1064. if (iomap.offset + iomap.length < pos + PMD_SIZE)
  1065. goto finish_iomap;
  1066. vmf.pgoff = pgoff;
  1067. vmf.flags = flags;
  1068. vmf.gfp_mask = mapping_gfp_mask(mapping) | __GFP_IO;
  1069. switch (iomap.type) {
  1070. case IOMAP_MAPPED:
  1071. result = dax_pmd_insert_mapping(vma, pmd, &vmf, address,
  1072. &iomap, pos, write, &entry);
  1073. break;
  1074. case IOMAP_UNWRITTEN:
  1075. case IOMAP_HOLE:
  1076. if (WARN_ON_ONCE(write))
  1077. goto finish_iomap;
  1078. result = dax_pmd_load_hole(vma, pmd, &vmf, address, &iomap,
  1079. &entry);
  1080. break;
  1081. default:
  1082. WARN_ON_ONCE(1);
  1083. break;
  1084. }
  1085. finish_iomap:
  1086. if (ops->iomap_end) {
  1087. if (result == VM_FAULT_FALLBACK) {
  1088. ops->iomap_end(inode, pos, PMD_SIZE, 0, iomap_flags,
  1089. &iomap);
  1090. } else {
  1091. error = ops->iomap_end(inode, pos, PMD_SIZE, PMD_SIZE,
  1092. iomap_flags, &iomap);
  1093. if (error)
  1094. result = VM_FAULT_FALLBACK;
  1095. }
  1096. }
  1097. unlock_entry:
  1098. put_locked_mapping_entry(mapping, pgoff, entry);
  1099. fallback:
  1100. if (result == VM_FAULT_FALLBACK) {
  1101. split_huge_pmd(vma, pmd, address);
  1102. count_vm_event(THP_FAULT_FALLBACK);
  1103. }
  1104. return result;
  1105. }
  1106. EXPORT_SYMBOL_GPL(dax_iomap_pmd_fault);
  1107. #endif /* CONFIG_FS_DAX_PMD */
  1108. #endif /* CONFIG_FS_IOMAP */