Whamcloud - gitweb
LU-15451 sec: read-only nodemap flag
[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         down_read(&obj->oo_guard);
593
594         if (unlikely(!dt_object_exists(dt) || obj->oo_destroyed))
595                 GOTO(out, rc = -ENOENT);
596
597         if (rw & DT_BUFS_TYPE_WRITE)
598                 rc = osd_bufs_get_write(env, obj, offset, len, lnb, maxlnb);
599         else
600                 rc = osd_bufs_get_read(env, obj, offset, len, lnb, maxlnb);
601
602 out:
603         up_read(&obj->oo_guard);
604         return rc;
605 }
606
607 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
608                         struct niobuf_local *lnb, int npages)
609 {
610         struct osd_object *obj = osd_dt_obj(dt);
611
612         LASSERT(dt_object_exists(dt));
613         LASSERT(obj->oo_dn);
614
615         return 0;
616 }
617
618 static inline uint64_t osd_roundup2blocksz(uint64_t size,
619                                            uint64_t offset,
620                                            uint32_t blksz)
621 {
622         LASSERT(blksz > 0);
623
624         size += offset % blksz;
625
626         if (likely(is_power_of_2(blksz)))
627                 return round_up(size, blksz);
628         else
629                 return DIV_ROUND_UP_ULL(size, blksz) * blksz;
630 }
631
632 static int osd_declare_write_commit(const struct lu_env *env,
633                                     struct dt_object *dt,
634                                     struct niobuf_local *lnb, int npages,
635                                     struct thandle *th)
636 {
637         struct osd_object  *obj = osd_dt_obj(dt);
638         struct osd_device  *osd = osd_obj2dev(obj);
639         struct osd_thandle *oh;
640         uint64_t            offset = 0;
641         uint32_t            size = 0;
642         uint32_t blksz = obj->oo_dn->dn_datablksz;
643         int                 i, rc;
644         bool synced = false;
645         long long           space = 0;
646         struct page        *last_page = NULL;
647         unsigned long       discont_pages = 0;
648         enum osd_quota_local_flags local_flags = 0;
649         enum osd_qid_declare_flags declare_flags = OSD_QID_BLK;
650         ENTRY;
651
652         LASSERT(dt_object_exists(dt));
653         LASSERT(obj->oo_dn);
654
655         LASSERT(lnb);
656         LASSERT(npages > 0);
657
658         oh = container_of(th, struct osd_thandle, ot_super);
659
660         for (i = 0; i < npages; i++) {
661                 if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
662                         ++discont_pages;
663                 last_page = lnb[i].lnb_page;
664                 if (lnb[i].lnb_rc)
665                         /* ENOSPC, network RPC error, etc.
666                          * We don't want to book space for pages which will be
667                          * skipped in osd_write_commit(). Hence we skip pages
668                          * with lnb_rc != 0 here too */
669                         continue;
670                 /* ignore quota for the whole request if any page is from
671                  * client cache or written by root.
672                  *
673                  * XXX we could handle this on per-lnb basis as done by
674                  * grant. */
675                 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
676                     (lnb[i].lnb_flags & OBD_BRW_SYS_RESOURCE) ||
677                     !(lnb[i].lnb_flags & OBD_BRW_SYNC))
678                         declare_flags |= OSD_QID_FORCE;
679
680                 if (size == 0) {
681                         /* first valid lnb */
682                         offset = lnb[i].lnb_file_offset;
683                         size = lnb[i].lnb_len;
684                         continue;
685                 }
686                 if (offset + size == lnb[i].lnb_file_offset) {
687                         /* this lnb is contiguous to the previous one */
688                         size += lnb[i].lnb_len;
689                         continue;
690                 }
691
692                 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object,
693                                   obj->oo_dn, offset, size);
694                 /* Estimating space to be consumed by a write is rather
695                  * complicated with ZFS. As a consequence, we don't account for
696                  * indirect blocks and just use as a rough estimate the worse
697                  * case where the old space is being held by a snapshot. Quota
698                  * overrun will be adjusted once the operation is committed, if
699                  * required. */
700                 space += osd_roundup2blocksz(size, offset, blksz);
701
702                 offset = lnb[i].lnb_file_offset;
703                 size = lnb[i].lnb_len;
704         }
705
706         if (size) {
707                 osd_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object, obj->oo_dn,
708                                   offset, size);
709                 space += osd_roundup2blocksz(size, offset, blksz);
710         }
711
712         /* backend zfs filesystem might be configured to store multiple data
713          * copies */
714         space  *= osd->od_os->os_copies;
715         space   = toqb(space);
716         CDEBUG(D_QUOTA, "writing %d pages, reserving %lldK of quota space\n",
717                npages, space);
718
719         record_start_io(osd, WRITE, discont_pages);
720 retry:
721         /* acquire quota space if needed */
722         rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
723                                obj->oo_attr.la_gid, obj->oo_attr.la_projid,
724                                space, oh, &local_flags, declare_flags);
725
726         if (!synced && rc == -EDQUOT &&
727             (local_flags & QUOTA_FL_SYNC) != 0) {
728                 dt_sync(env, th->th_dev);
729                 synced = true;
730                 CDEBUG(D_QUOTA, "retry after sync\n");
731                 local_flags = 0;
732                 goto retry;
733         }
734
735         /* we need only to store the overquota flags in the first lnb for
736          * now, once we support multiple objects BRW, this code needs be
737          * revised. */
738         if (local_flags & QUOTA_FL_OVER_USRQUOTA)
739                 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
740         if (local_flags & QUOTA_FL_OVER_GRPQUOTA)
741                 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
742 #ifdef ZFS_PROJINHERIT
743         if (local_flags & QUOTA_FL_OVER_PRJQUOTA)
744                 lnb[0].lnb_flags |= OBD_BRW_OVER_PRJQUOTA;
745 #endif
746
747         RETURN(rc);
748 }
749
750 /**
751  * Policy to grow ZFS block size by write pattern.
752  * For sequential write, it grows block size gradually until it reaches the
753  * maximum blocksize the dataset can support. Otherwise, it will pick a
754  * a block size by the writing region of this I/O.
755  */
756 static int osd_grow_blocksize(struct osd_object *obj, struct osd_thandle *oh,
757                               uint64_t start, uint64_t end)
758 {
759         struct osd_device       *osd = osd_obj2dev(obj);
760         dnode_t *dn = obj->oo_dn;
761         uint32_t                 blksz;
762         int                      rc = 0;
763
764         ENTRY;
765
766         if (dn->dn_maxblkid > 0) /* can't change block size */
767                 GOTO(out, rc);
768
769         if (dn->dn_datablksz >= osd->od_max_blksz)
770                 GOTO(out, rc);
771
772         down_write(&obj->oo_guard);
773
774         blksz = dn->dn_datablksz;
775         if (blksz >= osd->od_max_blksz) /* check again after grabbing lock */
776                 GOTO(out_unlock, rc);
777
778         /* now ZFS can support up to 16MB block size, and if the write
779          * is sequential, it just increases the block size gradually */
780         if (start <= blksz) { /* sequential */
781                 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz, end);
782         } else { /* sparse, pick a block size by write region */
783                 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz,
784                                         end - start);
785         }
786
787         if (!is_power_of_2(blksz))
788                 blksz = size_roundup_power2(blksz);
789
790         if (blksz > dn->dn_datablksz) {
791                 rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
792                                                blksz, 0, oh->ot_tx);
793                 LASSERT(ergo(rc == 0, dn->dn_datablksz >= blksz));
794                 if (rc < 0)
795                         CDEBUG(D_INODE, "object "DFID": change block size"
796                                "%u -> %u error rc = %d\n",
797                                PFID(lu_object_fid(&obj->oo_dt.do_lu)),
798                                dn->dn_datablksz, blksz, rc);
799         }
800         EXIT;
801 out_unlock:
802         up_write(&obj->oo_guard);
803 out:
804         return rc;
805 }
806
807 static void osd_evict_dbufs_after_write(struct osd_object *obj,
808                                         loff_t off, ssize_t len)
809 {
810         dmu_buf_t **dbp;
811         int i, rc, numbufs;
812
813         rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db, off, len,
814                                           TRUE, osd_0copy_tag, &numbufs, &dbp);
815         if (unlikely(rc))
816                 return;
817
818         for (i = 0; i < numbufs; i++)
819                 dbuf_set_pending_evict(dbp[i]);
820
821         dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
822 }
823
824 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
825                         struct niobuf_local *lnb, int npages,
826                         struct thandle *th, __u64 user_size)
827 {
828         struct osd_object  *obj  = osd_dt_obj(dt);
829         struct osd_device  *osd = osd_obj2dev(obj);
830         struct osd_thandle *oh;
831         uint64_t            new_size = 0;
832         int                 i, abufsz, rc = 0, drop_cache = 0;
833         unsigned long      iosize = 0;
834         ENTRY;
835
836         LASSERT(dt_object_exists(dt));
837         LASSERT(obj->oo_dn);
838
839         LASSERT(th != NULL);
840         oh = container_of(th, struct osd_thandle, ot_super);
841
842         /* adjust block size. Assume the buffers are sorted. */
843         (void)osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
844                                  lnb[npages - 1].lnb_file_offset +
845                                  lnb[npages - 1].lnb_len);
846
847         if (obj->oo_attr.la_size >= osd->od_readcache_max_filesize ||
848             lnb[npages - 1].lnb_file_offset + lnb[npages - 1].lnb_len >=
849             osd->od_readcache_max_filesize)
850                 drop_cache = 1;
851
852         if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC))
853                 RETURN(-ENOSPC);
854
855         /* if la_size is already bigger than specified user_size,
856          * ignore user_size
857          */
858         if (obj->oo_attr.la_size > user_size)
859                 user_size = 0;
860
861         /* LU-8791: take oo_guard to avoid the deadlock that changing block
862          * size and assigning arcbuf take place at the same time.
863          *
864          * Thread 1:
865          * osd_write_commit()
866          *  -> osd_grow_blocksize() with osd_object::oo_guard held
867          *   -> dmu_object_set_blocksize()
868          *    -> dnode_set_blksz(), with dnode_t::dn_struct_rwlock
869          *       write lock held
870          *     -> dbuf_new_size()
871          *      -> dmu_buf_will_dirty()
872          *       -> dbuf_read()
873          *        -> wait for the dbuf state to change
874          * Thread 2:
875          * osd_write_commit()
876          *  -> dmu_assign_arcbuf()
877          *   -> dbuf_assign_arcbuf(), set dbuf state to DB_FILL
878          *    -> dbuf_dirty()
879          *     -> try to hold the read lock of dnode_t::dn_struct_rwlock
880          *
881          * By taking the read lock, it can avoid thread 2 to enter into the
882          * critical section of assigning the arcbuf, while thread 1 is
883          * changing the block size.
884          */
885         down_read(&obj->oo_guard);
886         if (obj->oo_destroyed) {
887                 up_read(&obj->oo_guard);
888                 RETURN(-ENOENT);
889         }
890
891         for (i = 0; i < npages; i++) {
892                 CDEBUG(D_INODE, "write %u bytes at %u\n",
893                         (unsigned) lnb[i].lnb_len,
894                         (unsigned) lnb[i].lnb_file_offset);
895
896                 if (lnb[i].lnb_rc) {
897                         /* ENOSPC, network RPC error, etc.
898                          * Unlike ldiskfs, zfs allocates new blocks on rewrite,
899                          * so we skip this page if lnb_rc is set to -ENOSPC */
900                         CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
901                                 PFID(lu_object_fid(&dt->do_lu)), i,
902                                 lnb[i].lnb_rc);
903                         continue;
904                 }
905
906                 if (new_size < lnb[i].lnb_file_offset + lnb[i].lnb_len)
907                         new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
908                 if (lnb[i].lnb_page == NULL)
909                         continue;
910
911                 if (lnb[i].lnb_page->mapping == (void *)obj) {
912                         osd_dmu_write(osd, obj->oo_dn, lnb[i].lnb_file_offset,
913                                       lnb[i].lnb_len, kmap(lnb[i].lnb_page) +
914                                       lnb[i].lnb_page_offset, oh->ot_tx);
915                         kunmap(lnb[i].lnb_page);
916                         iosize += lnb[i].lnb_len;
917                         abufsz = lnb[i].lnb_len; /* to drop cache below */
918                 } else if (lnb[i].lnb_data) {
919                         int j, apages;
920                         LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
921                         /* buffer loaned for zerocopy, try to use it.
922                          * notice that dmu_assign_arcbuf() is smart
923                          * enough to recognize changed blocksize
924                          * in this case it fallbacks to dmu_write() */
925                         abufsz = arc_buf_size(lnb[i].lnb_data);
926                         LASSERT(abufsz & PAGE_MASK);
927                         apages = abufsz >> PAGE_SHIFT;
928                         LASSERT(i + apages <= npages);
929                         /* these references to pages must be invalidated
930                          * to prevent access in osd_bufs_put() */
931                         for (j = 0; j < apages; j++)
932                                 lnb[i + j].lnb_page = NULL;
933                         dmu_assign_arcbuf(&obj->oo_dn->dn_bonus->db,
934                                           lnb[i].lnb_file_offset,
935                                           lnb[i].lnb_data, oh->ot_tx);
936                         /* drop the reference, otherwise osd_put_bufs()
937                          * will be releasing it - bad! */
938                         lnb[i].lnb_data = NULL;
939                         atomic_dec(&osd->od_zerocopy_loan);
940                         iosize += abufsz;
941                 } else {
942                         /* we don't want to deal with cache if nothing
943                          * has been send to ZFS at this step */
944                         continue;
945                 }
946
947                 if (!drop_cache)
948                         continue;
949
950                 /* we have to mark dbufs for eviction here because
951                  * dmu_assign_arcbuf() may create a new dbuf for
952                  * loaned abuf */
953                 osd_evict_dbufs_after_write(obj, lnb[i].lnb_file_offset,
954                                             abufsz);
955         }
956
957         if (unlikely(new_size == 0)) {
958                 /* no pages to write, no transno is needed */
959                 th->th_local = 1;
960                 /* it is important to return 0 even when all lnb_rc == -ENOSPC
961                  * since ofd_commitrw_write() retries several times on ENOSPC */
962                 up_read(&obj->oo_guard);
963                 record_end_io(osd, WRITE, 0, 0, 0);
964                 RETURN(0);
965         }
966
967         /* if file has grown, take user_size into account */
968         if (user_size && new_size > user_size)
969                 new_size = user_size;
970         write_lock(&obj->oo_attr_lock);
971         if (obj->oo_attr.la_size < new_size) {
972                 obj->oo_attr.la_size = new_size;
973                 write_unlock(&obj->oo_attr_lock);
974                 /* osd_object_sa_update() will be copying directly from
975                  * oo_attr into dbuf. any update within a single txg will copy
976                  * the most actual */
977                 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
978                                           &obj->oo_attr.la_size, 8, oh);
979         } else {
980                 write_unlock(&obj->oo_attr_lock);
981         }
982
983         up_read(&obj->oo_guard);
984
985         record_end_io(osd, WRITE, 0, iosize, npages);
986
987         RETURN(rc);
988 }
989
990 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
991                         struct niobuf_local *lnb, int npages)
992 {
993         struct osd_object *obj  = osd_dt_obj(dt);
994         int                i;
995         loff_t             eof;
996
997         LASSERT(dt_object_exists(dt));
998         LASSERT(obj->oo_dn);
999
1000         read_lock(&obj->oo_attr_lock);
1001         eof = obj->oo_attr.la_size;
1002         read_unlock(&obj->oo_attr_lock);
1003
1004         for (i = 0; i < npages; i++) {
1005                 if (unlikely(lnb[i].lnb_rc < 0))
1006                         continue;
1007
1008                 lnb[i].lnb_rc = lnb[i].lnb_len;
1009
1010                 if (lnb[i].lnb_file_offset + lnb[i].lnb_len >= eof) {
1011                         /* send complete pages all the time */
1012                         if (eof <= lnb[i].lnb_file_offset)
1013                                 lnb[i].lnb_rc = 0;
1014
1015                         /* all subsequent rc should be 0 */
1016                         while (++i < npages)
1017                                 lnb[i].lnb_rc = 0;
1018                         break;
1019                 }
1020         }
1021
1022         return 0;
1023 }
1024
1025 /*
1026  * Punch/truncate an object
1027  *
1028  *      IN:     db  - dmu_buf of the object to free data in.
1029  *              off - start of section to free.
1030  *              len - length of section to free (DMU_OBJECT_END => to EOF).
1031  *
1032  *      RETURN: 0 if success
1033  *              error code if failure
1034  *
1035  * The transaction passed to this routine must have
1036  * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
1037  * called and then assigned to a transaction group.
1038  */
1039 static int __osd_object_punch(struct osd_object *obj, objset_t *os,
1040                               dmu_tx_t *tx, uint64_t off, uint64_t len)
1041 {
1042         dnode_t *dn = obj->oo_dn;
1043         uint64_t size = obj->oo_attr.la_size;
1044         int rc = 0;
1045
1046         /* Assert that the transaction has been assigned to a
1047            transaction group. */
1048         LASSERT(tx->tx_txg != 0);
1049         /*
1050          * Nothing to do if file already at desired length.
1051          */
1052         if (len == DMU_OBJECT_END && size == off)
1053                 return 0;
1054
1055         /* if object holds encrypted content, we need to make sure we truncate
1056          * on an encryption unit boundary, or subsequent reads will get
1057          * corrupted content
1058          */
1059         if (len != DMU_OBJECT_END)
1060                 len -= LUSTRE_ENCRYPTION_UNIT_SIZE -
1061                         (off & ~LUSTRE_ENCRYPTION_MASK);
1062         if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
1063             off & ~LUSTRE_ENCRYPTION_MASK)
1064                 off = (off & LUSTRE_ENCRYPTION_MASK) +
1065                         LUSTRE_ENCRYPTION_UNIT_SIZE;
1066
1067
1068         /* XXX: dnode_free_range() can be used to save on dnode lookup */
1069         if (off < size)
1070                 dmu_free_range(os, dn->dn_object, off, len, tx);
1071
1072         return rc;
1073 }
1074
1075 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
1076                         __u64 start, __u64 end, struct thandle *th)
1077 {
1078         struct osd_object  *obj = osd_dt_obj(dt);
1079         struct osd_device  *osd = osd_obj2dev(obj);
1080         struct osd_thandle *oh;
1081         __u64               len;
1082         int                 rc = 0;
1083         ENTRY;
1084
1085         LASSERT(dt_object_exists(dt));
1086         LASSERT(osd_invariant(obj));
1087
1088         LASSERT(th != NULL);
1089         oh = container_of(th, struct osd_thandle, ot_super);
1090
1091         write_lock(&obj->oo_attr_lock);
1092         /* truncate */
1093         if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1094                 len = DMU_OBJECT_END;
1095         else
1096                 len = end - start;
1097         write_unlock(&obj->oo_attr_lock);
1098
1099         down_read(&obj->oo_guard);
1100         if (obj->oo_destroyed)
1101                 GOTO(out, rc = -ENOENT);
1102
1103         rc = __osd_object_punch(obj, osd->od_os, oh->ot_tx, start, len);
1104
1105         /* set new size */
1106         if (len == DMU_OBJECT_END) {
1107                 write_lock(&obj->oo_attr_lock);
1108                 obj->oo_attr.la_size = start;
1109                 write_unlock(&obj->oo_attr_lock);
1110                 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
1111                                           &obj->oo_attr.la_size, 8, oh);
1112         }
1113 out:
1114         up_read(&obj->oo_guard);
1115         RETURN(rc);
1116 }
1117
1118 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
1119                         __u64 start, __u64 end, struct thandle *handle)
1120 {
1121         struct osd_object  *obj = osd_dt_obj(dt);
1122         struct osd_device  *osd = osd_obj2dev(obj);
1123         struct osd_thandle *oh;
1124         __u64               len;
1125         ENTRY;
1126
1127         oh = container_of(handle, struct osd_thandle, ot_super);
1128
1129         read_lock(&obj->oo_attr_lock);
1130         if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
1131                 len = DMU_OBJECT_END;
1132         else
1133                 len = end - start;
1134
1135         /* declare we'll free some blocks ... */
1136         /* if object holds encrypted content, we need to make sure we truncate
1137          * on an encryption unit boundary, or subsequent reads will get
1138          * corrupted content
1139          */
1140         if (obj->oo_lma_flags & LUSTRE_ENCRYPT_FL &&
1141             start & ~LUSTRE_ENCRYPTION_MASK)
1142                 start = (start & LUSTRE_ENCRYPTION_MASK) +
1143                         LUSTRE_ENCRYPTION_UNIT_SIZE;
1144         if (start < obj->oo_attr.la_size) {
1145                 read_unlock(&obj->oo_attr_lock);
1146                 dmu_tx_mark_netfree(oh->ot_tx);
1147                 dmu_tx_hold_free(oh->ot_tx, obj->oo_dn->dn_object, start, len);
1148         } else {
1149                 read_unlock(&obj->oo_attr_lock);
1150         }
1151
1152         RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
1153                                  obj->oo_attr.la_gid, obj->oo_attr.la_projid,
1154                                  0, oh, NULL, OSD_QID_BLK));
1155 }
1156
1157 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
1158                        __u64 start, __u64 end, enum lu_ladvise_type advice)
1159 {
1160         int     rc;
1161         ENTRY;
1162
1163         switch (advice) {
1164         default:
1165                 rc = -ENOTSUPP;
1166                 break;
1167         }
1168
1169         RETURN(rc);
1170 }
1171
1172 static int osd_fallocate(const struct lu_env *env, struct dt_object *dt,
1173                          __u64 start, __u64 end, int mode, struct thandle *th)
1174 {
1175         int rc = -EOPNOTSUPP;
1176         ENTRY;
1177
1178          /*
1179           * space preallocation is not supported for ZFS
1180           * Returns -EOPNOTSUPP for now
1181           */
1182         RETURN(rc);
1183 }
1184
1185 static int osd_declare_fallocate(const struct lu_env *env,
1186                                  struct dt_object *dt, __u64 start, __u64 end,
1187                                  int mode, struct thandle *th)
1188 {
1189         int rc = -EOPNOTSUPP;
1190         ENTRY;
1191
1192          /*
1193           * space preallocation is not supported for ZFS
1194           * Returns -EOPNOTSUPP for now
1195           */
1196         RETURN(rc);
1197 }
1198
1199 static loff_t osd_lseek(const struct lu_env *env, struct dt_object *dt,
1200                         loff_t offset, int whence)
1201 {
1202         struct osd_object *obj = osd_dt_obj(dt);
1203         struct osd_device *osd = osd_obj2dev(obj);
1204         uint64_t size = obj->oo_attr.la_size;
1205         uint64_t result = offset;
1206         int rc;
1207         boolean_t hole = whence == SEEK_HOLE;
1208
1209         ENTRY;
1210
1211         LASSERT(dt_object_exists(dt));
1212         LASSERT(osd_invariant(obj));
1213         LASSERT(offset >= 0);
1214
1215         /* for SEEK_HOLE treat 'offset' beyond the end of file as in real
1216          * hole. LOV to decide after all if that real hole or not.
1217          */
1218         if (offset >= size)
1219                 RETURN(hole ? offset : -ENXIO);
1220
1221         /* Currently ZFS reports no valid DATA offset if object has dirty data
1222          * and we cannot just switch to generic way with reporting DATA on all
1223          * file offsets and HOLE beyond end of file, because we may get HOLE
1224          * reported correctly at some offset inside file then DATA will find
1225          * dirty state and be reported also at that offset by generic approach.
1226          * This is because for HOLE report ZFS doesn't check dirty state but
1227          * does for DATA.
1228          * The only way to get reliable results is to call txg_wait_synced()
1229          * when ZFS reports EBUSY result and repeat lseek call and that is
1230          * controlled via od_sync_on_lseek option.
1231          */
1232         if (!osd->od_sync_on_lseek)
1233                 result = hole ? size : offset;
1234
1235 again:
1236         rc = osd_dmu_offset_next(osd->od_os, obj->oo_dn->dn_object, hole,
1237                                  &result);
1238         /* dirty inode, lseek result is unreliable without sync */
1239         if (rc == EBUSY) {
1240                 txg_wait_synced(dmu_objset_pool(osd->od_os), 0ULL);
1241                 goto again;
1242         }
1243
1244         if (rc == ESRCH)
1245                 RETURN(-ENXIO);
1246
1247         /* ZFS is not exported all needed function, so fall back to the
1248          * generic logic: for HOLE return file size, for DATA return
1249          * the current offset
1250          */
1251         if (rc == EOPNOTSUPP)
1252                 result = hole ? size : offset;
1253         else if (rc)
1254                 return -rc;
1255
1256         /* dmu_offset_next() only works on whole blocks so may return SEEK_HOLE
1257          * result as end of the last block instead of logical EOF which we need
1258          */
1259         if (result > size)
1260                 result = size;
1261
1262         RETURN(result);
1263 }
1264
1265 const struct dt_body_operations osd_body_ops = {
1266         .dbo_read                       = osd_read,
1267         .dbo_declare_write              = osd_declare_write,
1268         .dbo_write                      = osd_write,
1269         .dbo_bufs_get                   = osd_bufs_get,
1270         .dbo_bufs_put                   = osd_bufs_put,
1271         .dbo_write_prep                 = osd_write_prep,
1272         .dbo_declare_write_commit       = osd_declare_write_commit,
1273         .dbo_write_commit               = osd_write_commit,
1274         .dbo_read_prep                  = osd_read_prep,
1275         .dbo_declare_punch              = osd_declare_punch,
1276         .dbo_punch                      = osd_punch,
1277         .dbo_ladvise                    = osd_ladvise,
1278         .dbo_declare_fallocate          = osd_declare_fallocate,
1279         .dbo_fallocate                  = osd_fallocate,
1280         .dbo_lseek                      = osd_lseek,
1281 };
1282
1283 const struct dt_body_operations osd_body_scrub_ops = {
1284         .dbo_read                       = osd_read_no_record,
1285         .dbo_declare_write              = osd_declare_write,
1286         .dbo_write                      = osd_write,
1287 };