support make_ext4fs
[firefly-linux-kernel-4.4.55.git] / fs / block_dev.c
1 /*
2  *  linux/fs/block_dev.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *  Copyright (C) 2001  Andrea Arcangeli <andrea@suse.de> SuSE
6  */
7
8 #include <linux/init.h>
9 #include <linux/mm.h>
10 #include <linux/fcntl.h>
11 #include <linux/slab.h>
12 #include <linux/kmod.h>
13 #include <linux/major.h>
14 #include <linux/smp_lock.h>
15 #include <linux/device_cgroup.h>
16 #include <linux/highmem.h>
17 #include <linux/blkdev.h>
18 #include <linux/module.h>
19 #include <linux/blkpg.h>
20 #include <linux/buffer_head.h>
21 #include <linux/pagevec.h>
22 #include <linux/writeback.h>
23 #include <linux/mpage.h>
24 #include <linux/mount.h>
25 #include <linux/uio.h>
26 #include <linux/namei.h>
27 #include <linux/log2.h>
28 #include <linux/kmemleak.h>
29 #include <asm/uaccess.h>
30 #include "internal.h"
31 #include <linux/mtd/blktrans.h>
32 #include <linux/mtd/mtd.h>
33
34 struct bdev_inode {
35         struct block_device bdev;
36         struct inode vfs_inode;
37 };
38
39 static const struct address_space_operations def_blk_aops;
40
41 static inline struct bdev_inode *BDEV_I(struct inode *inode)
42 {
43         return container_of(inode, struct bdev_inode, vfs_inode);
44 }
45
46 inline struct block_device *I_BDEV(struct inode *inode)
47 {
48         return &BDEV_I(inode)->bdev;
49 }
50
51 EXPORT_SYMBOL(I_BDEV);
52
53 static sector_t max_block(struct block_device *bdev)
54 {
55         sector_t retval = ~((sector_t)0);
56         loff_t sz = i_size_read(bdev->bd_inode);
57
58         if (sz) {
59                 unsigned int size = block_size(bdev);
60                 unsigned int sizebits = blksize_bits(size);
61                 retval = (sz >> sizebits);
62         }
63         return retval;
64 }
65
66 /* Kill _all_ buffers and pagecache , dirty or not.. */
67 static void kill_bdev(struct block_device *bdev)
68 {
69         if (bdev->bd_inode->i_mapping->nrpages == 0)
70                 return;
71         invalidate_bh_lrus();
72         truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
73 }       
74
75 int set_blocksize(struct block_device *bdev, int size)
76 {
77         /* Size must be a power of two, and between 512 and PAGE_SIZE */
78         if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
79                 return -EINVAL;
80
81         /* Size cannot be smaller than the size supported by the device */
82         if (size < bdev_logical_block_size(bdev))
83                 return -EINVAL;
84
85         /* Don't change the size if it is same as current */
86         if (bdev->bd_block_size != size) {
87                 sync_blockdev(bdev);
88                 bdev->bd_block_size = size;
89                 bdev->bd_inode->i_blkbits = blksize_bits(size);
90                 kill_bdev(bdev);
91         }
92         return 0;
93 }
94
95 EXPORT_SYMBOL(set_blocksize);
96
97 int sb_set_blocksize(struct super_block *sb, int size)
98 {
99         if (set_blocksize(sb->s_bdev, size))
100                 return 0;
101         /* If we get here, we know size is power of two
102          * and it's value is between 512 and PAGE_SIZE */
103         sb->s_blocksize = size;
104         sb->s_blocksize_bits = blksize_bits(size);
105         return sb->s_blocksize;
106 }
107
108 EXPORT_SYMBOL(sb_set_blocksize);
109
110 int sb_min_blocksize(struct super_block *sb, int size)
111 {
112         int minsize = bdev_logical_block_size(sb->s_bdev);
113         if (size < minsize)
114                 size = minsize;
115         return sb_set_blocksize(sb, size);
116 }
117
118 EXPORT_SYMBOL(sb_min_blocksize);
119
120 static int
121 blkdev_get_block(struct inode *inode, sector_t iblock,
122                 struct buffer_head *bh, int create)
123 {
124         if (iblock >= max_block(I_BDEV(inode))) {
125                 if (create)
126                         return -EIO;
127
128                 /*
129                  * for reads, we're just trying to fill a partial page.
130                  * return a hole, they will have to call get_block again
131                  * before they can fill it, and they will get -EIO at that
132                  * time
133                  */
134                 return 0;
135         }
136         bh->b_bdev = I_BDEV(inode);
137         bh->b_blocknr = iblock;
138         set_buffer_mapped(bh);
139         return 0;
140 }
141
142 static int
143 blkdev_get_blocks(struct inode *inode, sector_t iblock,
144                 struct buffer_head *bh, int create)
145 {
146         sector_t end_block = max_block(I_BDEV(inode));
147         unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
148
149         if ((iblock + max_blocks) > end_block) {
150                 max_blocks = end_block - iblock;
151                 if ((long)max_blocks <= 0) {
152                         if (create)
153                                 return -EIO;    /* write fully beyond EOF */
154                         /*
155                          * It is a read which is fully beyond EOF.  We return
156                          * a !buffer_mapped buffer
157                          */
158                         max_blocks = 0;
159                 }
160         }
161
162         bh->b_bdev = I_BDEV(inode);
163         bh->b_blocknr = iblock;
164         bh->b_size = max_blocks << inode->i_blkbits;
165         if (max_blocks)
166                 set_buffer_mapped(bh);
167         return 0;
168 }
169
170 static ssize_t
171 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
172                         loff_t offset, unsigned long nr_segs)
173 {
174         struct file *file = iocb->ki_filp;
175         struct inode *inode = file->f_mapping->host;
176
177         return blockdev_direct_IO_no_locking(rw, iocb, inode, I_BDEV(inode),
178                                 iov, offset, nr_segs, blkdev_get_blocks, NULL);
179 }
180
181 int __sync_blockdev(struct block_device *bdev, int wait)
182 {
183         if (!bdev)
184                 return 0;
185         if (!wait)
186                 return filemap_flush(bdev->bd_inode->i_mapping);
187         return filemap_write_and_wait(bdev->bd_inode->i_mapping);
188 }
189
190 /*
191  * Write out and wait upon all the dirty data associated with a block
192  * device via its mapping.  Does not take the superblock lock.
193  */
194 int sync_blockdev(struct block_device *bdev)
195 {
196         return __sync_blockdev(bdev, 1);
197 }
198 EXPORT_SYMBOL(sync_blockdev);
199
200 /*
201  * Write out and wait upon all dirty data associated with this
202  * device.   Filesystem data as well as the underlying block
203  * device.  Takes the superblock lock.
204  */
205 int fsync_bdev(struct block_device *bdev)
206 {
207         struct super_block *sb = get_super(bdev);
208         if (sb) {
209                 int res = sync_filesystem(sb);
210                 drop_super(sb);
211                 return res;
212         }
213         return sync_blockdev(bdev);
214 }
215 EXPORT_SYMBOL(fsync_bdev);
216
217 /**
218  * freeze_bdev  --  lock a filesystem and force it into a consistent state
219  * @bdev:       blockdevice to lock
220  *
221  * If a superblock is found on this device, we take the s_umount semaphore
222  * on it to make sure nobody unmounts until the snapshot creation is done.
223  * The reference counter (bd_fsfreeze_count) guarantees that only the last
224  * unfreeze process can unfreeze the frozen filesystem actually when multiple
225  * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
226  * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
227  * actually.
228  */
229 struct super_block *freeze_bdev(struct block_device *bdev)
230 {
231         struct super_block *sb;
232         int error = 0;
233
234         mutex_lock(&bdev->bd_fsfreeze_mutex);
235         if (++bdev->bd_fsfreeze_count > 1) {
236                 /*
237                  * We don't even need to grab a reference - the first call
238                  * to freeze_bdev grab an active reference and only the last
239                  * thaw_bdev drops it.
240                  */
241                 sb = get_super(bdev);
242                 drop_super(sb);
243                 mutex_unlock(&bdev->bd_fsfreeze_mutex);
244                 return sb;
245         }
246
247         sb = get_active_super(bdev);
248         if (!sb)
249                 goto out;
250         if (sb->s_flags & MS_RDONLY) {
251                 sb->s_frozen = SB_FREEZE_TRANS;
252                 up_write(&sb->s_umount);
253                 mutex_unlock(&bdev->bd_fsfreeze_mutex);
254                 return sb;
255         }
256
257         sb->s_frozen = SB_FREEZE_WRITE;
258         smp_wmb();
259
260         sync_filesystem(sb);
261
262         sb->s_frozen = SB_FREEZE_TRANS;
263         smp_wmb();
264
265         sync_blockdev(sb->s_bdev);
266
267         if (sb->s_op->freeze_fs) {
268                 error = sb->s_op->freeze_fs(sb);
269                 if (error) {
270                         printk(KERN_ERR
271                                 "VFS:Filesystem freeze failed\n");
272                         sb->s_frozen = SB_UNFROZEN;
273                         deactivate_locked_super(sb);
274                         bdev->bd_fsfreeze_count--;
275                         mutex_unlock(&bdev->bd_fsfreeze_mutex);
276                         return ERR_PTR(error);
277                 }
278         }
279         up_write(&sb->s_umount);
280
281  out:
282         sync_blockdev(bdev);
283         mutex_unlock(&bdev->bd_fsfreeze_mutex);
284         return sb;      /* thaw_bdev releases s->s_umount */
285 }
286 EXPORT_SYMBOL(freeze_bdev);
287
288 /**
289  * thaw_bdev  -- unlock filesystem
290  * @bdev:       blockdevice to unlock
291  * @sb:         associated superblock
292  *
293  * Unlocks the filesystem and marks it writeable again after freeze_bdev().
294  */
295 int thaw_bdev(struct block_device *bdev, struct super_block *sb)
296 {
297         int error = -EINVAL;
298
299         mutex_lock(&bdev->bd_fsfreeze_mutex);
300         if (!bdev->bd_fsfreeze_count)
301                 goto out_unlock;
302
303         error = 0;
304         if (--bdev->bd_fsfreeze_count > 0)
305                 goto out_unlock;
306
307         if (!sb)
308                 goto out_unlock;
309
310         BUG_ON(sb->s_bdev != bdev);
311         down_write(&sb->s_umount);
312         if (sb->s_flags & MS_RDONLY)
313                 goto out_unfrozen;
314
315         if (sb->s_op->unfreeze_fs) {
316                 error = sb->s_op->unfreeze_fs(sb);
317                 if (error) {
318                         printk(KERN_ERR
319                                 "VFS:Filesystem thaw failed\n");
320                         sb->s_frozen = SB_FREEZE_TRANS;
321                         bdev->bd_fsfreeze_count++;
322                         mutex_unlock(&bdev->bd_fsfreeze_mutex);
323                         return error;
324                 }
325         }
326
327 out_unfrozen:
328         sb->s_frozen = SB_UNFROZEN;
329         smp_wmb();
330         wake_up(&sb->s_wait_unfrozen);
331
332         if (sb)
333                 deactivate_locked_super(sb);
334 out_unlock:
335         mutex_unlock(&bdev->bd_fsfreeze_mutex);
336         return 0;
337 }
338 EXPORT_SYMBOL(thaw_bdev);
339
340 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
341 {
342         return block_write_full_page(page, blkdev_get_block, wbc);
343 }
344
345 static int blkdev_readpage(struct file * file, struct page * page)
346 {
347         return block_read_full_page(page, blkdev_get_block);
348 }
349
350 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
351                         loff_t pos, unsigned len, unsigned flags,
352                         struct page **pagep, void **fsdata)
353 {
354         *pagep = NULL;
355         return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
356                                 blkdev_get_block);
357 }
358
359 static int blkdev_write_end(struct file *file, struct address_space *mapping,
360                         loff_t pos, unsigned len, unsigned copied,
361                         struct page *page, void *fsdata)
362 {
363         int ret;
364         ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
365
366         unlock_page(page);
367         page_cache_release(page);
368
369         return ret;
370 }
371
372 /*
373  * private llseek:
374  * for a block special file file->f_path.dentry->d_inode->i_size is zero
375  * so we compute the size by hand (just as in block_read/write above)
376  */
377 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
378 {
379         struct inode *bd_inode = file->f_mapping->host;
380         loff_t size;
381         loff_t retval;
382
383         mutex_lock(&bd_inode->i_mutex);
384         size = i_size_read(bd_inode);
385
386         switch (origin) {
387                 case 2:
388                         offset += size;
389                         break;
390                 case 1:
391                         offset += file->f_pos;
392         }
393         retval = -EINVAL;
394         if (offset >= 0 && offset <= size) {
395                 if (offset != file->f_pos) {
396                         file->f_pos = offset;
397                 }
398                 retval = offset;
399         }
400         mutex_unlock(&bd_inode->i_mutex);
401         return retval;
402 }
403         
404 /*
405  *      Filp is never NULL; the only case when ->fsync() is called with
406  *      NULL first argument is nfsd_sync_dir() and that's not a directory.
407  */
408  
409 int block_fsync(struct file *filp, struct dentry *dentry, int datasync)
410 {
411         return sync_blockdev(I_BDEV(filp->f_mapping->host));
412 }
413
414 /*
415  * pseudo-fs
416  */
417
418 static  __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
419 static struct kmem_cache * bdev_cachep __read_mostly;
420
421 static struct inode *bdev_alloc_inode(struct super_block *sb)
422 {
423         struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
424         if (!ei)
425                 return NULL;
426         return &ei->vfs_inode;
427 }
428 EXPORT_SYMBOL(block_fsync);
429
430 static void bdev_destroy_inode(struct inode *inode)
431 {
432         struct bdev_inode *bdi = BDEV_I(inode);
433
434         kmem_cache_free(bdev_cachep, bdi);
435 }
436
437 static void init_once(void *foo)
438 {
439         struct bdev_inode *ei = (struct bdev_inode *) foo;
440         struct block_device *bdev = &ei->bdev;
441
442         memset(bdev, 0, sizeof(*bdev));
443         mutex_init(&bdev->bd_mutex);
444         INIT_LIST_HEAD(&bdev->bd_inodes);
445         INIT_LIST_HEAD(&bdev->bd_list);
446 #ifdef CONFIG_SYSFS
447         INIT_LIST_HEAD(&bdev->bd_holder_list);
448 #endif
449         inode_init_once(&ei->vfs_inode);
450         /* Initialize mutex for freeze. */
451         mutex_init(&bdev->bd_fsfreeze_mutex);
452 }
453
454 static inline void __bd_forget(struct inode *inode)
455 {
456         list_del_init(&inode->i_devices);
457         inode->i_bdev = NULL;
458         inode->i_mapping = &inode->i_data;
459 }
460
461 static void bdev_clear_inode(struct inode *inode)
462 {
463         struct block_device *bdev = &BDEV_I(inode)->bdev;
464         struct list_head *p;
465         spin_lock(&bdev_lock);
466         while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
467                 __bd_forget(list_entry(p, struct inode, i_devices));
468         }
469         list_del_init(&bdev->bd_list);
470         spin_unlock(&bdev_lock);
471 }
472
473 static const struct super_operations bdev_sops = {
474         .statfs = simple_statfs,
475         .alloc_inode = bdev_alloc_inode,
476         .destroy_inode = bdev_destroy_inode,
477         .drop_inode = generic_delete_inode,
478         .clear_inode = bdev_clear_inode,
479 };
480
481 static int bd_get_sb(struct file_system_type *fs_type,
482         int flags, const char *dev_name, void *data, struct vfsmount *mnt)
483 {
484         return get_sb_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576, mnt);
485 }
486
487 static struct file_system_type bd_type = {
488         .name           = "bdev",
489         .get_sb         = bd_get_sb,
490         .kill_sb        = kill_anon_super,
491 };
492
493 struct super_block *blockdev_superblock __read_mostly;
494
495 void __init bdev_cache_init(void)
496 {
497         int err;
498         struct vfsmount *bd_mnt;
499
500         bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
501                         0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
502                                 SLAB_MEM_SPREAD|SLAB_PANIC),
503                         init_once);
504         err = register_filesystem(&bd_type);
505         if (err)
506                 panic("Cannot register bdev pseudo-fs");
507         bd_mnt = kern_mount(&bd_type);
508         if (IS_ERR(bd_mnt))
509                 panic("Cannot create bdev pseudo-fs");
510         /*
511          * This vfsmount structure is only used to obtain the
512          * blockdev_superblock, so tell kmemleak not to report it.
513          */
514         kmemleak_not_leak(bd_mnt);
515         blockdev_superblock = bd_mnt->mnt_sb;   /* For writeback */
516 }
517
518 /*
519  * Most likely _very_ bad one - but then it's hardly critical for small
520  * /dev and can be fixed when somebody will need really large one.
521  * Keep in mind that it will be fed through icache hash function too.
522  */
523 static inline unsigned long hash(dev_t dev)
524 {
525         return MAJOR(dev)+MINOR(dev);
526 }
527
528 static int bdev_test(struct inode *inode, void *data)
529 {
530         return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
531 }
532
533 static int bdev_set(struct inode *inode, void *data)
534 {
535         BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
536         return 0;
537 }
538
539 static LIST_HEAD(all_bdevs);
540
541 struct block_device *bdget(dev_t dev)
542 {
543         struct block_device *bdev;
544         struct inode *inode;
545
546         inode = iget5_locked(blockdev_superblock, hash(dev),
547                         bdev_test, bdev_set, &dev);
548
549         if (!inode)
550                 return NULL;
551
552         bdev = &BDEV_I(inode)->bdev;
553
554         if (inode->i_state & I_NEW) {
555                 bdev->bd_contains = NULL;
556                 bdev->bd_inode = inode;
557                 bdev->bd_block_size = (1 << inode->i_blkbits);
558                 bdev->bd_part_count = 0;
559                 bdev->bd_invalidated = 0;
560                 inode->i_mode = S_IFBLK;
561                 inode->i_rdev = dev;
562                 inode->i_bdev = bdev;
563                 inode->i_data.a_ops = &def_blk_aops;
564                 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
565                 inode->i_data.backing_dev_info = &default_backing_dev_info;
566                 spin_lock(&bdev_lock);
567                 list_add(&bdev->bd_list, &all_bdevs);
568                 spin_unlock(&bdev_lock);
569                 unlock_new_inode(inode);
570         }
571         return bdev;
572 }
573
574 EXPORT_SYMBOL(bdget);
575
576 /**
577  * bdgrab -- Grab a reference to an already referenced block device
578  * @bdev:       Block device to grab a reference to.
579  */
580 struct block_device *bdgrab(struct block_device *bdev)
581 {
582         atomic_inc(&bdev->bd_inode->i_count);
583         return bdev;
584 }
585
586 long nr_blockdev_pages(void)
587 {
588         struct block_device *bdev;
589         long ret = 0;
590         spin_lock(&bdev_lock);
591         list_for_each_entry(bdev, &all_bdevs, bd_list) {
592                 ret += bdev->bd_inode->i_mapping->nrpages;
593         }
594         spin_unlock(&bdev_lock);
595         return ret;
596 }
597
598 void bdput(struct block_device *bdev)
599 {
600         iput(bdev->bd_inode);
601 }
602
603 EXPORT_SYMBOL(bdput);
604  
605 static struct block_device *bd_acquire(struct inode *inode)
606 {
607         struct block_device *bdev;
608
609         spin_lock(&bdev_lock);
610         bdev = inode->i_bdev;
611         if (bdev) {
612                 atomic_inc(&bdev->bd_inode->i_count);
613                 spin_unlock(&bdev_lock);
614                 return bdev;
615         }
616         spin_unlock(&bdev_lock);
617
618         bdev = bdget(inode->i_rdev);
619         if (bdev) {
620                 spin_lock(&bdev_lock);
621                 if (!inode->i_bdev) {
622                         /*
623                          * We take an additional bd_inode->i_count for inode,
624                          * and it's released in clear_inode() of inode.
625                          * So, we can access it via ->i_mapping always
626                          * without igrab().
627                          */
628                         atomic_inc(&bdev->bd_inode->i_count);
629                         inode->i_bdev = bdev;
630                         inode->i_mapping = bdev->bd_inode->i_mapping;
631                         list_add(&inode->i_devices, &bdev->bd_inodes);
632                 }
633                 spin_unlock(&bdev_lock);
634         }
635         return bdev;
636 }
637
638 /* Call when you free inode */
639
640 void bd_forget(struct inode *inode)
641 {
642         struct block_device *bdev = NULL;
643
644         spin_lock(&bdev_lock);
645         if (inode->i_bdev) {
646                 if (!sb_is_blkdev_sb(inode->i_sb))
647                         bdev = inode->i_bdev;
648                 __bd_forget(inode);
649         }
650         spin_unlock(&bdev_lock);
651
652         if (bdev)
653                 iput(bdev->bd_inode);
654 }
655
656 int bd_claim(struct block_device *bdev, void *holder)
657 {
658         int res;
659         spin_lock(&bdev_lock);
660
661         /* first decide result */
662         if (bdev->bd_holder == holder)
663                 res = 0;         /* already a holder */
664         else if (bdev->bd_holder != NULL)
665                 res = -EBUSY;    /* held by someone else */
666         else if (bdev->bd_contains == bdev)
667                 res = 0;         /* is a whole device which isn't held */
668
669         else if (bdev->bd_contains->bd_holder == bd_claim)
670                 res = 0;         /* is a partition of a device that is being partitioned */
671         else if (bdev->bd_contains->bd_holder != NULL)
672                 res = -EBUSY;    /* is a partition of a held device */
673         else
674                 res = 0;         /* is a partition of an un-held device */
675
676         /* now impose change */
677         if (res==0) {
678                 /* note that for a whole device bd_holders
679                  * will be incremented twice, and bd_holder will
680                  * be set to bd_claim before being set to holder
681                  */
682                 bdev->bd_contains->bd_holders ++;
683                 bdev->bd_contains->bd_holder = bd_claim;
684                 bdev->bd_holders++;
685                 bdev->bd_holder = holder;
686         }
687         spin_unlock(&bdev_lock);
688         return res;
689 }
690
691 EXPORT_SYMBOL(bd_claim);
692
693 void bd_release(struct block_device *bdev)
694 {
695         spin_lock(&bdev_lock);
696         if (!--bdev->bd_contains->bd_holders)
697                 bdev->bd_contains->bd_holder = NULL;
698         if (!--bdev->bd_holders)
699                 bdev->bd_holder = NULL;
700         spin_unlock(&bdev_lock);
701 }
702
703 EXPORT_SYMBOL(bd_release);
704
705 #ifdef CONFIG_SYSFS
706 /*
707  * Functions for bd_claim_by_kobject / bd_release_from_kobject
708  *
709  *     If a kobject is passed to bd_claim_by_kobject()
710  *     and the kobject has a parent directory,
711  *     following symlinks are created:
712  *        o from the kobject to the claimed bdev
713  *        o from "holders" directory of the bdev to the parent of the kobject
714  *     bd_release_from_kobject() removes these symlinks.
715  *
716  *     Example:
717  *        If /dev/dm-0 maps to /dev/sda, kobject corresponding to
718  *        /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
719  *           /sys/block/dm-0/slaves/sda --> /sys/block/sda
720  *           /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
721  */
722
723 static int add_symlink(struct kobject *from, struct kobject *to)
724 {
725         if (!from || !to)
726                 return 0;
727         return sysfs_create_link(from, to, kobject_name(to));
728 }
729
730 static void del_symlink(struct kobject *from, struct kobject *to)
731 {
732         if (!from || !to)
733                 return;
734         sysfs_remove_link(from, kobject_name(to));
735 }
736
737 /*
738  * 'struct bd_holder' contains pointers to kobjects symlinked by
739  * bd_claim_by_kobject.
740  * It's connected to bd_holder_list which is protected by bdev->bd_sem.
741  */
742 struct bd_holder {
743         struct list_head list;  /* chain of holders of the bdev */
744         int count;              /* references from the holder */
745         struct kobject *sdir;   /* holder object, e.g. "/block/dm-0/slaves" */
746         struct kobject *hdev;   /* e.g. "/block/dm-0" */
747         struct kobject *hdir;   /* e.g. "/block/sda/holders" */
748         struct kobject *sdev;   /* e.g. "/block/sda" */
749 };
750
751 /*
752  * Get references of related kobjects at once.
753  * Returns 1 on success. 0 on failure.
754  *
755  * Should call bd_holder_release_dirs() after successful use.
756  */
757 static int bd_holder_grab_dirs(struct block_device *bdev,
758                         struct bd_holder *bo)
759 {
760         if (!bdev || !bo)
761                 return 0;
762
763         bo->sdir = kobject_get(bo->sdir);
764         if (!bo->sdir)
765                 return 0;
766
767         bo->hdev = kobject_get(bo->sdir->parent);
768         if (!bo->hdev)
769                 goto fail_put_sdir;
770
771         bo->sdev = kobject_get(&part_to_dev(bdev->bd_part)->kobj);
772         if (!bo->sdev)
773                 goto fail_put_hdev;
774
775         bo->hdir = kobject_get(bdev->bd_part->holder_dir);
776         if (!bo->hdir)
777                 goto fail_put_sdev;
778
779         return 1;
780
781 fail_put_sdev:
782         kobject_put(bo->sdev);
783 fail_put_hdev:
784         kobject_put(bo->hdev);
785 fail_put_sdir:
786         kobject_put(bo->sdir);
787
788         return 0;
789 }
790
791 /* Put references of related kobjects at once. */
792 static void bd_holder_release_dirs(struct bd_holder *bo)
793 {
794         kobject_put(bo->hdir);
795         kobject_put(bo->sdev);
796         kobject_put(bo->hdev);
797         kobject_put(bo->sdir);
798 }
799
800 static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
801 {
802         struct bd_holder *bo;
803
804         bo = kzalloc(sizeof(*bo), GFP_KERNEL);
805         if (!bo)
806                 return NULL;
807
808         bo->count = 1;
809         bo->sdir = kobj;
810
811         return bo;
812 }
813
814 static void free_bd_holder(struct bd_holder *bo)
815 {
816         kfree(bo);
817 }
818
819 /**
820  * find_bd_holder - find matching struct bd_holder from the block device
821  *
822  * @bdev:       struct block device to be searched
823  * @bo:         target struct bd_holder
824  *
825  * Returns matching entry with @bo in @bdev->bd_holder_list.
826  * If found, increment the reference count and return the pointer.
827  * If not found, returns NULL.
828  */
829 static struct bd_holder *find_bd_holder(struct block_device *bdev,
830                                         struct bd_holder *bo)
831 {
832         struct bd_holder *tmp;
833
834         list_for_each_entry(tmp, &bdev->bd_holder_list, list)
835                 if (tmp->sdir == bo->sdir) {
836                         tmp->count++;
837                         return tmp;
838                 }
839
840         return NULL;
841 }
842
843 /**
844  * add_bd_holder - create sysfs symlinks for bd_claim() relationship
845  *
846  * @bdev:       block device to be bd_claimed
847  * @bo:         preallocated and initialized by alloc_bd_holder()
848  *
849  * Add @bo to @bdev->bd_holder_list, create symlinks.
850  *
851  * Returns 0 if symlinks are created.
852  * Returns -ve if something fails.
853  */
854 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
855 {
856         int err;
857
858         if (!bo)
859                 return -EINVAL;
860
861         if (!bd_holder_grab_dirs(bdev, bo))
862                 return -EBUSY;
863
864         err = add_symlink(bo->sdir, bo->sdev);
865         if (err)
866                 return err;
867
868         err = add_symlink(bo->hdir, bo->hdev);
869         if (err) {
870                 del_symlink(bo->sdir, bo->sdev);
871                 return err;
872         }
873
874         list_add_tail(&bo->list, &bdev->bd_holder_list);
875         return 0;
876 }
877
878 /**
879  * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
880  *
881  * @bdev:       block device to be bd_claimed
882  * @kobj:       holder's kobject
883  *
884  * If there is matching entry with @kobj in @bdev->bd_holder_list
885  * and no other bd_claim() from the same kobject,
886  * remove the struct bd_holder from the list, delete symlinks for it.
887  *
888  * Returns a pointer to the struct bd_holder when it's removed from the list
889  * and ready to be freed.
890  * Returns NULL if matching claim isn't found or there is other bd_claim()
891  * by the same kobject.
892  */
893 static struct bd_holder *del_bd_holder(struct block_device *bdev,
894                                         struct kobject *kobj)
895 {
896         struct bd_holder *bo;
897
898         list_for_each_entry(bo, &bdev->bd_holder_list, list) {
899                 if (bo->sdir == kobj) {
900                         bo->count--;
901                         BUG_ON(bo->count < 0);
902                         if (!bo->count) {
903                                 list_del(&bo->list);
904                                 del_symlink(bo->sdir, bo->sdev);
905                                 del_symlink(bo->hdir, bo->hdev);
906                                 bd_holder_release_dirs(bo);
907                                 return bo;
908                         }
909                         break;
910                 }
911         }
912
913         return NULL;
914 }
915
916 /**
917  * bd_claim_by_kobject - bd_claim() with additional kobject signature
918  *
919  * @bdev:       block device to be claimed
920  * @holder:     holder's signature
921  * @kobj:       holder's kobject
922  *
923  * Do bd_claim() and if it succeeds, create sysfs symlinks between
924  * the bdev and the holder's kobject.
925  * Use bd_release_from_kobject() when relesing the claimed bdev.
926  *
927  * Returns 0 on success. (same as bd_claim())
928  * Returns errno on failure.
929  */
930 static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
931                                 struct kobject *kobj)
932 {
933         int err;
934         struct bd_holder *bo, *found;
935
936         if (!kobj)
937                 return -EINVAL;
938
939         bo = alloc_bd_holder(kobj);
940         if (!bo)
941                 return -ENOMEM;
942
943         mutex_lock(&bdev->bd_mutex);
944
945         err = bd_claim(bdev, holder);
946         if (err)
947                 goto fail;
948
949         found = find_bd_holder(bdev, bo);
950         if (found)
951                 goto fail;
952
953         err = add_bd_holder(bdev, bo);
954         if (err)
955                 bd_release(bdev);
956         else
957                 bo = NULL;
958 fail:
959         mutex_unlock(&bdev->bd_mutex);
960         free_bd_holder(bo);
961         return err;
962 }
963
964 /**
965  * bd_release_from_kobject - bd_release() with additional kobject signature
966  *
967  * @bdev:       block device to be released
968  * @kobj:       holder's kobject
969  *
970  * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
971  */
972 static void bd_release_from_kobject(struct block_device *bdev,
973                                         struct kobject *kobj)
974 {
975         if (!kobj)
976                 return;
977
978         mutex_lock(&bdev->bd_mutex);
979         bd_release(bdev);
980         free_bd_holder(del_bd_holder(bdev, kobj));
981         mutex_unlock(&bdev->bd_mutex);
982 }
983
984 /**
985  * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
986  *
987  * @bdev:       block device to be claimed
988  * @holder:     holder's signature
989  * @disk:       holder's gendisk
990  *
991  * Call bd_claim_by_kobject() with getting @disk->slave_dir.
992  */
993 int bd_claim_by_disk(struct block_device *bdev, void *holder,
994                         struct gendisk *disk)
995 {
996         return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
997 }
998 EXPORT_SYMBOL_GPL(bd_claim_by_disk);
999
1000 /**
1001  * bd_release_from_disk - wrapper function for bd_release_from_kobject()
1002  *
1003  * @bdev:       block device to be claimed
1004  * @disk:       holder's gendisk
1005  *
1006  * Call bd_release_from_kobject() and put @disk->slave_dir.
1007  */
1008 void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
1009 {
1010         bd_release_from_kobject(bdev, disk->slave_dir);
1011         kobject_put(disk->slave_dir);
1012 }
1013 EXPORT_SYMBOL_GPL(bd_release_from_disk);
1014 #endif
1015
1016 /*
1017  * Tries to open block device by device number.  Use it ONLY if you
1018  * really do not have anything better - i.e. when you are behind a
1019  * truly sucky interface and all you are given is a device number.  _Never_
1020  * to be used for internal purposes.  If you ever need it - reconsider
1021  * your API.
1022  */
1023 struct block_device *open_by_devnum(dev_t dev, fmode_t mode)
1024 {
1025         struct block_device *bdev = bdget(dev);
1026         int err = -ENOMEM;
1027         if (bdev)
1028                 err = blkdev_get(bdev, mode);
1029         return err ? ERR_PTR(err) : bdev;
1030 }
1031
1032 EXPORT_SYMBOL(open_by_devnum);
1033
1034 /**
1035  * flush_disk - invalidates all buffer-cache entries on a disk
1036  *
1037  * @bdev:      struct block device to be flushed
1038  *
1039  * Invalidates all buffer-cache entries on a disk. It should be called
1040  * when a disk has been changed -- either by a media change or online
1041  * resize.
1042  */
1043 static void flush_disk(struct block_device *bdev)
1044 {
1045         if (__invalidate_device(bdev)) {
1046                 char name[BDEVNAME_SIZE] = "";
1047
1048                 if (bdev->bd_disk)
1049                         disk_name(bdev->bd_disk, 0, name);
1050                 printk(KERN_WARNING "VFS: busy inodes on changed media or "
1051                        "resized disk %s\n", name);
1052         }
1053
1054         if (!bdev->bd_disk)
1055                 return;
1056         if (disk_partitionable(bdev->bd_disk))
1057                 bdev->bd_invalidated = 1;
1058 }
1059
1060 /**
1061  * check_disk_size_change - checks for disk size change and adjusts bdev size.
1062  * @disk: struct gendisk to check
1063  * @bdev: struct bdev to adjust.
1064  *
1065  * This routine checks to see if the bdev size does not match the disk size
1066  * and adjusts it if it differs.
1067  */
1068 void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
1069 {
1070         loff_t disk_size, bdev_size;
1071
1072         disk_size = (loff_t)get_capacity(disk) << 9;
1073         bdev_size = i_size_read(bdev->bd_inode);
1074         if (disk_size != bdev_size) {
1075                 char name[BDEVNAME_SIZE];
1076
1077                 disk_name(disk, 0, name);
1078                 printk(KERN_INFO
1079                        "%s: detected capacity change from %lld to %lld\n",
1080                        name, bdev_size, disk_size);
1081                 i_size_write(bdev->bd_inode, disk_size);
1082                 flush_disk(bdev);
1083         }
1084 }
1085 EXPORT_SYMBOL(check_disk_size_change);
1086
1087 /**
1088  * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
1089  * @disk: struct gendisk to be revalidated
1090  *
1091  * This routine is a wrapper for lower-level driver's revalidate_disk
1092  * call-backs.  It is used to do common pre and post operations needed
1093  * for all revalidate_disk operations.
1094  */
1095 int revalidate_disk(struct gendisk *disk)
1096 {
1097         struct block_device *bdev;
1098         int ret = 0;
1099
1100         if (disk->fops->revalidate_disk)
1101                 ret = disk->fops->revalidate_disk(disk);
1102
1103         bdev = bdget_disk(disk, 0);
1104         if (!bdev)
1105                 return ret;
1106
1107         mutex_lock(&bdev->bd_mutex);
1108         check_disk_size_change(disk, bdev);
1109         mutex_unlock(&bdev->bd_mutex);
1110         bdput(bdev);
1111         return ret;
1112 }
1113 EXPORT_SYMBOL(revalidate_disk);
1114
1115 /*
1116  * This routine checks whether a removable media has been changed,
1117  * and invalidates all buffer-cache-entries in that case. This
1118  * is a relatively slow routine, so we have to try to minimize using
1119  * it. Thus it is called only upon a 'mount' or 'open'. This
1120  * is the best way of combining speed and utility, I think.
1121  * People changing diskettes in the middle of an operation deserve
1122  * to lose :-)
1123  */
1124 int check_disk_change(struct block_device *bdev)
1125 {
1126         struct gendisk *disk = bdev->bd_disk;
1127         const struct block_device_operations *bdops = disk->fops;
1128
1129         if (!bdops->media_changed)
1130                 return 0;
1131         if (!bdops->media_changed(bdev->bd_disk))
1132                 return 0;
1133
1134         flush_disk(bdev);
1135         if (bdops->revalidate_disk)
1136                 bdops->revalidate_disk(bdev->bd_disk);
1137         return 1;
1138 }
1139
1140 EXPORT_SYMBOL(check_disk_change);
1141
1142 void bd_set_size(struct block_device *bdev, loff_t size)
1143 {
1144         unsigned bsize = bdev_logical_block_size(bdev);
1145
1146         bdev->bd_inode->i_size = size;
1147         while (bsize < PAGE_CACHE_SIZE) {
1148                 if (size & bsize)
1149                         break;
1150                 bsize <<= 1;
1151         }
1152         bdev->bd_block_size = bsize;
1153         bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1154 }
1155 EXPORT_SYMBOL(bd_set_size);
1156
1157 static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
1158
1159 /*
1160  * bd_mutex locking:
1161  *
1162  *  mutex_lock(part->bd_mutex)
1163  *    mutex_lock_nested(whole->bd_mutex, 1)
1164  */
1165
1166 static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
1167 {
1168         struct gendisk *disk;
1169         int ret;
1170         int partno;
1171         int perm = 0;
1172
1173         if (mode & FMODE_READ)
1174                 perm |= MAY_READ;
1175         if (mode & FMODE_WRITE)
1176                 perm |= MAY_WRITE;
1177         /*
1178          * hooks: /n/, see "layering violations".
1179          */
1180         if (!for_part) {
1181                 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1182                 if (ret != 0) {
1183                         bdput(bdev);
1184                         return ret;
1185                 }
1186         }
1187
1188         lock_kernel();
1189  restart:
1190
1191         ret = -ENXIO;
1192         disk = get_gendisk(bdev->bd_dev, &partno);
1193         if (!disk)
1194                 goto out_unlock_kernel;
1195
1196         mutex_lock_nested(&bdev->bd_mutex, for_part);
1197         if (!bdev->bd_openers) {
1198                 bdev->bd_disk = disk;
1199                 bdev->bd_contains = bdev;
1200                 if (!partno) {
1201                         struct backing_dev_info *bdi;
1202
1203                         ret = -ENXIO;
1204                         bdev->bd_part = disk_get_part(disk, partno);
1205                         if (!bdev->bd_part)
1206                                 goto out_clear;
1207
1208                         ret = 0;
1209                         if (disk->fops->open) {
1210                                 ret = disk->fops->open(bdev, mode);
1211                                 if (ret == -ERESTARTSYS) {
1212                                         /* Lost a race with 'disk' being
1213                                          * deleted, try again.
1214                                          * See md.c
1215                                          */
1216                                         disk_put_part(bdev->bd_part);
1217                                         bdev->bd_part = NULL;
1218                                         module_put(disk->fops->owner);
1219                                         put_disk(disk);
1220                                         bdev->bd_disk = NULL;
1221                                         mutex_unlock(&bdev->bd_mutex);
1222                                         goto restart;
1223                                 }
1224                         }
1225                         /*
1226                          * If the device is invalidated, rescan partition
1227                          * if open succeeded or failed with -ENOMEDIUM.
1228                          * The latter is necessary to prevent ghost
1229                          * partitions on a removed medium.
1230                          */
1231                         if (bdev->bd_invalidated && (!ret || ret == -ENOMEDIUM))
1232                                 rescan_partitions(disk, bdev);
1233                         if (ret)
1234                                 goto out_clear;
1235
1236                         if (!bdev->bd_openers) {
1237                                 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1238                                 bdi = blk_get_backing_dev_info(bdev);
1239                                 if (bdi == NULL)
1240                                         bdi = &default_backing_dev_info;
1241                                 bdev->bd_inode->i_data.backing_dev_info = bdi;
1242                         }
1243                 } else {
1244                         struct block_device *whole;
1245                         whole = bdget_disk(disk, 0);
1246                         ret = -ENOMEM;
1247                         if (!whole)
1248                                 goto out_clear;
1249                         BUG_ON(for_part);
1250                         ret = __blkdev_get(whole, mode, 1);
1251                         if (ret)
1252                                 goto out_clear;
1253                         bdev->bd_contains = whole;
1254                         bdev->bd_inode->i_data.backing_dev_info =
1255                            whole->bd_inode->i_data.backing_dev_info;
1256                         bdev->bd_part = disk_get_part(disk, partno);
1257                         if (!(disk->flags & GENHD_FL_UP) ||
1258                             !bdev->bd_part || !bdev->bd_part->nr_sects) {
1259                                 ret = -ENXIO;
1260                                 goto out_clear;
1261                         }
1262                         bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
1263                 }
1264         } else {
1265                 module_put(disk->fops->owner);
1266                 put_disk(disk);
1267                 disk = NULL;
1268                 if (bdev->bd_contains == bdev) {
1269                         ret = 0;
1270                         if (bdev->bd_disk->fops->open)
1271                                 ret = bdev->bd_disk->fops->open(bdev, mode);
1272                         /* the same as first opener case, read comment there */
1273                         if (bdev->bd_invalidated && (!ret || ret == -ENOMEDIUM))
1274                                 rescan_partitions(bdev->bd_disk, bdev);
1275                         if (ret)
1276                                 goto out_unlock_bdev;
1277                 }
1278         }
1279         bdev->bd_openers++;
1280         if (for_part)
1281                 bdev->bd_part_count++;
1282         mutex_unlock(&bdev->bd_mutex);
1283         unlock_kernel();
1284         return 0;
1285
1286  out_clear:
1287         disk_put_part(bdev->bd_part);
1288         bdev->bd_disk = NULL;
1289         bdev->bd_part = NULL;
1290         bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1291         if (bdev != bdev->bd_contains)
1292                 __blkdev_put(bdev->bd_contains, mode, 1);
1293         bdev->bd_contains = NULL;
1294  out_unlock_bdev:
1295         mutex_unlock(&bdev->bd_mutex);
1296  out_unlock_kernel:
1297         unlock_kernel();
1298
1299         if (disk)
1300                 module_put(disk->fops->owner);
1301         put_disk(disk);
1302         bdput(bdev);
1303
1304         return ret;
1305 }
1306
1307 int blkdev_get(struct block_device *bdev, fmode_t mode)
1308 {
1309         return __blkdev_get(bdev, mode, 0);
1310 }
1311 EXPORT_SYMBOL(blkdev_get);
1312
1313 static int blkdev_open(struct inode * inode, struct file * filp)
1314 {
1315         struct block_device *bdev;
1316         int res;
1317
1318         /*
1319          * Preserve backwards compatibility and allow large file access
1320          * even if userspace doesn't ask for it explicitly. Some mkfs
1321          * binary needs it. We might want to drop this workaround
1322          * during an unstable branch.
1323          */
1324         filp->f_flags |= O_LARGEFILE;
1325
1326         if (filp->f_flags & O_NDELAY)
1327                 filp->f_mode |= FMODE_NDELAY;
1328         if (filp->f_flags & O_EXCL)
1329                 filp->f_mode |= FMODE_EXCL;
1330         if ((filp->f_flags & O_ACCMODE) == 3)
1331                 filp->f_mode |= FMODE_WRITE_IOCTL;
1332
1333         bdev = bd_acquire(inode);
1334         if (bdev == NULL)
1335                 return -ENOMEM;
1336
1337         filp->f_mapping = bdev->bd_inode->i_mapping;
1338
1339         res = blkdev_get(bdev, filp->f_mode);
1340         if (res)
1341                 return res;
1342
1343         if (filp->f_mode & FMODE_EXCL) {
1344                 res = bd_claim(bdev, filp);
1345                 if (res)
1346                         goto out_blkdev_put;
1347         }
1348
1349         return 0;
1350
1351  out_blkdev_put:
1352         blkdev_put(bdev, filp->f_mode);
1353         return res;
1354 }
1355
1356 static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1357 {
1358         int ret = 0;
1359         struct gendisk *disk = bdev->bd_disk;
1360         struct block_device *victim = NULL;
1361
1362         mutex_lock_nested(&bdev->bd_mutex, for_part);
1363         lock_kernel();
1364         if (for_part)
1365                 bdev->bd_part_count--;
1366
1367         if (!--bdev->bd_openers) {
1368                 sync_blockdev(bdev);
1369                 kill_bdev(bdev);
1370         }
1371         if (bdev->bd_contains == bdev) {
1372                 if (disk->fops->release)
1373                         ret = disk->fops->release(disk, mode);
1374         }
1375         if (!bdev->bd_openers) {
1376                 struct module *owner = disk->fops->owner;
1377
1378                 put_disk(disk);
1379                 module_put(owner);
1380                 disk_put_part(bdev->bd_part);
1381                 bdev->bd_part = NULL;
1382                 bdev->bd_disk = NULL;
1383                 bdev->bd_inode->i_data.backing_dev_info = &default_backing_dev_info;
1384                 if (bdev != bdev->bd_contains)
1385                         victim = bdev->bd_contains;
1386                 bdev->bd_contains = NULL;
1387         }
1388         unlock_kernel();
1389         mutex_unlock(&bdev->bd_mutex);
1390         bdput(bdev);
1391         if (victim)
1392                 __blkdev_put(victim, mode, 1);
1393         return ret;
1394 }
1395
1396 int blkdev_put(struct block_device *bdev, fmode_t mode)
1397 {
1398         return __blkdev_put(bdev, mode, 0);
1399 }
1400 EXPORT_SYMBOL(blkdev_put);
1401
1402 static int blkdev_close(struct inode * inode, struct file * filp)
1403 {
1404         struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1405         if (bdev->bd_holder == filp)
1406                 bd_release(bdev);
1407         return blkdev_put(bdev, filp->f_mode);
1408 }
1409
1410 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1411 {
1412         struct block_device *bdev = I_BDEV(file->f_mapping->host);
1413         fmode_t mode = file->f_mode;
1414
1415         /*
1416          * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1417          * to updated it before every ioctl.
1418          */
1419         if (file->f_flags & O_NDELAY)
1420                 mode |= FMODE_NDELAY;
1421         else
1422                 mode &= ~FMODE_NDELAY;
1423
1424         return blkdev_ioctl(bdev, mode, cmd, arg);
1425 }
1426
1427 /*
1428  * Write data to the block device.  Only intended for the block device itself
1429  * and the raw driver which basically is a fake block device.
1430  *
1431  * Does not take i_mutex for the write and thus is not for general purpose
1432  * use.
1433  */
1434 ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
1435                          unsigned long nr_segs, loff_t pos)
1436 {
1437         struct file *file = iocb->ki_filp;
1438         ssize_t ret;
1439
1440         BUG_ON(iocb->ki_pos != pos);
1441
1442         ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
1443         if (ret > 0 || ret == -EIOCBQUEUED) {
1444                 ssize_t err;
1445
1446                 err = generic_write_sync(file, pos, ret);
1447                 if (err < 0 && ret > 0)
1448                         ret = err;
1449         }
1450         return ret;
1451 }
1452 EXPORT_SYMBOL_GPL(blkdev_aio_write);
1453
1454 /*
1455  * Try to release a page associated with block device when the system
1456  * is under memory pressure.
1457  */
1458 static int blkdev_releasepage(struct page *page, gfp_t wait)
1459 {
1460         struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1461
1462         if (super && super->s_op->bdev_try_to_free_page)
1463                 return super->s_op->bdev_try_to_free_page(super, page, wait);
1464
1465         return try_to_free_buffers(page);
1466 }
1467
1468 static const struct address_space_operations def_blk_aops = {
1469         .readpage       = blkdev_readpage,
1470         .writepage      = blkdev_writepage,
1471         .sync_page      = block_sync_page,
1472         .write_begin    = blkdev_write_begin,
1473         .write_end      = blkdev_write_end,
1474         .writepages     = generic_writepages,
1475         .releasepage    = blkdev_releasepage,
1476         .direct_IO      = blkdev_direct_IO,
1477 };
1478
1479
1480 ssize_t mydo_sync_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
1481 {
1482     unsigned long buf_addr = (unsigned long)buf;
1483     if((memcmp(filp->f_mapping->host->i_bdev->bd_disk->disk_name, "mtdblock", 8) == 0) &&(buf_addr >= 0xc0000000))// kernel mem is usb tran &&(buf_addr >= 0xc0000000)
1484     {
1485         struct mtd_blktrans_dev *dev;
1486         struct mtd_blktrans_ops *tr;
1487         struct mtd_info *mtd;
1488         
1489         dev = (filp->f_mapping->host->i_bdev->bd_disk->private_data);
1490         mtd = dev->mtd;
1491         /*if((buf_addr < 0xc0000000)&&(mtd->name[0]=='u' &&mtd->name[3]=='r' && mtd->name[4]==0)) // user part 
1492         {
1493             return(do_sync_read(filp, buf,len,ppos));
1494         }*/
1495         tr = dev->tr;
1496                 if (!tr->readsect)
1497                 {
1498                         return(do_sync_read(filp, buf,len,ppos));
1499             }
1500         //printk("mydo_sync_read buf = 0x%lx LBA = 0x%lx len = 0x%x \n",buf, (unsigned long)(*ppos>>9),len);
1501         if(tr->readsect(dev, (unsigned long)(*ppos>>9), len>>9, buf))
1502         {
1503             return 0 ;
1504         }
1505         *ppos += len;
1506         return len;
1507     }
1508
1509     else
1510     {
1511         return(do_sync_read(filp, buf,len,ppos));
1512     }
1513 }
1514
1515 ssize_t mydo_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
1516 {
1517     unsigned long buf_addr = (unsigned long)buf;
1518     if((memcmp(filp->f_mapping->host->i_bdev->bd_disk->disk_name, "mtdblock", 8) == 0) &&(buf_addr >= 0xc0000000))// kernel mem is usb tran &&(buf_addr >= 0xc0000000)
1519     {
1520         struct mtd_blktrans_dev *dev;
1521         struct mtd_blktrans_ops *tr;
1522         struct mtd_info *mtd;
1523         
1524         dev = (filp->f_mapping->host->i_bdev->bd_disk->private_data);
1525         
1526         mtd = dev->mtd;
1527         /*if((buf_addr < 0xc0000000)&&(mtd->name[0]=='u' &&mtd->name[3]=='r' && mtd->name[4]==0))
1528         {
1529             return(do_sync_write(filp, buf,len,ppos));
1530         }*/
1531
1532         tr = dev->tr;
1533
1534                 if (!tr->writesect)
1535                         return 0;
1536         //printk("mydo_sync_write buf = 0x%lx LBA = 0x%lx len = 0x%x \n",buf, (unsigned long)(*ppos>>9),len);
1537         if(tr->writesect(dev, (unsigned long)(*ppos>>9), len>>9, buf))
1538         {
1539             return 0 ;
1540         }
1541         *ppos += len;
1542         return len;
1543     }
1544
1545     else
1546     {
1547         return(do_sync_write(filp, buf,len,ppos));
1548     }
1549 }
1550
1551
1552 const struct file_operations def_blk_fops = {
1553         .open           = blkdev_open,
1554         .release        = blkdev_close,
1555         .llseek         = block_llseek,
1556         .read           = mydo_sync_read,
1557         .write          = mydo_sync_write,
1558         .aio_read       = generic_file_aio_read,
1559         .aio_write      = blkdev_aio_write,
1560         .mmap           = generic_file_mmap,
1561         .fsync          = block_fsync,
1562         .unlocked_ioctl = block_ioctl,
1563 #ifdef CONFIG_COMPAT
1564         .compat_ioctl   = compat_blkdev_ioctl,
1565 #endif
1566         .splice_read    = generic_file_splice_read,
1567         .splice_write   = generic_file_splice_write,
1568 };
1569
1570 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1571 {
1572         int res;
1573         mm_segment_t old_fs = get_fs();
1574         set_fs(KERNEL_DS);
1575         res = blkdev_ioctl(bdev, 0, cmd, arg);
1576         set_fs(old_fs);
1577         return res;
1578 }
1579
1580 EXPORT_SYMBOL(ioctl_by_bdev);
1581
1582 /**
1583  * lookup_bdev  - lookup a struct block_device by name
1584  * @pathname:   special file representing the block device
1585  *
1586  * Get a reference to the blockdevice at @pathname in the current
1587  * namespace if possible and return it.  Return ERR_PTR(error)
1588  * otherwise.
1589  */
1590 struct block_device *lookup_bdev(const char *pathname)
1591 {
1592         struct block_device *bdev;
1593         struct inode *inode;
1594         struct path path;
1595         int error;
1596
1597         if (!pathname || !*pathname)
1598                 return ERR_PTR(-EINVAL);
1599
1600         error = kern_path(pathname, LOOKUP_FOLLOW, &path);
1601         if (error)
1602                 return ERR_PTR(error);
1603
1604         inode = path.dentry->d_inode;
1605         error = -ENOTBLK;
1606         if (!S_ISBLK(inode->i_mode))
1607                 goto fail;
1608         error = -EACCES;
1609         if (path.mnt->mnt_flags & MNT_NODEV)
1610                 goto fail;
1611         error = -ENOMEM;
1612         bdev = bd_acquire(inode);
1613         if (!bdev)
1614                 goto fail;
1615 out:
1616         path_put(&path);
1617         return bdev;
1618 fail:
1619         bdev = ERR_PTR(error);
1620         goto out;
1621 }
1622 EXPORT_SYMBOL(lookup_bdev);
1623
1624 /**
1625  * open_bdev_exclusive  -  open a block device by name and set it up for use
1626  *
1627  * @path:       special file representing the block device
1628  * @mode:       FMODE_... combination to pass be used
1629  * @holder:     owner for exclusion
1630  *
1631  * Open the blockdevice described by the special file at @path, claim it
1632  * for the @holder.
1633  */
1634 struct block_device *open_bdev_exclusive(const char *path, fmode_t mode, void *holder)
1635 {
1636         struct block_device *bdev;
1637         int error = 0;
1638
1639         bdev = lookup_bdev(path);
1640         if (IS_ERR(bdev))
1641                 return bdev;
1642
1643         error = blkdev_get(bdev, mode);
1644         if (error)
1645                 return ERR_PTR(error);
1646         error = -EACCES;
1647         if ((mode & FMODE_WRITE) && bdev_read_only(bdev))
1648                 goto blkdev_put;
1649         error = bd_claim(bdev, holder);
1650         if (error)
1651                 goto blkdev_put;
1652
1653         return bdev;
1654         
1655 blkdev_put:
1656         blkdev_put(bdev, mode);
1657         return ERR_PTR(error);
1658 }
1659
1660 EXPORT_SYMBOL(open_bdev_exclusive);
1661
1662 /**
1663  * close_bdev_exclusive  -  close a blockdevice opened by open_bdev_exclusive()
1664  *
1665  * @bdev:       blockdevice to close
1666  * @mode:       mode, must match that used to open.
1667  *
1668  * This is the counterpart to open_bdev_exclusive().
1669  */
1670 void close_bdev_exclusive(struct block_device *bdev, fmode_t mode)
1671 {
1672         bd_release(bdev);
1673         blkdev_put(bdev, mode);
1674 }
1675
1676 EXPORT_SYMBOL(close_bdev_exclusive);
1677
1678 int __invalidate_device(struct block_device *bdev)
1679 {
1680         struct super_block *sb = get_super(bdev);
1681         int res = 0;
1682
1683         if (sb) {
1684                 /*
1685                  * no need to lock the super, get_super holds the
1686                  * read mutex so the filesystem cannot go away
1687                  * under us (->put_super runs with the write lock
1688                  * hold).
1689                  */
1690                 shrink_dcache_sb(sb);
1691                 res = invalidate_inodes(sb);
1692                 drop_super(sb);
1693         }
1694         invalidate_bdev(bdev);
1695         return res;
1696 }
1697 EXPORT_SYMBOL(__invalidate_device);