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