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