dax.c 17 KB

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