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