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