dax.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471
  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 = get_huge_zero_page();
  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. dax.sector = iomap->blkno +
  1120. (((pos & PAGE_MASK) - iomap->offset) >> 9);
  1121. dax.size = (length + offset + PAGE_SIZE - 1) & PAGE_MASK;
  1122. map_len = dax_map_atomic(iomap->bdev, &dax);
  1123. if (map_len < 0) {
  1124. ret = map_len;
  1125. break;
  1126. }
  1127. dax.addr += offset;
  1128. map_len -= offset;
  1129. if (map_len > end - pos)
  1130. map_len = end - pos;
  1131. if (iov_iter_rw(iter) == WRITE)
  1132. map_len = copy_from_iter_pmem(dax.addr, map_len, iter);
  1133. else
  1134. map_len = copy_to_iter(dax.addr, map_len, iter);
  1135. dax_unmap_atomic(iomap->bdev, &dax);
  1136. if (map_len <= 0) {
  1137. ret = map_len ? map_len : -EFAULT;
  1138. break;
  1139. }
  1140. pos += map_len;
  1141. length -= map_len;
  1142. done += map_len;
  1143. }
  1144. return done ? done : ret;
  1145. }
  1146. /**
  1147. * iomap_dax_rw - Perform I/O to a DAX file
  1148. * @iocb: The control block for this I/O
  1149. * @iter: The addresses to do I/O from or to
  1150. * @ops: iomap ops passed from the file system
  1151. *
  1152. * This function performs read and write operations to directly mapped
  1153. * persistent memory. The callers needs to take care of read/write exclusion
  1154. * and evicting any page cache pages in the region under I/O.
  1155. */
  1156. ssize_t
  1157. iomap_dax_rw(struct kiocb *iocb, struct iov_iter *iter,
  1158. struct iomap_ops *ops)
  1159. {
  1160. struct address_space *mapping = iocb->ki_filp->f_mapping;
  1161. struct inode *inode = mapping->host;
  1162. loff_t pos = iocb->ki_pos, ret = 0, done = 0;
  1163. unsigned flags = 0;
  1164. if (iov_iter_rw(iter) == WRITE)
  1165. flags |= IOMAP_WRITE;
  1166. /*
  1167. * Yes, even DAX files can have page cache attached to them: A zeroed
  1168. * page is inserted into the pagecache when we have to serve a write
  1169. * fault on a hole. It should never be dirtied and can simply be
  1170. * dropped from the pagecache once we get real data for the page.
  1171. *
  1172. * XXX: This is racy against mmap, and there's nothing we can do about
  1173. * it. We'll eventually need to shift this down even further so that
  1174. * we can check if we allocated blocks over a hole first.
  1175. */
  1176. if (mapping->nrpages) {
  1177. ret = invalidate_inode_pages2_range(mapping,
  1178. pos >> PAGE_SHIFT,
  1179. (pos + iov_iter_count(iter) - 1) >> PAGE_SHIFT);
  1180. WARN_ON_ONCE(ret);
  1181. }
  1182. while (iov_iter_count(iter)) {
  1183. ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
  1184. iter, iomap_dax_actor);
  1185. if (ret <= 0)
  1186. break;
  1187. pos += ret;
  1188. done += ret;
  1189. }
  1190. iocb->ki_pos += done;
  1191. return done ? done : ret;
  1192. }
  1193. EXPORT_SYMBOL_GPL(iomap_dax_rw);
  1194. /**
  1195. * iomap_dax_fault - handle a page fault on a DAX file
  1196. * @vma: The virtual memory area where the fault occurred
  1197. * @vmf: The description of the fault
  1198. * @ops: iomap ops passed from the file system
  1199. *
  1200. * When a page fault occurs, filesystems may call this helper in their fault
  1201. * or mkwrite handler for DAX files. Assumes the caller has done all the
  1202. * necessary locking for the page fault to proceed successfully.
  1203. */
  1204. int iomap_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  1205. struct iomap_ops *ops)
  1206. {
  1207. struct address_space *mapping = vma->vm_file->f_mapping;
  1208. struct inode *inode = mapping->host;
  1209. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  1210. loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
  1211. sector_t sector;
  1212. struct iomap iomap = { 0 };
  1213. unsigned flags = 0;
  1214. int error, major = 0;
  1215. void *entry;
  1216. /*
  1217. * Check whether offset isn't beyond end of file now. Caller is supposed
  1218. * to hold locks serializing us with truncate / punch hole so this is
  1219. * a reliable test.
  1220. */
  1221. if (pos >= i_size_read(inode))
  1222. return VM_FAULT_SIGBUS;
  1223. entry = grab_mapping_entry(mapping, vmf->pgoff);
  1224. if (IS_ERR(entry)) {
  1225. error = PTR_ERR(entry);
  1226. goto out;
  1227. }
  1228. if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
  1229. flags |= IOMAP_WRITE;
  1230. /*
  1231. * Note that we don't bother to use iomap_apply here: DAX required
  1232. * the file system block size to be equal the page size, which means
  1233. * that we never have to deal with more than a single extent here.
  1234. */
  1235. error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
  1236. if (error)
  1237. goto unlock_entry;
  1238. if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
  1239. error = -EIO; /* fs corruption? */
  1240. goto unlock_entry;
  1241. }
  1242. sector = iomap.blkno + (((pos & PAGE_MASK) - iomap.offset) >> 9);
  1243. if (vmf->cow_page) {
  1244. switch (iomap.type) {
  1245. case IOMAP_HOLE:
  1246. case IOMAP_UNWRITTEN:
  1247. clear_user_highpage(vmf->cow_page, vaddr);
  1248. break;
  1249. case IOMAP_MAPPED:
  1250. error = copy_user_dax(iomap.bdev, sector, PAGE_SIZE,
  1251. vmf->cow_page, vaddr);
  1252. break;
  1253. default:
  1254. WARN_ON_ONCE(1);
  1255. error = -EIO;
  1256. break;
  1257. }
  1258. if (error)
  1259. goto unlock_entry;
  1260. if (!radix_tree_exceptional_entry(entry)) {
  1261. vmf->page = entry;
  1262. return VM_FAULT_LOCKED;
  1263. }
  1264. vmf->entry = entry;
  1265. return VM_FAULT_DAX_LOCKED;
  1266. }
  1267. switch (iomap.type) {
  1268. case IOMAP_MAPPED:
  1269. if (iomap.flags & IOMAP_F_NEW) {
  1270. count_vm_event(PGMAJFAULT);
  1271. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  1272. major = VM_FAULT_MAJOR;
  1273. }
  1274. error = dax_insert_mapping(mapping, iomap.bdev, sector,
  1275. PAGE_SIZE, &entry, vma, vmf);
  1276. break;
  1277. case IOMAP_UNWRITTEN:
  1278. case IOMAP_HOLE:
  1279. if (!(vmf->flags & FAULT_FLAG_WRITE))
  1280. return dax_load_hole(mapping, entry, vmf);
  1281. /*FALLTHRU*/
  1282. default:
  1283. WARN_ON_ONCE(1);
  1284. error = -EIO;
  1285. break;
  1286. }
  1287. unlock_entry:
  1288. put_locked_mapping_entry(mapping, vmf->pgoff, entry);
  1289. out:
  1290. if (error == -ENOMEM)
  1291. return VM_FAULT_OOM | major;
  1292. /* -EBUSY is fine, somebody else faulted on the same PTE */
  1293. if (error < 0 && error != -EBUSY)
  1294. return VM_FAULT_SIGBUS | major;
  1295. return VM_FAULT_NOPAGE | major;
  1296. }
  1297. EXPORT_SYMBOL_GPL(iomap_dax_fault);
  1298. #endif /* CONFIG_FS_IOMAP */