dax.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  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. sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
  256. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  257. void __pmem *addr;
  258. unsigned long pfn;
  259. pgoff_t size;
  260. int error;
  261. /*
  262. * Check truncate didn't happen while we were allocating a block.
  263. * If it did, this block may or may not be still allocated to the
  264. * file. We can't tell the filesystem to free it because we can't
  265. * take i_mutex here. In the worst case, the file still has blocks
  266. * allocated past the end of the file.
  267. */
  268. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  269. if (unlikely(vmf->pgoff >= size)) {
  270. error = -EIO;
  271. goto out;
  272. }
  273. error = bdev_direct_access(bh->b_bdev, sector, &addr, &pfn, bh->b_size);
  274. if (error < 0)
  275. goto out;
  276. if (error < PAGE_SIZE) {
  277. error = -EIO;
  278. goto out;
  279. }
  280. if (buffer_unwritten(bh) || buffer_new(bh)) {
  281. clear_pmem(addr, PAGE_SIZE);
  282. wmb_pmem();
  283. }
  284. error = vm_insert_mixed(vma, vaddr, pfn);
  285. out:
  286. return error;
  287. }
  288. /**
  289. * __dax_fault - handle a page fault on a DAX file
  290. * @vma: The virtual memory area where the fault occurred
  291. * @vmf: The description of the fault
  292. * @get_block: The filesystem method used to translate file offsets to blocks
  293. * @complete_unwritten: The filesystem method used to convert unwritten blocks
  294. * to written so the data written to them is exposed. This is required for
  295. * required by write faults for filesystems that will return unwritten
  296. * extent mappings from @get_block, but it is optional for reads as
  297. * dax_insert_mapping() will always zero unwritten blocks. If the fs does
  298. * not support unwritten extents, the it should pass NULL.
  299. *
  300. * When a page fault occurs, filesystems may call this helper in their
  301. * fault handler for DAX files. __dax_fault() assumes the caller has done all
  302. * the necessary locking for the page fault to proceed successfully.
  303. */
  304. int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  305. get_block_t get_block, dax_iodone_t complete_unwritten)
  306. {
  307. struct file *file = vma->vm_file;
  308. struct address_space *mapping = file->f_mapping;
  309. struct inode *inode = mapping->host;
  310. struct page *page;
  311. struct buffer_head bh;
  312. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  313. unsigned blkbits = inode->i_blkbits;
  314. sector_t block;
  315. pgoff_t size;
  316. int error;
  317. int major = 0;
  318. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  319. if (vmf->pgoff >= size)
  320. return VM_FAULT_SIGBUS;
  321. memset(&bh, 0, sizeof(bh));
  322. block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
  323. bh.b_size = PAGE_SIZE;
  324. repeat:
  325. page = find_get_page(mapping, vmf->pgoff);
  326. if (page) {
  327. if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
  328. page_cache_release(page);
  329. return VM_FAULT_RETRY;
  330. }
  331. if (unlikely(page->mapping != mapping)) {
  332. unlock_page(page);
  333. page_cache_release(page);
  334. goto repeat;
  335. }
  336. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  337. if (unlikely(vmf->pgoff >= size)) {
  338. /*
  339. * We have a struct page covering a hole in the file
  340. * from a read fault and we've raced with a truncate
  341. */
  342. error = -EIO;
  343. goto unlock;
  344. }
  345. } else {
  346. i_mmap_lock_write(mapping);
  347. }
  348. error = get_block(inode, block, &bh, 0);
  349. if (!error && (bh.b_size < PAGE_SIZE))
  350. error = -EIO; /* fs corruption? */
  351. if (error)
  352. goto unlock;
  353. if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) {
  354. if (vmf->flags & FAULT_FLAG_WRITE) {
  355. error = get_block(inode, block, &bh, 1);
  356. count_vm_event(PGMAJFAULT);
  357. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  358. major = VM_FAULT_MAJOR;
  359. if (!error && (bh.b_size < PAGE_SIZE))
  360. error = -EIO;
  361. if (error)
  362. goto unlock;
  363. } else {
  364. i_mmap_unlock_write(mapping);
  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;
  376. vmf->page = page;
  377. if (!page) {
  378. /* Check we didn't race with truncate */
  379. size = (i_size_read(inode) + PAGE_SIZE - 1) >>
  380. PAGE_SHIFT;
  381. if (vmf->pgoff >= size) {
  382. error = -EIO;
  383. goto unlock;
  384. }
  385. }
  386. return VM_FAULT_LOCKED;
  387. }
  388. /* Check we didn't race with a read fault installing a new page */
  389. if (!page && major)
  390. page = find_lock_page(mapping, vmf->pgoff);
  391. if (page) {
  392. unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
  393. PAGE_CACHE_SIZE, 0);
  394. delete_from_page_cache(page);
  395. unlock_page(page);
  396. page_cache_release(page);
  397. }
  398. /*
  399. * If we successfully insert the new mapping over an unwritten extent,
  400. * we need to ensure we convert the unwritten extent. If there is an
  401. * error inserting the mapping, the filesystem needs to leave it as
  402. * unwritten to prevent exposure of the stale underlying data to
  403. * userspace, but we still need to call the completion function so
  404. * the private resources on the mapping buffer can be released. We
  405. * indicate what the callback should do via the uptodate variable, same
  406. * as for normal BH based IO completions.
  407. */
  408. error = dax_insert_mapping(inode, &bh, vma, vmf);
  409. if (buffer_unwritten(&bh)) {
  410. if (complete_unwritten)
  411. complete_unwritten(&bh, !error);
  412. else
  413. WARN_ON_ONCE(!(vmf->flags & FAULT_FLAG_WRITE));
  414. }
  415. if (!page)
  416. i_mmap_unlock_write(mapping);
  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:
  425. if (page) {
  426. unlock_page(page);
  427. page_cache_release(page);
  428. } else {
  429. i_mmap_unlock_write(mapping);
  430. }
  431. goto out;
  432. }
  433. EXPORT_SYMBOL(__dax_fault);
  434. /**
  435. * dax_fault - handle a page fault on a DAX file
  436. * @vma: The virtual memory area where the fault occurred
  437. * @vmf: The description of the fault
  438. * @get_block: The filesystem method used to translate file offsets to blocks
  439. *
  440. * When a page fault occurs, filesystems may call this helper in their
  441. * fault handler for DAX files.
  442. */
  443. int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  444. get_block_t get_block, dax_iodone_t complete_unwritten)
  445. {
  446. int result;
  447. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  448. if (vmf->flags & FAULT_FLAG_WRITE) {
  449. sb_start_pagefault(sb);
  450. file_update_time(vma->vm_file);
  451. }
  452. result = __dax_fault(vma, vmf, get_block, complete_unwritten);
  453. if (vmf->flags & FAULT_FLAG_WRITE)
  454. sb_end_pagefault(sb);
  455. return result;
  456. }
  457. EXPORT_SYMBOL_GPL(dax_fault);
  458. #ifdef CONFIG_TRANSPARENT_HUGEPAGE
  459. /*
  460. * The 'colour' (ie low bits) within a PMD of a page offset. This comes up
  461. * more often than one might expect in the below function.
  462. */
  463. #define PG_PMD_COLOUR ((PMD_SIZE >> PAGE_SHIFT) - 1)
  464. int __dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
  465. pmd_t *pmd, unsigned int flags, get_block_t get_block,
  466. dax_iodone_t complete_unwritten)
  467. {
  468. struct file *file = vma->vm_file;
  469. struct address_space *mapping = file->f_mapping;
  470. struct inode *inode = mapping->host;
  471. struct buffer_head bh;
  472. unsigned blkbits = inode->i_blkbits;
  473. unsigned long pmd_addr = address & PMD_MASK;
  474. bool write = flags & FAULT_FLAG_WRITE;
  475. long length;
  476. void __pmem *kaddr;
  477. pgoff_t size, pgoff;
  478. sector_t block, sector;
  479. unsigned long pfn;
  480. int result = 0;
  481. /* Fall back to PTEs if we're going to COW */
  482. if (write && !(vma->vm_flags & VM_SHARED))
  483. return VM_FAULT_FALLBACK;
  484. /* If the PMD would extend outside the VMA */
  485. if (pmd_addr < vma->vm_start)
  486. return VM_FAULT_FALLBACK;
  487. if ((pmd_addr + PMD_SIZE) > vma->vm_end)
  488. return VM_FAULT_FALLBACK;
  489. pgoff = linear_page_index(vma, pmd_addr);
  490. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  491. if (pgoff >= size)
  492. return VM_FAULT_SIGBUS;
  493. /* If the PMD would cover blocks out of the file */
  494. if ((pgoff | PG_PMD_COLOUR) >= size)
  495. return VM_FAULT_FALLBACK;
  496. memset(&bh, 0, sizeof(bh));
  497. block = (sector_t)pgoff << (PAGE_SHIFT - blkbits);
  498. bh.b_size = PMD_SIZE;
  499. i_mmap_lock_write(mapping);
  500. length = get_block(inode, block, &bh, write);
  501. if (length)
  502. return VM_FAULT_SIGBUS;
  503. /*
  504. * If the filesystem isn't willing to tell us the length of a hole,
  505. * just fall back to PTEs. Calling get_block 512 times in a loop
  506. * would be silly.
  507. */
  508. if (!buffer_size_valid(&bh) || bh.b_size < PMD_SIZE)
  509. goto fallback;
  510. sector = bh.b_blocknr << (blkbits - 9);
  511. if (buffer_unwritten(&bh) || buffer_new(&bh)) {
  512. int i;
  513. length = bdev_direct_access(bh.b_bdev, sector, &kaddr, &pfn,
  514. bh.b_size);
  515. if (length < 0) {
  516. result = VM_FAULT_SIGBUS;
  517. goto out;
  518. }
  519. if ((length < PMD_SIZE) || (pfn & PG_PMD_COLOUR))
  520. goto fallback;
  521. for (i = 0; i < PTRS_PER_PMD; i++)
  522. clear_pmem(kaddr + i * PAGE_SIZE, PAGE_SIZE);
  523. wmb_pmem();
  524. count_vm_event(PGMAJFAULT);
  525. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  526. result |= VM_FAULT_MAJOR;
  527. }
  528. /*
  529. * If we allocated new storage, make sure no process has any
  530. * zero pages covering this hole
  531. */
  532. if (buffer_new(&bh)) {
  533. i_mmap_unlock_write(mapping);
  534. unmap_mapping_range(mapping, pgoff << PAGE_SHIFT, PMD_SIZE, 0);
  535. i_mmap_lock_write(mapping);
  536. }
  537. /*
  538. * If a truncate happened while we were allocating blocks, we may
  539. * leave blocks allocated to the file that are beyond EOF. We can't
  540. * take i_mutex here, so just leave them hanging; they'll be freed
  541. * when the file is deleted.
  542. */
  543. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  544. if (pgoff >= size) {
  545. result = VM_FAULT_SIGBUS;
  546. goto out;
  547. }
  548. if ((pgoff | PG_PMD_COLOUR) >= size)
  549. goto fallback;
  550. if (!write && !buffer_mapped(&bh) && buffer_uptodate(&bh)) {
  551. spinlock_t *ptl;
  552. pmd_t entry;
  553. struct page *zero_page = get_huge_zero_page();
  554. if (unlikely(!zero_page))
  555. goto fallback;
  556. ptl = pmd_lock(vma->vm_mm, pmd);
  557. if (!pmd_none(*pmd)) {
  558. spin_unlock(ptl);
  559. goto fallback;
  560. }
  561. entry = mk_pmd(zero_page, vma->vm_page_prot);
  562. entry = pmd_mkhuge(entry);
  563. set_pmd_at(vma->vm_mm, pmd_addr, pmd, entry);
  564. result = VM_FAULT_NOPAGE;
  565. spin_unlock(ptl);
  566. } else {
  567. length = bdev_direct_access(bh.b_bdev, sector, &kaddr, &pfn,
  568. bh.b_size);
  569. if (length < 0) {
  570. result = VM_FAULT_SIGBUS;
  571. goto out;
  572. }
  573. if ((length < PMD_SIZE) || (pfn & PG_PMD_COLOUR))
  574. goto fallback;
  575. result |= vmf_insert_pfn_pmd(vma, address, pmd, pfn, write);
  576. }
  577. out:
  578. if (buffer_unwritten(&bh))
  579. complete_unwritten(&bh, !(result & VM_FAULT_ERROR));
  580. i_mmap_unlock_write(mapping);
  581. return result;
  582. fallback:
  583. count_vm_event(THP_FAULT_FALLBACK);
  584. result = VM_FAULT_FALLBACK;
  585. goto out;
  586. }
  587. EXPORT_SYMBOL_GPL(__dax_pmd_fault);
  588. /**
  589. * dax_pmd_fault - handle a PMD fault on a DAX file
  590. * @vma: The virtual memory area where the fault occurred
  591. * @vmf: The description of the fault
  592. * @get_block: The filesystem method used to translate file offsets to blocks
  593. *
  594. * When a page fault occurs, filesystems may call this helper in their
  595. * pmd_fault handler for DAX files.
  596. */
  597. int dax_pmd_fault(struct vm_area_struct *vma, unsigned long address,
  598. pmd_t *pmd, unsigned int flags, get_block_t get_block,
  599. dax_iodone_t complete_unwritten)
  600. {
  601. int result;
  602. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  603. if (flags & FAULT_FLAG_WRITE) {
  604. sb_start_pagefault(sb);
  605. file_update_time(vma->vm_file);
  606. }
  607. result = __dax_pmd_fault(vma, address, pmd, flags, get_block,
  608. complete_unwritten);
  609. if (flags & FAULT_FLAG_WRITE)
  610. sb_end_pagefault(sb);
  611. return result;
  612. }
  613. EXPORT_SYMBOL_GPL(dax_pmd_fault);
  614. #endif /* CONFIG_TRANSPARENT_HUGEPAGE */
  615. /**
  616. * dax_pfn_mkwrite - handle first write to DAX page
  617. * @vma: The virtual memory area where the fault occurred
  618. * @vmf: The description of the fault
  619. *
  620. */
  621. int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  622. {
  623. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  624. sb_start_pagefault(sb);
  625. file_update_time(vma->vm_file);
  626. sb_end_pagefault(sb);
  627. return VM_FAULT_NOPAGE;
  628. }
  629. EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
  630. /**
  631. * dax_zero_page_range - zero a range within a page of a DAX file
  632. * @inode: The file being truncated
  633. * @from: The file offset that is being truncated to
  634. * @length: The number of bytes to zero
  635. * @get_block: The filesystem method used to translate file offsets to blocks
  636. *
  637. * This function can be called by a filesystem when it is zeroing part of a
  638. * page in a DAX file. This is intended for hole-punch operations. If
  639. * you are truncating a file, the helper function dax_truncate_page() may be
  640. * more convenient.
  641. *
  642. * We work in terms of PAGE_CACHE_SIZE here for commonality with
  643. * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
  644. * took care of disposing of the unnecessary blocks. Even if the filesystem
  645. * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
  646. * since the file might be mmapped.
  647. */
  648. int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
  649. get_block_t get_block)
  650. {
  651. struct buffer_head bh;
  652. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  653. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  654. int err;
  655. /* Block boundary? Nothing to do */
  656. if (!length)
  657. return 0;
  658. BUG_ON((offset + length) > PAGE_CACHE_SIZE);
  659. memset(&bh, 0, sizeof(bh));
  660. bh.b_size = PAGE_CACHE_SIZE;
  661. err = get_block(inode, index, &bh, 0);
  662. if (err < 0)
  663. return err;
  664. if (buffer_written(&bh)) {
  665. void __pmem *addr;
  666. err = dax_get_addr(&bh, &addr, inode->i_blkbits);
  667. if (err < 0)
  668. return err;
  669. clear_pmem(addr + offset, length);
  670. wmb_pmem();
  671. }
  672. return 0;
  673. }
  674. EXPORT_SYMBOL_GPL(dax_zero_page_range);
  675. /**
  676. * dax_truncate_page - handle a partial page being truncated in a DAX file
  677. * @inode: The file being truncated
  678. * @from: The file offset that is being truncated to
  679. * @get_block: The filesystem method used to translate file offsets to blocks
  680. *
  681. * Similar to block_truncate_page(), this function can be called by a
  682. * filesystem when it is truncating a DAX file to handle the partial page.
  683. *
  684. * We work in terms of PAGE_CACHE_SIZE here for commonality with
  685. * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
  686. * took care of disposing of the unnecessary blocks. Even if the filesystem
  687. * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
  688. * since the file might be mmapped.
  689. */
  690. int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
  691. {
  692. unsigned length = PAGE_CACHE_ALIGN(from) - from;
  693. return dax_zero_page_range(inode, from, length, get_block);
  694. }
  695. EXPORT_SYMBOL_GPL(dax_truncate_page);