dax.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572
  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. *
  284. * When a page fault occurs, filesystems may call this helper in their
  285. * fault handler for DAX files. __dax_fault() assumes the caller has done all
  286. * the necessary locking for the page fault to proceed successfully.
  287. */
  288. int __dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  289. get_block_t get_block, dax_iodone_t complete_unwritten)
  290. {
  291. struct file *file = vma->vm_file;
  292. struct address_space *mapping = file->f_mapping;
  293. struct inode *inode = mapping->host;
  294. struct page *page;
  295. struct buffer_head bh;
  296. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  297. unsigned blkbits = inode->i_blkbits;
  298. sector_t block;
  299. pgoff_t size;
  300. int error;
  301. int major = 0;
  302. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  303. if (vmf->pgoff >= size)
  304. return VM_FAULT_SIGBUS;
  305. memset(&bh, 0, sizeof(bh));
  306. block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
  307. bh.b_size = PAGE_SIZE;
  308. repeat:
  309. page = find_get_page(mapping, vmf->pgoff);
  310. if (page) {
  311. if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
  312. page_cache_release(page);
  313. return VM_FAULT_RETRY;
  314. }
  315. if (unlikely(page->mapping != mapping)) {
  316. unlock_page(page);
  317. page_cache_release(page);
  318. goto repeat;
  319. }
  320. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  321. if (unlikely(vmf->pgoff >= size)) {
  322. /*
  323. * We have a struct page covering a hole in the file
  324. * from a read fault and we've raced with a truncate
  325. */
  326. error = -EIO;
  327. goto unlock_page;
  328. }
  329. }
  330. error = get_block(inode, block, &bh, 0);
  331. if (!error && (bh.b_size < PAGE_SIZE))
  332. error = -EIO; /* fs corruption? */
  333. if (error)
  334. goto unlock_page;
  335. if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) {
  336. if (vmf->flags & FAULT_FLAG_WRITE) {
  337. error = get_block(inode, block, &bh, 1);
  338. count_vm_event(PGMAJFAULT);
  339. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  340. major = VM_FAULT_MAJOR;
  341. if (!error && (bh.b_size < PAGE_SIZE))
  342. error = -EIO;
  343. if (error)
  344. goto unlock_page;
  345. } else {
  346. return dax_load_hole(mapping, page, vmf);
  347. }
  348. }
  349. if (vmf->cow_page) {
  350. struct page *new_page = vmf->cow_page;
  351. if (buffer_written(&bh))
  352. error = copy_user_bh(new_page, &bh, blkbits, vaddr);
  353. else
  354. clear_user_highpage(new_page, vaddr);
  355. if (error)
  356. goto unlock_page;
  357. vmf->page = page;
  358. if (!page) {
  359. i_mmap_lock_read(mapping);
  360. /* Check we didn't race with truncate */
  361. size = (i_size_read(inode) + PAGE_SIZE - 1) >>
  362. PAGE_SHIFT;
  363. if (vmf->pgoff >= size) {
  364. i_mmap_unlock_read(mapping);
  365. error = -EIO;
  366. goto out;
  367. }
  368. }
  369. return VM_FAULT_LOCKED;
  370. }
  371. /* Check we didn't race with a read fault installing a new page */
  372. if (!page && major)
  373. page = find_lock_page(mapping, vmf->pgoff);
  374. if (page) {
  375. unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
  376. PAGE_CACHE_SIZE, 0);
  377. delete_from_page_cache(page);
  378. unlock_page(page);
  379. page_cache_release(page);
  380. }
  381. /*
  382. * If we successfully insert the new mapping over an unwritten extent,
  383. * we need to ensure we convert the unwritten extent. If there is an
  384. * error inserting the mapping, the filesystem needs to leave it as
  385. * unwritten to prevent exposure of the stale underlying data to
  386. * userspace, but we still need to call the completion function so
  387. * the private resources on the mapping buffer can be released. We
  388. * indicate what the callback should do via the uptodate variable, same
  389. * as for normal BH based IO completions.
  390. */
  391. error = dax_insert_mapping(inode, &bh, vma, vmf);
  392. if (buffer_unwritten(&bh))
  393. complete_unwritten(&bh, !error);
  394. out:
  395. if (error == -ENOMEM)
  396. return VM_FAULT_OOM | major;
  397. /* -EBUSY is fine, somebody else faulted on the same PTE */
  398. if ((error < 0) && (error != -EBUSY))
  399. return VM_FAULT_SIGBUS | major;
  400. return VM_FAULT_NOPAGE | major;
  401. unlock_page:
  402. if (page) {
  403. unlock_page(page);
  404. page_cache_release(page);
  405. }
  406. goto out;
  407. }
  408. EXPORT_SYMBOL(__dax_fault);
  409. /**
  410. * dax_fault - handle a page fault on a DAX file
  411. * @vma: The virtual memory area where the fault occurred
  412. * @vmf: The description of the fault
  413. * @get_block: The filesystem method used to translate file offsets to blocks
  414. *
  415. * When a page fault occurs, filesystems may call this helper in their
  416. * fault handler for DAX files.
  417. */
  418. int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  419. get_block_t get_block, dax_iodone_t complete_unwritten)
  420. {
  421. int result;
  422. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  423. if (vmf->flags & FAULT_FLAG_WRITE) {
  424. sb_start_pagefault(sb);
  425. file_update_time(vma->vm_file);
  426. }
  427. result = __dax_fault(vma, vmf, get_block, complete_unwritten);
  428. if (vmf->flags & FAULT_FLAG_WRITE)
  429. sb_end_pagefault(sb);
  430. return result;
  431. }
  432. EXPORT_SYMBOL_GPL(dax_fault);
  433. /**
  434. * dax_pfn_mkwrite - handle first write to DAX page
  435. * @vma: The virtual memory area where the fault occurred
  436. * @vmf: The description of the fault
  437. *
  438. */
  439. int dax_pfn_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
  440. {
  441. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  442. sb_start_pagefault(sb);
  443. file_update_time(vma->vm_file);
  444. sb_end_pagefault(sb);
  445. return VM_FAULT_NOPAGE;
  446. }
  447. EXPORT_SYMBOL_GPL(dax_pfn_mkwrite);
  448. /**
  449. * dax_zero_page_range - zero a range within a page of a DAX file
  450. * @inode: The file being truncated
  451. * @from: The file offset that is being truncated to
  452. * @length: The number of bytes to zero
  453. * @get_block: The filesystem method used to translate file offsets to blocks
  454. *
  455. * This function can be called by a filesystem when it is zeroing part of a
  456. * page in a DAX file. This is intended for hole-punch operations. If
  457. * you are truncating a file, the helper function dax_truncate_page() may be
  458. * more convenient.
  459. *
  460. * We work in terms of PAGE_CACHE_SIZE here for commonality with
  461. * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
  462. * took care of disposing of the unnecessary blocks. Even if the filesystem
  463. * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
  464. * since the file might be mmapped.
  465. */
  466. int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
  467. get_block_t get_block)
  468. {
  469. struct buffer_head bh;
  470. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  471. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  472. int err;
  473. /* Block boundary? Nothing to do */
  474. if (!length)
  475. return 0;
  476. BUG_ON((offset + length) > PAGE_CACHE_SIZE);
  477. memset(&bh, 0, sizeof(bh));
  478. bh.b_size = PAGE_CACHE_SIZE;
  479. err = get_block(inode, index, &bh, 0);
  480. if (err < 0)
  481. return err;
  482. if (buffer_written(&bh)) {
  483. void *addr;
  484. err = dax_get_addr(&bh, &addr, inode->i_blkbits);
  485. if (err < 0)
  486. return err;
  487. memset(addr + offset, 0, length);
  488. }
  489. return 0;
  490. }
  491. EXPORT_SYMBOL_GPL(dax_zero_page_range);
  492. /**
  493. * dax_truncate_page - handle a partial page being truncated in a DAX file
  494. * @inode: The file being truncated
  495. * @from: The file offset that is being truncated to
  496. * @get_block: The filesystem method used to translate file offsets to blocks
  497. *
  498. * Similar to block_truncate_page(), this function can be called by a
  499. * filesystem when it is truncating a DAX file to handle the partial page.
  500. *
  501. * We work in terms of PAGE_CACHE_SIZE here for commonality with
  502. * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
  503. * took care of disposing of the unnecessary blocks. Even if the filesystem
  504. * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
  505. * since the file might be mmapped.
  506. */
  507. int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
  508. {
  509. unsigned length = PAGE_CACHE_ALIGN(from) - from;
  510. return dax_zero_page_range(inode, from, length, get_block);
  511. }
  512. EXPORT_SYMBOL_GPL(dax_truncate_page);