dax.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476
  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. /*
  36. * We use lowest available bit in exceptional entry for locking, other two
  37. * bits to determine entry type. In total 3 special bits.
  38. */
  39. #define RADIX_DAX_SHIFT (RADIX_TREE_EXCEPTIONAL_SHIFT + 3)
  40. #define RADIX_DAX_PTE (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 1))
  41. #define RADIX_DAX_PMD (1 << (RADIX_TREE_EXCEPTIONAL_SHIFT + 2))
  42. #define RADIX_DAX_TYPE_MASK (RADIX_DAX_PTE | RADIX_DAX_PMD)
  43. #define RADIX_DAX_TYPE(entry) ((unsigned long)entry & RADIX_DAX_TYPE_MASK)
  44. #define RADIX_DAX_SECTOR(entry) (((unsigned long)entry >> RADIX_DAX_SHIFT))
  45. #define RADIX_DAX_ENTRY(sector, pmd) ((void *)((unsigned long)sector << \
  46. RADIX_DAX_SHIFT | (pmd ? RADIX_DAX_PMD : RADIX_DAX_PTE) | \
  47. RADIX_TREE_EXCEPTIONAL_ENTRY))
  48. /* We choose 4096 entries - same as per-zone page wait tables */
  49. #define DAX_WAIT_TABLE_BITS 12
  50. #define DAX_WAIT_TABLE_ENTRIES (1 << DAX_WAIT_TABLE_BITS)
  51. wait_queue_head_t wait_table[DAX_WAIT_TABLE_ENTRIES];
  52. static int __init init_dax_wait_table(void)
  53. {
  54. int i;
  55. for (i = 0; i < DAX_WAIT_TABLE_ENTRIES; i++)
  56. init_waitqueue_head(wait_table + i);
  57. return 0;
  58. }
  59. fs_initcall(init_dax_wait_table);
  60. static wait_queue_head_t *dax_entry_waitqueue(struct address_space *mapping,
  61. pgoff_t index)
  62. {
  63. unsigned long hash = hash_long((unsigned long)mapping ^ index,
  64. DAX_WAIT_TABLE_BITS);
  65. return wait_table + hash;
  66. }
  67. static long dax_map_atomic(struct block_device *bdev, struct blk_dax_ctl *dax)
  68. {
  69. struct request_queue *q = bdev->bd_queue;
  70. long rc = -EIO;
  71. dax->addr = ERR_PTR(-EIO);
  72. if (blk_queue_enter(q, true) != 0)
  73. return rc;
  74. rc = bdev_direct_access(bdev, dax);
  75. if (rc < 0) {
  76. dax->addr = ERR_PTR(rc);
  77. blk_queue_exit(q);
  78. return rc;
  79. }
  80. return rc;
  81. }
  82. static void dax_unmap_atomic(struct block_device *bdev,
  83. const struct blk_dax_ctl *dax)
  84. {
  85. if (IS_ERR(dax->addr))
  86. return;
  87. blk_queue_exit(bdev->bd_queue);
  88. }
  89. struct page *read_dax_sector(struct block_device *bdev, sector_t n)
  90. {
  91. struct page *page = alloc_pages(GFP_KERNEL, 0);
  92. struct blk_dax_ctl dax = {
  93. .size = PAGE_SIZE,
  94. .sector = n & ~((((int) PAGE_SIZE) / 512) - 1),
  95. };
  96. long rc;
  97. if (!page)
  98. return ERR_PTR(-ENOMEM);
  99. rc = dax_map_atomic(bdev, &dax);
  100. if (rc < 0)
  101. return ERR_PTR(rc);
  102. memcpy_from_pmem(page_address(page), dax.addr, PAGE_SIZE);
  103. dax_unmap_atomic(bdev, &dax);
  104. return page;
  105. }
  106. static bool buffer_written(struct buffer_head *bh)
  107. {
  108. return buffer_mapped(bh) && !buffer_unwritten(bh);
  109. }
  110. /*
  111. * When ext4 encounters a hole, it returns without modifying the buffer_head
  112. * which means that we can't trust b_size. To cope with this, we set b_state
  113. * to 0 before calling get_block and, if any bit is set, we know we can trust
  114. * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is
  115. * and would save us time calling get_block repeatedly.
  116. */
  117. static bool buffer_size_valid(struct buffer_head *bh)
  118. {
  119. return bh->b_state != 0;
  120. }
  121. static sector_t to_sector(const struct buffer_head *bh,
  122. const struct inode *inode)
  123. {
  124. sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
  125. return sector;
  126. }
  127. static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
  128. loff_t start, loff_t end, get_block_t get_block,
  129. struct buffer_head *bh)
  130. {
  131. loff_t pos = start, max = start, bh_max = start;
  132. bool hole = false;
  133. struct block_device *bdev = NULL;
  134. int rw = iov_iter_rw(iter), rc;
  135. long map_len = 0;
  136. struct blk_dax_ctl dax = {
  137. .addr = ERR_PTR(-EIO),
  138. };
  139. unsigned blkbits = inode->i_blkbits;
  140. sector_t file_blks = (i_size_read(inode) + (1 << blkbits) - 1)
  141. >> blkbits;
  142. if (rw == READ)
  143. end = min(end, i_size_read(inode));
  144. while (pos < end) {
  145. size_t len;
  146. if (pos == max) {
  147. long page = pos >> PAGE_SHIFT;
  148. sector_t block = page << (PAGE_SHIFT - blkbits);
  149. unsigned first = pos - (block << blkbits);
  150. long size;
  151. if (pos == bh_max) {
  152. bh->b_size = PAGE_ALIGN(end - pos);
  153. bh->b_state = 0;
  154. rc = get_block(inode, block, bh, rw == WRITE);
  155. if (rc)
  156. break;
  157. if (!buffer_size_valid(bh))
  158. bh->b_size = 1 << blkbits;
  159. bh_max = pos - first + bh->b_size;
  160. bdev = bh->b_bdev;
  161. /*
  162. * We allow uninitialized buffers for writes
  163. * beyond EOF as those cannot race with faults
  164. */
  165. WARN_ON_ONCE(
  166. (buffer_new(bh) && block < file_blks) ||
  167. (rw == WRITE && buffer_unwritten(bh)));
  168. } else {
  169. unsigned done = bh->b_size -
  170. (bh_max - (pos - first));
  171. bh->b_blocknr += done >> blkbits;
  172. bh->b_size -= done;
  173. }
  174. hole = rw == READ && !buffer_written(bh);
  175. if (hole) {
  176. size = bh->b_size - first;
  177. } else {
  178. dax_unmap_atomic(bdev, &dax);
  179. dax.sector = to_sector(bh, inode);
  180. dax.size = bh->b_size;
  181. map_len = dax_map_atomic(bdev, &dax);
  182. if (map_len < 0) {
  183. rc = map_len;
  184. break;
  185. }
  186. dax.addr += first;
  187. size = map_len - first;
  188. }
  189. /*
  190. * pos + size is one past the last offset for IO,
  191. * so pos + size can overflow loff_t at extreme offsets.
  192. * Cast to u64 to catch this and get the true minimum.
  193. */
  194. max = min_t(u64, pos + size, end);
  195. }
  196. if (iov_iter_rw(iter) == WRITE) {
  197. len = copy_from_iter_pmem(dax.addr, max - pos, iter);
  198. } else if (!hole)
  199. len = copy_to_iter((void __force *) dax.addr, max - pos,
  200. iter);
  201. else
  202. len = iov_iter_zero(max - pos, iter);
  203. if (!len) {
  204. rc = -EFAULT;
  205. break;
  206. }
  207. pos += len;
  208. if (!IS_ERR(dax.addr))
  209. dax.addr += len;
  210. }
  211. dax_unmap_atomic(bdev, &dax);
  212. return (pos == start) ? rc : pos - start;
  213. }
  214. /**
  215. * dax_do_io - Perform I/O to a DAX file
  216. * @iocb: The control block for this I/O
  217. * @inode: The file which the I/O is directed at
  218. * @iter: The addresses to do I/O from or to
  219. * @get_block: The filesystem method used to translate file offsets to blocks
  220. * @end_io: A filesystem callback for I/O completion
  221. * @flags: See below
  222. *
  223. * This function uses the same locking scheme as do_blockdev_direct_IO:
  224. * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
  225. * caller for writes. For reads, we take and release the i_mutex ourselves.
  226. * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
  227. * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
  228. * is in progress.
  229. */
  230. ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
  231. struct iov_iter *iter, get_block_t get_block,
  232. dio_iodone_t end_io, int flags)
  233. {
  234. struct buffer_head bh;
  235. ssize_t retval = -EINVAL;
  236. loff_t pos = iocb->ki_pos;
  237. loff_t end = pos + iov_iter_count(iter);
  238. memset(&bh, 0, sizeof(bh));
  239. bh.b_bdev = inode->i_sb->s_bdev;
  240. if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
  241. inode_lock(inode);
  242. /* Protects against truncate */
  243. if (!(flags & DIO_SKIP_DIO_COUNT))
  244. inode_dio_begin(inode);
  245. retval = dax_io(inode, iter, pos, end, get_block, &bh);
  246. if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
  247. inode_unlock(inode);
  248. if (end_io) {
  249. int err;
  250. err = end_io(iocb, pos, retval, bh.b_private);
  251. if (err)
  252. retval = err;
  253. }
  254. if (!(flags & DIO_SKIP_DIO_COUNT))
  255. inode_dio_end(inode);
  256. return retval;
  257. }
  258. EXPORT_SYMBOL_GPL(dax_do_io);
  259. /*
  260. * DAX radix tree locking
  261. */
  262. struct exceptional_entry_key {
  263. struct address_space *mapping;
  264. unsigned long index;
  265. };
  266. struct wait_exceptional_entry_queue {
  267. wait_queue_t wait;
  268. struct exceptional_entry_key key;
  269. };
  270. static int wake_exceptional_entry_func(wait_queue_t *wait, unsigned int mode,
  271. int sync, void *keyp)
  272. {
  273. struct exceptional_entry_key *key = keyp;
  274. struct wait_exceptional_entry_queue *ewait =
  275. container_of(wait, struct wait_exceptional_entry_queue, wait);
  276. if (key->mapping != ewait->key.mapping ||
  277. key->index != ewait->key.index)
  278. return 0;
  279. return autoremove_wake_function(wait, mode, sync, NULL);
  280. }
  281. /*
  282. * Check whether the given slot is locked. The function must be called with
  283. * mapping->tree_lock held
  284. */
  285. static inline int slot_locked(struct address_space *mapping, void **slot)
  286. {
  287. unsigned long entry = (unsigned long)
  288. radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
  289. return entry & RADIX_DAX_ENTRY_LOCK;
  290. }
  291. /*
  292. * Mark the given slot is locked. The function must be called with
  293. * mapping->tree_lock held
  294. */
  295. static inline void *lock_slot(struct address_space *mapping, void **slot)
  296. {
  297. unsigned long entry = (unsigned long)
  298. radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
  299. entry |= RADIX_DAX_ENTRY_LOCK;
  300. radix_tree_replace_slot(slot, (void *)entry);
  301. return (void *)entry;
  302. }
  303. /*
  304. * Mark the given slot is unlocked. The function must be called with
  305. * mapping->tree_lock held
  306. */
  307. static inline void *unlock_slot(struct address_space *mapping, void **slot)
  308. {
  309. unsigned long entry = (unsigned long)
  310. radix_tree_deref_slot_protected(slot, &mapping->tree_lock);
  311. entry &= ~(unsigned long)RADIX_DAX_ENTRY_LOCK;
  312. radix_tree_replace_slot(slot, (void *)entry);
  313. return (void *)entry;
  314. }
  315. /*
  316. * Lookup entry in radix tree, wait for it to become unlocked if it is
  317. * exceptional entry and return it. The caller must call
  318. * put_unlocked_mapping_entry() when he decided not to lock the entry or
  319. * put_locked_mapping_entry() when he locked the entry and now wants to
  320. * unlock it.
  321. *
  322. * The function must be called with mapping->tree_lock held.
  323. */
  324. static void *get_unlocked_mapping_entry(struct address_space *mapping,
  325. pgoff_t index, void ***slotp)
  326. {
  327. void *ret, **slot;
  328. struct wait_exceptional_entry_queue ewait;
  329. wait_queue_head_t *wq = dax_entry_waitqueue(mapping, index);
  330. init_wait(&ewait.wait);
  331. ewait.wait.func = wake_exceptional_entry_func;
  332. ewait.key.mapping = mapping;
  333. ewait.key.index = index;
  334. for (;;) {
  335. ret = __radix_tree_lookup(&mapping->page_tree, index, NULL,
  336. &slot);
  337. if (!ret || !radix_tree_exceptional_entry(ret) ||
  338. !slot_locked(mapping, slot)) {
  339. if (slotp)
  340. *slotp = slot;
  341. return ret;
  342. }
  343. prepare_to_wait_exclusive(wq, &ewait.wait,
  344. TASK_UNINTERRUPTIBLE);
  345. spin_unlock_irq(&mapping->tree_lock);
  346. schedule();
  347. finish_wait(wq, &ewait.wait);
  348. spin_lock_irq(&mapping->tree_lock);
  349. }
  350. }
  351. /*
  352. * Find radix tree entry at given index. If it points to a page, return with
  353. * the page locked. If it points to the exceptional entry, return with the
  354. * radix tree entry locked. If the radix tree doesn't contain given index,
  355. * create empty exceptional entry for the index and return with it locked.
  356. *
  357. * Note: Unlike filemap_fault() we don't honor FAULT_FLAG_RETRY flags. For
  358. * persistent memory the benefit is doubtful. We can add that later if we can
  359. * show it helps.
  360. */
  361. static void *grab_mapping_entry(struct address_space *mapping, pgoff_t index)
  362. {
  363. void *ret, **slot;
  364. restart:
  365. spin_lock_irq(&mapping->tree_lock);
  366. ret = get_unlocked_mapping_entry(mapping, index, &slot);
  367. /* No entry for given index? Make sure radix tree is big enough. */
  368. if (!ret) {
  369. int err;
  370. spin_unlock_irq(&mapping->tree_lock);
  371. err = radix_tree_preload(
  372. mapping_gfp_mask(mapping) & ~__GFP_HIGHMEM);
  373. if (err)
  374. return ERR_PTR(err);
  375. ret = (void *)(RADIX_TREE_EXCEPTIONAL_ENTRY |
  376. RADIX_DAX_ENTRY_LOCK);
  377. spin_lock_irq(&mapping->tree_lock);
  378. err = radix_tree_insert(&mapping->page_tree, index, ret);
  379. radix_tree_preload_end();
  380. if (err) {
  381. spin_unlock_irq(&mapping->tree_lock);
  382. /* Someone already created the entry? */
  383. if (err == -EEXIST)
  384. goto restart;
  385. return ERR_PTR(err);
  386. }
  387. /* Good, we have inserted empty locked entry into the tree. */
  388. mapping->nrexceptional++;
  389. spin_unlock_irq(&mapping->tree_lock);
  390. return ret;
  391. }
  392. /* Normal page in radix tree? */
  393. if (!radix_tree_exceptional_entry(ret)) {
  394. struct page *page = ret;
  395. get_page(page);
  396. spin_unlock_irq(&mapping->tree_lock);
  397. lock_page(page);
  398. /* Page got truncated? Retry... */
  399. if (unlikely(page->mapping != mapping)) {
  400. unlock_page(page);
  401. put_page(page);
  402. goto restart;
  403. }
  404. return page;
  405. }
  406. ret = lock_slot(mapping, slot);
  407. spin_unlock_irq(&mapping->tree_lock);
  408. return ret;
  409. }
  410. void dax_wake_mapping_entry_waiter(struct address_space *mapping,
  411. pgoff_t index, bool wake_all)
  412. {
  413. wait_queue_head_t *wq = dax_entry_waitqueue(mapping, index);
  414. /*
  415. * Checking for locked entry and prepare_to_wait_exclusive() happens
  416. * under mapping->tree_lock, ditto for entry handling in our callers.
  417. * So at this point all tasks that could have seen our entry locked
  418. * must be in the waitqueue and the following check will see them.
  419. */
  420. if (waitqueue_active(wq)) {
  421. struct exceptional_entry_key key;
  422. key.mapping = mapping;
  423. key.index = index;
  424. __wake_up(wq, TASK_NORMAL, wake_all ? 0 : 1, &key);
  425. }
  426. }
  427. void dax_unlock_mapping_entry(struct address_space *mapping, pgoff_t index)
  428. {
  429. void *ret, **slot;
  430. spin_lock_irq(&mapping->tree_lock);
  431. ret = __radix_tree_lookup(&mapping->page_tree, index, NULL, &slot);
  432. if (WARN_ON_ONCE(!ret || !radix_tree_exceptional_entry(ret) ||
  433. !slot_locked(mapping, slot))) {
  434. spin_unlock_irq(&mapping->tree_lock);
  435. return;
  436. }
  437. unlock_slot(mapping, slot);
  438. spin_unlock_irq(&mapping->tree_lock);
  439. dax_wake_mapping_entry_waiter(mapping, index, false);
  440. }
  441. static void put_locked_mapping_entry(struct address_space *mapping,
  442. pgoff_t index, void *entry)
  443. {
  444. if (!radix_tree_exceptional_entry(entry)) {
  445. unlock_page(entry);
  446. put_page(entry);
  447. } else {
  448. dax_unlock_mapping_entry(mapping, index);
  449. }
  450. }
  451. /*
  452. * Called when we are done with radix tree entry we looked up via
  453. * get_unlocked_mapping_entry() and which we didn't lock in the end.
  454. */
  455. static void put_unlocked_mapping_entry(struct address_space *mapping,
  456. pgoff_t index, void *entry)
  457. {
  458. if (!radix_tree_exceptional_entry(entry))
  459. return;
  460. /* We have to wake up next waiter for the radix tree entry lock */
  461. dax_wake_mapping_entry_waiter(mapping, index, false);
  462. }
  463. /*
  464. * Delete exceptional DAX entry at @index from @mapping. Wait for radix tree
  465. * entry to get unlocked before deleting it.
  466. */
  467. int dax_delete_mapping_entry(struct address_space *mapping, pgoff_t index)
  468. {
  469. void *entry;
  470. spin_lock_irq(&mapping->tree_lock);
  471. entry = get_unlocked_mapping_entry(mapping, index, NULL);
  472. /*
  473. * This gets called from truncate / punch_hole path. As such, the caller
  474. * must hold locks protecting against concurrent modifications of the
  475. * radix tree (usually fs-private i_mmap_sem for writing). Since the
  476. * caller has seen exceptional entry for this index, we better find it
  477. * at that index as well...
  478. */
  479. if (WARN_ON_ONCE(!entry || !radix_tree_exceptional_entry(entry))) {
  480. spin_unlock_irq(&mapping->tree_lock);
  481. return 0;
  482. }
  483. radix_tree_delete(&mapping->page_tree, index);
  484. mapping->nrexceptional--;
  485. spin_unlock_irq(&mapping->tree_lock);
  486. dax_wake_mapping_entry_waiter(mapping, index, true);
  487. return 1;
  488. }
  489. /*
  490. * The user has performed a load from a hole in the file. Allocating
  491. * a new page in the file would cause excessive storage usage for
  492. * workloads with sparse files. We allocate a page cache page instead.
  493. * We'll kick it out of the page cache if it's ever written to,
  494. * otherwise it will simply fall out of the page cache under memory
  495. * pressure without ever having been dirtied.
  496. */
  497. static int dax_load_hole(struct address_space *mapping, void *entry,
  498. struct vm_fault *vmf)
  499. {
  500. struct page *page;
  501. /* Hole page already exists? Return it... */
  502. if (!radix_tree_exceptional_entry(entry)) {
  503. vmf->page = entry;
  504. return VM_FAULT_LOCKED;
  505. }
  506. /* This will replace locked radix tree entry with a hole page */
  507. page = find_or_create_page(mapping, vmf->pgoff,
  508. vmf->gfp_mask | __GFP_ZERO);
  509. if (!page) {
  510. put_locked_mapping_entry(mapping, vmf->pgoff, entry);
  511. return VM_FAULT_OOM;
  512. }
  513. vmf->page = page;
  514. return VM_FAULT_LOCKED;
  515. }
  516. static int copy_user_dax(struct block_device *bdev, sector_t sector, size_t size,
  517. struct page *to, unsigned long vaddr)
  518. {
  519. struct blk_dax_ctl dax = {
  520. .sector = sector,
  521. .size = size,
  522. };
  523. void *vto;
  524. if (dax_map_atomic(bdev, &dax) < 0)
  525. return PTR_ERR(dax.addr);
  526. vto = kmap_atomic(to);
  527. copy_user_page(vto, (void __force *)dax.addr, vaddr, to);
  528. kunmap_atomic(vto);
  529. dax_unmap_atomic(bdev, &dax);
  530. return 0;
  531. }
  532. #define DAX_PMD_INDEX(page_index) (page_index & (PMD_MASK >> PAGE_SHIFT))
  533. static void *dax_insert_mapping_entry(struct address_space *mapping,
  534. struct vm_fault *vmf,
  535. void *entry, sector_t sector)
  536. {
  537. struct radix_tree_root *page_tree = &mapping->page_tree;
  538. int error = 0;
  539. bool hole_fill = false;
  540. void *new_entry;
  541. pgoff_t index = vmf->pgoff;
  542. if (vmf->flags & FAULT_FLAG_WRITE)
  543. __mark_inode_dirty(mapping->host, I_DIRTY_PAGES);
  544. /* Replacing hole page with block mapping? */
  545. if (!radix_tree_exceptional_entry(entry)) {
  546. hole_fill = true;
  547. /*
  548. * Unmap the page now before we remove it from page cache below.
  549. * The page is locked so it cannot be faulted in again.
  550. */
  551. unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
  552. PAGE_SIZE, 0);
  553. error = radix_tree_preload(vmf->gfp_mask & ~__GFP_HIGHMEM);
  554. if (error)
  555. return ERR_PTR(error);
  556. }
  557. spin_lock_irq(&mapping->tree_lock);
  558. new_entry = (void *)((unsigned long)RADIX_DAX_ENTRY(sector, false) |
  559. RADIX_DAX_ENTRY_LOCK);
  560. if (hole_fill) {
  561. __delete_from_page_cache(entry, NULL);
  562. /* Drop pagecache reference */
  563. put_page(entry);
  564. error = radix_tree_insert(page_tree, index, new_entry);
  565. if (error) {
  566. new_entry = ERR_PTR(error);
  567. goto unlock;
  568. }
  569. mapping->nrexceptional++;
  570. } else {
  571. void **slot;
  572. void *ret;
  573. ret = __radix_tree_lookup(page_tree, index, NULL, &slot);
  574. WARN_ON_ONCE(ret != entry);
  575. radix_tree_replace_slot(slot, new_entry);
  576. }
  577. if (vmf->flags & FAULT_FLAG_WRITE)
  578. radix_tree_tag_set(page_tree, index, PAGECACHE_TAG_DIRTY);
  579. unlock:
  580. spin_unlock_irq(&mapping->tree_lock);
  581. if (hole_fill) {
  582. radix_tree_preload_end();
  583. /*
  584. * We don't need hole page anymore, it has been replaced with
  585. * locked radix tree entry now.
  586. */
  587. if (mapping->a_ops->freepage)
  588. mapping->a_ops->freepage(entry);
  589. unlock_page(entry);
  590. put_page(entry);
  591. }
  592. return new_entry;
  593. }
  594. static int dax_writeback_one(struct block_device *bdev,
  595. struct address_space *mapping, pgoff_t index, void *entry)
  596. {
  597. struct radix_tree_root *page_tree = &mapping->page_tree;
  598. int type = RADIX_DAX_TYPE(entry);
  599. struct radix_tree_node *node;
  600. struct blk_dax_ctl dax;
  601. void **slot;
  602. int ret = 0;
  603. spin_lock_irq(&mapping->tree_lock);
  604. /*
  605. * Regular page slots are stabilized by the page lock even
  606. * without the tree itself locked. These unlocked entries
  607. * need verification under the tree lock.
  608. */
  609. if (!__radix_tree_lookup(page_tree, index, &node, &slot))
  610. goto unlock;
  611. if (*slot != entry)
  612. goto unlock;
  613. /* another fsync thread may have already written back this entry */
  614. if (!radix_tree_tag_get(page_tree, index, PAGECACHE_TAG_TOWRITE))
  615. goto unlock;
  616. if (WARN_ON_ONCE(type != RADIX_DAX_PTE && type != RADIX_DAX_PMD)) {
  617. ret = -EIO;
  618. goto unlock;
  619. }
  620. dax.sector = RADIX_DAX_SECTOR(entry);
  621. dax.size = (type == RADIX_DAX_PMD ? PMD_SIZE : PAGE_SIZE);
  622. spin_unlock_irq(&mapping->tree_lock);
  623. /*
  624. * We cannot hold tree_lock while calling dax_map_atomic() because it
  625. * eventually calls cond_resched().
  626. */
  627. ret = dax_map_atomic(bdev, &dax);
  628. if (ret < 0)
  629. return ret;
  630. if (WARN_ON_ONCE(ret < dax.size)) {
  631. ret = -EIO;
  632. goto unmap;
  633. }
  634. wb_cache_pmem(dax.addr, dax.size);
  635. spin_lock_irq(&mapping->tree_lock);
  636. radix_tree_tag_clear(page_tree, index, PAGECACHE_TAG_TOWRITE);
  637. spin_unlock_irq(&mapping->tree_lock);
  638. unmap:
  639. dax_unmap_atomic(bdev, &dax);
  640. return ret;
  641. unlock:
  642. spin_unlock_irq(&mapping->tree_lock);
  643. return ret;
  644. }
  645. /*
  646. * Flush the mapping to the persistent domain within the byte range of [start,
  647. * end]. This is required by data integrity operations to ensure file data is
  648. * on persistent storage prior to completion of the operation.
  649. */
  650. int dax_writeback_mapping_range(struct address_space *mapping,
  651. struct block_device *bdev, struct writeback_control *wbc)
  652. {
  653. struct inode *inode = mapping->host;
  654. pgoff_t start_index, end_index, pmd_index;
  655. pgoff_t indices[PAGEVEC_SIZE];
  656. struct pagevec pvec;
  657. bool done = false;
  658. int i, ret = 0;
  659. void *entry;
  660. if (WARN_ON_ONCE(inode->i_blkbits != PAGE_SHIFT))
  661. return -EIO;
  662. if (!mapping->nrexceptional || wbc->sync_mode != WB_SYNC_ALL)
  663. return 0;
  664. start_index = wbc->range_start >> PAGE_SHIFT;
  665. end_index = wbc->range_end >> PAGE_SHIFT;
  666. pmd_index = DAX_PMD_INDEX(start_index);
  667. rcu_read_lock();
  668. entry = radix_tree_lookup(&mapping->page_tree, pmd_index);
  669. rcu_read_unlock();
  670. /* see if the start of our range is covered by a PMD entry */
  671. if (entry && RADIX_DAX_TYPE(entry) == RADIX_DAX_PMD)
  672. start_index = pmd_index;
  673. tag_pages_for_writeback(mapping, start_index, end_index);
  674. pagevec_init(&pvec, 0);
  675. while (!done) {
  676. pvec.nr = find_get_entries_tag(mapping, start_index,
  677. PAGECACHE_TAG_TOWRITE, PAGEVEC_SIZE,
  678. pvec.pages, indices);
  679. if (pvec.nr == 0)
  680. break;
  681. for (i = 0; i < pvec.nr; i++) {
  682. if (indices[i] > end_index) {
  683. done = true;
  684. break;
  685. }
  686. ret = dax_writeback_one(bdev, mapping, indices[i],
  687. pvec.pages[i]);
  688. if (ret < 0)
  689. return ret;
  690. }
  691. }
  692. return 0;
  693. }
  694. EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
  695. static int dax_insert_mapping(struct address_space *mapping,
  696. struct block_device *bdev, sector_t sector, size_t size,
  697. void **entryp, struct vm_area_struct *vma, struct vm_fault *vmf)
  698. {
  699. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  700. struct blk_dax_ctl dax = {
  701. .sector = sector,
  702. .size = size,
  703. };
  704. void *ret;
  705. void *entry = *entryp;
  706. if (dax_map_atomic(bdev, &dax) < 0)
  707. return PTR_ERR(dax.addr);
  708. dax_unmap_atomic(bdev, &dax);
  709. ret = dax_insert_mapping_entry(mapping, vmf, entry, dax.sector);
  710. if (IS_ERR(ret))
  711. return PTR_ERR(ret);
  712. *entryp = ret;
  713. return vm_insert_mixed(vma, vaddr, dax.pfn);
  714. }
  715. /**
  716. * dax_fault - handle a page fault on a DAX file
  717. * @vma: The virtual memory area where the fault occurred
  718. * @vmf: The description of the fault
  719. * @get_block: The filesystem method used to translate file offsets to blocks
  720. *
  721. * When a page fault occurs, filesystems may call this helper in their
  722. * fault handler for DAX files. dax_fault() assumes the caller has done all
  723. * the necessary locking for the page fault to proceed successfully.
  724. */
  725. int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  726. get_block_t get_block)
  727. {
  728. struct file *file = vma->vm_file;
  729. struct address_space *mapping = file->f_mapping;
  730. struct inode *inode = mapping->host;
  731. void *entry;
  732. struct buffer_head bh;
  733. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  734. unsigned blkbits = inode->i_blkbits;
  735. sector_t block;
  736. pgoff_t size;
  737. int error;
  738. int major = 0;
  739. /*
  740. * Check whether offset isn't beyond end of file now. Caller is supposed
  741. * to hold locks serializing us with truncate / punch hole so this is
  742. * a reliable test.
  743. */
  744. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  745. if (vmf->pgoff >= size)
  746. return VM_FAULT_SIGBUS;
  747. memset(&bh, 0, sizeof(bh));
  748. block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
  749. bh.b_bdev = inode->i_sb->s_bdev;
  750. bh.b_size = PAGE_SIZE;
  751. entry = grab_mapping_entry(mapping, vmf->pgoff);
  752. if (IS_ERR(entry)) {
  753. error = PTR_ERR(entry);
  754. goto out;
  755. }
  756. error = get_block(inode, block, &bh, 0);
  757. if (!error && (bh.b_size < PAGE_SIZE))
  758. error = -EIO; /* fs corruption? */
  759. if (error)
  760. goto unlock_entry;
  761. if (vmf->cow_page) {
  762. struct page *new_page = vmf->cow_page;
  763. if (buffer_written(&bh))
  764. error = copy_user_dax(bh.b_bdev, to_sector(&bh, inode),
  765. bh.b_size, new_page, vaddr);
  766. else
  767. clear_user_highpage(new_page, vaddr);
  768. if (error)
  769. goto unlock_entry;
  770. if (!radix_tree_exceptional_entry(entry)) {
  771. vmf->page = entry;
  772. return VM_FAULT_LOCKED;
  773. }
  774. vmf->entry = entry;
  775. return VM_FAULT_DAX_LOCKED;
  776. }
  777. if (!buffer_mapped(&bh)) {
  778. if (vmf->flags & FAULT_FLAG_WRITE) {
  779. error = get_block(inode, block, &bh, 1);
  780. count_vm_event(PGMAJFAULT);
  781. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  782. major = VM_FAULT_MAJOR;
  783. if (!error && (bh.b_size < PAGE_SIZE))
  784. error = -EIO;
  785. if (error)
  786. goto unlock_entry;
  787. } else {
  788. return dax_load_hole(mapping, entry, vmf);
  789. }
  790. }
  791. /* Filesystem should not return unwritten buffers to us! */
  792. WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
  793. error = dax_insert_mapping(mapping, bh.b_bdev, to_sector(&bh, inode),
  794. bh.b_size, &entry, vma, vmf);
  795. unlock_entry:
  796. put_locked_mapping_entry(mapping, vmf->pgoff, entry);
  797. out:
  798. if (error == -ENOMEM)
  799. return VM_FAULT_OOM | major;
  800. /* -EBUSY is fine, somebody else faulted on the same PTE */
  801. if ((error < 0) && (error != -EBUSY))
  802. return VM_FAULT_SIGBUS | major;
  803. return VM_FAULT_NOPAGE | major;
  804. }
  805. EXPORT_SYMBOL_GPL(dax_fault);
  806. #if defined(CONFIG_TRANSPARENT_HUGEPAGE)
  807. /*
  808. * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
  809. * more often than one might expect in the below function.
  810. */
  811. #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
  812. static void __dax_dbg(struct buffer_head *bh, unsigned long address,
  813. const char *reason, const char *fn)
  814. {
  815. if (bh) {
  816. char bname[BDEVNAME_SIZE];
  817. bdevname(bh->b_bdev, bname);
  818. pr_debug("%s: %s addr: %lx dev %s state %lx start %lld "
  819. "length %zd fallback: %s\n", fn, current->comm,
  820. address, bname, bh->b_state, (u64)bh->b_blocknr,
  821. bh->b_size, reason);
  822. } else {
  823. pr_debug("%s: %s addr: %lx fallback: %s\n", fn,
  824. current->comm, address, reason);
  825. }
  826. }
  827. #define dax_pmd_dbg(bh, address, reason) __dax_dbg(bh, address, reason, "dax_pmd")
  828. /**
  829. * dax_pmd_fault - handle a PMD fault on a DAX file
  830. * @vma: The virtual memory area where the fault occurred
  831. * @vmf: The description of the fault
  832. * @get_block: The filesystem method used to translate file offsets to blocks
  833. *
  834. * When a page fault occurs, filesystems may call this helper in their
  835. * pmd_fault handler for DAX files.
  836. */
  837. int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
  838. pmd_t *pmd, unsigned int flags, get_block_t get_block)
  839. {
  840. struct file *file = vma->vm_file;
  841. struct address_space *mapping = file->f_mapping;
  842. struct inode *inode = mapping->host;
  843. struct buffer_head bh;
  844. unsigned blkbits = inode->i_blkbits;
  845. unsigned long pmd_addr = address & PMD_MASK;
  846. bool write = flags & FAULT_FLAG_WRITE;
  847. struct block_device *bdev;
  848. pgoff_t size, pgoff;
  849. sector_t block;
  850. int result = 0;
  851. bool alloc = false;
  852. /* dax pmd mappings require pfn_t_devmap() */
  853. if (!IS_ENABLED(CONFIG_FS_DAX_PMD))
  854. return VM_FAULT_FALLBACK;
  855. /* Fall back to PTEs if we're going to COW */
  856. if (write && !(vma->vm_flags & VM_SHARED)) {
  857. split_huge_pmd(vma, pmd, address);
  858. dax_pmd_dbg(NULL, address, "cow write");
  859. return VM_FAULT_FALLBACK;
  860. }
  861. /* If the PMD would extend outside the VMA */
  862. if (pmd_addr < vma->vm_start) {
  863. dax_pmd_dbg(NULL, address, "vma start unaligned");
  864. return VM_FAULT_FALLBACK;
  865. }
  866. if ((pmd_addr + PMD_SIZE) > vma->vm_end) {
  867. dax_pmd_dbg(NULL, address, "vma end unaligned");
  868. return VM_FAULT_FALLBACK;
  869. }
  870. pgoff = linear_page_index(vma, pmd_addr);
  871. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  872. if (pgoff >= size)
  873. return VM_FAULT_SIGBUS;
  874. /* If the PMD would cover blocks out of the file */
  875. if ((pgoff | PG_PMD_COLOUR) >= size) {
  876. dax_pmd_dbg(NULL, address,
  877. "offset + huge page size > file size");
  878. return VM_FAULT_FALLBACK;
  879. }
  880. memset(&bh, 0, sizeof(bh));
  881. bh.b_bdev = inode->i_sb->s_bdev;
  882. block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
  883. bh.b_size = PMD_SIZE;
  884. if (get_block(inode, block, &bh, 0) != 0)
  885. return VM_FAULT_SIGBUS;
  886. if (!buffer_mapped(&bh) && write) {
  887. if (get_block(inode, block, &bh, 1) != 0)
  888. return VM_FAULT_SIGBUS;
  889. alloc = true;
  890. WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
  891. }
  892. bdev = bh.b_bdev;
  893. /*
  894. * If the filesystem isn't willing to tell us the length of a hole,
  895. * just fall back to PTEs. Calling get_block 512 times in a loop
  896. * would be silly.
  897. */
  898. if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE) {
  899. dax_pmd_dbg(&bh, address, "allocated block too small");
  900. return VM_FAULT_FALLBACK;
  901. }
  902. /*
  903. * If we allocated new storage, make sure no process has any
  904. * zero pages covering this hole
  905. */
  906. if (alloc) {
  907. loff_t lstart = pgoff << PAGE_SHIFT;
  908. loff_t lend = lstart + PMD_SIZE - 1; /* inclusive */
  909. truncate_pagecache_range(inode, lstart, lend);
  910. }
  911. if (!write && !buffer_mapped(&bh)) {
  912. spinlock_t *ptl;
  913. pmd_t entry;
  914. struct page *zero_page = mm_get_huge_zero_page(vma->vm_mm);
  915. if (unlikely(!zero_page)) {
  916. dax_pmd_dbg(&bh, address, "no zero page");
  917. goto fallback;
  918. }
  919. ptl = pmd_lock(vma->vm_mm, pmd);
  920. if (!pmd_none(*pmd)) {
  921. spin_unlock(ptl);
  922. dax_pmd_dbg(&bh, address, "pmd already present");
  923. goto fallback;
  924. }
  925. dev_dbg(part_to_dev(bdev->bd_part),
  926. "%s: %s addr: %lx pfn: <zero> sect: %llx\n",
  927. __func__, current->comm, address,
  928. (unsigned long long) to_sector(&bh, inode));
  929. entry = mk_pmd(zero_page, vma->vm_page_prot);
  930. entry = pmd_mkhuge(entry);
  931. set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
  932. result = VM_FAULT_NOPAGE;
  933. spin_unlock(ptl);
  934. } else {
  935. struct blk_dax_ctl dax = {
  936. .sector = to_sector(&bh, inode),
  937. .size = PMD_SIZE,
  938. };
  939. long length = dax_map_atomic(bdev, &dax);
  940. if (length < 0) {
  941. dax_pmd_dbg(&bh, address, "dax-error fallback");
  942. goto fallback;
  943. }
  944. if (length < PMD_SIZE) {
  945. dax_pmd_dbg(&bh, address, "dax-length too small");
  946. dax_unmap_atomic(bdev, &dax);
  947. goto fallback;
  948. }
  949. if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR) {
  950. dax_pmd_dbg(&bh, address, "pfn unaligned");
  951. dax_unmap_atomic(bdev, &dax);
  952. goto fallback;
  953. }
  954. if (!pfn_t_devmap(dax.pfn)) {
  955. dax_unmap_atomic(bdev, &dax);
  956. dax_pmd_dbg(&bh, address, "pfn not in memmap");
  957. goto fallback;
  958. }
  959. dax_unmap_atomic(bdev, &dax);
  960. /*
  961. * For PTE faults we insert a radix tree entry for reads, and
  962. * leave it clean. Then on the first write we dirty the radix
  963. * tree entry via the dax_pfn_mkwrite() path. This sequence
  964. * allows the dax_pfn_mkwrite() call to be simpler and avoid a
  965. * call into get_block() to translate the pgoff to a sector in
  966. * order to be able to create a new radix tree entry.
  967. *
  968. * The PMD path doesn't have an equivalent to
  969. * dax_pfn_mkwrite(), though, so for a read followed by a
  970. * write we traverse all the way through dax_pmd_fault()
  971. * twice. This means we can just skip inserting a radix tree
  972. * entry completely on the initial read and just wait until
  973. * the write to insert a dirty entry.
  974. */
  975. if (write) {
  976. /*
  977. * We should insert radix-tree entry and dirty it here.
  978. * For now this is broken...
  979. */
  980. }
  981. dev_dbg(part_to_dev(bdev->bd_part),
  982. "%s: %s addr: %lx pfn: %lx sect: %llx\n",
  983. __func__, current->comm, address,
  984. pfn_t_to_pfn(dax.pfn),
  985. (unsigned long long) dax.sector);
  986. result |= vmf_insert_pfn_pmd(vma, address, pmd,
  987. dax.pfn, write);
  988. }
  989. out:
  990. return result;
  991. fallback:
  992. count_vm_event(THP_FAULT_FALLBACK);
  993. result = VM_FAULT_FALLBACK;
  994. goto out;
  995. }
  996. EXPORT_SYMBOL_GPL(dax_pmd_fault);
  997. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  998. /**
  999. * dax_pfn_mkwrite - handle first write to DAX page
  1000. * @vma: The virtual memory area where the fault occurred
  1001. * @vmf: The description of the fault
  1002. */
  1003. int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  1004. {
  1005. struct file *file = vma->vm_file;
  1006. struct address_space *mapping = file->f_mapping;
  1007. void *entry;
  1008. pgoff_t index = vmf->pgoff;
  1009. spin_lock_irq(&mapping->tree_lock);
  1010. entry = get_unlocked_mapping_entry(mapping, index, NULL);
  1011. if (!entry || !radix_tree_exceptional_entry(entry))
  1012. goto out;
  1013. radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY);
  1014. put_unlocked_mapping_entry(mapping, index, entry);
  1015. out:
  1016. spin_unlock_irq(&mapping->tree_lock);
  1017. return VM_FAULT_NOPAGE;
  1018. }
  1019. EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
  1020. static bool dax_range_is_aligned(struct block_device *bdev,
  1021. unsigned int offset, unsigned int length)
  1022. {
  1023. unsigned short sector_size = bdev_logical_block_size(bdev);
  1024. if (!IS_ALIGNED(offset, sector_size))
  1025. return false;
  1026. if (!IS_ALIGNED(length, sector_size))
  1027. return false;
  1028. return true;
  1029. }
  1030. int __dax_zero_page_range(struct block_device *bdev, sector_t sector,
  1031. unsigned int offset, unsigned int length)
  1032. {
  1033. struct blk_dax_ctl dax = {
  1034. .sector = sector,
  1035. .size = PAGE_SIZE,
  1036. };
  1037. if (dax_range_is_aligned(bdev, offset, length)) {
  1038. sector_t start_sector = dax.sector + (offset >> 9);
  1039. return blkdev_issue_zeroout(bdev, start_sector,
  1040. length >> 9, GFP_NOFS, true);
  1041. } else {
  1042. if (dax_map_atomic(bdev, &dax) < 0)
  1043. return PTR_ERR(dax.addr);
  1044. clear_pmem(dax.addr + offset, length);
  1045. dax_unmap_atomic(bdev, &dax);
  1046. }
  1047. return 0;
  1048. }
  1049. EXPORT_SYMBOL_GPL(__dax_zero_page_range);
  1050. /**
  1051. * dax_zero_page_range - zero a range within a page of a DAX file
  1052. * @inode: The file being truncated
  1053. * @from: The file offset that is being truncated to
  1054. * @length: The number of bytes to zero
  1055. * @get_block: The filesystem method used to translate file offsets to blocks
  1056. *
  1057. * This function can be called by a filesystem when it is zeroing part of a
  1058. * page in a DAX file. This is intended for hole-punch operations. If
  1059. * you are truncating a file, the helper function dax_truncate_page() may be
  1060. * more convenient.
  1061. */
  1062. int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
  1063. get_block_t get_block)
  1064. {
  1065. struct buffer_head bh;
  1066. pgoff_t index = from >> PAGE_SHIFT;
  1067. unsigned offset = from & (PAGE_SIZE-1);
  1068. int err;
  1069. /* Block boundary? Nothing to do */
  1070. if (!length)
  1071. return 0;
  1072. BUG_ON((offset + length) > PAGE_SIZE);
  1073. memset(&bh, 0, sizeof(bh));
  1074. bh.b_bdev = inode->i_sb->s_bdev;
  1075. bh.b_size = PAGE_SIZE;
  1076. err = get_block(inode, index, &bh, 0);
  1077. if (err < 0 || !buffer_written(&bh))
  1078. return err;
  1079. return __dax_zero_page_range(bh.b_bdev, to_sector(&bh, inode),
  1080. offset, length);
  1081. }
  1082. EXPORT_SYMBOL_GPL(dax_zero_page_range);
  1083. /**
  1084. * dax_truncate_page - handle a partial page being truncated in a DAX file
  1085. * @inode: The file being truncated
  1086. * @from: The file offset that is being truncated to
  1087. * @get_block: The filesystem method used to translate file offsets to blocks
  1088. *
  1089. * Similar to block_truncate_page(), this function can be called by a
  1090. * filesystem when it is truncating a DAX file to handle the partial page.
  1091. */
  1092. int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
  1093. {
  1094. unsigned length = PAGE_ALIGN(from) - from;
  1095. return dax_zero_page_range(inode, from, length, get_block);
  1096. }
  1097. EXPORT_SYMBOL_GPL(dax_truncate_page);
  1098. #ifdef CONFIG_FS_IOMAP
  1099. static loff_t
  1100. iomap_dax_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  1101. struct iomap *iomap)
  1102. {
  1103. struct iov_iter *iter = data;
  1104. loff_t end = pos + length, done = 0;
  1105. ssize_t ret = 0;
  1106. if (iov_iter_rw(iter) == READ) {
  1107. end = min(end, i_size_read(inode));
  1108. if (pos >= end)
  1109. return 0;
  1110. if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
  1111. return iov_iter_zero(min(length, end - pos), iter);
  1112. }
  1113. if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
  1114. return -EIO;
  1115. while (pos < end) {
  1116. unsigned offset = pos & (PAGE_SIZE - 1);
  1117. struct blk_dax_ctl dax = { 0 };
  1118. ssize_t map_len;
  1119. if (fatal_signal_pending(current)) {
  1120. ret = -EINTR;
  1121. break;
  1122. }
  1123. dax.sector = iomap->blkno +
  1124. (((pos & PAGE_MASK) - iomap->offset) >> 9);
  1125. dax.size = (length + offset + PAGE_SIZE - 1) & PAGE_MASK;
  1126. map_len = dax_map_atomic(iomap->bdev, &dax);
  1127. if (map_len < 0) {
  1128. ret = map_len;
  1129. break;
  1130. }
  1131. dax.addr += offset;
  1132. map_len -= offset;
  1133. if (map_len > end - pos)
  1134. map_len = end - pos;
  1135. if (iov_iter_rw(iter) == WRITE)
  1136. map_len = copy_from_iter_pmem(dax.addr, map_len, iter);
  1137. else
  1138. map_len = copy_to_iter(dax.addr, map_len, iter);
  1139. dax_unmap_atomic(iomap->bdev, &dax);
  1140. if (map_len <= 0) {
  1141. ret = map_len ? map_len : -EFAULT;
  1142. break;
  1143. }
  1144. pos += map_len;
  1145. length -= map_len;
  1146. done += map_len;
  1147. }
  1148. return done ? done : ret;
  1149. }
  1150. /**
  1151. * iomap_dax_rw - Perform I/O to a DAX file
  1152. * @iocb: The control block for this I/O
  1153. * @iter: The addresses to do I/O from or to
  1154. * @ops: iomap ops passed from the file system
  1155. *
  1156. * This function performs read and write operations to directly mapped
  1157. * persistent memory. The callers needs to take care of read/write exclusion
  1158. * and evicting any page cache pages in the region under I/O.
  1159. */
  1160. ssize_t
  1161. iomap_dax_rw(struct kiocb *iocb, struct iov_iter *iter,
  1162. struct iomap_ops *ops)
  1163. {
  1164. struct address_space *mapping = iocb->ki_filp->f_mapping;
  1165. struct inode *inode = mapping->host;
  1166. loff_t pos = iocb->ki_pos, ret = 0, done = 0;
  1167. unsigned flags = 0;
  1168. if (iov_iter_rw(iter) == WRITE)
  1169. flags |= IOMAP_WRITE;
  1170. /*
  1171. * Yes, even DAX files can have page cache attached to them: A zeroed
  1172. * page is inserted into the pagecache when we have to serve a write
  1173. * fault on a hole. It should never be dirtied and can simply be
  1174. * dropped from the pagecache once we get real data for the page.
  1175. *
  1176. * XXX: This is racy against mmap, and there's nothing we can do about
  1177. * it. We'll eventually need to shift this down even further so that
  1178. * we can check if we allocated blocks over a hole first.
  1179. */
  1180. if (mapping->nrpages) {
  1181. ret = invalidate_inode_pages2_range(mapping,
  1182. pos >> PAGE_SHIFT,
  1183. (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT);
  1184. WARN_ON_ONCE(ret);
  1185. }
  1186. while (iov_iter_count(iter)) {
  1187. ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
  1188. iter, iomap_dax_actor);
  1189. if (ret <= 0)
  1190. break;
  1191. pos += ret;
  1192. done += ret;
  1193. }
  1194. iocb->ki_pos += done;
  1195. return done ? done : ret;
  1196. }
  1197. EXPORT_SYMBOL_GPL(iomap_dax_rw);
  1198. /**
  1199. * iomap_dax_fault - handle a page fault on a DAX file
  1200. * @vma: The virtual memory area where the fault occurred
  1201. * @vmf: The description of the fault
  1202. * @ops: iomap ops passed from the file system
  1203. *
  1204. * When a page fault occurs, filesystems may call this helper in their fault
  1205. * or mkwrite handler for DAX files. Assumes the caller has done all the
  1206. * necessary locking for the page fault to proceed successfully.
  1207. */
  1208. int iomap_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  1209. struct iomap_ops *ops)
  1210. {
  1211. struct address_space *mapping = vma->vm_file->f_mapping;
  1212. struct inode *inode = mapping->host;
  1213. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  1214. loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
  1215. sector_t sector;
  1216. struct iomap iomap = { 0 };
  1217. unsigned flags = 0;
  1218. int error, major = 0;
  1219. void *entry;
  1220. /*
  1221. * Check whether offset isn't beyond end of file now. Caller is supposed
  1222. * to hold locks serializing us with truncate / punch hole so this is
  1223. * a reliable test.
  1224. */
  1225. if (pos >= i_size_read(inode))
  1226. return VM_FAULT_SIGBUS;
  1227. entry = grab_mapping_entry(mapping, vmf->pgoff);
  1228. if (IS_ERR(entry)) {
  1229. error = PTR_ERR(entry);
  1230. goto out;
  1231. }
  1232. if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
  1233. flags |= IOMAP_WRITE;
  1234. /*
  1235. * Note that we don't bother to use iomap_apply here: DAX required
  1236. * the file system block size to be equal the page size, which means
  1237. * that we never have to deal with more than a single extent here.
  1238. */
  1239. error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
  1240. if (error)
  1241. goto unlock_entry;
  1242. if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
  1243. error = -EIO; /* fs corruption? */
  1244. goto unlock_entry;
  1245. }
  1246. sector = iomap.blkno + (((pos & PAGE_MASK) - iomap.offset) >> 9);
  1247. if (vmf->cow_page) {
  1248. switch (iomap.type) {
  1249. case IOMAP_HOLE:
  1250. case IOMAP_UNWRITTEN:
  1251. clear_user_highpage(vmf->cow_page, vaddr);
  1252. break;
  1253. case IOMAP_MAPPED:
  1254. error = copy_user_dax(iomap.bdev, sector, PAGE_SIZE,
  1255. vmf->cow_page, vaddr);
  1256. break;
  1257. default:
  1258. WARN_ON_ONCE(1);
  1259. error = -EIO;
  1260. break;
  1261. }
  1262. if (error)
  1263. goto unlock_entry;
  1264. if (!radix_tree_exceptional_entry(entry)) {
  1265. vmf->page = entry;
  1266. return VM_FAULT_LOCKED;
  1267. }
  1268. vmf->entry = entry;
  1269. return VM_FAULT_DAX_LOCKED;
  1270. }
  1271. switch (iomap.type) {
  1272. case IOMAP_MAPPED:
  1273. if (iomap.flags & IOMAP_F_NEW) {
  1274. count_vm_event(PGMAJFAULT);
  1275. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  1276. major = VM_FAULT_MAJOR;
  1277. }
  1278. error = dax_insert_mapping(mapping, iomap.bdev, sector,
  1279. PAGE_SIZE, &entry, vma, vmf);
  1280. break;
  1281. case IOMAP_UNWRITTEN:
  1282. case IOMAP_HOLE:
  1283. if (!(vmf->flags & FAULT_FLAG_WRITE))
  1284. return dax_load_hole(mapping, entry, vmf);
  1285. /*FALLTHRU*/
  1286. default:
  1287. WARN_ON_ONCE(1);
  1288. error = -EIO;
  1289. break;
  1290. }
  1291. unlock_entry:
  1292. put_locked_mapping_entry(mapping, vmf->pgoff, entry);
  1293. out:
  1294. if (error == -ENOMEM)
  1295. return VM_FAULT_OOM | major;
  1296. /* -EBUSY is fine, somebody else faulted on the same PTE */
  1297. if (error < 0 && error != -EBUSY)
  1298. return VM_FAULT_SIGBUS | major;
  1299. return VM_FAULT_NOPAGE | major;
  1300. }
  1301. EXPORT_SYMBOL_GPL(iomap_dax_fault);
  1302. #endif /* CONFIG_FS_IOMAP */