Abstract:
A storage controller for determining an amount of data to be sent to a flash memory apparatus for storage comprises a communications interface for communicating with the flash memory apparatus and a processor. The flash memory apparatus comprises a block including a plurality of pages. And at least one of the pages is unavailable for storage. The processor is configured to receive information of the block sent by the flash memory apparatus, wherein the information includes capacity of one or more unavailable pages in the block. And then, the processor determines an available capacity of the block, based on the information and a total capacity of the block. Further, the processor obtains data to be sent to the flash memory apparatus, wherein an amount of the data is equal to the available capacity of the block. At last, the processor sends the data to the flash memory apparatus.

Description:
CROSS-REFERENCE TO RELATED APPLICATIONS 
       [0001]    This application is a continuation of International Application NO. PCT/CN2014/080984, filed on 27 Jun., 2014, which is hereby incorporated by reference in its entirety. 
     
    
     TECHNICAL FIELD 
       [0002]    Embodiments of the present invention relate to the field of storage technologies, and in particular, to a controller, a flash memory apparatus, and a method for writing data into a flash memory apparatus. 
       BACKGROUND 
       [0003]    A flash memory apparatus is a non-volatile memory whose storage medium is a Flash unit, and has a characteristic that data does not disappear after a power outage. Therefore, the flash memory apparatus is widely used as an external or internal memory. A flash memory apparatus using a Flash unit as a storage medium may be a Solid State Disk (SSD), which is also referred to as an Solid State Drive (SSD), or another memory. 
         [0004]    One SSD generally includes multiple flash chips, and each flash chip includes several blocks (block), where each block further includes multiple pages. In some cases, a page that is damaged (also referred to as a damaged page) may occur in a block. When an SSD writes data into a block that includes a damaged page, if the block that includes a damaged page is insufficient to store the data, generally a new block is found to store an overflow of the data. Therefore, in the SSD, there are a large number of blocks that have been written with some data but are not filled with data, which reduces space utilization of blocks, and causes a waste of storage space of the SSD. 
       SUMMARY 
       [0005]    Embodiments of the present invention provide a controller, a flash memory apparatus, and a method for writing data into a flash memory apparatus, which can improve space utilization of a block and save storage space of an SSD. 
         [0006]    According to a first aspect, an embodiment of the present invention provides a controller, where the controller is applied in a storage system, the storage system includes the controller and a flash memory apparatus, the flash memory apparatus includes a flash chip, the flash chip includes a block, the block includes multiple pages, and at least one of the multiple pages is a damaged page; and the controller includes a communications interface and a processor; 
         [0007]    the communications interface is configured to communicate with the flash memory apparatus; and 
         [0008]    the processor is configured to: receive capacity information of the block that is sent by the flash memory apparatus; 
         [0009]    obtain an effective capacity of the block according to the capacity information of the block, where the effective capacity of the block does not include a capacity of the damaged page; 
         [0010]    read target data, where a size of the target data is the effective capacity of the block of the block; and 
         [0011]    send the target data to the flash memory apparatus. 
         [0012]    In a first possible implementation manner of the first aspect, the capacity information of the block includes information about the damaged page, the information about the damaged page is used to indicate the capacity of the damaged page, the processor is specifically configured to obtain the effective capacity of the block of the block according to a pre-stored standard capacity of the block and the capacity of the damaged page, and the effective capacity of the block of the block is the standard capacity of the block minus the capacity of the damaged page. 
         [0013]    In a second possible implementation manner of the first aspect, the capacity information of the block includes the effective capacity of the block of the block. 
         [0014]    In a third possible implementation manner of the first aspect, the capacity information of the block includes a capacity flag of the block, the controller further includes a memory, and a correspondence between the capacity flag of the block and the effective capacity of the block of the block is stored in the memory; and 
         [0015]    the processor is specifically configured to obtain the effective capacity of the block of the block according to the capacity flag of the block, and the correspondence between the capacity flag of the block and the effective capacity of the block of the block. 
         [0016]    With reference to the first aspect, or any one of the first to the third possible implementation manners of the first aspect, in a fourth possible implementation manner, the controller further includes a cache, and the target data is to-be-written data stored in the cache; 
         [0017]    the processor is further configured to receive multiple write data requests, and write the to-be-written data into the cache, where the multiple write data requests carry the to-be-written data; and 
         [0018]    the processor is further configured to determine that a size of the to-be-written data carried in the multiple write data requests is equal to the effective capacity of the block of the block. 
         [0019]    With reference to the first aspect, or any one of the first to the third possible implementation manners of the first aspect, in a fifth possible implementation manner, the controller further includes a cache, and the target data is a part of to-be-written data stored in the cache; 
         [0020]    the processor is further configured to receive multiple write data requests, and write the to-be-written data into the cache, where the multiple write data requests carry the to-be-written data; and 
         [0021]    the processor is further configured to determine that a size of the to-be-written data is greater than the effective capacity of the block of the block. 
         [0022]    According to a second aspect, an embodiment of the present invention provides a flash memory apparatus, where the flash memory apparatus includes a primary controller and a flash chip, the flash chip includes a block, the block includes multiple pages, and at least one of the multiple pages is a damaged page; 
         [0023]    the flash chip is configured to store target data; and 
         [0024]    the primary controller is configured to: collect statistics on capacity information of the block, where the capacity information of the block is used to obtain an effective capacity of the block, and the effective capacity of the block does not include a capacity of the damaged page; 
         [0025]    send the capacity information of the block to a controller; 
         [0026]    receive the target data sent by the controller, where a size of the target data is the effective capacity of the block of the block; and 
         [0027]    write the target data into the block. 
         [0028]    In a first possible implementation manner of the second aspect, the primary controller is further configured to receive a query command sent by the controller, and the query command is used to query the capacity information of the block. 
         [0029]    In a second possible implementation manner of the second aspect, the primary controller is specifically configured to periodically send the capacity information of the block to the controller. 
         [0030]    In a third possible implementation manner of the second aspect, before the sending the capacity information of the block to a controller, the primary controller is further configured to determine that the capacity information of the block is different from capacity information of the block that is obtained through previous statistics collection. 
         [0031]    With reference to the second aspect, or any one of the first to the third possible implementation manners of the second aspect, in a fourth possible implementation manner, the capacity information of the block includes information about the damaged page, and the information about the damaged page is used to indicate the capacity of the damaged page. 
         [0032]    With reference to the second aspect, or any one of the first to the third possible implementation manners of the second aspect, in a fifth possible implementation manner, the capacity information of the block includes the effective capacity of the block of the block. 
         [0033]    According to a third aspect, an embodiment of the present invention provides a method for writing data into a flash memory apparatus, where the method is applied in a storage system, the storage system includes a controller and the flash memory apparatus, the flash memory apparatus includes a flash chip, the flash chip includes a block, the block includes multiple pages, and at least one of the multiple pages is a damaged page; and the method includes: 
         [0034]    receiving, by the controller, capacity information of the block that is sent by the flash memory apparatus; 
         [0035]    obtaining, by the controller, an effective capacity of the block according to the capacity information of the block, where the effective capacity of the block does not include a capacity of the damaged page; 
         [0036]    reading, by the controller, target data, where a size of the target data is the effective capacity of the block of the block; and 
         [0037]    sending, by the controller, the target data to the flash memory apparatus. 
         [0038]    In a first possible implementation manner of the third aspect, the capacity information of the block includes information about the damaged page, and the information about the damaged page is used to indicate the capacity of the damaged page; and 
         [0039]    the obtaining, by the controller, an effective capacity of the block according to the capacity information of the block includes: 
         [0040]    obtaining, by the controller, the effective capacity of the block of the block according to a pre-stored standard capacity of the block and the capacity of the damaged page, where the effective capacity of the block of the block is the standard capacity of the block minus the capacity of the damaged page. 
         [0041]    In a second possible implementation manner of the third aspect, the capacity information of the block includes the effective capacity of the block of the block. 
         [0042]    In a third possible implementation manner of the third aspect, the capacity information of the block includes a capacity flag of the block, a correspondence between the capacity flag of the block and the effective capacity of the block of the block is stored in the memory; and 
         [0043]    the obtaining, by the controller, an effective capacity of the block according to the capacity information of the block includes: 
         [0044]    obtaining, by the controller, the effective capacity of the block of the block according to the capacity flag of the block, and the correspondence between the capacity flag of the block and the effective capacity of the block of the block. 
         [0045]    With reference to the third aspect, or any one of the first to the third possible implementation manner of the third aspect, in a fourth possible implementation manner, the controller includes a processor and a cache, and the target data is to-be-written data stored in the cache; and the method further includes: 
         [0046]    receiving, by the processor, multiple write data requests, and writing the to-be-written data into the cache, where the multiple write data requests carry the to-be-written data; and 
         [0047]    determining, by the processor, that a size of the to-be-written data carried in the multiple write data requests is equal to the effective capacity of the block of the block. 
         [0048]    With reference to the third aspect, or any one of the first to the third possible implementation manner of the third aspect, in a fifth possible implementation manner, the controller includes a processor and a cache, and the target data is a part of to-be-written data stored in the cache; and the method further includes: 
         [0049]    receiving, by the processor, multiple write data requests, and writing the to-be-written data into the cache, where the multiple write data requests carry the to-be-written data; and 
         [0050]    determining, by the processor, that a size of the to-be-written data is greater than the effective capacity of the block of the block. 
         [0051]    According to a fourth aspect, an embodiment of the present invention provides a method for writing data into a flash memory apparatus, where the flash memory apparatus includes a primary controller and a flash chip, the flash chip includes a block, the block includes multiple pages, and at least one of the multiple pages is a damaged page; and the method includes: 
         [0052]    collecting, by the primary controller, statistics on capacity information of the block, where the capacity information of the block is used to obtain an effective capacity of the block, and the effective capacity of the block does not include a capacity of the damaged page; 
         [0053]    sending, by the primary controller, the capacity information of the block to a controller; 
         [0054]    receiving, by the primary controller, target data sent by the controller, where a size of the target data is the effective capacity of the block of the block; and 
         [0055]    writing, by the primary controller, the target data into the block. 
         [0056]    In a first possible implementation manner of the fourth aspect, the method further includes: 
         [0057]    receiving, by the primary controller, a query command sent by the controller, where the query command is used to query the capacity information of the block. 
         [0058]    In a second possible implementation manner of the fourth aspect, the sending, by the primary controller, the capacity information of the block to a controller includes: periodically sending, by the primary controller, the capacity information of the block to the controller. 
         [0059]    In a third possible implementation manner of the fourth aspect, before the sending the capacity information of the block to a controller, the method further includes: determining, by the primary controller, that the capacity information of the block is different from capacity information of the block that is obtained through previous statistics collection. 
         [0060]    With reference to the fourth aspect, or any one of the first to the third possible implementation manners of the fourth aspect, in a fourth possible implementation manner, the capacity information of the block includes information about the damaged page, and the information about the damaged page is used to indicate the capacity of the damaged page. 
         [0061]    With reference to the fourth aspect, or any one of the first to the third possible implementation manners of the fourth aspect, in a fifth possible implementation manner, the capacity information of the block includes the effective capacity of the block of the block. 
         [0062]    The embodiments of the present invention provide a controller, and a method for writing data into a flash memory apparatus. The controller receives capacity information of a block that is sent by a flash memory apparatus; obtains an effective capacity of the block according to the capacity information of the block, where the effective capacity of the block does not include a capacity of a damaged page; reads target data, where a size of the target data is the effective capacity of the block of the block; and sends the target data to the flash memory apparatus. According to implementation manners of the embodiments of the present invention, if the block has a damaged page, the controller may send, to the flash memory apparatus, the target data whose size is the same as the effective capacity of the block of the block, so that the flash memory apparatus writes the target data into the block. Therefore, according to the embodiments of the present invention, it may be ensured that target data written into a block of the flash memory apparatus fills the block to a greatest extent without overflow data, thereby improving block utilization and saving storage space of an SSD. 
         [0063]    In addition, the embodiments of the present invention further provide a flash memory apparatus, and a method for writing data into a flash memory apparatus. The flash memory apparatus collects statistics on capacity information of a block, where the capacity information of the block is used to obtain an effective capacity of the block, and the effective capacity of the block does not include a capacity of a damaged page; sends the capacity information of the block to a controller; receives target data sent by the controller, where a size of the target data is the effective capacity of the block of the block; and writes the target data into the block. The flash memory apparatus may collect the statistics on the capacity information of the block, and send the capacity information of the block to the controller, so that the controller may send, to the flash memory apparatus, the target data whose size is the same as the effective capacity of the block of the block, and the flash memory apparatus writes the target data into the block. Therefore, according to the embodiments of the present invention, it may be ensured that target data written into a block of the flash memory apparatus fills the block to a greatest extent without overflow data, thereby improving block utilization and saving storage space of an SSD. 
     
    
     
       BRIEF DESCRIPTION OF DRAWINGS 
         [0064]    To describe the technical solutions in the embodiments of the present invention more clearly, the following briefly introduces the accompanying drawings required for describing the embodiments. 
           [0065]      FIG. 1  is a schematic structural diagram of a storage system according to an embodiment of the present invention; 
           [0066]      FIG. 2  is a schematic structural diagram of a controller according to an embodiment of the present invention; 
           [0067]      FIG. 3   a  is a schematic structural diagram of a storage medium of a flash memory apparatus according to an embodiment of the present invention; 
           [0068]      FIG. 3   b  is a schematic structural diagram of a primary controller of a flash memory apparatus according to an embodiment of the present invention; 
           [0069]      FIG. 4  is a schematic flowchart of writing data into a flash memory apparatus according to an embodiment of the present invention; 
           [0070]      FIG. 5  is another schematic flowchart of writing data into a flash memory apparatus according to an embodiment of the present invention; and 
           [0071]      FIG. 6  is still another schematic flowchart of writing data into a flash memory apparatus according to an embodiment of the present invention. 
       
    
    
     DESCRIPTION OF EMBODIMENTS 
       [0072]    Embodiments of the present invention provide a controller, a flash memory apparatus, and a method for writing data into a flash memory apparatus, which can improve space utilization of a block and save storage space of an SSD. 
         [0073]      FIG. 1  depicts a schematic structural diagram of a storage system according to an embodiment of the present invention. The storage system shown in  FIG. 1  includes a controller  11  and a flash memory apparatus  22 . The flash memory apparatus  22  is a storage apparatus that uses a Flash unit as a storage medium, may include an Solid State Disk (SSD), which is also referred to as a Solid State Drive (SSD), and may further include another memory. In this embodiment, the flash memory apparatus  22  is described by using an SSD as an example. 
         [0074]      FIG. 1  is only exemplarily illustrative and does not limit a specific networking manner, for example, both cascading tree networking and ring networking may be used as long as the controller  11  and the flash memory apparatus  22  can communicate with each other. 
         [0075]    The controller  11  may include any computing device known in the prior art, for example, a server or a desktop computer. An operating system and other application programs are installed in the controller  11 . The controller  11  may send an input/output (I/O) request to the flash memory apparatus  22 . For example, a write data request is sent to the flash memory apparatus  22 , so that the flash memory apparatus  22  writes to-be-written data carried in the write data request into the storage medium of the flash memory apparatus  22 . 
         [0076]    Referring to  FIG. 2 ,  FIG. 2  is a schematic structural diagram of a controller  11  according to an embodiment of the present invention. As shown in  FIG. 2 , the controller  11  mainly includes a processor  118 , a cache  120 , a memory  122 , a communications bus (a bus for short)  126 , and a communications interface  128 . The processor  118 , the cache  120 , the memory  122 , and the communications interface  128  complete mutual communication by using the communications bus  126 . 
         [0077]    The communications interface  128  is configured to communicate with a host (not shown in the figure) or a flash memory apparatus  22 . 
         [0078]    The memory  122  is configured to store a program  124 , and the memory  122  may include a high-speed Random-Access Memory (RAM) memory, or may include a non-volatile memory, for example, at least one disk memory. It may be understood that the memory  122  may be any non-transitory machine-readable medium that can store program code, such as a RAM, a magnetic disk, a hard disk, an optical disc, an SSD, or a non-volatile memory. 
         [0079]    Specifically, the program  124  may include program code, where the program code includes a computer operation instruction. 
         [0080]    The cache  120  is configured to temporarily stored data received from the host or data read from the flash memory apparatus  22 . The cache  120  may be any non-transitory machine-readable medium that can store data, such as a RAM, a ROM, a Flash memory, or a SSD, which is not limited herein. For example, when receiving a write data request sent by the host, the controller  11  may store the write data request in the cache  120 , and then the processor  118  processes the write data request. Optionally, when receiving one write data request sent by the host, the controller  11  may first store the write data request in the cache  120 ; then read the one write data request from the cache  120 , and send the one write data request to the flash memory apparatus  22  for processing. Alternatively, when receiving multiple write data requests sent by the host, the controller  11  may temporarily store the multiple write data requests in the cache  120 ; when to-be-written data carried in the multiple write data requests stored in the cache  120  reaches a set threshold, the controller  11  may send the to-be-written data carried in the multiple write data requests to the flash memory apparatus  22  for processing. 
         [0081]    In addition, the memory  122  and the cache  120  may be disposed together or separately, which is not limited in this embodiment of the present invention. 
         [0082]    The processor  118  may be a central processing unit CPU, an application specific integrated circuit ASIC (Application Specific Integrated Circuit), or one or more integrated circuits configured to implement this embodiment of the present invention. In this embodiment of the present invention, the processor  118  may be configured to receive a write data request or a read data request from the host, process the write data request or the read data request, send the write data request or the read data request to the flash memory apparatus  22 , and perform other operations. 
         [0083]    Referring to  FIG. 3   a ,  FIG. 3   a  is a schematic structural diagram of a flash memory apparatus  22  according to an embodiment of the present invention. In this embodiment, the flash memory apparatus  22  is described by using an SSD as an example. 
         [0084]    As shown in  FIG. 3   a , the flash memory apparatus  22  includes a primary controller  220  and a storage medium  221 . The primary controller  220  is configured to execute a write data request or a read data request sent by a controller  11 , and operations such as collecting statistics on damaged pages. The primary controller  220  herein is a primary controller of an SSD. 
         [0085]    The storage medium  221  generally includes several flash chips. In an SSD, channels are used to connect the several flash chips together. Concurrent processing of write data requests may be implemented for the channels. Four channels shown in  FIG. 3   a  are used as an example. If the primary controller  220  receives four write data requests sent by the controller  11 , the four channels each may execute a write data request, thereby improving efficiency in processing write data requests. In addition, according to this embodiment of the present invention, concurrent processing of write data requests may also be implemented for multiple concurrent units on one channel, which is not limited herein. 
         [0086]    Each flash chip includes several blocks, and an erase operation performed by an SSD is executed with a block as a unit. For example, when the SSD needs to perform garbage collection, valid data in a block may be first moved to another new block, and then all data (including valid data and invalid data) stored in the original block is erased. In this embodiment of the present invention, valid data in a block refers to data that is stored in the block and has not been modified, and this part of data may be read; and invalid data in a block refers to data that is stored in the block and has been modified, and this part of data cannot be read. A person skilled in the art may learn that due to an erase feature of a flash unit, data stored in a block may not be directly modified like a common mechanical hard drive. When data in a block needs to be modified, the primary controller  220  finds a new block and writes modified data into the new block, and the data in the original block becomes invalid data. When the SSD performs garbage collection, the invalid data is erased. 
         [0087]    It may be learned from  FIG. 3   a  that each block may include several pages. In some cases, damage may occur in a page in a block, and a page in which damage occurs is referred to as damaged page in this embodiment of the present invention. When there is a damaged page in a block, an actual capacity of the block is less than a capacity of a block that does not include a damaged page. In this embodiment of the present invention, an actual capacity of a block is referred to as an effective capacity. For example, a standard capacity of a block is 1 M, and a size of each page is 4 KB. When there is one damaged page in the block, an effective capacity of the block is 1 M minus 4 KB. In this embodiment of the present invention, a standard capacity of a block refers to a capacity of a blank block that does not include a damaged page, and a blank block refers to a block that is erased clean and includes neither valid data nor invalid data. An effective capacity of a block is equal to a standard capacity of the block minus a capacity of damaged pages, where the capacity of damaged pages is equal to a product of a size of each damaged page and the number of damaged pages. 
         [0088]    In this embodiment of the present invention, a standard capacity of a block may be pre-stored in the controller  11  and used by the controller  11  to send, to the flash memory apparatus  22 , target data whose size is the same as the standard capacity. A standard capacity of each block is the N th  power (M) of 2, where N is a positive integer. Standard capacities of blocks may be the same or different. When standard capacities of blocks are different, values of N may be different. In this case, a standard capacity of a largest block may be considered as the standard capacity used in any implementation manner of the embodiments of the present invention that are shown in  FIG. 4  to  FIG. 6 . 
         [0089]    In addition, in this embodiment of the present invention, when the SSD executes a write data request, data is also written by using a page as a unit. For example, the controller  11  sends a write data request to the primary controller  220 , where the write data request carries a segment of logical block addresses (Logical Block Address, LBA) and target data, and the LBAs are addresses that can be accessed by the controller  11 . When receiving the write data request, the primary controller  220  may write the target data into a block according to a predetermined policy, and addresses of multiple pages into which the target data is written are addresses for actually storing the target data, and are also referred to as physical address. The SSD may establish and store a correspondence between the segment of LBAs and the addresses of the multiple pages. When the controller  11  subsequently sends a read data request to the primary controller  220  and requests to read the target data, the read data request carries the LBAs. In this case, the primary controller  220  may read out the target data according to the LBAs and the correspondence between the LBAs and the physical addresses, and return the target data to the controller  11 . 
         [0090]    The following introduces a structure and a function of a primary controller  220 . Referring to  FIG. 3   b ,  FIG. 3   b  is a schematic structural diagram of a primary controller  220  of a flash memory apparatus  22  according to an embodiment of the present invention. 
         [0091]    The primary controller  220  mainly includes a processor  218 , a cache  230 , a communications bus (a bus for short)  226 , and a communications interface  228 . The processor  218 , the cache  230 , and the communications interface  228  complete mutual communication by using the communications bus  226 . 
         [0092]    The communications interface  228  is configured to communicate with a controller  11  and a storage medium  221 . 
         [0093]    The cache  230  is configured to temporarily stored data received from the controller  11  and data read from the storage medium  221 . The cache  230  may be any non-transitory (non-transitory) machine-readable medium that can store data, such as a RAM, a ROM, a Flash memory, or a SSD, which is not limited herein. For example, when a write data request sent by the controller  11  is received, the write data request may be stored in the cache  230  and is processed by the processor  218 . In addition, in some application scenarios, the cache  230  may also be disposed outside the primary controller  220 . 
         [0094]    The processor  218  may be a central processing unit CPU, an application specific integrated circuit ASIC (Application Specific Integrated Circuit), or one or more integrated circuits configured to implement this embodiment of the present invention. In this embodiment of the present invention, the processor  218  may be configured to receive a write data request or a read data request from the controller  11 , process the write data request or the read data request, send the write data request or the read data request to the storage medium  221 , and perform other operations. 
         [0095]    The processor  218  may further include a cache (not shown in the figure), configured to store various program instructions. For example, the cache may include a Flash Translation Layer (FTL). The processor  218  may perform an operation such as collecting statistics on damaged pages by using the FTL, and store a result of the collecting statistics on damaged pages in configuration information of the FTL. Alternatively, the processor  218  may achieve a similar function by using another software module. Therefore, any software module that has a function similar to that of the FTL and may perform an operation such as collecting statistics on damaged pages and store a result of the collecting statistics on damaged pages in configuration information of the software module falls into the protection scope of embodiments of the present invention. 
         [0096]    The following introduces a method procedure for writing data into a flash memory apparatus according to an embodiment of the present invention. The method for writing data into a flash memory apparatus in this embodiment of the present invention may be applied in the storage system shown in  FIG. 1 , the controller  11  shown in  FIG. 2 , and the flash memory apparatus (for example, an SSD) shown in  FIG. 3   a  and  FIG. 3   b . The flash memory apparatus includes a block, and the block includes multiple pages in which at least one page is a damaged page. As shown in  FIG. 4 , the method includes the following steps: 
         [0097]    Step S 101 : A flash memory apparatus  22  collects statistics on capacity information of a block. 
         [0098]    Specifically, a primary controller  220  may collect statistics on damaged pages in the block by using an FTL, and save a statistical result in configuration information of the FTL. 
         [0099]    Herein, the capacity information of the block may refer to capacity information of one block in an SSD, or capacity information of multiple or all blocks in an SSD. To facilitate description, one block is used as an example for description in this embodiment of the present invention. 
         [0100]    The capacity information of the block in this embodiment of the present invention may be used to obtain, by the primary controller  220  or a controller  11 , an effective capacity of the block. 
         [0101]    Optionally, the capacity information of the block may include the number of damaged pages included in the block. When the primary controller  220  obtains, through statistics collection, the number of the damaged pages included in the block, the effective capacity of the block of the block is equal to a standard capacity of the block minus a capacity of the damaged pages, where the capacity of the damaged pages is equal to a product of the number of the damaged pages and a size of a damaged page. 
         [0102]    Optionally, the capacity information of the block may include the capacity of the damaged pages included in the block. 
         [0103]    Optionally, the capacity information of the block may be the effective capacity of the block of the block. 
         [0104]    Optionally, the capacity information of the block may be a capacity flag of the block, or other information used to obtain the effective capacity of the block of the block. This embodiment of the present invention imposes no limitation on a form and content of the capacity information of the block. 
         [0105]    Step S 102 : The flash memory apparatus  22  sends the capacity information of the block to the controller  11 . 
         [0106]    Specifically, the primary controller  220  of the flash memory apparatus  22  may send the capacity information of the block to a processor  118  of the controller  11  through a communications interface  228  of the controller  11 . 
         [0107]    One optional implementation manner is that the controller  11  may send a query command to the flash memory apparatus  22  periodically or in real time, where the query command is used to query the capacity information of the block. After receiving the query command, the flash memory apparatus  22  starts to execute step S 101 , and sends the capacity information of the block to the controller  11  after the execution is completed. Specifically, the processor  118  of the controller  11  may send the query command to the flash memory apparatus  22 . 
         [0108]    Another optional implementation manner is that the primary controller  220  periodically executes step S 101 , and sends the capacity information of the block to the controller  11  each time after the execution is completed. Alternatively, the primary controller  220  periodically executes step S 101 , and the primary controller  220  compares a current statistical result with a previous statistical result. When finding that the capacity information of the block changes, the primary controller  220  sends the current statistical result to the controller  11 . It should be noted that this embodiment of the present invention imposes no limitation on a length of a period, and the length of a period may be adjusted according to a user requirement in an actual application. 
         [0109]    Step S 103 : The controller  11  obtains the effective capacity of the block of the block according to the capacity information of the block, where the effective capacity of the block of the block does not include the capacity of the damaged pages. 
         [0110]    Specifically, the processor  118  of the controller  11  may obtain the effective capacity of the block of the block according to the capacity information of the block. 
         [0111]    When the capacity information of the block includes the number of the damaged pages included in the block, the controller  11  may multiply the number of the damaged pages by the size of a damaged page to obtain the capacity of the damaged pages in the block, and then subtract the capacity of the damaged pages from the pre-stored standard capacity of the block, to obtain the effective capacity of the block of the block. 
         [0112]    When the capacity information of the block includes the capacity of the damaged pages included in the block, the controller  11  may subtract the capacity of the damaged pages from the pre-stored standard capacity of the block to obtain the effective capacity of the block of the block. 
         [0113]    When the capacity information of the block is the effective capacity of the block of the block, the controller  11  may directly obtain the effective capacity of the block of the block. 
         [0114]    When the capacity information of the block is a capacity flag of the block, the controller  11  may obtain the effective capacity of the block of the block according to the capacity flag and a correspondence between a capacity flag and an effective capacity. In this embodiment of the present invention, to obtain the effective capacity of the block of the block, the controller  11  may pre-store, in a memory  122  of the controller  11 , a correspondence between a capacity flag of each block and an effective capacity of each block, or pre-store, in a memory  122 , a correspondence between a capacity flag of each block and other capacity information. 
         [0115]    Step S 104 : The controller  11  reads target data, where a size of the target data is the effective capacity of the block of the block. 
         [0116]    Specifically, step S 104  may be executed by the processor  118  of the controller  11 . The processor  118  reads out the target data from a cache  120 , where the size of the target data is equal to the effective capacity of the block of the block. It should be noted that the size of the target data may not be necessarily equal to the effective capacity of the block of the block but may be slightly less than the effective capacity of the block of the block. 
         [0117]    The target data stored in the cache  120  may be from to-be-written data carried in a write data request sent by a host, and may be to-be-written data carried in one write data request or to-be-written data carried in multiple write data requests. 
         [0118]    Step S 105 : The controller  11  sends the target data to the flash memory apparatus  22 . 
         [0119]    Specifically, the processor  118  sends, through a communications interface  128 , the to-be-written data read out in step S 104  to the flash memory apparatus  22 . One optional implementation manner is that the processor  118  generates a new write data request, where the new write data request includes the target data; another optional implementation manner is that the processor  118  generates multiple new write data requests, where the multiple new write data requests each includes a part of the target data; and still another optional implementation manner is that the processor  118  directly forwards a write data request from a host to the flash memory apparatus  22 , where data carried in the write data request from the host is the target data. 
         [0120]    Step S 106 : The flash memory apparatus  22  writes the target data into the block. 
         [0121]    The size of the target data is the effective capacity of the block of the block; therefore, after the primary controller  220  writes the target data into another page of the block except the damaged pages, the block is exactly fully filled. 
         [0122]    In this embodiment of the present invention, a flash memory apparatus  22  sends capacity information of a block to a controller  11 , where the capacity information of the block is used to obtain an effective capacity of the block, the controller  11  may obtain the effective capacity of the block of the block according to the capacity information of the block, read target data whose size is the same as the effective capacity of the block of the block, and send the target data to the flash memory apparatus  22 , and the flash memory apparatus  22  writes the target data into the block. According to an implementation manner of this embodiment of the present invention, in a case in which the block has a damaged page, it may be ensured that the target data written into the block fills the block to a greatest extent without overflow data, thereby improving block utilization. 
         [0123]    In the foregoing embodiment, another implementation manner may be that the size of the target data read by the processor  118  from the cache  120  may be a sum of effective capacities of several blocks. An example in which the size of the target data may be a sum of effective capacities of four blocks is used. The processor  118  may generate four write data requests, where target data carried in each of the write data requests is equal to an effective capacity of one block among four blocks. Then the processor  118  sends the generated four write data requests to the primary controller  220 , and the primary controller  220  writes the generated four write data requests into blocks of four channels. According to this implementation manner, write data requests may be concurrently executed for channels of the flash memory apparatus  22 , thereby improving efficiency in writing data. In addition, in this embodiment of the present invention, concurrent processing of multiple write data requests may also be implemented for multiple concurrent units on one channel. 
         [0124]    The following introduces another method procedure for writing data into a flash memory apparatus according to an embodiment of the present invention. The method may be applied in the storage system shown in  FIG. 1 , the controller  11  shown in  FIG. 2 , and the flash memory apparatus (for example, an SSD) shown in  FIG. 3   a  and  FIG. 3   b . The flash memory apparatus includes a block, and the block includes multiple pages in which at least one page is a damaged page. As shown in  FIG. 5 , the method includes the following steps: 
         [0125]    Step S 201 : Step S 201  is the same as step S 101  in the embodiment shown in  FIG. 4 , and reference may be made to the description in step S 101 . 
         [0126]    Step S 202 : Step S 202  is the same as step S 102  in the embodiment shown in  FIG. 4 , and reference may be made to the description in step S 102 . 
         [0127]    Step S 203 : A processor  118  receives multiple write data requests. 
         [0128]    The processor  118  may receive multiple write data requests from a host or another device, where each of the write data requests carries data to be written into the flash memory apparatus  22  (to-be-written data for short). 
         [0129]    It should be noted that there is no sequence between step S 203  and step S 201  or step S 202 , and step S 203  may be executed before step S 201  and step S 202 , after step S 201  and step S 202 , or simultaneously with step S 201  and step S 202 . 
         [0130]    Step S 204 : The processor  118  writes the received multiple write data requests into a cache  120 . 
         [0131]    Because each of the write data requests carries to-be-written data, the to-be-written data is also stored in the cache  120 . 
         [0132]    Step S 205 : Step S 205  is the same as step S 103  in the embodiment shown in  FIG. 4 , and reference may be made to the description in step S 103 . 
         [0133]    Step S 206 : The processor  118  determines whether a size of the to-be-written data stored in the cache  120  reaches the effective capacity of the block of the block; and if the size of the to-be-written data stored in the cache  120  reaches the effective capacity of the block of the block, executes step S 207 ; if the size of the to-be-written data stored in the cache  120  does not reach the effective capacity of the block of the block, executes step S 203 . 
         [0134]    When a preset condition is met, the processor  118  may determine whether the size of the to-be-written data carried in the multiple write data requests and stored in the cache  120  reaches the effective capacity of the block of the block. The preset condition herein may be a moment at which a preset time interval starts (for example, triggering by a timer) or another triggering condition, which is not limited herein. 
         [0135]    If the size of the to-be-written data carried in the multiple write data requests and stored in the cache  120  has not reached the effective capacity of the block of the block, the processor  118  may temporarily wait for a period of time instead of processing the write data requests in the cache  120 . During this period of time, the processor  118  may continue to receive a write data request from the host until the size of the to-be-written data stored in the cache  120  reaches the effective capacity of the block of the block. 
         [0136]    Step S 207 : The processor  118  reads, from the cache  120 , the target data carried in the multiple write data requests. 
         [0137]    If the size of the to-be-written data carried in the multiple write data requests and stored in the cache  120  has reached the effective capacity of the block of the block, the processor  118  may read, from the cache  120 , the to-be-written data carried in the multiple write data requests. It may be understood that, when the size of the to-be-written data carried in the multiple write data requests and stored in the cache  120  has reached the effective capacity of the block of the block, the to-be-written data carried in the multiple write data requests and stored in the cache  120  at this moment is the target data in step S 104  to step S 106  in the embodiment shown in  FIG. 4 . 
         [0138]    Step S 208 : Step S 208  is the same as step S 105  in the embodiment shown in  FIG. 4 , and reference may be made to the description in step S 105 . 
         [0139]    Step S 209 : Step S 209  is the same as step S 106  in the embodiment shown in  FIG. 4 , and reference may be made to the description in step S 106 . 
         [0140]    In this embodiment of the present invention, a flash memory apparatus  22  sends capacity information of a block to a controller  11 , where the capacity information of the block is used to obtain an effective capacity of the block, the controller  11  may obtain the effective capacity of the block of the block according to the capacity information of the block, read target data whose size is the same as the effective capacity of the block of the block, and send the target data to the flash memory apparatus  22 , and the flash memory apparatus  22  writes the target data into the block. According to an implementation manner of this embodiment of the present invention, in a case in which the block has a damaged page, it may be ensured that the target data written into the block fills the block to a greatest extent without overflow data, thereby improving block utilization. 
         [0141]    The following introduces still another method procedure for writing data into a flash memory apparatus according to an embodiment of the present invention. The method may be applied in the storage system shown in  FIG. 1 , the controller  11  shown in  FIG. 2 , and the flash memory apparatus (for example, an SSD) shown in  FIG. 3   a  and  FIG. 3   b . The flash memory apparatus includes a block, and the block includes multiple pages in which at least one page is a damaged page. As shown in  FIG. 6 , the method includes the following steps: 
         [0142]    Step S 301 : Step S 301  is the same as step S 101  in the embodiment shown in  FIG. 4 , and reference may be made to the description in step S 101 . 
         [0143]    Step S 302 : Step S 302  is the same as step S 102  in the embodiment shown in  FIG. 4 , and reference may be made to the description in step S 102 . 
         [0144]    Step S 303 : Step S 303  is the same as step S 203  in the embodiment shown in  FIG. 5 , and reference may be made to the description in step S 203 . 
         [0145]    Step S 304 : Step S 304  is the same as step S 204  in the embodiment shown in  FIG. 5 , and reference may be made to the description in step S 204 . 
         [0146]    Step S 305 : Step S 305  is the same as step S 205  in the embodiment shown in  FIG. 5 , and reference may be made to the description in step S 205 . 
         [0147]    Step S 306 : The processor  118  determines that a size of the to-be-written data stored in the cache  120  is greater than the effective capacity of the block of the block. 
         [0148]    When a preset condition is met, the processor  118  may determine that a size of the to-be-written data that is carried in the multiple write data requests and stored in the cache  120  is greater than the effective capacity of the block of the block. The preset condition herein may be that a preset time interval arrives (for example, triggering by a timer) or another triggering condition, which is not limited herein. 
         [0149]    Step S 307 : The processor  118  reads a part of the to-be-written data from the cache  120 , where a size of the part of the to-be-written data is the effective capacity of the block of the block. 
         [0150]    When the size of the to-be-written data carried in the multiple write data requests is greater than the effective capacity of the block of the block, the processor  118  may read the part of the to-be-written data from the cache  120 , where the size of the part of the to-be-written data is the effective capacity of the block of the block. In this case, the part of the to-be-written data is the target data in step S 104  to step S 106  in the embodiment shown in  FIG. 4 . 
         [0151]    Step S 308 : Step S 308  is the same as step S 105  in the embodiment shown in  FIG. 4 , and reference may be made to the description in step S 105 . 
         [0152]    Step S 309 : Step S 309  is the same as step S 106  in the embodiment shown in  FIG. 4 , and reference may be made to the description in step S 106 . 
         [0153]    In this embodiment of the present invention, a flash memory apparatus  22  sends capacity information of a block to a controller  11 , where the capacity information of the block is used to obtain an effective capacity of the block, the controller  11  may obtain the effective capacity of the block of the block according to the capacity information of the block, read target data whose size is the same as the effective capacity of the block of the block, and send the target data to the flash memory apparatus  22 , and the flash memory apparatus  22  writes the target data into the block. According to an implementation manner of this embodiment of the present invention, in a case in which the block has a damaged page, it may be ensured that the target data written into the block fills the block to a greatest extent without overflow data, thereby improving block utilization. 
         [0154]    Further, in any one of the foregoing embodiments shown in  FIG. 4  to  FIG. 6 , addresses of multiple pages in a block into which the target data is written are physical addresses. After the primary controller  220  writes the target data into the block, the primary controller  220  may establish and store a correspondence between a segment of Logical Block Address (LBA) of the target data and physical addresses, which is used by the controller  11  to read the target data subsequently. 
         [0155]    It may be learned from any one of the embodiments shown in  FIG. 4  to  FIG. 6  that the target data is stored in one block; therefore, physical addresses thereof are a segment of consecutive physical space. If the target data is changed into other data subsequently, the target data stored in the block becomes invalid data, and an erase operation may be directly performed on the block without migrating valid data, thereby improving efficiency in garbage collection. 
         [0156]    In addition, if the controller  11  needs to perform defragment on the segment of LBAs corresponding to the target data, according to a technical principle of defragment, the controller  11  may send a data migration command to the primary controller  220 , where the data migration command carries the LBAs. After receiving the data migration command, the primary controller  220  may obtain, according to the correspondence between the LBAs and the physical addresses, the target data from pages in the block, and migrate the target data to another block, to complete the defragment operation. After the defragment operation is completed, the block is also accordingly erased clean and may receive new data, and garbage collection does not need to be performed on the block again. It may be seen that efficiency in garbage collection may further be improved by combining any one of the embodiments shown in  FIG. 4  to  FIG. 6  and the defragment operation. 
         [0157]    A person of ordinary skill in the art may understand that, each aspect of the present invention or a possible implementation manner of each aspect may be specifically implemented as a system, a method, or a computer program product. Therefore, each aspect of the present invention or a possible implementation manner of each aspect may use forms of hardware only embodiments, software only embodiments (including firmware, resident software, and the like), or embodiments with a combination of software and hardware, which are uniformly referred to as “circuit”, “module”, or “system” herein. In addition, each aspect of the present invention or the possible implementation manner of each aspect may take a form of a computer program product, where the computer program product refers to computer-readable program code stored in a computer-readable medium. 
         [0158]    The computer-readable medium may be a computer-readable signal medium or a computer-readable storage medium. The computer-readable storage medium includes but is not limited to an electronic, magnetic, optical, electromagnetic, infrared, or semi-conductive system, device, or apparatus, or any appropriate combination thereof, such as a random access memory (RAM), a read-only memory (ROM), an erasable programmable read only memory (EPROM or flash memory), an optical fiber, and a compact disc read only memory (CD-ROM). 
         [0159]    A processor in a computer reads computer-readable program code stored in a computer-readable medium, so that the processor can execute a function and an action specified in each step or a combination of steps in a flowchart; an apparatus is generated to implement a function and an action specified in each block or a combination of blocks in a block diagram. 
         [0160]    All computer-readable program code may be executed on a user computer, or some may be executed on a user computer as a standalone software package, or some may be executed on a computer of a user while some is executed on a remote computer, or all the code may be executed on a remote computer or a server. It should also be noted that, in some alternative implementation solutions, each step in the flowcharts or functions specified in each block in the block diagrams may not occur in the illustrated order. For example, two consecutive steps or two blocks in the illustration, which are dependent on an involved function, may in fact be executed substantially at the same time, or these blocks may sometimes be executed in reverse order. 
         [0161]    A person of ordinary skill in the art may be aware that, in combination with the examples described in the embodiments disclosed in this specification, units and algorithm steps may be implemented by electronic hardware or a combination of computer software and electronic hardware. Whether the functions are executed by hardware or software depends on particular applications and design constraint conditions of the technical solutions. A person skilled in the art may use different methods to implement the described functions for each particular application, but it should not be considered that the implementation goes beyond the scope of the present invention. 
         [0162]    The foregoing descriptions are merely specific implementation manners of the present invention, but are not intended to limit the protection scope of the present invention. Any variation or replacement readily figured out by a person skilled in the art within the technical scope disclosed in the present invention shall fall within the protection scope of the present invention. Therefore, the protection scope of the present invention shall be subject to the protection scope of the claims.