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