dax.c 22 KB

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