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