1eda14cbcSMatt Macy /* 2eda14cbcSMatt Macy * Copyright (c) 2020 iXsystems, Inc. 3eda14cbcSMatt Macy * All rights reserved. 4eda14cbcSMatt Macy * 5eda14cbcSMatt Macy * Redistribution and use in source and binary forms, with or without 6eda14cbcSMatt Macy * modification, are permitted provided that the following conditions 7eda14cbcSMatt Macy * are met: 8eda14cbcSMatt Macy * 1. Redistributions of source code must retain the above copyright 9eda14cbcSMatt Macy * notice, this list of conditions and the following disclaimer. 10eda14cbcSMatt Macy * 2. Redistributions in binary form must reproduce the above copyright 11eda14cbcSMatt Macy * notice, this list of conditions and the following disclaimer in the 12eda14cbcSMatt Macy * documentation and/or other materials provided with the distribution. 13eda14cbcSMatt Macy * 14eda14cbcSMatt Macy * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND 15eda14cbcSMatt Macy * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16eda14cbcSMatt Macy * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17eda14cbcSMatt Macy * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE 18eda14cbcSMatt Macy * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19eda14cbcSMatt Macy * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20eda14cbcSMatt Macy * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21eda14cbcSMatt Macy * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22eda14cbcSMatt Macy * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23eda14cbcSMatt Macy * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24eda14cbcSMatt Macy * SUCH DAMAGE. 25eda14cbcSMatt Macy * 26eda14cbcSMatt Macy */ 27eda14cbcSMatt Macy 28eda14cbcSMatt Macy #include <sys/types.h> 29eda14cbcSMatt Macy #include <sys/param.h> 30eda14cbcSMatt Macy #include <sys/dmu.h> 31eda14cbcSMatt Macy #include <sys/dmu_impl.h> 32eda14cbcSMatt Macy #include <sys/dmu_tx.h> 33eda14cbcSMatt Macy #include <sys/dbuf.h> 34eda14cbcSMatt Macy #include <sys/dnode.h> 35eda14cbcSMatt Macy #include <sys/zfs_context.h> 36eda14cbcSMatt Macy #include <sys/dmu_objset.h> 37eda14cbcSMatt Macy #include <sys/dmu_traverse.h> 38eda14cbcSMatt Macy #include <sys/dsl_dataset.h> 39eda14cbcSMatt Macy #include <sys/dsl_dir.h> 40eda14cbcSMatt Macy #include <sys/dsl_pool.h> 41eda14cbcSMatt Macy #include <sys/dsl_synctask.h> 42eda14cbcSMatt Macy #include <sys/dsl_prop.h> 43eda14cbcSMatt Macy #include <sys/dmu_zfetch.h> 44eda14cbcSMatt Macy #include <sys/zfs_ioctl.h> 45eda14cbcSMatt Macy #include <sys/zap.h> 46eda14cbcSMatt Macy #include <sys/zio_checksum.h> 47eda14cbcSMatt Macy #include <sys/zio_compress.h> 48eda14cbcSMatt Macy #include <sys/sa.h> 49eda14cbcSMatt Macy #include <sys/zfeature.h> 50eda14cbcSMatt Macy #include <sys/abd.h> 51eda14cbcSMatt Macy #include <sys/zfs_rlock.h> 52eda14cbcSMatt Macy #include <sys/racct.h> 53eda14cbcSMatt Macy #include <sys/vm.h> 54eda14cbcSMatt Macy #include <sys/zfs_znode.h> 55eda14cbcSMatt Macy #include <sys/zfs_vnops.h> 56eda14cbcSMatt Macy 57eda14cbcSMatt Macy #include <sys/ccompat.h> 58eda14cbcSMatt Macy 59eda14cbcSMatt Macy #ifndef IDX_TO_OFF 60eda14cbcSMatt Macy #define IDX_TO_OFF(idx) (((vm_ooffset_t)(idx)) << PAGE_SHIFT) 61eda14cbcSMatt Macy #endif 62eda14cbcSMatt Macy 63eda14cbcSMatt Macy #define VM_ALLOC_BUSY_FLAGS VM_ALLOC_SBUSY | VM_ALLOC_IGN_SBUSY 64eda14cbcSMatt Macy 65eda14cbcSMatt Macy int 66eda14cbcSMatt Macy dmu_write_pages(objset_t *os, uint64_t object, uint64_t offset, uint64_t size, 67eda14cbcSMatt Macy vm_page_t *ma, dmu_tx_t *tx) 68eda14cbcSMatt Macy { 69eda14cbcSMatt Macy dmu_buf_t **dbp; 70eda14cbcSMatt Macy struct sf_buf *sf; 71eda14cbcSMatt Macy int numbufs, i; 72eda14cbcSMatt Macy int err; 73eda14cbcSMatt Macy 74eda14cbcSMatt Macy if (size == 0) 75eda14cbcSMatt Macy return (0); 76eda14cbcSMatt Macy 77eda14cbcSMatt Macy err = dmu_buf_hold_array(os, object, offset, size, 78eda14cbcSMatt Macy FALSE, FTAG, &numbufs, &dbp); 79eda14cbcSMatt Macy if (err) 80eda14cbcSMatt Macy return (err); 81eda14cbcSMatt Macy 82eda14cbcSMatt Macy for (i = 0; i < numbufs; i++) { 83eda14cbcSMatt Macy int tocpy, copied, thiscpy; 84eda14cbcSMatt Macy int bufoff; 85eda14cbcSMatt Macy dmu_buf_t *db = dbp[i]; 86eda14cbcSMatt Macy caddr_t va; 87eda14cbcSMatt Macy 8816038816SMartin Matuska ASSERT3U(size, >, 0); 89eda14cbcSMatt Macy ASSERT3U(db->db_size, >=, PAGESIZE); 90eda14cbcSMatt Macy 91eda14cbcSMatt Macy bufoff = offset - db->db_offset; 92eda14cbcSMatt Macy tocpy = (int)MIN(db->db_size - bufoff, size); 93eda14cbcSMatt Macy 94eda14cbcSMatt Macy ASSERT(i == 0 || i == numbufs-1 || tocpy == db->db_size); 95eda14cbcSMatt Macy 96eda14cbcSMatt Macy if (tocpy == db->db_size) 97188408daSMartin Matuska dmu_buf_will_fill(db, tx, B_FALSE); 98eda14cbcSMatt Macy else 99eda14cbcSMatt Macy dmu_buf_will_dirty(db, tx); 100eda14cbcSMatt Macy 101eda14cbcSMatt Macy for (copied = 0; copied < tocpy; copied += PAGESIZE) { 102eda14cbcSMatt Macy ASSERT3U(ptoa((*ma)->pindex), ==, 103eda14cbcSMatt Macy db->db_offset + bufoff); 104eda14cbcSMatt Macy thiscpy = MIN(PAGESIZE, tocpy - copied); 105eda14cbcSMatt Macy va = zfs_map_page(*ma, &sf); 106*17aab35aSMartin Matuska ASSERT(db->db_data != NULL); 107da5137abSMartin Matuska memcpy((char *)db->db_data + bufoff, va, thiscpy); 108eda14cbcSMatt Macy zfs_unmap_page(sf); 109eda14cbcSMatt Macy ma += 1; 110eda14cbcSMatt Macy bufoff += PAGESIZE; 111eda14cbcSMatt Macy } 112eda14cbcSMatt Macy 113eda14cbcSMatt Macy if (tocpy == db->db_size) 114188408daSMartin Matuska dmu_buf_fill_done(db, tx, B_FALSE); 115eda14cbcSMatt Macy 116eda14cbcSMatt Macy offset += tocpy; 117eda14cbcSMatt Macy size -= tocpy; 118eda14cbcSMatt Macy } 119eda14cbcSMatt Macy dmu_buf_rele_array(dbp, numbufs, FTAG); 120eda14cbcSMatt Macy return (err); 121eda14cbcSMatt Macy } 122eda14cbcSMatt Macy 123eda14cbcSMatt Macy int 124eda14cbcSMatt Macy dmu_read_pages(objset_t *os, uint64_t object, vm_page_t *ma, int count, 125eda14cbcSMatt Macy int *rbehind, int *rahead, int last_size) 126eda14cbcSMatt Macy { 127eda14cbcSMatt Macy struct sf_buf *sf; 128eda14cbcSMatt Macy vm_object_t vmobj; 129eda14cbcSMatt Macy vm_page_t m; 130eda14cbcSMatt Macy dmu_buf_t **dbp; 131eda14cbcSMatt Macy dmu_buf_t *db; 132eda14cbcSMatt Macy caddr_t va; 133eda14cbcSMatt Macy int numbufs, i; 134eda14cbcSMatt Macy int bufoff, pgoff, tocpy; 135eda14cbcSMatt Macy int mi, di; 136eda14cbcSMatt Macy int err; 137eda14cbcSMatt Macy 138eda14cbcSMatt Macy ASSERT3U(ma[0]->pindex + count - 1, ==, ma[count - 1]->pindex); 13916038816SMartin Matuska ASSERT3S(last_size, <=, PAGE_SIZE); 140eda14cbcSMatt Macy 141eda14cbcSMatt Macy err = dmu_buf_hold_array(os, object, IDX_TO_OFF(ma[0]->pindex), 142eda14cbcSMatt Macy IDX_TO_OFF(count - 1) + last_size, TRUE, FTAG, &numbufs, &dbp); 143eda14cbcSMatt Macy if (err != 0) 144eda14cbcSMatt Macy return (err); 145eda14cbcSMatt Macy 146eda14cbcSMatt Macy #ifdef ZFS_DEBUG 147eda14cbcSMatt Macy IMPLY(last_size < PAGE_SIZE, *rahead == 0); 148eda14cbcSMatt Macy if (dbp[0]->db_offset != 0 || numbufs > 1) { 149eda14cbcSMatt Macy for (i = 0; i < numbufs; i++) { 150eda14cbcSMatt Macy ASSERT(ISP2(dbp[i]->db_size)); 15116038816SMartin Matuska ASSERT3U((dbp[i]->db_offset % dbp[i]->db_size), ==, 0); 152eda14cbcSMatt Macy ASSERT3U(dbp[i]->db_size, ==, dbp[0]->db_size); 153eda14cbcSMatt Macy } 154eda14cbcSMatt Macy } 155eda14cbcSMatt Macy #endif 156eda14cbcSMatt Macy 157eda14cbcSMatt Macy vmobj = ma[0]->object; 158eda14cbcSMatt Macy 159eda14cbcSMatt Macy db = dbp[0]; 160eda14cbcSMatt Macy for (i = 0; i < *rbehind; i++) { 161eda14cbcSMatt Macy m = vm_page_grab_unlocked(vmobj, ma[0]->pindex - 1 - i, 162eda14cbcSMatt Macy VM_ALLOC_NORMAL | VM_ALLOC_NOWAIT | VM_ALLOC_BUSY_FLAGS); 163eda14cbcSMatt Macy if (m == NULL) 164eda14cbcSMatt Macy break; 165eda14cbcSMatt Macy if (!vm_page_none_valid(m)) { 166eda14cbcSMatt Macy ASSERT3U(m->valid, ==, VM_PAGE_BITS_ALL); 167ce4dcb97SMartin Matuska vm_page_sunbusy(m); 168eda14cbcSMatt Macy break; 169eda14cbcSMatt Macy } 17016038816SMartin Matuska ASSERT3U(m->dirty, ==, 0); 171eda14cbcSMatt Macy ASSERT(!pmap_page_is_write_mapped(m)); 172eda14cbcSMatt Macy 17316038816SMartin Matuska ASSERT3U(db->db_size, >, PAGE_SIZE); 174eda14cbcSMatt Macy bufoff = IDX_TO_OFF(m->pindex) % db->db_size; 175eda14cbcSMatt Macy va = zfs_map_page(m, &sf); 176*17aab35aSMartin Matuska ASSERT(db->db_data != NULL); 177da5137abSMartin Matuska memcpy(va, (char *)db->db_data + bufoff, PAGESIZE); 178eda14cbcSMatt Macy zfs_unmap_page(sf); 179eda14cbcSMatt Macy vm_page_valid(m); 180eda14cbcSMatt Macy if ((m->busy_lock & VPB_BIT_WAITERS) != 0) 181eda14cbcSMatt Macy vm_page_activate(m); 182eda14cbcSMatt Macy else 183eda14cbcSMatt Macy vm_page_deactivate(m); 184ce4dcb97SMartin Matuska vm_page_sunbusy(m); 185eda14cbcSMatt Macy } 186eda14cbcSMatt Macy *rbehind = i; 187eda14cbcSMatt Macy 188eda14cbcSMatt Macy bufoff = IDX_TO_OFF(ma[0]->pindex) % db->db_size; 189eda14cbcSMatt Macy pgoff = 0; 190eda14cbcSMatt Macy for (mi = 0, di = 0; mi < count && di < numbufs; ) { 191eda14cbcSMatt Macy if (pgoff == 0) { 192eda14cbcSMatt Macy m = ma[mi]; 193eda14cbcSMatt Macy if (m != bogus_page) { 194eda14cbcSMatt Macy vm_page_assert_xbusied(m); 195eda14cbcSMatt Macy ASSERT(vm_page_none_valid(m)); 19616038816SMartin Matuska ASSERT3U(m->dirty, ==, 0); 197eda14cbcSMatt Macy ASSERT(!pmap_page_is_write_mapped(m)); 198eda14cbcSMatt Macy va = zfs_map_page(m, &sf); 199eda14cbcSMatt Macy } 200eda14cbcSMatt Macy } 201eda14cbcSMatt Macy if (bufoff == 0) 202eda14cbcSMatt Macy db = dbp[di]; 203eda14cbcSMatt Macy 204eda14cbcSMatt Macy if (m != bogus_page) { 205eda14cbcSMatt Macy ASSERT3U(IDX_TO_OFF(m->pindex) + pgoff, ==, 206eda14cbcSMatt Macy db->db_offset + bufoff); 207eda14cbcSMatt Macy } 208eda14cbcSMatt Macy 209eda14cbcSMatt Macy /* 210eda14cbcSMatt Macy * We do not need to clamp the copy size by the file 211eda14cbcSMatt Macy * size as the last block is zero-filled beyond the 212eda14cbcSMatt Macy * end of file anyway. 213eda14cbcSMatt Macy */ 214eda14cbcSMatt Macy tocpy = MIN(db->db_size - bufoff, PAGESIZE - pgoff); 21516038816SMartin Matuska ASSERT3S(tocpy, >=, 0); 216*17aab35aSMartin Matuska if (m != bogus_page) { 217*17aab35aSMartin Matuska ASSERT(db->db_data != NULL); 218da5137abSMartin Matuska memcpy(va + pgoff, (char *)db->db_data + bufoff, tocpy); 219*17aab35aSMartin Matuska } 220eda14cbcSMatt Macy 221eda14cbcSMatt Macy pgoff += tocpy; 22216038816SMartin Matuska ASSERT3S(pgoff, >=, 0); 22316038816SMartin Matuska ASSERT3S(pgoff, <=, PAGESIZE); 224eda14cbcSMatt Macy if (pgoff == PAGESIZE) { 225eda14cbcSMatt Macy if (m != bogus_page) { 226eda14cbcSMatt Macy zfs_unmap_page(sf); 227eda14cbcSMatt Macy vm_page_valid(m); 228eda14cbcSMatt Macy } 22916038816SMartin Matuska ASSERT3S(mi, <, count); 230eda14cbcSMatt Macy mi++; 231eda14cbcSMatt Macy pgoff = 0; 232eda14cbcSMatt Macy } 233eda14cbcSMatt Macy 234eda14cbcSMatt Macy bufoff += tocpy; 23516038816SMartin Matuska ASSERT3S(bufoff, >=, 0); 23616038816SMartin Matuska ASSERT3S(bufoff, <=, db->db_size); 237eda14cbcSMatt Macy if (bufoff == db->db_size) { 23816038816SMartin Matuska ASSERT3S(di, <, numbufs); 239eda14cbcSMatt Macy di++; 240eda14cbcSMatt Macy bufoff = 0; 241eda14cbcSMatt Macy } 242eda14cbcSMatt Macy } 243eda14cbcSMatt Macy 244eda14cbcSMatt Macy #ifdef ZFS_DEBUG 245eda14cbcSMatt Macy /* 246eda14cbcSMatt Macy * Three possibilities: 247eda14cbcSMatt Macy * - last requested page ends at a buffer boundary and , thus, 248eda14cbcSMatt Macy * all pages and buffers have been iterated; 249eda14cbcSMatt Macy * - all requested pages are filled, but the last buffer 250eda14cbcSMatt Macy * has not been exhausted; 251eda14cbcSMatt Macy * the read-ahead is possible only in this case; 252eda14cbcSMatt Macy * - all buffers have been read, but the last page has not been 253eda14cbcSMatt Macy * fully filled; 254eda14cbcSMatt Macy * this is only possible if the file has only a single buffer 255eda14cbcSMatt Macy * with a size that is not a multiple of the page size. 256eda14cbcSMatt Macy */ 257eda14cbcSMatt Macy if (mi == count) { 25816038816SMartin Matuska ASSERT3S(di, >=, numbufs - 1); 259eda14cbcSMatt Macy IMPLY(*rahead != 0, di == numbufs - 1); 260eda14cbcSMatt Macy IMPLY(*rahead != 0, bufoff != 0); 26116038816SMartin Matuska ASSERT0(pgoff); 262eda14cbcSMatt Macy } 263eda14cbcSMatt Macy if (di == numbufs) { 26416038816SMartin Matuska ASSERT3S(mi, >=, count - 1); 26516038816SMartin Matuska ASSERT0(*rahead); 266eda14cbcSMatt Macy IMPLY(pgoff == 0, mi == count); 267eda14cbcSMatt Macy if (pgoff != 0) { 26816038816SMartin Matuska ASSERT3S(mi, ==, count - 1); 26916038816SMartin Matuska ASSERT3U((dbp[0]->db_size & PAGE_MASK), !=, 0); 270eda14cbcSMatt Macy } 271eda14cbcSMatt Macy } 272eda14cbcSMatt Macy #endif 273eda14cbcSMatt Macy if (pgoff != 0) { 27416038816SMartin Matuska ASSERT3P(m, !=, bogus_page); 275da5137abSMartin Matuska memset(va + pgoff, 0, PAGESIZE - pgoff); 276eda14cbcSMatt Macy zfs_unmap_page(sf); 277eda14cbcSMatt Macy vm_page_valid(m); 278eda14cbcSMatt Macy } 279eda14cbcSMatt Macy 280eda14cbcSMatt Macy for (i = 0; i < *rahead; i++) { 281eda14cbcSMatt Macy m = vm_page_grab_unlocked(vmobj, ma[count - 1]->pindex + 1 + i, 282eda14cbcSMatt Macy VM_ALLOC_NORMAL | VM_ALLOC_NOWAIT | VM_ALLOC_BUSY_FLAGS); 283eda14cbcSMatt Macy if (m == NULL) 284eda14cbcSMatt Macy break; 285eda14cbcSMatt Macy if (!vm_page_none_valid(m)) { 286eda14cbcSMatt Macy ASSERT3U(m->valid, ==, VM_PAGE_BITS_ALL); 287ce4dcb97SMartin Matuska vm_page_sunbusy(m); 288eda14cbcSMatt Macy break; 289eda14cbcSMatt Macy } 29016038816SMartin Matuska ASSERT3U(m->dirty, ==, 0); 291caed7b1cSMartin Matuska ASSERT(!pmap_page_is_write_mapped(m)); 292eda14cbcSMatt Macy 29316038816SMartin Matuska ASSERT3U(db->db_size, >, PAGE_SIZE); 294eda14cbcSMatt Macy bufoff = IDX_TO_OFF(m->pindex) % db->db_size; 295eda14cbcSMatt Macy tocpy = MIN(db->db_size - bufoff, PAGESIZE); 296eda14cbcSMatt Macy va = zfs_map_page(m, &sf); 297*17aab35aSMartin Matuska ASSERT(db->db_data != NULL); 298da5137abSMartin Matuska memcpy(va, (char *)db->db_data + bufoff, tocpy); 299eda14cbcSMatt Macy if (tocpy < PAGESIZE) { 30016038816SMartin Matuska ASSERT3S(i, ==, *rahead - 1); 30116038816SMartin Matuska ASSERT3U((db->db_size & PAGE_MASK), !=, 0); 302da5137abSMartin Matuska memset(va + tocpy, 0, PAGESIZE - tocpy); 303eda14cbcSMatt Macy } 304eda14cbcSMatt Macy zfs_unmap_page(sf); 305eda14cbcSMatt Macy vm_page_valid(m); 306eda14cbcSMatt Macy if ((m->busy_lock & VPB_BIT_WAITERS) != 0) 307eda14cbcSMatt Macy vm_page_activate(m); 308eda14cbcSMatt Macy else 309eda14cbcSMatt Macy vm_page_deactivate(m); 310ce4dcb97SMartin Matuska vm_page_sunbusy(m); 311eda14cbcSMatt Macy } 312eda14cbcSMatt Macy *rahead = i; 313eda14cbcSMatt Macy 314eda14cbcSMatt Macy dmu_buf_rele_array(dbp, numbufs, FTAG); 315eda14cbcSMatt Macy return (0); 316eda14cbcSMatt Macy } 317