dax.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  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/pmem.h>
  27. #include <linux/sched.h>
  28. #include <linux/uio.h>
  29. #include <linux/vmstat.h>
  30. int dax_clear_blocks(struct inode *inode, sector_t block, long size)
  31. {
  32. struct block_device *bdev = inode->i_sb->s_bdev;
  33. sector_t sector = block << (inode->i_blkbits - 9);
  34. might_sleep();
  35. do {
  36. void __pmem *addr;
  37. unsigned long pfn;
  38. long count;
  39. count = bdev_direct_access(bdev, sector, &addr, &pfn, size);
  40. if (count < 0)
  41. return count;
  42. BUG_ON(size < count);
  43. while (count > 0) {
  44. unsigned pgsz = PAGE_SIZE - offset_in_page(addr);
  45. if (pgsz > count)
  46. pgsz = count;
  47. clear_pmem(addr, pgsz);
  48. addr += pgsz;
  49. size -= pgsz;
  50. count -= pgsz;
  51. BUG_ON(pgsz & 511);
  52. sector += pgsz / 512;
  53. cond_resched();
  54. }
  55. } while (size);
  56. wmb_pmem();
  57. return 0;
  58. }
  59. EXPORT_SYMBOL_GPL(dax_clear_blocks);
  60. static long dax_get_addr(struct buffer_head *bh, void __pmem **addr,
  61. unsigned blkbits)
  62. {
  63. unsigned long pfn;
  64. sector_t sector = bh->b_blocknr << (blkbits - 9);
  65. return bdev_direct_access(bh->b_bdev, sector, addr, &pfn, bh->b_size);
  66. }
  67. /* the clear_pmem() calls are ordered by a wmb_pmem() in the caller */
  68. static void dax_new_buf(void __pmem *addr, unsigned size, unsigned first,
  69. loff_t pos, loff_t end)
  70. {
  71. loff_t final = end - pos + first; /* The final byte of the buffer */
  72. if (first > 0)
  73. clear_pmem(addr, first);
  74. if (final < size)
  75. clear_pmem(addr + final, size - final);
  76. }
  77. static bool buffer_written(struct buffer_head *bh)
  78. {
  79. return buffer_mapped(bh) && !buffer_unwritten(bh);
  80. }
  81. /*
  82. * When ext4 encounters a hole, it returns without modifying the buffer_head
  83. * which means that we can't trust b_size. To cope with this, we set b_state
  84. * to 0 before calling get_block and, if any bit is set, we know we can trust
  85. * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is
  86. * and would save us time calling get_block repeatedly.
  87. */
  88. static bool buffer_size_valid(struct buffer_head *bh)
  89. {
  90. return bh->b_state != 0;
  91. }
  92. static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
  93. loff_t start, loff_t end, get_block_t get_block,
  94. struct buffer_head *bh)
  95. {
  96. ssize_t retval = 0;
  97. loff_t pos = start;
  98. loff_t max = start;
  99. loff_t bh_max = start;
  100. void __pmem *addr;
  101. bool hole = false;
  102. bool need_wmb = false;
  103. if (iov_iter_rw(iter) != WRITE)
  104. end = min(end, i_size_read(inode));
  105. while (pos < end) {
  106. size_t len;
  107. if (pos == max) {
  108. unsigned blkbits = inode->i_blkbits;
  109. sector_t block = pos >> blkbits;
  110. unsigned first = pos - (block << blkbits);
  111. long size;
  112. if (pos == bh_max) {
  113. bh->b_size = PAGE_ALIGN(end - pos);
  114. bh->b_state = 0;
  115. retval = get_block(inode, block, bh,
  116. iov_iter_rw(iter) == WRITE);
  117. if (retval)
  118. break;
  119. if (!buffer_size_valid(bh))
  120. bh->b_size = 1 << blkbits;
  121. bh_max = pos - first + bh->b_size;
  122. } else {
  123. unsigned done = bh->b_size -
  124. (bh_max - (pos - first));
  125. bh->b_blocknr += done >> blkbits;
  126. bh->b_size -= done;
  127. }
  128. hole = iov_iter_rw(iter) != WRITE && !buffer_written(bh);
  129. if (hole) {
  130. addr = NULL;
  131. size = bh->b_size - first;
  132. } else {
  133. retval = dax_get_addr(bh, &addr, blkbits);
  134. if (retval < 0)
  135. break;
  136. if (buffer_unwritten(bh) || buffer_new(bh)) {
  137. dax_new_buf(addr, retval, first, pos,
  138. end);
  139. need_wmb = true;
  140. }
  141. addr += first;
  142. size = retval - first;
  143. }
  144. max = min(pos + size, end);
  145. }
  146. if (iov_iter_rw(iter) == WRITE) {
  147. len = copy_from_iter_pmem(addr, max - pos, iter);
  148. need_wmb = true;
  149. } else if (!hole)
  150. len = copy_to_iter((void __force *)addr, max - pos,
  151. iter);
  152. else
  153. len = iov_iter_zero(max - pos, iter);
  154. if (!len)
  155. break;
  156. pos += len;
  157. addr += len;
  158. }
  159. if (need_wmb)
  160. wmb_pmem();
  161. return (pos == start) ? retval : pos - start;
  162. }
  163. /**
  164. * dax_do_io - Perform I/O to a DAX file
  165. * @iocb: The control block for this I/O
  166. * @inode: The file which the I/O is directed at
  167. * @iter: The addresses to do I/O from or to
  168. * @pos: The file offset where the I/O starts
  169. * @get_block: The filesystem method used to translate file offsets to blocks
  170. * @end_io: A filesystem callback for I/O completion
  171. * @flags: See below
  172. *
  173. * This function uses the same locking scheme as do_blockdev_direct_IO:
  174. * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
  175. * caller for writes. For reads, we take and release the i_mutex ourselves.
  176. * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
  177. * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
  178. * is in progress.
  179. */
  180. ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
  181. struct iov_iter *iter, loff_t pos, get_block_t get_block,
  182. dio_iodone_t end_io, int flags)
  183. {
  184. struct buffer_head bh;
  185. ssize_t retval = -EINVAL;
  186. loff_t end = pos + iov_iter_count(iter);
  187. memset(&bh, 0, sizeof(bh));
  188. if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) {
  189. struct address_space *mapping = inode->i_mapping;
  190. mutex_lock(&inode->i_mutex);
  191. retval = filemap_write_and_wait_range(mapping, pos, end - 1);
  192. if (retval) {
  193. mutex_unlock(&inode->i_mutex);
  194. goto out;
  195. }
  196. }
  197. /* Protects against truncate */
  198. if (!(flags & DIO_SKIP_DIO_COUNT))
  199. inode_dio_begin(inode);
  200. retval = dax_io(inode, iter, pos, end, get_block, &bh);
  201. if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
  202. mutex_unlock(&inode->i_mutex);
  203. if ((retval > 0) && end_io)
  204. end_io(iocb, pos, retval, bh.b_private);
  205. if (!(flags & DIO_SKIP_DIO_COUNT))
  206. inode_dio_end(inode);
  207. out:
  208. return retval;
  209. }
  210. EXPORT_SYMBOL_GPL(dax_do_io);
  211. /*
  212. * The user has performed a load from a hole in the file. Allocating
  213. * a new page in the file would cause excessive storage usage for
  214. * workloads with sparse files. We allocate a page cache page instead.
  215. * We'll kick it out of the page cache if it's ever written to,
  216. * otherwise it will simply fall out of the page cache under memory
  217. * pressure without ever having been dirtied.
  218. */
  219. static int dax_load_hole(struct address_space *mapping, struct page *page,
  220. struct vm_fault *vmf)
  221. {
  222. unsigned long size;
  223. struct inode *inode = mapping->host;
  224. if (!page)
  225. page = find_or_create_page(mapping, vmf->pgoff,
  226. GFP_KERNEL | __GFP_ZERO);
  227. if (!page)
  228. return VM_FAULT_OOM;
  229. /* Recheck i_size under page lock to avoid truncate race */
  230. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  231. if (vmf->pgoff >= size) {
  232. unlock_page(page);
  233. page_cache_release(page);
  234. return VM_FAULT_SIGBUS;
  235. }
  236. vmf->page = page;
  237. return VM_FAULT_LOCKED;
  238. }
  239. static int copy_user_bh(struct page *to, struct buffer_head *bh,
  240. unsigned blkbits, unsigned long vaddr)
  241. {
  242. void __pmem *vfrom;
  243. void *vto;
  244. if (dax_get_addr(bh, &vfrom, blkbits) < 0)
  245. return -EIO;
  246. vto = kmap_atomic(to);
  247. copy_user_page(vto, (void __force *)vfrom, vaddr, to);
  248. kunmap_atomic(vto);
  249. return 0;
  250. }
  251. static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
  252. struct vm_area_struct *vma, struct vm_fault *vmf)
  253. {
  254. sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
  255. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  256. void __pmem *addr;
  257. unsigned long pfn;
  258. pgoff_t size;
  259. int error;
  260. /*
  261. * Check truncate didn't happen while we were allocating a block.
  262. * If it did, this block may or may not be still allocated to the
  263. * file. We can't tell the filesystem to free it because we can't
  264. * take i_mutex here. In the worst case, the file still has blocks
  265. * allocated past the end of the file.
  266. */
  267. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  268. if (unlikely(vmf->pgoff >= size)) {
  269. error = -EIO;
  270. goto out;
  271. }
  272. error = bdev_direct_access(bh->b_bdev, sector, &addr, &pfn, bh->b_size);
  273. if (error < 0)
  274. goto out;
  275. if (error < PAGE_SIZE) {
  276. error = -EIO;
  277. goto out;
  278. }
  279. if (buffer_unwritten(bh) || buffer_new(bh)) {
  280. clear_pmem(addr, PAGE_SIZE);
  281. wmb_pmem();
  282. }
  283. error = vm_insert_mixed(vma, vaddr, pfn);
  284. out:
  285. return error;
  286. }
  287. /**
  288. * __dax_fault - handle a page fault on a DAX file
  289. * @vma: The virtual memory area where the fault occurred
  290. * @vmf: The description of the fault
  291. * @get_block: The filesystem method used to translate file offsets to blocks
  292. * @complete_unwritten: The filesystem method used to convert unwritten blocks
  293. * to written so the data written to them is exposed. This is required for
  294. * required by write faults for filesystems that will return unwritten
  295. * extent mappings from @get_block, but it is optional for reads as
  296. * dax_insert_mapping() will always zero unwritten blocks. If the fs does
  297. * not support unwritten extents, the it should pass NULL.
  298. *
  299. * When a page fault occurs, filesystems may call this helper in their
  300. * fault handler for DAX files. __dax_fault() assumes the caller has done all
  301. * the necessary locking for the page fault to proceed successfully.
  302. */
  303. int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  304. get_block_t get_block, dax_iodone_t complete_unwritten)
  305. {
  306. struct file *file = vma->vm_file;
  307. struct address_space *mapping = file->f_mapping;
  308. struct inode *inode = mapping->host;
  309. struct page *page;
  310. struct buffer_head bh;
  311. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  312. unsigned blkbits = inode->i_blkbits;
  313. sector_t block;
  314. pgoff_t size;
  315. int error;
  316. int major = 0;
  317. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  318. if (vmf->pgoff >= size)
  319. return VM_FAULT_SIGBUS;
  320. memset(&bh, 0, sizeof(bh));
  321. block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
  322. bh.b_size = PAGE_SIZE;
  323. repeat:
  324. page = find_get_page(mapping, vmf->pgoff);
  325. if (page) {
  326. if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
  327. page_cache_release(page);
  328. return VM_FAULT_RETRY;
  329. }
  330. if (unlikely(page->mapping != mapping)) {
  331. unlock_page(page);
  332. page_cache_release(page);
  333. goto repeat;
  334. }
  335. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  336. if (unlikely(vmf->pgoff >= size)) {
  337. /*
  338. * We have a struct page covering a hole in the file
  339. * from a read fault and we've raced with a truncate
  340. */
  341. error = -EIO;
  342. goto unlock;
  343. }
  344. } else {
  345. i_mmap_lock_write(mapping);
  346. }
  347. error = get_block(inode, block, &bh, 0);
  348. if (!error && (bh.b_size < PAGE_SIZE))
  349. error = -EIO; /* fs corruption? */
  350. if (error)
  351. goto unlock;
  352. if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) {
  353. if (vmf->flags & FAULT_FLAG_WRITE) {
  354. error = get_block(inode, block, &bh, 1);
  355. count_vm_event(PGMAJFAULT);
  356. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  357. major = VM_FAULT_MAJOR;
  358. if (!error && (bh.b_size < PAGE_SIZE))
  359. error = -EIO;
  360. if (error)
  361. goto unlock;
  362. } else {
  363. i_mmap_unlock_write(mapping);
  364. return dax_load_hole(mapping, page, vmf);
  365. }
  366. }
  367. if (vmf->cow_page) {
  368. struct page *new_page = vmf->cow_page;
  369. if (buffer_written(&bh))
  370. error = copy_user_bh(new_page, &bh, blkbits, vaddr);
  371. else
  372. clear_user_highpage(new_page, vaddr);
  373. if (error)
  374. goto unlock;
  375. vmf->page = page;
  376. if (!page) {
  377. /* Check we didn't race with truncate */
  378. size = (i_size_read(inode) + PAGE_SIZE - 1) >>
  379. PAGE_SHIFT;
  380. if (vmf->pgoff >= size) {
  381. error = -EIO;
  382. goto unlock;
  383. }
  384. }
  385. return VM_FAULT_LOCKED;
  386. }
  387. /* Check we didn't race with a read fault installing a new page */
  388. if (!page && major)
  389. page = find_lock_page(mapping, vmf->pgoff);
  390. if (page) {
  391. unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
  392. PAGE_CACHE_SIZE, 0);
  393. delete_from_page_cache(page);
  394. unlock_page(page);
  395. page_cache_release(page);
  396. }
  397. /*
  398. * If we successfully insert the new mapping over an unwritten extent,
  399. * we need to ensure we convert the unwritten extent. If there is an
  400. * error inserting the mapping, the filesystem needs to leave it as
  401. * unwritten to prevent exposure of the stale underlying data to
  402. * userspace, but we still need to call the completion function so
  403. * the private resources on the mapping buffer can be released. We
  404. * indicate what the callback should do via the uptodate variable, same
  405. * as for normal BH based IO completions.
  406. */
  407. error = dax_insert_mapping(inode, &bh, vma, vmf);
  408. if (buffer_unwritten(&bh)) {
  409. if (complete_unwritten)
  410. complete_unwritten(&bh, !error);
  411. else
  412. WARN_ON_ONCE(!(vmf->flags & FAULT_FLAG_WRITE));
  413. }
  414. if (!page)
  415. i_mmap_unlock_write(mapping);
  416. out:
  417. if (error == -ENOMEM)
  418. return VM_FAULT_OOM | major;
  419. /* -EBUSY is fine, somebody else faulted on the same PTE */
  420. if ((error < 0) && (error != -EBUSY))
  421. return VM_FAULT_SIGBUS | major;
  422. return VM_FAULT_NOPAGE | major;
  423. unlock:
  424. if (page) {
  425. unlock_page(page);
  426. page_cache_release(page);
  427. } else {
  428. i_mmap_unlock_write(mapping);
  429. }
  430. goto out;
  431. }
  432. EXPORT_SYMBOL(__dax_fault);
  433. /**
  434. * dax_fault - handle a page fault on a DAX file
  435. * @vma: The virtual memory area where the fault occurred
  436. * @vmf: The description of the fault
  437. * @get_block: The filesystem method used to translate file offsets to blocks
  438. *
  439. * When a page fault occurs, filesystems may call this helper in their
  440. * fault handler for DAX files.
  441. */
  442. int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  443. get_block_t get_block, dax_iodone_t complete_unwritten)
  444. {
  445. int result;
  446. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  447. if (vmf->flags & FAULT_FLAG_WRITE) {
  448. sb_start_pagefault(sb);
  449. file_update_time(vma->vm_file);
  450. }
  451. result = __dax_fault(vma, vmf, get_block, complete_unwritten);
  452. if (vmf->flags & FAULT_FLAG_WRITE)
  453. sb_end_pagefault(sb);
  454. return result;
  455. }
  456. EXPORT_SYMBOL_GPL(dax_fault);
  457. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  458. /*
  459. * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
  460. * more often than one might expect in the below function.
  461. */
  462. #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
  463. int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
  464. pmd_t *pmd, unsigned int flags, get_block_t get_block,
  465. dax_iodone_t complete_unwritten)
  466. {
  467. struct file *file = vma->vm_file;
  468. struct address_space *mapping = file->f_mapping;
  469. struct inode *inode = mapping->host;
  470. struct buffer_head bh;
  471. unsigned blkbits = inode->i_blkbits;
  472. unsigned long pmd_addr = address & PMD_MASK;
  473. bool write = flags & FAULT_FLAG_WRITE;
  474. long length;
  475. void __pmem *kaddr;
  476. pgoff_t size, pgoff;
  477. sector_t block, sector;
  478. unsigned long pfn;
  479. int result = 0;
  480. /* Fall back to PTEs if we're going to COW */
  481. if (write && !(vma->vm_flags & VM_SHARED))
  482. return VM_FAULT_FALLBACK;
  483. /* If the PMD would extend outside the VMA */
  484. if (pmd_addr < vma->vm_start)
  485. return VM_FAULT_FALLBACK;
  486. if ((pmd_addr + PMD_SIZE) > vma->vm_end)
  487. return VM_FAULT_FALLBACK;
  488. pgoff = linear_page_index(vma, pmd_addr);
  489. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  490. if (pgoff >= size)
  491. return VM_FAULT_SIGBUS;
  492. /* If the PMD would cover blocks out of the file */
  493. if ((pgoff | PG_PMD_COLOUR) >= size)
  494. return VM_FAULT_FALLBACK;
  495. memset(&bh, 0, sizeof(bh));
  496. block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
  497. bh.b_size = PMD_SIZE;
  498. i_mmap_lock_write(mapping);
  499. length = get_block(inode, block, &bh, write);
  500. if (length)
  501. return VM_FAULT_SIGBUS;
  502. /*
  503. * If the filesystem isn't willing to tell us the length of a hole,
  504. * just fall back to PTEs. Calling get_block 512 times in a loop
  505. * would be silly.
  506. */
  507. if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE)
  508. goto fallback;
  509. if (buffer_unwritten(&bh) || buffer_new(&bh)) {
  510. int i;
  511. for (i = 0; i < PTRS_PER_PMD; i++)
  512. clear_pmem(kaddr + i * PAGE_SIZE, PAGE_SIZE);
  513. wmb_pmem();
  514. count_vm_event(PGMAJFAULT);
  515. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  516. result |= VM_FAULT_MAJOR;
  517. }
  518. /*
  519. * If we allocated new storage, make sure no process has any
  520. * zero pages covering this hole
  521. */
  522. if (buffer_new(&bh)) {
  523. i_mmap_unlock_write(mapping);
  524. unmap_mapping_range(mapping, pgoff << PAGE_SHIFT, PMD_SIZE, 0);
  525. i_mmap_lock_write(mapping);
  526. }
  527. /*
  528. * If a truncate happened while we were allocating blocks, we may
  529. * leave blocks allocated to the file that are beyond EOF. We can't
  530. * take i_mutex here, so just leave them hanging; they'll be freed
  531. * when the file is deleted.
  532. */
  533. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  534. if (pgoff >= size) {
  535. result = VM_FAULT_SIGBUS;
  536. goto out;
  537. }
  538. if ((pgoff | PG_PMD_COLOUR) >= size)
  539. goto fallback;
  540. if (!write && !buffer_mapped(&bh) && buffer_uptodate(&bh)) {
  541. spinlock_t *ptl;
  542. pmd_t entry;
  543. struct page *zero_page = get_huge_zero_page();
  544. if (unlikely(!zero_page))
  545. goto fallback;
  546. ptl = pmd_lock(vma->vm_mm, pmd);
  547. if (!pmd_none(*pmd)) {
  548. spin_unlock(ptl);
  549. goto fallback;
  550. }
  551. entry = mk_pmd(zero_page, vma->vm_page_prot);
  552. entry = pmd_mkhuge(entry);
  553. set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
  554. result = VM_FAULT_NOPAGE;
  555. spin_unlock(ptl);
  556. } else {
  557. sector = bh.b_blocknr << (blkbits - 9);
  558. length = bdev_direct_access(bh.b_bdev, sector, &kaddr, &pfn,
  559. bh.b_size);
  560. if (length < 0) {
  561. result = VM_FAULT_SIGBUS;
  562. goto out;
  563. }
  564. if ((length < PMD_SIZE) || (pfn & PG_PMD_COLOUR))
  565. goto fallback;
  566. result |= vmf_insert_pfn_pmd(vma, address, pmd, pfn, write);
  567. }
  568. out:
  569. if (buffer_unwritten(&bh))
  570. complete_unwritten(&bh, !(result & VM_FAULT_ERROR));
  571. i_mmap_unlock_write(mapping);
  572. return result;
  573. fallback:
  574. count_vm_event(THP_FAULT_FALLBACK);
  575. result = VM_FAULT_FALLBACK;
  576. goto out;
  577. }
  578. EXPORT_SYMBOL_GPL(__dax_pmd_fault);
  579. /**
  580. * dax_pmd_fault - handle a PMD fault on a DAX file
  581. * @vma: The virtual memory area where the fault occurred
  582. * @vmf: The description of the fault
  583. * @get_block: The filesystem method used to translate file offsets to blocks
  584. *
  585. * When a page fault occurs, filesystems may call this helper in their
  586. * pmd_fault handler for DAX files.
  587. */
  588. int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
  589. pmd_t *pmd, unsigned int flags, get_block_t get_block,
  590. dax_iodone_t complete_unwritten)
  591. {
  592. int result;
  593. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  594. if (flags & FAULT_FLAG_WRITE) {
  595. sb_start_pagefault(sb);
  596. file_update_time(vma->vm_file);
  597. }
  598. result = __dax_pmd_fault(vma, address, pmd, flags, get_block,
  599. complete_unwritten);
  600. if (flags & FAULT_FLAG_WRITE)
  601. sb_end_pagefault(sb);
  602. return result;
  603. }
  604. EXPORT_SYMBOL_GPL(dax_pmd_fault);
  605. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  606. /**
  607. * dax_pfn_mkwrite - handle first write to DAX page
  608. * @vma: The virtual memory area where the fault occurred
  609. * @vmf: The description of the fault
  610. *
  611. */
  612. int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  613. {
  614. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  615. sb_start_pagefault(sb);
  616. file_update_time(vma->vm_file);
  617. sb_end_pagefault(sb);
  618. return VM_FAULT_NOPAGE;
  619. }
  620. EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
  621. /**
  622. * dax_zero_page_range - zero a range within a page of a DAX file
  623. * @inode: The file being truncated
  624. * @from: The file offset that is being truncated to
  625. * @length: The number of bytes to zero
  626. * @get_block: The filesystem method used to translate file offsets to blocks
  627. *
  628. * This function can be called by a filesystem when it is zeroing part of a
  629. * page in a DAX file. This is intended for hole-punch operations. If
  630. * you are truncating a file, the helper function dax_truncate_page() may be
  631. * more convenient.
  632. *
  633. * We work in terms of PAGE_CACHE_SIZE here for commonality with
  634. * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
  635. * took care of disposing of the unnecessary blocks. Even if the filesystem
  636. * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
  637. * since the file might be mmapped.
  638. */
  639. int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
  640. get_block_t get_block)
  641. {
  642. struct buffer_head bh;
  643. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  644. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  645. int err;
  646. /* Block boundary? Nothing to do */
  647. if (!length)
  648. return 0;
  649. BUG_ON((offset + length) > PAGE_CACHE_SIZE);
  650. memset(&bh, 0, sizeof(bh));
  651. bh.b_size = PAGE_CACHE_SIZE;
  652. err = get_block(inode, index, &bh, 0);
  653. if (err < 0)
  654. return err;
  655. if (buffer_written(&bh)) {
  656. void __pmem *addr;
  657. err = dax_get_addr(&bh, &addr, inode->i_blkbits);
  658. if (err < 0)
  659. return err;
  660. clear_pmem(addr + offset, length);
  661. wmb_pmem();
  662. }
  663. return 0;
  664. }
  665. EXPORT_SYMBOL_GPL(dax_zero_page_range);
  666. /**
  667. * dax_truncate_page - handle a partial page being truncated in a DAX file
  668. * @inode: The file being truncated
  669. * @from: The file offset that is being truncated to
  670. * @get_block: The filesystem method used to translate file offsets to blocks
  671. *
  672. * Similar to block_truncate_page(), this function can be called by a
  673. * filesystem when it is truncating a DAX file to handle the partial page.
  674. *
  675. * We work in terms of PAGE_CACHE_SIZE here for commonality with
  676. * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
  677. * took care of disposing of the unnecessary blocks. Even if the filesystem
  678. * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
  679. * since the file might be mmapped.
  680. */
  681. int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
  682. {
  683. unsigned length = PAGE_CACHE_ALIGN(from) - from;
  684. return dax_zero_page_range(inode, from, length, get_block);
  685. }
  686. EXPORT_SYMBOL_GPL(dax_truncate_page);