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