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