dax.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472
  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. start_index = indices[pvec.nr - 1] + 1;
  692. }
  693. return 0;
  694. }
  695. EXPORT_SYMBOL_GPL(dax_writeback_mapping_range);
  696. static int dax_insert_mapping(struct address_space *mapping,
  697. struct block_device *bdev, sector_t sector, size_t size,
  698. void **entryp, struct vm_area_struct *vma, struct vm_fault *vmf)
  699. {
  700. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  701. struct blk_dax_ctl dax = {
  702. .sector = sector,
  703. .size = size,
  704. };
  705. void *ret;
  706. void *entry = *entryp;
  707. if (dax_map_atomic(bdev, &dax) < 0)
  708. return PTR_ERR(dax.addr);
  709. dax_unmap_atomic(bdev, &dax);
  710. ret = dax_insert_mapping_entry(mapping, vmf, entry, dax.sector);
  711. if (IS_ERR(ret))
  712. return PTR_ERR(ret);
  713. *entryp = ret;
  714. return vm_insert_mixed(vma, vaddr, dax.pfn);
  715. }
  716. /**
  717. * dax_fault - handle a page fault on a DAX file
  718. * @vma: The virtual memory area where the fault occurred
  719. * @vmf: The description of the fault
  720. * @get_block: The filesystem method used to translate file offsets to blocks
  721. *
  722. * When a page fault occurs, filesystems may call this helper in their
  723. * fault handler for DAX files. dax_fault() assumes the caller has done all
  724. * the necessary locking for the page fault to proceed successfully.
  725. */
  726. int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  727. get_block_t get_block)
  728. {
  729. struct file *file = vma->vm_file;
  730. struct address_space *mapping = file->f_mapping;
  731. struct inode *inode = mapping->host;
  732. void *entry;
  733. struct buffer_head bh;
  734. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  735. unsigned blkbits = inode->i_blkbits;
  736. sector_t block;
  737. pgoff_t size;
  738. int error;
  739. int major = 0;
  740. /*
  741. * Check whether offset isn't beyond end of file now. Caller is supposed
  742. * to hold locks serializing us with truncate / punch hole so this is
  743. * a reliable test.
  744. */
  745. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  746. if (vmf->pgoff >= size)
  747. return VM_FAULT_SIGBUS;
  748. memset(&bh, 0, sizeof(bh));
  749. block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
  750. bh.b_bdev = inode->i_sb->s_bdev;
  751. bh.b_size = PAGE_SIZE;
  752. entry = grab_mapping_entry(mapping, vmf->pgoff);
  753. if (IS_ERR(entry)) {
  754. error = PTR_ERR(entry);
  755. goto out;
  756. }
  757. error = get_block(inode, block, &bh, 0);
  758. if (!error && (bh.b_size < PAGE_SIZE))
  759. error = -EIO; /* fs corruption? */
  760. if (error)
  761. goto unlock_entry;
  762. if (vmf->cow_page) {
  763. struct page *new_page = vmf->cow_page;
  764. if (buffer_written(&bh))
  765. error = copy_user_dax(bh.b_bdev, to_sector(&bh, inode),
  766. bh.b_size, new_page, vaddr);
  767. else
  768. clear_user_highpage(new_page, vaddr);
  769. if (error)
  770. goto unlock_entry;
  771. if (!radix_tree_exceptional_entry(entry)) {
  772. vmf->page = entry;
  773. return VM_FAULT_LOCKED;
  774. }
  775. vmf->entry = entry;
  776. return VM_FAULT_DAX_LOCKED;
  777. }
  778. if (!buffer_mapped(&bh)) {
  779. if (vmf->flags & FAULT_FLAG_WRITE) {
  780. error = get_block(inode, block, &bh, 1);
  781. count_vm_event(PGMAJFAULT);
  782. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  783. major = VM_FAULT_MAJOR;
  784. if (!error && (bh.b_size < PAGE_SIZE))
  785. error = -EIO;
  786. if (error)
  787. goto unlock_entry;
  788. } else {
  789. return dax_load_hole(mapping, entry, vmf);
  790. }
  791. }
  792. /* Filesystem should not return unwritten buffers to us! */
  793. WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
  794. error = dax_insert_mapping(mapping, bh.b_bdev, to_sector(&bh, inode),
  795. bh.b_size, &entry, vma, vmf);
  796. unlock_entry:
  797. put_locked_mapping_entry(mapping, vmf->pgoff, entry);
  798. out:
  799. if (error == -ENOMEM)
  800. return VM_FAULT_OOM | major;
  801. /* -EBUSY is fine, somebody else faulted on the same PTE */
  802. if ((error < 0) && (error != -EBUSY))
  803. return VM_FAULT_SIGBUS | major;
  804. return VM_FAULT_NOPAGE | major;
  805. }
  806. EXPORT_SYMBOL_GPL(dax_fault);
  807. #if defined(CONFIG_TRANSPARENT_HUGEPAGE)
  808. /*
  809. * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
  810. * more often than one might expect in the below function.
  811. */
  812. #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
  813. static void __dax_dbg(struct buffer_head *bh, unsigned long address,
  814. const char *reason, const char *fn)
  815. {
  816. if (bh) {
  817. char bname[BDEVNAME_SIZE];
  818. bdevname(bh->b_bdev, bname);
  819. pr_debug("%s: %s addr: %lx dev %s state %lx start %lld "
  820. "length %zd fallback: %s\n", fn, current->comm,
  821. address, bname, bh->b_state, (u64)bh->b_blocknr,
  822. bh->b_size, reason);
  823. } else {
  824. pr_debug("%s: %s addr: %lx fallback: %s\n", fn,
  825. current->comm, address, reason);
  826. }
  827. }
  828. #define dax_pmd_dbg(bh, address, reason) __dax_dbg(bh, address, reason, "dax_pmd")
  829. /**
  830. * dax_pmd_fault - handle a PMD fault on a DAX file
  831. * @vma: The virtual memory area where the fault occurred
  832. * @vmf: The description of the fault
  833. * @get_block: The filesystem method used to translate file offsets to blocks
  834. *
  835. * When a page fault occurs, filesystems may call this helper in their
  836. * pmd_fault handler for DAX files.
  837. */
  838. int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
  839. pmd_t *pmd, unsigned int flags, get_block_t get_block)
  840. {
  841. struct file *file = vma->vm_file;
  842. struct address_space *mapping = file->f_mapping;
  843. struct inode *inode = mapping->host;
  844. struct buffer_head bh;
  845. unsigned blkbits = inode->i_blkbits;
  846. unsigned long pmd_addr = address & PMD_MASK;
  847. bool write = flags & FAULT_FLAG_WRITE;
  848. struct block_device *bdev;
  849. pgoff_t size, pgoff;
  850. sector_t block;
  851. int result = 0;
  852. bool alloc = false;
  853. /* dax pmd mappings require pfn_t_devmap() */
  854. if (!IS_ENABLED(CONFIG_FS_DAX_PMD))
  855. return VM_FAULT_FALLBACK;
  856. /* Fall back to PTEs if we're going to COW */
  857. if (write && !(vma->vm_flags & VM_SHARED)) {
  858. split_huge_pmd(vma, pmd, address);
  859. dax_pmd_dbg(NULL, address, "cow write");
  860. return VM_FAULT_FALLBACK;
  861. }
  862. /* If the PMD would extend outside the VMA */
  863. if (pmd_addr < vma->vm_start) {
  864. dax_pmd_dbg(NULL, address, "vma start unaligned");
  865. return VM_FAULT_FALLBACK;
  866. }
  867. if ((pmd_addr + PMD_SIZE) > vma->vm_end) {
  868. dax_pmd_dbg(NULL, address, "vma end unaligned");
  869. return VM_FAULT_FALLBACK;
  870. }
  871. pgoff = linear_page_index(vma, pmd_addr);
  872. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  873. if (pgoff >= size)
  874. return VM_FAULT_SIGBUS;
  875. /* If the PMD would cover blocks out of the file */
  876. if ((pgoff | PG_PMD_COLOUR) >= size) {
  877. dax_pmd_dbg(NULL, address,
  878. "offset + huge page size > file size");
  879. return VM_FAULT_FALLBACK;
  880. }
  881. memset(&bh, 0, sizeof(bh));
  882. bh.b_bdev = inode->i_sb->s_bdev;
  883. block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
  884. bh.b_size = PMD_SIZE;
  885. if (get_block(inode, block, &bh, 0) != 0)
  886. return VM_FAULT_SIGBUS;
  887. if (!buffer_mapped(&bh) && write) {
  888. if (get_block(inode, block, &bh, 1) != 0)
  889. return VM_FAULT_SIGBUS;
  890. alloc = true;
  891. WARN_ON_ONCE(buffer_unwritten(&bh) || buffer_new(&bh));
  892. }
  893. bdev = bh.b_bdev;
  894. /*
  895. * If the filesystem isn't willing to tell us the length of a hole,
  896. * just fall back to PTEs. Calling get_block 512 times in a loop
  897. * would be silly.
  898. */
  899. if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE) {
  900. dax_pmd_dbg(&bh, address, "allocated block too small");
  901. return VM_FAULT_FALLBACK;
  902. }
  903. /*
  904. * If we allocated new storage, make sure no process has any
  905. * zero pages covering this hole
  906. */
  907. if (alloc) {
  908. loff_t lstart = pgoff << PAGE_SHIFT;
  909. loff_t lend = lstart + PMD_SIZE - 1; /* inclusive */
  910. truncate_pagecache_range(inode, lstart, lend);
  911. }
  912. if (!write && !buffer_mapped(&bh)) {
  913. spinlock_t *ptl;
  914. pmd_t entry;
  915. struct page *zero_page = mm_get_huge_zero_page(vma->vm_mm);
  916. if (unlikely(!zero_page)) {
  917. dax_pmd_dbg(&bh, address, "no zero page");
  918. goto fallback;
  919. }
  920. ptl = pmd_lock(vma->vm_mm, pmd);
  921. if (!pmd_none(*pmd)) {
  922. spin_unlock(ptl);
  923. dax_pmd_dbg(&bh, address, "pmd already present");
  924. goto fallback;
  925. }
  926. dev_dbg(part_to_dev(bdev->bd_part),
  927. "%s: %s addr: %lx pfn: <zero> sect: %llx\n",
  928. __func__, current->comm, address,
  929. (unsigned long long) to_sector(&bh, inode));
  930. entry = mk_pmd(zero_page, vma->vm_page_prot);
  931. entry = pmd_mkhuge(entry);
  932. set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
  933. result = VM_FAULT_NOPAGE;
  934. spin_unlock(ptl);
  935. } else {
  936. struct blk_dax_ctl dax = {
  937. .sector = to_sector(&bh, inode),
  938. .size = PMD_SIZE,
  939. };
  940. long length = dax_map_atomic(bdev, &dax);
  941. if (length < 0) {
  942. dax_pmd_dbg(&bh, address, "dax-error fallback");
  943. goto fallback;
  944. }
  945. if (length < PMD_SIZE) {
  946. dax_pmd_dbg(&bh, address, "dax-length too small");
  947. dax_unmap_atomic(bdev, &dax);
  948. goto fallback;
  949. }
  950. if (pfn_t_to_pfn(dax.pfn) & PG_PMD_COLOUR) {
  951. dax_pmd_dbg(&bh, address, "pfn unaligned");
  952. dax_unmap_atomic(bdev, &dax);
  953. goto fallback;
  954. }
  955. if (!pfn_t_devmap(dax.pfn)) {
  956. dax_unmap_atomic(bdev, &dax);
  957. dax_pmd_dbg(&bh, address, "pfn not in memmap");
  958. goto fallback;
  959. }
  960. dax_unmap_atomic(bdev, &dax);
  961. /*
  962. * For PTE faults we insert a radix tree entry for reads, and
  963. * leave it clean. Then on the first write we dirty the radix
  964. * tree entry via the dax_pfn_mkwrite() path. This sequence
  965. * allows the dax_pfn_mkwrite() call to be simpler and avoid a
  966. * call into get_block() to translate the pgoff to a sector in
  967. * order to be able to create a new radix tree entry.
  968. *
  969. * The PMD path doesn't have an equivalent to
  970. * dax_pfn_mkwrite(), though, so for a read followed by a
  971. * write we traverse all the way through dax_pmd_fault()
  972. * twice. This means we can just skip inserting a radix tree
  973. * entry completely on the initial read and just wait until
  974. * the write to insert a dirty entry.
  975. */
  976. if (write) {
  977. /*
  978. * We should insert radix-tree entry and dirty it here.
  979. * For now this is broken...
  980. */
  981. }
  982. dev_dbg(part_to_dev(bdev->bd_part),
  983. "%s: %s addr: %lx pfn: %lx sect: %llx\n",
  984. __func__, current->comm, address,
  985. pfn_t_to_pfn(dax.pfn),
  986. (unsigned long long) dax.sector);
  987. result |= vmf_insert_pfn_pmd(vma, address, pmd,
  988. dax.pfn, write);
  989. }
  990. out:
  991. return result;
  992. fallback:
  993. count_vm_event(THP_FAULT_FALLBACK);
  994. result = VM_FAULT_FALLBACK;
  995. goto out;
  996. }
  997. EXPORT_SYMBOL_GPL(dax_pmd_fault);
  998. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  999. /**
  1000. * dax_pfn_mkwrite - handle first write to DAX page
  1001. * @vma: The virtual memory area where the fault occurred
  1002. * @vmf: The description of the fault
  1003. */
  1004. int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  1005. {
  1006. struct file *file = vma->vm_file;
  1007. struct address_space *mapping = file->f_mapping;
  1008. void *entry;
  1009. pgoff_t index = vmf->pgoff;
  1010. spin_lock_irq(&mapping->tree_lock);
  1011. entry = get_unlocked_mapping_entry(mapping, index, NULL);
  1012. if (!entry || !radix_tree_exceptional_entry(entry))
  1013. goto out;
  1014. radix_tree_tag_set(&mapping->page_tree, index, PAGECACHE_TAG_DIRTY);
  1015. put_unlocked_mapping_entry(mapping, index, entry);
  1016. out:
  1017. spin_unlock_irq(&mapping->tree_lock);
  1018. return VM_FAULT_NOPAGE;
  1019. }
  1020. EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
  1021. static bool dax_range_is_aligned(struct block_device *bdev,
  1022. unsigned int offset, unsigned int length)
  1023. {
  1024. unsigned short sector_size = bdev_logical_block_size(bdev);
  1025. if (!IS_ALIGNED(offset, sector_size))
  1026. return false;
  1027. if (!IS_ALIGNED(length, sector_size))
  1028. return false;
  1029. return true;
  1030. }
  1031. int __dax_zero_page_range(struct block_device *bdev, sector_t sector,
  1032. unsigned int offset, unsigned int length)
  1033. {
  1034. struct blk_dax_ctl dax = {
  1035. .sector = sector,
  1036. .size = PAGE_SIZE,
  1037. };
  1038. if (dax_range_is_aligned(bdev, offset, length)) {
  1039. sector_t start_sector = dax.sector + (offset >> 9);
  1040. return blkdev_issue_zeroout(bdev, start_sector,
  1041. length >> 9, GFP_NOFS, true);
  1042. } else {
  1043. if (dax_map_atomic(bdev, &dax) < 0)
  1044. return PTR_ERR(dax.addr);
  1045. clear_pmem(dax.addr + offset, length);
  1046. dax_unmap_atomic(bdev, &dax);
  1047. }
  1048. return 0;
  1049. }
  1050. EXPORT_SYMBOL_GPL(__dax_zero_page_range);
  1051. /**
  1052. * dax_zero_page_range - zero a range within a page of a DAX file
  1053. * @inode: The file being truncated
  1054. * @from: The file offset that is being truncated to
  1055. * @length: The number of bytes to zero
  1056. * @get_block: The filesystem method used to translate file offsets to blocks
  1057. *
  1058. * This function can be called by a filesystem when it is zeroing part of a
  1059. * page in a DAX file. This is intended for hole-punch operations. If
  1060. * you are truncating a file, the helper function dax_truncate_page() may be
  1061. * more convenient.
  1062. */
  1063. int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
  1064. get_block_t get_block)
  1065. {
  1066. struct buffer_head bh;
  1067. pgoff_t index = from >> PAGE_SHIFT;
  1068. unsigned offset = from & (PAGE_SIZE-1);
  1069. int err;
  1070. /* Block boundary? Nothing to do */
  1071. if (!length)
  1072. return 0;
  1073. BUG_ON((offset + length) > PAGE_SIZE);
  1074. memset(&bh, 0, sizeof(bh));
  1075. bh.b_bdev = inode->i_sb->s_bdev;
  1076. bh.b_size = PAGE_SIZE;
  1077. err = get_block(inode, index, &bh, 0);
  1078. if (err < 0 || !buffer_written(&bh))
  1079. return err;
  1080. return __dax_zero_page_range(bh.b_bdev, to_sector(&bh, inode),
  1081. offset, length);
  1082. }
  1083. EXPORT_SYMBOL_GPL(dax_zero_page_range);
  1084. /**
  1085. * dax_truncate_page - handle a partial page being truncated in a DAX file
  1086. * @inode: The file being truncated
  1087. * @from: The file offset that is being truncated to
  1088. * @get_block: The filesystem method used to translate file offsets to blocks
  1089. *
  1090. * Similar to block_truncate_page(), this function can be called by a
  1091. * filesystem when it is truncating a DAX file to handle the partial page.
  1092. */
  1093. int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
  1094. {
  1095. unsigned length = PAGE_ALIGN(from) - from;
  1096. return dax_zero_page_range(inode, from, length, get_block);
  1097. }
  1098. EXPORT_SYMBOL_GPL(dax_truncate_page);
  1099. #ifdef CONFIG_FS_IOMAP
  1100. static loff_t
  1101. iomap_dax_actor(struct inode *inode, loff_t pos, loff_t length, void *data,
  1102. struct iomap *iomap)
  1103. {
  1104. struct iov_iter *iter = data;
  1105. loff_t end = pos + length, done = 0;
  1106. ssize_t ret = 0;
  1107. if (iov_iter_rw(iter) == READ) {
  1108. end = min(end, i_size_read(inode));
  1109. if (pos >= end)
  1110. return 0;
  1111. if (iomap->type == IOMAP_HOLE || iomap->type == IOMAP_UNWRITTEN)
  1112. return iov_iter_zero(min(length, end - pos), iter);
  1113. }
  1114. if (WARN_ON_ONCE(iomap->type != IOMAP_MAPPED))
  1115. return -EIO;
  1116. /*
  1117. * Write can allocate block for an area which has a hole page mapped
  1118. * into page tables. We have to tear down these mappings so that data
  1119. * written by write(2) is visible in mmap.
  1120. */
  1121. if ((iomap->flags & IOMAP_F_NEW) && inode->i_mapping->nrpages) {
  1122. invalidate_inode_pages2_range(inode->i_mapping,
  1123. pos >> PAGE_SHIFT,
  1124. (end - 1) >> PAGE_SHIFT);
  1125. }
  1126. while (pos < end) {
  1127. unsigned offset = pos & (PAGE_SIZE - 1);
  1128. struct blk_dax_ctl dax = { 0 };
  1129. ssize_t map_len;
  1130. if (fatal_signal_pending(current)) {
  1131. ret = -EINTR;
  1132. break;
  1133. }
  1134. dax.sector = iomap->blkno +
  1135. (((pos & PAGE_MASK) - iomap->offset) >> 9);
  1136. dax.size = (length + offset + PAGE_SIZE - 1) & PAGE_MASK;
  1137. map_len = dax_map_atomic(iomap->bdev, &dax);
  1138. if (map_len < 0) {
  1139. ret = map_len;
  1140. break;
  1141. }
  1142. dax.addr += offset;
  1143. map_len -= offset;
  1144. if (map_len > end - pos)
  1145. map_len = end - pos;
  1146. if (iov_iter_rw(iter) == WRITE)
  1147. map_len = copy_from_iter_pmem(dax.addr, map_len, iter);
  1148. else
  1149. map_len = copy_to_iter(dax.addr, map_len, iter);
  1150. dax_unmap_atomic(iomap->bdev, &dax);
  1151. if (map_len <= 0) {
  1152. ret = map_len ? map_len : -EFAULT;
  1153. break;
  1154. }
  1155. pos += map_len;
  1156. length -= map_len;
  1157. done += map_len;
  1158. }
  1159. return done ? done : ret;
  1160. }
  1161. /**
  1162. * iomap_dax_rw - Perform I/O to a DAX file
  1163. * @iocb: The control block for this I/O
  1164. * @iter: The addresses to do I/O from or to
  1165. * @ops: iomap ops passed from the file system
  1166. *
  1167. * This function performs read and write operations to directly mapped
  1168. * persistent memory. The callers needs to take care of read/write exclusion
  1169. * and evicting any page cache pages in the region under I/O.
  1170. */
  1171. ssize_t
  1172. iomap_dax_rw(struct kiocb *iocb, struct iov_iter *iter,
  1173. struct iomap_ops *ops)
  1174. {
  1175. struct address_space *mapping = iocb->ki_filp->f_mapping;
  1176. struct inode *inode = mapping->host;
  1177. loff_t pos = iocb->ki_pos, ret = 0, done = 0;
  1178. unsigned flags = 0;
  1179. if (iov_iter_rw(iter) == WRITE)
  1180. flags |= IOMAP_WRITE;
  1181. while (iov_iter_count(iter)) {
  1182. ret = iomap_apply(inode, pos, iov_iter_count(iter), flags, ops,
  1183. iter, iomap_dax_actor);
  1184. if (ret <= 0)
  1185. break;
  1186. pos += ret;
  1187. done += ret;
  1188. }
  1189. iocb->ki_pos += done;
  1190. return done ? done : ret;
  1191. }
  1192. EXPORT_SYMBOL_GPL(iomap_dax_rw);
  1193. /**
  1194. * iomap_dax_fault - handle a page fault on a DAX file
  1195. * @vma: The virtual memory area where the fault occurred
  1196. * @vmf: The description of the fault
  1197. * @ops: iomap ops passed from the file system
  1198. *
  1199. * When a page fault occurs, filesystems may call this helper in their fault
  1200. * or mkwrite handler for DAX files. Assumes the caller has done all the
  1201. * necessary locking for the page fault to proceed successfully.
  1202. */
  1203. int iomap_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  1204. struct iomap_ops *ops)
  1205. {
  1206. struct address_space *mapping = vma->vm_file->f_mapping;
  1207. struct inode *inode = mapping->host;
  1208. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  1209. loff_t pos = (loff_t)vmf->pgoff << PAGE_SHIFT;
  1210. sector_t sector;
  1211. struct iomap iomap = { 0 };
  1212. unsigned flags = 0;
  1213. int error, major = 0;
  1214. void *entry;
  1215. /*
  1216. * Check whether offset isn't beyond end of file now. Caller is supposed
  1217. * to hold locks serializing us with truncate / punch hole so this is
  1218. * a reliable test.
  1219. */
  1220. if (pos >= i_size_read(inode))
  1221. return VM_FAULT_SIGBUS;
  1222. entry = grab_mapping_entry(mapping, vmf->pgoff);
  1223. if (IS_ERR(entry)) {
  1224. error = PTR_ERR(entry);
  1225. goto out;
  1226. }
  1227. if ((vmf->flags & FAULT_FLAG_WRITE) && !vmf->cow_page)
  1228. flags |= IOMAP_WRITE;
  1229. /*
  1230. * Note that we don't bother to use iomap_apply here: DAX required
  1231. * the file system block size to be equal the page size, which means
  1232. * that we never have to deal with more than a single extent here.
  1233. */
  1234. error = ops->iomap_begin(inode, pos, PAGE_SIZE, flags, &iomap);
  1235. if (error)
  1236. goto unlock_entry;
  1237. if (WARN_ON_ONCE(iomap.offset + iomap.length < pos + PAGE_SIZE)) {
  1238. error = -EIO; /* fs corruption? */
  1239. goto unlock_entry;
  1240. }
  1241. sector = iomap.blkno + (((pos & PAGE_MASK) - iomap.offset) >> 9);
  1242. if (vmf->cow_page) {
  1243. switch (iomap.type) {
  1244. case IOMAP_HOLE:
  1245. case IOMAP_UNWRITTEN:
  1246. clear_user_highpage(vmf->cow_page, vaddr);
  1247. break;
  1248. case IOMAP_MAPPED:
  1249. error = copy_user_dax(iomap.bdev, sector, PAGE_SIZE,
  1250. vmf->cow_page, vaddr);
  1251. break;
  1252. default:
  1253. WARN_ON_ONCE(1);
  1254. error = -EIO;
  1255. break;
  1256. }
  1257. if (error)
  1258. goto unlock_entry;
  1259. if (!radix_tree_exceptional_entry(entry)) {
  1260. vmf->page = entry;
  1261. return VM_FAULT_LOCKED;
  1262. }
  1263. vmf->entry = entry;
  1264. return VM_FAULT_DAX_LOCKED;
  1265. }
  1266. switch (iomap.type) {
  1267. case IOMAP_MAPPED:
  1268. if (iomap.flags & IOMAP_F_NEW) {
  1269. count_vm_event(PGMAJFAULT);
  1270. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  1271. major = VM_FAULT_MAJOR;
  1272. }
  1273. error = dax_insert_mapping(mapping, iomap.bdev, sector,
  1274. PAGE_SIZE, &entry, vma, vmf);
  1275. break;
  1276. case IOMAP_UNWRITTEN:
  1277. case IOMAP_HOLE:
  1278. if (!(vmf->flags & FAULT_FLAG_WRITE))
  1279. return dax_load_hole(mapping, entry, vmf);
  1280. /*FALLTHRU*/
  1281. default:
  1282. WARN_ON_ONCE(1);
  1283. error = -EIO;
  1284. break;
  1285. }
  1286. unlock_entry:
  1287. put_locked_mapping_entry(mapping, vmf->pgoff, entry);
  1288. out:
  1289. if (error == -ENOMEM)
  1290. return VM_FAULT_OOM | major;
  1291. /* -EBUSY is fine, somebody else faulted on the same PTE */
  1292. if (error < 0 && error != -EBUSY)
  1293. return VM_FAULT_SIGBUS | major;
  1294. return VM_FAULT_NOPAGE | major;
  1295. }
  1296. EXPORT_SYMBOL_GPL(iomap_dax_fault);
  1297. #endif /* CONFIG_FS_IOMAP */