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