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