]> bbs.cooldavid.org Git - net-next-2.6.git/blob - fs/exofs/inode.c
exofs: Move all operations to an io_engine
[net-next-2.6.git] / fs / exofs / inode.c
1 /*
2  * Copyright (C) 2005, 2006
3  * Avishay Traeger (avishay@gmail.com)
4  * Copyright (C) 2008, 2009
5  * Boaz Harrosh <bharrosh@panasas.com>
6  *
7  * Copyrights for code taken from ext2:
8  *     Copyright (C) 1992, 1993, 1994, 1995
9  *     Remy Card (card@masi.ibp.fr)
10  *     Laboratoire MASI - Institut Blaise Pascal
11  *     Universite Pierre et Marie Curie (Paris VI)
12  *     from
13  *     linux/fs/minix/inode.c
14  *     Copyright (C) 1991, 1992  Linus Torvalds
15  *
16  * This file is part of exofs.
17  *
18  * exofs is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation.  Since it is based on ext2, and the only
21  * valid version of GPL for the Linux kernel is version 2, the only valid
22  * version of GPL for exofs is version 2.
23  *
24  * exofs is distributed in the hope that it will be useful,
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  * GNU General Public License for more details.
28  *
29  * You should have received a copy of the GNU General Public License
30  * along with exofs; if not, write to the Free Software
31  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
32  */
33
34 #include <linux/writeback.h>
35 #include <linux/buffer_head.h>
36 #include <scsi/scsi_device.h>
37
38 #include "exofs.h"
39
40 #define EXOFS_DBGMSG2(M...) do {} while (0)
41
42 enum { BIO_MAX_PAGES_KMALLOC =
43                 (PAGE_SIZE - sizeof(struct bio)) / sizeof(struct bio_vec),
44 };
45
46 struct page_collect {
47         struct exofs_sb_info *sbi;
48         struct request_queue *req_q;
49         struct inode *inode;
50         unsigned expected_pages;
51         struct exofs_io_state *ios;
52
53         struct bio *bio;
54         unsigned nr_pages;
55         unsigned long length;
56         loff_t pg_first; /* keep 64bit also in 32-arches */
57 };
58
59 static void _pcol_init(struct page_collect *pcol, unsigned expected_pages,
60                        struct inode *inode)
61 {
62         struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
63
64         pcol->sbi = sbi;
65         pcol->req_q = osd_request_queue(sbi->s_dev);
66         pcol->inode = inode;
67         pcol->expected_pages = expected_pages;
68
69         pcol->ios = NULL;
70         pcol->bio = NULL;
71         pcol->nr_pages = 0;
72         pcol->length = 0;
73         pcol->pg_first = -1;
74 }
75
76 static void _pcol_reset(struct page_collect *pcol)
77 {
78         pcol->expected_pages -= min(pcol->nr_pages, pcol->expected_pages);
79
80         pcol->bio = NULL;
81         pcol->nr_pages = 0;
82         pcol->length = 0;
83         pcol->pg_first = -1;
84         pcol->ios = NULL;
85
86         /* this is probably the end of the loop but in writes
87          * it might not end here. don't be left with nothing
88          */
89         if (!pcol->expected_pages)
90                 pcol->expected_pages = BIO_MAX_PAGES_KMALLOC;
91 }
92
93 static int pcol_try_alloc(struct page_collect *pcol)
94 {
95         int pages = min_t(unsigned, pcol->expected_pages,
96                           BIO_MAX_PAGES_KMALLOC);
97
98         if (!pcol->ios) { /* First time allocate io_state */
99                 int ret = exofs_get_io_state(pcol->sbi, &pcol->ios);
100
101                 if (ret)
102                         return ret;
103         }
104
105         for (; pages; pages >>= 1) {
106                 pcol->bio = bio_kmalloc(GFP_KERNEL, pages);
107                 if (likely(pcol->bio))
108                         return 0;
109         }
110
111         EXOFS_ERR("Failed to bio_kmalloc expected_pages=%u\n",
112                   pcol->expected_pages);
113         return -ENOMEM;
114 }
115
116 static void pcol_free(struct page_collect *pcol)
117 {
118         if (pcol->bio) {
119                 bio_put(pcol->bio);
120                 pcol->bio = NULL;
121         }
122
123         if (pcol->ios) {
124                 exofs_put_io_state(pcol->ios);
125                 pcol->ios = NULL;
126         }
127 }
128
129 static int pcol_add_page(struct page_collect *pcol, struct page *page,
130                          unsigned len)
131 {
132         int added_len = bio_add_pc_page(pcol->req_q, pcol->bio, page, len, 0);
133         if (unlikely(len != added_len))
134                 return -ENOMEM;
135
136         ++pcol->nr_pages;
137         pcol->length += len;
138         return 0;
139 }
140
141 static int update_read_page(struct page *page, int ret)
142 {
143         if (ret == 0) {
144                 /* Everything is OK */
145                 SetPageUptodate(page);
146                 if (PageError(page))
147                         ClearPageError(page);
148         } else if (ret == -EFAULT) {
149                 /* In this case we were trying to read something that wasn't on
150                  * disk yet - return a page full of zeroes.  This should be OK,
151                  * because the object should be empty (if there was a write
152                  * before this read, the read would be waiting with the page
153                  * locked */
154                 clear_highpage(page);
155
156                 SetPageUptodate(page);
157                 if (PageError(page))
158                         ClearPageError(page);
159                 ret = 0; /* recovered error */
160                 EXOFS_DBGMSG("recovered read error\n");
161         } else /* Error */
162                 SetPageError(page);
163
164         return ret;
165 }
166
167 static void update_write_page(struct page *page, int ret)
168 {
169         if (ret) {
170                 mapping_set_error(page->mapping, ret);
171                 SetPageError(page);
172         }
173         end_page_writeback(page);
174 }
175
176 /* Called at the end of reads, to optionally unlock pages and update their
177  * status.
178  */
179 static int __readpages_done(struct page_collect *pcol, bool do_unlock)
180 {
181         struct bio_vec *bvec;
182         int i;
183         u64 resid;
184         u64 good_bytes;
185         u64 length = 0;
186         int ret = exofs_check_io(pcol->ios, &resid);
187
188         if (likely(!ret))
189                 good_bytes = pcol->length;
190         else
191                 good_bytes = pcol->length - resid;
192
193         EXOFS_DBGMSG("readpages_done(0x%lx) good_bytes=0x%llx"
194                      " length=0x%lx nr_pages=%u\n",
195                      pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
196                      pcol->nr_pages);
197
198         __bio_for_each_segment(bvec, pcol->bio, i, 0) {
199                 struct page *page = bvec->bv_page;
200                 struct inode *inode = page->mapping->host;
201                 int page_stat;
202
203                 if (inode != pcol->inode)
204                         continue; /* osd might add more pages at end */
205
206                 if (likely(length < good_bytes))
207                         page_stat = 0;
208                 else
209                         page_stat = ret;
210
211                 EXOFS_DBGMSG2("    readpages_done(0x%lx, 0x%lx) %s\n",
212                           inode->i_ino, page->index,
213                           page_stat ? "bad_bytes" : "good_bytes");
214
215                 ret = update_read_page(page, page_stat);
216                 if (do_unlock)
217                         unlock_page(page);
218                 length += bvec->bv_len;
219         }
220
221         pcol_free(pcol);
222         EXOFS_DBGMSG("readpages_done END\n");
223         return ret;
224 }
225
226 /* callback of async reads */
227 static void readpages_done(struct exofs_io_state *ios, void *p)
228 {
229         struct page_collect *pcol = p;
230
231         __readpages_done(pcol, true);
232         atomic_dec(&pcol->sbi->s_curr_pending);
233         kfree(pcol);
234 }
235
236 static void _unlock_pcol_pages(struct page_collect *pcol, int ret, int rw)
237 {
238         struct bio_vec *bvec;
239         int i;
240
241         __bio_for_each_segment(bvec, pcol->bio, i, 0) {
242                 struct page *page = bvec->bv_page;
243
244                 if (rw == READ)
245                         update_read_page(page, ret);
246                 else
247                         update_write_page(page, ret);
248
249                 unlock_page(page);
250         }
251 }
252
253 static int read_exec(struct page_collect *pcol, bool is_sync)
254 {
255         struct exofs_i_info *oi = exofs_i(pcol->inode);
256         struct exofs_io_state *ios = pcol->ios;
257         struct page_collect *pcol_copy = NULL;
258         int ret;
259
260         if (!pcol->bio)
261                 return 0;
262
263         /* see comment in _readpage() about sync reads */
264         WARN_ON(is_sync && (pcol->nr_pages != 1));
265
266         ios->bio = pcol->bio;
267         ios->length = pcol->length;
268         ios->offset = pcol->pg_first << PAGE_CACHE_SHIFT;
269
270         if (is_sync) {
271                 exofs_oi_read(oi, pcol->ios);
272                 return __readpages_done(pcol, false);
273         }
274
275         pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
276         if (!pcol_copy) {
277                 ret = -ENOMEM;
278                 goto err;
279         }
280
281         *pcol_copy = *pcol;
282         ios->done = readpages_done;
283         ios->private = pcol_copy;
284         ret = exofs_oi_read(oi, ios);
285         if (unlikely(ret))
286                 goto err;
287
288         atomic_inc(&pcol->sbi->s_curr_pending);
289
290         EXOFS_DBGMSG("read_exec obj=0x%llx start=0x%llx length=0x%lx\n",
291                   ios->obj.id, _LLU(ios->offset), pcol->length);
292
293         /* pages ownership was passed to pcol_copy */
294         _pcol_reset(pcol);
295         return 0;
296
297 err:
298         if (!is_sync)
299                 _unlock_pcol_pages(pcol, ret, READ);
300
301         pcol_free(pcol);
302
303         kfree(pcol_copy);
304         return ret;
305 }
306
307 /* readpage_strip is called either directly from readpage() or by the VFS from
308  * within read_cache_pages(), to add one more page to be read. It will try to
309  * collect as many contiguous pages as posible. If a discontinuity is
310  * encountered, or it runs out of resources, it will submit the previous segment
311  * and will start a new collection. Eventually caller must submit the last
312  * segment if present.
313  */
314 static int readpage_strip(void *data, struct page *page)
315 {
316         struct page_collect *pcol = data;
317         struct inode *inode = pcol->inode;
318         struct exofs_i_info *oi = exofs_i(inode);
319         loff_t i_size = i_size_read(inode);
320         pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
321         size_t len;
322         int ret;
323
324         /* FIXME: Just for debugging, will be removed */
325         if (PageUptodate(page))
326                 EXOFS_ERR("PageUptodate(0x%lx, 0x%lx)\n", pcol->inode->i_ino,
327                           page->index);
328
329         if (page->index < end_index)
330                 len = PAGE_CACHE_SIZE;
331         else if (page->index == end_index)
332                 len = i_size & ~PAGE_CACHE_MASK;
333         else
334                 len = 0;
335
336         if (!len || !obj_created(oi)) {
337                 /* this will be out of bounds, or doesn't exist yet.
338                  * Current page is cleared and the request is split
339                  */
340                 clear_highpage(page);
341
342                 SetPageUptodate(page);
343                 if (PageError(page))
344                         ClearPageError(page);
345
346                 unlock_page(page);
347                 EXOFS_DBGMSG("readpage_strip(0x%lx, 0x%lx) empty page,"
348                              " splitting\n", inode->i_ino, page->index);
349
350                 return read_exec(pcol, false);
351         }
352
353 try_again:
354
355         if (unlikely(pcol->pg_first == -1)) {
356                 pcol->pg_first = page->index;
357         } else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
358                    page->index)) {
359                 /* Discontinuity detected, split the request */
360                 ret = read_exec(pcol, false);
361                 if (unlikely(ret))
362                         goto fail;
363                 goto try_again;
364         }
365
366         if (!pcol->bio) {
367                 ret = pcol_try_alloc(pcol);
368                 if (unlikely(ret))
369                         goto fail;
370         }
371
372         if (len != PAGE_CACHE_SIZE)
373                 zero_user(page, len, PAGE_CACHE_SIZE - len);
374
375         EXOFS_DBGMSG2("    readpage_strip(0x%lx, 0x%lx) len=0x%zx\n",
376                      inode->i_ino, page->index, len);
377
378         ret = pcol_add_page(pcol, page, len);
379         if (ret) {
380                 EXOFS_DBGMSG2("Failed pcol_add_page pages[i]=%p "
381                           "this_len=0x%zx nr_pages=%u length=0x%lx\n",
382                           page, len, pcol->nr_pages, pcol->length);
383
384                 /* split the request, and start again with current page */
385                 ret = read_exec(pcol, false);
386                 if (unlikely(ret))
387                         goto fail;
388
389                 goto try_again;
390         }
391
392         return 0;
393
394 fail:
395         /* SetPageError(page); ??? */
396         unlock_page(page);
397         return ret;
398 }
399
400 static int exofs_readpages(struct file *file, struct address_space *mapping,
401                            struct list_head *pages, unsigned nr_pages)
402 {
403         struct page_collect pcol;
404         int ret;
405
406         _pcol_init(&pcol, nr_pages, mapping->host);
407
408         ret = read_cache_pages(mapping, pages, readpage_strip, &pcol);
409         if (ret) {
410                 EXOFS_ERR("read_cache_pages => %d\n", ret);
411                 return ret;
412         }
413
414         return read_exec(&pcol, false);
415 }
416
417 static int _readpage(struct page *page, bool is_sync)
418 {
419         struct page_collect pcol;
420         int ret;
421
422         _pcol_init(&pcol, 1, page->mapping->host);
423
424         /* readpage_strip might call read_exec(,is_sync==false) at several
425          * places but not if we have a single page.
426          */
427         ret = readpage_strip(&pcol, page);
428         if (ret) {
429                 EXOFS_ERR("_readpage => %d\n", ret);
430                 return ret;
431         }
432
433         return read_exec(&pcol, is_sync);
434 }
435
436 /*
437  * We don't need the file
438  */
439 static int exofs_readpage(struct file *file, struct page *page)
440 {
441         return _readpage(page, false);
442 }
443
444 /* Callback for osd_write. All writes are asynchronous */
445 static void writepages_done(struct exofs_io_state *ios, void *p)
446 {
447         struct page_collect *pcol = p;
448         struct bio_vec *bvec;
449         int i;
450         u64 resid;
451         u64  good_bytes;
452         u64  length = 0;
453         int ret = exofs_check_io(ios, &resid);
454
455         atomic_dec(&pcol->sbi->s_curr_pending);
456
457         if (likely(!ret))
458                 good_bytes = pcol->length;
459         else
460                 good_bytes = pcol->length - resid;
461
462         EXOFS_DBGMSG("writepages_done(0x%lx) good_bytes=0x%llx"
463                      " length=0x%lx nr_pages=%u\n",
464                      pcol->inode->i_ino, _LLU(good_bytes), pcol->length,
465                      pcol->nr_pages);
466
467         __bio_for_each_segment(bvec, pcol->bio, i, 0) {
468                 struct page *page = bvec->bv_page;
469                 struct inode *inode = page->mapping->host;
470                 int page_stat;
471
472                 if (inode != pcol->inode)
473                         continue; /* osd might add more pages to a bio */
474
475                 if (likely(length < good_bytes))
476                         page_stat = 0;
477                 else
478                         page_stat = ret;
479
480                 update_write_page(page, page_stat);
481                 unlock_page(page);
482                 EXOFS_DBGMSG2("    writepages_done(0x%lx, 0x%lx) status=%d\n",
483                              inode->i_ino, page->index, page_stat);
484
485                 length += bvec->bv_len;
486         }
487
488         pcol_free(pcol);
489         kfree(pcol);
490         EXOFS_DBGMSG("writepages_done END\n");
491 }
492
493 static int write_exec(struct page_collect *pcol)
494 {
495         struct exofs_i_info *oi = exofs_i(pcol->inode);
496         struct exofs_io_state *ios = pcol->ios;
497         struct page_collect *pcol_copy = NULL;
498         int ret;
499
500         if (!pcol->bio)
501                 return 0;
502
503         pcol_copy = kmalloc(sizeof(*pcol_copy), GFP_KERNEL);
504         if (!pcol_copy) {
505                 EXOFS_ERR("write_exec: Faild to kmalloc(pcol)\n");
506                 ret = -ENOMEM;
507                 goto err;
508         }
509
510         *pcol_copy = *pcol;
511
512         pcol_copy->bio->bi_rw |= (1 << BIO_RW); /* FIXME: bio_set_dir() */
513
514         ios->bio = pcol_copy->bio;
515         ios->offset = pcol_copy->pg_first << PAGE_CACHE_SHIFT;
516         ios->length = pcol_copy->length;
517         ios->done = writepages_done;
518         ios->private = pcol_copy;
519
520         ret = exofs_oi_write(oi, ios);
521         if (unlikely(ret)) {
522                 EXOFS_ERR("write_exec: exofs_oi_write() Faild\n");
523                 goto err;
524         }
525
526         atomic_inc(&pcol->sbi->s_curr_pending);
527         EXOFS_DBGMSG("write_exec(0x%lx, 0x%llx) start=0x%llx length=0x%lx\n",
528                   pcol->inode->i_ino, pcol->pg_first, _LLU(ios->offset),
529                   pcol->length);
530         /* pages ownership was passed to pcol_copy */
531         _pcol_reset(pcol);
532         return 0;
533
534 err:
535         _unlock_pcol_pages(pcol, ret, WRITE);
536         pcol_free(pcol);
537         kfree(pcol_copy);
538
539         return ret;
540 }
541
542 /* writepage_strip is called either directly from writepage() or by the VFS from
543  * within write_cache_pages(), to add one more page to be written to storage.
544  * It will try to collect as many contiguous pages as possible. If a
545  * discontinuity is encountered or it runs out of resources it will submit the
546  * previous segment and will start a new collection.
547  * Eventually caller must submit the last segment if present.
548  */
549 static int writepage_strip(struct page *page,
550                            struct writeback_control *wbc_unused, void *data)
551 {
552         struct page_collect *pcol = data;
553         struct inode *inode = pcol->inode;
554         struct exofs_i_info *oi = exofs_i(inode);
555         loff_t i_size = i_size_read(inode);
556         pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
557         size_t len;
558         int ret;
559
560         BUG_ON(!PageLocked(page));
561
562         ret = wait_obj_created(oi);
563         if (unlikely(ret))
564                 goto fail;
565
566         if (page->index < end_index)
567                 /* in this case, the page is within the limits of the file */
568                 len = PAGE_CACHE_SIZE;
569         else {
570                 len = i_size & ~PAGE_CACHE_MASK;
571
572                 if (page->index > end_index || !len) {
573                         /* in this case, the page is outside the limits
574                          * (truncate in progress)
575                          */
576                         ret = write_exec(pcol);
577                         if (unlikely(ret))
578                                 goto fail;
579                         if (PageError(page))
580                                 ClearPageError(page);
581                         unlock_page(page);
582                         EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) "
583                                      "outside the limits\n",
584                                      inode->i_ino, page->index);
585                         return 0;
586                 }
587         }
588
589 try_again:
590
591         if (unlikely(pcol->pg_first == -1)) {
592                 pcol->pg_first = page->index;
593         } else if (unlikely((pcol->pg_first + pcol->nr_pages) !=
594                    page->index)) {
595                 /* Discontinuity detected, split the request */
596                 ret = write_exec(pcol);
597                 if (unlikely(ret))
598                         goto fail;
599
600                 EXOFS_DBGMSG("writepage_strip(0x%lx, 0x%lx) Discontinuity\n",
601                              inode->i_ino, page->index);
602                 goto try_again;
603         }
604
605         if (!pcol->bio) {
606                 ret = pcol_try_alloc(pcol);
607                 if (unlikely(ret))
608                         goto fail;
609         }
610
611         EXOFS_DBGMSG2("    writepage_strip(0x%lx, 0x%lx) len=0x%zx\n",
612                      inode->i_ino, page->index, len);
613
614         ret = pcol_add_page(pcol, page, len);
615         if (unlikely(ret)) {
616                 EXOFS_DBGMSG("Failed pcol_add_page "
617                              "nr_pages=%u total_length=0x%lx\n",
618                              pcol->nr_pages, pcol->length);
619
620                 /* split the request, next loop will start again */
621                 ret = write_exec(pcol);
622                 if (unlikely(ret)) {
623                         EXOFS_DBGMSG("write_exec faild => %d", ret);
624                         goto fail;
625                 }
626
627                 goto try_again;
628         }
629
630         BUG_ON(PageWriteback(page));
631         set_page_writeback(page);
632
633         return 0;
634
635 fail:
636         EXOFS_DBGMSG("Error: writepage_strip(0x%lx, 0x%lx)=>%d\n",
637                      inode->i_ino, page->index, ret);
638         set_bit(AS_EIO, &page->mapping->flags);
639         unlock_page(page);
640         return ret;
641 }
642
643 static int exofs_writepages(struct address_space *mapping,
644                        struct writeback_control *wbc)
645 {
646         struct page_collect pcol;
647         long start, end, expected_pages;
648         int ret;
649
650         start = wbc->range_start >> PAGE_CACHE_SHIFT;
651         end = (wbc->range_end == LLONG_MAX) ?
652                         start + mapping->nrpages :
653                         wbc->range_end >> PAGE_CACHE_SHIFT;
654
655         if (start || end)
656                 expected_pages = end - start + 1;
657         else
658                 expected_pages = mapping->nrpages;
659
660         if (expected_pages < 32L)
661                 expected_pages = 32L;
662
663         EXOFS_DBGMSG("inode(0x%lx) wbc->start=0x%llx wbc->end=0x%llx "
664                      "nrpages=%lu start=0x%lx end=0x%lx expected_pages=%ld\n",
665                      mapping->host->i_ino, wbc->range_start, wbc->range_end,
666                      mapping->nrpages, start, end, expected_pages);
667
668         _pcol_init(&pcol, expected_pages, mapping->host);
669
670         ret = write_cache_pages(mapping, wbc, writepage_strip, &pcol);
671         if (ret) {
672                 EXOFS_ERR("write_cache_pages => %d\n", ret);
673                 return ret;
674         }
675
676         return write_exec(&pcol);
677 }
678
679 static int exofs_writepage(struct page *page, struct writeback_control *wbc)
680 {
681         struct page_collect pcol;
682         int ret;
683
684         _pcol_init(&pcol, 1, page->mapping->host);
685
686         ret = writepage_strip(page, NULL, &pcol);
687         if (ret) {
688                 EXOFS_ERR("exofs_writepage => %d\n", ret);
689                 return ret;
690         }
691
692         return write_exec(&pcol);
693 }
694
695 int exofs_write_begin(struct file *file, struct address_space *mapping,
696                 loff_t pos, unsigned len, unsigned flags,
697                 struct page **pagep, void **fsdata)
698 {
699         int ret = 0;
700         struct page *page;
701
702         page = *pagep;
703         if (page == NULL) {
704                 ret = simple_write_begin(file, mapping, pos, len, flags, pagep,
705                                          fsdata);
706                 if (ret) {
707                         EXOFS_DBGMSG("simple_write_begin faild\n");
708                         return ret;
709                 }
710
711                 page = *pagep;
712         }
713
714          /* read modify write */
715         if (!PageUptodate(page) && (len != PAGE_CACHE_SIZE)) {
716                 ret = _readpage(page, true);
717                 if (ret) {
718                         /*SetPageError was done by _readpage. Is it ok?*/
719                         unlock_page(page);
720                         EXOFS_DBGMSG("__readpage_filler faild\n");
721                 }
722         }
723
724         return ret;
725 }
726
727 static int exofs_write_begin_export(struct file *file,
728                 struct address_space *mapping,
729                 loff_t pos, unsigned len, unsigned flags,
730                 struct page **pagep, void **fsdata)
731 {
732         *pagep = NULL;
733
734         return exofs_write_begin(file, mapping, pos, len, flags, pagep,
735                                         fsdata);
736 }
737
738 const struct address_space_operations exofs_aops = {
739         .readpage       = exofs_readpage,
740         .readpages      = exofs_readpages,
741         .writepage      = exofs_writepage,
742         .writepages     = exofs_writepages,
743         .write_begin    = exofs_write_begin_export,
744         .write_end      = simple_write_end,
745 };
746
747 /******************************************************************************
748  * INODE OPERATIONS
749  *****************************************************************************/
750
751 /*
752  * Test whether an inode is a fast symlink.
753  */
754 static inline int exofs_inode_is_fast_symlink(struct inode *inode)
755 {
756         struct exofs_i_info *oi = exofs_i(inode);
757
758         return S_ISLNK(inode->i_mode) && (oi->i_data[0] != 0);
759 }
760
761 /*
762  * get_block_t - Fill in a buffer_head
763  * An OSD takes care of block allocation so we just fake an allocation by
764  * putting in the inode's sector_t in the buffer_head.
765  * TODO: What about the case of create==0 and @iblock does not exist in the
766  * object?
767  */
768 static int exofs_get_block(struct inode *inode, sector_t iblock,
769                     struct buffer_head *bh_result, int create)
770 {
771         map_bh(bh_result, inode->i_sb, iblock);
772         return 0;
773 }
774
775 const struct osd_attr g_attr_logical_length = ATTR_DEF(
776         OSD_APAGE_OBJECT_INFORMATION, OSD_ATTR_OI_LOGICAL_LENGTH, 8);
777
778 static int _do_truncate(struct inode *inode)
779 {
780         struct exofs_i_info *oi = exofs_i(inode);
781         loff_t isize = i_size_read(inode);
782         int ret;
783
784         inode->i_mtime = inode->i_ctime = CURRENT_TIME;
785
786         nobh_truncate_page(inode->i_mapping, isize, exofs_get_block);
787
788         ret = exofs_oi_truncate(oi, (u64)isize);
789         EXOFS_DBGMSG("(0x%lx) size=0x%llx\n", inode->i_ino, isize);
790         return ret;
791 }
792
793 /*
794  * Truncate a file to the specified size - all we have to do is set the size
795  * attribute.  We make sure the object exists first.
796  */
797 void exofs_truncate(struct inode *inode)
798 {
799         struct exofs_i_info *oi = exofs_i(inode);
800         int ret;
801
802         if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)
803              || S_ISLNK(inode->i_mode)))
804                 return;
805         if (exofs_inode_is_fast_symlink(inode))
806                 return;
807         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
808                 return;
809
810         /* if we are about to truncate an object, and it hasn't been
811          * created yet, wait
812          */
813         if (unlikely(wait_obj_created(oi)))
814                 goto fail;
815
816         ret = _do_truncate(inode);
817         if (ret)
818                 goto fail;
819
820 out:
821         mark_inode_dirty(inode);
822         return;
823 fail:
824         make_bad_inode(inode);
825         goto out;
826 }
827
828 /*
829  * Set inode attributes - just call generic functions.
830  */
831 int exofs_setattr(struct dentry *dentry, struct iattr *iattr)
832 {
833         struct inode *inode = dentry->d_inode;
834         int error;
835
836         error = inode_change_ok(inode, iattr);
837         if (error)
838                 return error;
839
840         error = inode_setattr(inode, iattr);
841         return error;
842 }
843
844 /*
845  * Read an inode from the OSD, and return it as is.  We also return the size
846  * attribute in the 'obj_size' argument.
847  */
848 static int exofs_get_inode(struct super_block *sb, struct exofs_i_info *oi,
849                     struct exofs_fcb *inode, uint64_t *obj_size)
850 {
851         struct exofs_sb_info *sbi = sb->s_fs_info;
852         struct osd_attr attrs[2];
853         struct exofs_io_state *ios;
854         int ret;
855
856         *obj_size = ~0;
857         ret = exofs_get_io_state(sbi, &ios);
858         if (unlikely(ret)) {
859                 EXOFS_ERR("%s: exofs_get_io_state failed.\n", __func__);
860                 return ret;
861         }
862
863         ios->obj.id = exofs_oi_objno(oi);
864         exofs_make_credential(oi->i_cred, &ios->obj);
865         ios->cred = oi->i_cred;
866
867         attrs[0] = g_attr_inode_data;
868         attrs[1] = g_attr_logical_length;
869         ios->in_attr = attrs;
870         ios->in_attr_len = ARRAY_SIZE(attrs);
871
872         ret = exofs_sbi_read(ios);
873         if (ret)
874                 goto out;
875
876         ret = extract_attr_from_ios(ios, &attrs[0]);
877         if (ret) {
878                 EXOFS_ERR("%s: extract_attr of inode_data failed\n", __func__);
879                 goto out;
880         }
881         WARN_ON(attrs[0].len != EXOFS_INO_ATTR_SIZE);
882         memcpy(inode, attrs[0].val_ptr, EXOFS_INO_ATTR_SIZE);
883
884         ret = extract_attr_from_ios(ios, &attrs[1]);
885         if (ret) {
886                 EXOFS_ERR("%s: extract_attr of logical_length failed\n",
887                           __func__);
888                 goto out;
889         }
890         *obj_size = get_unaligned_be64(attrs[1].val_ptr);
891
892 out:
893         exofs_put_io_state(ios);
894         return ret;
895 }
896
897 static void __oi_init(struct exofs_i_info *oi)
898 {
899         init_waitqueue_head(&oi->i_wq);
900         oi->i_flags = 0;
901 }
902 /*
903  * Fill in an inode read from the OSD and set it up for use
904  */
905 struct inode *exofs_iget(struct super_block *sb, unsigned long ino)
906 {
907         struct exofs_i_info *oi;
908         struct exofs_fcb fcb;
909         struct inode *inode;
910         uint64_t obj_size;
911         int ret;
912
913         inode = iget_locked(sb, ino);
914         if (!inode)
915                 return ERR_PTR(-ENOMEM);
916         if (!(inode->i_state & I_NEW))
917                 return inode;
918         oi = exofs_i(inode);
919         __oi_init(oi);
920
921         /* read the inode from the osd */
922         ret = exofs_get_inode(sb, oi, &fcb, &obj_size);
923         if (ret)
924                 goto bad_inode;
925
926         set_obj_created(oi);
927
928         /* copy stuff from on-disk struct to in-memory struct */
929         inode->i_mode = le16_to_cpu(fcb.i_mode);
930         inode->i_uid = le32_to_cpu(fcb.i_uid);
931         inode->i_gid = le32_to_cpu(fcb.i_gid);
932         inode->i_nlink = le16_to_cpu(fcb.i_links_count);
933         inode->i_ctime.tv_sec = (signed)le32_to_cpu(fcb.i_ctime);
934         inode->i_atime.tv_sec = (signed)le32_to_cpu(fcb.i_atime);
935         inode->i_mtime.tv_sec = (signed)le32_to_cpu(fcb.i_mtime);
936         inode->i_ctime.tv_nsec =
937                 inode->i_atime.tv_nsec = inode->i_mtime.tv_nsec = 0;
938         oi->i_commit_size = le64_to_cpu(fcb.i_size);
939         i_size_write(inode, oi->i_commit_size);
940         inode->i_blkbits = EXOFS_BLKSHIFT;
941         inode->i_generation = le32_to_cpu(fcb.i_generation);
942
943         if ((inode->i_size != obj_size) &&
944                 (!exofs_inode_is_fast_symlink(inode))) {
945                 EXOFS_ERR("WARNING: Size of inode=%llu != object=%llu\n",
946                           inode->i_size, _LLU(obj_size));
947                 /* FIXME: call exofs_inode_recovery() */
948         }
949
950         oi->i_dir_start_lookup = 0;
951
952         if ((inode->i_nlink == 0) && (inode->i_mode == 0)) {
953                 ret = -ESTALE;
954                 goto bad_inode;
955         }
956
957         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
958                 if (fcb.i_data[0])
959                         inode->i_rdev =
960                                 old_decode_dev(le32_to_cpu(fcb.i_data[0]));
961                 else
962                         inode->i_rdev =
963                                 new_decode_dev(le32_to_cpu(fcb.i_data[1]));
964         } else {
965                 memcpy(oi->i_data, fcb.i_data, sizeof(fcb.i_data));
966         }
967
968         if (S_ISREG(inode->i_mode)) {
969                 inode->i_op = &exofs_file_inode_operations;
970                 inode->i_fop = &exofs_file_operations;
971                 inode->i_mapping->a_ops = &exofs_aops;
972         } else if (S_ISDIR(inode->i_mode)) {
973                 inode->i_op = &exofs_dir_inode_operations;
974                 inode->i_fop = &exofs_dir_operations;
975                 inode->i_mapping->a_ops = &exofs_aops;
976         } else if (S_ISLNK(inode->i_mode)) {
977                 if (exofs_inode_is_fast_symlink(inode))
978                         inode->i_op = &exofs_fast_symlink_inode_operations;
979                 else {
980                         inode->i_op = &exofs_symlink_inode_operations;
981                         inode->i_mapping->a_ops = &exofs_aops;
982                 }
983         } else {
984                 inode->i_op = &exofs_special_inode_operations;
985                 if (fcb.i_data[0])
986                         init_special_inode(inode, inode->i_mode,
987                            old_decode_dev(le32_to_cpu(fcb.i_data[0])));
988                 else
989                         init_special_inode(inode, inode->i_mode,
990                            new_decode_dev(le32_to_cpu(fcb.i_data[1])));
991         }
992
993         unlock_new_inode(inode);
994         return inode;
995
996 bad_inode:
997         iget_failed(inode);
998         return ERR_PTR(ret);
999 }
1000
1001 int __exofs_wait_obj_created(struct exofs_i_info *oi)
1002 {
1003         if (!obj_created(oi)) {
1004                 BUG_ON(!obj_2bcreated(oi));
1005                 wait_event(oi->i_wq, obj_created(oi));
1006         }
1007         return unlikely(is_bad_inode(&oi->vfs_inode)) ? -EIO : 0;
1008 }
1009 /*
1010  * Callback function from exofs_new_inode().  The important thing is that we
1011  * set the obj_created flag so that other methods know that the object exists on
1012  * the OSD.
1013  */
1014 static void create_done(struct exofs_io_state *ios, void *p)
1015 {
1016         struct inode *inode = p;
1017         struct exofs_i_info *oi = exofs_i(inode);
1018         struct exofs_sb_info *sbi = inode->i_sb->s_fs_info;
1019         int ret;
1020
1021         ret = exofs_check_io(ios, NULL);
1022         exofs_put_io_state(ios);
1023
1024         atomic_dec(&sbi->s_curr_pending);
1025
1026         if (unlikely(ret)) {
1027                 EXOFS_ERR("object=0x%llx creation faild in pid=0x%llx",
1028                           _LLU(exofs_oi_objno(oi)), _LLU(sbi->s_pid));
1029                 /*TODO: When FS is corrupted creation can fail, object already
1030                  * exist. Get rid of this asynchronous creation, if exist
1031                  * increment the obj counter and try the next object. Until we
1032                  * succeed. All these dangling objects will be made into lost
1033                  * files by chkfs.exofs
1034                  */
1035         }
1036
1037         set_obj_created(oi);
1038
1039         atomic_dec(&inode->i_count);
1040         wake_up(&oi->i_wq);
1041 }
1042
1043 /*
1044  * Set up a new inode and create an object for it on the OSD
1045  */
1046 struct inode *exofs_new_inode(struct inode *dir, int mode)
1047 {
1048         struct super_block *sb;
1049         struct inode *inode;
1050         struct exofs_i_info *oi;
1051         struct exofs_sb_info *sbi;
1052         struct exofs_io_state *ios;
1053         int ret;
1054
1055         sb = dir->i_sb;
1056         inode = new_inode(sb);
1057         if (!inode)
1058                 return ERR_PTR(-ENOMEM);
1059
1060         oi = exofs_i(inode);
1061         __oi_init(oi);
1062
1063         set_obj_2bcreated(oi);
1064
1065         sbi = sb->s_fs_info;
1066
1067         sb->s_dirt = 1;
1068         inode->i_uid = current->cred->fsuid;
1069         if (dir->i_mode & S_ISGID) {
1070                 inode->i_gid = dir->i_gid;
1071                 if (S_ISDIR(mode))
1072                         mode |= S_ISGID;
1073         } else {
1074                 inode->i_gid = current->cred->fsgid;
1075         }
1076         inode->i_mode = mode;
1077
1078         inode->i_ino = sbi->s_nextid++;
1079         inode->i_blkbits = EXOFS_BLKSHIFT;
1080         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1081         oi->i_commit_size = inode->i_size = 0;
1082         spin_lock(&sbi->s_next_gen_lock);
1083         inode->i_generation = sbi->s_next_generation++;
1084         spin_unlock(&sbi->s_next_gen_lock);
1085         insert_inode_hash(inode);
1086
1087         mark_inode_dirty(inode);
1088
1089         ret = exofs_get_io_state(sbi, &ios);
1090         if (unlikely(ret)) {
1091                 EXOFS_ERR("exofs_new_inode: exofs_get_io_state failed\n");
1092                 return ERR_PTR(ret);
1093         }
1094
1095         ios->obj.id = exofs_oi_objno(oi);
1096         exofs_make_credential(oi->i_cred, &ios->obj);
1097
1098         /* increment the refcount so that the inode will still be around when we
1099          * reach the callback
1100          */
1101         atomic_inc(&inode->i_count);
1102
1103         ios->done = create_done;
1104         ios->private = inode;
1105         ios->cred = oi->i_cred;
1106         ret = exofs_sbi_create(ios);
1107         if (ret) {
1108                 atomic_dec(&inode->i_count);
1109                 exofs_put_io_state(ios);
1110                 return ERR_PTR(ret);
1111         }
1112         atomic_inc(&sbi->s_curr_pending);
1113
1114         return inode;
1115 }
1116
1117 /*
1118  * struct to pass two arguments to update_inode's callback
1119  */
1120 struct updatei_args {
1121         struct exofs_sb_info    *sbi;
1122         struct exofs_fcb        fcb;
1123 };
1124
1125 /*
1126  * Callback function from exofs_update_inode().
1127  */
1128 static void updatei_done(struct exofs_io_state *ios, void *p)
1129 {
1130         struct updatei_args *args = p;
1131
1132         exofs_put_io_state(ios);
1133
1134         atomic_dec(&args->sbi->s_curr_pending);
1135
1136         kfree(args);
1137 }
1138
1139 /*
1140  * Write the inode to the OSD.  Just fill up the struct, and set the attribute
1141  * synchronously or asynchronously depending on the do_sync flag.
1142  */
1143 static int exofs_update_inode(struct inode *inode, int do_sync)
1144 {
1145         struct exofs_i_info *oi = exofs_i(inode);
1146         struct super_block *sb = inode->i_sb;
1147         struct exofs_sb_info *sbi = sb->s_fs_info;
1148         struct exofs_io_state *ios;
1149         struct osd_attr attr;
1150         struct exofs_fcb *fcb;
1151         struct updatei_args *args;
1152         int ret;
1153
1154         args = kzalloc(sizeof(*args), GFP_KERNEL);
1155         if (!args)
1156                 return -ENOMEM;
1157
1158         fcb = &args->fcb;
1159
1160         fcb->i_mode = cpu_to_le16(inode->i_mode);
1161         fcb->i_uid = cpu_to_le32(inode->i_uid);
1162         fcb->i_gid = cpu_to_le32(inode->i_gid);
1163         fcb->i_links_count = cpu_to_le16(inode->i_nlink);
1164         fcb->i_ctime = cpu_to_le32(inode->i_ctime.tv_sec);
1165         fcb->i_atime = cpu_to_le32(inode->i_atime.tv_sec);
1166         fcb->i_mtime = cpu_to_le32(inode->i_mtime.tv_sec);
1167         oi->i_commit_size = i_size_read(inode);
1168         fcb->i_size = cpu_to_le64(oi->i_commit_size);
1169         fcb->i_generation = cpu_to_le32(inode->i_generation);
1170
1171         if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1172                 if (old_valid_dev(inode->i_rdev)) {
1173                         fcb->i_data[0] =
1174                                 cpu_to_le32(old_encode_dev(inode->i_rdev));
1175                         fcb->i_data[1] = 0;
1176                 } else {
1177                         fcb->i_data[0] = 0;
1178                         fcb->i_data[1] =
1179                                 cpu_to_le32(new_encode_dev(inode->i_rdev));
1180                         fcb->i_data[2] = 0;
1181                 }
1182         } else
1183                 memcpy(fcb->i_data, oi->i_data, sizeof(fcb->i_data));
1184
1185         ret = exofs_get_io_state(sbi, &ios);
1186         if (unlikely(ret)) {
1187                 EXOFS_ERR("%s: exofs_get_io_state failed.\n", __func__);
1188                 goto free_args;
1189         }
1190
1191         attr = g_attr_inode_data;
1192         attr.val_ptr = fcb;
1193         ios->out_attr_len = 1;
1194         ios->out_attr = &attr;
1195
1196         if (!obj_created(oi)) {
1197                 EXOFS_DBGMSG("!obj_created\n");
1198                 BUG_ON(!obj_2bcreated(oi));
1199                 wait_event(oi->i_wq, obj_created(oi));
1200                 EXOFS_DBGMSG("wait_event done\n");
1201         }
1202
1203         if (!do_sync) {
1204                 args->sbi = sbi;
1205                 ios->done = updatei_done;
1206                 ios->private = args;
1207         }
1208
1209         ret = exofs_oi_write(oi, ios);
1210         if (!do_sync && !ret) {
1211                 atomic_inc(&sbi->s_curr_pending);
1212                 goto out; /* deallocation in updatei_done */
1213         }
1214
1215         exofs_put_io_state(ios);
1216 free_args:
1217         kfree(args);
1218 out:
1219         EXOFS_DBGMSG("ret=>%d\n", ret);
1220         return ret;
1221 }
1222
1223 int exofs_write_inode(struct inode *inode, int wait)
1224 {
1225         return exofs_update_inode(inode, wait);
1226 }
1227
1228 /*
1229  * Callback function from exofs_delete_inode() - don't have much cleaning up to
1230  * do.
1231  */
1232 static void delete_done(struct exofs_io_state *ios, void *p)
1233 {
1234         struct exofs_sb_info *sbi = p;
1235
1236         exofs_put_io_state(ios);
1237
1238         atomic_dec(&sbi->s_curr_pending);
1239 }
1240
1241 /*
1242  * Called when the refcount of an inode reaches zero.  We remove the object
1243  * from the OSD here.  We make sure the object was created before we try and
1244  * delete it.
1245  */
1246 void exofs_delete_inode(struct inode *inode)
1247 {
1248         struct exofs_i_info *oi = exofs_i(inode);
1249         struct super_block *sb = inode->i_sb;
1250         struct exofs_sb_info *sbi = sb->s_fs_info;
1251         struct exofs_io_state *ios;
1252         int ret;
1253
1254         truncate_inode_pages(&inode->i_data, 0);
1255
1256         if (is_bad_inode(inode))
1257                 goto no_delete;
1258
1259         mark_inode_dirty(inode);
1260         exofs_update_inode(inode, inode_needs_sync(inode));
1261
1262         inode->i_size = 0;
1263         if (inode->i_blocks)
1264                 exofs_truncate(inode);
1265
1266         clear_inode(inode);
1267
1268         ret = exofs_get_io_state(sbi, &ios);
1269         if (unlikely(ret)) {
1270                 EXOFS_ERR("%s: exofs_get_io_state failed\n", __func__);
1271                 return;
1272         }
1273
1274         /* if we are deleting an obj that hasn't been created yet, wait */
1275         if (!obj_created(oi)) {
1276                 BUG_ON(!obj_2bcreated(oi));
1277                 wait_event(oi->i_wq, obj_created(oi));
1278         }
1279
1280         ios->obj.id = exofs_oi_objno(oi);
1281         ios->done = delete_done;
1282         ios->private = sbi;
1283         ios->cred = oi->i_cred;
1284         ret = exofs_sbi_remove(ios);
1285         if (ret) {
1286                 EXOFS_ERR("%s: exofs_sbi_remove failed\n", __func__);
1287                 exofs_put_io_state(ios);
1288                 return;
1289         }
1290         atomic_inc(&sbi->s_curr_pending);
1291
1292         return;
1293
1294 no_delete:
1295         clear_inode(inode);
1296 }