Whamcloud - gitweb
LU-2800 autoconf: remove obsolete autoconf options
[fs/lustre-release.git] / lustre / osd-ldiskfs / 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) 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, 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/osd_io.c
37  *
38  * body operations
39  *
40  * Author: Nikita Danilov <nikita@clusterfs.com>
41  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
42  *
43  */
44
45 /* LUSTRE_VERSION_CODE */
46 #include <lustre_ver.h>
47 /* prerequisite for linux/xattr.h */
48 #include <linux/types.h>
49 /* prerequisite for linux/xattr.h */
50 #include <linux/fs.h>
51
52 /*
53  * struct OBD_{ALLOC,FREE}*()
54  * OBD_FAIL_CHECK
55  */
56 #include <obd_support.h>
57
58 #include "osd_internal.h"
59
60 /* ext_depth() */
61 #include <ldiskfs/ldiskfs_extents.h>
62
63 #ifndef HAVE_PAGE_CONSTANT
64 #define mapping_cap_page_constant_write(mapping) 0
65 #define SetPageConstant(page) do {} while (0)
66 #define ClearPageConstant(page) do {} while (0)
67 #endif
68
69 static int __osd_init_iobuf(struct osd_device *d, struct osd_iobuf *iobuf,
70                             int rw, int line, int pages)
71 {
72         int blocks, i;
73
74         LASSERTF(iobuf->dr_elapsed_valid == 0,
75                  "iobuf %p, reqs %d, rw %d, line %d\n", iobuf,
76                  cfs_atomic_read(&iobuf->dr_numreqs), iobuf->dr_rw,
77                  iobuf->dr_init_at);
78         LASSERT(pages <= PTLRPC_MAX_BRW_PAGES);
79
80         cfs_waitq_init(&iobuf->dr_wait);
81         cfs_atomic_set(&iobuf->dr_numreqs, 0);
82         iobuf->dr_npages = 0;
83         iobuf->dr_error = 0;
84         iobuf->dr_dev = d;
85         iobuf->dr_frags = 0;
86         iobuf->dr_elapsed = 0;
87         /* must be counted before, so assert */
88         iobuf->dr_rw = rw;
89         iobuf->dr_init_at = line;
90
91         blocks = pages * (PAGE_CACHE_SIZE >> osd_sb(d)->s_blocksize_bits);
92         if (iobuf->dr_bl_buf.lb_len >= blocks * sizeof(iobuf->dr_blocks[0])) {
93                 LASSERT(iobuf->dr_pg_buf.lb_len >=
94                         pages * sizeof(iobuf->dr_pages[0]));
95                 return 0;
96         }
97
98         /* start with 1MB for 4K blocks */
99         i = 256;
100         while (i <= PTLRPC_MAX_BRW_PAGES && i < pages)
101                 i <<= 1;
102
103         CDEBUG(D_OTHER, "realloc %u for %u (%u) pages\n",
104                (unsigned)(pages * sizeof(iobuf->dr_pages[0])), i, pages);
105         pages = i;
106         blocks = pages * (PAGE_CACHE_SIZE >> osd_sb(d)->s_blocksize_bits);
107         iobuf->dr_max_pages = 0;
108         CDEBUG(D_OTHER, "realloc %u for %u blocks\n",
109                (unsigned)(blocks * sizeof(iobuf->dr_blocks[0])), blocks);
110
111         lu_buf_realloc(&iobuf->dr_bl_buf, blocks * sizeof(iobuf->dr_blocks[0]));
112         iobuf->dr_blocks = iobuf->dr_bl_buf.lb_buf;
113         if (unlikely(iobuf->dr_blocks == NULL))
114                 return -ENOMEM;
115
116         lu_buf_realloc(&iobuf->dr_pg_buf, pages * sizeof(iobuf->dr_pages[0]));
117         iobuf->dr_pages = iobuf->dr_pg_buf.lb_buf;
118         if (unlikely(iobuf->dr_pages == NULL))
119                 return -ENOMEM;
120
121         iobuf->dr_max_pages = pages;
122
123         return 0;
124 }
125 #define osd_init_iobuf(dev, iobuf, rw, pages) \
126         __osd_init_iobuf(dev, iobuf, rw, __LINE__, pages)
127
128 static void osd_iobuf_add_page(struct osd_iobuf *iobuf, struct page *page)
129 {
130         LASSERT(iobuf->dr_npages < iobuf->dr_max_pages);
131         iobuf->dr_pages[iobuf->dr_npages++] = page;
132 }
133
134 void osd_fini_iobuf(struct osd_device *d, struct osd_iobuf *iobuf)
135 {
136         int rw = iobuf->dr_rw;
137
138         if (iobuf->dr_elapsed_valid) {
139                 iobuf->dr_elapsed_valid = 0;
140                 LASSERT(iobuf->dr_dev == d);
141                 LASSERT(iobuf->dr_frags > 0);
142                 lprocfs_oh_tally(&d->od_brw_stats.
143                                  hist[BRW_R_DIO_FRAGS+rw],
144                                  iobuf->dr_frags);
145                 lprocfs_oh_tally_log2(&d->od_brw_stats.hist[BRW_R_IO_TIME+rw],
146                                       iobuf->dr_elapsed);
147         }
148 }
149
150 #ifndef REQ_WRITE /* pre-2.6.35 */
151 #define __REQ_WRITE BIO_RW
152 #endif
153
154 #ifdef HAVE_BIO_ENDIO_2ARG
155 #define DIO_RETURN(a)
156 static void dio_complete_routine(struct bio *bio, int error)
157 #else
158 #define DIO_RETURN(a)   return(a)
159 static int dio_complete_routine(struct bio *bio, unsigned int done, int error)
160 #endif
161 {
162         struct osd_iobuf *iobuf = bio->bi_private;
163         struct bio_vec *bvl;
164         int i;
165
166         /* CAVEAT EMPTOR: possibly in IRQ context
167          * DO NOT record procfs stats here!!! */
168
169         if (unlikely(iobuf == NULL)) {
170                 CERROR("***** bio->bi_private is NULL!  This should never "
171                        "happen.  Normally, I would crash here, but instead I "
172                        "will dump the bio contents to the console.  Please "
173                        "report this to <http://jira.whamcloud.com/> , along "
174                        "with any interesting messages leading up to this point "
175                        "(like SCSI errors, perhaps).  Because bi_private is "
176                        "NULL, I can't wake up the thread that initiated this "
177                        "IO - you will probably have to reboot this node.\n");
178                 CERROR("bi_next: %p, bi_flags: %lx, bi_rw: %lu, bi_vcnt: %d, "
179                        "bi_idx: %d, bi->size: %d, bi_end_io: %p, bi_cnt: %d, "
180                        "bi_private: %p\n", bio->bi_next, bio->bi_flags,
181                        bio->bi_rw, bio->bi_vcnt, bio->bi_idx, bio->bi_size,
182                        bio->bi_end_io, cfs_atomic_read(&bio->bi_cnt),
183                        bio->bi_private);
184                 DIO_RETURN(0);
185         }
186
187         /* the check is outside of the cycle for performance reason -bzzz */
188         if (!test_bit(__REQ_WRITE, &bio->bi_rw)) {
189                 bio_for_each_segment(bvl, bio, i) {
190                         if (likely(error == 0))
191                                 SetPageUptodate(bvl->bv_page);
192                         LASSERT(PageLocked(bvl->bv_page));
193                         ClearPageConstant(bvl->bv_page);
194                 }
195                 cfs_atomic_dec(&iobuf->dr_dev->od_r_in_flight);
196         } else {
197                 struct page *p = iobuf->dr_pages[0];
198                 if (p->mapping) {
199                         if (mapping_cap_page_constant_write(p->mapping)) {
200                                 bio_for_each_segment(bvl, bio, i) {
201                                         ClearPageConstant(bvl->bv_page);
202                                 }
203                         }
204                 }
205                 cfs_atomic_dec(&iobuf->dr_dev->od_w_in_flight);
206         }
207
208         /* any real error is good enough -bzzz */
209         if (error != 0 && iobuf->dr_error == 0)
210                 iobuf->dr_error = error;
211
212         /*
213          * set dr_elapsed before dr_numreqs turns to 0, otherwise
214          * it's possible that service thread will see dr_numreqs
215          * is zero, but dr_elapsed is not set yet, leading to lost
216          * data in this processing and an assertion in a subsequent
217          * call to OSD.
218          */
219         if (cfs_atomic_read(&iobuf->dr_numreqs) == 1) {
220                 iobuf->dr_elapsed = jiffies - iobuf->dr_start_time;
221                 iobuf->dr_elapsed_valid = 1;
222         }
223         if (cfs_atomic_dec_and_test(&iobuf->dr_numreqs))
224                 cfs_waitq_signal(&iobuf->dr_wait);
225
226         /* Completed bios used to be chained off iobuf->dr_bios and freed in
227          * filter_clear_dreq().  It was then possible to exhaust the biovec-256
228          * mempool when serious on-disk fragmentation was encountered,
229          * deadlocking the OST.  The bios are now released as soon as complete
230          * so the pool cannot be exhausted while IOs are competing. bug 10076 */
231         bio_put(bio);
232         DIO_RETURN(0);
233 }
234
235 static void record_start_io(struct osd_iobuf *iobuf, int size)
236 {
237         struct osd_device    *osd = iobuf->dr_dev;
238         struct obd_histogram *h = osd->od_brw_stats.hist;
239
240         iobuf->dr_frags++;
241         cfs_atomic_inc(&iobuf->dr_numreqs);
242
243         if (iobuf->dr_rw == 0) {
244                 cfs_atomic_inc(&osd->od_r_in_flight);
245                 lprocfs_oh_tally(&h[BRW_R_RPC_HIST],
246                                  cfs_atomic_read(&osd->od_r_in_flight));
247                 lprocfs_oh_tally_log2(&h[BRW_R_DISK_IOSIZE], size);
248         } else if (iobuf->dr_rw == 1) {
249                 cfs_atomic_inc(&osd->od_w_in_flight);
250                 lprocfs_oh_tally(&h[BRW_W_RPC_HIST],
251                                  cfs_atomic_read(&osd->od_w_in_flight));
252                 lprocfs_oh_tally_log2(&h[BRW_W_DISK_IOSIZE], size);
253         } else {
254                 LBUG();
255         }
256 }
257
258 static void osd_submit_bio(int rw, struct bio *bio)
259 {
260         LASSERTF(rw == 0 || rw == 1, "%x\n", rw);
261         if (rw == 0)
262                 submit_bio(READ, bio);
263         else
264                 submit_bio(WRITE, bio);
265 }
266
267 static int can_be_merged(struct bio *bio, sector_t sector)
268 {
269         unsigned int size;
270
271         if (!bio)
272                 return 0;
273
274         size = bio->bi_size >> 9;
275         return bio->bi_sector + size == sector ? 1 : 0;
276 }
277
278 static int osd_do_bio(struct osd_device *osd, struct inode *inode,
279                       struct osd_iobuf *iobuf)
280 {
281         int            blocks_per_page = PAGE_CACHE_SIZE >> inode->i_blkbits;
282         struct page  **pages = iobuf->dr_pages;
283         int            npages = iobuf->dr_npages;
284         unsigned long *blocks = iobuf->dr_blocks;
285         int            total_blocks = npages * blocks_per_page;
286         int            sector_bits = inode->i_sb->s_blocksize_bits - 9;
287         unsigned int   blocksize = inode->i_sb->s_blocksize;
288         struct bio    *bio = NULL;
289         struct page   *page;
290         unsigned int   page_offset;
291         sector_t       sector;
292         int            nblocks;
293         int            block_idx;
294         int            page_idx;
295         int            i;
296         int            rc = 0;
297         ENTRY;
298
299         LASSERT(iobuf->dr_npages == npages);
300
301         osd_brw_stats_update(osd, iobuf);
302         iobuf->dr_start_time = cfs_time_current();
303
304         for (page_idx = 0, block_idx = 0;
305              page_idx < npages;
306              page_idx++, block_idx += blocks_per_page) {
307
308                 page = pages[page_idx];
309                 LASSERT(block_idx + blocks_per_page <= total_blocks);
310
311                 for (i = 0, page_offset = 0;
312                      i < blocks_per_page;
313                      i += nblocks, page_offset += blocksize * nblocks) {
314
315                         nblocks = 1;
316
317                         if (blocks[block_idx + i] == 0) {  /* hole */
318                                 LASSERTF(iobuf->dr_rw == 0,
319                                          "page_idx %u, block_idx %u, i %u\n",
320                                          page_idx, block_idx, i);
321                                 memset(kmap(page) + page_offset, 0, blocksize);
322                                 kunmap(page);
323                                 continue;
324                         }
325
326                         sector = (sector_t)blocks[block_idx + i] << sector_bits;
327
328                         /* Additional contiguous file blocks? */
329                         while (i + nblocks < blocks_per_page &&
330                                (sector + (nblocks << sector_bits)) ==
331                                ((sector_t)blocks[block_idx + i + nblocks] <<
332                                 sector_bits))
333                                 nblocks++;
334
335                         /* I only set the page to be constant only if it
336                          * is mapped to a contiguous underlying disk block(s).
337                          * It will then make sure the corresponding device
338                          * cache of raid5 will be overwritten by this page.
339                          * - jay */
340                         if (iobuf->dr_rw && (nblocks == blocks_per_page) &&
341                             mapping_cap_page_constant_write(inode->i_mapping))
342                                 SetPageConstant(page);
343
344                         if (bio != NULL &&
345                             can_be_merged(bio, sector) &&
346                             bio_add_page(bio, page,
347                                          blocksize * nblocks, page_offset) != 0)
348                                 continue;       /* added this frag OK */
349
350                         if (bio != NULL) {
351                                 struct request_queue *q =
352                                         bdev_get_queue(bio->bi_bdev);
353
354                                 /* Dang! I have to fragment this I/O */
355                                 CDEBUG(D_INODE, "bio++ sz %d vcnt %d(%d) "
356                                        "sectors %d(%d) psg %d(%d) hsg %d(%d)\n",
357                                        bio->bi_size,
358                                        bio->bi_vcnt, bio->bi_max_vecs,
359                                        bio->bi_size >> 9, queue_max_sectors(q),
360                                        bio_phys_segments(q, bio),
361                                        queue_max_phys_segments(q),
362                                        bio_hw_segments(q, bio),
363                                        queue_max_hw_segments(q));
364
365                                 record_start_io(iobuf, bio->bi_size);
366                                 osd_submit_bio(iobuf->dr_rw, bio);
367                         }
368
369                         /* allocate new bio */
370                         bio = bio_alloc(GFP_NOIO, min(BIO_MAX_PAGES,
371                                                       (npages - page_idx) *
372                                                       blocks_per_page));
373                         if (bio == NULL) {
374                                 CERROR("Can't allocate bio %u*%u = %u pages\n",
375                                        (npages - page_idx), blocks_per_page,
376                                        (npages - page_idx) * blocks_per_page);
377                                 rc = -ENOMEM;
378                                 goto out;
379                         }
380
381                         bio->bi_bdev = inode->i_sb->s_bdev;
382                         bio->bi_sector = sector;
383                         bio->bi_rw = (iobuf->dr_rw == 0) ? READ : WRITE;
384                         bio->bi_end_io = dio_complete_routine;
385                         bio->bi_private = iobuf;
386
387                         rc = bio_add_page(bio, page,
388                                           blocksize * nblocks, page_offset);
389                         LASSERT(rc != 0);
390                 }
391         }
392
393         if (bio != NULL) {
394                 record_start_io(iobuf, bio->bi_size);
395                 osd_submit_bio(iobuf->dr_rw, bio);
396                 rc = 0;
397         }
398
399  out:
400         /* in order to achieve better IO throughput, we don't wait for writes
401          * completion here. instead we proceed with transaction commit in
402          * parallel and wait for IO completion once transaction is stopped
403          * see osd_trans_stop() for more details -bzzz */
404         if (iobuf->dr_rw == 0) {
405                 cfs_wait_event(iobuf->dr_wait,
406                                cfs_atomic_read(&iobuf->dr_numreqs) == 0);
407         }
408
409         if (rc == 0)
410                 rc = iobuf->dr_error;
411         RETURN(rc);
412 }
413
414 static int osd_map_remote_to_local(loff_t offset, ssize_t len, int *nrpages,
415                                    struct niobuf_local *lnb)
416 {
417         ENTRY;
418
419         *nrpages = 0;
420
421         while (len > 0) {
422                 int poff = offset & (PAGE_CACHE_SIZE - 1);
423                 int plen = PAGE_CACHE_SIZE - poff;
424
425                 if (plen > len)
426                         plen = len;
427                 lnb->lnb_file_offset = offset;
428                 lnb->lnb_page_offset = poff;
429                 lnb->len = plen;
430                 /* lb->flags = rnb->flags; */
431                 lnb->flags = 0;
432                 lnb->page = NULL;
433                 lnb->rc = 0;
434
435                 LASSERTF(plen <= len, "plen %u, len %lld\n", plen,
436                          (long long) len);
437                 offset += plen;
438                 len -= plen;
439                 lnb++;
440                 (*nrpages)++;
441         }
442
443         RETURN(0);
444 }
445
446 struct page *osd_get_page(struct dt_object *dt, loff_t offset, int rw)
447 {
448         struct inode      *inode = osd_dt_obj(dt)->oo_inode;
449         struct osd_device *d = osd_obj2dev(osd_dt_obj(dt));
450         struct page       *page;
451
452         LASSERT(inode);
453
454         page = find_or_create_page(inode->i_mapping, offset >> PAGE_CACHE_SHIFT,
455                                    GFP_NOFS | __GFP_HIGHMEM);
456         if (unlikely(page == NULL))
457                 lprocfs_counter_add(d->od_stats, LPROC_OSD_NO_PAGE, 1);
458
459         return page;
460 }
461
462 /*
463  * there are following "locks":
464  * journal_start
465  * i_mutex
466  * page lock
467
468  * osd write path
469     * lock page(s)
470     * journal_start
471     * truncate_sem
472
473  * ext4 vmtruncate:
474     * lock pages, unlock
475     * journal_start
476     * lock partial page
477     * i_data_sem
478
479 */
480 int osd_bufs_get(const struct lu_env *env, struct dt_object *d, loff_t pos,
481                  ssize_t len, struct niobuf_local *lnb, int rw,
482                  struct lustre_capa *capa)
483 {
484         struct osd_object   *obj    = osd_dt_obj(d);
485         int npages, i, rc = 0;
486
487         LASSERT(obj->oo_inode);
488
489         osd_map_remote_to_local(pos, len, &npages, lnb);
490
491         for (i = 0; i < npages; i++, lnb++) {
492
493                 /* We still set up for ungranted pages so that granted pages
494                  * can be written to disk as they were promised, and portals
495                  * needs to keep the pages all aligned properly. */
496                 lnb->dentry = (void *) obj;
497
498                 lnb->page = osd_get_page(d, lnb->lnb_file_offset, rw);
499                 if (lnb->page == NULL)
500                         GOTO(cleanup, rc = -ENOMEM);
501
502                 /* DLM locking protects us from write and truncate competing
503                  * for same region, but truncate can leave dirty page in the
504                  * cache. it's possible the writeout on a such a page is in
505                  * progress when we access it. it's also possible that during
506                  * this writeout we put new (partial) data, but then won't
507                  * be able to proceed in filter_commitrw_write(). thus let's
508                  * just wait for writeout completion, should be rare enough.
509                  * -bzzz */
510                 wait_on_page_writeback(lnb->page);
511                 BUG_ON(PageWriteback(lnb->page));
512
513                 lu_object_get(&d->do_lu);
514         }
515         rc = i;
516
517 cleanup:
518         RETURN(rc);
519 }
520
521 static int osd_bufs_put(const struct lu_env *env, struct dt_object *dt,
522                         struct niobuf_local *lnb, int npages)
523 {
524         struct osd_thread_info *oti = osd_oti_get(env);
525         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
526         struct osd_device      *d = osd_obj2dev(osd_dt_obj(dt));
527         int                     i;
528
529         /* to do IO stats, notice we do this here because
530          * osd_do_bio() doesn't wait for write to complete */
531         osd_fini_iobuf(d, iobuf);
532
533         for (i = 0; i < npages; i++) {
534                 if (lnb[i].page == NULL)
535                         continue;
536                 LASSERT(PageLocked(lnb[i].page));
537                 unlock_page(lnb[i].page);
538                 page_cache_release(lnb[i].page);
539                 lu_object_put(env, &dt->do_lu);
540                 lnb[i].page = NULL;
541         }
542         RETURN(0);
543 }
544
545 static int osd_write_prep(const struct lu_env *env, struct dt_object *dt,
546                           struct niobuf_local *lnb, int npages)
547 {
548         struct osd_thread_info *oti   = osd_oti_get(env);
549         struct osd_iobuf       *iobuf = &oti->oti_iobuf;
550         struct inode           *inode = osd_dt_obj(dt)->oo_inode;
551         struct osd_device      *osd   = osd_obj2dev(osd_dt_obj(dt));
552         struct timeval          start;
553         struct timeval          end;
554         unsigned long           timediff;
555         ssize_t                 isize;
556         __s64                   maxidx;
557         int                     rc = 0;
558         int                     i;
559         int                     cache = 0;
560
561         LASSERT(inode);
562
563         rc = osd_init_iobuf(osd, iobuf, 0, npages);
564         if (unlikely(rc != 0))
565                 RETURN(rc);
566
567         isize = i_size_read(inode);
568         maxidx = ((isize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT) - 1;
569
570         if (osd->od_writethrough_cache)
571                 cache = 1;
572         if (isize > osd->od_readcache_max_filesize)
573                 cache = 0;
574
575         cfs_gettimeofday(&start);
576         for (i = 0; i < npages; i++) {
577
578                 if (cache == 0)
579                         generic_error_remove_page(inode->i_mapping,
580                                                   lnb[i].page);
581
582                 /*
583                  * till commit the content of the page is undefined
584                  * we'll set it uptodate once bulk is done. otherwise
585                  * subsequent reads can access non-stable data
586                  */
587                 ClearPageUptodate(lnb[i].page);
588
589                 if (lnb[i].len == PAGE_CACHE_SIZE)
590                         continue;
591
592                 if (maxidx >= lnb[i].page->index) {
593                         osd_iobuf_add_page(iobuf, lnb[i].page);
594                 } else {
595                         long off;
596                         char *p = kmap(lnb[i].page);
597
598                         off = lnb[i].lnb_page_offset;
599                         if (off)
600                                 memset(p, 0, off);
601                         off = (lnb[i].lnb_page_offset + lnb[i].len) &
602                               ~CFS_PAGE_MASK;
603                         if (off)
604                                 memset(p + off, 0, PAGE_CACHE_SIZE - off);
605                         kunmap(lnb[i].page);
606                 }
607         }
608         cfs_gettimeofday(&end);
609         timediff = cfs_timeval_sub(&end, &start, NULL);
610         lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
611
612         if (iobuf->dr_npages) {
613                 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
614                                                        iobuf->dr_npages,
615                                                        iobuf->dr_blocks,
616                                                        0, NULL);
617                 if (likely(rc == 0)) {
618                         rc = osd_do_bio(osd, inode, iobuf);
619                         /* do IO stats for preparation reads */
620                         osd_fini_iobuf(osd, iobuf);
621                 }
622         }
623         RETURN(rc);
624 }
625
626 /* Check if a block is allocated or not */
627 static int osd_is_mapped(struct inode *inode, obd_size offset)
628 {
629         sector_t (*fs_bmap)(struct address_space *, sector_t);
630
631         fs_bmap = inode->i_mapping->a_ops->bmap;
632
633         /* We can't know if we are overwriting or not */
634         if (unlikely(fs_bmap == NULL))
635                 return 0;
636
637         if (i_size_read(inode) == 0)
638                 return 0;
639
640         /* Beyond EOF, must not be mapped */
641         if (((i_size_read(inode) - 1) >> inode->i_blkbits) <
642             (offset >> inode->i_blkbits))
643                 return 0;
644
645         if (fs_bmap(inode->i_mapping, offset >> inode->i_blkbits) == 0)
646                 return 0;
647
648         return 1;
649 }
650
651 static int osd_declare_write_commit(const struct lu_env *env,
652                                     struct dt_object *dt,
653                                     struct niobuf_local *lnb, int npages,
654                                     struct thandle *handle)
655 {
656         const struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
657         struct inode            *inode = osd_dt_obj(dt)->oo_inode;
658         struct osd_thandle      *oh;
659         int                      extents = 1;
660         int                      depth;
661         int                      i;
662         int                      newblocks;
663         int                      rc = 0;
664         int                      flags = 0;
665         bool                     ignore_quota = false;
666         long long                quota_space = 0;
667         ENTRY;
668
669         LASSERT(handle != NULL);
670         oh = container_of0(handle, struct osd_thandle, ot_super);
671         LASSERT(oh->ot_handle == NULL);
672
673         newblocks = npages;
674
675         /* calculate number of extents (probably better to pass nb) */
676         for (i = 0; i < npages; i++) {
677                 if (i && lnb[i].lnb_file_offset !=
678                     lnb[i - 1].lnb_file_offset + lnb[i - 1].len)
679                         extents++;
680
681                 if (!osd_is_mapped(inode, lnb[i].lnb_file_offset))
682                         quota_space += PAGE_CACHE_SIZE;
683
684                 /* ignore quota for the whole request if any page is from
685                  * client cache or written by root.
686                  *
687                  * XXX once we drop the 1.8 client support, the checking
688                  * for whether page is from cache can be simplified as:
689                  * !(lnb[i].flags & OBD_BRW_SYNC)
690                  *
691                  * XXX we could handle this on per-lnb basis as done by
692                  * grant. */
693                 if ((lnb[i].flags & OBD_BRW_NOQUOTA) ||
694                     (lnb[i].flags & (OBD_BRW_FROM_GRANT | OBD_BRW_SYNC)) ==
695                     OBD_BRW_FROM_GRANT)
696                         ignore_quota = true;
697         }
698
699         /*
700          * each extent can go into new leaf causing a split
701          * 5 is max tree depth: inode + 4 index blocks
702          * with blockmaps, depth is 3 at most
703          */
704         if (LDISKFS_I(inode)->i_flags & LDISKFS_EXTENTS_FL) {
705                 /*
706                  * many concurrent threads may grow tree by the time
707                  * our transaction starts. so, consider 2 is a min depth
708                  */
709                 depth = ext_depth(inode);
710                 depth = max(depth, 1) + 1;
711                 newblocks += depth;
712                 oh->ot_credits++; /* inode */
713                 oh->ot_credits += depth * 2 * extents;
714         } else {
715                 depth = 3;
716                 newblocks += depth;
717                 oh->ot_credits++; /* inode */
718                 oh->ot_credits += depth * extents;
719         }
720
721         /* quota space for metadata blocks */
722         quota_space += depth * extents * LDISKFS_BLOCK_SIZE(osd_sb(osd));
723
724         /* quota space should be reported in 1K blocks */
725         quota_space = toqb(quota_space);
726
727         /* each new block can go in different group (bitmap + gd) */
728
729         /* we can't dirty more bitmap blocks than exist */
730         if (newblocks > LDISKFS_SB(osd_sb(osd))->s_groups_count)
731                 oh->ot_credits += LDISKFS_SB(osd_sb(osd))->s_groups_count;
732         else
733                 oh->ot_credits += newblocks;
734
735         /* we can't dirty more gd blocks than exist */
736         if (newblocks > LDISKFS_SB(osd_sb(osd))->s_gdb_count)
737                 oh->ot_credits += LDISKFS_SB(osd_sb(osd))->s_gdb_count;
738         else
739                 oh->ot_credits += newblocks;
740
741         /* make sure the over quota flags were not set */
742         lnb[0].flags &= ~(OBD_BRW_OVER_USRQUOTA | OBD_BRW_OVER_GRPQUOTA);
743
744         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid,
745                                    quota_space, oh, true, true, &flags,
746                                    ignore_quota);
747
748         /* we need only to store the overquota flags in the first lnb for
749          * now, once we support multiple objects BRW, this code needs be
750          * revised. */
751         if (flags & QUOTA_FL_OVER_USRQUOTA)
752                 lnb[0].flags |= OBD_BRW_OVER_USRQUOTA;
753         if (flags & QUOTA_FL_OVER_GRPQUOTA)
754                 lnb[0].flags |= OBD_BRW_OVER_GRPQUOTA;
755
756         RETURN(rc);
757 }
758
759 /* Check if a block is allocated or not */
760 static int osd_write_commit(const struct lu_env *env, struct dt_object *dt,
761                             struct niobuf_local *lnb, int npages,
762                             struct thandle *thandle)
763 {
764         struct osd_thread_info *oti = osd_oti_get(env);
765         struct osd_iobuf *iobuf = &oti->oti_iobuf;
766         struct inode *inode = osd_dt_obj(dt)->oo_inode;
767         struct osd_device  *osd = osd_obj2dev(osd_dt_obj(dt));
768         loff_t isize;
769         int rc = 0, i;
770
771         LASSERT(inode);
772
773         rc = osd_init_iobuf(osd, iobuf, 1, npages);
774         if (unlikely(rc != 0))
775                 RETURN(rc);
776
777         isize = i_size_read(inode);
778         ll_vfs_dq_init(inode);
779
780         for (i = 0; i < npages; i++) {
781                 if (lnb[i].rc == -ENOSPC &&
782                     osd_is_mapped(inode, lnb[i].lnb_file_offset)) {
783                         /* Allow the write to proceed if overwriting an
784                          * existing block */
785                         lnb[i].rc = 0;
786                 }
787
788                 if (lnb[i].rc) { /* ENOSPC, network RPC error, etc. */
789                         CDEBUG(D_INODE, "Skipping [%d] == %d\n", i,
790                                lnb[i].rc);
791                         LASSERT(lnb[i].page);
792                         generic_error_remove_page(inode->i_mapping,lnb[i].page);
793                         continue;
794                 }
795
796                 LASSERT(PageLocked(lnb[i].page));
797                 LASSERT(!PageWriteback(lnb[i].page));
798
799                 if (lnb[i].lnb_file_offset + lnb[i].len > isize)
800                         isize = lnb[i].lnb_file_offset + lnb[i].len;
801
802                 /*
803                  * Since write and truncate are serialized by oo_sem, even
804                  * partial-page truncate should not leave dirty pages in the
805                  * page cache.
806                  */
807                 LASSERT(!PageDirty(lnb[i].page));
808
809                 SetPageUptodate(lnb[i].page);
810
811                 osd_iobuf_add_page(iobuf, lnb[i].page);
812         }
813
814         if (OBD_FAIL_CHECK(OBD_FAIL_OST_MAPBLK_ENOSPC)) {
815                 rc = -ENOSPC;
816         } else if (iobuf->dr_npages > 0) {
817                 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
818                                                        iobuf->dr_npages,
819                                                        iobuf->dr_blocks,
820                                                        1, NULL);
821         } else {
822                 /* no pages to write, no transno is needed */
823                 thandle->th_local = 1;
824         }
825
826         if (likely(rc == 0)) {
827                 if (isize > i_size_read(inode)) {
828                         i_size_write(inode, isize);
829                         LDISKFS_I(inode)->i_disksize = isize;
830                         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
831                 }
832
833                 rc = osd_do_bio(osd, inode, iobuf);
834                 /* we don't do stats here as in read path because
835                  * write is async: we'll do this in osd_put_bufs() */
836         }
837
838         if (unlikely(rc != 0)) {
839                 /* if write fails, we should drop pages from the cache */
840                 for (i = 0; i < npages; i++) {
841                         if (lnb[i].page == NULL)
842                                 continue;
843                         LASSERT(PageLocked(lnb[i].page));
844                         generic_error_remove_page(inode->i_mapping,lnb[i].page);
845                 }
846         }
847
848         RETURN(rc);
849 }
850
851 static int osd_read_prep(const struct lu_env *env, struct dt_object *dt,
852                          struct niobuf_local *lnb, int npages)
853 {
854         struct osd_thread_info *oti = osd_oti_get(env);
855         struct osd_iobuf *iobuf = &oti->oti_iobuf;
856         struct inode *inode = osd_dt_obj(dt)->oo_inode;
857         struct osd_device *osd = osd_obj2dev(osd_dt_obj(dt));
858         struct timeval start, end;
859         unsigned long timediff;
860         int rc = 0, i, m = 0, cache = 0;
861
862         LASSERT(inode);
863
864         rc = osd_init_iobuf(osd, iobuf, 0, npages);
865         if (unlikely(rc != 0))
866                 RETURN(rc);
867
868         if (osd->od_read_cache)
869                 cache = 1;
870         if (i_size_read(inode) > osd->od_readcache_max_filesize)
871                 cache = 0;
872
873         cfs_gettimeofday(&start);
874         for (i = 0; i < npages; i++) {
875
876                 if (i_size_read(inode) <= lnb[i].lnb_file_offset)
877                         /* If there's no more data, abort early.
878                          * lnb->rc == 0, so it's easy to detect later. */
879                         break;
880
881                 if (i_size_read(inode) <
882                     lnb[i].lnb_file_offset + lnb[i].len - 1)
883                         lnb[i].rc = i_size_read(inode) - lnb[i].lnb_file_offset;
884                 else
885                         lnb[i].rc = lnb[i].len;
886                 m += lnb[i].len;
887
888                 lprocfs_counter_add(osd->od_stats, LPROC_OSD_CACHE_ACCESS, 1);
889                 if (PageUptodate(lnb[i].page)) {
890                         lprocfs_counter_add(osd->od_stats,
891                                             LPROC_OSD_CACHE_HIT, 1);
892                 } else {
893                         lprocfs_counter_add(osd->od_stats,
894                                             LPROC_OSD_CACHE_MISS, 1);
895                         osd_iobuf_add_page(iobuf, lnb[i].page);
896                 }
897                 if (cache == 0)
898                         generic_error_remove_page(inode->i_mapping,lnb[i].page);
899         }
900         cfs_gettimeofday(&end);
901         timediff = cfs_timeval_sub(&end, &start, NULL);
902         lprocfs_counter_add(osd->od_stats, LPROC_OSD_GET_PAGE, timediff);
903
904         if (iobuf->dr_npages) {
905                 rc = osd->od_fsops->fs_map_inode_pages(inode, iobuf->dr_pages,
906                                                        iobuf->dr_npages,
907                                                        iobuf->dr_blocks,
908                                                        0, NULL);
909                 rc = osd_do_bio(osd, inode, iobuf);
910
911                 /* IO stats will be done in osd_bufs_put() */
912         }
913
914         RETURN(rc);
915 }
916
917 /*
918  * XXX: Another layering violation for now.
919  *
920  * We don't want to use ->f_op->read methods, because generic file write
921  *
922  *         - serializes on ->i_sem, and
923  *
924  *         - does a lot of extra work like balance_dirty_pages(),
925  *
926  * which doesn't work for globally shared files like /last_rcvd.
927  */
928 static int osd_ldiskfs_readlink(struct inode *inode, char *buffer, int buflen)
929 {
930         struct ldiskfs_inode_info *ei = LDISKFS_I(inode);
931
932         memcpy(buffer, (char *)ei->i_data, buflen);
933
934         return  buflen;
935 }
936
937 int osd_ldiskfs_read(struct inode *inode, void *buf, int size, loff_t *offs)
938 {
939         struct buffer_head *bh;
940         unsigned long block;
941         int osize;
942         int blocksize;
943         int csize;
944         int boffs;
945         int err;
946
947         /* prevent reading after eof */
948         spin_lock(&inode->i_lock);
949         if (i_size_read(inode) < *offs + size) {
950                 loff_t diff = i_size_read(inode) - *offs;
951                 spin_unlock(&inode->i_lock);
952                 if (diff < 0) {
953                         CDEBUG(D_EXT2, "size %llu is too short to read @%llu\n",
954                                i_size_read(inode), *offs);
955                         return -EBADR;
956                 } else if (diff == 0) {
957                         return 0;
958                 } else {
959                         size = diff;
960                 }
961         } else {
962                 spin_unlock(&inode->i_lock);
963         }
964
965         blocksize = 1 << inode->i_blkbits;
966         osize = size;
967         while (size > 0) {
968                 block = *offs >> inode->i_blkbits;
969                 boffs = *offs & (blocksize - 1);
970                 csize = min(blocksize - boffs, size);
971                 bh = ldiskfs_bread(NULL, inode, block, 0, &err);
972                 if (!bh) {
973                         CERROR("%s: can't read %u@%llu on ino %lu: rc = %d\n",
974                                LDISKFS_SB(inode->i_sb)->s_es->s_volume_name,
975                                csize, *offs, inode->i_ino, err);
976                         return err;
977                 }
978
979                 memcpy(buf, bh->b_data + boffs, csize);
980                 brelse(bh);
981
982                 *offs += csize;
983                 buf += csize;
984                 size -= csize;
985         }
986         return osize;
987 }
988
989 static ssize_t osd_read(const struct lu_env *env, struct dt_object *dt,
990                         struct lu_buf *buf, loff_t *pos,
991                         struct lustre_capa *capa)
992 {
993         struct inode *inode = osd_dt_obj(dt)->oo_inode;
994         int           rc;
995
996         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_READ))
997                 RETURN(-EACCES);
998
999         /* Read small symlink from inode body as we need to maintain correct
1000          * on-disk symlinks for ldiskfs.
1001          */
1002         if (S_ISLNK(dt->do_lu.lo_header->loh_attr) &&
1003             (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
1004                 rc = osd_ldiskfs_readlink(inode, buf->lb_buf, buf->lb_len);
1005         else
1006                 rc = osd_ldiskfs_read(inode, buf->lb_buf, buf->lb_len, pos);
1007
1008         return rc;
1009 }
1010
1011 static ssize_t osd_declare_write(const struct lu_env *env, struct dt_object *dt,
1012                                  const loff_t size, loff_t pos,
1013                                  struct thandle *handle)
1014 {
1015         struct osd_thandle *oh;
1016         int                 credits;
1017         struct inode       *inode;
1018         int                 rc;
1019         ENTRY;
1020
1021         LASSERT(handle != NULL);
1022
1023         oh = container_of0(handle, struct osd_thandle, ot_super);
1024         LASSERT(oh->ot_handle == NULL);
1025
1026         credits = osd_dto_credits_noquota[DTO_WRITE_BLOCK];
1027
1028         osd_trans_declare_op(env, oh, OSD_OT_WRITE, credits);
1029
1030         inode = osd_dt_obj(dt)->oo_inode;
1031
1032         /* we may declare write to non-exist llog */
1033         if (inode == NULL)
1034                 RETURN(0);
1035
1036         /* dt_declare_write() is usually called for system objects, such
1037          * as llog or last_rcvd files. We needn't enforce quota on those
1038          * objects, so always set the lqi_space as 0. */
1039         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
1040                                    true, true, NULL, false);
1041         RETURN(rc);
1042 }
1043
1044 static int osd_ldiskfs_writelink(struct inode *inode, char *buffer, int buflen)
1045 {
1046         /* LU-2634: clear the extent format for fast symlink */
1047         ldiskfs_clear_inode_flag(inode, LDISKFS_INODE_EXTENTS);
1048
1049         memcpy((char *)&LDISKFS_I(inode)->i_data, (char *)buffer, buflen);
1050         LDISKFS_I(inode)->i_disksize = buflen;
1051         i_size_write(inode, buflen);
1052         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1053
1054         return 0;
1055 }
1056
1057 int osd_ldiskfs_write_record(struct inode *inode, void *buf, int bufsize,
1058                              int write_NUL, loff_t *offs, handle_t *handle)
1059 {
1060         struct buffer_head *bh        = NULL;
1061         loff_t              offset    = *offs;
1062         loff_t              new_size  = i_size_read(inode);
1063         unsigned long       block;
1064         int                 blocksize = 1 << inode->i_blkbits;
1065         int                 err = 0;
1066         int                 size;
1067         int                 boffs;
1068         int                 dirty_inode = 0;
1069
1070         if (write_NUL) {
1071                 /*
1072                  * long symlink write does not count the NUL terminator in
1073                  * bufsize, we write it, and the inode's file size does not
1074                  * count the NUL terminator as well.
1075                  */
1076                 ((char *)buf)[bufsize] = '\0';
1077                 ++bufsize;
1078         }
1079         while (bufsize > 0) {
1080                 if (bh != NULL)
1081                         brelse(bh);
1082
1083                 block = offset >> inode->i_blkbits;
1084                 boffs = offset & (blocksize - 1);
1085                 size = min(blocksize - boffs, bufsize);
1086                 bh = ldiskfs_bread(handle, inode, block, 1, &err);
1087                 if (!bh) {
1088                         CERROR("%s: error reading offset %llu (block %lu): "
1089                                "rc = %d\n",
1090                                inode->i_sb->s_id, offset, block, err);
1091                         break;
1092                 }
1093
1094                 err = ldiskfs_journal_get_write_access(handle, bh);
1095                 if (err) {
1096                         CERROR("journal_get_write_access() returned error %d\n",
1097                                err);
1098                         break;
1099                 }
1100                 LASSERTF(boffs + size <= bh->b_size,
1101                          "boffs %d size %d bh->b_size %lu",
1102                          boffs, size, (unsigned long)bh->b_size);
1103                 memcpy(bh->b_data + boffs, buf, size);
1104                 err = ldiskfs_journal_dirty_metadata(handle, bh);
1105                 if (err)
1106                         break;
1107
1108                 if (offset + size > new_size)
1109                         new_size = offset + size;
1110                 offset += size;
1111                 bufsize -= size;
1112                 buf += size;
1113         }
1114         if (bh)
1115                 brelse(bh);
1116
1117         if (write_NUL)
1118                 --new_size;
1119         /* correct in-core and on-disk sizes */
1120         if (new_size > i_size_read(inode)) {
1121                 spin_lock(&inode->i_lock);
1122                 if (new_size > i_size_read(inode))
1123                         i_size_write(inode, new_size);
1124                 if (i_size_read(inode) > LDISKFS_I(inode)->i_disksize) {
1125                         LDISKFS_I(inode)->i_disksize = i_size_read(inode);
1126                         dirty_inode = 1;
1127                 }
1128                 spin_unlock(&inode->i_lock);
1129                 if (dirty_inode)
1130                         ll_dirty_inode(inode, I_DIRTY_DATASYNC);
1131         }
1132
1133         if (err == 0)
1134                 *offs = offset;
1135         return err;
1136 }
1137
1138 static ssize_t osd_write(const struct lu_env *env, struct dt_object *dt,
1139                          const struct lu_buf *buf, loff_t *pos,
1140                          struct thandle *handle, struct lustre_capa *capa,
1141                          int ignore_quota)
1142 {
1143         struct inode            *inode = osd_dt_obj(dt)->oo_inode;
1144         struct osd_thandle      *oh;
1145         ssize_t                 result;
1146         int                     is_link;
1147
1148         LASSERT(dt_object_exists(dt));
1149
1150         if (osd_object_auth(env, dt, capa, CAPA_OPC_BODY_WRITE))
1151                 return -EACCES;
1152
1153         LASSERT(handle != NULL);
1154         LASSERT(inode != NULL);
1155         ll_vfs_dq_init(inode);
1156
1157         /* XXX: don't check: one declared chunk can be used many times */
1158         /* osd_trans_exec_op(env, handle, OSD_OT_WRITE); */
1159
1160         oh = container_of(handle, struct osd_thandle, ot_super);
1161         LASSERT(oh->ot_handle->h_transaction != NULL);
1162         /* Write small symlink to inode body as we need to maintain correct
1163          * on-disk symlinks for ldiskfs.
1164          * Note: the buf->lb_buf contains a NUL terminator while buf->lb_len
1165          * does not count it in.
1166          */
1167         is_link = S_ISLNK(dt->do_lu.lo_header->loh_attr);
1168         if (is_link && (buf->lb_len < sizeof(LDISKFS_I(inode)->i_data)))
1169                 result = osd_ldiskfs_writelink(inode, buf->lb_buf, buf->lb_len);
1170         else
1171                 result = osd_ldiskfs_write_record(inode, buf->lb_buf,
1172                                                   buf->lb_len, is_link, pos,
1173                                                   oh->ot_handle);
1174         if (result == 0)
1175                 result = buf->lb_len;
1176         return result;
1177 }
1178
1179 static int osd_declare_punch(const struct lu_env *env, struct dt_object *dt,
1180                              __u64 start, __u64 end, struct thandle *th)
1181 {
1182         struct osd_thandle *oh;
1183         struct inode       *inode;
1184         int                 rc;
1185         ENTRY;
1186
1187         LASSERT(th);
1188         oh = container_of(th, struct osd_thandle, ot_super);
1189
1190         /*
1191          * we don't need to reserve credits for whole truncate
1192          * it's not possible as truncate may need to free too many
1193          * blocks and that won't fit a single transaction. instead
1194          * we reserve credits to change i_size and put inode onto
1195          * orphan list. if needed truncate will extend or restart
1196          * transaction
1197          */
1198         osd_trans_declare_op(env, oh, OSD_OT_PUNCH,
1199                              osd_dto_credits_noquota[DTO_ATTR_SET_BASE] + 3);
1200
1201         inode = osd_dt_obj(dt)->oo_inode;
1202         LASSERT(inode);
1203
1204         rc = osd_declare_inode_qid(env, inode->i_uid, inode->i_gid, 0, oh,
1205                                    true, true, NULL, false);
1206         RETURN(rc);
1207 }
1208
1209 static int osd_punch(const struct lu_env *env, struct dt_object *dt,
1210                      __u64 start, __u64 end, struct thandle *th,
1211                      struct lustre_capa *capa)
1212 {
1213         struct osd_thandle *oh;
1214         struct osd_object  *obj = osd_dt_obj(dt);
1215         struct inode       *inode = obj->oo_inode;
1216         handle_t           *h;
1217         tid_t               tid;
1218         loff_t             oldsize;
1219         int                rc = 0, rc2 = 0;
1220         ENTRY;
1221
1222         LASSERT(end == OBD_OBJECT_EOF);
1223         LASSERT(dt_object_exists(dt));
1224         LASSERT(osd_invariant(obj));
1225         LASSERT(inode != NULL);
1226         ll_vfs_dq_init(inode);
1227
1228         LASSERT(th);
1229         oh = container_of(th, struct osd_thandle, ot_super);
1230         LASSERT(oh->ot_handle->h_transaction != NULL);
1231
1232         osd_trans_exec_op(env, th, OSD_OT_PUNCH);
1233
1234         tid = oh->ot_handle->h_transaction->t_tid;
1235
1236         oldsize=inode->i_size;
1237         i_size_write(inode, start);
1238         truncate_pagecache(inode, oldsize, start);
1239         if (inode->i_op->truncate)
1240                 inode->i_op->truncate(inode);
1241
1242         /*
1243          * For a partial-page truncate, flush the page to disk immediately to
1244          * avoid data corruption during direct disk write.  b=17397
1245          */
1246         if ((start & ~CFS_PAGE_MASK) != 0)
1247                 rc = filemap_fdatawrite_range(inode->i_mapping, start, start+1);
1248
1249         h = journal_current_handle();
1250         LASSERT(h != NULL);
1251         LASSERT(h == oh->ot_handle);
1252
1253         if (tid != h->h_transaction->t_tid) {
1254                 int credits = oh->ot_credits;
1255                 /*
1256                  * transaction has changed during truncate
1257                  * we need to restart the handle with our credits
1258                  */
1259                 if (h->h_buffer_credits < credits) {
1260                         if (ldiskfs_journal_extend(h, credits))
1261                                 rc2 = ldiskfs_journal_restart(h, credits);
1262                 }
1263         }
1264
1265         RETURN(rc == 0 ? rc2 : rc);
1266 }
1267
1268 static int osd_fiemap_get(const struct lu_env *env, struct dt_object *dt,
1269                           struct ll_user_fiemap *fm)
1270 {
1271         struct inode *inode = osd_dt_obj(dt)->oo_inode;
1272         struct osd_thread_info *info   = osd_oti_get(env);
1273         struct dentry          *dentry = &info->oti_obj_dentry;
1274         struct file            *file   = &info->oti_file;
1275         mm_segment_t            saved_fs;
1276         int rc;
1277
1278         LASSERT(inode);
1279         dentry->d_inode = inode;
1280         dentry->d_sb = inode->i_sb;
1281         file->f_dentry = dentry;
1282         file->f_mapping = inode->i_mapping;
1283         file->f_op = inode->i_fop;
1284
1285         saved_fs = get_fs();
1286         set_fs(get_ds());
1287         /* ldiskfs_ioctl does not have a inode argument */
1288         if (inode->i_fop->unlocked_ioctl)
1289                 rc = inode->i_fop->unlocked_ioctl(file, FSFILT_IOC_FIEMAP,
1290                                                   (long)fm);
1291         else
1292                 rc = -ENOTTY;
1293         set_fs(saved_fs);
1294         return rc;
1295 }
1296
1297 /*
1298  * in some cases we may need declare methods for objects being created
1299  * e.g., when we create symlink
1300  */
1301 const struct dt_body_operations osd_body_ops_new = {
1302         .dbo_declare_write = osd_declare_write,
1303 };
1304
1305 const struct dt_body_operations osd_body_ops = {
1306         .dbo_read                 = osd_read,
1307         .dbo_declare_write        = osd_declare_write,
1308         .dbo_write                = osd_write,
1309         .dbo_bufs_get             = osd_bufs_get,
1310         .dbo_bufs_put             = osd_bufs_put,
1311         .dbo_write_prep           = osd_write_prep,
1312         .dbo_declare_write_commit = osd_declare_write_commit,
1313         .dbo_write_commit         = osd_write_commit,
1314         .dbo_read_prep            = osd_read_prep,
1315         .dbo_declare_punch         = osd_declare_punch,
1316         .dbo_punch                 = osd_punch,
1317         .dbo_fiemap_get           = osd_fiemap_get,
1318 };
1319