LCOV - code coverage report
Current view: top level - drivers/md - md-bitmap.c (source / functions) Hit Total Coverage
Test: combined.info Lines: 0 1350 0.0 %
Date: 2022-04-01 14:58:12 Functions: 0 61 0.0 %
Branches: 0 859 0.0 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0-only
       2                 :            : /*
       3                 :            :  * bitmap.c two-level bitmap (C) Peter T. Breuer (ptb@ot.uc3m.es) 2003
       4                 :            :  *
       5                 :            :  * bitmap_create  - sets up the bitmap structure
       6                 :            :  * bitmap_destroy - destroys the bitmap structure
       7                 :            :  *
       8                 :            :  * additions, Copyright (C) 2003-2004, Paul Clements, SteelEye Technology, Inc.:
       9                 :            :  * - added disk storage for bitmap
      10                 :            :  * - changes to allow various bitmap chunk sizes
      11                 :            :  */
      12                 :            : 
      13                 :            : /*
      14                 :            :  * Still to do:
      15                 :            :  *
      16                 :            :  * flush after percent set rather than just time based. (maybe both).
      17                 :            :  */
      18                 :            : 
      19                 :            : #include <linux/blkdev.h>
      20                 :            : #include <linux/module.h>
      21                 :            : #include <linux/errno.h>
      22                 :            : #include <linux/slab.h>
      23                 :            : #include <linux/init.h>
      24                 :            : #include <linux/timer.h>
      25                 :            : #include <linux/sched.h>
      26                 :            : #include <linux/list.h>
      27                 :            : #include <linux/file.h>
      28                 :            : #include <linux/mount.h>
      29                 :            : #include <linux/buffer_head.h>
      30                 :            : #include <linux/seq_file.h>
      31                 :            : #include <trace/events/block.h>
      32                 :            : #include "md.h"
      33                 :            : #include "md-bitmap.h"
      34                 :            : 
      35                 :          0 : static inline char *bmname(struct bitmap *bitmap)
      36                 :            : {
      37   [ #  #  #  #  :          0 :         return bitmap->mddev ? mdname(bitmap->mddev) : "mdX";
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                      # ]
      38                 :            : }
      39                 :            : 
      40                 :            : /*
      41                 :            :  * check a page and, if necessary, allocate it (or hijack it if the alloc fails)
      42                 :            :  *
      43                 :            :  * 1) check to see if this page is allocated, if it's not then try to alloc
      44                 :            :  * 2) if the alloc fails, set the page's hijacked flag so we'll use the
      45                 :            :  *    page pointer directly as a counter
      46                 :            :  *
      47                 :            :  * if we find our page, we increment the page's refcount so that it stays
      48                 :            :  * allocated while we're using it
      49                 :            :  */
      50                 :          0 : static int md_bitmap_checkpage(struct bitmap_counts *bitmap,
      51                 :            :                                unsigned long page, int create, int no_hijack)
      52                 :            : __releases(bitmap->lock)
      53                 :            : __acquires(bitmap->lock)
      54                 :            : {
      55                 :          0 :         unsigned char *mappage;
      56                 :            : 
      57         [ #  # ]:          0 :         if (page >= bitmap->pages) {
      58                 :            :                 /* This can happen if bitmap_start_sync goes beyond
      59                 :            :                  * End-of-device while looking for a whole page.
      60                 :            :                  * It is harmless.
      61                 :            :                  */
      62                 :            :                 return -EINVAL;
      63                 :            :         }
      64                 :            : 
      65         [ #  # ]:          0 :         if (bitmap->bp[page].hijacked) /* it's hijacked, don't try to alloc */
      66                 :            :                 return 0;
      67                 :            : 
      68         [ #  # ]:          0 :         if (bitmap->bp[page].map) /* page is already allocated, just return */
      69                 :            :                 return 0;
      70                 :            : 
      71         [ #  # ]:          0 :         if (!create)
      72                 :            :                 return -ENOENT;
      73                 :            : 
      74                 :            :         /* this page has not been allocated yet */
      75                 :            : 
      76                 :          0 :         spin_unlock_irq(&bitmap->lock);
      77                 :            :         /* It is possible that this is being called inside a
      78                 :            :          * prepare_to_wait/finish_wait loop from raid5c:make_request().
      79                 :            :          * In general it is not permitted to sleep in that context as it
      80                 :            :          * can cause the loop to spin freely.
      81                 :            :          * That doesn't apply here as we can only reach this point
      82                 :            :          * once with any loop.
      83                 :            :          * When this function completes, either bp[page].map or
      84                 :            :          * bp[page].hijacked.  In either case, this function will
      85                 :            :          * abort before getting to this point again.  So there is
      86                 :            :          * no risk of a free-spin, and so it is safe to assert
      87                 :            :          * that sleeping here is allowed.
      88                 :            :          */
      89                 :          0 :         sched_annotate_sleep();
      90                 :          0 :         mappage = kzalloc(PAGE_SIZE, GFP_NOIO);
      91                 :          0 :         spin_lock_irq(&bitmap->lock);
      92                 :            : 
      93         [ #  # ]:          0 :         if (mappage == NULL) {
      94                 :          0 :                 pr_debug("md/bitmap: map page allocation failed, hijacking\n");
      95                 :            :                 /* We don't support hijack for cluster raid */
      96         [ #  # ]:          0 :                 if (no_hijack)
      97                 :            :                         return -ENOMEM;
      98                 :            :                 /* failed - set the hijacked flag so that we can use the
      99                 :            :                  * pointer as a counter */
     100         [ #  # ]:          0 :                 if (!bitmap->bp[page].map)
     101                 :          0 :                         bitmap->bp[page].hijacked = 1;
     102   [ #  #  #  # ]:          0 :         } else if (bitmap->bp[page].map ||
     103                 :            :                    bitmap->bp[page].hijacked) {
     104                 :            :                 /* somebody beat us to getting the page */
     105                 :          0 :                 kfree(mappage);
     106                 :            :         } else {
     107                 :            : 
     108                 :            :                 /* no page was in place and we have one, so install it */
     109                 :            : 
     110                 :          0 :                 bitmap->bp[page].map = mappage;
     111                 :          0 :                 bitmap->missing_pages--;
     112                 :            :         }
     113                 :            :         return 0;
     114                 :            : }
     115                 :            : 
     116                 :            : /* if page is completely empty, put it back on the free list, or dealloc it */
     117                 :            : /* if page was hijacked, unmark the flag so it might get alloced next time */
     118                 :            : /* Note: lock should be held when calling this */
     119                 :            : static void md_bitmap_checkfree(struct bitmap_counts *bitmap, unsigned long page)
     120                 :            : {
     121                 :            :         char *ptr;
     122                 :            : 
     123                 :            :         if (bitmap->bp[page].count) /* page is still busy */
     124                 :            :                 return;
     125                 :            : 
     126                 :            :         /* page is no longer in use, it can be released */
     127                 :            : 
     128                 :            :         if (bitmap->bp[page].hijacked) { /* page was hijacked, undo this now */
     129                 :            :                 bitmap->bp[page].hijacked = 0;
     130                 :            :                 bitmap->bp[page].map = NULL;
     131                 :            :         } else {
     132                 :            :                 /* normal case, free the page */
     133                 :            :                 ptr = bitmap->bp[page].map;
     134                 :            :                 bitmap->bp[page].map = NULL;
     135                 :            :                 bitmap->missing_pages++;
     136                 :            :                 kfree(ptr);
     137                 :            :         }
     138                 :            : }
     139                 :            : 
     140                 :            : /*
     141                 :            :  * bitmap file handling - read and write the bitmap file and its superblock
     142                 :            :  */
     143                 :            : 
     144                 :            : /*
     145                 :            :  * basic page I/O operations
     146                 :            :  */
     147                 :            : 
     148                 :            : /* IO operations when bitmap is stored near all superblocks */
     149                 :          0 : static int read_sb_page(struct mddev *mddev, loff_t offset,
     150                 :            :                         struct page *page,
     151                 :            :                         unsigned long index, int size)
     152                 :            : {
     153                 :            :         /* choose a good rdev and read the page from there */
     154                 :            : 
     155                 :          0 :         struct md_rdev *rdev;
     156                 :          0 :         sector_t target;
     157                 :            : 
     158         [ #  # ]:          0 :         rdev_for_each(rdev, mddev) {
     159         [ #  # ]:          0 :                 if (! test_bit(In_sync, &rdev->flags)
     160         [ #  # ]:          0 :                     || test_bit(Faulty, &rdev->flags)
     161         [ #  # ]:          0 :                     || test_bit(Bitmap_sync, &rdev->flags))
     162                 :          0 :                         continue;
     163                 :            : 
     164                 :          0 :                 target = offset + index * (PAGE_SIZE/512);
     165                 :            : 
     166         [ #  # ]:          0 :                 if (sync_page_io(rdev, target,
     167         [ #  # ]:          0 :                                  roundup(size, bdev_logical_block_size(rdev->bdev)),
     168                 :            :                                  page, REQ_OP_READ, 0, true)) {
     169                 :          0 :                         page->index = index;
     170                 :          0 :                         return 0;
     171                 :            :                 }
     172                 :            :         }
     173                 :            :         return -EIO;
     174                 :            : }
     175                 :            : 
     176                 :          0 : static struct md_rdev *next_active_rdev(struct md_rdev *rdev, struct mddev *mddev)
     177                 :            : {
     178                 :            :         /* Iterate the disks of an mddev, using rcu to protect access to the
     179                 :            :          * linked list, and raising the refcount of devices we return to ensure
     180                 :            :          * they don't disappear while in use.
     181                 :            :          * As devices are only added or removed when raid_disk is < 0 and
     182                 :            :          * nr_pending is 0 and In_sync is clear, the entries we return will
     183                 :            :          * still be in the same position on the list when we re-enter
     184                 :            :          * list_for_each_entry_continue_rcu.
     185                 :            :          *
     186                 :            :          * Note that if entered with 'rdev == NULL' to start at the
     187                 :            :          * beginning, we temporarily assign 'rdev' to an address which
     188                 :            :          * isn't really an rdev, but which can be used by
     189                 :            :          * list_for_each_entry_continue_rcu() to find the first entry.
     190                 :            :          */
     191                 :          0 :         rcu_read_lock();
     192         [ #  # ]:          0 :         if (rdev == NULL)
     193                 :            :                 /* start at the beginning */
     194                 :          0 :                 rdev = list_entry(&mddev->disks, struct md_rdev, same_set);
     195                 :            :         else {
     196                 :            :                 /* release the previous rdev and start from there. */
     197                 :          0 :                 rdev_dec_pending(rdev, mddev);
     198                 :            :         }
     199         [ #  # ]:          0 :         list_for_each_entry_continue_rcu(rdev, &mddev->disks, same_set) {
     200   [ #  #  #  # ]:          0 :                 if (rdev->raid_disk >= 0 &&
     201                 :          0 :                     !test_bit(Faulty, &rdev->flags)) {
     202                 :            :                         /* this is a usable devices */
     203                 :          0 :                         atomic_inc(&rdev->nr_pending);
     204                 :          0 :                         rcu_read_unlock();
     205                 :          0 :                         return rdev;
     206                 :            :                 }
     207                 :            :         }
     208                 :          0 :         rcu_read_unlock();
     209                 :          0 :         return NULL;
     210                 :            : }
     211                 :            : 
     212                 :          0 : static int write_sb_page(struct bitmap *bitmap, struct page *page, int wait)
     213                 :            : {
     214                 :          0 :         struct md_rdev *rdev;
     215                 :          0 :         struct block_device *bdev;
     216                 :          0 :         struct mddev *mddev = bitmap->mddev;
     217                 :          0 :         struct bitmap_storage *store = &bitmap->storage;
     218                 :            : 
     219                 :          0 : restart:
     220                 :          0 :         rdev = NULL;
     221         [ #  # ]:          0 :         while ((rdev = next_active_rdev(rdev, mddev)) != NULL) {
     222                 :          0 :                 int size = PAGE_SIZE;
     223                 :          0 :                 loff_t offset = mddev->bitmap_info.offset;
     224                 :            : 
     225         [ #  # ]:          0 :                 bdev = (rdev->meta_bdev) ? rdev->meta_bdev : rdev->bdev;
     226                 :            : 
     227         [ #  # ]:          0 :                 if (page->index == store->file_pages-1) {
     228                 :          0 :                         int last_page_size = store->bytes & (PAGE_SIZE-1);
     229         [ #  # ]:          0 :                         if (last_page_size == 0)
     230                 :          0 :                                 last_page_size = PAGE_SIZE;
     231         [ #  # ]:          0 :                         size = roundup(last_page_size,
     232                 :            :                                        bdev_logical_block_size(bdev));
     233                 :            :                 }
     234                 :            :                 /* Just make sure we aren't corrupting data or
     235                 :            :                  * metadata
     236                 :            :                  */
     237         [ #  # ]:          0 :                 if (mddev->external) {
     238                 :            :                         /* Bitmap could be anywhere. */
     239                 :          0 :                         if (rdev->sb_start + offset + (page->index
     240                 :          0 :                                                        * (PAGE_SIZE/512))
     241         [ #  # ]:          0 :                             > rdev->data_offset
     242                 :          0 :                             &&
     243                 :            :                             rdev->sb_start + offset
     244                 :          0 :                             < (rdev->data_offset + mddev->dev_sectors
     245         [ #  # ]:          0 :                              + (PAGE_SIZE/512)))
     246                 :          0 :                                 goto bad_alignment;
     247         [ #  # ]:          0 :                 } else if (offset < 0) {
     248                 :            :                         /* DATA  BITMAP METADATA  */
     249                 :          0 :                         if (offset
     250                 :          0 :                             + (long)(page->index * (PAGE_SIZE/512))
     251         [ #  # ]:          0 :                             + size/512 > 0)
     252                 :            :                                 /* bitmap runs in to metadata */
     253                 :          0 :                                 goto bad_alignment;
     254                 :          0 :                         if (rdev->data_offset + mddev->dev_sectors
     255         [ #  # ]:          0 :                             > rdev->sb_start + offset)
     256                 :            :                                 /* data runs in to bitmap */
     257                 :          0 :                                 goto bad_alignment;
     258         [ #  # ]:          0 :                 } else if (rdev->sb_start < rdev->data_offset) {
     259                 :            :                         /* METADATA BITMAP DATA */
     260                 :          0 :                         if (rdev->sb_start
     261                 :          0 :                             + offset
     262         [ #  # ]:          0 :                             + page->index*(PAGE_SIZE/512) + size/512
     263                 :            :                             > rdev->data_offset)
     264                 :            :                                 /* bitmap runs in to data */
     265                 :          0 :                                 goto bad_alignment;
     266                 :            :                 } else {
     267                 :            :                         /* DATA METADATA BITMAP - no problems */
     268                 :          0 :                 }
     269                 :          0 :                 md_super_write(mddev, rdev,
     270                 :          0 :                                rdev->sb_start + offset
     271                 :          0 :                                + page->index * (PAGE_SIZE/512),
     272                 :            :                                size,
     273                 :            :                                page);
     274                 :            :         }
     275                 :            : 
     276   [ #  #  #  # ]:          0 :         if (wait && md_super_wait(mddev) < 0)
     277                 :          0 :                 goto restart;
     278                 :            :         return 0;
     279                 :            : 
     280                 :          0 :  bad_alignment:
     281                 :            :         return -EINVAL;
     282                 :            : }
     283                 :            : 
     284                 :            : static void md_bitmap_file_kick(struct bitmap *bitmap);
     285                 :            : /*
     286                 :            :  * write out a page to a file
     287                 :            :  */
     288                 :          0 : static void write_page(struct bitmap *bitmap, struct page *page, int wait)
     289                 :            : {
     290                 :          0 :         struct buffer_head *bh;
     291                 :            : 
     292         [ #  # ]:          0 :         if (bitmap->storage.file == NULL) {
     293         [ #  # ]:          0 :                 switch (write_sb_page(bitmap, page, wait)) {
     294                 :          0 :                 case -EINVAL:
     295                 :          0 :                         set_bit(BITMAP_WRITE_ERROR, &bitmap->flags);
     296                 :            :                 }
     297                 :            :         } else {
     298                 :            : 
     299         [ #  # ]:          0 :                 bh = page_buffers(page);
     300                 :            : 
     301   [ #  #  #  # ]:          0 :                 while (bh && bh->b_blocknr) {
     302                 :          0 :                         atomic_inc(&bitmap->pending_writes);
     303                 :          0 :                         set_buffer_locked(bh);
     304                 :          0 :                         set_buffer_mapped(bh);
     305                 :          0 :                         submit_bh(REQ_OP_WRITE, REQ_SYNC, bh);
     306                 :          0 :                         bh = bh->b_this_page;
     307                 :            :                 }
     308                 :            : 
     309         [ #  # ]:          0 :                 if (wait)
     310   [ #  #  #  # ]:          0 :                         wait_event(bitmap->write_wait,
     311                 :            :                                    atomic_read(&bitmap->pending_writes)==0);
     312                 :            :         }
     313         [ #  # ]:          0 :         if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
     314                 :          0 :                 md_bitmap_file_kick(bitmap);
     315                 :          0 : }
     316                 :            : 
     317                 :          0 : static void end_bitmap_write(struct buffer_head *bh, int uptodate)
     318                 :            : {
     319                 :          0 :         struct bitmap *bitmap = bh->b_private;
     320                 :            : 
     321         [ #  # ]:          0 :         if (!uptodate)
     322                 :          0 :                 set_bit(BITMAP_WRITE_ERROR, &bitmap->flags);
     323         [ #  # ]:          0 :         if (atomic_dec_and_test(&bitmap->pending_writes))
     324                 :          0 :                 wake_up(&bitmap->write_wait);
     325                 :          0 : }
     326                 :            : 
     327                 :            : /* copied from buffer.c */
     328                 :            : static void
     329                 :          0 : __clear_page_buffers(struct page *page)
     330                 :            : {
     331                 :          0 :         ClearPagePrivate(page);
     332                 :          0 :         set_page_private(page, 0);
     333                 :          0 :         put_page(page);
     334                 :            : }
     335                 :          0 : static void free_buffers(struct page *page)
     336                 :            : {
     337                 :          0 :         struct buffer_head *bh;
     338                 :            : 
     339         [ #  # ]:          0 :         if (!PagePrivate(page))
     340                 :            :                 return;
     341                 :            : 
     342         [ #  # ]:          0 :         bh = page_buffers(page);
     343         [ #  # ]:          0 :         while (bh) {
     344                 :          0 :                 struct buffer_head *next = bh->b_this_page;
     345                 :          0 :                 free_buffer_head(bh);
     346                 :          0 :                 bh = next;
     347                 :            :         }
     348                 :          0 :         __clear_page_buffers(page);
     349                 :          0 :         put_page(page);
     350                 :            : }
     351                 :            : 
     352                 :            : /* read a page from a file.
     353                 :            :  * We both read the page, and attach buffers to the page to record the
     354                 :            :  * address of each block (using bmap).  These addresses will be used
     355                 :            :  * to write the block later, completely bypassing the filesystem.
     356                 :            :  * This usage is similar to how swap files are handled, and allows us
     357                 :            :  * to write to a file with no concerns of memory allocation failing.
     358                 :            :  */
     359                 :          0 : static int read_page(struct file *file, unsigned long index,
     360                 :            :                      struct bitmap *bitmap,
     361                 :            :                      unsigned long count,
     362                 :            :                      struct page *page)
     363                 :            : {
     364                 :          0 :         int ret = 0;
     365                 :          0 :         struct inode *inode = file_inode(file);
     366                 :          0 :         struct buffer_head *bh;
     367                 :          0 :         sector_t block, blk_cur;
     368                 :            : 
     369                 :          0 :         pr_debug("read bitmap file (%dB @ %llu)\n", (int)PAGE_SIZE,
     370                 :            :                  (unsigned long long)index << PAGE_SHIFT);
     371                 :            : 
     372                 :          0 :         bh = alloc_page_buffers(page, 1<<inode->i_blkbits, false);
     373         [ #  # ]:          0 :         if (!bh) {
     374                 :          0 :                 ret = -ENOMEM;
     375                 :          0 :                 goto out;
     376                 :            :         }
     377                 :          0 :         attach_page_buffers(page, bh);
     378                 :          0 :         blk_cur = index << (PAGE_SHIFT - inode->i_blkbits);
     379         [ #  # ]:          0 :         while (bh) {
     380                 :          0 :                 block = blk_cur;
     381                 :            : 
     382         [ #  # ]:          0 :                 if (count == 0)
     383                 :          0 :                         bh->b_blocknr = 0;
     384                 :            :                 else {
     385                 :          0 :                         ret = bmap(inode, &block);
     386   [ #  #  #  # ]:          0 :                         if (ret || !block) {
     387                 :          0 :                                 ret = -EINVAL;
     388                 :          0 :                                 bh->b_blocknr = 0;
     389                 :          0 :                                 goto out;
     390                 :            :                         }
     391                 :            : 
     392                 :          0 :                         bh->b_blocknr = block;
     393                 :          0 :                         bh->b_bdev = inode->i_sb->s_bdev;
     394         [ #  # ]:          0 :                         if (count < (1<<inode->i_blkbits))
     395                 :            :                                 count = 0;
     396                 :            :                         else
     397                 :          0 :                                 count -= (1<<inode->i_blkbits);
     398                 :            : 
     399                 :          0 :                         bh->b_end_io = end_bitmap_write;
     400                 :          0 :                         bh->b_private = bitmap;
     401                 :          0 :                         atomic_inc(&bitmap->pending_writes);
     402                 :          0 :                         set_buffer_locked(bh);
     403                 :          0 :                         set_buffer_mapped(bh);
     404                 :          0 :                         submit_bh(REQ_OP_READ, 0, bh);
     405                 :            :                 }
     406                 :          0 :                 blk_cur++;
     407                 :          0 :                 bh = bh->b_this_page;
     408                 :            :         }
     409                 :          0 :         page->index = index;
     410                 :            : 
     411   [ #  #  #  # ]:          0 :         wait_event(bitmap->write_wait,
     412                 :            :                    atomic_read(&bitmap->pending_writes)==0);
     413         [ #  # ]:          0 :         if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
     414                 :            :                 ret = -EIO;
     415                 :          0 : out:
     416         [ #  # ]:          0 :         if (ret)
     417                 :          0 :                 pr_err("md: bitmap read error: (%dB @ %llu): %d\n",
     418                 :            :                        (int)PAGE_SIZE,
     419                 :            :                        (unsigned long long)index << PAGE_SHIFT,
     420                 :            :                        ret);
     421                 :          0 :         return ret;
     422                 :            : }
     423                 :            : 
     424                 :            : /*
     425                 :            :  * bitmap file superblock operations
     426                 :            :  */
     427                 :            : 
     428                 :            : /*
     429                 :            :  * md_bitmap_wait_writes() should be called before writing any bitmap
     430                 :            :  * blocks, to ensure previous writes, particularly from
     431                 :            :  * md_bitmap_daemon_work(), have completed.
     432                 :            :  */
     433                 :          0 : static void md_bitmap_wait_writes(struct bitmap *bitmap)
     434                 :            : {
     435         [ #  # ]:          0 :         if (bitmap->storage.file)
     436   [ #  #  #  # ]:          0 :                 wait_event(bitmap->write_wait,
     437                 :            :                            atomic_read(&bitmap->pending_writes)==0);
     438                 :            :         else
     439                 :            :                 /* Note that we ignore the return value.  The writes
     440                 :            :                  * might have failed, but that would just mean that
     441                 :            :                  * some bits which should be cleared haven't been,
     442                 :            :                  * which is safe.  The relevant bitmap blocks will
     443                 :            :                  * probably get written again, but there is no great
     444                 :            :                  * loss if they aren't.
     445                 :            :                  */
     446                 :          0 :                 md_super_wait(bitmap->mddev);
     447                 :          0 : }
     448                 :            : 
     449                 :            : 
     450                 :            : /* update the event counter and sync the superblock to disk */
     451                 :          0 : void md_bitmap_update_sb(struct bitmap *bitmap)
     452                 :            : {
     453                 :          0 :         bitmap_super_t *sb;
     454                 :            : 
     455   [ #  #  #  # ]:          0 :         if (!bitmap || !bitmap->mddev) /* no bitmap for this array */
     456                 :            :                 return;
     457         [ #  # ]:          0 :         if (bitmap->mddev->bitmap_info.external)
     458                 :            :                 return;
     459         [ #  # ]:          0 :         if (!bitmap->storage.sb_page) /* no superblock */
     460                 :            :                 return;
     461                 :          0 :         sb = kmap_atomic(bitmap->storage.sb_page);
     462                 :          0 :         sb->events = cpu_to_le64(bitmap->mddev->events);
     463         [ #  # ]:          0 :         if (bitmap->mddev->events < bitmap->events_cleared)
     464                 :            :                 /* rocking back to read-only */
     465                 :          0 :                 bitmap->events_cleared = bitmap->mddev->events;
     466                 :          0 :         sb->events_cleared = cpu_to_le64(bitmap->events_cleared);
     467                 :            :         /*
     468                 :            :          * clear BITMAP_WRITE_ERROR bit to protect against the case that
     469                 :            :          * a bitmap write error occurred but the later writes succeeded.
     470                 :            :          */
     471                 :          0 :         sb->state = cpu_to_le32(bitmap->flags & ~BIT(BITMAP_WRITE_ERROR));
     472                 :            :         /* Just in case these have been changed via sysfs: */
     473                 :          0 :         sb->daemon_sleep = cpu_to_le32(bitmap->mddev->bitmap_info.daemon_sleep/HZ);
     474                 :          0 :         sb->write_behind = cpu_to_le32(bitmap->mddev->bitmap_info.max_write_behind);
     475                 :            :         /* This might have been changed by a reshape */
     476                 :          0 :         sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
     477                 :          0 :         sb->chunksize = cpu_to_le32(bitmap->mddev->bitmap_info.chunksize);
     478                 :          0 :         sb->nodes = cpu_to_le32(bitmap->mddev->bitmap_info.nodes);
     479                 :          0 :         sb->sectors_reserved = cpu_to_le32(bitmap->mddev->
     480                 :            :                                            bitmap_info.space);
     481                 :          0 :         kunmap_atomic(sb);
     482                 :          0 :         write_page(bitmap, bitmap->storage.sb_page, 1);
     483                 :            : }
     484                 :            : EXPORT_SYMBOL(md_bitmap_update_sb);
     485                 :            : 
     486                 :            : /* print out the bitmap file superblock */
     487                 :          0 : void md_bitmap_print_sb(struct bitmap *bitmap)
     488                 :            : {
     489                 :          0 :         bitmap_super_t *sb;
     490                 :            : 
     491   [ #  #  #  # ]:          0 :         if (!bitmap || !bitmap->storage.sb_page)
     492                 :            :                 return;
     493                 :          0 :         sb = kmap_atomic(bitmap->storage.sb_page);
     494                 :          0 :         pr_debug("%s: bitmap file superblock:\n", bmname(bitmap));
     495                 :          0 :         pr_debug("         magic: %08x\n", le32_to_cpu(sb->magic));
     496                 :          0 :         pr_debug("       version: %d\n", le32_to_cpu(sb->version));
     497                 :          0 :         pr_debug("          uuid: %08x.%08x.%08x.%08x\n",
     498                 :            :                  le32_to_cpu(*(__le32 *)(sb->uuid+0)),
     499                 :            :                  le32_to_cpu(*(__le32 *)(sb->uuid+4)),
     500                 :            :                  le32_to_cpu(*(__le32 *)(sb->uuid+8)),
     501                 :            :                  le32_to_cpu(*(__le32 *)(sb->uuid+12)));
     502                 :          0 :         pr_debug("        events: %llu\n",
     503                 :            :                  (unsigned long long) le64_to_cpu(sb->events));
     504                 :          0 :         pr_debug("events cleared: %llu\n",
     505                 :            :                  (unsigned long long) le64_to_cpu(sb->events_cleared));
     506                 :          0 :         pr_debug("         state: %08x\n", le32_to_cpu(sb->state));
     507                 :          0 :         pr_debug("     chunksize: %d B\n", le32_to_cpu(sb->chunksize));
     508                 :          0 :         pr_debug("  daemon sleep: %ds\n", le32_to_cpu(sb->daemon_sleep));
     509                 :          0 :         pr_debug("     sync size: %llu KB\n",
     510                 :            :                  (unsigned long long)le64_to_cpu(sb->sync_size)/2);
     511                 :          0 :         pr_debug("max write behind: %d\n", le32_to_cpu(sb->write_behind));
     512                 :          0 :         kunmap_atomic(sb);
     513                 :            : }
     514                 :            : 
     515                 :            : /*
     516                 :            :  * bitmap_new_disk_sb
     517                 :            :  * @bitmap
     518                 :            :  *
     519                 :            :  * This function is somewhat the reverse of bitmap_read_sb.  bitmap_read_sb
     520                 :            :  * reads and verifies the on-disk bitmap superblock and populates bitmap_info.
     521                 :            :  * This function verifies 'bitmap_info' and populates the on-disk bitmap
     522                 :            :  * structure, which is to be written to disk.
     523                 :            :  *
     524                 :            :  * Returns: 0 on success, -Exxx on error
     525                 :            :  */
     526                 :          0 : static int md_bitmap_new_disk_sb(struct bitmap *bitmap)
     527                 :            : {
     528                 :          0 :         bitmap_super_t *sb;
     529                 :          0 :         unsigned long chunksize, daemon_sleep, write_behind;
     530                 :            : 
     531                 :          0 :         bitmap->storage.sb_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
     532         [ #  # ]:          0 :         if (bitmap->storage.sb_page == NULL)
     533                 :            :                 return -ENOMEM;
     534                 :          0 :         bitmap->storage.sb_page->index = 0;
     535                 :            : 
     536                 :          0 :         sb = kmap_atomic(bitmap->storage.sb_page);
     537                 :            : 
     538                 :          0 :         sb->magic = cpu_to_le32(BITMAP_MAGIC);
     539                 :          0 :         sb->version = cpu_to_le32(BITMAP_MAJOR_HI);
     540                 :            : 
     541                 :          0 :         chunksize = bitmap->mddev->bitmap_info.chunksize;
     542         [ #  # ]:          0 :         BUG_ON(!chunksize);
     543   [ #  #  #  # ]:          0 :         if (!is_power_of_2(chunksize)) {
     544                 :          0 :                 kunmap_atomic(sb);
     545                 :          0 :                 pr_warn("bitmap chunksize not a power of 2\n");
     546                 :          0 :                 return -EINVAL;
     547                 :            :         }
     548                 :          0 :         sb->chunksize = cpu_to_le32(chunksize);
     549                 :            : 
     550                 :          0 :         daemon_sleep = bitmap->mddev->bitmap_info.daemon_sleep;
     551         [ #  # ]:          0 :         if (!daemon_sleep || (daemon_sleep > MAX_SCHEDULE_TIMEOUT)) {
     552                 :          0 :                 pr_debug("Choosing daemon_sleep default (5 sec)\n");
     553                 :          0 :                 daemon_sleep = 5 * HZ;
     554                 :            :         }
     555                 :          0 :         sb->daemon_sleep = cpu_to_le32(daemon_sleep);
     556                 :          0 :         bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
     557                 :            : 
     558                 :            :         /*
     559                 :            :          * FIXME: write_behind for RAID1.  If not specified, what
     560                 :            :          * is a good choice?  We choose COUNTER_MAX / 2 arbitrarily.
     561                 :            :          */
     562                 :          0 :         write_behind = bitmap->mddev->bitmap_info.max_write_behind;
     563         [ #  # ]:          0 :         if (write_behind > COUNTER_MAX)
     564                 :          0 :                 write_behind = COUNTER_MAX / 2;
     565                 :          0 :         sb->write_behind = cpu_to_le32(write_behind);
     566                 :          0 :         bitmap->mddev->bitmap_info.max_write_behind = write_behind;
     567                 :            : 
     568                 :            :         /* keep the array size field of the bitmap superblock up to date */
     569                 :          0 :         sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
     570                 :            : 
     571                 :          0 :         memcpy(sb->uuid, bitmap->mddev->uuid, 16);
     572                 :            : 
     573                 :          0 :         set_bit(BITMAP_STALE, &bitmap->flags);
     574                 :          0 :         sb->state = cpu_to_le32(bitmap->flags);
     575                 :          0 :         bitmap->events_cleared = bitmap->mddev->events;
     576                 :          0 :         sb->events_cleared = cpu_to_le64(bitmap->mddev->events);
     577                 :          0 :         bitmap->mddev->bitmap_info.nodes = 0;
     578                 :            : 
     579                 :          0 :         kunmap_atomic(sb);
     580                 :            : 
     581                 :          0 :         return 0;
     582                 :            : }
     583                 :            : 
     584                 :            : /* read the superblock from the bitmap file and initialize some bitmap fields */
     585                 :          0 : static int md_bitmap_read_sb(struct bitmap *bitmap)
     586                 :            : {
     587                 :          0 :         char *reason = NULL;
     588                 :          0 :         bitmap_super_t *sb;
     589                 :          0 :         unsigned long chunksize, daemon_sleep, write_behind;
     590                 :          0 :         unsigned long long events;
     591                 :          0 :         int nodes = 0;
     592                 :          0 :         unsigned long sectors_reserved = 0;
     593                 :          0 :         int err = -EINVAL;
     594                 :          0 :         struct page *sb_page;
     595                 :          0 :         loff_t offset = bitmap->mddev->bitmap_info.offset;
     596                 :            : 
     597   [ #  #  #  # ]:          0 :         if (!bitmap->storage.file && !bitmap->mddev->bitmap_info.offset) {
     598                 :          0 :                 chunksize = 128 * 1024 * 1024;
     599                 :          0 :                 daemon_sleep = 5 * HZ;
     600                 :          0 :                 write_behind = 0;
     601                 :          0 :                 set_bit(BITMAP_STALE, &bitmap->flags);
     602                 :          0 :                 err = 0;
     603                 :          0 :                 goto out_no_sb;
     604                 :            :         }
     605                 :            :         /* page 0 is the superblock, read it... */
     606                 :          0 :         sb_page = alloc_page(GFP_KERNEL);
     607         [ #  # ]:          0 :         if (!sb_page)
     608                 :            :                 return -ENOMEM;
     609                 :          0 :         bitmap->storage.sb_page = sb_page;
     610                 :            : 
     611                 :          0 : re_read:
     612                 :            :         /* If cluster_slot is set, the cluster is setup */
     613         [ #  # ]:          0 :         if (bitmap->cluster_slot >= 0) {
     614                 :          0 :                 sector_t bm_blocks = bitmap->mddev->resync_max_sectors;
     615                 :            : 
     616                 :          0 :                 sector_div(bm_blocks,
     617                 :            :                            bitmap->mddev->bitmap_info.chunksize >> 9);
     618                 :            :                 /* bits to bytes */
     619                 :          0 :                 bm_blocks = ((bm_blocks+7) >> 3) + sizeof(bitmap_super_t);
     620                 :            :                 /* to 4k blocks */
     621                 :          0 :                 bm_blocks = DIV_ROUND_UP_SECTOR_T(bm_blocks, 4096);
     622                 :          0 :                 offset = bitmap->mddev->bitmap_info.offset + (bitmap->cluster_slot * (bm_blocks << 3));
     623                 :          0 :                 pr_debug("%s:%d bm slot: %d offset: %llu\n", __func__, __LINE__,
     624                 :            :                         bitmap->cluster_slot, offset);
     625                 :            :         }
     626                 :            : 
     627         [ #  # ]:          0 :         if (bitmap->storage.file) {
     628                 :          0 :                 loff_t isize = i_size_read(bitmap->storage.file->f_mapping->host);
     629                 :          0 :                 int bytes = isize > PAGE_SIZE ? PAGE_SIZE : isize;
     630                 :            : 
     631                 :          0 :                 err = read_page(bitmap->storage.file, 0,
     632                 :            :                                 bitmap, bytes, sb_page);
     633                 :            :         } else {
     634                 :          0 :                 err = read_sb_page(bitmap->mddev,
     635                 :            :                                    offset,
     636                 :            :                                    sb_page,
     637                 :            :                                    0, sizeof(bitmap_super_t));
     638                 :            :         }
     639         [ #  # ]:          0 :         if (err)
     640                 :          0 :                 return err;
     641                 :            : 
     642                 :          0 :         err = -EINVAL;
     643                 :          0 :         sb = kmap_atomic(sb_page);
     644                 :            : 
     645                 :          0 :         chunksize = le32_to_cpu(sb->chunksize);
     646                 :          0 :         daemon_sleep = le32_to_cpu(sb->daemon_sleep) * HZ;
     647                 :          0 :         write_behind = le32_to_cpu(sb->write_behind);
     648                 :          0 :         sectors_reserved = le32_to_cpu(sb->sectors_reserved);
     649                 :            :         /* Setup nodes/clustername only if bitmap version is
     650                 :            :          * cluster-compatible
     651                 :            :          */
     652         [ #  # ]:          0 :         if (sb->version == cpu_to_le32(BITMAP_MAJOR_CLUSTERED)) {
     653                 :          0 :                 nodes = le32_to_cpu(sb->nodes);
     654                 :          0 :                 strlcpy(bitmap->mddev->bitmap_info.cluster_name,
     655                 :          0 :                                 sb->cluster_name, 64);
     656                 :            :         }
     657                 :            : 
     658                 :            :         /* verify that the bitmap-specific fields are valid */
     659         [ #  # ]:          0 :         if (sb->magic != cpu_to_le32(BITMAP_MAGIC))
     660                 :            :                 reason = "bad magic";
     661         [ #  # ]:          0 :         else if (le32_to_cpu(sb->version) < BITMAP_MAJOR_LO ||
     662                 :            :                  le32_to_cpu(sb->version) > BITMAP_MAJOR_CLUSTERED)
     663                 :            :                 reason = "unrecognized superblock version";
     664         [ #  # ]:          0 :         else if (chunksize < 512)
     665                 :            :                 reason = "bitmap chunksize too small";
     666   [ #  #  #  # ]:          0 :         else if (!is_power_of_2(chunksize))
     667                 :            :                 reason = "bitmap chunksize not a power of 2";
     668         [ #  # ]:          0 :         else if (daemon_sleep < 1 || daemon_sleep > MAX_SCHEDULE_TIMEOUT)
     669                 :            :                 reason = "daemon sleep period out of range";
     670         [ #  # ]:          0 :         else if (write_behind > COUNTER_MAX)
     671                 :          0 :                 reason = "write-behind limit out of range (0 - 16383)";
     672         [ #  # ]:          0 :         if (reason) {
     673         [ #  # ]:          0 :                 pr_warn("%s: invalid bitmap file superblock: %s\n",
     674                 :            :                         bmname(bitmap), reason);
     675                 :          0 :                 goto out;
     676                 :            :         }
     677                 :            : 
     678                 :            :         /* keep the array size field of the bitmap superblock up to date */
     679                 :          0 :         sb->sync_size = cpu_to_le64(bitmap->mddev->resync_max_sectors);
     680                 :            : 
     681         [ #  # ]:          0 :         if (bitmap->mddev->persistent) {
     682                 :            :                 /*
     683                 :            :                  * We have a persistent array superblock, so compare the
     684                 :            :                  * bitmap's UUID and event counter to the mddev's
     685                 :            :                  */
     686         [ #  # ]:          0 :                 if (memcmp(sb->uuid, bitmap->mddev->uuid, 16)) {
     687         [ #  # ]:          0 :                         pr_warn("%s: bitmap superblock UUID mismatch\n",
     688                 :            :                                 bmname(bitmap));
     689                 :          0 :                         goto out;
     690                 :            :                 }
     691                 :          0 :                 events = le64_to_cpu(sb->events);
     692   [ #  #  #  # ]:          0 :                 if (!nodes && (events < bitmap->mddev->events)) {
     693         [ #  # ]:          0 :                         pr_warn("%s: bitmap file is out of date (%llu < %llu) -- forcing full recovery\n",
     694                 :            :                                 bmname(bitmap), events,
     695                 :            :                                 (unsigned long long) bitmap->mddev->events);
     696                 :          0 :                         set_bit(BITMAP_STALE, &bitmap->flags);
     697                 :            :                 }
     698                 :            :         }
     699                 :            : 
     700                 :            :         /* assign fields using values from superblock */
     701                 :          0 :         bitmap->flags |= le32_to_cpu(sb->state);
     702         [ #  # ]:          0 :         if (le32_to_cpu(sb->version) == BITMAP_MAJOR_HOSTENDIAN)
     703                 :          0 :                 set_bit(BITMAP_HOSTENDIAN, &bitmap->flags);
     704                 :          0 :         bitmap->events_cleared = le64_to_cpu(sb->events_cleared);
     705                 :          0 :         strlcpy(bitmap->mddev->bitmap_info.cluster_name, sb->cluster_name, 64);
     706                 :          0 :         err = 0;
     707                 :            : 
     708                 :          0 : out:
     709                 :          0 :         kunmap_atomic(sb);
     710                 :            :         /* Assigning chunksize is required for "re_read" */
     711                 :          0 :         bitmap->mddev->bitmap_info.chunksize = chunksize;
     712   [ #  #  #  # ]:          0 :         if (err == 0 && nodes && (bitmap->cluster_slot < 0)) {
     713                 :          0 :                 err = md_setup_cluster(bitmap->mddev, nodes);
     714         [ #  # ]:          0 :                 if (err) {
     715         [ #  # ]:          0 :                         pr_warn("%s: Could not setup cluster service (%d)\n",
     716                 :            :                                 bmname(bitmap), err);
     717                 :          0 :                         goto out_no_sb;
     718                 :            :                 }
     719                 :          0 :                 bitmap->cluster_slot = md_cluster_ops->slot_number(bitmap->mddev);
     720                 :          0 :                 goto re_read;
     721                 :            :         }
     722                 :            : 
     723                 :            : 
     724                 :          0 : out_no_sb:
     725         [ #  # ]:          0 :         if (test_bit(BITMAP_STALE, &bitmap->flags))
     726                 :          0 :                 bitmap->events_cleared = bitmap->mddev->events;
     727                 :          0 :         bitmap->mddev->bitmap_info.chunksize = chunksize;
     728                 :          0 :         bitmap->mddev->bitmap_info.daemon_sleep = daemon_sleep;
     729                 :          0 :         bitmap->mddev->bitmap_info.max_write_behind = write_behind;
     730                 :          0 :         bitmap->mddev->bitmap_info.nodes = nodes;
     731   [ #  #  #  # ]:          0 :         if (bitmap->mddev->bitmap_info.space == 0 ||
     732                 :            :             bitmap->mddev->bitmap_info.space > sectors_reserved)
     733                 :          0 :                 bitmap->mddev->bitmap_info.space = sectors_reserved;
     734         [ #  # ]:          0 :         if (err) {
     735                 :          0 :                 md_bitmap_print_sb(bitmap);
     736         [ #  # ]:          0 :                 if (bitmap->cluster_slot < 0)
     737                 :          0 :                         md_cluster_stop(bitmap->mddev);
     738                 :            :         }
     739                 :            :         return err;
     740                 :            : }
     741                 :            : 
     742                 :            : /*
     743                 :            :  * general bitmap file operations
     744                 :            :  */
     745                 :            : 
     746                 :            : /*
     747                 :            :  * on-disk bitmap:
     748                 :            :  *
     749                 :            :  * Use one bit per "chunk" (block set). We do the disk I/O on the bitmap
     750                 :            :  * file a page at a time. There's a superblock at the start of the file.
     751                 :            :  */
     752                 :            : /* calculate the index of the page that contains this bit */
     753                 :          0 : static inline unsigned long file_page_index(struct bitmap_storage *store,
     754                 :            :                                             unsigned long chunk)
     755                 :            : {
     756                 :          0 :         if (store->sb_page)
     757                 :          0 :                 chunk += sizeof(bitmap_super_t) << 3;
     758                 :          0 :         return chunk >> PAGE_BIT_SHIFT;
     759                 :            : }
     760                 :            : 
     761                 :            : /* calculate the (bit) offset of this bit within a page */
     762                 :          0 : static inline unsigned long file_page_offset(struct bitmap_storage *store,
     763                 :            :                                              unsigned long chunk)
     764                 :            : {
     765                 :          0 :         if (store->sb_page)
     766                 :            :                 chunk += sizeof(bitmap_super_t) << 3;
     767                 :          0 :         return chunk & (PAGE_BITS - 1);
     768                 :            : }
     769                 :            : 
     770                 :            : /*
     771                 :            :  * return a pointer to the page in the filemap that contains the given bit
     772                 :            :  *
     773                 :            :  */
     774                 :          0 : static inline struct page *filemap_get_page(struct bitmap_storage *store,
     775                 :            :                                             unsigned long chunk)
     776                 :            : {
     777   [ #  #  #  #  :          0 :         if (file_page_index(store, chunk) >= store->file_pages)
                   #  # ]
     778                 :            :                 return NULL;
     779                 :          0 :         return store->filemap[file_page_index(store, chunk)];
     780                 :            : }
     781                 :            : 
     782                 :          0 : static int md_bitmap_storage_alloc(struct bitmap_storage *store,
     783                 :            :                                    unsigned long chunks, int with_super,
     784                 :            :                                    int slot_number)
     785                 :            : {
     786                 :          0 :         int pnum, offset = 0;
     787                 :          0 :         unsigned long num_pages;
     788                 :          0 :         unsigned long bytes;
     789                 :            : 
     790                 :          0 :         bytes = DIV_ROUND_UP(chunks, 8);
     791         [ #  # ]:          0 :         if (with_super)
     792                 :          0 :                 bytes += sizeof(bitmap_super_t);
     793                 :            : 
     794                 :          0 :         num_pages = DIV_ROUND_UP(bytes, PAGE_SIZE);
     795                 :          0 :         offset = slot_number * num_pages;
     796                 :            : 
     797                 :          0 :         store->filemap = kmalloc_array(num_pages, sizeof(struct page *),
     798                 :            :                                        GFP_KERNEL);
     799         [ #  # ]:          0 :         if (!store->filemap)
     800                 :            :                 return -ENOMEM;
     801                 :            : 
     802   [ #  #  #  # ]:          0 :         if (with_super && !store->sb_page) {
     803                 :          0 :                 store->sb_page = alloc_page(GFP_KERNEL|__GFP_ZERO);
     804         [ #  # ]:          0 :                 if (store->sb_page == NULL)
     805                 :            :                         return -ENOMEM;
     806                 :            :         }
     807                 :            : 
     808                 :          0 :         pnum = 0;
     809         [ #  # ]:          0 :         if (store->sb_page) {
     810                 :          0 :                 store->filemap[0] = store->sb_page;
     811                 :          0 :                 pnum = 1;
     812                 :          0 :                 store->sb_page->index = offset;
     813                 :            :         }
     814                 :            : 
     815         [ #  # ]:          0 :         for ( ; pnum < num_pages; pnum++) {
     816                 :          0 :                 store->filemap[pnum] = alloc_page(GFP_KERNEL|__GFP_ZERO);
     817         [ #  # ]:          0 :                 if (!store->filemap[pnum]) {
     818                 :          0 :                         store->file_pages = pnum;
     819                 :          0 :                         return -ENOMEM;
     820                 :            :                 }
     821                 :          0 :                 store->filemap[pnum]->index = pnum + offset;
     822                 :            :         }
     823                 :          0 :         store->file_pages = pnum;
     824                 :            : 
     825                 :            :         /* We need 4 bits per page, rounded up to a multiple
     826                 :            :          * of sizeof(unsigned long) */
     827                 :          0 :         store->filemap_attr = kzalloc(
     828                 :          0 :                 roundup(DIV_ROUND_UP(num_pages*4, 8), sizeof(unsigned long)),
     829                 :            :                 GFP_KERNEL);
     830         [ #  # ]:          0 :         if (!store->filemap_attr)
     831                 :            :                 return -ENOMEM;
     832                 :            : 
     833                 :          0 :         store->bytes = bytes;
     834                 :            : 
     835                 :          0 :         return 0;
     836                 :            : }
     837                 :            : 
     838                 :          0 : static void md_bitmap_file_unmap(struct bitmap_storage *store)
     839                 :            : {
     840                 :          0 :         struct page **map, *sb_page;
     841                 :          0 :         int pages;
     842                 :          0 :         struct file *file;
     843                 :            : 
     844                 :          0 :         file = store->file;
     845                 :          0 :         map = store->filemap;
     846                 :          0 :         pages = store->file_pages;
     847                 :          0 :         sb_page = store->sb_page;
     848                 :            : 
     849         [ #  # ]:          0 :         while (pages--)
     850         [ #  # ]:          0 :                 if (map[pages] != sb_page) /* 0 is sb_page, release it below */
     851                 :          0 :                         free_buffers(map[pages]);
     852                 :          0 :         kfree(map);
     853                 :          0 :         kfree(store->filemap_attr);
     854                 :            : 
     855         [ #  # ]:          0 :         if (sb_page)
     856                 :          0 :                 free_buffers(sb_page);
     857                 :            : 
     858         [ #  # ]:          0 :         if (file) {
     859                 :          0 :                 struct inode *inode = file_inode(file);
     860                 :          0 :                 invalidate_mapping_pages(inode->i_mapping, 0, -1);
     861                 :          0 :                 fput(file);
     862                 :            :         }
     863                 :          0 : }
     864                 :            : 
     865                 :            : /*
     866                 :            :  * bitmap_file_kick - if an error occurs while manipulating the bitmap file
     867                 :            :  * then it is no longer reliable, so we stop using it and we mark the file
     868                 :            :  * as failed in the superblock
     869                 :            :  */
     870                 :          0 : static void md_bitmap_file_kick(struct bitmap *bitmap)
     871                 :            : {
     872                 :          0 :         char *path, *ptr = NULL;
     873                 :            : 
     874         [ #  # ]:          0 :         if (!test_and_set_bit(BITMAP_STALE, &bitmap->flags)) {
     875                 :          0 :                 md_bitmap_update_sb(bitmap);
     876                 :            : 
     877         [ #  # ]:          0 :                 if (bitmap->storage.file) {
     878                 :          0 :                         path = kmalloc(PAGE_SIZE, GFP_KERNEL);
     879         [ #  # ]:          0 :                         if (path)
     880                 :          0 :                                 ptr = file_path(bitmap->storage.file,
     881                 :            :                                              path, PAGE_SIZE);
     882                 :            : 
     883   [ #  #  #  # ]:          0 :                         pr_warn("%s: kicking failed bitmap file %s from array!\n",
     884                 :            :                                 bmname(bitmap), IS_ERR(ptr) ? "" : ptr);
     885                 :            : 
     886                 :          0 :                         kfree(path);
     887                 :            :                 } else
     888         [ #  # ]:          0 :                         pr_warn("%s: disabling internal bitmap due to errors\n",
     889                 :            :                                 bmname(bitmap));
     890                 :            :         }
     891                 :          0 : }
     892                 :            : 
     893                 :            : enum bitmap_page_attr {
     894                 :            :         BITMAP_PAGE_DIRTY = 0,     /* there are set bits that need to be synced */
     895                 :            :         BITMAP_PAGE_PENDING = 1,   /* there are bits that are being cleaned.
     896                 :            :                                     * i.e. counter is 1 or 2. */
     897                 :            :         BITMAP_PAGE_NEEDWRITE = 2, /* there are cleared bits that need to be synced */
     898                 :            : };
     899                 :            : 
     900                 :          0 : static inline void set_page_attr(struct bitmap *bitmap, int pnum,
     901                 :            :                                  enum bitmap_page_attr attr)
     902                 :            : {
     903                 :          0 :         set_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
     904                 :          0 : }
     905                 :            : 
     906                 :          0 : static inline void clear_page_attr(struct bitmap *bitmap, int pnum,
     907                 :            :                                    enum bitmap_page_attr attr)
     908                 :            : {
     909                 :          0 :         clear_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
     910                 :            : }
     911                 :            : 
     912                 :          0 : static inline int test_page_attr(struct bitmap *bitmap, int pnum,
     913                 :            :                                  enum bitmap_page_attr attr)
     914                 :            : {
     915                 :          0 :         return test_bit((pnum<<2) + attr, bitmap->storage.filemap_attr);
     916                 :            : }
     917                 :            : 
     918                 :          0 : static inline int test_and_clear_page_attr(struct bitmap *bitmap, int pnum,
     919                 :            :                                            enum bitmap_page_attr attr)
     920                 :            : {
     921                 :          0 :         return test_and_clear_bit((pnum<<2) + attr,
     922                 :            :                                   bitmap->storage.filemap_attr);
     923                 :            : }
     924                 :            : /*
     925                 :            :  * bitmap_file_set_bit -- called before performing a write to the md device
     926                 :            :  * to set (and eventually sync) a particular bit in the bitmap file
     927                 :            :  *
     928                 :            :  * we set the bit immediately, then we record the page number so that
     929                 :            :  * when an unplug occurs, we can flush the dirty pages out to disk
     930                 :            :  */
     931                 :          0 : static void md_bitmap_file_set_bit(struct bitmap *bitmap, sector_t block)
     932                 :            : {
     933                 :          0 :         unsigned long bit;
     934                 :          0 :         struct page *page;
     935                 :          0 :         void *kaddr;
     936                 :          0 :         unsigned long chunk = block >> bitmap->counts.chunkshift;
     937                 :          0 :         struct bitmap_storage *store = &bitmap->storage;
     938                 :          0 :         unsigned long node_offset = 0;
     939                 :            : 
     940         [ #  # ]:          0 :         if (mddev_is_clustered(bitmap->mddev))
     941                 :          0 :                 node_offset = bitmap->cluster_slot * store->file_pages;
     942                 :            : 
     943         [ #  # ]:          0 :         page = filemap_get_page(&bitmap->storage, chunk);
     944         [ #  # ]:          0 :         if (!page)
     945                 :            :                 return;
     946                 :          0 :         bit = file_page_offset(&bitmap->storage, chunk);
     947                 :            : 
     948                 :            :         /* set the bit */
     949                 :          0 :         kaddr = kmap_atomic(page);
     950         [ #  # ]:          0 :         if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
     951                 :          0 :                 set_bit(bit, kaddr);
     952                 :            :         else
     953                 :          0 :                 set_bit_le(bit, kaddr);
     954                 :          0 :         kunmap_atomic(kaddr);
     955                 :          0 :         pr_debug("set file bit %lu page %lu\n", bit, page->index);
     956                 :            :         /* record page number so it gets flushed to disk when unplug occurs */
     957                 :          0 :         set_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_DIRTY);
     958                 :            : }
     959                 :            : 
     960                 :          0 : static void md_bitmap_file_clear_bit(struct bitmap *bitmap, sector_t block)
     961                 :            : {
     962                 :          0 :         unsigned long bit;
     963                 :          0 :         struct page *page;
     964                 :          0 :         void *paddr;
     965                 :          0 :         unsigned long chunk = block >> bitmap->counts.chunkshift;
     966                 :          0 :         struct bitmap_storage *store = &bitmap->storage;
     967                 :          0 :         unsigned long node_offset = 0;
     968                 :            : 
     969         [ #  # ]:          0 :         if (mddev_is_clustered(bitmap->mddev))
     970                 :          0 :                 node_offset = bitmap->cluster_slot * store->file_pages;
     971                 :            : 
     972         [ #  # ]:          0 :         page = filemap_get_page(&bitmap->storage, chunk);
     973         [ #  # ]:          0 :         if (!page)
     974                 :            :                 return;
     975                 :          0 :         bit = file_page_offset(&bitmap->storage, chunk);
     976                 :          0 :         paddr = kmap_atomic(page);
     977         [ #  # ]:          0 :         if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
     978                 :          0 :                 clear_bit(bit, paddr);
     979                 :            :         else
     980                 :          0 :                 clear_bit_le(bit, paddr);
     981                 :          0 :         kunmap_atomic(paddr);
     982         [ #  # ]:          0 :         if (!test_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_NEEDWRITE)) {
     983                 :          0 :                 set_page_attr(bitmap, page->index - node_offset, BITMAP_PAGE_PENDING);
     984                 :          0 :                 bitmap->allclean = 0;
     985                 :            :         }
     986                 :            : }
     987                 :            : 
     988                 :          0 : static int md_bitmap_file_test_bit(struct bitmap *bitmap, sector_t block)
     989                 :            : {
     990                 :          0 :         unsigned long bit;
     991                 :          0 :         struct page *page;
     992                 :          0 :         void *paddr;
     993                 :          0 :         unsigned long chunk = block >> bitmap->counts.chunkshift;
     994                 :          0 :         int set = 0;
     995                 :            : 
     996         [ #  # ]:          0 :         page = filemap_get_page(&bitmap->storage, chunk);
     997         [ #  # ]:          0 :         if (!page)
     998                 :            :                 return -EINVAL;
     999                 :          0 :         bit = file_page_offset(&bitmap->storage, chunk);
    1000                 :          0 :         paddr = kmap_atomic(page);
    1001         [ #  # ]:          0 :         if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
    1002                 :          0 :                 set = test_bit(bit, paddr);
    1003                 :            :         else
    1004                 :          0 :                 set = test_bit_le(bit, paddr);
    1005                 :          0 :         kunmap_atomic(paddr);
    1006                 :          0 :         return set;
    1007                 :            : }
    1008                 :            : 
    1009                 :            : 
    1010                 :            : /* this gets called when the md device is ready to unplug its underlying
    1011                 :            :  * (slave) device queues -- before we let any writes go down, we need to
    1012                 :            :  * sync the dirty pages of the bitmap file to disk */
    1013                 :          0 : void md_bitmap_unplug(struct bitmap *bitmap)
    1014                 :            : {
    1015                 :          0 :         unsigned long i;
    1016                 :          0 :         int dirty, need_write;
    1017                 :          0 :         int writing = 0;
    1018                 :            : 
    1019   [ #  #  #  #  :          0 :         if (!bitmap || !bitmap->storage.filemap ||
                   #  # ]
    1020                 :          0 :             test_bit(BITMAP_STALE, &bitmap->flags))
    1021                 :          0 :                 return;
    1022                 :            : 
    1023                 :            :         /* look at each page to see if there are any set bits that need to be
    1024                 :            :          * flushed out to disk */
    1025         [ #  # ]:          0 :         for (i = 0; i < bitmap->storage.file_pages; i++) {
    1026                 :          0 :                 dirty = test_and_clear_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
    1027                 :          0 :                 need_write = test_and_clear_page_attr(bitmap, i,
    1028                 :            :                                                       BITMAP_PAGE_NEEDWRITE);
    1029         [ #  # ]:          0 :                 if (dirty || need_write) {
    1030         [ #  # ]:          0 :                         if (!writing) {
    1031                 :          0 :                                 md_bitmap_wait_writes(bitmap);
    1032         [ #  # ]:          0 :                                 if (bitmap->mddev->queue)
    1033         [ #  # ]:          0 :                                         blk_add_trace_msg(bitmap->mddev->queue,
    1034                 :            :                                                           "md bitmap_unplug");
    1035                 :            :                         }
    1036                 :          0 :                         clear_page_attr(bitmap, i, BITMAP_PAGE_PENDING);
    1037                 :          0 :                         write_page(bitmap, bitmap->storage.filemap[i], 0);
    1038                 :          0 :                         writing = 1;
    1039                 :            :                 }
    1040                 :            :         }
    1041         [ #  # ]:          0 :         if (writing)
    1042                 :          0 :                 md_bitmap_wait_writes(bitmap);
    1043                 :            : 
    1044         [ #  # ]:          0 :         if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
    1045                 :          0 :                 md_bitmap_file_kick(bitmap);
    1046                 :            : }
    1047                 :            : EXPORT_SYMBOL(md_bitmap_unplug);
    1048                 :            : 
    1049                 :            : static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed);
    1050                 :            : /* * bitmap_init_from_disk -- called at bitmap_create time to initialize
    1051                 :            :  * the in-memory bitmap from the on-disk bitmap -- also, sets up the
    1052                 :            :  * memory mapping of the bitmap file
    1053                 :            :  * Special cases:
    1054                 :            :  *   if there's no bitmap file, or if the bitmap file had been
    1055                 :            :  *   previously kicked from the array, we mark all the bits as
    1056                 :            :  *   1's in order to cause a full resync.
    1057                 :            :  *
    1058                 :            :  * We ignore all bits for sectors that end earlier than 'start'.
    1059                 :            :  * This is used when reading an out-of-date bitmap...
    1060                 :            :  */
    1061                 :          0 : static int md_bitmap_init_from_disk(struct bitmap *bitmap, sector_t start)
    1062                 :            : {
    1063                 :          0 :         unsigned long i, chunks, index, oldindex, bit, node_offset = 0;
    1064                 :          0 :         struct page *page = NULL;
    1065                 :          0 :         unsigned long bit_cnt = 0;
    1066                 :          0 :         struct file *file;
    1067                 :          0 :         unsigned long offset;
    1068                 :          0 :         int outofdate;
    1069                 :          0 :         int ret = -ENOSPC;
    1070                 :          0 :         void *paddr;
    1071                 :          0 :         struct bitmap_storage *store = &bitmap->storage;
    1072                 :            : 
    1073                 :          0 :         chunks = bitmap->counts.chunks;
    1074                 :          0 :         file = store->file;
    1075                 :            : 
    1076   [ #  #  #  # ]:          0 :         if (!file && !bitmap->mddev->bitmap_info.offset) {
    1077                 :            :                 /* No permanent bitmap - fill with '1s'. */
    1078                 :          0 :                 store->filemap = NULL;
    1079                 :          0 :                 store->file_pages = 0;
    1080         [ #  # ]:          0 :                 for (i = 0; i < chunks ; i++) {
    1081                 :            :                         /* if the disk bit is set, set the memory bit */
    1082                 :          0 :                         int needed = ((sector_t)(i+1) << (bitmap->counts.chunkshift)
    1083                 :          0 :                                       >= start);
    1084                 :          0 :                         md_bitmap_set_memory_bits(bitmap,
    1085                 :            :                                                   (sector_t)i << bitmap->counts.chunkshift,
    1086                 :            :                                                   needed);
    1087                 :            :                 }
    1088                 :            :                 return 0;
    1089                 :            :         }
    1090                 :            : 
    1091                 :          0 :         outofdate = test_bit(BITMAP_STALE, &bitmap->flags);
    1092         [ #  # ]:          0 :         if (outofdate)
    1093         [ #  # ]:          0 :                 pr_warn("%s: bitmap file is out of date, doing full recovery\n", bmname(bitmap));
    1094                 :            : 
    1095   [ #  #  #  # ]:          0 :         if (file && i_size_read(file->f_mapping->host) < store->bytes) {
    1096         [ #  # ]:          0 :                 pr_warn("%s: bitmap file too short %lu < %lu\n",
    1097                 :            :                         bmname(bitmap),
    1098                 :            :                         (unsigned long) i_size_read(file->f_mapping->host),
    1099                 :            :                         store->bytes);
    1100                 :          0 :                 goto err;
    1101                 :            :         }
    1102                 :            : 
    1103                 :          0 :         oldindex = ~0L;
    1104                 :          0 :         offset = 0;
    1105         [ #  # ]:          0 :         if (!bitmap->mddev->bitmap_info.external)
    1106                 :          0 :                 offset = sizeof(bitmap_super_t);
    1107                 :            : 
    1108         [ #  # ]:          0 :         if (mddev_is_clustered(bitmap->mddev))
    1109                 :          0 :                 node_offset = bitmap->cluster_slot * (DIV_ROUND_UP(store->bytes, PAGE_SIZE));
    1110                 :            : 
    1111         [ #  # ]:          0 :         for (i = 0; i < chunks; i++) {
    1112                 :          0 :                 int b;
    1113         [ #  # ]:          0 :                 index = file_page_index(&bitmap->storage, i);
    1114                 :          0 :                 bit = file_page_offset(&bitmap->storage, i);
    1115         [ #  # ]:          0 :                 if (index != oldindex) { /* this is a new page, read it in */
    1116                 :          0 :                         int count;
    1117                 :            :                         /* unmap the old page, we're done with it */
    1118         [ #  # ]:          0 :                         if (index == store->file_pages-1)
    1119                 :          0 :                                 count = store->bytes - index * PAGE_SIZE;
    1120                 :            :                         else
    1121                 :            :                                 count = PAGE_SIZE;
    1122                 :          0 :                         page = store->filemap[index];
    1123         [ #  # ]:          0 :                         if (file)
    1124                 :          0 :                                 ret = read_page(file, index, bitmap,
    1125                 :            :                                                 count, page);
    1126                 :            :                         else
    1127                 :          0 :                                 ret = read_sb_page(
    1128                 :            :                                         bitmap->mddev,
    1129                 :          0 :                                         bitmap->mddev->bitmap_info.offset,
    1130                 :            :                                         page,
    1131                 :            :                                         index + node_offset, count);
    1132                 :            : 
    1133         [ #  # ]:          0 :                         if (ret)
    1134                 :          0 :                                 goto err;
    1135                 :            : 
    1136                 :          0 :                         oldindex = index;
    1137                 :            : 
    1138         [ #  # ]:          0 :                         if (outofdate) {
    1139                 :            :                                 /*
    1140                 :            :                                  * if bitmap is out of date, dirty the
    1141                 :            :                                  * whole page and write it out
    1142                 :            :                                  */
    1143                 :          0 :                                 paddr = kmap_atomic(page);
    1144                 :          0 :                                 memset(paddr + offset, 0xff,
    1145                 :            :                                        PAGE_SIZE - offset);
    1146                 :          0 :                                 kunmap_atomic(paddr);
    1147                 :          0 :                                 write_page(bitmap, page, 1);
    1148                 :            : 
    1149                 :          0 :                                 ret = -EIO;
    1150         [ #  # ]:          0 :                                 if (test_bit(BITMAP_WRITE_ERROR,
    1151                 :            :                                              &bitmap->flags))
    1152                 :          0 :                                         goto err;
    1153                 :            :                         }
    1154                 :            :                 }
    1155                 :          0 :                 paddr = kmap_atomic(page);
    1156         [ #  # ]:          0 :                 if (test_bit(BITMAP_HOSTENDIAN, &bitmap->flags))
    1157                 :          0 :                         b = test_bit(bit, paddr);
    1158                 :            :                 else
    1159                 :          0 :                         b = test_bit_le(bit, paddr);
    1160                 :          0 :                 kunmap_atomic(paddr);
    1161         [ #  # ]:          0 :                 if (b) {
    1162                 :            :                         /* if the disk bit is set, set the memory bit */
    1163                 :          0 :                         int needed = ((sector_t)(i+1) << bitmap->counts.chunkshift
    1164                 :          0 :                                       >= start);
    1165                 :          0 :                         md_bitmap_set_memory_bits(bitmap,
    1166                 :            :                                                   (sector_t)i << bitmap->counts.chunkshift,
    1167                 :            :                                                   needed);
    1168                 :          0 :                         bit_cnt++;
    1169                 :            :                 }
    1170                 :          0 :                 offset = 0;
    1171                 :            :         }
    1172                 :            : 
    1173                 :            :         pr_debug("%s: bitmap initialized from disk: read %lu pages, set %lu of %lu bits\n",
    1174                 :            :                  bmname(bitmap), store->file_pages,
    1175                 :            :                  bit_cnt, chunks);
    1176                 :            : 
    1177                 :            :         return 0;
    1178                 :            : 
    1179                 :          0 :  err:
    1180         [ #  # ]:          0 :         pr_warn("%s: bitmap initialisation failed: %d\n",
    1181                 :            :                 bmname(bitmap), ret);
    1182                 :          0 :         return ret;
    1183                 :            : }
    1184                 :            : 
    1185                 :          0 : void md_bitmap_write_all(struct bitmap *bitmap)
    1186                 :            : {
    1187                 :            :         /* We don't actually write all bitmap blocks here,
    1188                 :            :          * just flag them as needing to be written
    1189                 :            :          */
    1190                 :          0 :         int i;
    1191                 :            : 
    1192   [ #  #  #  # ]:          0 :         if (!bitmap || !bitmap->storage.filemap)
    1193                 :            :                 return;
    1194         [ #  # ]:          0 :         if (bitmap->storage.file)
    1195                 :            :                 /* Only one copy, so nothing needed */
    1196                 :            :                 return;
    1197                 :            : 
    1198         [ #  # ]:          0 :         for (i = 0; i < bitmap->storage.file_pages; i++)
    1199                 :          0 :                 set_page_attr(bitmap, i,
    1200                 :            :                               BITMAP_PAGE_NEEDWRITE);
    1201                 :          0 :         bitmap->allclean = 0;
    1202                 :            : }
    1203                 :            : 
    1204                 :          0 : static void md_bitmap_count_page(struct bitmap_counts *bitmap,
    1205                 :            :                                  sector_t offset, int inc)
    1206                 :            : {
    1207                 :          0 :         sector_t chunk = offset >> bitmap->chunkshift;
    1208                 :          0 :         unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
    1209                 :          0 :         bitmap->bp[page].count += inc;
    1210                 :          0 :         md_bitmap_checkfree(bitmap, page);
    1211                 :          0 : }
    1212                 :            : 
    1213                 :          0 : static void md_bitmap_set_pending(struct bitmap_counts *bitmap, sector_t offset)
    1214                 :            : {
    1215                 :          0 :         sector_t chunk = offset >> bitmap->chunkshift;
    1216                 :          0 :         unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
    1217                 :          0 :         struct bitmap_page *bp = &bitmap->bp[page];
    1218                 :            : 
    1219                 :          0 :         if (!bp->pending)
    1220                 :          0 :                 bp->pending = 1;
    1221                 :            : }
    1222                 :            : 
    1223                 :            : static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap,
    1224                 :            :                                                sector_t offset, sector_t *blocks,
    1225                 :            :                                                int create);
    1226                 :            : 
    1227                 :            : /*
    1228                 :            :  * bitmap daemon -- periodically wakes up to clean bits and flush pages
    1229                 :            :  *                      out to disk
    1230                 :            :  */
    1231                 :            : 
    1232                 :          0 : void md_bitmap_daemon_work(struct mddev *mddev)
    1233                 :            : {
    1234                 :          0 :         struct bitmap *bitmap;
    1235                 :          0 :         unsigned long j;
    1236                 :          0 :         unsigned long nextpage;
    1237                 :          0 :         sector_t blocks;
    1238                 :          0 :         struct bitmap_counts *counts;
    1239                 :            : 
    1240                 :            :         /* Use a mutex to guard daemon_work against
    1241                 :            :          * bitmap_destroy.
    1242                 :            :          */
    1243                 :          0 :         mutex_lock(&mddev->bitmap_info.mutex);
    1244                 :          0 :         bitmap = mddev->bitmap;
    1245         [ #  # ]:          0 :         if (bitmap == NULL) {
    1246                 :          0 :                 mutex_unlock(&mddev->bitmap_info.mutex);
    1247                 :          0 :                 return;
    1248                 :            :         }
    1249         [ #  # ]:          0 :         if (time_before(jiffies, bitmap->daemon_lastrun
    1250                 :            :                         + mddev->bitmap_info.daemon_sleep))
    1251                 :          0 :                 goto done;
    1252                 :            : 
    1253                 :          0 :         bitmap->daemon_lastrun = jiffies;
    1254         [ #  # ]:          0 :         if (bitmap->allclean) {
    1255                 :          0 :                 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
    1256                 :          0 :                 goto done;
    1257                 :            :         }
    1258                 :          0 :         bitmap->allclean = 1;
    1259                 :            : 
    1260         [ #  # ]:          0 :         if (bitmap->mddev->queue)
    1261         [ #  # ]:          0 :                 blk_add_trace_msg(bitmap->mddev->queue,
    1262                 :            :                                   "md bitmap_daemon_work");
    1263                 :            : 
    1264                 :            :         /* Any file-page which is PENDING now needs to be written.
    1265                 :            :          * So set NEEDWRITE now, then after we make any last-minute changes
    1266                 :            :          * we will write it.
    1267                 :            :          */
    1268         [ #  # ]:          0 :         for (j = 0; j < bitmap->storage.file_pages; j++)
    1269         [ #  # ]:          0 :                 if (test_and_clear_page_attr(bitmap, j,
    1270                 :            :                                              BITMAP_PAGE_PENDING))
    1271                 :          0 :                         set_page_attr(bitmap, j,
    1272                 :            :                                       BITMAP_PAGE_NEEDWRITE);
    1273                 :            : 
    1274         [ #  # ]:          0 :         if (bitmap->need_sync &&
    1275         [ #  # ]:          0 :             mddev->bitmap_info.external == 0) {
    1276                 :            :                 /* Arrange for superblock update as well as
    1277                 :            :                  * other changes */
    1278                 :          0 :                 bitmap_super_t *sb;
    1279                 :          0 :                 bitmap->need_sync = 0;
    1280         [ #  # ]:          0 :                 if (bitmap->storage.filemap) {
    1281                 :          0 :                         sb = kmap_atomic(bitmap->storage.sb_page);
    1282                 :          0 :                         sb->events_cleared =
    1283                 :          0 :                                 cpu_to_le64(bitmap->events_cleared);
    1284                 :          0 :                         kunmap_atomic(sb);
    1285                 :          0 :                         set_page_attr(bitmap, 0,
    1286                 :            :                                       BITMAP_PAGE_NEEDWRITE);
    1287                 :            :                 }
    1288                 :            :         }
    1289                 :            :         /* Now look at the bitmap counters and if any are '2' or '1',
    1290                 :            :          * decrement and handle accordingly.
    1291                 :            :          */
    1292                 :          0 :         counts = &bitmap->counts;
    1293                 :          0 :         spin_lock_irq(&counts->lock);
    1294                 :          0 :         nextpage = 0;
    1295         [ #  # ]:          0 :         for (j = 0; j < counts->chunks; j++) {
    1296                 :          0 :                 bitmap_counter_t *bmc;
    1297                 :          0 :                 sector_t  block = (sector_t)j << counts->chunkshift;
    1298                 :            : 
    1299         [ #  # ]:          0 :                 if (j == nextpage) {
    1300                 :          0 :                         nextpage += PAGE_COUNTER_RATIO;
    1301         [ #  # ]:          0 :                         if (!counts->bp[j >> PAGE_COUNTER_SHIFT].pending) {
    1302                 :          0 :                                 j |= PAGE_COUNTER_MASK;
    1303                 :          0 :                                 continue;
    1304                 :            :                         }
    1305                 :          0 :                         counts->bp[j >> PAGE_COUNTER_SHIFT].pending = 0;
    1306                 :            :                 }
    1307                 :            : 
    1308                 :          0 :                 bmc = md_bitmap_get_counter(counts, block, &blocks, 0);
    1309         [ #  # ]:          0 :                 if (!bmc) {
    1310                 :          0 :                         j |= PAGE_COUNTER_MASK;
    1311                 :          0 :                         continue;
    1312                 :            :                 }
    1313   [ #  #  #  # ]:          0 :                 if (*bmc == 1 && !bitmap->need_sync) {
    1314                 :            :                         /* We can clear the bit */
    1315                 :          0 :                         *bmc = 0;
    1316                 :          0 :                         md_bitmap_count_page(counts, block, -1);
    1317                 :          0 :                         md_bitmap_file_clear_bit(bitmap, block);
    1318         [ #  # ]:          0 :                 } else if (*bmc && *bmc <= 2) {
    1319                 :          0 :                         *bmc = 1;
    1320         [ #  # ]:          0 :                         md_bitmap_set_pending(counts, block);
    1321                 :          0 :                         bitmap->allclean = 0;
    1322                 :            :                 }
    1323                 :            :         }
    1324                 :          0 :         spin_unlock_irq(&counts->lock);
    1325                 :            : 
    1326                 :          0 :         md_bitmap_wait_writes(bitmap);
    1327                 :            :         /* Now start writeout on any page in NEEDWRITE that isn't DIRTY.
    1328                 :            :          * DIRTY pages need to be written by bitmap_unplug so it can wait
    1329                 :            :          * for them.
    1330                 :            :          * If we find any DIRTY page we stop there and let bitmap_unplug
    1331                 :            :          * handle all the rest.  This is important in the case where
    1332                 :            :          * the first blocking holds the superblock and it has been updated.
    1333                 :            :          * We mustn't write any other blocks before the superblock.
    1334                 :            :          */
    1335                 :          0 :         for (j = 0;
    1336         [ #  # ]:          0 :              j < bitmap->storage.file_pages
    1337         [ #  # ]:          0 :                      && !test_bit(BITMAP_STALE, &bitmap->flags);
    1338                 :          0 :              j++) {
    1339         [ #  # ]:          0 :                 if (test_page_attr(bitmap, j,
    1340                 :            :                                    BITMAP_PAGE_DIRTY))
    1341                 :            :                         /* bitmap_unplug will handle the rest */
    1342                 :            :                         break;
    1343   [ #  #  #  # ]:          0 :                 if (bitmap->storage.filemap &&
    1344                 :          0 :                     test_and_clear_page_attr(bitmap, j,
    1345                 :            :                                              BITMAP_PAGE_NEEDWRITE)) {
    1346                 :          0 :                         write_page(bitmap, bitmap->storage.filemap[j], 0);
    1347                 :            :                 }
    1348                 :            :         }
    1349                 :            : 
    1350                 :          0 :  done:
    1351         [ #  # ]:          0 :         if (bitmap->allclean == 0)
    1352                 :          0 :                 mddev->thread->timeout =
    1353                 :          0 :                         mddev->bitmap_info.daemon_sleep;
    1354                 :          0 :         mutex_unlock(&mddev->bitmap_info.mutex);
    1355                 :            : }
    1356                 :            : 
    1357                 :          0 : static bitmap_counter_t *md_bitmap_get_counter(struct bitmap_counts *bitmap,
    1358                 :            :                                                sector_t offset, sector_t *blocks,
    1359                 :            :                                                int create)
    1360                 :            : __releases(bitmap->lock)
    1361                 :            : __acquires(bitmap->lock)
    1362                 :            : {
    1363                 :            :         /* If 'create', we might release the lock and reclaim it.
    1364                 :            :          * The lock must have been taken with interrupts enabled.
    1365                 :            :          * If !create, we don't release the lock.
    1366                 :            :          */
    1367                 :          0 :         sector_t chunk = offset >> bitmap->chunkshift;
    1368                 :          0 :         unsigned long page = chunk >> PAGE_COUNTER_SHIFT;
    1369                 :          0 :         unsigned long pageoff = (chunk & PAGE_COUNTER_MASK) << COUNTER_BYTE_SHIFT;
    1370                 :          0 :         sector_t csize;
    1371                 :          0 :         int err;
    1372                 :            : 
    1373                 :          0 :         err = md_bitmap_checkpage(bitmap, page, create, 0);
    1374                 :            : 
    1375         [ #  # ]:          0 :         if (bitmap->bp[page].hijacked ||
    1376         [ #  # ]:          0 :             bitmap->bp[page].map == NULL)
    1377                 :          0 :                 csize = ((sector_t)1) << (bitmap->chunkshift +
    1378                 :          0 :                                           PAGE_COUNTER_SHIFT - 1);
    1379                 :            :         else
    1380                 :          0 :                 csize = ((sector_t)1) << bitmap->chunkshift;
    1381                 :          0 :         *blocks = csize - (offset & (csize - 1));
    1382                 :            : 
    1383         [ #  # ]:          0 :         if (err < 0)
    1384                 :            :                 return NULL;
    1385                 :            : 
    1386                 :            :         /* now locked ... */
    1387                 :            : 
    1388         [ #  # ]:          0 :         if (bitmap->bp[page].hijacked) { /* hijacked pointer */
    1389                 :            :                 /* should we use the first or second counter field
    1390                 :            :                  * of the hijacked pointer? */
    1391                 :          0 :                 int hi = (pageoff > PAGE_COUNTER_MASK);
    1392                 :          0 :                 return  &((bitmap_counter_t *)
    1393                 :          0 :                           &bitmap->bp[page].map)[hi];
    1394                 :            :         } else /* page is allocated */
    1395                 :          0 :                 return (bitmap_counter_t *)
    1396                 :          0 :                         &(bitmap->bp[page].map[pageoff]);
    1397                 :            : }
    1398                 :            : 
    1399                 :          0 : int md_bitmap_startwrite(struct bitmap *bitmap, sector_t offset, unsigned long sectors, int behind)
    1400                 :            : {
    1401         [ #  # ]:          0 :         if (!bitmap)
    1402                 :            :                 return 0;
    1403                 :            : 
    1404         [ #  # ]:          0 :         if (behind) {
    1405                 :          0 :                 int bw;
    1406                 :          0 :                 atomic_inc(&bitmap->behind_writes);
    1407                 :          0 :                 bw = atomic_read(&bitmap->behind_writes);
    1408         [ #  # ]:          0 :                 if (bw > bitmap->behind_writes_used)
    1409                 :          0 :                         bitmap->behind_writes_used = bw;
    1410                 :            : 
    1411                 :            :                 pr_debug("inc write-behind count %d/%lu\n",
    1412                 :            :                          bw, bitmap->mddev->bitmap_info.max_write_behind);
    1413                 :            :         }
    1414                 :            : 
    1415         [ #  # ]:          0 :         while (sectors) {
    1416                 :          0 :                 sector_t blocks;
    1417                 :          0 :                 bitmap_counter_t *bmc;
    1418                 :            : 
    1419                 :          0 :                 spin_lock_irq(&bitmap->counts.lock);
    1420                 :          0 :                 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 1);
    1421         [ #  # ]:          0 :                 if (!bmc) {
    1422                 :          0 :                         spin_unlock_irq(&bitmap->counts.lock);
    1423                 :          0 :                         return 0;
    1424                 :            :                 }
    1425                 :            : 
    1426         [ #  # ]:          0 :                 if (unlikely(COUNTER(*bmc) == COUNTER_MAX)) {
    1427                 :          0 :                         DEFINE_WAIT(__wait);
    1428                 :            :                         /* note that it is safe to do the prepare_to_wait
    1429                 :            :                          * after the test as long as we do it before dropping
    1430                 :            :                          * the spinlock.
    1431                 :            :                          */
    1432                 :          0 :                         prepare_to_wait(&bitmap->overflow_wait, &__wait,
    1433                 :            :                                         TASK_UNINTERRUPTIBLE);
    1434                 :          0 :                         spin_unlock_irq(&bitmap->counts.lock);
    1435                 :          0 :                         schedule();
    1436                 :          0 :                         finish_wait(&bitmap->overflow_wait, &__wait);
    1437                 :          0 :                         continue;
    1438                 :            :                 }
    1439                 :            : 
    1440      [ #  #  # ]:          0 :                 switch (*bmc) {
    1441                 :          0 :                 case 0:
    1442                 :          0 :                         md_bitmap_file_set_bit(bitmap, offset);
    1443                 :          0 :                         md_bitmap_count_page(&bitmap->counts, offset, 1);
    1444                 :            :                         /* fall through */
    1445                 :          0 :                 case 1:
    1446                 :          0 :                         *bmc = 2;
    1447                 :            :                 }
    1448                 :            : 
    1449                 :          0 :                 (*bmc)++;
    1450                 :            : 
    1451                 :          0 :                 spin_unlock_irq(&bitmap->counts.lock);
    1452                 :            : 
    1453                 :          0 :                 offset += blocks;
    1454         [ #  # ]:          0 :                 if (sectors > blocks)
    1455                 :          0 :                         sectors -= blocks;
    1456                 :            :                 else
    1457                 :            :                         sectors = 0;
    1458                 :            :         }
    1459                 :            :         return 0;
    1460                 :            : }
    1461                 :            : EXPORT_SYMBOL(md_bitmap_startwrite);
    1462                 :            : 
    1463                 :          0 : void md_bitmap_endwrite(struct bitmap *bitmap, sector_t offset,
    1464                 :            :                         unsigned long sectors, int success, int behind)
    1465                 :            : {
    1466         [ #  # ]:          0 :         if (!bitmap)
    1467                 :            :                 return;
    1468         [ #  # ]:          0 :         if (behind) {
    1469         [ #  # ]:          0 :                 if (atomic_dec_and_test(&bitmap->behind_writes))
    1470                 :          0 :                         wake_up(&bitmap->behind_wait);
    1471                 :            :                 pr_debug("dec write-behind count %d/%lu\n",
    1472                 :            :                          atomic_read(&bitmap->behind_writes),
    1473                 :            :                          bitmap->mddev->bitmap_info.max_write_behind);
    1474                 :            :         }
    1475                 :            : 
    1476         [ #  # ]:          0 :         while (sectors) {
    1477                 :          0 :                 sector_t blocks;
    1478                 :          0 :                 unsigned long flags;
    1479                 :          0 :                 bitmap_counter_t *bmc;
    1480                 :            : 
    1481                 :          0 :                 spin_lock_irqsave(&bitmap->counts.lock, flags);
    1482                 :          0 :                 bmc = md_bitmap_get_counter(&bitmap->counts, offset, &blocks, 0);
    1483         [ #  # ]:          0 :                 if (!bmc) {
    1484                 :          0 :                         spin_unlock_irqrestore(&bitmap->counts.lock, flags);
    1485                 :          0 :                         return;
    1486                 :            :                 }
    1487                 :            : 
    1488   [ #  #  #  # ]:          0 :                 if (success && !bitmap->mddev->degraded &&
    1489         [ #  # ]:          0 :                     bitmap->events_cleared < bitmap->mddev->events) {
    1490                 :          0 :                         bitmap->events_cleared = bitmap->mddev->events;
    1491                 :          0 :                         bitmap->need_sync = 1;
    1492         [ #  # ]:          0 :                         sysfs_notify_dirent_safe(bitmap->sysfs_can_clear);
    1493                 :            :                 }
    1494                 :            : 
    1495   [ #  #  #  # ]:          0 :                 if (!success && !NEEDED(*bmc))
    1496                 :          0 :                         *bmc |= NEEDED_MASK;
    1497                 :            : 
    1498         [ #  # ]:          0 :                 if (COUNTER(*bmc) == COUNTER_MAX)
    1499                 :          0 :                         wake_up(&bitmap->overflow_wait);
    1500                 :            : 
    1501                 :          0 :                 (*bmc)--;
    1502         [ #  # ]:          0 :                 if (*bmc <= 2) {
    1503         [ #  # ]:          0 :                         md_bitmap_set_pending(&bitmap->counts, offset);
    1504                 :          0 :                         bitmap->allclean = 0;
    1505                 :            :                 }
    1506                 :          0 :                 spin_unlock_irqrestore(&bitmap->counts.lock, flags);
    1507                 :          0 :                 offset += blocks;
    1508         [ #  # ]:          0 :                 if (sectors > blocks)
    1509                 :          0 :                         sectors -= blocks;
    1510                 :            :                 else
    1511                 :            :                         sectors = 0;
    1512                 :            :         }
    1513                 :            : }
    1514                 :            : EXPORT_SYMBOL(md_bitmap_endwrite);
    1515                 :            : 
    1516                 :          0 : static int __bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
    1517                 :            :                                int degraded)
    1518                 :            : {
    1519                 :          0 :         bitmap_counter_t *bmc;
    1520                 :          0 :         int rv;
    1521         [ #  # ]:          0 :         if (bitmap == NULL) {/* FIXME or bitmap set as 'failed' */
    1522                 :          0 :                 *blocks = 1024;
    1523                 :          0 :                 return 1; /* always resync if no bitmap */
    1524                 :            :         }
    1525                 :          0 :         spin_lock_irq(&bitmap->counts.lock);
    1526                 :          0 :         bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
    1527                 :          0 :         rv = 0;
    1528         [ #  # ]:          0 :         if (bmc) {
    1529                 :            :                 /* locked */
    1530         [ #  # ]:          0 :                 if (RESYNC(*bmc))
    1531                 :            :                         rv = 1;
    1532         [ #  # ]:          0 :                 else if (NEEDED(*bmc)) {
    1533                 :          0 :                         rv = 1;
    1534         [ #  # ]:          0 :                         if (!degraded) { /* don't set/clear bits if degraded */
    1535                 :          0 :                                 *bmc |= RESYNC_MASK;
    1536                 :          0 :                                 *bmc &= ~NEEDED_MASK;
    1537                 :            :                         }
    1538                 :            :                 }
    1539                 :            :         }
    1540                 :          0 :         spin_unlock_irq(&bitmap->counts.lock);
    1541                 :          0 :         return rv;
    1542                 :            : }
    1543                 :            : 
    1544                 :          0 : int md_bitmap_start_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks,
    1545                 :            :                          int degraded)
    1546                 :            : {
    1547                 :            :         /* bitmap_start_sync must always report on multiples of whole
    1548                 :            :          * pages, otherwise resync (which is very PAGE_SIZE based) will
    1549                 :            :          * get confused.
    1550                 :            :          * So call __bitmap_start_sync repeatedly (if needed) until
    1551                 :            :          * At least PAGE_SIZE>>9 blocks are covered.
    1552                 :            :          * Return the 'or' of the result.
    1553                 :            :          */
    1554                 :          0 :         int rv = 0;
    1555                 :          0 :         sector_t blocks1;
    1556                 :            : 
    1557                 :          0 :         *blocks = 0;
    1558         [ #  # ]:          0 :         while (*blocks < (PAGE_SIZE>>9)) {
    1559                 :          0 :                 rv |= __bitmap_start_sync(bitmap, offset,
    1560                 :            :                                           &blocks1, degraded);
    1561                 :          0 :                 offset += blocks1;
    1562                 :          0 :                 *blocks += blocks1;
    1563                 :            :         }
    1564                 :          0 :         return rv;
    1565                 :            : }
    1566                 :            : EXPORT_SYMBOL(md_bitmap_start_sync);
    1567                 :            : 
    1568                 :          0 : void md_bitmap_end_sync(struct bitmap *bitmap, sector_t offset, sector_t *blocks, int aborted)
    1569                 :            : {
    1570                 :          0 :         bitmap_counter_t *bmc;
    1571                 :          0 :         unsigned long flags;
    1572                 :            : 
    1573         [ #  # ]:          0 :         if (bitmap == NULL) {
    1574                 :          0 :                 *blocks = 1024;
    1575                 :          0 :                 return;
    1576                 :            :         }
    1577                 :          0 :         spin_lock_irqsave(&bitmap->counts.lock, flags);
    1578                 :          0 :         bmc = md_bitmap_get_counter(&bitmap->counts, offset, blocks, 0);
    1579         [ #  # ]:          0 :         if (bmc == NULL)
    1580                 :          0 :                 goto unlock;
    1581                 :            :         /* locked */
    1582         [ #  # ]:          0 :         if (RESYNC(*bmc)) {
    1583                 :          0 :                 *bmc &= ~RESYNC_MASK;
    1584                 :            : 
    1585   [ #  #  #  # ]:          0 :                 if (!NEEDED(*bmc) && aborted)
    1586                 :          0 :                         *bmc |= NEEDED_MASK;
    1587                 :            :                 else {
    1588         [ #  # ]:          0 :                         if (*bmc <= 2) {
    1589         [ #  # ]:          0 :                                 md_bitmap_set_pending(&bitmap->counts, offset);
    1590                 :          0 :                                 bitmap->allclean = 0;
    1591                 :            :                         }
    1592                 :            :                 }
    1593                 :            :         }
    1594                 :          0 :  unlock:
    1595                 :          0 :         spin_unlock_irqrestore(&bitmap->counts.lock, flags);
    1596                 :            : }
    1597                 :            : EXPORT_SYMBOL(md_bitmap_end_sync);
    1598                 :            : 
    1599                 :          0 : void md_bitmap_close_sync(struct bitmap *bitmap)
    1600                 :            : {
    1601                 :            :         /* Sync has finished, and any bitmap chunks that weren't synced
    1602                 :            :          * properly have been aborted.  It remains to us to clear the
    1603                 :            :          * RESYNC bit wherever it is still on
    1604                 :            :          */
    1605                 :          0 :         sector_t sector = 0;
    1606                 :          0 :         sector_t blocks;
    1607         [ #  # ]:          0 :         if (!bitmap)
    1608                 :          0 :                 return;
    1609         [ #  # ]:          0 :         while (sector < bitmap->mddev->resync_max_sectors) {
    1610                 :          0 :                 md_bitmap_end_sync(bitmap, sector, &blocks, 0);
    1611                 :          0 :                 sector += blocks;
    1612                 :            :         }
    1613                 :            : }
    1614                 :            : EXPORT_SYMBOL(md_bitmap_close_sync);
    1615                 :            : 
    1616                 :          0 : void md_bitmap_cond_end_sync(struct bitmap *bitmap, sector_t sector, bool force)
    1617                 :            : {
    1618                 :          0 :         sector_t s = 0;
    1619                 :          0 :         sector_t blocks;
    1620                 :            : 
    1621         [ #  # ]:          0 :         if (!bitmap)
    1622                 :          0 :                 return;
    1623         [ #  # ]:          0 :         if (sector == 0) {
    1624                 :          0 :                 bitmap->last_end_sync = jiffies;
    1625                 :          0 :                 return;
    1626                 :            :         }
    1627   [ #  #  #  # ]:          0 :         if (!force && time_before(jiffies, (bitmap->last_end_sync
    1628                 :            :                                   + bitmap->mddev->bitmap_info.daemon_sleep)))
    1629                 :            :                 return;
    1630   [ #  #  #  # ]:          0 :         wait_event(bitmap->mddev->recovery_wait,
    1631                 :            :                    atomic_read(&bitmap->mddev->recovery_active) == 0);
    1632                 :            : 
    1633                 :          0 :         bitmap->mddev->curr_resync_completed = sector;
    1634                 :          0 :         set_bit(MD_SB_CHANGE_CLEAN, &bitmap->mddev->sb_flags);
    1635                 :          0 :         sector &= ~((1ULL << bitmap->counts.chunkshift) - 1);
    1636                 :          0 :         s = 0;
    1637   [ #  #  #  # ]:          0 :         while (s < sector && s < bitmap->mddev->resync_max_sectors) {
    1638                 :          0 :                 md_bitmap_end_sync(bitmap, s, &blocks, 0);
    1639                 :          0 :                 s += blocks;
    1640                 :            :         }
    1641                 :          0 :         bitmap->last_end_sync = jiffies;
    1642                 :          0 :         sysfs_notify(&bitmap->mddev->kobj, NULL, "sync_completed");
    1643                 :            : }
    1644                 :            : EXPORT_SYMBOL(md_bitmap_cond_end_sync);
    1645                 :            : 
    1646                 :          0 : void md_bitmap_sync_with_cluster(struct mddev *mddev,
    1647                 :            :                               sector_t old_lo, sector_t old_hi,
    1648                 :            :                               sector_t new_lo, sector_t new_hi)
    1649                 :            : {
    1650                 :          0 :         struct bitmap *bitmap = mddev->bitmap;
    1651                 :          0 :         sector_t sector, blocks = 0;
    1652                 :            : 
    1653         [ #  # ]:          0 :         for (sector = old_lo; sector < new_lo; ) {
    1654                 :          0 :                 md_bitmap_end_sync(bitmap, sector, &blocks, 0);
    1655                 :          0 :                 sector += blocks;
    1656                 :            :         }
    1657   [ #  #  #  #  :          0 :         WARN((blocks > new_lo) && old_lo, "alignment is not correct for lo\n");
                   #  # ]
    1658                 :            : 
    1659         [ #  # ]:          0 :         for (sector = old_hi; sector < new_hi; ) {
    1660                 :          0 :                 md_bitmap_start_sync(bitmap, sector, &blocks, 0);
    1661                 :          0 :                 sector += blocks;
    1662                 :            :         }
    1663   [ #  #  #  #  :          0 :         WARN((blocks > new_hi) && old_hi, "alignment is not correct for hi\n");
                   #  # ]
    1664                 :          0 : }
    1665                 :            : EXPORT_SYMBOL(md_bitmap_sync_with_cluster);
    1666                 :            : 
    1667                 :          0 : static void md_bitmap_set_memory_bits(struct bitmap *bitmap, sector_t offset, int needed)
    1668                 :            : {
    1669                 :            :         /* For each chunk covered by any of these sectors, set the
    1670                 :            :          * counter to 2 and possibly set resync_needed.  They should all
    1671                 :            :          * be 0 at this point
    1672                 :            :          */
    1673                 :            : 
    1674                 :          0 :         sector_t secs;
    1675                 :          0 :         bitmap_counter_t *bmc;
    1676                 :          0 :         spin_lock_irq(&bitmap->counts.lock);
    1677                 :          0 :         bmc = md_bitmap_get_counter(&bitmap->counts, offset, &secs, 1);
    1678         [ #  # ]:          0 :         if (!bmc) {
    1679                 :          0 :                 spin_unlock_irq(&bitmap->counts.lock);
    1680                 :          0 :                 return;
    1681                 :            :         }
    1682         [ #  # ]:          0 :         if (!*bmc) {
    1683                 :          0 :                 *bmc = 2;
    1684                 :          0 :                 md_bitmap_count_page(&bitmap->counts, offset, 1);
    1685         [ #  # ]:          0 :                 md_bitmap_set_pending(&bitmap->counts, offset);
    1686                 :          0 :                 bitmap->allclean = 0;
    1687                 :            :         }
    1688         [ #  # ]:          0 :         if (needed)
    1689                 :          0 :                 *bmc |= NEEDED_MASK;
    1690                 :          0 :         spin_unlock_irq(&bitmap->counts.lock);
    1691                 :            : }
    1692                 :            : 
    1693                 :            : /* dirty the memory and file bits for bitmap chunks "s" to "e" */
    1694                 :          0 : void md_bitmap_dirty_bits(struct bitmap *bitmap, unsigned long s, unsigned long e)
    1695                 :            : {
    1696                 :          0 :         unsigned long chunk;
    1697                 :            : 
    1698         [ #  # ]:          0 :         for (chunk = s; chunk <= e; chunk++) {
    1699                 :          0 :                 sector_t sec = (sector_t)chunk << bitmap->counts.chunkshift;
    1700                 :          0 :                 md_bitmap_set_memory_bits(bitmap, sec, 1);
    1701                 :          0 :                 md_bitmap_file_set_bit(bitmap, sec);
    1702         [ #  # ]:          0 :                 if (sec < bitmap->mddev->recovery_cp)
    1703                 :            :                         /* We are asserting that the array is dirty,
    1704                 :            :                          * so move the recovery_cp address back so
    1705                 :            :                          * that it is obvious that it is dirty
    1706                 :            :                          */
    1707                 :          0 :                         bitmap->mddev->recovery_cp = sec;
    1708                 :            :         }
    1709                 :          0 : }
    1710                 :            : 
    1711                 :            : /*
    1712                 :            :  * flush out any pending updates
    1713                 :            :  */
    1714                 :          0 : void md_bitmap_flush(struct mddev *mddev)
    1715                 :            : {
    1716                 :          0 :         struct bitmap *bitmap = mddev->bitmap;
    1717                 :          0 :         long sleep;
    1718                 :            : 
    1719         [ #  # ]:          0 :         if (!bitmap) /* there was no bitmap */
    1720                 :            :                 return;
    1721                 :            : 
    1722                 :            :         /* run the daemon_work three time to ensure everything is flushed
    1723                 :            :          * that can be
    1724                 :            :          */
    1725                 :          0 :         sleep = mddev->bitmap_info.daemon_sleep * 2;
    1726                 :          0 :         bitmap->daemon_lastrun -= sleep;
    1727                 :          0 :         md_bitmap_daemon_work(mddev);
    1728                 :          0 :         bitmap->daemon_lastrun -= sleep;
    1729                 :          0 :         md_bitmap_daemon_work(mddev);
    1730                 :          0 :         bitmap->daemon_lastrun -= sleep;
    1731                 :          0 :         md_bitmap_daemon_work(mddev);
    1732                 :          0 :         md_bitmap_update_sb(bitmap);
    1733                 :            : }
    1734                 :            : 
    1735                 :            : /*
    1736                 :            :  * free memory that was allocated
    1737                 :            :  */
    1738                 :          0 : void md_bitmap_free(struct bitmap *bitmap)
    1739                 :            : {
    1740                 :          0 :         unsigned long k, pages;
    1741                 :          0 :         struct bitmap_page *bp;
    1742                 :            : 
    1743         [ #  # ]:          0 :         if (!bitmap) /* there was no bitmap */
    1744                 :            :                 return;
    1745                 :            : 
    1746         [ #  # ]:          0 :         if (bitmap->sysfs_can_clear)
    1747                 :          0 :                 sysfs_put(bitmap->sysfs_can_clear);
    1748                 :            : 
    1749   [ #  #  #  # ]:          0 :         if (mddev_is_clustered(bitmap->mddev) && bitmap->mddev->cluster_info &&
    1750                 :          0 :                 bitmap->cluster_slot == md_cluster_ops->slot_number(bitmap->mddev))
    1751                 :          0 :                 md_cluster_stop(bitmap->mddev);
    1752                 :            : 
    1753                 :            :         /* Shouldn't be needed - but just in case.... */
    1754   [ #  #  #  # ]:          0 :         wait_event(bitmap->write_wait,
    1755                 :            :                    atomic_read(&bitmap->pending_writes) == 0);
    1756                 :            : 
    1757                 :            :         /* release the bitmap file  */
    1758                 :          0 :         md_bitmap_file_unmap(&bitmap->storage);
    1759                 :            : 
    1760                 :          0 :         bp = bitmap->counts.bp;
    1761                 :          0 :         pages = bitmap->counts.pages;
    1762                 :            : 
    1763                 :            :         /* free all allocated memory */
    1764                 :            : 
    1765         [ #  # ]:          0 :         if (bp) /* deallocate the page memory */
    1766         [ #  # ]:          0 :                 for (k = 0; k < pages; k++)
    1767   [ #  #  #  # ]:          0 :                         if (bp[k].map && !bp[k].hijacked)
    1768                 :          0 :                                 kfree(bp[k].map);
    1769                 :          0 :         kfree(bp);
    1770                 :          0 :         kfree(bitmap);
    1771                 :            : }
    1772                 :            : EXPORT_SYMBOL(md_bitmap_free);
    1773                 :            : 
    1774                 :          0 : void md_bitmap_wait_behind_writes(struct mddev *mddev)
    1775                 :            : {
    1776                 :          0 :         struct bitmap *bitmap = mddev->bitmap;
    1777                 :            : 
    1778                 :            :         /* wait for behind writes to complete */
    1779   [ #  #  #  # ]:          0 :         if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
    1780                 :          0 :                 pr_debug("md:%s: behind writes in progress - waiting to stop.\n",
    1781                 :            :                          mdname(mddev));
    1782                 :            :                 /* need to kick something here to make sure I/O goes? */
    1783   [ #  #  #  # ]:          0 :                 wait_event(bitmap->behind_wait,
    1784                 :            :                            atomic_read(&bitmap->behind_writes) == 0);
    1785                 :            :         }
    1786                 :          0 : }
    1787                 :            : 
    1788                 :          0 : void md_bitmap_destroy(struct mddev *mddev)
    1789                 :            : {
    1790                 :          0 :         struct bitmap *bitmap = mddev->bitmap;
    1791                 :            : 
    1792         [ #  # ]:          0 :         if (!bitmap) /* there was no bitmap */
    1793                 :            :                 return;
    1794                 :            : 
    1795                 :          0 :         md_bitmap_wait_behind_writes(mddev);
    1796         [ #  # ]:          0 :         if (!mddev->serialize_policy)
    1797                 :          0 :                 mddev_destroy_serial_pool(mddev, NULL, true);
    1798                 :            : 
    1799                 :          0 :         mutex_lock(&mddev->bitmap_info.mutex);
    1800                 :          0 :         spin_lock(&mddev->lock);
    1801                 :          0 :         mddev->bitmap = NULL; /* disconnect from the md device */
    1802                 :          0 :         spin_unlock(&mddev->lock);
    1803                 :          0 :         mutex_unlock(&mddev->bitmap_info.mutex);
    1804         [ #  # ]:          0 :         if (mddev->thread)
    1805                 :          0 :                 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
    1806                 :            : 
    1807                 :          0 :         md_bitmap_free(bitmap);
    1808                 :            : }
    1809                 :            : 
    1810                 :            : /*
    1811                 :            :  * initialize the bitmap structure
    1812                 :            :  * if this returns an error, bitmap_destroy must be called to do clean up
    1813                 :            :  * once mddev->bitmap is set
    1814                 :            :  */
    1815                 :          0 : struct bitmap *md_bitmap_create(struct mddev *mddev, int slot)
    1816                 :            : {
    1817                 :          0 :         struct bitmap *bitmap;
    1818                 :          0 :         sector_t blocks = mddev->resync_max_sectors;
    1819                 :          0 :         struct file *file = mddev->bitmap_info.file;
    1820                 :          0 :         int err;
    1821                 :          0 :         struct kernfs_node *bm = NULL;
    1822                 :            : 
    1823                 :          0 :         BUILD_BUG_ON(sizeof(bitmap_super_t) != 256);
    1824                 :            : 
    1825   [ #  #  #  # ]:          0 :         BUG_ON(file && mddev->bitmap_info.offset);
    1826                 :            : 
    1827         [ #  # ]:          0 :         if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
    1828         [ #  # ]:          0 :                 pr_notice("md/raid:%s: array with journal cannot have bitmap\n",
    1829                 :            :                           mdname(mddev));
    1830                 :          0 :                 return ERR_PTR(-EBUSY);
    1831                 :            :         }
    1832                 :            : 
    1833                 :          0 :         bitmap = kzalloc(sizeof(*bitmap), GFP_KERNEL);
    1834         [ #  # ]:          0 :         if (!bitmap)
    1835                 :            :                 return ERR_PTR(-ENOMEM);
    1836                 :            : 
    1837                 :          0 :         spin_lock_init(&bitmap->counts.lock);
    1838                 :          0 :         atomic_set(&bitmap->pending_writes, 0);
    1839                 :          0 :         init_waitqueue_head(&bitmap->write_wait);
    1840                 :          0 :         init_waitqueue_head(&bitmap->overflow_wait);
    1841                 :          0 :         init_waitqueue_head(&bitmap->behind_wait);
    1842                 :            : 
    1843                 :          0 :         bitmap->mddev = mddev;
    1844                 :          0 :         bitmap->cluster_slot = slot;
    1845                 :            : 
    1846         [ #  # ]:          0 :         if (mddev->kobj.sd)
    1847                 :          0 :                 bm = sysfs_get_dirent(mddev->kobj.sd, "bitmap");
    1848         [ #  # ]:          0 :         if (bm) {
    1849                 :          0 :                 bitmap->sysfs_can_clear = sysfs_get_dirent(bm, "can_clear");
    1850                 :          0 :                 sysfs_put(bm);
    1851                 :            :         } else
    1852                 :          0 :                 bitmap->sysfs_can_clear = NULL;
    1853                 :            : 
    1854                 :          0 :         bitmap->storage.file = file;
    1855         [ #  # ]:          0 :         if (file) {
    1856                 :          0 :                 get_file(file);
    1857                 :            :                 /* As future accesses to this file will use bmap,
    1858                 :            :                  * and bypass the page cache, we must sync the file
    1859                 :            :                  * first.
    1860                 :            :                  */
    1861                 :          0 :                 vfs_fsync(file, 1);
    1862                 :            :         }
    1863                 :            :         /* read superblock from bitmap file (this sets mddev->bitmap_info.chunksize) */
    1864         [ #  # ]:          0 :         if (!mddev->bitmap_info.external) {
    1865                 :            :                 /*
    1866                 :            :                  * If 'MD_ARRAY_FIRST_USE' is set, then device-mapper is
    1867                 :            :                  * instructing us to create a new on-disk bitmap instance.
    1868                 :            :                  */
    1869         [ #  # ]:          0 :                 if (test_and_clear_bit(MD_ARRAY_FIRST_USE, &mddev->flags))
    1870                 :          0 :                         err = md_bitmap_new_disk_sb(bitmap);
    1871                 :            :                 else
    1872                 :          0 :                         err = md_bitmap_read_sb(bitmap);
    1873                 :            :         } else {
    1874                 :          0 :                 err = 0;
    1875         [ #  # ]:          0 :                 if (mddev->bitmap_info.chunksize == 0 ||
    1876         [ #  # ]:          0 :                     mddev->bitmap_info.daemon_sleep == 0)
    1877                 :            :                         /* chunksize and time_base need to be
    1878                 :            :                          * set first. */
    1879                 :            :                         err = -EINVAL;
    1880                 :            :         }
    1881         [ #  # ]:          0 :         if (err)
    1882                 :          0 :                 goto error;
    1883                 :            : 
    1884                 :          0 :         bitmap->daemon_lastrun = jiffies;
    1885                 :          0 :         err = md_bitmap_resize(bitmap, blocks, mddev->bitmap_info.chunksize, 1);
    1886         [ #  # ]:          0 :         if (err)
    1887                 :          0 :                 goto error;
    1888                 :            : 
    1889                 :          0 :         pr_debug("created bitmap (%lu pages) for device %s\n",
    1890                 :            :                  bitmap->counts.pages, bmname(bitmap));
    1891                 :            : 
    1892         [ #  # ]:          0 :         err = test_bit(BITMAP_WRITE_ERROR, &bitmap->flags) ? -EIO : 0;
    1893                 :          0 :         if (err)
    1894                 :          0 :                 goto error;
    1895                 :            : 
    1896                 :            :         return bitmap;
    1897                 :          0 :  error:
    1898                 :          0 :         md_bitmap_free(bitmap);
    1899                 :          0 :         return ERR_PTR(err);
    1900                 :            : }
    1901                 :            : 
    1902                 :          0 : int md_bitmap_load(struct mddev *mddev)
    1903                 :            : {
    1904                 :          0 :         int err = 0;
    1905                 :          0 :         sector_t start = 0;
    1906                 :          0 :         sector_t sector = 0;
    1907                 :          0 :         struct bitmap *bitmap = mddev->bitmap;
    1908                 :          0 :         struct md_rdev *rdev;
    1909                 :            : 
    1910         [ #  # ]:          0 :         if (!bitmap)
    1911                 :          0 :                 goto out;
    1912                 :            : 
    1913         [ #  # ]:          0 :         rdev_for_each(rdev, mddev)
    1914                 :          0 :                 mddev_create_serial_pool(mddev, rdev, true);
    1915                 :            : 
    1916         [ #  # ]:          0 :         if (mddev_is_clustered(mddev))
    1917                 :          0 :                 md_cluster_ops->load_bitmaps(mddev, mddev->bitmap_info.nodes);
    1918                 :            : 
    1919                 :            :         /* Clear out old bitmap info first:  Either there is none, or we
    1920                 :            :          * are resuming after someone else has possibly changed things,
    1921                 :            :          * so we should forget old cached info.
    1922                 :            :          * All chunks should be clean, but some might need_sync.
    1923                 :            :          */
    1924         [ #  # ]:          0 :         while (sector < mddev->resync_max_sectors) {
    1925                 :          0 :                 sector_t blocks;
    1926                 :          0 :                 md_bitmap_start_sync(bitmap, sector, &blocks, 0);
    1927                 :          0 :                 sector += blocks;
    1928                 :            :         }
    1929                 :          0 :         md_bitmap_close_sync(bitmap);
    1930                 :            : 
    1931         [ #  # ]:          0 :         if (mddev->degraded == 0
    1932         [ #  # ]:          0 :             || bitmap->events_cleared == mddev->events)
    1933                 :            :                 /* no need to keep dirty bits to optimise a
    1934                 :            :                  * re-add of a missing device */
    1935                 :          0 :                 start = mddev->recovery_cp;
    1936                 :            : 
    1937                 :          0 :         mutex_lock(&mddev->bitmap_info.mutex);
    1938                 :          0 :         err = md_bitmap_init_from_disk(bitmap, start);
    1939                 :          0 :         mutex_unlock(&mddev->bitmap_info.mutex);
    1940                 :            : 
    1941         [ #  # ]:          0 :         if (err)
    1942                 :          0 :                 goto out;
    1943                 :          0 :         clear_bit(BITMAP_STALE, &bitmap->flags);
    1944                 :            : 
    1945                 :            :         /* Kick recovery in case any bits were set */
    1946                 :          0 :         set_bit(MD_RECOVERY_NEEDED, &bitmap->mddev->recovery);
    1947                 :            : 
    1948                 :          0 :         mddev->thread->timeout = mddev->bitmap_info.daemon_sleep;
    1949                 :          0 :         md_wakeup_thread(mddev->thread);
    1950                 :            : 
    1951                 :          0 :         md_bitmap_update_sb(bitmap);
    1952                 :            : 
    1953         [ #  # ]:          0 :         if (test_bit(BITMAP_WRITE_ERROR, &bitmap->flags))
    1954                 :          0 :                 err = -EIO;
    1955                 :          0 : out:
    1956                 :          0 :         return err;
    1957                 :            : }
    1958                 :            : EXPORT_SYMBOL_GPL(md_bitmap_load);
    1959                 :            : 
    1960                 :          0 : struct bitmap *get_bitmap_from_slot(struct mddev *mddev, int slot)
    1961                 :            : {
    1962                 :          0 :         int rv = 0;
    1963                 :          0 :         struct bitmap *bitmap;
    1964                 :            : 
    1965                 :          0 :         bitmap = md_bitmap_create(mddev, slot);
    1966         [ #  # ]:          0 :         if (IS_ERR(bitmap)) {
    1967                 :          0 :                 rv = PTR_ERR(bitmap);
    1968                 :          0 :                 return ERR_PTR(rv);
    1969                 :            :         }
    1970                 :            : 
    1971                 :          0 :         rv = md_bitmap_init_from_disk(bitmap, 0);
    1972         [ #  # ]:          0 :         if (rv) {
    1973                 :          0 :                 md_bitmap_free(bitmap);
    1974                 :          0 :                 return ERR_PTR(rv);
    1975                 :            :         }
    1976                 :            : 
    1977                 :            :         return bitmap;
    1978                 :            : }
    1979                 :            : EXPORT_SYMBOL(get_bitmap_from_slot);
    1980                 :            : 
    1981                 :            : /* Loads the bitmap associated with slot and copies the resync information
    1982                 :            :  * to our bitmap
    1983                 :            :  */
    1984                 :          0 : int md_bitmap_copy_from_slot(struct mddev *mddev, int slot,
    1985                 :            :                 sector_t *low, sector_t *high, bool clear_bits)
    1986                 :            : {
    1987                 :          0 :         int rv = 0, i, j;
    1988                 :          0 :         sector_t block, lo = 0, hi = 0;
    1989                 :          0 :         struct bitmap_counts *counts;
    1990                 :          0 :         struct bitmap *bitmap;
    1991                 :            : 
    1992                 :          0 :         bitmap = get_bitmap_from_slot(mddev, slot);
    1993         [ #  # ]:          0 :         if (IS_ERR(bitmap)) {
    1994                 :          0 :                 pr_err("%s can't get bitmap from slot %d\n", __func__, slot);
    1995                 :          0 :                 return -1;
    1996                 :            :         }
    1997                 :            : 
    1998                 :            :         counts = &bitmap->counts;
    1999         [ #  # ]:          0 :         for (j = 0; j < counts->chunks; j++) {
    2000                 :          0 :                 block = (sector_t)j << counts->chunkshift;
    2001         [ #  # ]:          0 :                 if (md_bitmap_file_test_bit(bitmap, block)) {
    2002         [ #  # ]:          0 :                         if (!lo)
    2003                 :          0 :                                 lo = block;
    2004                 :          0 :                         hi = block;
    2005                 :          0 :                         md_bitmap_file_clear_bit(bitmap, block);
    2006                 :          0 :                         md_bitmap_set_memory_bits(mddev->bitmap, block, 1);
    2007                 :          0 :                         md_bitmap_file_set_bit(mddev->bitmap, block);
    2008                 :            :                 }
    2009                 :            :         }
    2010                 :            : 
    2011         [ #  # ]:          0 :         if (clear_bits) {
    2012                 :          0 :                 md_bitmap_update_sb(bitmap);
    2013                 :            :                 /* BITMAP_PAGE_PENDING is set, but bitmap_unplug needs
    2014                 :            :                  * BITMAP_PAGE_DIRTY or _NEEDWRITE to write ... */
    2015         [ #  # ]:          0 :                 for (i = 0; i < bitmap->storage.file_pages; i++)
    2016         [ #  # ]:          0 :                         if (test_page_attr(bitmap, i, BITMAP_PAGE_PENDING))
    2017                 :          0 :                                 set_page_attr(bitmap, i, BITMAP_PAGE_NEEDWRITE);
    2018                 :          0 :                 md_bitmap_unplug(bitmap);
    2019                 :            :         }
    2020                 :          0 :         md_bitmap_unplug(mddev->bitmap);
    2021                 :          0 :         *low = lo;
    2022                 :          0 :         *high = hi;
    2023                 :            : 
    2024                 :          0 :         return rv;
    2025                 :            : }
    2026                 :            : EXPORT_SYMBOL_GPL(md_bitmap_copy_from_slot);
    2027                 :            : 
    2028                 :            : 
    2029                 :          0 : void md_bitmap_status(struct seq_file *seq, struct bitmap *bitmap)
    2030                 :            : {
    2031                 :          0 :         unsigned long chunk_kb;
    2032                 :          0 :         struct bitmap_counts *counts;
    2033                 :            : 
    2034         [ #  # ]:          0 :         if (!bitmap)
    2035                 :            :                 return;
    2036                 :            : 
    2037                 :          0 :         counts = &bitmap->counts;
    2038                 :            : 
    2039                 :          0 :         chunk_kb = bitmap->mddev->bitmap_info.chunksize >> 10;
    2040   [ #  #  #  # ]:          0 :         seq_printf(seq, "bitmap: %lu/%lu pages [%luKB], "
    2041                 :            :                    "%lu%s chunk",
    2042                 :            :                    counts->pages - counts->missing_pages,
    2043                 :            :                    counts->pages,
    2044                 :          0 :                    (counts->pages - counts->missing_pages)
    2045                 :            :                    << (PAGE_SHIFT - 10),
    2046                 :            :                    chunk_kb ? chunk_kb : bitmap->mddev->bitmap_info.chunksize,
    2047                 :            :                    chunk_kb ? "KB" : "B");
    2048         [ #  # ]:          0 :         if (bitmap->storage.file) {
    2049                 :          0 :                 seq_printf(seq, ", file: ");
    2050                 :          0 :                 seq_file_path(seq, bitmap->storage.file, " \t\n");
    2051                 :            :         }
    2052                 :            : 
    2053                 :          0 :         seq_printf(seq, "\n");
    2054                 :            : }
    2055                 :            : 
    2056                 :          0 : int md_bitmap_resize(struct bitmap *bitmap, sector_t blocks,
    2057                 :            :                   int chunksize, int init)
    2058                 :            : {
    2059                 :            :         /* If chunk_size is 0, choose an appropriate chunk size.
    2060                 :            :          * Then possibly allocate new storage space.
    2061                 :            :          * Then quiesce, copy bits, replace bitmap, and re-start
    2062                 :            :          *
    2063                 :            :          * This function is called both to set up the initial bitmap
    2064                 :            :          * and to resize the bitmap while the array is active.
    2065                 :            :          * If this happens as a result of the array being resized,
    2066                 :            :          * chunksize will be zero, and we need to choose a suitable
    2067                 :            :          * chunksize, otherwise we use what we are given.
    2068                 :            :          */
    2069                 :          0 :         struct bitmap_storage store;
    2070                 :          0 :         struct bitmap_counts old_counts;
    2071                 :          0 :         unsigned long chunks;
    2072                 :          0 :         sector_t block;
    2073                 :          0 :         sector_t old_blocks, new_blocks;
    2074                 :          0 :         int chunkshift;
    2075                 :          0 :         int ret = 0;
    2076                 :          0 :         long pages;
    2077                 :          0 :         struct bitmap_page *new_bp;
    2078                 :            : 
    2079   [ #  #  #  # ]:          0 :         if (bitmap->storage.file && !init) {
    2080                 :          0 :                 pr_info("md: cannot resize file-based bitmap\n");
    2081                 :          0 :                 return -EINVAL;
    2082                 :            :         }
    2083                 :            : 
    2084         [ #  # ]:          0 :         if (chunksize == 0) {
    2085                 :            :                 /* If there is enough space, leave the chunk size unchanged,
    2086                 :            :                  * else increase by factor of two until there is enough space.
    2087                 :            :                  */
    2088                 :          0 :                 long bytes;
    2089                 :          0 :                 long space = bitmap->mddev->bitmap_info.space;
    2090                 :            : 
    2091         [ #  # ]:          0 :                 if (space == 0) {
    2092                 :            :                         /* We don't know how much space there is, so limit
    2093                 :            :                          * to current size - in sectors.
    2094                 :            :                          */
    2095                 :          0 :                         bytes = DIV_ROUND_UP(bitmap->counts.chunks, 8);
    2096         [ #  # ]:          0 :                         if (!bitmap->mddev->bitmap_info.external)
    2097                 :          0 :                                 bytes += sizeof(bitmap_super_t);
    2098                 :          0 :                         space = DIV_ROUND_UP(bytes, 512);
    2099                 :          0 :                         bitmap->mddev->bitmap_info.space = space;
    2100                 :            :                 }
    2101                 :          0 :                 chunkshift = bitmap->counts.chunkshift;
    2102                 :          0 :                 chunkshift--;
    2103                 :          0 :                 do {
    2104                 :            :                         /* 'chunkshift' is shift from block size to chunk size */
    2105                 :          0 :                         chunkshift++;
    2106                 :          0 :                         chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
    2107                 :          0 :                         bytes = DIV_ROUND_UP(chunks, 8);
    2108         [ #  # ]:          0 :                         if (!bitmap->mddev->bitmap_info.external)
    2109                 :          0 :                                 bytes += sizeof(bitmap_super_t);
    2110         [ #  # ]:          0 :                 } while (bytes > (space << 9));
    2111                 :            :         } else
    2112                 :          0 :                 chunkshift = ffz(~chunksize) - BITMAP_BLOCK_SHIFT;
    2113                 :            : 
    2114                 :          0 :         chunks = DIV_ROUND_UP_SECTOR_T(blocks, 1 << chunkshift);
    2115                 :          0 :         memset(&store, 0, sizeof(store));
    2116   [ #  #  #  # ]:          0 :         if (bitmap->mddev->bitmap_info.offset || bitmap->mddev->bitmap_info.file)
    2117                 :          0 :                 ret = md_bitmap_storage_alloc(&store, chunks,
    2118                 :          0 :                                               !bitmap->mddev->bitmap_info.external,
    2119         [ #  # ]:          0 :                                               mddev_is_clustered(bitmap->mddev)
    2120                 :            :                                               ? bitmap->cluster_slot : 0);
    2121         [ #  # ]:          0 :         if (ret) {
    2122                 :          0 :                 md_bitmap_file_unmap(&store);
    2123                 :          0 :                 goto err;
    2124                 :            :         }
    2125                 :            : 
    2126                 :          0 :         pages = DIV_ROUND_UP(chunks, PAGE_COUNTER_RATIO);
    2127                 :            : 
    2128                 :          0 :         new_bp = kcalloc(pages, sizeof(*new_bp), GFP_KERNEL);
    2129                 :          0 :         ret = -ENOMEM;
    2130         [ #  # ]:          0 :         if (!new_bp) {
    2131                 :          0 :                 md_bitmap_file_unmap(&store);
    2132                 :          0 :                 goto err;
    2133                 :            :         }
    2134                 :            : 
    2135         [ #  # ]:          0 :         if (!init)
    2136                 :          0 :                 bitmap->mddev->pers->quiesce(bitmap->mddev, 1);
    2137                 :            : 
    2138                 :          0 :         store.file = bitmap->storage.file;
    2139                 :          0 :         bitmap->storage.file = NULL;
    2140                 :            : 
    2141   [ #  #  #  # ]:          0 :         if (store.sb_page && bitmap->storage.sb_page)
    2142                 :          0 :                 memcpy(page_address(store.sb_page),
    2143                 :            :                        page_address(bitmap->storage.sb_page),
    2144                 :            :                        sizeof(bitmap_super_t));
    2145                 :          0 :         spin_lock_irq(&bitmap->counts.lock);
    2146                 :          0 :         md_bitmap_file_unmap(&bitmap->storage);
    2147                 :          0 :         bitmap->storage = store;
    2148                 :            : 
    2149                 :          0 :         old_counts = bitmap->counts;
    2150                 :          0 :         bitmap->counts.bp = new_bp;
    2151                 :          0 :         bitmap->counts.pages = pages;
    2152                 :          0 :         bitmap->counts.missing_pages = pages;
    2153                 :          0 :         bitmap->counts.chunkshift = chunkshift;
    2154                 :          0 :         bitmap->counts.chunks = chunks;
    2155                 :          0 :         bitmap->mddev->bitmap_info.chunksize = 1 << (chunkshift +
    2156                 :            :                                                      BITMAP_BLOCK_SHIFT);
    2157                 :            : 
    2158                 :          0 :         blocks = min(old_counts.chunks << old_counts.chunkshift,
    2159                 :            :                      chunks << chunkshift);
    2160                 :            : 
    2161                 :            :         /* For cluster raid, need to pre-allocate bitmap */
    2162         [ #  # ]:          0 :         if (mddev_is_clustered(bitmap->mddev)) {
    2163                 :            :                 unsigned long page;
    2164         [ #  # ]:          0 :                 for (page = 0; page < pages; page++) {
    2165                 :          0 :                         ret = md_bitmap_checkpage(&bitmap->counts, page, 1, 1);
    2166         [ #  # ]:          0 :                         if (ret) {
    2167                 :            :                                 unsigned long k;
    2168                 :            : 
    2169                 :            :                                 /* deallocate the page memory */
    2170         [ #  # ]:          0 :                                 for (k = 0; k < page; k++) {
    2171                 :          0 :                                         kfree(new_bp[k].map);
    2172                 :            :                                 }
    2173                 :          0 :                                 kfree(new_bp);
    2174                 :            : 
    2175                 :            :                                 /* restore some fields from old_counts */
    2176                 :          0 :                                 bitmap->counts.bp = old_counts.bp;
    2177                 :          0 :                                 bitmap->counts.pages = old_counts.pages;
    2178                 :          0 :                                 bitmap->counts.missing_pages = old_counts.pages;
    2179                 :          0 :                                 bitmap->counts.chunkshift = old_counts.chunkshift;
    2180                 :          0 :                                 bitmap->counts.chunks = old_counts.chunks;
    2181                 :          0 :                                 bitmap->mddev->bitmap_info.chunksize = 1 << (old_counts.chunkshift +
    2182                 :            :                                                                              BITMAP_BLOCK_SHIFT);
    2183                 :          0 :                                 blocks = old_counts.chunks << old_counts.chunkshift;
    2184                 :          0 :                                 pr_warn("Could not pre-allocate in-memory bitmap for cluster raid\n");
    2185                 :          0 :                                 break;
    2186                 :            :                         } else
    2187                 :          0 :                                 bitmap->counts.bp[page].count += 1;
    2188                 :            :                 }
    2189                 :            :         }
    2190                 :            : 
    2191         [ #  # ]:          0 :         for (block = 0; block < blocks; ) {
    2192                 :          0 :                 bitmap_counter_t *bmc_old, *bmc_new;
    2193                 :          0 :                 int set;
    2194                 :            : 
    2195                 :          0 :                 bmc_old = md_bitmap_get_counter(&old_counts, block, &old_blocks, 0);
    2196   [ #  #  #  # ]:          0 :                 set = bmc_old && NEEDED(*bmc_old);
    2197                 :            : 
    2198                 :          0 :                 if (set) {
    2199                 :          0 :                         bmc_new = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1);
    2200         [ #  # ]:          0 :                         if (*bmc_new == 0) {
    2201                 :            :                                 /* need to set on-disk bits too. */
    2202                 :          0 :                                 sector_t end = block + new_blocks;
    2203                 :          0 :                                 sector_t start = block >> chunkshift;
    2204                 :          0 :                                 start <<= chunkshift;
    2205         [ #  # ]:          0 :                                 while (start < end) {
    2206                 :          0 :                                         md_bitmap_file_set_bit(bitmap, block);
    2207                 :          0 :                                         start += 1 << chunkshift;
    2208                 :            :                                 }
    2209                 :          0 :                                 *bmc_new = 2;
    2210                 :          0 :                                 md_bitmap_count_page(&bitmap->counts, block, 1);
    2211         [ #  # ]:          0 :                                 md_bitmap_set_pending(&bitmap->counts, block);
    2212                 :            :                         }
    2213                 :          0 :                         *bmc_new |= NEEDED_MASK;
    2214         [ #  # ]:          0 :                         if (new_blocks < old_blocks)
    2215                 :          0 :                                 old_blocks = new_blocks;
    2216                 :            :                 }
    2217                 :          0 :                 block += old_blocks;
    2218                 :            :         }
    2219                 :            : 
    2220         [ #  # ]:          0 :         if (bitmap->counts.bp != old_counts.bp) {
    2221                 :            :                 unsigned long k;
    2222         [ #  # ]:          0 :                 for (k = 0; k < old_counts.pages; k++)
    2223         [ #  # ]:          0 :                         if (!old_counts.bp[k].hijacked)
    2224                 :          0 :                                 kfree(old_counts.bp[k].map);
    2225                 :          0 :                 kfree(old_counts.bp);
    2226                 :            :         }
    2227                 :            : 
    2228         [ #  # ]:          0 :         if (!init) {
    2229                 :            :                 int i;
    2230         [ #  # ]:          0 :                 while (block < (chunks << chunkshift)) {
    2231                 :          0 :                         bitmap_counter_t *bmc;
    2232                 :          0 :                         bmc = md_bitmap_get_counter(&bitmap->counts, block, &new_blocks, 1);
    2233         [ #  # ]:          0 :                         if (bmc) {
    2234                 :            :                                 /* new space.  It needs to be resynced, so
    2235                 :            :                                  * we set NEEDED_MASK.
    2236                 :            :                                  */
    2237         [ #  # ]:          0 :                                 if (*bmc == 0) {
    2238                 :          0 :                                         *bmc = NEEDED_MASK | 2;
    2239                 :          0 :                                         md_bitmap_count_page(&bitmap->counts, block, 1);
    2240         [ #  # ]:          0 :                                         md_bitmap_set_pending(&bitmap->counts, block);
    2241                 :            :                                 }
    2242                 :            :                         }
    2243                 :          0 :                         block += new_blocks;
    2244                 :            :                 }
    2245         [ #  # ]:          0 :                 for (i = 0; i < bitmap->storage.file_pages; i++)
    2246                 :          0 :                         set_page_attr(bitmap, i, BITMAP_PAGE_DIRTY);
    2247                 :            :         }
    2248                 :          0 :         spin_unlock_irq(&bitmap->counts.lock);
    2249                 :            : 
    2250         [ #  # ]:          0 :         if (!init) {
    2251                 :          0 :                 md_bitmap_unplug(bitmap);
    2252                 :          0 :                 bitmap->mddev->pers->quiesce(bitmap->mddev, 0);
    2253                 :            :         }
    2254                 :            :         ret = 0;
    2255                 :            : err:
    2256                 :            :         return ret;
    2257                 :            : }
    2258                 :            : EXPORT_SYMBOL_GPL(md_bitmap_resize);
    2259                 :            : 
    2260                 :            : static ssize_t
    2261                 :          0 : location_show(struct mddev *mddev, char *page)
    2262                 :            : {
    2263                 :          0 :         ssize_t len;
    2264         [ #  # ]:          0 :         if (mddev->bitmap_info.file)
    2265                 :          0 :                 len = sprintf(page, "file");
    2266         [ #  # ]:          0 :         else if (mddev->bitmap_info.offset)
    2267                 :          0 :                 len = sprintf(page, "%+lld", (long long)mddev->bitmap_info.offset);
    2268                 :            :         else
    2269                 :          0 :                 len = sprintf(page, "none");
    2270                 :          0 :         len += sprintf(page+len, "\n");
    2271                 :          0 :         return len;
    2272                 :            : }
    2273                 :            : 
    2274                 :            : static ssize_t
    2275                 :          0 : location_store(struct mddev *mddev, const char *buf, size_t len)
    2276                 :            : {
    2277                 :          0 :         int rv;
    2278                 :            : 
    2279                 :          0 :         rv = mddev_lock(mddev);
    2280         [ #  # ]:          0 :         if (rv)
    2281                 :          0 :                 return rv;
    2282         [ #  # ]:          0 :         if (mddev->pers) {
    2283         [ #  # ]:          0 :                 if (!mddev->pers->quiesce) {
    2284                 :          0 :                         rv = -EBUSY;
    2285                 :          0 :                         goto out;
    2286                 :            :                 }
    2287   [ #  #  #  # ]:          0 :                 if (mddev->recovery || mddev->sync_thread) {
    2288                 :          0 :                         rv = -EBUSY;
    2289                 :          0 :                         goto out;
    2290                 :            :                 }
    2291                 :            :         }
    2292                 :            : 
    2293   [ #  #  #  # ]:          0 :         if (mddev->bitmap || mddev->bitmap_info.file ||
    2294         [ #  # ]:          0 :             mddev->bitmap_info.offset) {
    2295                 :            :                 /* bitmap already configured.  Only option is to clear it */
    2296         [ #  # ]:          0 :                 if (strncmp(buf, "none", 4) != 0) {
    2297                 :          0 :                         rv = -EBUSY;
    2298                 :          0 :                         goto out;
    2299                 :            :                 }
    2300         [ #  # ]:          0 :                 if (mddev->pers) {
    2301                 :          0 :                         mddev_suspend(mddev);
    2302                 :          0 :                         md_bitmap_destroy(mddev);
    2303                 :          0 :                         mddev_resume(mddev);
    2304                 :            :                 }
    2305                 :          0 :                 mddev->bitmap_info.offset = 0;
    2306         [ #  # ]:          0 :                 if (mddev->bitmap_info.file) {
    2307                 :          0 :                         struct file *f = mddev->bitmap_info.file;
    2308                 :          0 :                         mddev->bitmap_info.file = NULL;
    2309                 :          0 :                         fput(f);
    2310                 :            :                 }
    2311                 :            :         } else {
    2312                 :            :                 /* No bitmap, OK to set a location */
    2313                 :          0 :                 long long offset;
    2314         [ #  # ]:          0 :                 if (strncmp(buf, "none", 4) == 0)
    2315                 :            :                         /* nothing to be done */;
    2316         [ #  # ]:          0 :                 else if (strncmp(buf, "file:", 5) == 0) {
    2317                 :            :                         /* Not supported yet */
    2318                 :          0 :                         rv = -EINVAL;
    2319                 :          0 :                         goto out;
    2320                 :            :                 } else {
    2321         [ #  # ]:          0 :                         if (buf[0] == '+')
    2322                 :          0 :                                 rv = kstrtoll(buf+1, 10, &offset);
    2323                 :            :                         else
    2324                 :          0 :                                 rv = kstrtoll(buf, 10, &offset);
    2325         [ #  # ]:          0 :                         if (rv)
    2326                 :          0 :                                 goto out;
    2327         [ #  # ]:          0 :                         if (offset == 0) {
    2328                 :          0 :                                 rv = -EINVAL;
    2329                 :          0 :                                 goto out;
    2330                 :            :                         }
    2331         [ #  # ]:          0 :                         if (mddev->bitmap_info.external == 0 &&
    2332         [ #  # ]:          0 :                             mddev->major_version == 0 &&
    2333         [ #  # ]:          0 :                             offset != mddev->bitmap_info.default_offset) {
    2334                 :          0 :                                 rv = -EINVAL;
    2335                 :          0 :                                 goto out;
    2336                 :            :                         }
    2337                 :          0 :                         mddev->bitmap_info.offset = offset;
    2338         [ #  # ]:          0 :                         if (mddev->pers) {
    2339                 :          0 :                                 struct bitmap *bitmap;
    2340                 :          0 :                                 bitmap = md_bitmap_create(mddev, -1);
    2341                 :          0 :                                 mddev_suspend(mddev);
    2342         [ #  # ]:          0 :                                 if (IS_ERR(bitmap))
    2343                 :          0 :                                         rv = PTR_ERR(bitmap);
    2344                 :            :                                 else {
    2345                 :          0 :                                         mddev->bitmap = bitmap;
    2346                 :          0 :                                         rv = md_bitmap_load(mddev);
    2347         [ #  # ]:          0 :                                         if (rv)
    2348                 :          0 :                                                 mddev->bitmap_info.offset = 0;
    2349                 :            :                                 }
    2350         [ #  # ]:          0 :                                 if (rv) {
    2351                 :          0 :                                         md_bitmap_destroy(mddev);
    2352                 :          0 :                                         mddev_resume(mddev);
    2353                 :          0 :                                         goto out;
    2354                 :            :                                 }
    2355                 :          0 :                                 mddev_resume(mddev);
    2356                 :            :                         }
    2357                 :            :                 }
    2358                 :            :         }
    2359         [ #  # ]:          0 :         if (!mddev->external) {
    2360                 :            :                 /* Ensure new bitmap info is stored in
    2361                 :            :                  * metadata promptly.
    2362                 :            :                  */
    2363                 :          0 :                 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
    2364                 :          0 :                 md_wakeup_thread(mddev->thread);
    2365                 :            :         }
    2366                 :            :         rv = 0;
    2367                 :          0 : out:
    2368                 :          0 :         mddev_unlock(mddev);
    2369         [ #  # ]:          0 :         if (rv)
    2370                 :          0 :                 return rv;
    2371                 :          0 :         return len;
    2372                 :            : }
    2373                 :            : 
    2374                 :            : static struct md_sysfs_entry bitmap_location =
    2375                 :            : __ATTR(location, S_IRUGO|S_IWUSR, location_show, location_store);
    2376                 :            : 
    2377                 :            : /* 'bitmap/space' is the space available at 'location' for the
    2378                 :            :  * bitmap.  This allows the kernel to know when it is safe to
    2379                 :            :  * resize the bitmap to match a resized array.
    2380                 :            :  */
    2381                 :            : static ssize_t
    2382                 :          0 : space_show(struct mddev *mddev, char *page)
    2383                 :            : {
    2384                 :          0 :         return sprintf(page, "%lu\n", mddev->bitmap_info.space);
    2385                 :            : }
    2386                 :            : 
    2387                 :            : static ssize_t
    2388                 :          0 : space_store(struct mddev *mddev, const char *buf, size_t len)
    2389                 :            : {
    2390                 :          0 :         unsigned long sectors;
    2391                 :          0 :         int rv;
    2392                 :            : 
    2393                 :          0 :         rv = kstrtoul(buf, 10, &sectors);
    2394         [ #  # ]:          0 :         if (rv)
    2395                 :          0 :                 return rv;
    2396                 :            : 
    2397         [ #  # ]:          0 :         if (sectors == 0)
    2398                 :            :                 return -EINVAL;
    2399                 :            : 
    2400         [ #  # ]:          0 :         if (mddev->bitmap &&
    2401         [ #  # ]:          0 :             sectors < (mddev->bitmap->storage.bytes + 511) >> 9)
    2402                 :            :                 return -EFBIG; /* Bitmap is too big for this small space */
    2403                 :            : 
    2404                 :            :         /* could make sure it isn't too big, but that isn't really
    2405                 :            :          * needed - user-space should be careful.
    2406                 :            :          */
    2407                 :          0 :         mddev->bitmap_info.space = sectors;
    2408                 :          0 :         return len;
    2409                 :            : }
    2410                 :            : 
    2411                 :            : static struct md_sysfs_entry bitmap_space =
    2412                 :            : __ATTR(space, S_IRUGO|S_IWUSR, space_show, space_store);
    2413                 :            : 
    2414                 :            : static ssize_t
    2415                 :          0 : timeout_show(struct mddev *mddev, char *page)
    2416                 :            : {
    2417                 :          0 :         ssize_t len;
    2418                 :          0 :         unsigned long secs = mddev->bitmap_info.daemon_sleep / HZ;
    2419                 :          0 :         unsigned long jifs = mddev->bitmap_info.daemon_sleep % HZ;
    2420                 :            : 
    2421                 :          0 :         len = sprintf(page, "%lu", secs);
    2422         [ #  # ]:          0 :         if (jifs)
    2423                 :          0 :                 len += sprintf(page+len, ".%03u", jiffies_to_msecs(jifs));
    2424                 :          0 :         len += sprintf(page+len, "\n");
    2425                 :          0 :         return len;
    2426                 :            : }
    2427                 :            : 
    2428                 :            : static ssize_t
    2429                 :          0 : timeout_store(struct mddev *mddev, const char *buf, size_t len)
    2430                 :            : {
    2431                 :            :         /* timeout can be set at any time */
    2432                 :          0 :         unsigned long timeout;
    2433                 :          0 :         int rv = strict_strtoul_scaled(buf, &timeout, 4);
    2434         [ #  # ]:          0 :         if (rv)
    2435                 :          0 :                 return rv;
    2436                 :            : 
    2437                 :            :         /* just to make sure we don't overflow... */
    2438         [ #  # ]:          0 :         if (timeout >= LONG_MAX / HZ)
    2439                 :            :                 return -EINVAL;
    2440                 :            : 
    2441                 :          0 :         timeout = timeout * HZ / 10000;
    2442                 :            : 
    2443                 :          0 :         if (timeout >= MAX_SCHEDULE_TIMEOUT)
    2444                 :            :                 timeout = MAX_SCHEDULE_TIMEOUT-1;
    2445         [ #  # ]:          0 :         if (timeout < 1)
    2446                 :          0 :                 timeout = 1;
    2447                 :          0 :         mddev->bitmap_info.daemon_sleep = timeout;
    2448         [ #  # ]:          0 :         if (mddev->thread) {
    2449                 :            :                 /* if thread->timeout is MAX_SCHEDULE_TIMEOUT, then
    2450                 :            :                  * the bitmap is all clean and we don't need to
    2451                 :            :                  * adjust the timeout right now
    2452                 :            :                  */
    2453         [ #  # ]:          0 :                 if (mddev->thread->timeout < MAX_SCHEDULE_TIMEOUT) {
    2454                 :          0 :                         mddev->thread->timeout = timeout;
    2455                 :          0 :                         md_wakeup_thread(mddev->thread);
    2456                 :            :                 }
    2457                 :            :         }
    2458                 :          0 :         return len;
    2459                 :            : }
    2460                 :            : 
    2461                 :            : static struct md_sysfs_entry bitmap_timeout =
    2462                 :            : __ATTR(time_base, S_IRUGO|S_IWUSR, timeout_show, timeout_store);
    2463                 :            : 
    2464                 :            : static ssize_t
    2465                 :          0 : backlog_show(struct mddev *mddev, char *page)
    2466                 :            : {
    2467                 :          0 :         return sprintf(page, "%lu\n", mddev->bitmap_info.max_write_behind);
    2468                 :            : }
    2469                 :            : 
    2470                 :            : static ssize_t
    2471                 :          0 : backlog_store(struct mddev *mddev, const char *buf, size_t len)
    2472                 :            : {
    2473                 :          0 :         unsigned long backlog;
    2474                 :          0 :         unsigned long old_mwb = mddev->bitmap_info.max_write_behind;
    2475                 :          0 :         int rv = kstrtoul(buf, 10, &backlog);
    2476         [ #  # ]:          0 :         if (rv)
    2477                 :          0 :                 return rv;
    2478         [ #  # ]:          0 :         if (backlog > COUNTER_MAX)
    2479                 :            :                 return -EINVAL;
    2480                 :          0 :         mddev->bitmap_info.max_write_behind = backlog;
    2481   [ #  #  #  # ]:          0 :         if (!backlog && mddev->serial_info_pool) {
    2482                 :            :                 /* serial_info_pool is not needed if backlog is zero */
    2483         [ #  # ]:          0 :                 if (!mddev->serialize_policy)
    2484                 :          0 :                         mddev_destroy_serial_pool(mddev, NULL, false);
    2485   [ #  #  #  # ]:          0 :         } else if (backlog && !mddev->serial_info_pool) {
    2486                 :            :                 /* serial_info_pool is needed since backlog is not zero */
    2487                 :          0 :                 struct md_rdev *rdev;
    2488                 :            : 
    2489         [ #  # ]:          0 :                 rdev_for_each(rdev, mddev)
    2490                 :          0 :                         mddev_create_serial_pool(mddev, rdev, false);
    2491                 :            :         }
    2492         [ #  # ]:          0 :         if (old_mwb != backlog)
    2493                 :          0 :                 md_bitmap_update_sb(mddev->bitmap);
    2494                 :          0 :         return len;
    2495                 :            : }
    2496                 :            : 
    2497                 :            : static struct md_sysfs_entry bitmap_backlog =
    2498                 :            : __ATTR(backlog, S_IRUGO|S_IWUSR, backlog_show, backlog_store);
    2499                 :            : 
    2500                 :            : static ssize_t
    2501                 :          0 : chunksize_show(struct mddev *mddev, char *page)
    2502                 :            : {
    2503                 :          0 :         return sprintf(page, "%lu\n", mddev->bitmap_info.chunksize);
    2504                 :            : }
    2505                 :            : 
    2506                 :            : static ssize_t
    2507                 :          0 : chunksize_store(struct mddev *mddev, const char *buf, size_t len)
    2508                 :            : {
    2509                 :            :         /* Can only be changed when no bitmap is active */
    2510                 :          0 :         int rv;
    2511                 :          0 :         unsigned long csize;
    2512         [ #  # ]:          0 :         if (mddev->bitmap)
    2513                 :            :                 return -EBUSY;
    2514                 :          0 :         rv = kstrtoul(buf, 10, &csize);
    2515         [ #  # ]:          0 :         if (rv)
    2516                 :          0 :                 return rv;
    2517   [ #  #  #  # ]:          0 :         if (csize < 512 ||
    2518                 :            :             !is_power_of_2(csize))
    2519                 :            :                 return -EINVAL;
    2520                 :          0 :         mddev->bitmap_info.chunksize = csize;
    2521                 :          0 :         return len;
    2522                 :            : }
    2523                 :            : 
    2524                 :            : static struct md_sysfs_entry bitmap_chunksize =
    2525                 :            : __ATTR(chunksize, S_IRUGO|S_IWUSR, chunksize_show, chunksize_store);
    2526                 :            : 
    2527                 :          0 : static ssize_t metadata_show(struct mddev *mddev, char *page)
    2528                 :            : {
    2529         [ #  # ]:          0 :         if (mddev_is_clustered(mddev))
    2530                 :          0 :                 return sprintf(page, "clustered\n");
    2531         [ #  # ]:          0 :         return sprintf(page, "%s\n", (mddev->bitmap_info.external
    2532                 :            :                                       ? "external" : "internal"));
    2533                 :            : }
    2534                 :            : 
    2535                 :          0 : static ssize_t metadata_store(struct mddev *mddev, const char *buf, size_t len)
    2536                 :            : {
    2537         [ #  # ]:          0 :         if (mddev->bitmap ||
    2538         [ #  # ]:          0 :             mddev->bitmap_info.file ||
    2539         [ #  # ]:          0 :             mddev->bitmap_info.offset)
    2540                 :            :                 return -EBUSY;
    2541         [ #  # ]:          0 :         if (strncmp(buf, "external", 8) == 0)
    2542                 :          0 :                 mddev->bitmap_info.external = 1;
    2543         [ #  # ]:          0 :         else if ((strncmp(buf, "internal", 8) == 0) ||
    2544         [ #  # ]:          0 :                         (strncmp(buf, "clustered", 9) == 0))
    2545                 :          0 :                 mddev->bitmap_info.external = 0;
    2546                 :            :         else
    2547                 :            :                 return -EINVAL;
    2548                 :          0 :         return len;
    2549                 :            : }
    2550                 :            : 
    2551                 :            : static struct md_sysfs_entry bitmap_metadata =
    2552                 :            : __ATTR(metadata, S_IRUGO|S_IWUSR, metadata_show, metadata_store);
    2553                 :            : 
    2554                 :          0 : static ssize_t can_clear_show(struct mddev *mddev, char *page)
    2555                 :            : {
    2556                 :          0 :         int len;
    2557                 :          0 :         spin_lock(&mddev->lock);
    2558         [ #  # ]:          0 :         if (mddev->bitmap)
    2559         [ #  # ]:          0 :                 len = sprintf(page, "%s\n", (mddev->bitmap->need_sync ?
    2560                 :            :                                              "false" : "true"));
    2561                 :            :         else
    2562                 :          0 :                 len = sprintf(page, "\n");
    2563                 :          0 :         spin_unlock(&mddev->lock);
    2564                 :          0 :         return len;
    2565                 :            : }
    2566                 :            : 
    2567                 :          0 : static ssize_t can_clear_store(struct mddev *mddev, const char *buf, size_t len)
    2568                 :            : {
    2569         [ #  # ]:          0 :         if (mddev->bitmap == NULL)
    2570                 :            :                 return -ENOENT;
    2571         [ #  # ]:          0 :         if (strncmp(buf, "false", 5) == 0)
    2572                 :          0 :                 mddev->bitmap->need_sync = 1;
    2573         [ #  # ]:          0 :         else if (strncmp(buf, "true", 4) == 0) {
    2574         [ #  # ]:          0 :                 if (mddev->degraded)
    2575                 :            :                         return -EBUSY;
    2576                 :          0 :                 mddev->bitmap->need_sync = 0;
    2577                 :            :         } else
    2578                 :            :                 return -EINVAL;
    2579                 :          0 :         return len;
    2580                 :            : }
    2581                 :            : 
    2582                 :            : static struct md_sysfs_entry bitmap_can_clear =
    2583                 :            : __ATTR(can_clear, S_IRUGO|S_IWUSR, can_clear_show, can_clear_store);
    2584                 :            : 
    2585                 :            : static ssize_t
    2586                 :          0 : behind_writes_used_show(struct mddev *mddev, char *page)
    2587                 :            : {
    2588                 :          0 :         ssize_t ret;
    2589                 :          0 :         spin_lock(&mddev->lock);
    2590         [ #  # ]:          0 :         if (mddev->bitmap == NULL)
    2591                 :          0 :                 ret = sprintf(page, "0\n");
    2592                 :            :         else
    2593                 :          0 :                 ret = sprintf(page, "%lu\n",
    2594                 :            :                               mddev->bitmap->behind_writes_used);
    2595                 :          0 :         spin_unlock(&mddev->lock);
    2596                 :          0 :         return ret;
    2597                 :            : }
    2598                 :            : 
    2599                 :            : static ssize_t
    2600                 :          0 : behind_writes_used_reset(struct mddev *mddev, const char *buf, size_t len)
    2601                 :            : {
    2602         [ #  # ]:          0 :         if (mddev->bitmap)
    2603                 :          0 :                 mddev->bitmap->behind_writes_used = 0;
    2604                 :          0 :         return len;
    2605                 :            : }
    2606                 :            : 
    2607                 :            : static struct md_sysfs_entry max_backlog_used =
    2608                 :            : __ATTR(max_backlog_used, S_IRUGO | S_IWUSR,
    2609                 :            :        behind_writes_used_show, behind_writes_used_reset);
    2610                 :            : 
    2611                 :            : static struct attribute *md_bitmap_attrs[] = {
    2612                 :            :         &bitmap_location.attr,
    2613                 :            :         &bitmap_space.attr,
    2614                 :            :         &bitmap_timeout.attr,
    2615                 :            :         &bitmap_backlog.attr,
    2616                 :            :         &bitmap_chunksize.attr,
    2617                 :            :         &bitmap_metadata.attr,
    2618                 :            :         &bitmap_can_clear.attr,
    2619                 :            :         &max_backlog_used.attr,
    2620                 :            :         NULL
    2621                 :            : };
    2622                 :            : struct attribute_group md_bitmap_group = {
    2623                 :            :         .name = "bitmap",
    2624                 :            :         .attrs = md_bitmap_attrs,
    2625                 :            : };
    2626                 :            : 

Generated by: LCOV version 1.14