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