dax.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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(int rw, 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 (rw != 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. rw == 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 = (rw != 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 (rw == WRITE)
  142. len = copy_from_iter(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. * @rw: READ to read or WRITE to write
  157. * @iocb: The control block for this I/O
  158. * @inode: The file which the I/O is directed at
  159. * @iter: The addresses to do I/O from or to
  160. * @pos: The file offset where the I/O starts
  161. * @get_block: The filesystem method used to translate file offsets to blocks
  162. * @end_io: A filesystem callback for I/O completion
  163. * @flags: See below
  164. *
  165. * This function uses the same locking scheme as do_blockdev_direct_IO:
  166. * If @flags has DIO_LOCKING set, we assume that the i_mutex is held by the
  167. * caller for writes. For reads, we take and release the i_mutex ourselves.
  168. * If DIO_LOCKING is not set, the filesystem takes care of its own locking.
  169. * As with do_blockdev_direct_IO(), we increment i_dio_count while the I/O
  170. * is in progress.
  171. */
  172. ssize_t dax_do_io(int rw, struct kiocb *iocb, struct inode *inode,
  173. struct iov_iter *iter, loff_t pos,
  174. get_block_t get_block, dio_iodone_t end_io, int flags)
  175. {
  176. struct buffer_head bh;
  177. ssize_t retval = -EINVAL;
  178. loff_t end = pos + iov_iter_count(iter);
  179. memset(&bh, 0, sizeof(bh));
  180. if ((flags & DIO_LOCKING) && (rw == READ)) {
  181. struct address_space *mapping = inode->i_mapping;
  182. mutex_lock(&inode->i_mutex);
  183. retval = filemap_write_and_wait_range(mapping, pos, end - 1);
  184. if (retval) {
  185. mutex_unlock(&inode->i_mutex);
  186. goto out;
  187. }
  188. }
  189. /* Protects against truncate */
  190. atomic_inc(&inode->i_dio_count);
  191. retval = dax_io(rw, inode, iter, pos, end, get_block, &bh);
  192. if ((flags & DIO_LOCKING) && (rw == READ))
  193. mutex_unlock(&inode->i_mutex);
  194. if ((retval > 0) && end_io)
  195. end_io(iocb, pos, retval, bh.b_private);
  196. inode_dio_done(inode);
  197. out:
  198. return retval;
  199. }
  200. EXPORT_SYMBOL_GPL(dax_do_io);
  201. /*
  202. * The user has performed a load from a hole in the file. Allocating
  203. * a new page in the file would cause excessive storage usage for
  204. * workloads with sparse files. We allocate a page cache page instead.
  205. * We'll kick it out of the page cache if it's ever written to,
  206. * otherwise it will simply fall out of the page cache under memory
  207. * pressure without ever having been dirtied.
  208. */
  209. static int dax_load_hole(struct address_space *mapping, struct page *page,
  210. struct vm_fault *vmf)
  211. {
  212. unsigned long size;
  213. struct inode *inode = mapping->host;
  214. if (!page)
  215. page = find_or_create_page(mapping, vmf->pgoff,
  216. GFP_KERNEL | __GFP_ZERO);
  217. if (!page)
  218. return VM_FAULT_OOM;
  219. /* Recheck i_size under page lock to avoid truncate race */
  220. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  221. if (vmf->pgoff >= size) {
  222. unlock_page(page);
  223. page_cache_release(page);
  224. return VM_FAULT_SIGBUS;
  225. }
  226. vmf->page = page;
  227. return VM_FAULT_LOCKED;
  228. }
  229. static int copy_user_bh(struct page *to, struct buffer_head *bh,
  230. unsigned blkbits, unsigned long vaddr)
  231. {
  232. void *vfrom, *vto;
  233. if (dax_get_addr(bh, &vfrom, blkbits) < 0)
  234. return -EIO;
  235. vto = kmap_atomic(to);
  236. copy_user_page(vto, vfrom, vaddr, to);
  237. kunmap_atomic(vto);
  238. return 0;
  239. }
  240. static int dax_insert_mapping(struct inode *inode, struct buffer_head *bh,
  241. struct vm_area_struct *vma, struct vm_fault *vmf)
  242. {
  243. struct address_space *mapping = inode->i_mapping;
  244. sector_t sector = bh->b_blocknr << (inode->i_blkbits - 9);
  245. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  246. void *addr;
  247. unsigned long pfn;
  248. pgoff_t size;
  249. int error;
  250. i_mmap_lock_read(mapping);
  251. /*
  252. * Check truncate didn't happen while we were allocating a block.
  253. * If it did, this block may or may not be still allocated to the
  254. * file. We can't tell the filesystem to free it because we can't
  255. * take i_mutex here. In the worst case, the file still has blocks
  256. * allocated past the end of the file.
  257. */
  258. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  259. if (unlikely(vmf->pgoff >= size)) {
  260. error = -EIO;
  261. goto out;
  262. }
  263. error = bdev_direct_access(bh->b_bdev, sector, &addr, &pfn, bh->b_size);
  264. if (error < 0)
  265. goto out;
  266. if (error < PAGE_SIZE) {
  267. error = -EIO;
  268. goto out;
  269. }
  270. if (buffer_unwritten(bh) || buffer_new(bh))
  271. clear_page(addr);
  272. error = vm_insert_mixed(vma, vaddr, pfn);
  273. out:
  274. i_mmap_unlock_read(mapping);
  275. if (bh->b_end_io)
  276. bh->b_end_io(bh, 1);
  277. return error;
  278. }
  279. static int do_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  280. get_block_t get_block)
  281. {
  282. struct file *file = vma->vm_file;
  283. struct address_space *mapping = file->f_mapping;
  284. struct inode *inode = mapping->host;
  285. struct page *page;
  286. struct buffer_head bh;
  287. unsigned long vaddr = (unsigned long)vmf->virtual_address;
  288. unsigned blkbits = inode->i_blkbits;
  289. sector_t block;
  290. pgoff_t size;
  291. int error;
  292. int major = 0;
  293. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  294. if (vmf->pgoff >= size)
  295. return VM_FAULT_SIGBUS;
  296. memset(&bh, 0, sizeof(bh));
  297. block = (sector_t)vmf->pgoff << (PAGE_SHIFT - blkbits);
  298. bh.b_size = PAGE_SIZE;
  299. repeat:
  300. page = find_get_page(mapping, vmf->pgoff);
  301. if (page) {
  302. if (!lock_page_or_retry(page, vma->vm_mm, vmf->flags)) {
  303. page_cache_release(page);
  304. return VM_FAULT_RETRY;
  305. }
  306. if (unlikely(page->mapping != mapping)) {
  307. unlock_page(page);
  308. page_cache_release(page);
  309. goto repeat;
  310. }
  311. size = (i_size_read(inode) + PAGE_SIZE - 1) >> PAGE_SHIFT;
  312. if (unlikely(vmf->pgoff >= size)) {
  313. /*
  314. * We have a struct page covering a hole in the file
  315. * from a read fault and we've raced with a truncate
  316. */
  317. error = -EIO;
  318. goto unlock_page;
  319. }
  320. }
  321. error = get_block(inode, block, &bh, 0);
  322. if (!error && (bh.b_size < PAGE_SIZE))
  323. error = -EIO; /* fs corruption? */
  324. if (error)
  325. goto unlock_page;
  326. if (!buffer_mapped(&bh) && !buffer_unwritten(&bh) && !vmf->cow_page) {
  327. if (vmf->flags & FAULT_FLAG_WRITE) {
  328. error = get_block(inode, block, &bh, 1);
  329. count_vm_event(PGMAJFAULT);
  330. mem_cgroup_count_vm_event(vma->vm_mm, PGMAJFAULT);
  331. major = VM_FAULT_MAJOR;
  332. if (!error && (bh.b_size < PAGE_SIZE))
  333. error = -EIO;
  334. if (error)
  335. goto unlock_page;
  336. } else {
  337. return dax_load_hole(mapping, page, vmf);
  338. }
  339. }
  340. if (vmf->cow_page) {
  341. struct page *new_page = vmf->cow_page;
  342. if (buffer_written(&bh))
  343. error = copy_user_bh(new_page, &bh, blkbits, vaddr);
  344. else
  345. clear_user_highpage(new_page, vaddr);
  346. if (error)
  347. goto unlock_page;
  348. vmf->page = page;
  349. if (!page) {
  350. i_mmap_lock_read(mapping);
  351. /* Check we didn't race with truncate */
  352. size = (i_size_read(inode) + PAGE_SIZE - 1) >>
  353. PAGE_SHIFT;
  354. if (vmf->pgoff >= size) {
  355. i_mmap_unlock_read(mapping);
  356. error = -EIO;
  357. goto out;
  358. }
  359. }
  360. return VM_FAULT_LOCKED;
  361. }
  362. /* Check we didn't race with a read fault installing a new page */
  363. if (!page && major)
  364. page = find_lock_page(mapping, vmf->pgoff);
  365. if (page) {
  366. unmap_mapping_range(mapping, vmf->pgoff << PAGE_SHIFT,
  367. PAGE_CACHE_SIZE, 0);
  368. delete_from_page_cache(page);
  369. unlock_page(page);
  370. page_cache_release(page);
  371. }
  372. error = dax_insert_mapping(inode, &bh, vma, vmf);
  373. out:
  374. if (error == -ENOMEM)
  375. return VM_FAULT_OOM | major;
  376. /* -EBUSY is fine, somebody else faulted on the same PTE */
  377. if ((error < 0) && (error != -EBUSY))
  378. return VM_FAULT_SIGBUS | major;
  379. return VM_FAULT_NOPAGE | major;
  380. unlock_page:
  381. if (page) {
  382. unlock_page(page);
  383. page_cache_release(page);
  384. }
  385. goto out;
  386. }
  387. /**
  388. * dax_fault - handle a page fault on a DAX file
  389. * @vma: The virtual memory area where the fault occurred
  390. * @vmf: The description of the fault
  391. * @get_block: The filesystem method used to translate file offsets to blocks
  392. *
  393. * When a page fault occurs, filesystems may call this helper in their
  394. * fault handler for DAX files.
  395. */
  396. int dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf,
  397. get_block_t get_block)
  398. {
  399. int result;
  400. struct super_block *sb = file_inode(vma->vm_file)->i_sb;
  401. if (vmf->flags & FAULT_FLAG_WRITE) {
  402. sb_start_pagefault(sb);
  403. file_update_time(vma->vm_file);
  404. }
  405. result = do_dax_fault(vma, vmf, get_block);
  406. if (vmf->flags & FAULT_FLAG_WRITE)
  407. sb_end_pagefault(sb);
  408. return result;
  409. }
  410. EXPORT_SYMBOL_GPL(dax_fault);
  411. /**
  412. * dax_zero_page_range - zero a range within a page of a DAX file
  413. * @inode: The file being truncated
  414. * @from: The file offset that is being truncated to
  415. * @length: The number of bytes to zero
  416. * @get_block: The filesystem method used to translate file offsets to blocks
  417. *
  418. * This function can be called by a filesystem when it is zeroing part of a
  419. * page in a DAX file. This is intended for hole-punch operations. If
  420. * you are truncating a file, the helper function dax_truncate_page() may be
  421. * more convenient.
  422. *
  423. * We work in terms of PAGE_CACHE_SIZE here for commonality with
  424. * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
  425. * took care of disposing of the unnecessary blocks. Even if the filesystem
  426. * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
  427. * since the file might be mmapped.
  428. */
  429. int dax_zero_page_range(struct inode *inode, loff_t from, unsigned length,
  430. get_block_t get_block)
  431. {
  432. struct buffer_head bh;
  433. pgoff_t index = from >> PAGE_CACHE_SHIFT;
  434. unsigned offset = from & (PAGE_CACHE_SIZE-1);
  435. int err;
  436. /* Block boundary? Nothing to do */
  437. if (!length)
  438. return 0;
  439. BUG_ON((offset + length) > PAGE_CACHE_SIZE);
  440. memset(&bh, 0, sizeof(bh));
  441. bh.b_size = PAGE_CACHE_SIZE;
  442. err = get_block(inode, index, &bh, 0);
  443. if (err < 0)
  444. return err;
  445. if (buffer_written(&bh)) {
  446. void *addr;
  447. err = dax_get_addr(&bh, &addr, inode->i_blkbits);
  448. if (err < 0)
  449. return err;
  450. memset(addr + offset, 0, length);
  451. }
  452. return 0;
  453. }
  454. EXPORT_SYMBOL_GPL(dax_zero_page_range);
  455. /**
  456. * dax_truncate_page - handle a partial page being truncated in a DAX file
  457. * @inode: The file being truncated
  458. * @from: The file offset that is being truncated to
  459. * @get_block: The filesystem method used to translate file offsets to blocks
  460. *
  461. * Similar to block_truncate_page(), this function can be called by a
  462. * filesystem when it is truncating a DAX file to handle the partial page.
  463. *
  464. * We work in terms of PAGE_CACHE_SIZE here for commonality with
  465. * block_truncate_page(), but we could go down to PAGE_SIZE if the filesystem
  466. * took care of disposing of the unnecessary blocks. Even if the filesystem
  467. * block size is smaller than PAGE_SIZE, we have to zero the rest of the page
  468. * since the file might be mmapped.
  469. */
  470. int dax_truncate_page(struct inode *inode, loff_t from, get_block_t get_block)
  471. {
  472. unsigned length = PAGE_CACHE_ALIGN(from) - from;
  473. return dax_zero_page_range(inode, from, length, get_block);
  474. }
  475. EXPORT_SYMBOL_GPL(dax_truncate_page);