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