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