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