dax.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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/fs.h>
  20. #include <linux/genhd.h>
  21. #include <linux/highmem.h>
  22. #include <linux/memcontrol.h>
  23. #include <linux/mm.h>
  24. #include <linux/mutex.h>
  25. #include <linux/pmem.h>
  26. #include <linux/sched.h>
  27. #include <linux/uio.h>
  28. #include <linux/vmstat.h>
  29. int dax_clear_blocks(struct inode *inode, sector_t block, long size)
  30. {
  31. struct block_device *bdev = inode->i_sb->s_bdev;
  32. sector_t sector = block << (inode->i_blkbits - 9);
  33. might_sleep();
  34. do {
  35. void __pmem *addr;
  36. unsigned long pfn;
  37. long count;
  38. count = bdev_direct_access(bdev, sector, &addr, &pfn, size);
  39. if (count < 0)
  40. return count;
  41. BUG_ON(size < count);
  42. while (count > 0) {
  43. unsigned pgsz = PAGE_SIZE - offset_in_page(addr);
  44. if (pgsz > count)
  45. pgsz = count;
  46. clear_pmem(addr, pgsz);
  47. addr += pgsz;
  48. size -= pgsz;
  49. count -= pgsz;
  50. BUG_ON(pgsz & 511);
  51. sector += pgsz / 512;
  52. cond_resched();
  53. }
  54. } while (size);
  55. wmb_pmem();
  56. return 0;
  57. }
  58. EXPORT_SYMBOL_GPL(dax_clear_blocks);
  59. static long dax_get_addr(struct buffer_head *bh, void __pmem **addr,
  60. unsigned blkbits)
  61. {
  62. unsigned long pfn;
  63. sector_t sector = bh->b_blocknr << (blkbits - 9);
  64. return bdev_direct_access(bh->b_bdev, sector, addr, &pfn, bh->b_size);
  65. }
  66. /* the clear_pmem() calls are ordered by a wmb_pmem() in the caller */
  67. static void dax_new_buf(void __pmem *addr, unsigned size, unsigned first,
  68. loff_t pos, loff_t end)
  69. {
  70. loff_t final = end - pos + first; /* The final byte of the buffer */
  71. if (first > 0)
  72. clear_pmem(addr, first);
  73. if (final < size)
  74. clear_pmem(addr + final, size - final);
  75. }
  76. static bool buffer_written(struct buffer_head *bh)
  77. {
  78. return buffer_mapped(bh) && !buffer_unwritten(bh);
  79. }
  80. /*
  81. * When ext4 encounters a hole, it returns without modifying the buffer_head
  82. * which means that we can't trust b_size. To cope with this, we set b_state
  83. * to 0 before calling get_block and, if any bit is set, we know we can trust
  84. * b_size. Unfortunate, really, since ext4 knows precisely how long a hole is
  85. * and would save us time calling get_block repeatedly.
  86. */
  87. static bool buffer_size_valid(struct buffer_head *bh)
  88. {
  89. return bh->b_state != 0;
  90. }
  91. static ssize_t dax_io(struct inode *inode, struct iov_iter *iter,
  92. loff_t start, loff_t end, get_block_t get_block,
  93. struct buffer_head *bh)
  94. {
  95. ssize_t retval = 0;
  96. loff_t pos = start;
  97. loff_t max = start;
  98. loff_t bh_max = start;
  99. void __pmem *addr;
  100. bool hole = false;
  101. bool need_wmb = false;
  102. if (iov_iter_rw(iter) != WRITE)
  103. end = min(end, i_size_read(inode));
  104. while (pos < end) {
  105. size_t len;
  106. if (pos == max) {
  107. unsigned blkbits = inode->i_blkbits;
  108. sector_t block = pos >> blkbits;
  109. unsigned first = pos - (block << blkbits);
  110. long size;
  111. if (pos == bh_max) {
  112. bh->b_size = PAGE_ALIGN(end - pos);
  113. bh->b_state = 0;
  114. retval = get_block(inode, block, bh,
  115. iov_iter_rw(iter) == WRITE);
  116. if (retval)
  117. break;
  118. if (!buffer_size_valid(bh))
  119. bh->b_size = 1 << blkbits;
  120. bh_max = pos - first + bh->b_size;
  121. } else {
  122. unsigned done = bh->b_size -
  123. (bh_max - (pos - first));
  124. bh->b_blocknr += done >> blkbits;
  125. bh->b_size -= done;
  126. }
  127. hole = iov_iter_rw(iter) != WRITE && !buffer_written(bh);
  128. if (hole) {
  129. addr = NULL;
  130. size = bh->b_size - first;
  131. } else {
  132. retval = dax_get_addr(bh, &addr, blkbits);
  133. if (retval < 0)
  134. break;
  135. if (buffer_unwritten(bh) || buffer_new(bh)) {
  136. dax_new_buf(addr, retval, first, pos,
  137. end);
  138. need_wmb = true;
  139. }
  140. addr += first;
  141. size = retval - first;
  142. }
  143. max = min(pos + size, end);
  144. }
  145. if (iov_iter_rw(iter) == WRITE) {
  146. len = copy_from_iter_pmem(addr, max - pos, iter);
  147. need_wmb = true;
  148. } else if (!hole)
  149. len = copy_to_iter((void __force *)addr, max - pos,
  150. iter);
  151. else
  152. len = iov_iter_zero(max - pos, iter);
  153. if (!len)
  154. break;
  155. pos += len;
  156. addr += len;
  157. }
  158. if (need_wmb)
  159. wmb_pmem();
  160. return (pos == start) ? retval : pos - start;
  161. }
  162. /**
  163. * dax_do_io - Perform I/O to a DAX file
  164. * @iocb: The control block for this I/O
  165. * @inode: The file which the I/O is directed at
  166. * @iter: The addresses to do I/O from or to
  167. * @pos: The file offset where the I/O starts
  168. * @get_block: The filesystem method used to translate file offsets to blocks
  169. * @end_io: A filesystem callback for I/O completion
  170. * @flags: See below
  171. *
  172. * This function uses the same locking scheme as do_blockdev_direct_IO:
  173. * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
  174. * caller for writes. For reads, we take and release the i_mutex ourselves.
  175. * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
  176. * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
  177. * is in progress.
  178. */
  179. ssize_t dax_do_io(struct kiocb *iocb, struct inode *inode,
  180. struct iov_iter *iter, loff_t pos, get_block_t get_block,
  181. dio_iodone_t end_io, int flags)
  182. {
  183. struct buffer_head bh;
  184. ssize_t retval = -EINVAL;
  185. loff_t end = pos + iov_iter_count(iter);
  186. memset(&bh, 0, sizeof(bh));
  187. if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ) {
  188. struct address_space *mapping = inode->i_mapping;
  189. mutex_lock(&inode->i_mutex);
  190. retval = filemap_write_and_wait_range(mapping, pos, end - 1);
  191. if (retval) {
  192. mutex_unlock(&inode->i_mutex);
  193. goto out;
  194. }
  195. }
  196. /* Protects against truncate */
  197. if (!(flags & DIO_SKIP_DIO_COUNT))
  198. inode_dio_begin(inode);
  199. retval = dax_io(inode, iter, pos, end, get_block, &bh);
  200. if ((flags & DIO_LOCKING) && iov_iter_rw(iter) == READ)
  201. mutex_unlock(&inode->i_mutex);
  202. if ((retval > 0) && end_io)
  203. end_io(iocb, pos, retval, bh.b_private);
  204. if (!(flags & DIO_SKIP_DIO_COUNT))
  205. inode_dio_end(inode);
  206. out:
  207. return retval;
  208. }
  209. EXPORT_SYMBOL_GPL(dax_do_io);
  210. /*
  211. * The user has performed a load from a hole in the file. Allocating
  212. * a new page in the file would cause excessive storage usage for
  213. * workloads with sparse files. We allocate a page cache page instead.
  214. * We'll kick it out of the page cache if it's ever written to,
  215. * otherwise it will simply fall out of the page cache under memory
  216. * pressure without ever having been dirtied.
  217. */
  218. static int dax_load_hole(struct address_space *mapping, struct page *page,
  219. struct vm_fault *vmf)
  220. {
  221. unsigned long size;
  222. struct inode *inode = mapping->host;
  223. if (!page)
  224. page = find_or_create_page(mapping, vmf->pgoff,
  225. GFP_KERNEL | __GFP_ZERO);
  226. if (!page)
  227. return VM_FAULT_OOM;
  228. /* Recheck i_size under page lock to avoid truncate race */
  229. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  230. if (vmf->pgoff >= size) {
  231. unlock_page(page);
  232. page_cache_release(page);
  233. return VM_FAULT_SIGBUS;
  234. }
  235. vmf->page = page;
  236. return VM_FAULT_LOCKED;
  237. }
  238. static int copy_user_bh(struct page *to, struct buffer_head *bh,
  239. unsigned blkbits, unsigned long vaddr)
  240. {
  241. void __pmem *vfrom;
  242. void *vto;
  243. if (dax_get_addr(bh, &vfrom, blkbits) < 0)
  244. return -EIO;
  245. vto = kmap_atomic(to);
  246. copy_user_page(vto, (void __force *)vfrom, vaddr, to);
  247. kunmap_atomic(vto);
  248. return 0;
  249. }
  250. static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
  251. struct vm_area_struct *vma, struct vm_fault *vmf)
  252. {
  253. struct address_space *mapping = inode->i_mapping;
  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. i_mmap_lock_read(mapping);
  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. i_mmap_unlock_read(mapping);
  287. return error;
  288. }
  289. /**
  290. * __dax_fault - handle a page fault on a DAX file
  291. * @vma: The virtual memory area where the fault occurred
  292. * @vmf: The description of the fault
  293. * @get_block: The filesystem method used to translate file offsets to blocks
  294. * @complete_unwritten: The filesystem method used to convert unwritten blocks
  295. * to written so the data written to them is exposed. This is required for
  296. * required by write faults for filesystems that will return unwritten
  297. * extent mappings from @get_block, but it is optional for reads as
  298. * dax_insert_mapping() will always zero unwritten blocks. If the fs does
  299. * not support unwritten extents, the it should pass NULL.
  300. *
  301. * When a page fault occurs, filesystems may call this helper in their
  302. * fault handler for DAX files. __dax_fault() assumes the caller has done all
  303. * the necessary locking for the page fault to proceed successfully.
  304. */
  305. int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  306. get_block_t get_block, dax_iodone_t complete_unwritten)
  307. {
  308. struct file *file = vma->vm_file;
  309. struct address_space *mapping = file->f_mapping;
  310. struct inode *inode = mapping->host;
  311. struct page *page;
  312. struct buffer_head bh;
  313. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  314. unsigned blkbits = inode->i_blkbits;
  315. sector_t block;
  316. pgoff_t size;
  317. int error;
  318. int major = 0;
  319. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  320. if (vmf->pgoff >= size)
  321. return VM_FAULT_SIGBUS;
  322. memset(&bh, 0, sizeof(bh));
  323. block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
  324. bh.b_size = PAGE_SIZE;
  325. repeat:
  326. page = find_get_page(mapping, vmf->pgoff);
  327. if (page) {
  328. if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
  329. page_cache_release(page);
  330. return VM_FAULT_RETRY;
  331. }
  332. if (unlikely(page->mapping != mapping)) {
  333. unlock_page(page);
  334. page_cache_release(page);
  335. goto repeat;
  336. }
  337. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  338. if (unlikely(vmf->pgoff >= size)) {
  339. /*
  340. * We have a struct page covering a hole in the file
  341. * from a read fault and we've raced with a truncate
  342. */
  343. error = -EIO;
  344. goto unlock_page;
  345. }
  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_page;
  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_page;
  362. } else {
  363. return dax_load_hole(mapping, page, vmf);
  364. }
  365. }
  366. if (vmf->cow_page) {
  367. struct page *new_page = vmf->cow_page;
  368. if (buffer_written(&bh))
  369. error = copy_user_bh(new_page, &bh, blkbits, vaddr);
  370. else
  371. clear_user_highpage(new_page, vaddr);
  372. if (error)
  373. goto unlock_page;
  374. vmf->page = page;
  375. if (!page) {
  376. i_mmap_lock_read(mapping);
  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. i_mmap_unlock_read(mapping);
  382. error = -EIO;
  383. goto out;
  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. out:
  416. if (error == -ENOMEM)
  417. return VM_FAULT_OOM | major;
  418. /* -EBUSY is fine, somebody else faulted on the same PTE */
  419. if ((error < 0) && (error != -EBUSY))
  420. return VM_FAULT_SIGBUS | major;
  421. return VM_FAULT_NOPAGE | major;
  422. unlock_page:
  423. if (page) {
  424. unlock_page(page);
  425. page_cache_release(page);
  426. }
  427. goto out;
  428. }
  429. EXPORT_SYMBOL(__dax_fault);
  430. /**
  431. * dax_fault - handle a page fault on a DAX file
  432. * @vma: The virtual memory area where the fault occurred
  433. * @vmf: The description of the fault
  434. * @get_block: The filesystem method used to translate file offsets to blocks
  435. *
  436. * When a page fault occurs, filesystems may call this helper in their
  437. * fault handler for DAX files.
  438. */
  439. int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  440. get_block_t get_block, dax_iodone_t complete_unwritten)
  441. {
  442. int result;
  443. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  444. if (vmf->flags & FAULT_FLAG_WRITE) {
  445. sb_start_pagefault(sb);
  446. file_update_time(vma->vm_file);
  447. }
  448. result = __dax_fault(vma, vmf, get_block, complete_unwritten);
  449. if (vmf->flags & FAULT_FLAG_WRITE)
  450. sb_end_pagefault(sb);
  451. return result;
  452. }
  453. EXPORT_SYMBOL_GPL(dax_fault);
  454. /**
  455. * dax_pfn_mkwrite - handle first write to DAX page
  456. * @vma: The virtual memory area where the fault occurred
  457. * @vmf: The description of the fault
  458. *
  459. */
  460. int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  461. {
  462. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  463. sb_start_pagefault(sb);
  464. file_update_time(vma->vm_file);
  465. sb_end_pagefault(sb);
  466. return VM_FAULT_NOPAGE;
  467. }
  468. EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
  469. /**
  470. * dax_zero_page_range - zero a range within a page of a DAX file
  471. * @inode: The file being truncated
  472. * @from: The file offset that is being truncated to
  473. * @length: The number of bytes to zero
  474. * @get_block: The filesystem method used to translate file offsets to blocks
  475. *
  476. * This function can be called by a filesystem when it is zeroing part of a
  477. * page in a DAX file. This is intended for hole-punch operations. If
  478. * you are truncating a file, the helper function dax_truncate_page() may be
  479. * more convenient.
  480. *
  481. * We work in terms of PAGE_CACHE_SIZE here for commonality with
  482. * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
  483. * took care of disposing of the unnecessary blocks. Even if the filesystem
  484. * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
  485. * since the file might be mmapped.
  486. */
  487. int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
  488. get_block_t get_block)
  489. {
  490. struct buffer_head bh;
  491. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  492. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  493. int err;
  494. /* Block boundary? Nothing to do */
  495. if (!length)
  496. return 0;
  497. BUG_ON((offset + length) > PAGE_CACHE_SIZE);
  498. memset(&bh, 0, sizeof(bh));
  499. bh.b_size = PAGE_CACHE_SIZE;
  500. err = get_block(inode, index, &bh, 0);
  501. if (err < 0)
  502. return err;
  503. if (buffer_written(&bh)) {
  504. void __pmem *addr;
  505. err = dax_get_addr(&bh, &addr, inode->i_blkbits);
  506. if (err < 0)
  507. return err;
  508. clear_pmem(addr + offset, length);
  509. wmb_pmem();
  510. }
  511. return 0;
  512. }
  513. EXPORT_SYMBOL_GPL(dax_zero_page_range);
  514. /**
  515. * dax_truncate_page - handle a partial page being truncated in a DAX file
  516. * @inode: The file being truncated
  517. * @from: The file offset that is being truncated to
  518. * @get_block: The filesystem method used to translate file offsets to blocks
  519. *
  520. * Similar to block_truncate_page(), this function can be called by a
  521. * filesystem when it is truncating a DAX file to handle the partial page.
  522. *
  523. * We work in terms of PAGE_CACHE_SIZE here for commonality with
  524. * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
  525. * took care of disposing of the unnecessary blocks. Even if the filesystem
  526. * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
  527. * since the file might be mmapped.
  528. */
  529. int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
  530. {
  531. unsigned length = PAGE_CACHE_ALIGN(from) - from;
  532. return dax_zero_page_range(inode, from, length, get_block);
  533. }
  534. EXPORT_SYMBOL_GPL(dax_truncate_page);