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