Whamcloud - gitweb
LU-8928 osd: convert osd-zfs to reference dnode, not db
[fs/lustre-release.git] / lustre / osd-zfs / osd_io.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/osd-zfs/osd_io.c
33  *
34  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
35  * Author: Mike Pershin <tappro@whamcloud.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_OSD
39
40 #include <lustre_ver.h>
41 #include <libcfs/libcfs.h>
42 #include <obd_support.h>
43 #include <lustre_net.h>
44 #include <obd.h>
45 #include <obd_class.h>
46 #include <lustre_disk.h>
47 #include <lustre_fid.h>
48 #include <lustre/lustre_idl.h>  /* LLOG_MIN_CHUNK_SIZE definition */
49
50 #include "osd_internal.h"
51
52 #include <sys/dnode.h>
53 #include <sys/dbuf.h>
54 #include <sys/spa.h>
55 #include <sys/stat.h>
56 #include <sys/zap.h>
57 #include <sys/spa_impl.h>
58 #include <sys/zfs_znode.h>
59 #include <sys/dmu_tx.h>
60 #include <sys/dmu_objset.h>
61 #include <sys/dsl_prop.h>
62 #include <sys/sa_impl.h>
63 #include <sys/txg.h>
64
65 static char *osd_0copy_tag = "zerocopy";
66
67
68 static void record_start_io(struct osd_device *osd, int rw, int discont_pages)
69 {
70         struct obd_histogram *h = osd->od_brw_stats.hist;
71
72         if (rw == READ) {
73                 atomic_inc(&osd->od_r_in_flight);
74                 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
75                                  atomic_read(&osd->od_r_in_flight));
76                 lprocfs_oh_tally(&h[BRW_R_DISCONT_PAGES], discont_pages);
77
78         } else {
79                 atomic_inc(&osd->od_w_in_flight);
80                 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
81                                  atomic_read(&osd->od_w_in_flight));
82                 lprocfs_oh_tally(&h[BRW_W_DISCONT_PAGES], discont_pages);
83
84         }
85 }
86
87 static void record_end_io(struct osd_device *osd, int rw,
88                           unsigned long elapsed, int disksize, int npages)
89 {
90         struct obd_histogram *h = osd->od_brw_stats.hist;
91
92         if (rw == READ) {
93                 atomic_dec(&osd->od_r_in_flight);
94                 lprocfs_oh_tally_log2(&h[BRW_R_PAGES], npages);
95                 if (disksize > 0)
96                         lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], disksize);
97                 if (elapsed)
98                         lprocfs_oh_tally_log2(&h[BRW_R_IO_TIME], elapsed);
99
100         } else {
101                 atomic_dec(&osd->od_w_in_flight);
102                 lprocfs_oh_tally_log2(&h[BRW_W_PAGES], npages);
103                 if (disksize > 0)
104                         lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], disksize);
105                 if (elapsed)
106                         lprocfs_oh_tally_log2(&h[BRW_W_IO_TIME], elapsed);
107         }
108 }
109
110 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
111                         struct lu_buf *buf, loff_t *pos)
112 {
113         struct osd_object *obj  = osd_dt_obj(dt);
114         struct osd_device *osd = osd_obj2dev(obj);
115         uint64_t           old_size;
116         int                size = buf->lb_len;
117         int                rc;
118         unsigned long      start;
119
120         LASSERT(dt_object_exists(dt));
121         LASSERT(obj->oo_dn);
122
123         start = cfs_time_current();
124
125         read_lock(&obj->oo_attr_lock);
126         old_size = obj->oo_attr.la_size;
127         read_unlock(&obj->oo_attr_lock);
128
129         if (*pos + size > old_size) {
130                 if (old_size < *pos)
131                         return 0;
132                 else
133                         size = old_size - *pos;
134         }
135
136         record_start_io(osd, READ, 0);
137
138         rc = -dmu_read(osd->od_os, obj->oo_dn->dn_object, *pos, size,
139                         buf->lb_buf, DMU_READ_PREFETCH);
140
141         record_end_io(osd, READ, cfs_time_current() - start, size,
142                       size >> PAGE_SHIFT);
143         if (rc == 0) {
144                 rc = size;
145                 *pos += size;
146         }
147         return rc;
148 }
149
150 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
151                                 const struct lu_buf *buf, loff_t pos,
152                                 struct thandle *th)
153 {
154         struct osd_object  *obj  = osd_dt_obj(dt);
155         struct osd_device  *osd = osd_obj2dev(obj);
156         struct osd_thandle *oh;
157         uint64_t            oid;
158         ENTRY;
159
160         oh = container_of0(th, struct osd_thandle, ot_super);
161
162         /* in some cases declare can race with creation (e.g. llog)
163          * and we need to wait till object is initialized. notice
164          * LOHA_EXISTs is supposed to be the last step in the
165          * initialization */
166
167         /* size change (in dnode) will be declared by dmu_tx_hold_write() */
168         if (dt_object_exists(dt))
169                 oid = obj->oo_dn->dn_object;
170         else
171                 oid = DMU_NEW_OBJECT;
172
173         /* XXX: we still miss for append declaration support in ZFS
174          *      -1 means append which is used by llog mostly, llog
175          *      can grow upto LLOG_MIN_CHUNK_SIZE*8 records */
176         if (pos == -1)
177                 pos = max_t(loff_t, 256 * 8 * LLOG_MIN_CHUNK_SIZE,
178                             obj->oo_attr.la_size + (2 << 20));
179         dmu_tx_hold_write(oh->ot_tx, oid, pos, buf->lb_len);
180
181         /* dt_declare_write() is usually called for system objects, such
182          * as llog or last_rcvd files. We needn't enforce quota on those
183          * objects, so always set the lqi_space as 0. */
184         RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
185                                  obj->oo_attr.la_gid, 0, oh, true, NULL,
186                                  false));
187 }
188
189 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
190                         const struct lu_buf *buf, loff_t *pos,
191                         struct thandle *th, int ignore_quota)
192 {
193         struct osd_object  *obj  = osd_dt_obj(dt);
194         struct osd_device  *osd = osd_obj2dev(obj);
195         struct osd_thandle *oh;
196         uint64_t            offset = *pos;
197         int                 rc;
198
199         ENTRY;
200
201         LASSERT(dt_object_exists(dt));
202         LASSERT(obj->oo_dn);
203
204         LASSERT(th != NULL);
205         oh = container_of0(th, struct osd_thandle, ot_super);
206
207         dmu_write(osd->od_os, obj->oo_dn->dn_object, offset,
208                 (uint64_t)buf->lb_len, buf->lb_buf, oh->ot_tx);
209         write_lock(&obj->oo_attr_lock);
210         if (obj->oo_attr.la_size < offset + buf->lb_len) {
211                 obj->oo_attr.la_size = offset + buf->lb_len;
212                 write_unlock(&obj->oo_attr_lock);
213                 /* osd_object_sa_update() will be copying directly from oo_attr
214                  * into dbuf.  any update within a single txg will copy the
215                  * most actual */
216                 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
217                                         &obj->oo_attr.la_size, 8, oh);
218                 if (unlikely(rc))
219                         GOTO(out, rc);
220         } else {
221                 write_unlock(&obj->oo_attr_lock);
222         }
223
224         *pos += buf->lb_len;
225         rc = buf->lb_len;
226
227 out:
228         RETURN(rc);
229 }
230
231 /*
232  * XXX: for the moment I don't want to use lnb_flags for osd-internal
233  *      purposes as it's not very well defined ...
234  *      instead I use the lowest bit of the address so that:
235  *        arc buffer:  .lnb_data = abuf          (arc we loan for write)
236  *        dbuf buffer: .lnb_data = dbuf | 1      (dbuf we get for read)
237  *        copy buffer: .lnb_page->mapping = obj (page we allocate for write)
238  *
239  *      bzzz, to blame
240  */
241 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
242                         struct niobuf_local *lnb, int npages)
243 {
244         struct osd_object *obj  = osd_dt_obj(dt);
245         struct osd_device *osd = osd_obj2dev(obj);
246         unsigned long      ptr;
247         int                i;
248
249         LASSERT(dt_object_exists(dt));
250         LASSERT(obj->oo_dn);
251
252         for (i = 0; i < npages; i++) {
253                 if (lnb[i].lnb_page == NULL)
254                         continue;
255                 if (lnb[i].lnb_page->mapping == (void *)obj) {
256                         /* this is anonymous page allocated for copy-write */
257                         lnb[i].lnb_page->mapping = NULL;
258                         __free_page(lnb[i].lnb_page);
259                         atomic_dec(&osd->od_zerocopy_alloc);
260                 } else {
261                         /* see comment in osd_bufs_get_read() */
262                         ptr = (unsigned long)lnb[i].lnb_data;
263                         if (ptr & 1UL) {
264                                 ptr &= ~1UL;
265                                 dmu_buf_rele((void *)ptr, osd_0copy_tag);
266                                 atomic_dec(&osd->od_zerocopy_pin);
267                         } else if (lnb[i].lnb_data != NULL) {
268                                 dmu_return_arcbuf(lnb[i].lnb_data);
269                                 atomic_dec(&osd->od_zerocopy_loan);
270                         }
271                 }
272                 lnb[i].lnb_page = NULL;
273                 lnb[i].lnb_data = NULL;
274         }
275
276         return 0;
277 }
278
279 static inline struct page *kmem_to_page(void *addr)
280 {
281         if (is_vmalloc_addr(addr))
282                 return vmalloc_to_page(addr);
283         else
284                 return virt_to_page(addr);
285 }
286
287 /**
288  * Prepare buffers for read.
289  *
290  * The function maps the range described by \a off and \a len to \a lnb array.
291  * dmu_buf_hold_array_by_bonus() finds/creates appropriate ARC buffers, then
292  * we fill \a lnb array with the pages storing ARC buffers. Notice the current
293  * implementationt passes TRUE to dmu_buf_hold_array_by_bonus() to fill ARC
294  * buffers with actual data, I/O is done in the conext of osd_bufs_get_read().
295  * A better implementation would just return the buffers (potentially unfilled)
296  * and subsequent osd_read_prep() would do I/O for many ranges concurrently.
297  *
298  * \param[in] env       environment
299  * \param[in] obj       object
300  * \param[in] off       offset in bytes
301  * \param[in] len       the number of bytes to access
302  * \param[out] lnb      array of local niobufs pointing to the buffers with data
303  *
304  * \retval              0 for success
305  * \retval              negative error number of failure
306  */
307 static int osd_bufs_get_read(const struct lu_env *env, struct osd_object *obj,
308                                 loff_t off, ssize_t len, struct niobuf_local *lnb)
309 {
310         struct osd_device *osd = osd_obj2dev(obj);
311         unsigned long      start = cfs_time_current();
312         int                rc, i, numbufs, npages = 0;
313         dmu_buf_t        **dbp;
314         ENTRY;
315
316         record_start_io(osd, READ, 0);
317
318         /* grab buffers for read:
319          * OSD API let us to grab buffers first, then initiate IO(s)
320          * so that all required IOs will be done in parallel, but at the
321          * moment DMU doesn't provide us with a method to grab buffers.
322          * If we discover this is a vital for good performance we
323          * can get own replacement for dmu_buf_hold_array_by_bonus().
324          */
325         while (len > 0) {
326                 rc = -dmu_buf_hold_array_by_bonus(&obj->oo_dn->dn_bonus->db,
327                                                   off, len, TRUE, osd_0copy_tag,
328                                                   &numbufs, &dbp);
329                 if (unlikely(rc))
330                         GOTO(err, rc);
331
332                 for (i = 0; i < numbufs; i++) {
333                         int bufoff, tocpy, thispage;
334                         void *dbf = dbp[i];
335
336                         LASSERT(len > 0);
337
338                         atomic_inc(&osd->od_zerocopy_pin);
339
340                         bufoff = off - dbp[i]->db_offset;
341                         tocpy = min_t(int, dbp[i]->db_size - bufoff, len);
342
343                         /* kind of trick to differentiate dbuf vs. arcbuf */
344                         LASSERT(((unsigned long)dbp[i] & 1) == 0);
345                         dbf = (void *) ((unsigned long)dbp[i] | 1);
346
347                         while (tocpy > 0) {
348                                 thispage = PAGE_SIZE;
349                                 thispage -= bufoff & (PAGE_SIZE - 1);
350                                 thispage = min(tocpy, thispage);
351
352                                 lnb->lnb_rc = 0;
353                                 lnb->lnb_file_offset = off;
354                                 lnb->lnb_page_offset = bufoff & ~PAGE_MASK;
355                                 lnb->lnb_len = thispage;
356                                 lnb->lnb_page = kmem_to_page(dbp[i]->db_data +
357                                                              bufoff);
358                                 /* mark just a single slot: we need this
359                                  * reference to dbuf to be released once */
360                                 lnb->lnb_data = dbf;
361                                 dbf = NULL;
362
363                                 tocpy -= thispage;
364                                 len -= thispage;
365                                 bufoff += thispage;
366                                 off += thispage;
367
368                                 npages++;
369                                 lnb++;
370                         }
371
372                         /* steal dbuf so dmu_buf_rele_array() can't release
373                          * it */
374                         dbp[i] = NULL;
375                 }
376
377                 dmu_buf_rele_array(dbp, numbufs, osd_0copy_tag);
378         }
379
380         record_end_io(osd, READ, cfs_time_current() - start,
381                       npages * PAGE_SIZE, npages);
382
383         RETURN(npages);
384
385 err:
386         LASSERT(rc < 0);
387         osd_bufs_put(env, &obj->oo_dt, lnb - npages, npages);
388         RETURN(rc);
389 }
390
391 static int osd_bufs_get_write(const struct lu_env *env, struct osd_object *obj,
392                                 loff_t off, ssize_t len, struct niobuf_local *lnb)
393 {
394         struct osd_device *osd = osd_obj2dev(obj);
395         int                plen, off_in_block, sz_in_block;
396         int                rc, i = 0, npages = 0;
397         dnode_t *dn = obj->oo_dn;
398         arc_buf_t *abuf;
399         uint32_t bs = dn->dn_datablksz;
400         ENTRY;
401
402         /*
403          * currently only full blocks are subject to zerocopy approach:
404          * so that we're sure nobody is trying to update the same block
405          */
406         while (len > 0) {
407                 LASSERT(npages < PTLRPC_MAX_BRW_PAGES);
408
409                 off_in_block = off & (bs - 1);
410                 sz_in_block = min_t(int, bs - off_in_block, len);
411
412                 if (sz_in_block == bs) {
413                         /* full block, try to use zerocopy */
414
415                         abuf = dmu_request_arcbuf(&dn->dn_bonus->db, bs);
416                         if (unlikely(abuf == NULL))
417                                 GOTO(out_err, rc = -ENOMEM);
418
419                         atomic_inc(&osd->od_zerocopy_loan);
420
421                         /* go over pages arcbuf contains, put them as
422                          * local niobufs for ptlrpc's bulks */
423                         while (sz_in_block > 0) {
424                                 plen = min_t(int, sz_in_block, PAGE_SIZE);
425
426                                 lnb[i].lnb_file_offset = off;
427                                 lnb[i].lnb_page_offset = 0;
428                                 lnb[i].lnb_len = plen;
429                                 lnb[i].lnb_rc = 0;
430                                 if (sz_in_block == bs)
431                                         lnb[i].lnb_data = abuf;
432                                 else
433                                         lnb[i].lnb_data = NULL;
434
435                                 /* this one is not supposed to fail */
436                                 lnb[i].lnb_page = kmem_to_page(abuf->b_data +
437                                                         off_in_block);
438                                 LASSERT(lnb[i].lnb_page);
439
440                                 lprocfs_counter_add(osd->od_stats,
441                                                 LPROC_OSD_ZEROCOPY_IO, 1);
442
443                                 sz_in_block -= plen;
444                                 len -= plen;
445                                 off += plen;
446                                 off_in_block += plen;
447                                 i++;
448                                 npages++;
449                         }
450                 } else {
451                         if (off_in_block == 0 && len < bs &&
452                                         off + len >= obj->oo_attr.la_size)
453                                 lprocfs_counter_add(osd->od_stats,
454                                                 LPROC_OSD_TAIL_IO, 1);
455
456                         /* can't use zerocopy, allocate temp. buffers */
457                         while (sz_in_block > 0) {
458                                 plen = min_t(int, sz_in_block, PAGE_SIZE);
459
460                                 lnb[i].lnb_file_offset = off;
461                                 lnb[i].lnb_page_offset = 0;
462                                 lnb[i].lnb_len = plen;
463                                 lnb[i].lnb_rc = 0;
464                                 lnb[i].lnb_data = NULL;
465
466                                 lnb[i].lnb_page = alloc_page(OSD_GFP_IO);
467                                 if (unlikely(lnb[i].lnb_page == NULL))
468                                         GOTO(out_err, rc = -ENOMEM);
469
470                                 LASSERT(lnb[i].lnb_page->mapping == NULL);
471                                 lnb[i].lnb_page->mapping = (void *)obj;
472
473                                 atomic_inc(&osd->od_zerocopy_alloc);
474                                 lprocfs_counter_add(osd->od_stats,
475                                                 LPROC_OSD_COPY_IO, 1);
476
477                                 sz_in_block -= plen;
478                                 len -= plen;
479                                 off += plen;
480                                 i++;
481                                 npages++;
482                         }
483                 }
484         }
485
486         RETURN(npages);
487
488 out_err:
489         osd_bufs_put(env, &obj->oo_dt, lnb, npages);
490         RETURN(rc);
491 }
492
493 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
494                         loff_t offset, ssize_t len, struct niobuf_local *lnb,
495                         int rw)
496 {
497         struct osd_object *obj  = osd_dt_obj(dt);
498         int                rc;
499
500         LASSERT(dt_object_exists(dt));
501         LASSERT(obj->oo_dn);
502
503         if (rw == 0)
504                 rc = osd_bufs_get_read(env, obj, offset, len, lnb);
505         else
506                 rc = osd_bufs_get_write(env, obj, offset, len, lnb);
507
508         return rc;
509 }
510
511 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
512                         struct niobuf_local *lnb, int npages)
513 {
514         struct osd_object *obj = osd_dt_obj(dt);
515
516         LASSERT(dt_object_exists(dt));
517         LASSERT(obj->oo_dn);
518
519         return 0;
520 }
521
522 static inline uint64_t osd_roundup2blocksz(uint64_t size,
523                                            uint64_t offset,
524                                            uint32_t blksz)
525 {
526         LASSERT(blksz > 0);
527
528         size += offset % blksz;
529
530         if (likely(IS_PO2(blksz)))
531                 return PO2_ROUNDUP_TYPED(size, blksz, uint64_t);
532
533         size += blksz - 1;
534         do_div(size, blksz);
535         return size * blksz;
536 }
537
538 static int osd_declare_write_commit(const struct lu_env *env,
539                                     struct dt_object *dt,
540                                     struct niobuf_local *lnb, int npages,
541                                     struct thandle *th)
542 {
543         struct osd_object  *obj = osd_dt_obj(dt);
544         struct osd_device  *osd = osd_obj2dev(obj);
545         struct osd_thandle *oh;
546         uint64_t            offset = 0;
547         uint32_t            size = 0;
548         uint32_t blksz = obj->oo_dn->dn_datablksz;
549         int                 i, rc, flags = 0;
550         bool                ignore_quota = false, synced = false;
551         long long           space = 0;
552         struct page        *last_page = NULL;
553         unsigned long       discont_pages = 0;
554         ENTRY;
555
556         LASSERT(dt_object_exists(dt));
557         LASSERT(obj->oo_dn);
558
559         LASSERT(lnb);
560         LASSERT(npages > 0);
561
562         oh = container_of0(th, struct osd_thandle, ot_super);
563
564         for (i = 0; i < npages; i++) {
565                 if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
566                         ++discont_pages;
567                 last_page = lnb[i].lnb_page;
568                 if (lnb[i].lnb_rc)
569                         /* ENOSPC, network RPC error, etc.
570                          * We don't want to book space for pages which will be
571                          * skipped in osd_write_commit(). Hence we skip pages
572                          * with lnb_rc != 0 here too */
573                         continue;
574                 /* ignore quota for the whole request if any page is from
575                  * client cache or written by root.
576                  *
577                  * XXX once we drop the 1.8 client support, the checking
578                  * for whether page is from cache can be simplified as:
579                  * !(lnb[i].flags & OBD_BRW_SYNC)
580                  *
581                  * XXX we could handle this on per-lnb basis as done by
582                  * grant. */
583                 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
584                     (lnb[i].lnb_flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
585                     OBD_BRW_FROM_GRANT)
586                         ignore_quota = true;
587                 if (size == 0) {
588                         /* first valid lnb */
589                         offset = lnb[i].lnb_file_offset;
590                         size = lnb[i].lnb_len;
591                         continue;
592                 }
593                 if (offset + size == lnb[i].lnb_file_offset) {
594                         /* this lnb is contiguous to the previous one */
595                         size += lnb[i].lnb_len;
596                         continue;
597                 }
598
599                 dmu_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object,
600                                   offset, size);
601                 /* Estimating space to be consumed by a write is rather
602                  * complicated with ZFS. As a consequence, we don't account for
603                  * indirect blocks and just use as a rough estimate the worse
604                  * case where the old space is being held by a snapshot. Quota
605                  * overrun will be adjusted once the operation is committed, if
606                  * required. */
607                 space += osd_roundup2blocksz(size, offset, blksz);
608
609                 offset = lnb[i].lnb_file_offset;
610                 size = lnb[i].lnb_len;
611         }
612
613         if (size) {
614                 dmu_tx_hold_write(oh->ot_tx, obj->oo_dn->dn_object,
615                                   offset, size);
616                 space += osd_roundup2blocksz(size, offset, blksz);
617         }
618
619         dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
620
621         oh->ot_write_commit = 1; /* used in osd_trans_start() for fail_loc */
622
623         /* backend zfs filesystem might be configured to store multiple data
624          * copies */
625         space  *= osd->od_os->os_copies;
626         space   = toqb(space);
627         CDEBUG(D_QUOTA, "writing %d pages, reserving %lldK of quota space\n",
628                npages, space);
629
630         record_start_io(osd, WRITE, discont_pages);
631 retry:
632         /* acquire quota space if needed */
633         rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
634                                obj->oo_attr.la_gid, space, oh, true, &flags,
635                                ignore_quota);
636
637         if (!synced && rc == -EDQUOT && (flags & QUOTA_FL_SYNC) != 0) {
638                 dt_sync(env, th->th_dev);
639                 synced = true;
640                 CDEBUG(D_QUOTA, "retry after sync\n");
641                 flags = 0;
642                 goto retry;
643         }
644
645         /* we need only to store the overquota flags in the first lnb for
646          * now, once we support multiple objects BRW, this code needs be
647          * revised. */
648         if (flags & QUOTA_FL_OVER_USRQUOTA)
649                 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
650         if (flags & QUOTA_FL_OVER_GRPQUOTA)
651                 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
652
653         RETURN(rc);
654 }
655
656 /**
657  * Policy to grow ZFS block size by write pattern.
658  * For sequential write, it grows block size gradually until it reaches the
659  * maximum blocksize the dataset can support. Otherwise, it will pick a
660  * a block size by the writing region of this I/O.
661  */
662 static int osd_grow_blocksize(struct osd_object *obj, struct osd_thandle *oh,
663                               uint64_t start, uint64_t end)
664 {
665         struct osd_device       *osd = osd_obj2dev(obj);
666         dnode_t *dn = obj->oo_dn;
667         uint32_t                 blksz;
668         int                      rc = 0;
669
670         ENTRY;
671
672         if (dn->dn_maxblkid > 0) /* can't change block size */
673                 GOTO(out, rc);
674
675         if (dn->dn_datablksz >= osd->od_max_blksz)
676                 GOTO(out, rc);
677
678         down_write(&obj->oo_guard);
679
680         blksz = dn->dn_datablksz;
681         if (blksz >= osd->od_max_blksz) /* check again after grabbing lock */
682                 GOTO(out_unlock, rc);
683
684         /* now ZFS can support up to 16MB block size, and if the write
685          * is sequential, it just increases the block size gradually */
686         if (start <= blksz) { /* sequential */
687                 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz, end);
688         } else { /* sparse, pick a block size by write region */
689                 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz,
690                                         end - start);
691         }
692
693         if (!is_power_of_2(blksz))
694                 blksz = size_roundup_power2(blksz);
695
696         if (blksz > dn->dn_datablksz) {
697                 rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
698                                                blksz, 0, oh->ot_tx);
699                 LASSERT(ergo(rc == 0, dn->dn_datablksz >= blksz));
700                 if (rc < 0)
701                         CDEBUG(D_INODE, "object "DFID": change block size"
702                                "%u -> %u error rc = %d\n",
703                                PFID(lu_object_fid(&obj->oo_dt.do_lu)),
704                                dn->dn_datablksz, blksz, rc);
705         }
706         EXIT;
707 out_unlock:
708         up_write(&obj->oo_guard);
709 out:
710         return rc;
711 }
712
713 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
714                         struct niobuf_local *lnb, int npages,
715                         struct thandle *th)
716 {
717         struct osd_object  *obj  = osd_dt_obj(dt);
718         struct osd_device  *osd = osd_obj2dev(obj);
719         struct osd_thandle *oh;
720         uint64_t            new_size = 0;
721         int                 i, rc = 0;
722         unsigned long      iosize = 0;
723         ENTRY;
724
725         LASSERT(dt_object_exists(dt));
726         LASSERT(obj->oo_dn);
727
728         LASSERT(th != NULL);
729         oh = container_of0(th, struct osd_thandle, ot_super);
730
731         /* adjust block size. Assume the buffers are sorted. */
732         (void)osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
733                                  lnb[npages - 1].lnb_file_offset +
734                                  lnb[npages - 1].lnb_len);
735
736         /* LU-8791: take oo_guard to avoid the deadlock that changing block
737          * size and assigning arcbuf take place at the same time.
738          *
739          * Thread 1:
740          * osd_write_commit()
741          *  -> osd_grow_blocksize() with osd_object::oo_guard held
742          *   -> dmu_object_set_blocksize()
743          *    -> dnode_set_blksz(), with dnode_t::dn_struct_rwlock
744          *       write lock held
745          *     -> dbuf_new_size()
746          *      -> dmu_buf_will_dirty()
747          *       -> dbuf_read()
748          *        -> wait for the dbuf state to change
749          * Thread 2:
750          * osd_write_commit()
751          *  -> dmu_assign_arcbuf()
752          *   -> dbuf_assign_arcbuf(), set dbuf state to DB_FILL
753          *    -> dbuf_dirty()
754          *     -> try to hold the read lock of dnode_t::dn_struct_rwlock
755          *
756          * By taking the read lock, it can avoid thread 2 to enter into the
757          * critical section of assigning the arcbuf, while thread 1 is
758          * changing the block size.
759          */
760         down_read(&obj->oo_guard);
761         for (i = 0; i < npages; i++) {
762                 CDEBUG(D_INODE, "write %u bytes at %u\n",
763                         (unsigned) lnb[i].lnb_len,
764                         (unsigned) lnb[i].lnb_file_offset);
765
766                 if (lnb[i].lnb_rc) {
767                         /* ENOSPC, network RPC error, etc.
768                          * Unlike ldiskfs, zfs allocates new blocks on rewrite,
769                          * so we skip this page if lnb_rc is set to -ENOSPC */
770                         CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
771                                 PFID(lu_object_fid(&dt->do_lu)), i,
772                                 lnb[i].lnb_rc);
773                         continue;
774                 }
775
776                 if (lnb[i].lnb_page->mapping == (void *)obj) {
777                         dmu_write(osd->od_os, obj->oo_dn->dn_object,
778                                 lnb[i].lnb_file_offset, lnb[i].lnb_len,
779                                 kmap(lnb[i].lnb_page), oh->ot_tx);
780                         kunmap(lnb[i].lnb_page);
781                 } else if (lnb[i].lnb_data) {
782                         LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
783                         /* buffer loaned for zerocopy, try to use it.
784                          * notice that dmu_assign_arcbuf() is smart
785                          * enough to recognize changed blocksize
786                          * in this case it fallbacks to dmu_write() */
787                         dmu_assign_arcbuf(&obj->oo_dn->dn_bonus->db,
788                                           lnb[i].lnb_file_offset,
789                                           lnb[i].lnb_data, oh->ot_tx);
790                         /* drop the reference, otherwise osd_put_bufs()
791                          * will be releasing it - bad! */
792                         lnb[i].lnb_data = NULL;
793                         atomic_dec(&osd->od_zerocopy_loan);
794                 }
795
796                 if (new_size < lnb[i].lnb_file_offset + lnb[i].lnb_len)
797                         new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
798                 iosize += lnb[i].lnb_len;
799         }
800         up_read(&obj->oo_guard);
801
802         if (unlikely(new_size == 0)) {
803                 /* no pages to write, no transno is needed */
804                 th->th_local = 1;
805                 /* it is important to return 0 even when all lnb_rc == -ENOSPC
806                  * since ofd_commitrw_write() retries several times on ENOSPC */
807                 record_end_io(osd, WRITE, 0, 0, 0);
808                 RETURN(0);
809         }
810
811         write_lock(&obj->oo_attr_lock);
812         if (obj->oo_attr.la_size < new_size) {
813                 obj->oo_attr.la_size = new_size;
814                 write_unlock(&obj->oo_attr_lock);
815                 /* osd_object_sa_update() will be copying directly from
816                  * oo_attr into dbuf. any update within a single txg will copy
817                  * the most actual */
818                 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
819                                           &obj->oo_attr.la_size, 8, oh);
820         } else {
821                 write_unlock(&obj->oo_attr_lock);
822         }
823
824         record_end_io(osd, WRITE, 0, iosize, npages);
825
826         RETURN(rc);
827 }
828
829 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
830                         struct niobuf_local *lnb, int npages)
831 {
832         struct osd_object *obj  = osd_dt_obj(dt);
833         int                i;
834         loff_t             eof;
835
836         LASSERT(dt_object_exists(dt));
837         LASSERT(obj->oo_dn);
838
839         read_lock(&obj->oo_attr_lock);
840         eof = obj->oo_attr.la_size;
841         read_unlock(&obj->oo_attr_lock);
842
843         for (i = 0; i < npages; i++) {
844                 if (unlikely(lnb[i].lnb_rc < 0))
845                         continue;
846
847                 lnb[i].lnb_rc = lnb[i].lnb_len;
848
849                 if (lnb[i].lnb_file_offset + lnb[i].lnb_len >= eof) {
850                         if (eof <= lnb[i].lnb_file_offset)
851                                 lnb[i].lnb_rc = 0;
852                         else
853                                 lnb[i].lnb_rc = eof - lnb[i].lnb_file_offset;
854
855                         /* all subsequent rc should be 0 */
856                         while (++i < npages)
857                                 lnb[i].lnb_rc = 0;
858                         break;
859                 }
860         }
861
862         return 0;
863 }
864
865 /*
866  * Punch/truncate an object
867  *
868  *      IN:     db  - dmu_buf of the object to free data in.
869  *              off - start of section to free.
870  *              len - length of section to free (DMU_OBJECT_END => to EOF).
871  *
872  *      RETURN: 0 if success
873  *              error code if failure
874  *
875  * The transaction passed to this routine must have
876  * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
877  * called and then assigned to a transaction group.
878  */
879 static int __osd_object_punch(objset_t *os, dnode_t *dn, dmu_tx_t *tx,
880                                 uint64_t size, uint64_t off, uint64_t len)
881 {
882         int rc = 0;
883
884         /* Assert that the transaction has been assigned to a
885            transaction group. */
886         LASSERT(tx->tx_txg != 0);
887         /*
888          * Nothing to do if file already at desired length.
889          */
890         if (len == DMU_OBJECT_END && size == off)
891                 return 0;
892
893         /* XXX: dnode_free_range() can be used to save on dnode lookup */
894         if (off < size)
895                 dmu_free_range(os, dn->dn_object, off, len, tx);
896
897         return rc;
898 }
899
900 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
901                         __u64 start, __u64 end, struct thandle *th)
902 {
903         struct osd_object  *obj = osd_dt_obj(dt);
904         struct osd_device  *osd = osd_obj2dev(obj);
905         struct osd_thandle *oh;
906         __u64               len;
907         int                 rc = 0;
908         ENTRY;
909
910         LASSERT(dt_object_exists(dt));
911         LASSERT(osd_invariant(obj));
912
913         LASSERT(th != NULL);
914         oh = container_of0(th, struct osd_thandle, ot_super);
915
916         write_lock(&obj->oo_attr_lock);
917         /* truncate */
918         if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
919                 len = DMU_OBJECT_END;
920         else
921                 len = end - start;
922         write_unlock(&obj->oo_attr_lock);
923
924         rc = __osd_object_punch(osd->od_os, obj->oo_dn, oh->ot_tx,
925                                 obj->oo_attr.la_size, start, len);
926         /* set new size */
927         if (len == DMU_OBJECT_END) {
928                 write_lock(&obj->oo_attr_lock);
929                 obj->oo_attr.la_size = start;
930                 write_unlock(&obj->oo_attr_lock);
931                 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
932                                           &obj->oo_attr.la_size, 8, oh);
933         }
934         RETURN(rc);
935 }
936
937 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
938                         __u64 start, __u64 end, struct thandle *handle)
939 {
940         struct osd_object  *obj = osd_dt_obj(dt);
941         struct osd_device  *osd = osd_obj2dev(obj);
942         struct osd_thandle *oh;
943         __u64               len;
944         ENTRY;
945
946         oh = container_of0(handle, struct osd_thandle, ot_super);
947
948         read_lock(&obj->oo_attr_lock);
949         if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
950                 len = DMU_OBJECT_END;
951         else
952                 len = end - start;
953
954         /* declare we'll free some blocks ... */
955         if (start < obj->oo_attr.la_size) {
956                 read_unlock(&obj->oo_attr_lock);
957                 dmu_tx_hold_free(oh->ot_tx, obj->oo_dn->dn_object, start, len);
958         } else {
959                 read_unlock(&obj->oo_attr_lock);
960         }
961
962         /* ... and we'll modify size attribute */
963         dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
964
965         RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
966                                  obj->oo_attr.la_gid, 0, oh, true, NULL,
967                                  false));
968 }
969
970 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
971                        __u64 start, __u64 end, enum lu_ladvise_type advice)
972 {
973         int     rc;
974         ENTRY;
975
976         switch (advice) {
977         default:
978                 rc = -ENOTSUPP;
979                 break;
980         }
981
982         RETURN(rc);
983 }
984
985 struct dt_body_operations osd_body_ops = {
986         .dbo_read                       = osd_read,
987         .dbo_declare_write              = osd_declare_write,
988         .dbo_write                      = osd_write,
989         .dbo_bufs_get                   = osd_bufs_get,
990         .dbo_bufs_put                   = osd_bufs_put,
991         .dbo_write_prep                 = osd_write_prep,
992         .dbo_declare_write_commit       = osd_declare_write_commit,
993         .dbo_write_commit               = osd_write_commit,
994         .dbo_read_prep                  = osd_read_prep,
995         .dbo_declare_punch              = osd_declare_punch,
996         .dbo_punch                      = osd_punch,
997         .dbo_ladvise                    = osd_ladvise,
998 };