dax.c 15 KB

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