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