Whamcloud - gitweb
a5dbac404e8c228eb2a7e40369856d4b5f60475a
[fs/lustre-release.git] / lustre / osd-zfs / osd_io.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/osd-zfs/osd_io.c
32  *
33  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
34  * Author: Mike Pershin <tappro@whamcloud.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_OSD
38
39 #include <libcfs/libcfs.h>
40 #include <obd_support.h>
41 #include <lustre_net.h>
42 #include <obd.h>
43 #include <obd_class.h>
44 #include <lustre_disk.h>
45 #include <lustre_fid.h>
46 #include <lustre_quota.h>
47
48 #include "osd_internal.h"
49
50 #include <sys/dnode.h>
51 #include <sys/dbuf.h>
52 #include <sys/spa.h>
53 #include <sys/stat.h>
54 #include <sys/zap.h>
55 #include <sys/spa_impl.h>
56 #include <sys/zfs_znode.h>
57 #include <sys/dmu_tx.h>
58 #include <sys/dmu_objset.h>
59 #include <sys/dsl_prop.h>
60 #include <sys/sa_impl.h>
61 #include <sys/txg.h>
62
63 static char osd_0copy_tag[] = "zerocopy";
64
65 static void dbuf_set_pending_evict(dmu_buf_t *db)
66 {
67         dmu_buf_impl_t *dbi = (dmu_buf_impl_t *)db;
68         dbi->db_pending_evict = TRUE;
69 }
70
71 static void record_start_io(struct osd_device *osd, int rw, int discont_pages)
72 {
73         struct obd_histogram *h = osd->od_brw_stats.hist;
74
75         if (rw == READ) {
76                 atomic_inc(&osd->od_r_in_flight);
77                 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
78                                  atomic_read(&osd->od_r_in_flight));
79                 lprocfs_oh_tally(&h[BRW_R_DISCONT_PAGES], discont_pages);
80
81         } else {
82                 atomic_inc(&osd->od_w_in_flight);
83                 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
84                                  atomic_read(&osd->od_w_in_flight));
85                 lprocfs_oh_tally(&h[BRW_W_DISCONT_PAGES], discont_pages);
86
87         }
88 }
89
90 static void record_end_io(struct osd_device *osd, int rw,
91                           unsigned long elapsed, int disksize, int npages)
92 {
93         struct obd_histogram *h = osd->od_brw_stats.hist;
94
95         if (rw == READ)
96                 atomic_dec(&osd->od_r_in_flight);
97         else
98                 atomic_dec(&osd->od_w_in_flight);
99
100         lprocfs_oh_tally_log2(&h[BRW_R_PAGES + rw], npages);
101         if (disksize > 0)
102                 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE + rw], disksize);
103         if (elapsed)
104                 lprocfs_oh_tally_log2(&h[BRW_R_IO_TIME + rw], elapsed);
105 }
106
107 static ssize_t __osd_read(const struct lu_env *env, struct dt_object *dt,
108                           struct lu_buf *buf, loff_t *pos, size_t *size)
109 {
110         struct osd_object *obj = osd_dt_obj(dt);
111         uint64_t old_size;
112         int rc;
113
114         LASSERT(dt_object_exists(dt));
115         LASSERT(obj->oo_dn);
116
117         read_lock(&obj->oo_attr_lock);
118         old_size = obj->oo_attr.la_size;
119         read_unlock(&obj->oo_attr_lock);
120
121         if (*pos + *size > old_size) {
122                 if (old_size < *pos)
123                         return 0;
124
125                 *size = old_size - *pos;
126         }
127
128         rc = osd_dmu_read(osd_obj2dev(obj), obj->oo_dn, *pos, *size,
129                           buf->lb_buf, DMU_READ_PREFETCH);
130         if (!rc) {
131                 rc = *size;
132                 *pos += *size;
133         }
134
135         return rc;
136 }
137
138 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
139                         struct lu_buf *buf, loff_t *pos)
140 {
141         struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
142         size_t size = buf->lb_len;
143         hrtime_t start = gethrtime();
144         s64 delta_ms;
145         int rc;
146
147         record_start_io(osd, READ, 0);
148         rc = __osd_read(env, dt, buf, pos, &size);
149         delta_ms = gethrtime() - start;
150         do_div(delta_ms, NSEC_PER_MSEC);
151         record_end_io(osd, READ, delta_ms, size, size >> PAGE_SHIFT);
152
153         return rc;
154 }
155
156 static inline ssize_t osd_read_no_record(const struct lu_env *env,
157                                          struct dt_object *dt,
158                                          struct lu_buf *buf, loff_t *pos)
159 {
160         size_t size = buf->lb_len;
161
162         return __osd_read(env, dt, buf, pos, &size);
163 }
164
165 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
166                                 const struct lu_buf *buf, loff_t pos,
167                                 struct thandle *th)
168 {
169         struct osd_object  *obj  = osd_dt_obj(dt);
170         struct osd_device  *osd = osd_obj2dev(obj);
171         struct osd_thandle *oh;
172         uint64_t            oid;
173         ENTRY;
174
175         oh = container_of(th, struct osd_thandle, ot_super);
176
177         /* in some cases declare can race with creation (e.g. llog)
178          * and we need to wait till object is initialized. notice
179          * LOHA_EXISTs is supposed to be the last step in the
180          * initialization */
181
182         /* size change (in dnode) will be declared by dmu_tx_hold_write() */
183         if (dt_object_exists(dt))
184                 oid = obj->oo_dn->dn_object;
185         else
186                 oid = DMU_NEW_OBJECT;
187
188         /* XXX: we still miss for append declaration support in ZFS
189          *      -1 means append which is used by llog mostly, llog
190          *      can grow upto LLOG_MIN_CHUNK_SIZE*8 records */
191         if (pos == -1)
192                 pos = max_t(loff_t, 256 * 8 * LLOG_MIN_CHUNK_SIZE,
193                             obj->oo_attr.la_size + (2 << 20));
194         osd_tx_hold_write(oh->ot_tx, oid, obj->oo_dn, pos, buf->lb_len);
195
196         /* dt_declare_write() is usually called for system objects, such
197          * as llog or last_rcvd files. We needn't enforce quota on those
198          * objects, so always set the lqi_space as 0. */
199         RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
200                                  obj->oo_attr.la_gid, obj->oo_attr.la_projid,
201                                  0, oh, NULL, OSD_QID_BLK));
202 }
203
204 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
205                         const struct lu_buf *buf, loff_t *pos,
206                          struct thandle *th)
207 {
208         struct osd_object  *obj  = osd_dt_obj(dt);
209         struct osd_device  *osd = osd_obj2dev(obj);
210         struct osd_thandle *oh;
211         uint64_t            offset = *pos;
212         int                 rc;
213
214         ENTRY;
215
216         LASSERT(dt_object_exists(dt));
217         LASSERT(obj->oo_dn);
218
219         LASSERT(th != NULL);
220         oh = container_of(th, struct osd_thandle, ot_super);
221
222         down_read(&obj->oo_guard);
223         if (obj->oo_destroyed)
224                 GOTO(out, rc = -ENOENT);
225
226         osd_dmu_write(osd, obj->oo_dn, offset, (uint64_t)buf->lb_len,
227                       buf->lb_buf, oh->ot_tx);
228         write_lock(&obj->oo_attr_lock);
229         if (obj->oo_attr.la_size < offset + buf->lb_len) {
230                 obj->oo_attr.la_size = offset + buf->lb_len;
231                 write_unlock(&obj->oo_attr_lock);
232                 /* osd_object_sa_update() will be copying directly from oo_attr
233                  * into dbuf.  any update within a single txg will copy the
234                  * most actual */
235                 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
236                                         &obj->oo_attr.la_size, 8, oh);
237                 if (unlikely(rc))
238                         GOTO(out, rc);
239         } else {
240                 write_unlock(&obj->oo_attr_lock);
241         }
242
243         *pos += buf->lb_len;
244         rc = buf->lb_len;
245
246 out:
247         up_read(&obj->oo_guard);
248         RETURN(rc);
249 }
250
251 /*
252  * XXX: for the moment I don't want to use lnb_flags for osd-internal
253  *      purposes as it's not very well defined ...
254  *      instead I use the lowest bit of the address so that:
255  *        arc buffer:  .lnb_data = abuf          (arc we loan for write)
256  *        dbuf buffer: .lnb_data = dbuf | 1      (dbuf we get for read)
257  *        copy buffer: .lnb_page->mapping = obj (page we allocate for write)
258  *
259  *      bzzz, to blame
260  */
261 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
262                         struct niobuf_local *lnb, int npages)
263 {
264         struct osd_object *obj  = osd_dt_obj(dt);
265         struct osd_device *osd = osd_obj2dev(obj);
266         unsigned long      ptr;
267         int                i;
268
269         LASSERT(dt_object_exists(dt));
270         LASSERT(obj->oo_dn);
271
272         for (i = 0; i < npages; i++) {
273                 if (lnb[i].lnb_page == NULL)
274                         continue;
275                 if (lnb[i].lnb_page->mapping == (void *)obj) {
276                         /* this is anonymous page allocated for copy-write */
277                         lnb[i].lnb_page->mapping = NULL;
278                         __free_page(lnb[i].lnb_page);
279                         atomic_dec(&osd->od_zerocopy_alloc);
280                 } else {
281                         /* see comment in osd_bufs_get_read() */
282                         ptr = (unsigned long)lnb[i].lnb_data;
283                         if (ptr & 1UL) {
284                                 ptr &= ~1UL;
285                                 dmu_buf_rele((void *)ptr, osd_0copy_tag);
286                                 atomic_dec(&osd->od_zerocopy_pin);
287                         } else if (lnb[i].lnb_data != NULL) {
288                                 int j, apages, abufsz;
289                                 abufsz = arc_buf_size(lnb[i].lnb_data);
290                                 apages = abufsz >> PAGE_SHIFT;
291                                 /* these references to pages must be invalidated
292                                  * to prevent access in osd_bufs_put() */
293                                 for (j = 0; j < apages; j++)
294                                         lnb[i + j].lnb_page = NULL;
295                                 dmu_return_arcbuf(lnb[i].lnb_data);
296                                 atomic_dec(&osd->od_zerocopy_loan);
297                         }
298                 }
299                 lnb[i].lnb_page = NULL;
300                 lnb[i].lnb_data = NULL;
301         }
302
303         return 0;
304 }
305
306 static inline struct page *kmem_to_page(void *addr)
307 {
308         LASSERT(!((unsigned long)addr & ~PAGE_MASK));
309         if (is_vmalloc_addr(addr))
310                 return vmalloc_to_page(addr);
311         else
312                 return virt_to_page(addr);
313 }
314
315 /**
316  * Prepare buffers for read.
317  *
318  * The function maps the range described by \a off and \a len to \a lnb array.
319  * dmu_buf_hold_array_by_bonus() finds/creates appropriate ARC buffers, then
320  * we fill \a lnb array with the pages storing ARC buffers. Notice the current
321  * implementationt passes TRUE to dmu_buf_hold_array_by_bonus() to fill ARC
322  * buffers with actual data, I/O is done in the conext of osd_bufs_get_read().
323  * A better implementation would just return the buffers (potentially unfilled)
324  * and subsequent osd_read_prep() would do I/O for many ranges concurrently.
325  *
326  * \param[in] env       environment
327  * \param[in] obj       object
328  * \param[in] off       offset in bytes
329  * \param[in] len       the number of bytes to access
330  * \param[out] lnb      array of local niobufs pointing to the buffers with data
331  *
332  * \retval              0 for success
333  * \retval              negative error number of failure
334  */
335 static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
336                              loff_t off, ssize_t len, struct niobuf_local *lnb,
337                              int maxlnb)
338 {
339         struct osd_device *osd = osd_obj2dev(obj);
340         int rc, i, numbufs, npages = 0, drop_cache = 0;
341         hrtime_t start = gethrtime();
342         dmu_buf_t **dbp;
343         s64 delta_ms;
344
345         ENTRY;
346         record_start_io(osd, READ, 0);
347
348         if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize)
349                 drop_cache = 1;
350
351         /* grab buffers for read:
352          * OSD API let us to grab buffers first, then initiate IO(s)
353          * so that all required IOs will be done in parallel, but at the
354          * moment DMU doesn't provide us with a method to grab buffers.
355          * If we discover this is a vital for good performance we
356          * can get own replacement for dmu_buf_hold_array_by_bonus().
357          */
358         while (len > 0 &&
359                (obj->oo_dn->dn_datablkshift != 0 ||
360                 off < obj->oo_dn->dn_datablksz)) {
361                 if (obj->oo_dn->dn_datablkshift == 0 &&
362                     off + len > obj->oo_dn->dn_datablksz)
363                         len = obj->oo_dn->dn_datablksz - off;
364
365                 dbp = NULL;
366                 if (unlikely(npages >= maxlnb))
367                         GOTO(err, rc = -EOVERFLOW);
368
369                 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db,
370                                                   off, len, TRUE, osd_0copy_tag,
371                                                   &numbufs, &dbp);
372                 if (unlikely(rc))
373                         GOTO(err, rc);
374
375                 for (i = 0; i < numbufs; i++) {
376                         int bufoff, tocpy, thispage;
377                         void *dbf = dbp[i];
378
379                         LASSERT(len > 0);
380
381                         atomic_inc(&osd->od_zerocopy_pin);
382
383                         bufoff = off - dbp[i]->db_offset;
384                         tocpy = min_t(int, dbp[i]->db_size - bufoff, len);
385
386                         /* kind of trick to differentiate dbuf vs. arcbuf */
387                         LASSERT(((unsigned long)dbp[i] & 1) == 0);
388                         dbf = (void *) ((unsigned long)dbp[i] | 1);
389
390                         while (tocpy > 0) {
391                                 if (unlikely(npages >= maxlnb))
392                                         GOTO(err, rc = -EOVERFLOW);
393
394                                 thispage = PAGE_SIZE;
395                                 thispage -= bufoff & (PAGE_SIZE - 1);
396                                 thispage = min(tocpy, thispage);
397
398                                 lnb->lnb_rc = 0;
399                                 lnb->lnb_file_offset = off;
400                                 lnb->lnb_page_offset = bufoff & ~PAGE_MASK;
401                                 lnb->lnb_len = thispage;
402                                 lnb->lnb_page = kmem_to_page(dbp[i]->db_data +
403                                                              bufoff);
404                                 /* mark just a single slot: we need this
405                                  * reference to dbuf to be released once */
406                                 lnb->lnb_data = dbf;
407                                 dbf = NULL;
408
409                                 tocpy -= thispage;
410                                 len -= thispage;
411                                 bufoff += thispage;
412                                 off += thispage;
413
414                                 npages++;
415                                 lnb++;
416                         }
417
418                         if (drop_cache)
419                                 dbuf_set_pending_evict(dbp[i]);
420
421                         /* steal dbuf so dmu_buf_rele_array() can't release
422                          * it */
423                         dbp[i] = NULL;
424                 }
425
426                 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
427         }
428
429         delta_ms = gethrtime() - start;
430         do_div(delta_ms, NSEC_PER_MSEC);
431         record_end_io(osd, READ, delta_ms, npages * PAGE_SIZE, npages);
432
433         RETURN(npages);
434
435 err:
436         LASSERT(rc < 0);
437         if (dbp)
438                 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
439         osd_bufs_put(env, &obj->oo_dt, lnb - npages, npages);
440         RETURN(rc);
441 }
442
443 static inline arc_buf_t *osd_request_arcbuf(dnode_t *dn, size_t bs)
444 {
445         arc_buf_t *abuf;
446
447         abuf = dmu_request_arcbuf(&dn->dn_bonus->db, bs);
448         if (unlikely(!abuf))
449                 return ERR_PTR(-ENOMEM);
450
451 #if ZFS_VERSION_CODE < OBD_OCD_VERSION(0, 7, 0, 0)
452         /**
453          * ZFS prior to 0.7.0 doesn't guarantee PAGE_SIZE alignment for zio
454          * blocks smaller than (PAGE_SIZE << 2). This poses a problem of
455          * setting up page array for RDMA transfer. See LU-9305.
456          */
457         if ((unsigned long)abuf->b_data & ~PAGE_MASK) {
458                 dmu_return_arcbuf(abuf);
459                 return NULL;
460         }
461 #endif
462
463         return abuf;
464 }
465
466 static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
467                               loff_t off, ssize_t len, struct niobuf_local *lnb,
468                               int maxlnb)
469 {
470         struct osd_device *osd = osd_obj2dev(obj);
471         int                poff, plen, off_in_block, sz_in_block;
472         int                rc, i = 0, npages = 0;
473         dnode_t *dn = obj->oo_dn;
474         arc_buf_t *abuf;
475         uint32_t bs = dn->dn_datablksz;
476         ENTRY;
477
478         /*
479          * currently only full blocks are subject to zerocopy approach:
480          * so that we're sure nobody is trying to update the same block
481          */
482         while (len > 0) {
483                 if (unlikely(npages >= maxlnb))
484                         GOTO(out_err, rc = -EOVERFLOW);
485
486                 off_in_block = off & (bs - 1);
487                 sz_in_block = min_t(int, bs - off_in_block, len);
488
489                 abuf = NULL;
490                 if (sz_in_block == bs) {
491                         /* full block, try to use zerocopy */
492                         abuf = osd_request_arcbuf(dn, bs);
493                         if (unlikely(IS_ERR(abuf)))
494                                 GOTO(out_err, rc = PTR_ERR(abuf));
495                 }
496
497                 if (abuf != NULL) {
498                         atomic_inc(&osd->od_zerocopy_loan);
499
500                         /* go over pages arcbuf contains, put them as
501                          * local niobufs for ptlrpc's bulks */
502                         while (sz_in_block > 0) {
503                                 plen = min_t(int, sz_in_block, PAGE_SIZE);
504
505                                 if (unlikely(npages >= maxlnb))
506                                         GOTO(out_err, rc = -EOVERFLOW);
507
508                                 lnb[i].lnb_file_offset = off;
509                                 lnb[i].lnb_page_offset = 0;
510                                 lnb[i].lnb_len = plen;
511                                 lnb[i].lnb_rc = 0;
512                                 if (sz_in_block == bs)
513                                         lnb[i].lnb_data = abuf;
514                                 else
515                                         lnb[i].lnb_data = NULL;
516
517                                 /* this one is not supposed to fail */
518                                 lnb[i].lnb_page = kmem_to_page(abuf->b_data +
519                                                         off_in_block);
520                                 LASSERT(lnb[i].lnb_page);
521
522                                 lprocfs_counter_add(osd->od_stats,
523                                                 LPROC_OSD_ZEROCOPY_IO, 1);
524
525                                 sz_in_block -= plen;
526                                 len -= plen;
527                                 off += plen;
528                                 off_in_block += plen;
529                                 i++;
530                                 npages++;
531                         }
532                 } else {
533                         if (off_in_block == 0 && len < bs &&
534                                         off + len >= obj->oo_attr.la_size)
535                                 lprocfs_counter_add(osd->od_stats,
536                                                 LPROC_OSD_TAIL_IO, 1);
537
538                         /* can't use zerocopy, allocate temp. buffers */
539                         poff = off & (PAGE_SIZE - 1);
540                         while (sz_in_block > 0) {
541                                 plen = min_t(int, poff + sz_in_block,
542                                              PAGE_SIZE);
543                                 plen -= poff;
544
545                                 if (unlikely(npages >= maxlnb))
546                                         GOTO(out_err, rc = -EOVERFLOW);
547
548                                 lnb[i].lnb_file_offset = off;
549                                 lnb[i].lnb_page_offset = poff;
550                                 poff = 0;
551
552                                 lnb[i].lnb_len = plen;
553                                 lnb[i].lnb_rc = 0;
554                                 lnb[i].lnb_data = NULL;
555
556                                 lnb[i].lnb_page = alloc_page(OSD_GFP_IO);
557                                 if (unlikely(lnb[i].lnb_page == NULL))
558                                         GOTO(out_err, rc = -ENOMEM);
559
560                                 LASSERT(lnb[i].lnb_page->mapping == NULL);
561                                 lnb[i].lnb_page->mapping = (void *)obj;
562
563                                 atomic_inc(&osd->od_zerocopy_alloc);
564                                 lprocfs_counter_add(osd->od_stats,
565                                                 LPROC_OSD_COPY_IO, 1);
566
567                                 sz_in_block -= plen;
568                                 len -= plen;
569                                 off += plen;
570                                 i++;
571                                 npages++;
572                         }
573                 }
574         }
575
576         RETURN(npages);
577
578 out_err:
579         osd_bufs_put(env, &obj->oo_dt, lnb, npages);
580         RETURN(rc);
581 }
582
583 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
584                         loff_t offset, ssize_t len, struct niobuf_local *lnb,
585                         int maxlnb, enum dt_bufs_type rw)
586 {
587         struct osd_object *obj  = osd_dt_obj(dt);
588         int                rc;
589
590         LASSERT(dt_object_exists(dt));
591         LASSERT(obj->oo_dn);
592
593         if (rw & DT_BUFS_TYPE_WRITE)
594                 rc = osd_bufs_get_write(env, obj, offset, len, lnb, maxlnb);
595         else
596                 rc = osd_bufs_get_read(env, obj, offset, len, lnb, maxlnb);
597
598         return rc;
599 }
600
601 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
602                         struct niobuf_local *lnb, int npages)
603 {
604         struct osd_object *obj = osd_dt_obj(dt);
605
606         LASSERT(dt_object_exists(dt));
607         LASSERT(obj->oo_dn);
608
609         return 0;
610 }
611
612 static inline uint64_t osd_roundup2blocksz(uint64_t size,
613                                            uint64_t offset,
614                                            uint32_t blksz)
615 {
616         LASSERT(blksz > 0);
617
618         size += offset % blksz;
619
620         if (likely(is_power_of_2(blksz)))
621                 return round_up(size, blksz);
622         else
623                 return DIV_ROUND_UP_ULL(size, blksz) * blksz;
624 }
625
626 static int osd_declare_write_commit(const struct lu_env *env,
627                                     struct dt_object *dt,
628                                     struct niobuf_local *lnb, int npages,
629                                     struct thandle *th)
630 {
631         struct osd_object  *obj = osd_dt_obj(dt);
632         struct osd_device  *osd = osd_obj2dev(obj);
633         struct osd_thandle *oh;
634         uint64_t            offset = 0;
635         uint32_t            size = 0;
636         uint32_t blksz = obj->oo_dn->dn_datablksz;
637         int                 i, rc;
638         bool synced = false;
639         long long           space = 0;
640         struct page        *last_page = NULL;
641         unsigned long       discont_pages = 0;
642         enum osd_quota_local_flags local_flags = 0;
643         enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
644         ENTRY;
645
646         LASSERT(dt_object_exists(dt));
647         LASSERT(obj->oo_dn);
648
649         LASSERT(lnb);
650         LASSERT(npages > 0);
651
652         oh = container_of(th, struct osd_thandle, ot_super);
653
654         for (i = 0; i < npages; i++) {
655                 if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
656                         ++discont_pages;
657                 last_page = lnb[i].lnb_page;
658                 if (lnb[i].lnb_rc)
659                         /* ENOSPC, network RPC error, etc.
660                          * We don't want to book space for pages which will be
661                          * skipped in osd_write_commit(). Hence we skip pages
662                          * with lnb_rc != 0 here too */
663                         continue;
664                 /* ignore quota for the whole request if any page is from
665                  * client cache or written by root.
666                  *
667                  * XXX once we drop the 1.8 client support, the checking
668                  * for whether page is from cache can be simplified as:
669                  * !(lnb[i].flags & OBD_BRW_SYNC)
670                  *
671                  * XXX we could handle this on per-lnb basis as done by
672                  * grant. */
673                 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
674                     (lnb[i].lnb_flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
675                     OBD_BRW_FROM_GRANT)
676                         declare_flags |= OSD_QID_FORCE;
677
678                 if (size == 0) {
679                         /* first valid lnb */
680                         offset = lnb[i].lnb_file_offset;
681                         size = lnb[i].lnb_len;
682                         continue;
683                 }
684                 if (offset + size == lnb[i].lnb_file_offset) {
685                         /* this lnb is contiguous to the previous one */
686                         size += lnb[i].lnb_len;
687                         continue;
688                 }
689
690                 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object,
691                                   obj->oo_dn, offset, size);
692                 /* Estimating space to be consumed by a write is rather
693                  * complicated with ZFS. As a consequence, we don't account for
694                  * indirect blocks and just use as a rough estimate the worse
695                  * case where the old space is being held by a snapshot. Quota
696                  * overrun will be adjusted once the operation is committed, if
697                  * required. */
698                 space += osd_roundup2blocksz(size, offset, blksz);
699
700                 offset = lnb[i].lnb_file_offset;
701                 size = lnb[i].lnb_len;
702         }
703
704         if (size) {
705                 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
706                                   offset, size);
707                 space += osd_roundup2blocksz(size, offset, blksz);
708         }
709
710         /* backend zfs filesystem might be configured to store multiple data
711          * copies */
712         space  *= osd->od_os->os_copies;
713         space   = toqb(space);
714         CDEBUG(D_QUOTA, "writing %d pages, reserving %lldK of quota space\n",
715                npages, space);
716
717         record_start_io(osd, WRITE, discont_pages);
718 retry:
719         /* acquire quota space if needed */
720         rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
721                                obj->oo_attr.la_gid, obj->oo_attr.la_projid,
722                                space, oh, &local_flags, declare_flags);
723
724         if (!synced && rc == -EDQUOT &&
725             (local_flags & QUOTA_FL_SYNC) != 0) {
726                 dt_sync(env, th->th_dev);
727                 synced = true;
728                 CDEBUG(D_QUOTA, "retry after sync\n");
729                 local_flags = 0;
730                 goto retry;
731         }
732
733         /* we need only to store the overquota flags in the first lnb for
734          * now, once we support multiple objects BRW, this code needs be
735          * revised. */
736         if (local_flags & QUOTA_FL_OVER_USRQUOTA)
737                 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
738         if (local_flags & QUOTA_FL_OVER_GRPQUOTA)
739                 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
740 #ifdef ZFS_PROJINHERIT
741         if (local_flags & QUOTA_FL_OVER_PRJQUOTA)
742                 lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
743 #endif
744
745         RETURN(rc);
746 }
747
748 /**
749  * Policy to grow ZFS block size by write pattern.
750  * For sequential write, it grows block size gradually until it reaches the
751  * maximum blocksize the dataset can support. Otherwise, it will pick a
752  * a block size by the writing region of this I/O.
753  */
754 static int osd_grow_blocksize(struct osd_object *obj, struct osd_thandle *oh,
755                               uint64_t start, uint64_t end)
756 {
757         struct osd_device       *osd = osd_obj2dev(obj);
758         dnode_t *dn = obj->oo_dn;
759         uint32_t                 blksz;
760         int                      rc = 0;
761
762         ENTRY;
763
764         if (dn->dn_maxblkid > 0) /* can't change block size */
765                 GOTO(out, rc);
766
767         if (dn->dn_datablksz >= osd->od_max_blksz)
768                 GOTO(out, rc);
769
770         down_write(&obj->oo_guard);
771
772         blksz = dn->dn_datablksz;
773         if (blksz >= osd->od_max_blksz) /* check again after grabbing lock */
774                 GOTO(out_unlock, rc);
775
776         /* now ZFS can support up to 16MB block size, and if the write
777          * is sequential, it just increases the block size gradually */
778         if (start <= blksz) { /* sequential */
779                 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz, end);
780         } else { /* sparse, pick a block size by write region */
781                 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz,
782                                         end - start);
783         }
784
785         if (!is_power_of_2(blksz))
786                 blksz = size_roundup_power2(blksz);
787
788         if (blksz > dn->dn_datablksz) {
789                 rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
790                                                blksz, 0, oh->ot_tx);
791                 LASSERT(ergo(rc == 0, dn->dn_datablksz >= blksz));
792                 if (rc < 0)
793                         CDEBUG(D_INODE, "object "DFID": change block size"
794                                "%u -> %u error rc = %d\n",
795                                PFID(lu_object_fid(&obj->oo_dt.do_lu)),
796                                dn->dn_datablksz, blksz, rc);
797         }
798         EXIT;
799 out_unlock:
800         up_write(&obj->oo_guard);
801 out:
802         return rc;
803 }
804
805 static void osd_evict_dbufs_after_write(struct osd_object *obj,
806                                         loff_t off, ssize_t len)
807 {
808         dmu_buf_t **dbp;
809         int i, rc, numbufs;
810
811         rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db, off, len,
812                                           TRUE, osd_0copy_tag, &numbufs, &dbp);
813         if (unlikely(rc))
814                 return;
815
816         for (i = 0; i < numbufs; i++)
817                 dbuf_set_pending_evict(dbp[i]);
818
819         dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
820 }
821
822 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
823                         struct niobuf_local *lnb, int npages,
824                         struct thandle *th, __u64 user_size)
825 {
826         struct osd_object  *obj  = osd_dt_obj(dt);
827         struct osd_device  *osd = osd_obj2dev(obj);
828         struct osd_thandle *oh;
829         uint64_t            new_size = 0;
830         int                 i, abufsz, rc = 0, drop_cache = 0;
831         unsigned long      iosize = 0;
832         ENTRY;
833
834         LASSERT(dt_object_exists(dt));
835         LASSERT(obj->oo_dn);
836
837         LASSERT(th != NULL);
838         oh = container_of(th, struct osd_thandle, ot_super);
839
840         /* adjust block size. Assume the buffers are sorted. */
841         (void)osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
842                                  lnb[npages - 1].lnb_file_offset +
843                                  lnb[npages - 1].lnb_len);
844
845         if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize ||
846             lnb[npages - 1].lnb_file_offset + lnb[npages - 1].lnb_len >=
847             osd->od_readcache_max_filesize)
848                 drop_cache = 1;
849
850         if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC))
851                 RETURN(-ENOSPC);
852
853         /* if la_size is already bigger than specified user_size,
854          * ignore user_size
855          */
856         if (obj->oo_attr.la_size > user_size)
857                 user_size = 0;
858
859         /* LU-8791: take oo_guard to avoid the deadlock that changing block
860          * size and assigning arcbuf take place at the same time.
861          *
862          * Thread 1:
863          * osd_write_commit()
864          *  -> osd_grow_blocksize() with osd_object::oo_guard held
865          *   -> dmu_object_set_blocksize()
866          *    -> dnode_set_blksz(), with dnode_t::dn_struct_rwlock
867          *       write lock held
868          *     -> dbuf_new_size()
869          *      -> dmu_buf_will_dirty()
870          *       -> dbuf_read()
871          *        -> wait for the dbuf state to change
872          * Thread 2:
873          * osd_write_commit()
874          *  -> dmu_assign_arcbuf()
875          *   -> dbuf_assign_arcbuf(), set dbuf state to DB_FILL
876          *    -> dbuf_dirty()
877          *     -> try to hold the read lock of dnode_t::dn_struct_rwlock
878          *
879          * By taking the read lock, it can avoid thread 2 to enter into the
880          * critical section of assigning the arcbuf, while thread 1 is
881          * changing the block size.
882          */
883         down_read(&obj->oo_guard);
884         if (obj->oo_destroyed) {
885                 up_read(&obj->oo_guard);
886                 RETURN(-ENOENT);
887         }
888
889         for (i = 0; i < npages; i++) {
890                 CDEBUG(D_INODE, "write %u bytes at %u\n",
891                         (unsigned) lnb[i].lnb_len,
892                         (unsigned) lnb[i].lnb_file_offset);
893
894                 if (lnb[i].lnb_rc) {
895                         /* ENOSPC, network RPC error, etc.
896                          * Unlike ldiskfs, zfs allocates new blocks on rewrite,
897                          * so we skip this page if lnb_rc is set to -ENOSPC */
898                         CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
899                                 PFID(lu_object_fid(&dt->do_lu)), i,
900                                 lnb[i].lnb_rc);
901                         continue;
902                 }
903
904                 if (new_size < lnb[i].lnb_file_offset + lnb[i].lnb_len)
905                         new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
906                 if (lnb[i].lnb_page == NULL)
907                         continue;
908
909                 if (lnb[i].lnb_page->mapping == (void *)obj) {
910                         osd_dmu_write(osd, obj->oo_dn, lnb[i].lnb_file_offset,
911                                       lnb[i].lnb_len, kmap(lnb[i].lnb_page) +
912                                       lnb[i].lnb_page_offset, oh->ot_tx);
913                         kunmap(lnb[i].lnb_page);
914                         iosize += lnb[i].lnb_len;
915                         abufsz = lnb[i].lnb_len; /* to drop cache below */
916                 } else if (lnb[i].lnb_data) {
917                         int j, apages;
918                         LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
919                         /* buffer loaned for zerocopy, try to use it.
920                          * notice that dmu_assign_arcbuf() is smart
921                          * enough to recognize changed blocksize
922                          * in this case it fallbacks to dmu_write() */
923                         abufsz = arc_buf_size(lnb[i].lnb_data);
924                         LASSERT(abufsz & PAGE_MASK);
925                         apages = abufsz >> PAGE_SHIFT;
926                         LASSERT(i + apages <= npages);
927                         /* these references to pages must be invalidated
928                          * to prevent access in osd_bufs_put() */
929                         for (j = 0; j < apages; j++)
930                                 lnb[i + j].lnb_page = NULL;
931                         dmu_assign_arcbuf(&obj->oo_dn->dn_bonus->db,
932                                           lnb[i].lnb_file_offset,
933                                           lnb[i].lnb_data, oh->ot_tx);
934                         /* drop the reference, otherwise osd_put_bufs()
935                          * will be releasing it - bad! */
936                         lnb[i].lnb_data = NULL;
937                         atomic_dec(&osd->od_zerocopy_loan);
938                         iosize += abufsz;
939                 } else {
940                         /* we don't want to deal with cache if nothing
941                          * has been send to ZFS at this step */
942                         continue;
943                 }
944
945                 if (!drop_cache)
946                         continue;
947
948                 /* we have to mark dbufs for eviction here because
949                  * dmu_assign_arcbuf() may create a new dbuf for
950                  * loaned abuf */
951                 osd_evict_dbufs_after_write(obj, lnb[i].lnb_file_offset,
952                                             abufsz);
953         }
954
955         if (unlikely(new_size == 0)) {
956                 /* no pages to write, no transno is needed */
957                 th->th_local = 1;
958                 /* it is important to return 0 even when all lnb_rc == -ENOSPC
959                  * since ofd_commitrw_write() retries several times on ENOSPC */
960                 up_read(&obj->oo_guard);
961                 record_end_io(osd, WRITE, 0, 0, 0);
962                 RETURN(0);
963         }
964
965         /* if file has grown, take user_size into account */
966         if (user_size && new_size > user_size)
967                 new_size = user_size;
968         write_lock(&obj->oo_attr_lock);
969         if (obj->oo_attr.la_size < new_size) {
970                 obj->oo_attr.la_size = new_size;
971                 write_unlock(&obj->oo_attr_lock);
972                 /* osd_object_sa_update() will be copying directly from
973                  * oo_attr into dbuf. any update within a single txg will copy
974                  * the most actual */
975                 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
976                                           &obj->oo_attr.la_size, 8, oh);
977         } else {
978                 write_unlock(&obj->oo_attr_lock);
979         }
980
981         up_read(&obj->oo_guard);
982
983         record_end_io(osd, WRITE, 0, iosize, npages);
984
985         RETURN(rc);
986 }
987
988 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
989                         struct niobuf_local *lnb, int npages)
990 {
991         struct osd_object *obj  = osd_dt_obj(dt);
992         int                i;
993         loff_t             eof;
994
995         LASSERT(dt_object_exists(dt));
996         LASSERT(obj->oo_dn);
997
998         read_lock(&obj->oo_attr_lock);
999         eof = obj->oo_attr.la_size;
1000         read_unlock(&obj->oo_attr_lock);
1001
1002         for (i = 0; i < npages; i++) {
1003                 if (unlikely(lnb[i].lnb_rc < 0))
1004                         continue;
1005
1006                 lnb[i].lnb_rc = lnb[i].lnb_len;
1007
1008                 if (lnb[i].lnb_file_offset + lnb[i].lnb_len >= eof) {
1009                         /* send complete pages all the time */
1010                         if (eof <= lnb[i].lnb_file_offset)
1011                                 lnb[i].lnb_rc = 0;
1012
1013                         /* all subsequent rc should be 0 */
1014                         while (++i < npages)
1015                                 lnb[i].lnb_rc = 0;
1016                         break;
1017                 }
1018         }
1019
1020         return 0;
1021 }
1022
1023 /*
1024  * Punch/truncate an object
1025  *
1026  *      IN:     db  - dmu_buf of the object to free data in.
1027  *              off - start of section to free.
1028  *              len - length of section to free (DMU_OBJECT_END => to EOF).
1029  *
1030  *      RETURN: 0 if success
1031  *              error code if failure
1032  *
1033  * The transaction passed to this routine must have
1034  * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
1035  * called and then assigned to a transaction group.
1036  */
1037 static int __osd_object_punch(struct osd_object *obj, objset_t *os,
1038                               dmu_tx_t *tx, uint64_t off, uint64_t len)
1039 {
1040         dnode_t *dn = obj->oo_dn;
1041         uint64_t size = obj->oo_attr.la_size;
1042         int rc = 0;
1043
1044         /* Assert that the transaction has been assigned to a
1045            transaction group. */
1046         LASSERT(tx->tx_txg != 0);
1047         /*
1048          * Nothing to do if file already at desired length.
1049          */
1050         if (len == DMU_OBJECT_END && size == off)
1051                 return 0;
1052
1053         /* if object holds encrypted content, we need to make sure we truncate
1054          * on an encryption unit boundary, or subsequent reads will get
1055          * corrupted content
1056          */
1057         if (len != DMU_OBJECT_END)
1058                 len -= LUSTRE_ENCRYPTION_UNIT_SIZE -
1059                         (off & ~LUSTRE_ENCRYPTION_MASK);
1060         if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
1061             off & ~LUSTRE_ENCRYPTION_MASK)
1062                 off = (off & LUSTRE_ENCRYPTION_MASK) +
1063                         LUSTRE_ENCRYPTION_UNIT_SIZE;
1064
1065
1066         /* XXX: dnode_free_range() can be used to save on dnode lookup */
1067         if (off < size)
1068                 dmu_free_range(os, dn->dn_object, off, len, tx);
1069
1070         return rc;
1071 }
1072
1073 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
1074                         __u64 start, __u64 end, struct thandle *th)
1075 {
1076         struct osd_object  *obj = osd_dt_obj(dt);
1077         struct osd_device  *osd = osd_obj2dev(obj);
1078         struct osd_thandle *oh;
1079         __u64               len;
1080         int                 rc = 0;
1081         ENTRY;
1082
1083         LASSERT(dt_object_exists(dt));
1084         LASSERT(osd_invariant(obj));
1085
1086         LASSERT(th != NULL);
1087         oh = container_of(th, struct osd_thandle, ot_super);
1088
1089         write_lock(&obj->oo_attr_lock);
1090         /* truncate */
1091         if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1092                 len = DMU_OBJECT_END;
1093         else
1094                 len = end - start;
1095         write_unlock(&obj->oo_attr_lock);
1096
1097         down_read(&obj->oo_guard);
1098         if (obj->oo_destroyed)
1099                 GOTO(out, rc = -ENOENT);
1100
1101         rc = __osd_object_punch(obj, osd->od_os, oh->ot_tx, start, len);
1102
1103         /* set new size */
1104         if (len == DMU_OBJECT_END) {
1105                 write_lock(&obj->oo_attr_lock);
1106                 obj->oo_attr.la_size = start;
1107                 write_unlock(&obj->oo_attr_lock);
1108                 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
1109                                           &obj->oo_attr.la_size, 8, oh);
1110         }
1111 out:
1112         up_read(&obj->oo_guard);
1113         RETURN(rc);
1114 }
1115
1116 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
1117                         __u64 start, __u64 end, struct thandle *handle)
1118 {
1119         struct osd_object  *obj = osd_dt_obj(dt);
1120         struct osd_device  *osd = osd_obj2dev(obj);
1121         struct osd_thandle *oh;
1122         __u64               len;
1123         ENTRY;
1124
1125         oh = container_of(handle, struct osd_thandle, ot_super);
1126
1127         read_lock(&obj->oo_attr_lock);
1128         if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1129                 len = DMU_OBJECT_END;
1130         else
1131                 len = end - start;
1132
1133         /* declare we'll free some blocks ... */
1134         /* if object holds encrypted content, we need to make sure we truncate
1135          * on an encryption unit boundary, or subsequent reads will get
1136          * corrupted content
1137          */
1138         if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
1139             start & ~LUSTRE_ENCRYPTION_MASK)
1140                 start = (start & LUSTRE_ENCRYPTION_MASK) +
1141                         LUSTRE_ENCRYPTION_UNIT_SIZE;
1142         if (start < obj->oo_attr.la_size) {
1143                 read_unlock(&obj->oo_attr_lock);
1144                 dmu_tx_mark_netfree(oh->ot_tx);
1145                 dmu_tx_hold_free(oh->ot_tx, obj->oo_dn->dn_object, start, len);
1146         } else {
1147                 read_unlock(&obj->oo_attr_lock);
1148         }
1149
1150         RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
1151                                  obj->oo_attr.la_gid, obj->oo_attr.la_projid,
1152                                  0, oh, NULL, OSD_QID_BLK));
1153 }
1154
1155 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
1156                        __u64 start, __u64 end, enum lu_ladvise_type advice)
1157 {
1158         int     rc;
1159         ENTRY;
1160
1161         switch (advice) {
1162         default:
1163                 rc = -ENOTSUPP;
1164                 break;
1165         }
1166
1167         RETURN(rc);
1168 }
1169
1170 static int osd_fallocate(const struct lu_env *env, struct dt_object *dt,
1171                          __u64 start, __u64 end, int mode, struct thandle *th)
1172 {
1173         int rc = -EOPNOTSUPP;
1174         ENTRY;
1175
1176          /*
1177           * space preallocation is not supported for ZFS
1178           * Returns -EOPNOTSUPP for now
1179           */
1180         RETURN(rc);
1181 }
1182
1183 static int osd_declare_fallocate(const struct lu_env *env,
1184                                  struct dt_object *dt, __u64 start, __u64 end,
1185                                  int mode, struct thandle *th)
1186 {
1187         int rc = -EOPNOTSUPP;
1188         ENTRY;
1189
1190          /*
1191           * space preallocation is not supported for ZFS
1192           * Returns -EOPNOTSUPP for now
1193           */
1194         RETURN(rc);
1195 }
1196
1197 static loff_t osd_lseek(const struct lu_env *env, struct dt_object *dt,
1198                         loff_t offset, int whence)
1199 {
1200         struct osd_object *obj = osd_dt_obj(dt);
1201         struct osd_device *osd = osd_obj2dev(obj);
1202         uint64_t size = obj->oo_attr.la_size;
1203         uint64_t result = offset;
1204         int rc;
1205         boolean_t hole = whence == SEEK_HOLE;
1206
1207         ENTRY;
1208
1209         LASSERT(dt_object_exists(dt));
1210         LASSERT(osd_invariant(obj));
1211         LASSERT(offset >= 0);
1212
1213         /* for SEEK_HOLE treat 'offset' beyond the end of file as in real
1214          * hole. LOV to decide after all if that real hole or not.
1215          */
1216         if (offset >= size)
1217                 RETURN(hole ? offset : -ENXIO);
1218
1219         /* Currently ZFS reports no valid DATA offset if object has dirty data
1220          * and we cannot just switch to generic way with reporting DATA on all
1221          * file offsets and HOLE beyond end of file, because we may get HOLE
1222          * reported correctly at some offset inside file then DATA will find
1223          * dirty state and be reported also at that offset by generic approach.
1224          * This is because for HOLE report ZFS doesn't check dirty state but
1225          * does for DATA.
1226          * The only way to get reliable results is to call txg_wait_synced()
1227          * when ZFS reports EBUSY result and repeat lseek call and that is
1228          * controlled via od_sync_on_lseek option.
1229          */
1230         if (!osd->od_sync_on_lseek)
1231                 result = hole ? size : offset;
1232
1233 again:
1234         rc = osd_dmu_offset_next(osd->od_os, obj->oo_dn->dn_object, hole,
1235                                  &result);
1236         /* dirty inode, lseek result is unreliable without sync */
1237         if (rc == EBUSY) {
1238                 txg_wait_synced(dmu_objset_pool(osd->od_os), 0ULL);
1239                 goto again;
1240         }
1241
1242         if (rc == ESRCH)
1243                 RETURN(-ENXIO);
1244
1245         /* ZFS is not exported all needed function, so fall back to the
1246          * generic logic: for HOLE return file size, for DATA return
1247          * the current offset
1248          */
1249         if (rc == EOPNOTSUPP)
1250                 result = hole ? size : offset;
1251         else if (rc)
1252                 return -rc;
1253
1254         /* dmu_offset_next() only works on whole blocks so may return SEEK_HOLE
1255          * result as end of the last block instead of logical EOF which we need
1256          */
1257         if (result > size)
1258                 result = size;
1259
1260         RETURN(result);
1261 }
1262
1263 const struct dt_body_operations osd_body_ops = {
1264         .dbo_read                       = osd_read,
1265         .dbo_declare_write              = osd_declare_write,
1266         .dbo_write                      = osd_write,
1267         .dbo_bufs_get                   = osd_bufs_get,
1268         .dbo_bufs_put                   = osd_bufs_put,
1269         .dbo_write_prep                 = osd_write_prep,
1270         .dbo_declare_write_commit       = osd_declare_write_commit,
1271         .dbo_write_commit               = osd_write_commit,
1272         .dbo_read_prep                  = osd_read_prep,
1273         .dbo_declare_punch              = osd_declare_punch,
1274         .dbo_punch                      = osd_punch,
1275         .dbo_ladvise                    = osd_ladvise,
1276         .dbo_declare_fallocate          = osd_declare_fallocate,
1277         .dbo_fallocate                  = osd_fallocate,
1278         .dbo_lseek                      = osd_lseek,
1279 };
1280
1281 const struct dt_body_operations osd_body_scrub_ops = {
1282         .dbo_read                       = osd_read_no_record,
1283         .dbo_declare_write              = osd_declare_write,
1284         .dbo_write                      = osd_write,
1285 };