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