12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021 |
- #include <linux/init.h>
- #include <linux/mm.h>
- #include <linux/fcntl.h>
- #include <linux/slab.h>
- #include <linux/kmod.h>
- #include <linux/major.h>
- #include <linux/device_cgroup.h>
- #include <linux/highmem.h>
- #include <linux/blkdev.h>
- #include <linux/backing-dev.h>
- #include <linux/module.h>
- #include <linux/blkpg.h>
- #include <linux/magic.h>
- #include <linux/buffer_head.h>
- #include <linux/swap.h>
- #include <linux/pagevec.h>
- #include <linux/writeback.h>
- #include <linux/mpage.h>
- #include <linux/mount.h>
- #include <linux/uio.h>
- #include <linux/namei.h>
- #include <linux/log2.h>
- #include <linux/cleancache.h>
- #include <linux/dax.h>
- #include <linux/badblocks.h>
- #include <linux/falloc.h>
- #include <asm/uaccess.h>
- #include "internal.h"
- struct bdev_inode {
- struct block_device bdev;
- struct inode vfs_inode;
- };
- static const struct address_space_operations def_blk_aops;
- static inline struct bdev_inode *BDEV_I(struct inode *inode)
- {
- return container_of(inode, struct bdev_inode, vfs_inode);
- }
- struct block_device *I_BDEV(struct inode *inode)
- {
- return &BDEV_I(inode)->bdev;
- }
- EXPORT_SYMBOL(I_BDEV);
- void __vfs_msg(struct super_block *sb, const char *prefix, const char *fmt, ...)
- {
- struct va_format vaf;
- va_list args;
- va_start(args, fmt);
- vaf.fmt = fmt;
- vaf.va = &args;
- printk_ratelimited("%sVFS (%s): %pV\n", prefix, sb->s_id, &vaf);
- va_end(args);
- }
- static void bdev_write_inode(struct block_device *bdev)
- {
- struct inode *inode = bdev->bd_inode;
- int ret;
- spin_lock(&inode->i_lock);
- while (inode->i_state & I_DIRTY) {
- spin_unlock(&inode->i_lock);
- ret = write_inode_now(inode, true);
- if (ret) {
- char name[BDEVNAME_SIZE];
- pr_warn_ratelimited("VFS: Dirty inode writeback failed "
- "for block device %s (err=%d).\n",
- bdevname(bdev, name), ret);
- }
- spin_lock(&inode->i_lock);
- }
- spin_unlock(&inode->i_lock);
- }
- void kill_bdev(struct block_device *bdev)
- {
- struct address_space *mapping = bdev->bd_inode->i_mapping;
- if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
- return;
- invalidate_bh_lrus();
- truncate_inode_pages(mapping, 0);
- }
- EXPORT_SYMBOL(kill_bdev);
- void invalidate_bdev(struct block_device *bdev)
- {
- struct address_space *mapping = bdev->bd_inode->i_mapping;
- if (mapping->nrpages) {
- invalidate_bh_lrus();
- lru_add_drain_all();
- invalidate_mapping_pages(mapping, 0, -1);
- }
-
- cleancache_invalidate_inode(mapping);
- }
- EXPORT_SYMBOL(invalidate_bdev);
- static void set_init_blocksize(struct block_device *bdev)
- {
- unsigned bsize = bdev_logical_block_size(bdev);
- loff_t size = i_size_read(bdev->bd_inode);
- while (bsize < PAGE_SIZE) {
- if (size & bsize)
- break;
- bsize <<= 1;
- }
- bdev->bd_block_size = bsize;
- bdev->bd_inode->i_blkbits = blksize_bits(bsize);
- }
- int set_blocksize(struct block_device *bdev, int size)
- {
-
- if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
- return -EINVAL;
-
- if (size < bdev_logical_block_size(bdev))
- return -EINVAL;
-
- if (bdev->bd_block_size != size) {
- sync_blockdev(bdev);
- bdev->bd_block_size = size;
- bdev->bd_inode->i_blkbits = blksize_bits(size);
- kill_bdev(bdev);
- }
- return 0;
- }
- EXPORT_SYMBOL(set_blocksize);
- int sb_set_blocksize(struct super_block *sb, int size)
- {
- if (set_blocksize(sb->s_bdev, size))
- return 0;
-
- sb->s_blocksize = size;
- sb->s_blocksize_bits = blksize_bits(size);
- return sb->s_blocksize;
- }
- EXPORT_SYMBOL(sb_set_blocksize);
- int sb_min_blocksize(struct super_block *sb, int size)
- {
- int minsize = bdev_logical_block_size(sb->s_bdev);
- if (size < minsize)
- size = minsize;
- return sb_set_blocksize(sb, size);
- }
- EXPORT_SYMBOL(sb_min_blocksize);
- static int
- blkdev_get_block(struct inode *inode, sector_t iblock,
- struct buffer_head *bh, int create)
- {
- bh->b_bdev = I_BDEV(inode);
- bh->b_blocknr = iblock;
- set_buffer_mapped(bh);
- return 0;
- }
- static struct inode *bdev_file_inode(struct file *file)
- {
- return file->f_mapping->host;
- }
- static ssize_t
- blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
- {
- struct file *file = iocb->ki_filp;
- struct inode *inode = bdev_file_inode(file);
- return __blockdev_direct_IO(iocb, inode, I_BDEV(inode), iter,
- blkdev_get_block, NULL, NULL,
- DIO_SKIP_DIO_COUNT);
- }
- int __sync_blockdev(struct block_device *bdev, int wait)
- {
- if (!bdev)
- return 0;
- if (!wait)
- return filemap_flush(bdev->bd_inode->i_mapping);
- return filemap_write_and_wait(bdev->bd_inode->i_mapping);
- }
- int sync_blockdev(struct block_device *bdev)
- {
- return __sync_blockdev(bdev, 1);
- }
- EXPORT_SYMBOL(sync_blockdev);
- int fsync_bdev(struct block_device *bdev)
- {
- struct super_block *sb = get_super(bdev);
- if (sb) {
- int res = sync_filesystem(sb);
- drop_super(sb);
- return res;
- }
- return sync_blockdev(bdev);
- }
- EXPORT_SYMBOL(fsync_bdev);
- struct super_block *freeze_bdev(struct block_device *bdev)
- {
- struct super_block *sb;
- int error = 0;
- mutex_lock(&bdev->bd_fsfreeze_mutex);
- if (++bdev->bd_fsfreeze_count > 1) {
-
- sb = get_super(bdev);
- if (sb)
- drop_super(sb);
- mutex_unlock(&bdev->bd_fsfreeze_mutex);
- return sb;
- }
- sb = get_active_super(bdev);
- if (!sb)
- goto out;
- if (sb->s_op->freeze_super)
- error = sb->s_op->freeze_super(sb);
- else
- error = freeze_super(sb);
- if (error) {
- deactivate_super(sb);
- bdev->bd_fsfreeze_count--;
- mutex_unlock(&bdev->bd_fsfreeze_mutex);
- return ERR_PTR(error);
- }
- deactivate_super(sb);
- out:
- sync_blockdev(bdev);
- mutex_unlock(&bdev->bd_fsfreeze_mutex);
- return sb;
- }
- EXPORT_SYMBOL(freeze_bdev);
- int thaw_bdev(struct block_device *bdev, struct super_block *sb)
- {
- int error = -EINVAL;
- mutex_lock(&bdev->bd_fsfreeze_mutex);
- if (!bdev->bd_fsfreeze_count)
- goto out;
- error = 0;
- if (--bdev->bd_fsfreeze_count > 0)
- goto out;
- if (!sb)
- goto out;
- if (sb->s_op->thaw_super)
- error = sb->s_op->thaw_super(sb);
- else
- error = thaw_super(sb);
- if (error)
- bdev->bd_fsfreeze_count++;
- out:
- mutex_unlock(&bdev->bd_fsfreeze_mutex);
- return error;
- }
- EXPORT_SYMBOL(thaw_bdev);
- static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
- {
- return block_write_full_page(page, blkdev_get_block, wbc);
- }
- static int blkdev_readpage(struct file * file, struct page * page)
- {
- return block_read_full_page(page, blkdev_get_block);
- }
- static int blkdev_readpages(struct file *file, struct address_space *mapping,
- struct list_head *pages, unsigned nr_pages)
- {
- return mpage_readpages(mapping, pages, nr_pages, blkdev_get_block);
- }
- static int blkdev_write_begin(struct file *file, struct address_space *mapping,
- loff_t pos, unsigned len, unsigned flags,
- struct page **pagep, void **fsdata)
- {
- return block_write_begin(mapping, pos, len, flags, pagep,
- blkdev_get_block);
- }
- static int blkdev_write_end(struct file *file, struct address_space *mapping,
- loff_t pos, unsigned len, unsigned copied,
- struct page *page, void *fsdata)
- {
- int ret;
- ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
- unlock_page(page);
- put_page(page);
- return ret;
- }
- static loff_t block_llseek(struct file *file, loff_t offset, int whence)
- {
- struct inode *bd_inode = bdev_file_inode(file);
- loff_t retval;
- inode_lock(bd_inode);
- retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
- inode_unlock(bd_inode);
- return retval;
- }
-
- int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
- {
- struct inode *bd_inode = bdev_file_inode(filp);
- struct block_device *bdev = I_BDEV(bd_inode);
- int error;
-
- error = filemap_write_and_wait_range(filp->f_mapping, start, end);
- if (error)
- return error;
-
- error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
- if (error == -EOPNOTSUPP)
- error = 0;
- return error;
- }
- EXPORT_SYMBOL(blkdev_fsync);
- int bdev_read_page(struct block_device *bdev, sector_t sector,
- struct page *page)
- {
- const struct block_device_operations *ops = bdev->bd_disk->fops;
- int result = -EOPNOTSUPP;
- if (!ops->rw_page || bdev_get_integrity(bdev))
- return result;
- result = blk_queue_enter(bdev->bd_queue, false);
- if (result)
- return result;
- result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, false);
- blk_queue_exit(bdev->bd_queue);
- return result;
- }
- EXPORT_SYMBOL_GPL(bdev_read_page);
- int bdev_write_page(struct block_device *bdev, sector_t sector,
- struct page *page, struct writeback_control *wbc)
- {
- int result;
- const struct block_device_operations *ops = bdev->bd_disk->fops;
- if (!ops->rw_page || bdev_get_integrity(bdev))
- return -EOPNOTSUPP;
- result = blk_queue_enter(bdev->bd_queue, false);
- if (result)
- return result;
- set_page_writeback(page);
- result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, true);
- if (result) {
- end_page_writeback(page);
- } else {
- clean_page_buffers(page);
- unlock_page(page);
- }
- blk_queue_exit(bdev->bd_queue);
- return result;
- }
- EXPORT_SYMBOL_GPL(bdev_write_page);
- long bdev_direct_access(struct block_device *bdev, struct blk_dax_ctl *dax)
- {
- sector_t sector = dax->sector;
- long avail, size = dax->size;
- const struct block_device_operations *ops = bdev->bd_disk->fops;
-
- might_sleep();
- if (size < 0)
- return size;
- if (!blk_queue_dax(bdev_get_queue(bdev)) || !ops->direct_access)
- return -EOPNOTSUPP;
- if ((sector + DIV_ROUND_UP(size, 512)) >
- part_nr_sects_read(bdev->bd_part))
- return -ERANGE;
- sector += get_start_sect(bdev);
- if (sector % (PAGE_SIZE / 512))
- return -EINVAL;
- avail = ops->direct_access(bdev, sector, &dax->addr, &dax->pfn, size);
- if (!avail)
- return -ERANGE;
- if (avail > 0 && avail & ~PAGE_MASK)
- return -ENXIO;
- return min(avail, size);
- }
- EXPORT_SYMBOL_GPL(bdev_direct_access);
- int bdev_dax_supported(struct super_block *sb, int blocksize)
- {
- struct blk_dax_ctl dax = {
- .sector = 0,
- .size = PAGE_SIZE,
- };
- int err;
- if (blocksize != PAGE_SIZE) {
- vfs_msg(sb, KERN_ERR, "error: unsupported blocksize for dax");
- return -EINVAL;
- }
- err = bdev_direct_access(sb->s_bdev, &dax);
- if (err < 0) {
- switch (err) {
- case -EOPNOTSUPP:
- vfs_msg(sb, KERN_ERR,
- "error: device does not support dax");
- break;
- case -EINVAL:
- vfs_msg(sb, KERN_ERR,
- "error: unaligned partition for dax");
- break;
- default:
- vfs_msg(sb, KERN_ERR,
- "error: dax access failed (%d)", err);
- }
- return err;
- }
- return 0;
- }
- EXPORT_SYMBOL_GPL(bdev_dax_supported);
- bool bdev_dax_capable(struct block_device *bdev)
- {
- struct blk_dax_ctl dax = {
- .size = PAGE_SIZE,
- };
- if (!IS_ENABLED(CONFIG_FS_DAX))
- return false;
- dax.sector = 0;
- if (bdev_direct_access(bdev, &dax) < 0)
- return false;
- dax.sector = bdev->bd_part->nr_sects - (PAGE_SIZE / 512);
- if (bdev_direct_access(bdev, &dax) < 0)
- return false;
- return true;
- }
- static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
- static struct kmem_cache * bdev_cachep __read_mostly;
- static struct inode *bdev_alloc_inode(struct super_block *sb)
- {
- struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
- if (!ei)
- return NULL;
- return &ei->vfs_inode;
- }
- static void bdev_i_callback(struct rcu_head *head)
- {
- struct inode *inode = container_of(head, struct inode, i_rcu);
- struct bdev_inode *bdi = BDEV_I(inode);
- kmem_cache_free(bdev_cachep, bdi);
- }
- static void bdev_destroy_inode(struct inode *inode)
- {
- call_rcu(&inode->i_rcu, bdev_i_callback);
- }
- static void init_once(void *foo)
- {
- struct bdev_inode *ei = (struct bdev_inode *) foo;
- struct block_device *bdev = &ei->bdev;
- memset(bdev, 0, sizeof(*bdev));
- mutex_init(&bdev->bd_mutex);
- INIT_LIST_HEAD(&bdev->bd_list);
- #ifdef CONFIG_SYSFS
- INIT_LIST_HEAD(&bdev->bd_holder_disks);
- #endif
- bdev->bd_bdi = &noop_backing_dev_info;
- inode_init_once(&ei->vfs_inode);
-
- mutex_init(&bdev->bd_fsfreeze_mutex);
- }
- static void bdev_evict_inode(struct inode *inode)
- {
- struct block_device *bdev = &BDEV_I(inode)->bdev;
- truncate_inode_pages_final(&inode->i_data);
- invalidate_inode_buffers(inode);
- clear_inode(inode);
- spin_lock(&bdev_lock);
- list_del_init(&bdev->bd_list);
- spin_unlock(&bdev_lock);
-
- inode_detach_wb(inode);
- if (bdev->bd_bdi != &noop_backing_dev_info) {
- bdi_put(bdev->bd_bdi);
- bdev->bd_bdi = &noop_backing_dev_info;
- }
- }
- static const struct super_operations bdev_sops = {
- .statfs = simple_statfs,
- .alloc_inode = bdev_alloc_inode,
- .destroy_inode = bdev_destroy_inode,
- .drop_inode = generic_delete_inode,
- .evict_inode = bdev_evict_inode,
- };
- static struct dentry *bd_mount(struct file_system_type *fs_type,
- int flags, const char *dev_name, void *data)
- {
- struct dentry *dent;
- dent = mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, BDEVFS_MAGIC);
- if (!IS_ERR(dent))
- dent->d_sb->s_iflags |= SB_I_CGROUPWB;
- return dent;
- }
- static struct file_system_type bd_type = {
- .name = "bdev",
- .mount = bd_mount,
- .kill_sb = kill_anon_super,
- };
- struct super_block *blockdev_superblock __read_mostly;
- EXPORT_SYMBOL_GPL(blockdev_superblock);
- void __init bdev_cache_init(void)
- {
- int err;
- static struct vfsmount *bd_mnt;
- bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
- 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
- SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
- init_once);
- err = register_filesystem(&bd_type);
- if (err)
- panic("Cannot register bdev pseudo-fs");
- bd_mnt = kern_mount(&bd_type);
- if (IS_ERR(bd_mnt))
- panic("Cannot create bdev pseudo-fs");
- blockdev_superblock = bd_mnt->mnt_sb;
- }
- static inline unsigned long hash(dev_t dev)
- {
- return MAJOR(dev)+MINOR(dev);
- }
- static int bdev_test(struct inode *inode, void *data)
- {
- return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
- }
- static int bdev_set(struct inode *inode, void *data)
- {
- BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
- return 0;
- }
- static LIST_HEAD(all_bdevs);
- void bdev_unhash_inode(dev_t dev)
- {
- struct inode *inode;
- inode = ilookup5(blockdev_superblock, hash(dev), bdev_test, &dev);
- if (inode) {
- remove_inode_hash(inode);
- iput(inode);
- }
- }
- struct block_device *bdget(dev_t dev)
- {
- struct block_device *bdev;
- struct inode *inode;
- inode = iget5_locked(blockdev_superblock, hash(dev),
- bdev_test, bdev_set, &dev);
- if (!inode)
- return NULL;
- bdev = &BDEV_I(inode)->bdev;
- if (inode->i_state & I_NEW) {
- bdev->bd_contains = NULL;
- bdev->bd_super = NULL;
- bdev->bd_inode = inode;
- bdev->bd_block_size = i_blocksize(inode);
- bdev->bd_part_count = 0;
- bdev->bd_invalidated = 0;
- inode->i_mode = S_IFBLK;
- inode->i_rdev = dev;
- inode->i_bdev = bdev;
- inode->i_data.a_ops = &def_blk_aops;
- mapping_set_gfp_mask(&inode->i_data, GFP_USER);
- spin_lock(&bdev_lock);
- list_add(&bdev->bd_list, &all_bdevs);
- spin_unlock(&bdev_lock);
- unlock_new_inode(inode);
- }
- return bdev;
- }
- EXPORT_SYMBOL(bdget);
- struct block_device *bdgrab(struct block_device *bdev)
- {
- ihold(bdev->bd_inode);
- return bdev;
- }
- EXPORT_SYMBOL(bdgrab);
- long nr_blockdev_pages(void)
- {
- struct block_device *bdev;
- long ret = 0;
- spin_lock(&bdev_lock);
- list_for_each_entry(bdev, &all_bdevs, bd_list) {
- ret += bdev->bd_inode->i_mapping->nrpages;
- }
- spin_unlock(&bdev_lock);
- return ret;
- }
- void bdput(struct block_device *bdev)
- {
- iput(bdev->bd_inode);
- }
- EXPORT_SYMBOL(bdput);
-
- static struct block_device *bd_acquire(struct inode *inode)
- {
- struct block_device *bdev;
- spin_lock(&bdev_lock);
- bdev = inode->i_bdev;
- if (bdev && !inode_unhashed(bdev->bd_inode)) {
- bdgrab(bdev);
- spin_unlock(&bdev_lock);
- return bdev;
- }
- spin_unlock(&bdev_lock);
-
- if (bdev)
- bd_forget(inode);
- bdev = bdget(inode->i_rdev);
- if (bdev) {
- spin_lock(&bdev_lock);
- if (!inode->i_bdev) {
-
- bdgrab(bdev);
- inode->i_bdev = bdev;
- inode->i_mapping = bdev->bd_inode->i_mapping;
- }
- spin_unlock(&bdev_lock);
- }
- return bdev;
- }
- void bd_forget(struct inode *inode)
- {
- struct block_device *bdev = NULL;
- spin_lock(&bdev_lock);
- if (!sb_is_blkdev_sb(inode->i_sb))
- bdev = inode->i_bdev;
- inode->i_bdev = NULL;
- inode->i_mapping = &inode->i_data;
- spin_unlock(&bdev_lock);
- if (bdev)
- bdput(bdev);
- }
- static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
- void *holder)
- {
- if (bdev->bd_holder == holder)
- return true;
- else if (bdev->bd_holder != NULL)
- return false;
- else if (whole == bdev)
- return true;
- else if (whole->bd_holder == bd_may_claim)
- return true;
- else if (whole->bd_holder != NULL)
- return false;
- else
- return true;
- }
- static int bd_prepare_to_claim(struct block_device *bdev,
- struct block_device *whole, void *holder)
- {
- retry:
-
- if (!bd_may_claim(bdev, whole, holder))
- return -EBUSY;
-
- if (whole->bd_claiming) {
- wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
- DEFINE_WAIT(wait);
- prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
- spin_unlock(&bdev_lock);
- schedule();
- finish_wait(wq, &wait);
- spin_lock(&bdev_lock);
- goto retry;
- }
-
- return 0;
- }
- static struct block_device *bd_start_claiming(struct block_device *bdev,
- void *holder)
- {
- struct gendisk *disk;
- struct block_device *whole;
- int partno, err;
- might_sleep();
-
- disk = get_gendisk(bdev->bd_dev, &partno);
- if (!disk)
- return ERR_PTR(-ENXIO);
-
- if (partno)
- whole = bdget_disk(disk, 0);
- else
- whole = bdgrab(bdev);
- module_put(disk->fops->owner);
- put_disk(disk);
- if (!whole)
- return ERR_PTR(-ENOMEM);
-
- spin_lock(&bdev_lock);
- err = bd_prepare_to_claim(bdev, whole, holder);
- if (err == 0) {
- whole->bd_claiming = holder;
- spin_unlock(&bdev_lock);
- return whole;
- } else {
- spin_unlock(&bdev_lock);
- bdput(whole);
- return ERR_PTR(err);
- }
- }
- #ifdef CONFIG_SYSFS
- struct bd_holder_disk {
- struct list_head list;
- struct gendisk *disk;
- int refcnt;
- };
- static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
- struct gendisk *disk)
- {
- struct bd_holder_disk *holder;
- list_for_each_entry(holder, &bdev->bd_holder_disks, list)
- if (holder->disk == disk)
- return holder;
- return NULL;
- }
- static int add_symlink(struct kobject *from, struct kobject *to)
- {
- return sysfs_create_link(from, to, kobject_name(to));
- }
- static void del_symlink(struct kobject *from, struct kobject *to)
- {
- sysfs_remove_link(from, kobject_name(to));
- }
- int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
- {
- struct bd_holder_disk *holder;
- int ret = 0;
- mutex_lock(&bdev->bd_mutex);
- WARN_ON_ONCE(!bdev->bd_holder);
-
- if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
- goto out_unlock;
- holder = bd_find_holder_disk(bdev, disk);
- if (holder) {
- holder->refcnt++;
- goto out_unlock;
- }
- holder = kzalloc(sizeof(*holder), GFP_KERNEL);
- if (!holder) {
- ret = -ENOMEM;
- goto out_unlock;
- }
- INIT_LIST_HEAD(&holder->list);
- holder->disk = disk;
- holder->refcnt = 1;
- ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
- if (ret)
- goto out_free;
- ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
- if (ret)
- goto out_del;
-
- kobject_get(bdev->bd_part->holder_dir);
- list_add(&holder->list, &bdev->bd_holder_disks);
- goto out_unlock;
- out_del:
- del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
- out_free:
- kfree(holder);
- out_unlock:
- mutex_unlock(&bdev->bd_mutex);
- return ret;
- }
- EXPORT_SYMBOL_GPL(bd_link_disk_holder);
- void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
- {
- struct bd_holder_disk *holder;
- mutex_lock(&bdev->bd_mutex);
- holder = bd_find_holder_disk(bdev, disk);
- if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
- del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
- del_symlink(bdev->bd_part->holder_dir,
- &disk_to_dev(disk)->kobj);
- kobject_put(bdev->bd_part->holder_dir);
- list_del_init(&holder->list);
- kfree(holder);
- }
- mutex_unlock(&bdev->bd_mutex);
- }
- EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
- #endif
- static void flush_disk(struct block_device *bdev, bool kill_dirty)
- {
- if (__invalidate_device(bdev, kill_dirty)) {
- printk(KERN_WARNING "VFS: busy inodes on changed media or "
- "resized disk %s\n",
- bdev->bd_disk ? bdev->bd_disk->disk_name : "");
- }
- if (!bdev->bd_disk)
- return;
- if (disk_part_scan_enabled(bdev->bd_disk))
- bdev->bd_invalidated = 1;
- }
- void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
- {
- loff_t disk_size, bdev_size;
- disk_size = (loff_t)get_capacity(disk) << 9;
- bdev_size = i_size_read(bdev->bd_inode);
- if (disk_size != bdev_size) {
- printk(KERN_INFO
- "%s: detected capacity change from %lld to %lld\n",
- disk->disk_name, bdev_size, disk_size);
- i_size_write(bdev->bd_inode, disk_size);
- flush_disk(bdev, false);
- }
- }
- EXPORT_SYMBOL(check_disk_size_change);
- int revalidate_disk(struct gendisk *disk)
- {
- struct block_device *bdev;
- int ret = 0;
- if (disk->fops->revalidate_disk)
- ret = disk->fops->revalidate_disk(disk);
- bdev = bdget_disk(disk, 0);
- if (!bdev)
- return ret;
- mutex_lock(&bdev->bd_mutex);
- check_disk_size_change(disk, bdev);
- bdev->bd_invalidated = 0;
- mutex_unlock(&bdev->bd_mutex);
- bdput(bdev);
- return ret;
- }
- EXPORT_SYMBOL(revalidate_disk);
- int check_disk_change(struct block_device *bdev)
- {
- struct gendisk *disk = bdev->bd_disk;
- const struct block_device_operations *bdops = disk->fops;
- unsigned int events;
- events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
- DISK_EVENT_EJECT_REQUEST);
- if (!(events & DISK_EVENT_MEDIA_CHANGE))
- return 0;
- flush_disk(bdev, true);
- if (bdops->revalidate_disk)
- bdops->revalidate_disk(bdev->bd_disk);
- return 1;
- }
- EXPORT_SYMBOL(check_disk_change);
- void bd_set_size(struct block_device *bdev, loff_t size)
- {
- inode_lock(bdev->bd_inode);
- i_size_write(bdev->bd_inode, size);
- inode_unlock(bdev->bd_inode);
- }
- EXPORT_SYMBOL(bd_set_size);
- static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
- static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
- {
- struct gendisk *disk;
- struct module *owner;
- int ret;
- int partno;
- int perm = 0;
- if (mode & FMODE_READ)
- perm |= MAY_READ;
- if (mode & FMODE_WRITE)
- perm |= MAY_WRITE;
-
- if (!for_part) {
- ret = devcgroup_inode_permission(bdev->bd_inode, perm);
- if (ret != 0) {
- bdput(bdev);
- return ret;
- }
- }
- restart:
- ret = -ENXIO;
- disk = get_gendisk(bdev->bd_dev, &partno);
- if (!disk)
- goto out;
- owner = disk->fops->owner;
- disk_block_events(disk);
- mutex_lock_nested(&bdev->bd_mutex, for_part);
- if (!bdev->bd_openers) {
- bdev->bd_disk = disk;
- bdev->bd_queue = disk->queue;
- bdev->bd_contains = bdev;
- if (!partno) {
- ret = -ENXIO;
- bdev->bd_part = disk_get_part(disk, partno);
- if (!bdev->bd_part)
- goto out_clear;
- ret = 0;
- if (disk->fops->open) {
- ret = disk->fops->open(bdev, mode);
- if (ret == -ERESTARTSYS) {
-
- disk_put_part(bdev->bd_part);
- bdev->bd_part = NULL;
- bdev->bd_disk = NULL;
- bdev->bd_queue = NULL;
- mutex_unlock(&bdev->bd_mutex);
- disk_unblock_events(disk);
- put_disk(disk);
- module_put(owner);
- goto restart;
- }
- }
- if (!ret) {
- bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
- set_init_blocksize(bdev);
- }
-
- if (bdev->bd_invalidated) {
- if (!ret)
- rescan_partitions(disk, bdev);
- else if (ret == -ENOMEDIUM)
- invalidate_partitions(disk, bdev);
- }
- if (ret)
- goto out_clear;
- } else {
- struct block_device *whole;
- whole = bdget_disk(disk, 0);
- ret = -ENOMEM;
- if (!whole)
- goto out_clear;
- BUG_ON(for_part);
- ret = __blkdev_get(whole, mode, 1);
- if (ret)
- goto out_clear;
- bdev->bd_contains = whole;
- bdev->bd_part = disk_get_part(disk, partno);
- if (!(disk->flags & GENHD_FL_UP) ||
- !bdev->bd_part || !bdev->bd_part->nr_sects) {
- ret = -ENXIO;
- goto out_clear;
- }
- bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
- set_init_blocksize(bdev);
- }
- if (bdev->bd_bdi == &noop_backing_dev_info)
- bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
- } else {
- if (bdev->bd_contains == bdev) {
- ret = 0;
- if (bdev->bd_disk->fops->open)
- ret = bdev->bd_disk->fops->open(bdev, mode);
-
- if (bdev->bd_invalidated) {
- if (!ret)
- rescan_partitions(bdev->bd_disk, bdev);
- else if (ret == -ENOMEDIUM)
- invalidate_partitions(bdev->bd_disk, bdev);
- }
- if (ret)
- goto out_unlock_bdev;
- }
-
- put_disk(disk);
- module_put(owner);
- }
- bdev->bd_openers++;
- if (for_part)
- bdev->bd_part_count++;
- mutex_unlock(&bdev->bd_mutex);
- disk_unblock_events(disk);
- return 0;
- out_clear:
- disk_put_part(bdev->bd_part);
- bdev->bd_disk = NULL;
- bdev->bd_part = NULL;
- bdev->bd_queue = NULL;
- if (bdev != bdev->bd_contains)
- __blkdev_put(bdev->bd_contains, mode, 1);
- bdev->bd_contains = NULL;
- out_unlock_bdev:
- mutex_unlock(&bdev->bd_mutex);
- disk_unblock_events(disk);
- put_disk(disk);
- module_put(owner);
- out:
- bdput(bdev);
- return ret;
- }
- int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
- {
- struct block_device *whole = NULL;
- int res;
- WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
- if ((mode & FMODE_EXCL) && holder) {
- whole = bd_start_claiming(bdev, holder);
- if (IS_ERR(whole)) {
- bdput(bdev);
- return PTR_ERR(whole);
- }
- }
- res = __blkdev_get(bdev, mode, 0);
- if (whole) {
- struct gendisk *disk = whole->bd_disk;
-
- mutex_lock(&bdev->bd_mutex);
- spin_lock(&bdev_lock);
- if (!res) {
- BUG_ON(!bd_may_claim(bdev, whole, holder));
-
- whole->bd_holders++;
- whole->bd_holder = bd_may_claim;
- bdev->bd_holders++;
- bdev->bd_holder = holder;
- }
-
- BUG_ON(whole->bd_claiming != holder);
- whole->bd_claiming = NULL;
- wake_up_bit(&whole->bd_claiming, 0);
- spin_unlock(&bdev_lock);
-
- if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
- (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
- bdev->bd_write_holder = true;
- disk_block_events(disk);
- }
- mutex_unlock(&bdev->bd_mutex);
- bdput(whole);
- }
- return res;
- }
- EXPORT_SYMBOL(blkdev_get);
- struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
- void *holder)
- {
- struct block_device *bdev;
- int err;
- bdev = lookup_bdev(path);
- if (IS_ERR(bdev))
- return bdev;
- err = blkdev_get(bdev, mode, holder);
- if (err)
- return ERR_PTR(err);
- if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
- blkdev_put(bdev, mode);
- return ERR_PTR(-EACCES);
- }
- return bdev;
- }
- EXPORT_SYMBOL(blkdev_get_by_path);
- struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
- {
- struct block_device *bdev;
- int err;
- bdev = bdget(dev);
- if (!bdev)
- return ERR_PTR(-ENOMEM);
- err = blkdev_get(bdev, mode, holder);
- if (err)
- return ERR_PTR(err);
- return bdev;
- }
- EXPORT_SYMBOL(blkdev_get_by_dev);
- static int blkdev_open(struct inode * inode, struct file * filp)
- {
- struct block_device *bdev;
-
- filp->f_flags |= O_LARGEFILE;
- if (filp->f_flags & O_NDELAY)
- filp->f_mode |= FMODE_NDELAY;
- if (filp->f_flags & O_EXCL)
- filp->f_mode |= FMODE_EXCL;
- if ((filp->f_flags & O_ACCMODE) == 3)
- filp->f_mode |= FMODE_WRITE_IOCTL;
- bdev = bd_acquire(inode);
- if (bdev == NULL)
- return -ENOMEM;
- filp->f_mapping = bdev->bd_inode->i_mapping;
- return blkdev_get(bdev, filp->f_mode, filp);
- }
- static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
- {
- struct gendisk *disk = bdev->bd_disk;
- struct block_device *victim = NULL;
- mutex_lock_nested(&bdev->bd_mutex, for_part);
- if (for_part)
- bdev->bd_part_count--;
- if (!--bdev->bd_openers) {
- WARN_ON_ONCE(bdev->bd_holders);
- sync_blockdev(bdev);
- kill_bdev(bdev);
- bdev_write_inode(bdev);
- }
- if (bdev->bd_contains == bdev) {
- if (disk->fops->release)
- disk->fops->release(disk, mode);
- }
- if (!bdev->bd_openers) {
- struct module *owner = disk->fops->owner;
- disk_put_part(bdev->bd_part);
- bdev->bd_part = NULL;
- bdev->bd_disk = NULL;
- if (bdev != bdev->bd_contains)
- victim = bdev->bd_contains;
- bdev->bd_contains = NULL;
- put_disk(disk);
- module_put(owner);
- }
- mutex_unlock(&bdev->bd_mutex);
- bdput(bdev);
- if (victim)
- __blkdev_put(victim, mode, 1);
- }
- void blkdev_put(struct block_device *bdev, fmode_t mode)
- {
- mutex_lock(&bdev->bd_mutex);
- if (mode & FMODE_EXCL) {
- bool bdev_free;
-
- spin_lock(&bdev_lock);
- WARN_ON_ONCE(--bdev->bd_holders < 0);
- WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
-
- if ((bdev_free = !bdev->bd_holders))
- bdev->bd_holder = NULL;
- if (!bdev->bd_contains->bd_holders)
- bdev->bd_contains->bd_holder = NULL;
- spin_unlock(&bdev_lock);
-
- if (bdev_free && bdev->bd_write_holder) {
- disk_unblock_events(bdev->bd_disk);
- bdev->bd_write_holder = false;
- }
- }
-
- disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
- mutex_unlock(&bdev->bd_mutex);
- __blkdev_put(bdev, mode, 0);
- }
- EXPORT_SYMBOL(blkdev_put);
- static int blkdev_close(struct inode * inode, struct file * filp)
- {
- struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
- blkdev_put(bdev, filp->f_mode);
- return 0;
- }
- static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
- {
- struct block_device *bdev = I_BDEV(bdev_file_inode(file));
- fmode_t mode = file->f_mode;
-
- if (file->f_flags & O_NDELAY)
- mode |= FMODE_NDELAY;
- else
- mode &= ~FMODE_NDELAY;
- return blkdev_ioctl(bdev, mode, cmd, arg);
- }
- ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
- {
- struct file *file = iocb->ki_filp;
- struct inode *bd_inode = bdev_file_inode(file);
- loff_t size = i_size_read(bd_inode);
- struct blk_plug plug;
- ssize_t ret;
- if (bdev_read_only(I_BDEV(bd_inode)))
- return -EPERM;
- if (!iov_iter_count(from))
- return 0;
- if (iocb->ki_pos >= size)
- return -ENOSPC;
- iov_iter_truncate(from, size - iocb->ki_pos);
- blk_start_plug(&plug);
- ret = __generic_file_write_iter(iocb, from);
- if (ret > 0)
- ret = generic_write_sync(iocb, ret);
- blk_finish_plug(&plug);
- return ret;
- }
- EXPORT_SYMBOL_GPL(blkdev_write_iter);
- ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
- {
- struct file *file = iocb->ki_filp;
- struct inode *bd_inode = bdev_file_inode(file);
- loff_t size = i_size_read(bd_inode);
- loff_t pos = iocb->ki_pos;
- if (pos >= size)
- return 0;
- size -= pos;
- iov_iter_truncate(to, size);
- return generic_file_read_iter(iocb, to);
- }
- EXPORT_SYMBOL_GPL(blkdev_read_iter);
- static int blkdev_releasepage(struct page *page, gfp_t wait)
- {
- struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
- if (super && super->s_op->bdev_try_to_free_page)
- return super->s_op->bdev_try_to_free_page(super, page, wait);
- return try_to_free_buffers(page);
- }
- static int blkdev_writepages(struct address_space *mapping,
- struct writeback_control *wbc)
- {
- if (dax_mapping(mapping)) {
- struct block_device *bdev = I_BDEV(mapping->host);
- return dax_writeback_mapping_range(mapping, bdev, wbc);
- }
- return generic_writepages(mapping, wbc);
- }
- static const struct address_space_operations def_blk_aops = {
- .readpage = blkdev_readpage,
- .readpages = blkdev_readpages,
- .writepage = blkdev_writepage,
- .write_begin = blkdev_write_begin,
- .write_end = blkdev_write_end,
- .writepages = blkdev_writepages,
- .releasepage = blkdev_releasepage,
- .direct_IO = blkdev_direct_IO,
- .is_dirty_writeback = buffer_check_dirty_writeback,
- };
- #define BLKDEV_FALLOC_FL_SUPPORTED \
- (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
- FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
- static long blkdev_fallocate(struct file *file, int mode, loff_t start,
- loff_t len)
- {
- struct block_device *bdev = I_BDEV(bdev_file_inode(file));
- struct request_queue *q = bdev_get_queue(bdev);
- struct address_space *mapping;
- loff_t end = start + len - 1;
- loff_t isize;
- int error;
-
- if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
- return -EOPNOTSUPP;
-
- isize = i_size_read(bdev->bd_inode);
- if (start >= isize)
- return -EINVAL;
- if (end >= isize) {
- if (mode & FALLOC_FL_KEEP_SIZE) {
- len = isize - start;
- end = start + len - 1;
- } else
- return -EINVAL;
- }
-
- if ((start | len) & (bdev_logical_block_size(bdev) - 1))
- return -EINVAL;
-
- mapping = bdev->bd_inode->i_mapping;
- truncate_inode_pages_range(mapping, start, end);
- switch (mode) {
- case FALLOC_FL_ZERO_RANGE:
- case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
- error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
- GFP_KERNEL, false);
- break;
- case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
-
- if (!blk_queue_discard(q) || !q->limits.discard_zeroes_data)
- return -EOPNOTSUPP;
- error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
- GFP_KERNEL, 0);
- break;
- case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
- if (!blk_queue_discard(q))
- return -EOPNOTSUPP;
- error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
- GFP_KERNEL, 0);
- break;
- default:
- return -EOPNOTSUPP;
- }
- if (error)
- return error;
-
- return invalidate_inode_pages2_range(mapping,
- start >> PAGE_SHIFT,
- end >> PAGE_SHIFT);
- }
- const struct file_operations def_blk_fops = {
- .open = blkdev_open,
- .release = blkdev_close,
- .llseek = block_llseek,
- .read_iter = blkdev_read_iter,
- .write_iter = blkdev_write_iter,
- .mmap = generic_file_mmap,
- .fsync = blkdev_fsync,
- .unlocked_ioctl = block_ioctl,
- #ifdef CONFIG_COMPAT
- .compat_ioctl = compat_blkdev_ioctl,
- #endif
- .splice_read = generic_file_splice_read,
- .splice_write = iter_file_splice_write,
- .fallocate = blkdev_fallocate,
- };
- int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
- {
- int res;
- mm_segment_t old_fs = get_fs();
- set_fs(KERNEL_DS);
- res = blkdev_ioctl(bdev, 0, cmd, arg);
- set_fs(old_fs);
- return res;
- }
- EXPORT_SYMBOL(ioctl_by_bdev);
- struct block_device *lookup_bdev(const char *pathname)
- {
- struct block_device *bdev;
- struct inode *inode;
- struct path path;
- int error;
- if (!pathname || !*pathname)
- return ERR_PTR(-EINVAL);
- error = kern_path(pathname, LOOKUP_FOLLOW, &path);
- if (error)
- return ERR_PTR(error);
- inode = d_backing_inode(path.dentry);
- error = -ENOTBLK;
- if (!S_ISBLK(inode->i_mode))
- goto fail;
- error = -EACCES;
- if (!may_open_dev(&path))
- goto fail;
- error = -ENOMEM;
- bdev = bd_acquire(inode);
- if (!bdev)
- goto fail;
- out:
- path_put(&path);
- return bdev;
- fail:
- bdev = ERR_PTR(error);
- goto out;
- }
- EXPORT_SYMBOL(lookup_bdev);
- int __invalidate_device(struct block_device *bdev, bool kill_dirty)
- {
- struct super_block *sb = get_super(bdev);
- int res = 0;
- if (sb) {
-
- shrink_dcache_sb(sb);
- res = invalidate_inodes(sb, kill_dirty);
- drop_super(sb);
- }
- invalidate_bdev(bdev);
- return res;
- }
- EXPORT_SYMBOL(__invalidate_device);
- void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
- {
- struct inode *inode, *old_inode = NULL;
- spin_lock(&blockdev_superblock->s_inode_list_lock);
- list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
- struct address_space *mapping = inode->i_mapping;
- struct block_device *bdev;
- spin_lock(&inode->i_lock);
- if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
- mapping->nrpages == 0) {
- spin_unlock(&inode->i_lock);
- continue;
- }
- __iget(inode);
- spin_unlock(&inode->i_lock);
- spin_unlock(&blockdev_superblock->s_inode_list_lock);
-
- iput(old_inode);
- old_inode = inode;
- bdev = I_BDEV(inode);
- mutex_lock(&bdev->bd_mutex);
- if (bdev->bd_openers)
- func(bdev, arg);
- mutex_unlock(&bdev->bd_mutex);
- spin_lock(&blockdev_superblock->s_inode_list_lock);
- }
- spin_unlock(&blockdev_superblock->s_inode_list_lock);
- iput(old_inode);
- }
|