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