Whamcloud - gitweb
LU-8775 osd: do not report special writes in brw stats
[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, 2015, 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_zerocopy_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_db);
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_db->db_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_db->db_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_db);
203
204         LASSERT(th != NULL);
205         oh = container_of0(th, struct osd_thandle, ot_super);
206
207         dmu_write(osd->od_os, obj->oo_db->db_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_db);
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_zerocopy_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_db, off, len, TRUE,
327                                                   osd_zerocopy_tag, &numbufs,
328                                                   &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_zerocopy_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         arc_buf_t         *abuf;
398         uint32_t           bs;
399         uint64_t           dummy;
400         ENTRY;
401
402         dmu_object_size_from_db(obj->oo_db, &bs, &dummy);
403
404         /*
405          * currently only full blocks are subject to zerocopy approach:
406          * so that we're sure nobody is trying to update the same block
407          */
408         while (len > 0) {
409                 LASSERT(npages < PTLRPC_MAX_BRW_PAGES);
410
411                 off_in_block = off & (bs - 1);
412                 sz_in_block = min_t(int, bs - off_in_block, len);
413
414                 if (sz_in_block == bs) {
415                         /* full block, try to use zerocopy */
416
417                         abuf = dmu_request_arcbuf(obj->oo_db, bs);
418                         if (unlikely(abuf == NULL))
419                                 GOTO(out_err, rc = -ENOMEM);
420
421                         atomic_inc(&osd->od_zerocopy_loan);
422
423                         /* go over pages arcbuf contains, put them as
424                          * local niobufs for ptlrpc's bulks */
425                         while (sz_in_block > 0) {
426                                 plen = min_t(int, sz_in_block, PAGE_SIZE);
427
428                                 lnb[i].lnb_file_offset = off;
429                                 lnb[i].lnb_page_offset = 0;
430                                 lnb[i].lnb_len = plen;
431                                 lnb[i].lnb_rc = 0;
432                                 if (sz_in_block == bs)
433                                         lnb[i].lnb_data = abuf;
434                                 else
435                                         lnb[i].lnb_data = NULL;
436
437                                 /* this one is not supposed to fail */
438                                 lnb[i].lnb_page = kmem_to_page(abuf->b_data +
439                                                         off_in_block);
440                                 LASSERT(lnb[i].lnb_page);
441
442                                 lprocfs_counter_add(osd->od_stats,
443                                                 LPROC_OSD_ZEROCOPY_IO, 1);
444
445                                 sz_in_block -= plen;
446                                 len -= plen;
447                                 off += plen;
448                                 off_in_block += plen;
449                                 i++;
450                                 npages++;
451                         }
452                 } else {
453                         if (off_in_block == 0 && len < bs &&
454                                         off + len >= obj->oo_attr.la_size)
455                                 lprocfs_counter_add(osd->od_stats,
456                                                 LPROC_OSD_TAIL_IO, 1);
457
458                         /* can't use zerocopy, allocate temp. buffers */
459                         while (sz_in_block > 0) {
460                                 plen = min_t(int, sz_in_block, PAGE_SIZE);
461
462                                 lnb[i].lnb_file_offset = off;
463                                 lnb[i].lnb_page_offset = 0;
464                                 lnb[i].lnb_len = plen;
465                                 lnb[i].lnb_rc = 0;
466                                 lnb[i].lnb_data = NULL;
467
468                                 lnb[i].lnb_page = alloc_page(OSD_GFP_IO);
469                                 if (unlikely(lnb[i].lnb_page == NULL))
470                                         GOTO(out_err, rc = -ENOMEM);
471
472                                 LASSERT(lnb[i].lnb_page->mapping == NULL);
473                                 lnb[i].lnb_page->mapping = (void *)obj;
474
475                                 atomic_inc(&osd->od_zerocopy_alloc);
476                                 lprocfs_counter_add(osd->od_stats,
477                                                 LPROC_OSD_COPY_IO, 1);
478
479                                 sz_in_block -= plen;
480                                 len -= plen;
481                                 off += plen;
482                                 i++;
483                                 npages++;
484                         }
485                 }
486         }
487
488         RETURN(npages);
489
490 out_err:
491         osd_bufs_put(env, &obj->oo_dt, lnb, npages);
492         RETURN(rc);
493 }
494
495 static int osd_bufs_get(const struct lu_env *env, struct dt_object *dt,
496                         loff_t offset, ssize_t len, struct niobuf_local *lnb,
497                         int rw)
498 {
499         struct osd_object *obj  = osd_dt_obj(dt);
500         int                rc;
501
502         LASSERT(dt_object_exists(dt));
503         LASSERT(obj->oo_db);
504
505         if (rw == 0)
506                 rc = osd_bufs_get_read(env, obj, offset, len, lnb);
507         else
508                 rc = osd_bufs_get_write(env, obj, offset, len, lnb);
509
510         return rc;
511 }
512
513 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
514                         struct niobuf_local *lnb, int npages)
515 {
516         struct osd_object *obj = osd_dt_obj(dt);
517
518         LASSERT(dt_object_exists(dt));
519         LASSERT(obj->oo_db);
520
521         return 0;
522 }
523
524 static inline uint32_t osd_get_blocksz(struct osd_object *obj)
525 {
526         uint32_t blksz;
527         u_longlong_t unused;
528
529         LASSERT(obj->oo_db);
530
531         dmu_object_size_from_db(obj->oo_db, &blksz, &unused);
532         return blksz;
533 }
534
535 static inline uint64_t osd_roundup2blocksz(uint64_t size,
536                                            uint64_t offset,
537                                            uint32_t blksz)
538 {
539         LASSERT(blksz > 0);
540
541         size += offset % blksz;
542
543         if (likely(IS_PO2(blksz)))
544                 return PO2_ROUNDUP_TYPED(size, blksz, uint64_t);
545
546         size += blksz - 1;
547         do_div(size, blksz);
548         return size * blksz;
549 }
550
551 static int osd_declare_write_commit(const struct lu_env *env,
552                                     struct dt_object *dt,
553                                     struct niobuf_local *lnb, int npages,
554                                     struct thandle *th)
555 {
556         struct osd_object  *obj = osd_dt_obj(dt);
557         struct osd_device  *osd = osd_obj2dev(obj);
558         struct osd_thandle *oh;
559         uint64_t            offset = 0;
560         uint32_t            size = 0;
561         uint32_t            blksz = osd_get_blocksz(obj);
562         int                 i, rc, flags = 0;
563         bool                ignore_quota = false, synced = false;
564         long long           space = 0;
565         struct page        *last_page = NULL;
566         unsigned long       discont_pages = 0;
567         ENTRY;
568
569         LASSERT(dt_object_exists(dt));
570         LASSERT(obj->oo_db);
571
572         LASSERT(lnb);
573         LASSERT(npages > 0);
574
575         oh = container_of0(th, struct osd_thandle, ot_super);
576
577         for (i = 0; i < npages; i++) {
578                 if (last_page && lnb[i].lnb_page->index != (last_page->index + 1))
579                         ++discont_pages;
580                 last_page = lnb[i].lnb_page;
581                 if (lnb[i].lnb_rc)
582                         /* ENOSPC, network RPC error, etc.
583                          * We don't want to book space for pages which will be
584                          * skipped in osd_write_commit(). Hence we skip pages
585                          * with lnb_rc != 0 here too */
586                         continue;
587                 /* ignore quota for the whole request if any page is from
588                  * client cache or written by root.
589                  *
590                  * XXX once we drop the 1.8 client support, the checking
591                  * for whether page is from cache can be simplified as:
592                  * !(lnb[i].flags & OBD_BRW_SYNC)
593                  *
594                  * XXX we could handle this on per-lnb basis as done by
595                  * grant. */
596                 if ((lnb[i].lnb_flags & OBD_BRW_NOQUOTA) ||
597                     (lnb[i].lnb_flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
598                     OBD_BRW_FROM_GRANT)
599                         ignore_quota = true;
600                 if (size == 0) {
601                         /* first valid lnb */
602                         offset = lnb[i].lnb_file_offset;
603                         size = lnb[i].lnb_len;
604                         continue;
605                 }
606                 if (offset + size == lnb[i].lnb_file_offset) {
607                         /* this lnb is contiguous to the previous one */
608                         size += lnb[i].lnb_len;
609                         continue;
610                 }
611
612                 dmu_tx_hold_write(oh->ot_tx, obj->oo_db->db_object,
613                                   offset, size);
614                 /* Estimating space to be consumed by a write is rather
615                  * complicated with ZFS. As a consequence, we don't account for
616                  * indirect blocks and just use as a rough estimate the worse
617                  * case where the old space is being held by a snapshot. Quota
618                  * overrun will be adjusted once the operation is committed, if
619                  * required. */
620                 space += osd_roundup2blocksz(size, offset, blksz);
621
622                 offset = lnb[i].lnb_file_offset;
623                 size = lnb[i].lnb_len;
624         }
625
626         if (size) {
627                 dmu_tx_hold_write(oh->ot_tx, obj->oo_db->db_object,
628                                   offset, size);
629                 space += osd_roundup2blocksz(size, offset, blksz);
630         }
631
632         dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
633
634         oh->ot_write_commit = 1; /* used in osd_trans_start() for fail_loc */
635
636         /* backend zfs filesystem might be configured to store multiple data
637          * copies */
638         space  *= osd->od_os->os_copies;
639         space   = toqb(space);
640         CDEBUG(D_QUOTA, "writing %d pages, reserving %lldK of quota space\n",
641                npages, space);
642
643         record_start_io(osd, WRITE, discont_pages);
644 retry:
645         /* acquire quota space if needed */
646         rc = osd_declare_quota(env, osd, obj->oo_attr.la_uid,
647                                obj->oo_attr.la_gid, space, oh, true, &flags,
648                                ignore_quota);
649
650         if (!synced && rc == -EDQUOT && (flags & QUOTA_FL_SYNC) != 0) {
651                 dt_sync(env, th->th_dev);
652                 synced = true;
653                 CDEBUG(D_QUOTA, "retry after sync\n");
654                 flags = 0;
655                 goto retry;
656         }
657
658         /* we need only to store the overquota flags in the first lnb for
659          * now, once we support multiple objects BRW, this code needs be
660          * revised. */
661         if (flags & QUOTA_FL_OVER_USRQUOTA)
662                 lnb[0].lnb_flags |= OBD_BRW_OVER_USRQUOTA;
663         if (flags & QUOTA_FL_OVER_GRPQUOTA)
664                 lnb[0].lnb_flags |= OBD_BRW_OVER_GRPQUOTA;
665
666         RETURN(rc);
667 }
668
669 /**
670  * Policy to grow ZFS block size by write pattern.
671  * For sequential write, it grows block size gradually until it reaches the
672  * maximum blocksize the dataset can support. Otherwise, it will pick a
673  * a block size by the writing region of this I/O.
674  */
675 static int osd_grow_blocksize(struct osd_object *obj, struct osd_thandle *oh,
676                               uint64_t start, uint64_t end)
677 {
678         struct osd_device       *osd = osd_obj2dev(obj);
679         dmu_buf_impl_t          *db = (dmu_buf_impl_t *)obj->oo_db;
680         dnode_t                 *dn;
681         uint32_t                 blksz;
682         int                      rc = 0;
683
684         ENTRY;
685
686         DB_DNODE_ENTER(db);
687         dn = DB_DNODE(db);
688
689         if (dn->dn_maxblkid > 0) /* can't change block size */
690                 GOTO(out, rc);
691
692         if (dn->dn_datablksz >= osd->od_max_blksz)
693                 GOTO(out, rc);
694
695         down_write(&obj->oo_guard);
696
697         blksz = dn->dn_datablksz;
698         if (blksz >= osd->od_max_blksz) /* check again after grabbing lock */
699                 GOTO(out_unlock, rc);
700
701         /* now ZFS can support up to 16MB block size, and if the write
702          * is sequential, it just increases the block size gradually */
703         if (start <= blksz) { /* sequential */
704                 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz, end);
705         } else { /* sparse, pick a block size by write region */
706                 blksz = (uint32_t)min_t(uint64_t, osd->od_max_blksz,
707                                         end - start);
708         }
709
710         if (!is_power_of_2(blksz))
711                 blksz = size_roundup_power2(blksz);
712
713         if (blksz > dn->dn_datablksz) {
714                 rc = -dmu_object_set_blocksize(osd->od_os, dn->dn_object,
715                                                blksz, 0, oh->ot_tx);
716                 LASSERT(ergo(rc == 0, dn->dn_datablksz >= blksz));
717                 if (rc < 0)
718                         CDEBUG(D_INODE, "object "DFID": change block size"
719                                "%u -> %u error rc = %d\n",
720                                PFID(lu_object_fid(&obj->oo_dt.do_lu)),
721                                dn->dn_datablksz, blksz, rc);
722         }
723         EXIT;
724 out_unlock:
725         up_write(&obj->oo_guard);
726 out:
727         DB_DNODE_EXIT(db);
728         return rc;
729 }
730
731 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
732                         struct niobuf_local *lnb, int npages,
733                         struct thandle *th)
734 {
735         struct osd_object  *obj  = osd_dt_obj(dt);
736         struct osd_device  *osd = osd_obj2dev(obj);
737         struct osd_thandle *oh;
738         uint64_t            new_size = 0;
739         int                 i, rc = 0;
740         unsigned long      iosize = 0;
741         ENTRY;
742
743         LASSERT(dt_object_exists(dt));
744         LASSERT(obj->oo_db);
745
746         LASSERT(th != NULL);
747         oh = container_of0(th, struct osd_thandle, ot_super);
748
749         /* adjust block size. Assume the buffers are sorted. */
750         (void)osd_grow_blocksize(obj, oh, lnb[0].lnb_file_offset,
751                                  lnb[npages - 1].lnb_file_offset +
752                                  lnb[npages - 1].lnb_len);
753         for (i = 0; i < npages; i++) {
754                 CDEBUG(D_INODE, "write %u bytes at %u\n",
755                         (unsigned) lnb[i].lnb_len,
756                         (unsigned) lnb[i].lnb_file_offset);
757
758                 if (lnb[i].lnb_rc) {
759                         /* ENOSPC, network RPC error, etc.
760                          * Unlike ldiskfs, zfs allocates new blocks on rewrite,
761                          * so we skip this page if lnb_rc is set to -ENOSPC */
762                         CDEBUG(D_INODE, "obj "DFID": skipping lnb[%u]: rc=%d\n",
763                                 PFID(lu_object_fid(&dt->do_lu)), i,
764                                 lnb[i].lnb_rc);
765                         continue;
766                 }
767
768                 if (lnb[i].lnb_page->mapping == (void *)obj) {
769                         dmu_write(osd->od_os, obj->oo_db->db_object,
770                                 lnb[i].lnb_file_offset, lnb[i].lnb_len,
771                                 kmap(lnb[i].lnb_page), oh->ot_tx);
772                         kunmap(lnb[i].lnb_page);
773                 } else if (lnb[i].lnb_data) {
774                         LASSERT(((unsigned long)lnb[i].lnb_data & 1) == 0);
775                         /* buffer loaned for zerocopy, try to use it.
776                          * notice that dmu_assign_arcbuf() is smart
777                          * enough to recognize changed blocksize
778                          * in this case it fallbacks to dmu_write() */
779                         dmu_assign_arcbuf(obj->oo_db, lnb[i].lnb_file_offset,
780                                           lnb[i].lnb_data, oh->ot_tx);
781                         /* drop the reference, otherwise osd_put_bufs()
782                          * will be releasing it - bad! */
783                         lnb[i].lnb_data = NULL;
784                         atomic_dec(&osd->od_zerocopy_loan);
785                 }
786
787                 if (new_size < lnb[i].lnb_file_offset + lnb[i].lnb_len)
788                         new_size = lnb[i].lnb_file_offset + lnb[i].lnb_len;
789                 iosize += lnb[i].lnb_len;
790         }
791
792         if (unlikely(new_size == 0)) {
793                 /* no pages to write, no transno is needed */
794                 th->th_local = 1;
795                 /* it is important to return 0 even when all lnb_rc == -ENOSPC
796                  * since ofd_commitrw_write() retries several times on ENOSPC */
797                 record_end_io(osd, WRITE, 0, 0, 0);
798                 RETURN(0);
799         }
800
801         write_lock(&obj->oo_attr_lock);
802         if (obj->oo_attr.la_size < new_size) {
803                 obj->oo_attr.la_size = new_size;
804                 write_unlock(&obj->oo_attr_lock);
805                 /* osd_object_sa_update() will be copying directly from
806                  * oo_attr into dbuf. any update within a single txg will copy
807                  * the most actual */
808                 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
809                                           &obj->oo_attr.la_size, 8, oh);
810         } else {
811                 write_unlock(&obj->oo_attr_lock);
812         }
813
814         record_end_io(osd, WRITE, 0, iosize, npages);
815
816         RETURN(rc);
817 }
818
819 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
820                         struct niobuf_local *lnb, int npages)
821 {
822         struct osd_object *obj  = osd_dt_obj(dt);
823         int                i;
824         loff_t             eof;
825
826         LASSERT(dt_object_exists(dt));
827         LASSERT(obj->oo_db);
828
829         read_lock(&obj->oo_attr_lock);
830         eof = obj->oo_attr.la_size;
831         read_unlock(&obj->oo_attr_lock);
832
833         for (i = 0; i < npages; i++) {
834                 if (unlikely(lnb[i].lnb_rc < 0))
835                         continue;
836
837                 lnb[i].lnb_rc = lnb[i].lnb_len;
838
839                 if (lnb[i].lnb_file_offset + lnb[i].lnb_len >= eof) {
840                         if (eof <= lnb[i].lnb_file_offset)
841                                 lnb[i].lnb_rc = 0;
842                         else
843                                 lnb[i].lnb_rc = eof - lnb[i].lnb_file_offset;
844
845                         /* all subsequent rc should be 0 */
846                         while (++i < npages)
847                                 lnb[i].lnb_rc = 0;
848                         break;
849                 }
850         }
851
852         return 0;
853 }
854
855 /*
856  * Punch/truncate an object
857  *
858  *      IN:     db  - dmu_buf of the object to free data in.
859  *              off - start of section to free.
860  *              len - length of section to free (DMU_OBJECT_END => to EOF).
861  *
862  *      RETURN: 0 if success
863  *              error code if failure
864  *
865  * The transaction passed to this routine must have
866  * dmu_tx_hold_sa() and if off < size, dmu_tx_hold_free()
867  * called and then assigned to a transaction group.
868  */
869 static int __osd_object_punch(objset_t *os, dmu_buf_t *db, dmu_tx_t *tx,
870                                 uint64_t size, uint64_t off, uint64_t len)
871 {
872         int rc = 0;
873
874         /* Assert that the transaction has been assigned to a
875            transaction group. */
876         LASSERT(tx->tx_txg != 0);
877         /*
878          * Nothing to do if file already at desired length.
879          */
880         if (len == DMU_OBJECT_END && size == off)
881                 return 0;
882
883         if (off < size)
884                 rc = -dmu_free_range(os, db->db_object, off, len, tx);
885
886         return rc;
887 }
888
889 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
890                         __u64 start, __u64 end, struct thandle *th)
891 {
892         struct osd_object  *obj = osd_dt_obj(dt);
893         struct osd_device  *osd = osd_obj2dev(obj);
894         struct osd_thandle *oh;
895         __u64               len;
896         int                 rc = 0;
897         ENTRY;
898
899         LASSERT(dt_object_exists(dt));
900         LASSERT(osd_invariant(obj));
901
902         LASSERT(th != NULL);
903         oh = container_of0(th, struct osd_thandle, ot_super);
904
905         write_lock(&obj->oo_attr_lock);
906         /* truncate */
907         if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
908                 len = DMU_OBJECT_END;
909         else
910                 len = end - start;
911         write_unlock(&obj->oo_attr_lock);
912
913         rc = __osd_object_punch(osd->od_os, obj->oo_db, oh->ot_tx,
914                                 obj->oo_attr.la_size, start, len);
915         /* set new size */
916         if (len == DMU_OBJECT_END) {
917                 write_lock(&obj->oo_attr_lock);
918                 obj->oo_attr.la_size = start;
919                 write_unlock(&obj->oo_attr_lock);
920                 rc = osd_object_sa_update(obj, SA_ZPL_SIZE(osd),
921                                           &obj->oo_attr.la_size, 8, oh);
922         }
923         RETURN(rc);
924 }
925
926 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
927                         __u64 start, __u64 end, struct thandle *handle)
928 {
929         struct osd_object  *obj = osd_dt_obj(dt);
930         struct osd_device  *osd = osd_obj2dev(obj);
931         struct osd_thandle *oh;
932         __u64               len;
933         ENTRY;
934
935         oh = container_of0(handle, struct osd_thandle, ot_super);
936
937         read_lock(&obj->oo_attr_lock);
938         if (end == OBD_OBJECT_EOF || end >= obj->oo_attr.la_size)
939                 len = DMU_OBJECT_END;
940         else
941                 len = end - start;
942
943         /* declare we'll free some blocks ... */
944         if (start < obj->oo_attr.la_size) {
945                 read_unlock(&obj->oo_attr_lock);
946                 dmu_tx_hold_free(oh->ot_tx, obj->oo_db->db_object, start, len);
947         } else {
948                 read_unlock(&obj->oo_attr_lock);
949         }
950
951         /* ... and we'll modify size attribute */
952         dmu_tx_hold_sa(oh->ot_tx, obj->oo_sa_hdl, 0);
953
954         RETURN(osd_declare_quota(env, osd, obj->oo_attr.la_uid,
955                                  obj->oo_attr.la_gid, 0, oh, true, NULL,
956                                  false));
957 }
958
959 static int osd_ladvise(const struct lu_env *env, struct dt_object *dt,
960                        __u64 start, __u64 end, enum lu_ladvise_type advice)
961 {
962         int     rc;
963         ENTRY;
964
965         switch (advice) {
966         default:
967                 rc = -ENOTSUPP;
968                 break;
969         }
970
971         RETURN(rc);
972 }
973
974 struct dt_body_operations osd_body_ops = {
975         .dbo_read                       = osd_read,
976         .dbo_declare_write              = osd_declare_write,
977         .dbo_write                      = osd_write,
978         .dbo_bufs_get                   = osd_bufs_get,
979         .dbo_bufs_put                   = osd_bufs_put,
980         .dbo_write_prep                 = osd_write_prep,
981         .dbo_declare_write_commit       = osd_declare_write_commit,
982         .dbo_write_commit               = osd_write_commit,
983         .dbo_read_prep                  = osd_read_prep,
984         .dbo_declare_punch              = osd_declare_punch,
985         .dbo_punch                      = osd_punch,
986         .dbo_ladvise                    = osd_ladvise,
987 };