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