Whamcloud - gitweb
LU-4952 osd: wrong paramters for dmu_tx_hold_write
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * Copyright (c) 2012, 2013, Intel Corporation.
32  * Use is subject to license terms.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/osd-zfs/osd_io.c
39  *
40  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
41  * Author: Mike Pershin <tappro@whamcloud.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_OSD
45
46 #include <lustre_ver.h>
47 #include <libcfs/libcfs.h>
48 #include <obd_support.h>
49 #include <lustre_net.h>
50 #include <obd.h>
51 #include <obd_class.h>
52 #include <lustre_disk.h>
53 #include <lustre_fid.h>
54 #include <lustre/lustre_idl.h>  /* LLOG_CHUNK_SIZE definition */
55
56 #include "osd_internal.h"
57
58 #include <sys/dnode.h>
59 #include <sys/dbuf.h>
60 #include <sys/spa.h>
61 #include <sys/stat.h>
62 #include <sys/zap.h>
63 #include <sys/spa_impl.h>
64 #include <sys/zfs_znode.h>
65 #include <sys/dmu_tx.h>
66 #include <sys/dmu_objset.h>
67 #include <sys/dsl_prop.h>
68 #include <sys/sa_impl.h>
69 #include <sys/txg.h>
70
71 static char *osd_zerocopy_tag = "zerocopy";
72
73 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
74                         struct lu_buf *buf, loff_t *pos,
75                         struct lustre_capa *capa)
76 {
77         struct osd_object *obj  = osd_dt_obj(dt);
78         struct osd_device *osd = osd_obj2dev(obj);
79         uint64_t           old_size;
80         int                size = buf->lb_len;
81         int                rc;
82
83         LASSERT(dt_object_exists(dt));
84         LASSERT(obj->oo_db);
85
86         read_lock(&obj->oo_attr_lock);
87         old_size = obj->oo_attr.la_size;
88         read_unlock(&obj->oo_attr_lock);
89
90         if (*pos + size > old_size) {
91                 if (old_size < *pos)
92                         return 0;
93                 else
94                         size = old_size - *pos;
95         }
96
97         rc = -dmu_read(osd->od_objset.os, obj->oo_db->db_object, *pos, size,
98                         buf->lb_buf, DMU_READ_PREFETCH);
99         if (rc == 0) {
100                 rc = size;
101                 *pos += size;
102
103                 /* XXX: workaround for bug in HEAD: fsfilt_ldiskfs_read() returns
104                  * requested number of bytes, not actually read ones */
105                 if (S_ISLNK(obj->oo_dt.do_lu.lo_header->loh_attr))
106                         rc = buf->lb_len;
107         }
108         return rc;
109 }
110
111 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
112                                 const struct lu_buf *buf, loff_t pos,
113                                 struct thandle *th)
114 {
115         struct osd_object  *obj  = osd_dt_obj(dt);
116         struct osd_device  *osd = osd_obj2dev(obj);
117         struct osd_thandle *oh;
118         uint64_t            oid;
119         ENTRY;
120
121         oh = container_of0(th, struct osd_thandle, ot_super);
122
123         /* in some cases declare can race with creation (e.g. llog)
124          * and we need to wait till object is initialized. notice
125          * LOHA_EXISTs is supposed to be the last step in the
126          * initialization */
127
128         /* declare possible size change. notice we can't check
129          * current size here as another thread can change it */
130
131         if (dt_object_exists(dt)) {
132                 LASSERT(obj->oo_db);
133                 oid = obj->oo_db->db_object;
134
135                 dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
136         } else {
137                 oid = DMU_NEW_OBJECT;
138                 dmu_tx_hold_sa_create(oh->ot_tx, ZFS_SA_BASE_ATTR_SIZE);
139         }
140
141         /* XXX: we still miss for append declaration support in ZFS
142          *      -1 means append which is used by llog mostly, llog
143          *      can grow upto LLOG_CHUNK_SIZE*8 records */
144         if (pos == -1)
145                 pos = max_t(loff_t, 256 * 8 * LLOG_CHUNK_SIZE,
146                             obj->oo_attr.la_size + (2 << 20));
147         dmu_tx_hold_write(oh->ot_tx, oid, pos, buf->lb_len);
148
149         /* dt_declare_write() is usually called for system objects, such
150          * as llog or last_rcvd files. We needn't enforce quota on those
151          * objects, so always set the lqi_space as 0. */
152         RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
153                                  obj->oo_attr.la_gid, 0, oh, true, NULL,
154                                  false));
155 }
156
157 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
158                         const struct lu_buf *buf, loff_t *pos,
159                         struct thandle *th, struct lustre_capa *capa,
160                         int ignore_quota)
161 {
162         struct osd_object  *obj  = osd_dt_obj(dt);
163         struct osd_device  *osd = osd_obj2dev(obj);
164         udmu_objset_t      *uos = &osd->od_objset;
165         struct osd_thandle *oh;
166         uint64_t            offset = *pos;
167         int                 rc;
168         ENTRY;
169
170         LASSERT(dt_object_exists(dt));
171         LASSERT(obj->oo_db);
172
173         LASSERT(th != NULL);
174         oh = container_of0(th, struct osd_thandle, ot_super);
175
176         dmu_write(osd->od_objset.os, obj->oo_db->db_object, offset,
177                 (uint64_t)buf->lb_len, buf->lb_buf, oh->ot_tx);
178         write_lock(&obj->oo_attr_lock);
179         if (obj->oo_attr.la_size < offset + buf->lb_len) {
180                 obj->oo_attr.la_size = offset + buf->lb_len;
181                 write_unlock(&obj->oo_attr_lock);
182                 /* osd_object_sa_update() will be copying directly from oo_attr
183                  * into dbuf.  any update within a single txg will copy the
184                  * most actual */
185                 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(uos),
186                                         &obj->oo_attr.la_size, 8, oh);
187                 if (unlikely(rc))
188                         GOTO(out, rc);
189         } else {
190                 write_unlock(&obj->oo_attr_lock);
191         }
192
193         *pos += buf->lb_len;
194         rc = buf->lb_len;
195
196 out:
197         RETURN(rc);
198 }
199
200 /*
201  * XXX: for the moment I don't want to use lnb_flags for osd-internal
202  *      purposes as it's not very well defined ...
203  *      instead I use the lowest bit of the address so that:
204  *        arc buffer:  .lnb_obj = abuf          (arc we loan for write)
205  *        dbuf buffer: .lnb_obj = dbuf | 1      (dbuf we get for read)
206  *        copy buffer: .lnb_page->mapping = obj (page we allocate for write)
207  *
208  *      bzzz, to blame
209  */
210 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
211                         struct niobuf_local *lnb, int npages)
212 {
213         struct osd_object *obj  = osd_dt_obj(dt);
214         struct osd_device *osd = osd_obj2dev(obj);
215         unsigned long      ptr;
216         int                i;
217
218         LASSERT(dt_object_exists(dt));
219         LASSERT(obj->oo_db);
220
221         for (i = 0; i < npages; i++) {
222                 if (lnb[i].page == NULL)
223                         continue;
224                 if (lnb[i].page->mapping == (void *)obj) {
225                         /* this is anonymous page allocated for copy-write */
226                         lnb[i].page->mapping = NULL;
227                         __free_page(lnb[i].page);
228                         atomic_dec(&osd->od_zerocopy_alloc);
229                 } else {
230                         /* see comment in osd_bufs_get_read() */
231                         ptr = (unsigned long)lnb[i].dentry;
232                         if (ptr & 1UL) {
233                                 ptr &= ~1UL;
234                                 dmu_buf_rele((void *)ptr, osd_zerocopy_tag);
235                                 atomic_dec(&osd->od_zerocopy_pin);
236                         } else if (lnb[i].dentry != NULL) {
237                                 dmu_return_arcbuf((void *)lnb[i].dentry);
238                                 atomic_dec(&osd->od_zerocopy_loan);
239                         }
240                 }
241                 lnb[i].page = NULL;
242                 lnb[i].dentry = NULL;
243         }
244
245         return 0;
246 }
247
248 static inline struct page *kmem_to_page(void *addr)
249 {
250         if (is_vmalloc_addr(addr))
251                 return vmalloc_to_page(addr);
252         else
253                 return virt_to_page(addr);
254 }
255
256 static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
257                                 loff_t off, ssize_t len, struct niobuf_local *lnb)
258 {
259         struct osd_device *osd = osd_obj2dev(obj);
260         dmu_buf_t        **dbp;
261         int                rc, i, numbufs, npages = 0;
262         ENTRY;
263
264         /* grab buffers for read:
265          * OSD API let us to grab buffers first, then initiate IO(s)
266          * so that all required IOs will be done in parallel, but at the
267          * moment DMU doesn't provide us with a method to grab buffers.
268          * If we discover this is a vital for good performance we
269          * can get own replacement for dmu_buf_hold_array_by_bonus().
270          */
271         while (len > 0) {
272                 rc = -dmu_buf_hold_array_by_bonus(obj->oo_db, off, len, TRUE,
273                                                   osd_zerocopy_tag, &numbufs,
274                                                   &dbp);
275                 if (unlikely(rc))
276                         GOTO(err, rc);
277
278                 for (i = 0; i < numbufs; i++) {
279                         int bufoff, tocpy, thispage;
280                         void *dbf = dbp[i];
281
282                         LASSERT(len > 0);
283
284                         atomic_inc(&osd->od_zerocopy_pin);
285
286                         bufoff = off - dbp[i]->db_offset;
287                         tocpy = min_t(int, dbp[i]->db_size - bufoff, len);
288
289                         /* kind of trick to differentiate dbuf vs. arcbuf */
290                         LASSERT(((unsigned long)dbp[i] & 1) == 0);
291                         dbf = (void *) ((unsigned long)dbp[i] | 1);
292
293                         while (tocpy > 0) {
294                                 thispage = PAGE_CACHE_SIZE;
295                                 thispage -= bufoff & (PAGE_CACHE_SIZE - 1);
296                                 thispage = min(tocpy, thispage);
297
298                                 lnb->rc = 0;
299                                 lnb->lnb_file_offset = off;
300                                 lnb->lnb_page_offset = bufoff & ~CFS_PAGE_MASK;
301                                 lnb->len = thispage;
302                                 lnb->page = kmem_to_page(dbp[i]->db_data +
303                                                                 bufoff);
304                                 /* mark just a single slot: we need this
305                                  * reference to dbuf to be release once */
306                                 lnb->dentry = dbf;
307                                 dbf = NULL;
308
309                                 tocpy -= thispage;
310                                 len -= thispage;
311                                 bufoff += thispage;
312                                 off += thispage;
313
314                                 npages++;
315                                 lnb++;
316                         }
317
318                         /* steal dbuf so dmu_buf_rele_array() cant release it */
319                         dbp[i] = NULL;
320                 }
321
322                 dmu_buf_rele_array(dbp, numbufs, osd_zerocopy_tag);
323         }
324
325         RETURN(npages);
326
327 err:
328         LASSERT(rc < 0);
329         osd_bufs_put(env, &obj->oo_dt, lnb - npages, npages);
330         RETURN(rc);
331 }
332
333 static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
334                                 loff_t off, ssize_t len, struct niobuf_local *lnb)
335 {
336         struct osd_device *osd = osd_obj2dev(obj);
337         int                plen, off_in_block, sz_in_block;
338         int                rc, i = 0, npages = 0;
339         arc_buf_t         *abuf;
340         uint32_t           bs;
341         uint64_t           dummy;
342         ENTRY;
343
344         dmu_object_size_from_db(obj->oo_db, &bs, &dummy);
345
346         /*
347          * currently only full blocks are subject to zerocopy approach:
348          * so that we're sure nobody is trying to update the same block
349          */
350         while (len > 0) {
351                 LASSERT(npages < PTLRPC_MAX_BRW_PAGES);
352
353                 off_in_block = off & (bs - 1);
354                 sz_in_block = min_t(int, bs - off_in_block, len);
355
356                 if (sz_in_block == bs) {
357                         /* full block, try to use zerocopy */
358
359                         abuf = dmu_request_arcbuf(obj->oo_db, bs);
360                         if (unlikely(abuf == NULL))
361                                 GOTO(out_err, rc = -ENOMEM);
362
363                         atomic_inc(&osd->od_zerocopy_loan);
364
365                         /* go over pages arcbuf contains, put them as
366                          * local niobufs for ptlrpc's bulks */
367                         while (sz_in_block > 0) {
368                                 plen = min_t(int, sz_in_block, PAGE_CACHE_SIZE);
369
370                                 lnb[i].lnb_file_offset = off;
371                                 lnb[i].lnb_page_offset = 0;
372                                 lnb[i].len = plen;
373                                 lnb[i].rc = 0;
374                                 if (sz_in_block == bs)
375                                         lnb[i].dentry = (void *)abuf;
376                                 else
377                                         lnb[i].dentry = NULL;
378
379                                 /* this one is not supposed to fail */
380                                 lnb[i].page = kmem_to_page(abuf->b_data +
381                                                         off_in_block);
382                                 LASSERT(lnb[i].page);
383
384                                 lprocfs_counter_add(osd->od_stats,
385                                                 LPROC_OSD_ZEROCOPY_IO, 1);
386
387                                 sz_in_block -= plen;
388                                 len -= plen;
389                                 off += plen;
390                                 off_in_block += plen;
391                                 i++;
392                                 npages++;
393                         }
394                 } else {
395                         if (off_in_block == 0 && len < bs &&
396                                         off + len >= obj->oo_attr.la_size)
397                                 lprocfs_counter_add(osd->od_stats,
398                                                 LPROC_OSD_TAIL_IO, 1);
399
400                         /* can't use zerocopy, allocate temp. buffers */
401                         while (sz_in_block > 0) {
402                                 plen = min_t(int, sz_in_block, PAGE_CACHE_SIZE);
403
404                                 lnb[i].lnb_file_offset = off;
405                                 lnb[i].lnb_page_offset = 0;
406                                 lnb[i].len = plen;
407                                 lnb[i].rc = 0;
408                                 lnb[i].dentry = NULL;
409
410                                 lnb[i].page = alloc_page(OSD_GFP_IO);
411                                 if (unlikely(lnb[i].page == NULL))
412                                         GOTO(out_err, rc = -ENOMEM);
413
414                                 LASSERT(lnb[i].page->mapping == NULL);
415                                 lnb[i].page->mapping = (void *)obj;
416
417                                 atomic_inc(&osd->od_zerocopy_alloc);
418                                 lprocfs_counter_add(osd->od_stats,
419                                                 LPROC_OSD_COPY_IO, 1);
420
421                                 sz_in_block -= plen;
422                                 len -= plen;
423                                 off += plen;
424                                 i++;
425                                 npages++;
426                         }
427                 }
428         }
429
430         RETURN(npages);
431
432 out_err:
433         osd_bufs_put(env, &obj->oo_dt, lnb, npages);
434         RETURN(rc);
435 }
436
437 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
438                         loff_t offset, ssize_t len, struct niobuf_local *lnb,
439                         int rw, struct lustre_capa *capa)
440 {
441         struct osd_object *obj  = osd_dt_obj(dt);
442         int                rc;
443
444         LASSERT(dt_object_exists(dt));
445         LASSERT(obj->oo_db);
446
447         if (rw == 0)
448                 rc = osd_bufs_get_read(env, obj, offset, len, lnb);
449         else
450                 rc = osd_bufs_get_write(env, obj, offset, len, lnb);
451
452         return rc;
453 }
454
455 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
456                         struct niobuf_local *lnb, int npages)
457 {
458         struct osd_object *obj = osd_dt_obj(dt);
459
460         LASSERT(dt_object_exists(dt));
461         LASSERT(obj->oo_db);
462
463         return 0;
464 }
465
466 /* Return number of blocks that aren't mapped in the [start, start + size]
467  * region */
468 static int osd_count_not_mapped(struct osd_object *obj, uint64_t start,
469                                 uint32_t size)
470 {
471         dmu_buf_impl_t  *dbi = (dmu_buf_impl_t *)obj->oo_db;
472         dmu_buf_impl_t  *db;
473         dnode_t         *dn;
474         uint32_t         blkshift;
475         uint64_t         end, blkid;
476         int              rc;
477         ENTRY;
478
479         DB_DNODE_ENTER(dbi);
480         dn = DB_DNODE(dbi);
481
482         if (dn->dn_maxblkid == 0) {
483                 if (start + size <= dn->dn_datablksz)
484                         GOTO(out, size = 0);
485                 if (start < dn->dn_datablksz)
486                         start = dn->dn_datablksz;
487                 /* assume largest block size */
488                 blkshift = SPA_MAXBLOCKSHIFT;
489         } else {
490                 /* blocksize can't change */
491                 blkshift = dn->dn_datablkshift;
492         }
493
494         /* compute address of last block */
495         end = (start + size - 1) >> blkshift;
496         /* align start on block boundaries */
497         start >>= blkshift;
498
499         /* size is null, can't be mapped */
500         if (obj->oo_attr.la_size == 0 || dn->dn_maxblkid == 0)
501                 GOTO(out, size = (end - start + 1) << blkshift);
502
503         /* beyond EOF, can't be mapped */
504         if (start > dn->dn_maxblkid)
505                 GOTO(out, size = (end - start + 1) << blkshift);
506
507         size = 0;
508         for (blkid = start; blkid <= end; blkid++) {
509                 if (blkid == dn->dn_maxblkid)
510                         /* this one is mapped for sure */
511                         continue;
512                 if (blkid > dn->dn_maxblkid) {
513                         size += (end - blkid + 1) << blkshift;
514                         GOTO(out, size);
515                 }
516
517                 rc = dbuf_hold_impl(dn, 0, blkid, TRUE, FTAG, &db);
518                 if (rc) {
519                         /* for ENOENT (block not mapped) and any other errors,
520                          * assume the block isn't mapped */
521                         size += 1 << blkshift;
522                         continue;
523                 }
524                 dbuf_rele(db, FTAG);
525         }
526
527         GOTO(out, size);
528 out:
529         DB_DNODE_EXIT(dbi);
530         return size;
531 }
532
533 static int osd_declare_write_commit(const struct lu_env *env,
534                                 struct dt_object *dt,
535                                 struct niobuf_local *lnb, int npages,
536                                 struct thandle *th)
537 {
538         struct osd_object  *obj = osd_dt_obj(dt);
539         struct osd_device  *osd = osd_obj2dev(obj);
540         struct osd_thandle *oh;
541         uint64_t            offset = 0;
542         uint32_t            size = 0;
543         int                 i, rc, flags = 0;
544         bool                ignore_quota = false, synced = false;
545         long long           space = 0;
546         ENTRY;
547
548         LASSERT(dt_object_exists(dt));
549         LASSERT(obj->oo_db);
550
551         LASSERT(lnb);
552         LASSERT(npages > 0);
553
554         oh = container_of0(th, struct osd_thandle, ot_super);
555
556         for (i = 0; i < npages; i++) {
557                 if (lnb[i].rc)
558                         /* ENOSPC, network RPC error, etc.
559                          * We don't want to book space for pages which will be
560                          * skipped in osd_write_commit(). Hence we skip pages
561                          * with lnb_rc != 0 here too */
562                         continue;
563                 /* ignore quota for the whole request if any page is from
564                  * client cache or written by root.
565                  *
566                  * XXX once we drop the 1.8 client support, the checking
567                  * for whether page is from cache can be simplified as:
568                  * !(lnb[i].flags & OBD_BRW_SYNC)
569                  *
570                  * XXX we could handle this on per-lnb basis as done by
571                  * grant. */
572                 if ((lnb[i].flags & OBD_BRW_NOQUOTA) ||
573                     (lnb[i].flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
574                     OBD_BRW_FROM_GRANT)
575                         ignore_quota = true;
576                 if (size == 0) {
577                         /* first valid lnb */
578                         offset = lnb[i].lnb_file_offset;
579                         size = lnb[i].len;
580                         continue;
581                 }
582                 if (offset + size == lnb[i].lnb_file_offset) {
583                         /* this lnb is contiguous to the previous one */
584                         size += lnb[i].len;
585                         continue;
586                 }
587
588                 dmu_tx_hold_write(oh->ot_tx, obj->oo_db->db_object,
589                                   offset, size);
590                 /* estimating space that will be consumed by a write is rather
591                  * complicated with ZFS. As a consequence, we don't account for
592                  * indirect blocks and quota overrun will be adjusted once the
593                  * operation is committed, if required. */
594                 space += osd_count_not_mapped(obj, offset, size);
595
596                 offset = lnb[i].lnb_file_offset;
597                 size = lnb[i].len;
598         }
599
600         if (size) {
601                 dmu_tx_hold_write(oh->ot_tx, obj->oo_db->db_object,
602                                   offset, size);
603                 space += osd_count_not_mapped(obj, offset, size);
604         }
605
606         dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
607
608         oh->ot_write_commit = 1; /* used in osd_trans_start() for fail_loc */
609
610         /* backend zfs filesystem might be configured to store multiple data
611          * copies */
612         space  *= osd->od_objset.os->os_copies;
613         space   = toqb(space);
614         CDEBUG(D_QUOTA, "writting %d pages, reserving "LPD64"K of quota "
615                "space\n", npages, space);
616
617 retry:
618         /* acquire quota space if needed */
619         rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
620                                obj->oo_attr.la_gid, space, oh, true, &flags,
621                                ignore_quota);
622
623         if (!synced && rc == -EDQUOT && (flags & QUOTA_FL_SYNC) != 0) {
624                 dt_sync(env, th->th_dev);
625                 synced = true;
626                 CDEBUG(D_QUOTA, "retry after sync\n");
627                 flags = 0;
628                 goto retry;
629         }
630
631         /* we need only to store the overquota flags in the first lnb for
632          * now, once we support multiple objects BRW, this code needs be
633          * revised. */
634         if (flags & QUOTA_FL_OVER_USRQUOTA)
635                 lnb[0].flags |= OBD_BRW_OVER_USRQUOTA;
636         if (flags & QUOTA_FL_OVER_GRPQUOTA)
637                 lnb[0].flags |= OBD_BRW_OVER_GRPQUOTA;
638
639         RETURN(rc);
640 }
641
642 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
643                         struct niobuf_local *lnb, int npages,
644                         struct thandle *th)
645 {
646         struct osd_object  *obj  = osd_dt_obj(dt);
647         struct osd_device  *osd = osd_obj2dev(obj);
648         udmu_objset_t      *uos = &osd->od_objset;
649         struct osd_thandle *oh;
650         uint64_t            new_size = 0;
651         int                 i, rc = 0;
652         ENTRY;
653
654         LASSERT(dt_object_exists(dt));
655         LASSERT(obj->oo_db);
656
657         LASSERT(th != NULL);
658         oh = container_of0(th, struct osd_thandle, ot_super);
659
660         for (i = 0; i < npages; i++) {
661                 CDEBUG(D_INODE, "write %u bytes at %u\n",
662                         (unsigned) lnb[i].len,
663                         (unsigned) lnb[i].lnb_file_offset);
664
665                 if (lnb[i].rc) {
666                         /* ENOSPC, network RPC error, etc.
667                          * Unlike ldiskfs, zfs allocates new blocks on rewrite,
668                          * so we skip this page if lnb_rc is set to -ENOSPC */
669                         CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
670                                 PFID(lu_object_fid(&dt->do_lu)), i,
671                                 lnb[i].rc);
672                         continue;
673                 }
674
675                 if (lnb[i].page->mapping == (void *)obj) {
676                         dmu_write(osd->od_objset.os, obj->oo_db->db_object,
677                                 lnb[i].lnb_file_offset, lnb[i].len,
678                                 kmap(lnb[i].page), oh->ot_tx);
679                         kunmap(lnb[i].page);
680                 } else if (lnb[i].dentry) {
681                         LASSERT(((unsigned long)lnb[i].dentry & 1) == 0);
682                         /* buffer loaned for zerocopy, try to use it.
683                          * notice that dmu_assign_arcbuf() is smart
684                          * enough to recognize changed blocksize
685                          * in this case it fallbacks to dmu_write() */
686                         dmu_assign_arcbuf(obj->oo_db, lnb[i].lnb_file_offset,
687                                         (void *)lnb[i].dentry, oh->ot_tx);
688                         /* drop the reference, otherwise osd_put_bufs()
689                          * will be releasing it - bad! */
690                         lnb[i].dentry = NULL;
691                         atomic_dec(&osd->od_zerocopy_loan);
692                 }
693
694                 if (new_size < lnb[i].lnb_file_offset + lnb[i].len)
695                         new_size = lnb[i].lnb_file_offset + lnb[i].len;
696         }
697
698         if (unlikely(new_size == 0)) {
699                 /* no pages to write, no transno is needed */
700                 th->th_local = 1;
701                 /* it is important to return 0 even when all lnb_rc == -ENOSPC
702                  * since ofd_commitrw_write() retries several times on ENOSPC */
703                 RETURN(0);
704         }
705
706         write_lock(&obj->oo_attr_lock);
707         if (obj->oo_attr.la_size < new_size) {
708                 obj->oo_attr.la_size = new_size;
709                 write_unlock(&obj->oo_attr_lock);
710                 /* osd_object_sa_update() will be copying directly from
711                  * oo_attr into dbuf. any update within a single txg will copy
712                  * the most actual */
713                 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(uos),
714                                         &obj->oo_attr.la_size, 8, oh);
715         } else {
716                 write_unlock(&obj->oo_attr_lock);
717         }
718
719         RETURN(rc);
720 }
721
722 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
723                         struct niobuf_local *lnb, int npages)
724 {
725         struct osd_object *obj  = osd_dt_obj(dt);
726         struct lu_buf      buf;
727         loff_t             offset;
728         int                i;
729
730         LASSERT(dt_object_exists(dt));
731         LASSERT(obj->oo_db);
732
733         for (i = 0; i < npages; i++) {
734                 buf.lb_buf = kmap(lnb[i].page);
735                 buf.lb_len = lnb[i].len;
736                 offset = lnb[i].lnb_file_offset;
737
738                 CDEBUG(D_OTHER, "read %u bytes at %u\n",
739                         (unsigned) lnb[i].len,
740                         (unsigned) lnb[i].lnb_file_offset);
741                 lnb[i].rc = osd_read(env, dt, &buf, &offset, NULL);
742                 kunmap(lnb[i].page);
743
744                 if (lnb[i].rc < buf.lb_len) {
745                         /* all subsequent rc should be 0 */
746                         while (++i < npages)
747                                 lnb[i].rc = 0;
748                         break;
749                 }
750         }
751
752         return 0;
753 }
754
755 /*
756  * Punch/truncate an object
757  *
758  *      IN:     db  - dmu_buf of the object to free data in.
759  *              off - start of section to free.
760  *              len - length of section to free (DMU_OBJECT_END => to EOF).
761  *
762  *      RETURN: 0 if success
763  *              error code if failure
764  *
765  * The transaction passed to this routine must have
766  * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
767  * called and then assigned to a transaction group.
768  */
769 static int __osd_object_punch(objset_t *os, dmu_buf_t *db, dmu_tx_t *tx,
770                                 uint64_t size, uint64_t off, uint64_t len)
771 {
772         int rc = 0;
773
774         /* Assert that the transaction has been assigned to a
775            transaction group. */
776         LASSERT(tx->tx_txg != 0);
777         /*
778          * Nothing to do if file already at desired length.
779          */
780         if (len == DMU_OBJECT_END && size == off)
781                 return 0;
782
783         if (off < size)
784                 rc = -dmu_free_range(os, db->db_object, off, len, tx);
785
786         return rc;
787 }
788
789 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
790                         __u64 start, __u64 end, struct thandle *th,
791                         struct lustre_capa *capa)
792 {
793         struct osd_object  *obj = osd_dt_obj(dt);
794         struct osd_device  *osd = osd_obj2dev(obj);
795         udmu_objset_t      *uos = &osd->od_objset;
796         struct osd_thandle *oh;
797         __u64               len;
798         int                 rc = 0;
799         ENTRY;
800
801         LASSERT(dt_object_exists(dt));
802         LASSERT(osd_invariant(obj));
803
804         LASSERT(th != NULL);
805         oh = container_of0(th, struct osd_thandle, ot_super);
806
807         write_lock(&obj->oo_attr_lock);
808         /* truncate */
809         if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
810                 len = DMU_OBJECT_END;
811         else
812                 len = end - start;
813         write_unlock(&obj->oo_attr_lock);
814
815         rc = __osd_object_punch(osd->od_objset.os, obj->oo_db, oh->ot_tx,
816                                 obj->oo_attr.la_size, start, len);
817         /* set new size */
818         if (len == DMU_OBJECT_END) {
819                 write_lock(&obj->oo_attr_lock);
820                 obj->oo_attr.la_size = start;
821                 write_unlock(&obj->oo_attr_lock);
822                 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(uos),
823                                         &obj->oo_attr.la_size, 8, oh);
824         }
825         RETURN(rc);
826 }
827
828 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
829                         __u64 start, __u64 end, struct thandle *handle)
830 {
831         struct osd_object  *obj = osd_dt_obj(dt);
832         struct osd_device  *osd = osd_obj2dev(obj);
833         struct osd_thandle *oh;
834         __u64               len;
835         ENTRY;
836
837         oh = container_of0(handle, struct osd_thandle, ot_super);
838
839         read_lock(&obj->oo_attr_lock);
840         if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
841                 len = DMU_OBJECT_END;
842         else
843                 len = end - start;
844
845         /* declare we'll free some blocks ... */
846         if (start < obj->oo_attr.la_size) {
847                 read_unlock(&obj->oo_attr_lock);
848                 dmu_tx_hold_free(oh->ot_tx, obj->oo_db->db_object, start, len);
849         } else {
850                 read_unlock(&obj->oo_attr_lock);
851         }
852
853         /* ... and we'll modify size attribute */
854         dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
855
856         RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
857                                  obj->oo_attr.la_gid, 0, oh, true, NULL,
858                                  false));
859 }
860
861
862 struct dt_body_operations osd_body_ops = {
863         .dbo_read                       = osd_read,
864         .dbo_declare_write              = osd_declare_write,
865         .dbo_write                      = osd_write,
866         .dbo_bufs_get                   = osd_bufs_get,
867         .dbo_bufs_put                   = osd_bufs_put,
868         .dbo_write_prep                 = osd_write_prep,
869         .dbo_declare_write_commit       = osd_declare_write_commit,
870         .dbo_write_commit               = osd_write_commit,
871         .dbo_read_prep                  = osd_read_prep,
872         .dbo_declare_punch              = osd_declare_punch,
873         .dbo_punch                      = osd_punch,
874 };
875