dax.c 35 KB

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