*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; If not, see
- * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
- *
- * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
- * CA 95054 USA or visit www.sun.com if you need additional information or
- * have any questions.
+ * http://www.gnu.org/licenses/gpl-2.0.html
*
* GPL HEADER END
*/
* Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
* Use is subject to license terms.
*
- * Copyright (c) 2012, 2014, Intel Corporation.
+ * Copyright (c) 2012, 2016, Intel Corporation.
*/
/*
* This file is part of Lustre, http://www.lustre.org/
#include <obd_class.h>
#include <lustre_disk.h>
#include <lustre_fid.h>
-#include <lustre/lustre_idl.h> /* LLOG_CHUNK_SIZE definition */
+#include <lustre/lustre_idl.h> /* LLOG_MIN_CHUNK_SIZE definition */
#include "osd_internal.h"
buf->lb_buf, DMU_READ_PREFETCH);
record_end_io(osd, READ, cfs_time_current() - start, size,
- size >> PAGE_CACHE_SHIFT);
+ size >> PAGE_SHIFT);
if (rc == 0) {
rc = size;
*pos += size;
* LOHA_EXISTs is supposed to be the last step in the
* initialization */
- /* declare possible size change. notice we can't check
- * current size here as another thread can change it */
-
- if (dt_object_exists(dt)) {
- LASSERT(obj->oo_db);
+ /* size change (in dnode) will be declared by dmu_tx_hold_write() */
+ if (dt_object_exists(dt))
oid = obj->oo_db->db_object;
-
- dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
- } else {
+ else
oid = DMU_NEW_OBJECT;
- dmu_tx_hold_sa_create(oh->ot_tx, ZFS_SA_BASE_ATTR_SIZE);
- }
/* XXX: we still miss for append declaration support in ZFS
* -1 means append which is used by llog mostly, llog
- * can grow upto LLOG_CHUNK_SIZE*8 records */
+ * can grow upto LLOG_MIN_CHUNK_SIZE*8 records */
if (pos == -1)
- pos = max_t(loff_t, 256 * 8 * LLOG_CHUNK_SIZE,
+ pos = max_t(loff_t, 256 * 8 * LLOG_MIN_CHUNK_SIZE,
obj->oo_attr.la_size + (2 << 20));
dmu_tx_hold_write(oh->ot_tx, oid, pos, buf->lb_len);
LASSERT(th != NULL);
oh = container_of0(th, struct osd_thandle, ot_super);
- record_start_io(osd, WRITE, 0);
-
dmu_write(osd->od_os, obj->oo_db->db_object, offset,
(uint64_t)buf->lb_len, buf->lb_buf, oh->ot_tx);
write_lock(&obj->oo_attr_lock);
rc = buf->lb_len;
out:
- record_end_io(osd, WRITE, 0, buf->lb_len,
- buf->lb_len >> PAGE_CACHE_SHIFT);
-
RETURN(rc);
}
dbf = (void *) ((unsigned long)dbp[i] | 1);
while (tocpy > 0) {
- thispage = PAGE_CACHE_SIZE;
- thispage -= bufoff & (PAGE_CACHE_SIZE - 1);
+ thispage = PAGE_SIZE;
+ thispage -= bufoff & (PAGE_SIZE - 1);
thispage = min(tocpy, thispage);
lnb->lnb_rc = 0;
lnb->lnb_file_offset = off;
- lnb->lnb_page_offset = bufoff & ~CFS_PAGE_MASK;
+ lnb->lnb_page_offset = bufoff & ~PAGE_MASK;
lnb->lnb_len = thispage;
lnb->lnb_page = kmem_to_page(dbp[i]->db_data +
bufoff);
/* mark just a single slot: we need this
- * reference to dbuf to be release once */
+ * reference to dbuf to be released once */
lnb->lnb_data = dbf;
dbf = NULL;
lnb++;
}
- /* steal dbuf so dmu_buf_rele_array() cant release it */
+ /* steal dbuf so dmu_buf_rele_array() can't release
+ * it */
dbp[i] = NULL;
}
/* go over pages arcbuf contains, put them as
* local niobufs for ptlrpc's bulks */
while (sz_in_block > 0) {
- plen = min_t(int, sz_in_block, PAGE_CACHE_SIZE);
+ plen = min_t(int, sz_in_block, PAGE_SIZE);
lnb[i].lnb_file_offset = off;
lnb[i].lnb_page_offset = 0;
/* can't use zerocopy, allocate temp. buffers */
while (sz_in_block > 0) {
- plen = min_t(int, sz_in_block, PAGE_CACHE_SIZE);
+ plen = min_t(int, sz_in_block, PAGE_SIZE);
lnb[i].lnb_file_offset = off;
lnb[i].lnb_page_offset = 0;
return 0;
}
-/* Return number of blocks that aren't mapped in the [start, start + size]
- * region */
-static int osd_count_not_mapped(struct osd_object *obj, uint64_t start,
- uint32_t size)
+static inline uint32_t osd_get_blocksz(struct osd_object *obj)
{
- dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)obj->oo_db;
- dmu_buf_impl_t *db;
- dnode_t *dn;
- uint32_t blkshift;
- uint64_t end, blkid;
- int rc;
- ENTRY;
-
- DB_DNODE_ENTER(dbi);
- dn = DB_DNODE(dbi);
-
- if (dn->dn_maxblkid == 0) {
- if (start + size <= dn->dn_datablksz)
- GOTO(out, size = 0);
- if (start < dn->dn_datablksz)
- start = dn->dn_datablksz;
- /* assume largest block size */
- blkshift = SPA_MAXBLOCKSHIFT;
- } else {
- /* blocksize can't change */
- blkshift = dn->dn_datablkshift;
- }
+ uint32_t blksz;
+ u_longlong_t unused;
- /* compute address of last block */
- end = (start + size - 1) >> blkshift;
- /* align start on block boundaries */
- start >>= blkshift;
+ LASSERT(obj->oo_db);
- /* size is null, can't be mapped */
- if (obj->oo_attr.la_size == 0 || dn->dn_maxblkid == 0)
- GOTO(out, size = (end - start + 1) << blkshift);
+ dmu_object_size_from_db(obj->oo_db, &blksz, &unused);
+ return blksz;
+}
- /* beyond EOF, can't be mapped */
- if (start > dn->dn_maxblkid)
- GOTO(out, size = (end - start + 1) << blkshift);
+static inline uint64_t osd_roundup2blocksz(uint64_t size,
+ uint64_t offset,
+ uint32_t blksz)
+{
+ LASSERT(blksz > 0);
- size = 0;
- for (blkid = start; blkid <= end; blkid++) {
- if (blkid == dn->dn_maxblkid)
- /* this one is mapped for sure */
- continue;
- if (blkid > dn->dn_maxblkid) {
- size += (end - blkid + 1) << blkshift;
- GOTO(out, size);
- }
+ size += offset % blksz;
- rc = dbuf_hold_impl(dn, 0, blkid, TRUE, FTAG, &db);
- if (rc) {
- /* for ENOENT (block not mapped) and any other errors,
- * assume the block isn't mapped */
- size += 1 << blkshift;
- continue;
- }
- dbuf_rele(db, FTAG);
- }
+ if (likely(IS_PO2(blksz)))
+ return PO2_ROUNDUP_TYPED(size, blksz, uint64_t);
- GOTO(out, size);
-out:
- DB_DNODE_EXIT(dbi);
- return size;
+ size += blksz - 1;
+ do_div(size, blksz);
+ return size * blksz;
}
static int osd_declare_write_commit(const struct lu_env *env,
- struct dt_object *dt,
- struct niobuf_local *lnb, int npages,
- struct thandle *th)
+ struct dt_object *dt,
+ struct niobuf_local *lnb, int npages,
+ struct thandle *th)
{
struct osd_object *obj = osd_dt_obj(dt);
struct osd_device *osd = osd_obj2dev(obj);
struct osd_thandle *oh;
uint64_t offset = 0;
uint32_t size = 0;
+ uint32_t blksz = osd_get_blocksz(obj);
int i, rc, flags = 0;
bool ignore_quota = false, synced = false;
long long space = 0;
dmu_tx_hold_write(oh->ot_tx, obj->oo_db->db_object,
offset, size);
- /* estimating space that will be consumed by a write is rather
+ /* Estimating space to be consumed by a write is rather
* complicated with ZFS. As a consequence, we don't account for
- * indirect blocks and quota overrun will be adjusted once the
- * operation is committed, if required. */
- space += osd_count_not_mapped(obj, offset, size);
+ * indirect blocks and just use as a rough estimate the worse
+ * case where the old space is being held by a snapshot. Quota
+ * overrun will be adjusted once the operation is committed, if
+ * required. */
+ space += osd_roundup2blocksz(size, offset, blksz);
offset = lnb[i].lnb_file_offset;
size = lnb[i].lnb_len;
if (size) {
dmu_tx_hold_write(oh->ot_tx, obj->oo_db->db_object,
offset, size);
- space += osd_count_not_mapped(obj, offset, size);
+ space += osd_roundup2blocksz(size, offset, blksz);
}
dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
* copies */
space *= osd->od_os->os_copies;
space = toqb(space);
- CDEBUG(D_QUOTA, "writting %d pages, reserving "LPD64"K of quota "
- "space\n", npages, space);
+ CDEBUG(D_QUOTA, "writing %d pages, reserving %lldK of quota space\n",
+ npages, space);
record_start_io(osd, WRITE, discont_pages);
retry:
RETURN(rc);
}
+/**
+ * Policy to grow ZFS block size by write pattern.
+ * For sequential write, it grows block size gradually until it reaches the
+ * maximum blocksize the dataset can support. Otherwise, it will pick a
+ * a block size by the writing region of this I/O.
+ */
+static int osd_grow_blocksize(struct osd_object *obj, struct osd_thandle *oh,
+ uint64_t start, uint64_t end)
+{
+ struct osd_device *osd = osd_obj2dev(obj);
+ dmu_buf_impl_t *db = (dmu_buf_impl_t *)obj->oo_db;
+ dnode_t *dn;
+ uint32_t blksz;
+ int rc = 0;
+
+ ENTRY;
+
+ DB_DNODE_ENTER(db);
+ dn = DB_DNODE(db);
+
+ if (dn->dn_maxblkid > 0) /* can't change block size */
+ GOTO(out, rc);
+
+ if (dn->dn_datablksz >= osd->od_max_blksz)
+ GOTO(out, rc);
+
+ down_write(&obj->oo_guard);
+
+ blksz = dn->dn_datablksz;
+ if (blksz >= osd->od_max_blksz) /* check again after grabbing lock */
+ GOTO(out_unlock, rc);
+
+ /* now ZFS can support up to 16MB block size, and if the write
+ * is sequential, it just increases the block size gradually */
+ if (start <= blksz) { /* sequential */
+ blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz, end);
+ } else { /* sparse, pick a block size by write region */
+ blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz,
+ end - start);
+ }
+
+ if (!is_power_of_2(blksz))
+ blksz = size_roundup_power2(blksz);
+
+ if (blksz > dn->dn_datablksz) {
+ rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
+ blksz, 0, oh->ot_tx);
+ LASSERT(ergo(rc == 0, dn->dn_datablksz >= blksz));
+ if (rc < 0)
+ CDEBUG(D_INODE, "object "DFID": change block size"
+ "%u -> %u error rc = %d\n",
+ PFID(lu_object_fid(&obj->oo_dt.do_lu)),
+ dn->dn_datablksz, blksz, rc);
+ }
+ EXIT;
+out_unlock:
+ up_write(&obj->oo_guard);
+out:
+ DB_DNODE_EXIT(db);
+ return rc;
+}
+
static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
struct niobuf_local *lnb, int npages,
struct thandle *th)
LASSERT(th != NULL);
oh = container_of0(th, struct osd_thandle, ot_super);
+ /* adjust block size. Assume the buffers are sorted. */
+ (void)osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
+ lnb[npages - 1].lnb_file_offset +
+ lnb[npages - 1].lnb_len);
+
+ /* LU-8791: take oo_guard to avoid the deadlock that changing block
+ * size and assigning arcbuf take place at the same time.
+ *
+ * Thread 1:
+ * osd_write_commit()
+ * -> osd_grow_blocksize() with osd_object::oo_guard held
+ * -> dmu_object_set_blocksize()
+ * -> dnode_set_blksz(), with dnode_t::dn_struct_rwlock
+ * write lock held
+ * -> dbuf_new_size()
+ * -> dmu_buf_will_dirty()
+ * -> dbuf_read()
+ * -> wait for the dbuf state to change
+ * Thread 2:
+ * osd_write_commit()
+ * -> dmu_assign_arcbuf()
+ * -> dbuf_assign_arcbuf(), set dbuf state to DB_FILL
+ * -> dbuf_dirty()
+ * -> try to hold the read lock of dnode_t::dn_struct_rwlock
+ *
+ * By taking the read lock, it can avoid thread 2 to enter into the
+ * critical section of assigning the arcbuf, while thread 1 is
+ * changing the block size.
+ */
+ down_read(&obj->oo_guard);
for (i = 0; i < npages; i++) {
CDEBUG(D_INODE, "write %u bytes at %u\n",
(unsigned) lnb[i].lnb_len,
new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
iosize += lnb[i].lnb_len;
}
+ up_read(&obj->oo_guard);
if (unlikely(new_size == 0)) {
/* no pages to write, no transno is needed */
{
struct osd_object *obj = osd_dt_obj(dt);
int i;
- unsigned long size = 0;
loff_t eof;
LASSERT(dt_object_exists(dt));
continue;
lnb[i].lnb_rc = lnb[i].lnb_len;
- size += lnb[i].lnb_rc;
- if (lnb[i].lnb_file_offset + lnb[i].lnb_len > eof) {
- lnb[i].lnb_rc = eof - lnb[i].lnb_file_offset;
- if (lnb[i].lnb_rc < 0)
+ if (lnb[i].lnb_file_offset + lnb[i].lnb_len >= eof) {
+ if (eof <= lnb[i].lnb_file_offset)
lnb[i].lnb_rc = 0;
+ else
+ lnb[i].lnb_rc = eof - lnb[i].lnb_file_offset;
/* all subsequent rc should be 0 */
while (++i < npages)
false));
}
+static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
+ __u64 start, __u64 end, enum lu_ladvise_type advice)
+{
+ int rc;
+ ENTRY;
+
+ switch (advice) {
+ default:
+ rc = -ENOTSUPP;
+ break;
+ }
+
+ RETURN(rc);
+}
struct dt_body_operations osd_body_ops = {
.dbo_read = osd_read,
.dbo_read_prep = osd_read_prep,
.dbo_declare_punch = osd_declare_punch,
.dbo_punch = osd_punch,
+ .dbo_ladvise = osd_ladvise,
};