From 61fef4fb5dc5d5da0f4528dac9f8cd99b6cda4c6 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Fri, 1 Oct 2010 22:36:35 +1000 Subject: [PATCH 1/1] Initial checking of lafs-utils Basic mkfs.lafs works. Signed-off-by: NeilBrown --- .gitignore | 4 + COPYING | 340 +++++++++++++++++++++++++++ Makefile | 4 + include/lafs/endian.h | 47 ++++ include/lafs/lafs.h | 149 ++++++++++++ include/lafs/layout.h | 284 +++++++++++++++++++++++ include/lafs/list.h | 275 ++++++++++++++++++++++ include/lafs/struct.h | 224 ++++++++++++++++++ lib/Makefile | 14 ++ lib/crc32.c | 340 +++++++++++++++++++++++++++ lib/crc32.h | 441 ++++++++++++++++++++++++++++++++++++ lib/interface | 70 ++++++ lib/lafs_add_device.c | 144 ++++++++++++ lib/lafs_add_free_seg.c | 18 ++ lib/lafs_add_inode.c | 22 ++ lib/lafs_alloc.c | 23 ++ lib/lafs_allocated_block.c | 39 ++++ lib/lafs_checkpoint.c | 85 +++++++ lib/lafs_cluster_allocate.c | 49 ++++ lib/lafs_cluster_flush.c | 343 ++++++++++++++++++++++++++++ lib/lafs_cluster_init.c | 78 +++++++ lib/lafs_dblk.c | 41 ++++ lib/lafs_dev_find.c | 14 ++ lib/lafs_dirty_inode.c | 15 ++ lib/lafs_find_dblk.c | 173 ++++++++++++++ lib/lafs_get_itable.c | 43 ++++ lib/lafs_imap_clr.c | 26 +++ lib/lafs_imap_set.c | 36 +++ lib/lafs_import_inode.c | 11 + lib/lafs_import_inode_buf.c | 133 +++++++++++ lib/lafs_incorporate.c | 59 +++++ lib/lafs_inode_fillblock.c | 100 ++++++++ lib/lafs_inode_init.c | 76 +++++++ lib/lafs_load_dblk.c | 35 +++ lib/lafs_make_iblock.c | 29 +++ lib/lafs_new.c | 40 ++++ lib/lafs_new_segment.c | 62 +++++ lib/lafs_read_virtual.c | 24 ++ lib/lafs_sched_blk.c | 28 +++ lib/lafs_segment_count.c | 75 ++++++ lib/lafs_summary_update.c | 55 +++++ lib/lafs_write_dev.c | 61 +++++ lib/lafs_write_state.c | 43 ++++ lib/lafs_write_virtual.c | 24 ++ tools/Makefile | 31 +++ tools/mkfs.lafs.c | 387 +++++++++++++++++++++++++++++++ 46 files changed, 4614 insertions(+) create mode 100644 .gitignore create mode 100644 COPYING create mode 100644 Makefile create mode 100644 include/lafs/endian.h create mode 100644 include/lafs/lafs.h create mode 100644 include/lafs/layout.h create mode 100644 include/lafs/list.h create mode 100644 include/lafs/struct.h create mode 100644 lib/Makefile create mode 100644 lib/crc32.c create mode 100644 lib/crc32.h create mode 100644 lib/interface create mode 100644 lib/lafs_add_device.c create mode 100644 lib/lafs_add_free_seg.c create mode 100644 lib/lafs_add_inode.c create mode 100644 lib/lafs_alloc.c create mode 100644 lib/lafs_allocated_block.c create mode 100644 lib/lafs_checkpoint.c create mode 100644 lib/lafs_cluster_allocate.c create mode 100644 lib/lafs_cluster_flush.c create mode 100644 lib/lafs_cluster_init.c create mode 100644 lib/lafs_dblk.c create mode 100644 lib/lafs_dev_find.c create mode 100644 lib/lafs_dirty_inode.c create mode 100644 lib/lafs_find_dblk.c create mode 100644 lib/lafs_get_itable.c create mode 100644 lib/lafs_imap_clr.c create mode 100644 lib/lafs_imap_set.c create mode 100644 lib/lafs_import_inode.c create mode 100644 lib/lafs_import_inode_buf.c create mode 100644 lib/lafs_incorporate.c create mode 100644 lib/lafs_inode_fillblock.c create mode 100644 lib/lafs_inode_init.c create mode 100644 lib/lafs_load_dblk.c create mode 100644 lib/lafs_make_iblock.c create mode 100644 lib/lafs_new.c create mode 100644 lib/lafs_new_segment.c create mode 100644 lib/lafs_read_virtual.c create mode 100644 lib/lafs_sched_blk.c create mode 100644 lib/lafs_segment_count.c create mode 100644 lib/lafs_summary_update.c create mode 100644 lib/lafs_write_dev.c create mode 100644 lib/lafs_write_state.c create mode 100644 lib/lafs_write_virtual.c create mode 100644 tools/Makefile create mode 100644 tools/mkfs.lafs.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..bffc514 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.o +*.a +mkfs.lafs +core diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..60549be --- /dev/null +++ b/COPYING @@ -0,0 +1,340 @@ + GNU GENERAL PUBLIC LICENSE + Version 2, June 1991 + + Copyright (C) 1989, 1991 Free Software Foundation, Inc. + 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +License is intended to guarantee your freedom to share and change free +software--to make sure the software is free for all its users. This +General Public License applies to most of the Free Software +Foundation's software and to any other program whose authors commit to +using it. (Some other Free Software Foundation software is covered by +the GNU Library General Public License instead.) You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +this service if you wish), that you receive source code or can get it +if you want it, that you can change the software or use pieces of it +in new free programs; and that you know you can do these things. + + To protect your rights, we need to make restrictions that forbid +anyone to deny you these rights or to ask you to surrender the rights. +These restrictions translate to certain responsibilities for you if you +distribute copies of the software, or if you modify it. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must give the recipients all the rights that +you have. You must make sure that they, too, receive or can get the +source code. And you must show them these terms so they know their +rights. + + We protect your rights with two steps: (1) copyright the software, and +(2) offer you this license which gives you legal permission to copy, +distribute and/or modify the software. + + Also, for each author's protection and ours, we want to make certain +that everyone understands that there is no warranty for this free +software. If the software is modified by someone else and passed on, we +want its recipients to know that what they have is not the original, so +that any problems introduced by others will not reflect on the original +authors' reputations. + + Finally, any free program is threatened constantly by software +patents. We wish to avoid the danger that redistributors of a free +program will individually obtain patent licenses, in effect making the +program proprietary. To prevent this, we have made it clear that any +patent must be licensed for everyone's free use or not licensed at all. + + The precise terms and conditions for copying, distribution and +modification follow. + + GNU GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License applies to any program or other work which contains +a notice placed by the copyright holder saying it may be distributed +under the terms of this General Public License. The "Program", below, +refers to any such program or work, and a "work based on the Program" +means either the Program or any derivative work under copyright law: +that is to say, a work containing the Program or a portion of it, +either verbatim or with modifications and/or translated into another +language. (Hereinafter, translation is included without limitation in +the term "modification".) Each licensee is addressed as "you". + +Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running the Program is not restricted, and the output from the Program +is covered only if its contents constitute a work based on the +Program (independent of having been made by running the Program). +Whether that is true depends on what the Program does. + + 1. You may copy and distribute verbatim copies of the Program's +source code as you receive it, in any medium, provided that you +conspicuously and appropriately publish on each copy an appropriate +copyright notice and disclaimer of warranty; keep intact all the +notices that refer to this License and to the absence of any warranty; +and give any other recipients of the Program a copy of this License +along with the Program. + +You may charge a fee for the physical act of transferring a copy, and +you may at your option offer warranty protection in exchange for a fee. + + 2. You may modify your copy or copies of the Program or any portion +of it, thus forming a work based on the Program, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) You must cause the modified files to carry prominent notices + stating that you changed the files and the date of any change. + + b) You must cause any work that you distribute or publish, that in + whole or in part contains or is derived from the Program or any + part thereof, to be licensed as a whole at no charge to all third + parties under the terms of this License. + + c) If the modified program normally reads commands interactively + when run, you must cause it, when started running for such + interactive use in the most ordinary way, to print or display an + announcement including an appropriate copyright notice and a + notice that there is no warranty (or else, saying that you provide + a warranty) and that users may redistribute the program under + these conditions, and telling the user how to view a copy of this + License. (Exception: if the Program itself is interactive but + does not normally print such an announcement, your work based on + the Program is not required to print an announcement.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Program, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Program, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Program. + +In addition, mere aggregation of another work not based on the Program +with the Program (or with a work based on the Program) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may copy and distribute the Program (or a work based on it, +under Section 2) in object code or executable form under the terms of +Sections 1 and 2 above provided that you also do one of the following: + + a) Accompany it with the complete corresponding machine-readable + source code, which must be distributed under the terms of Sections + 1 and 2 above on a medium customarily used for software interchange; or, + + b) Accompany it with a written offer, valid for at least three + years, to give any third party, for a charge no more than your + cost of physically performing source distribution, a complete + machine-readable copy of the corresponding source code, to be + distributed under the terms of Sections 1 and 2 above on a medium + customarily used for software interchange; or, + + c) Accompany it with the information you received as to the offer + to distribute corresponding source code. (This alternative is + allowed only for noncommercial distribution and only if you + received the program in object code or executable form with such + an offer, in accord with Subsection b above.) + +The source code for a work means the preferred form of the work for +making modifications to it. For an executable work, complete source +code means all the source code for all modules it contains, plus any +associated interface definition files, plus the scripts used to +control compilation and installation of the executable. However, as a +special exception, the source code distributed need not include +anything that is normally distributed (in either source or binary +form) with the major components (compiler, kernel, and so on) of the +operating system on which the executable runs, unless that component +itself accompanies the executable. + +If distribution of executable or object code is made by offering +access to copy from a designated place, then offering equivalent +access to copy the source code from the same place counts as +distribution of the source code, even though third parties are not +compelled to copy the source along with the object code. + + 4. You may not copy, modify, sublicense, or distribute the Program +except as expressly provided under this License. Any attempt +otherwise to copy, modify, sublicense or distribute the Program is +void, and will automatically terminate your rights under this License. +However, parties who have received copies, or rights, from you under +this License will not have their licenses terminated so long as such +parties remain in full compliance. + + 5. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Program or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Program (or any work based on the +Program), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Program or works based on it. + + 6. Each time you redistribute the Program (or any work based on the +Program), the recipient automatically receives a license from the +original licensor to copy, distribute or modify the Program subject to +these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties to +this License. + + 7. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Program at all. For example, if a patent +license would not permit royalty-free redistribution of the Program by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Program. + +If any portion of this section is held invalid or unenforceable under +any particular circumstance, the balance of the section is intended to +apply and the section as a whole is intended to apply in other +circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system, which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 8. If the distribution and/or use of the Program is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Program under this License +may add an explicit geographical distribution limitation excluding +those countries, so that distribution is permitted only in or among +countries not thus excluded. In such case, this License incorporates +the limitation as if written in the body of this License. + + 9. The Free Software Foundation may publish revised and/or new versions +of the General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + +Each version is given a distinguishing version number. If the Program +specifies a version number of this License which applies to it and "any +later version", you have the option of following the terms and conditions +either of that version or of any later version published by the Free +Software Foundation. If the Program does not specify a version number of +this License, you may choose any version ever published by the Free Software +Foundation. + + 10. If you wish to incorporate parts of the Program into other free +programs whose distribution conditions are different, write to the author +to ask for permission. For software which is copyrighted by the Free +Software Foundation, write to the Free Software Foundation; we sometimes +make exceptions for this. Our decision will be guided by the two goals +of preserving the free status of all derivatives of our free software and +of promoting the sharing and reuse of software generally. + + NO WARRANTY + + 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY +FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN +OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES +PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED +OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS +TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE +PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, +REPAIR OR CORRECTION. + + 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR +REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, +INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING +OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED +TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY +YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER +PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE +POSSIBILITY OF SUCH DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) 19yy + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + + +Also add information on how to contact you by electronic and paper mail. + +If the program is interactive, make it output a short notice like this +when it starts in an interactive mode: + + Gnomovision version 69, Copyright (C) 19yy name of author + Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, the commands you use may +be called something other than `show w' and `show c'; they could even be +mouse-clicks or menu items--whatever suits your program. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the program, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the program + `Gnomovision' (which makes passes at compilers) written by James Hacker. + + , 1 April 1989 + Ty Coon, President of Vice + +This General Public License does not permit incorporating your program into +proprietary programs. If your program is a subroutine library, you may +consider it more useful to permit linking proprietary applications with the +library. If this is what you want to do, use the GNU Library General +Public License instead of this License. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..8e0ae1f --- /dev/null +++ b/Makefile @@ -0,0 +1,4 @@ + +all: + $(MAKE) -C lib/ + $(MAKE) -C tools/ \ No newline at end of file diff --git a/include/lafs/endian.h b/include/lafs/endian.h new file mode 100644 index 0000000..e750e0d --- /dev/null +++ b/include/lafs/endian.h @@ -0,0 +1,47 @@ +#include +#define bswap_16(x) (((x) & 0x00ffU) << 8 | \ + ((x) & 0xff00U) >> 8) +#define bswap_32(x) (((x) & 0x000000ffU) << 24 | \ + ((x) & 0xff000000U) >> 24 | \ + ((x) & 0x0000ff00U) << 8 | \ + ((x) & 0x00ff0000U) >> 8) +#define bswap_64(x) (((x) & 0x00000000000000ffULL) << 56 | \ + ((x) & 0xff00000000000000ULL) >> 56 | \ + ((x) & 0x000000000000ff00ULL) << 40 | \ + ((x) & 0x00ff000000000000ULL) >> 40 | \ + ((x) & 0x0000000000ff0000ULL) << 24 | \ + ((x) & 0x0000ff0000000000ULL) >> 24 | \ + ((x) & 0x00000000ff000000ULL) << 8 | \ + ((x) & 0x000000ff00000000ULL) >> 8) + +#if BYTE_ORDER == LITTLE_ENDIAN +#define __cpu_to_le16(_x) (_x) +#define __cpu_to_le32(_x) (_x) +#define __cpu_to_le64(_x) (_x) +#define __le16_to_cpu(_x) (_x) +#define __le32_to_cpu(_x) (_x) +#define __le64_to_cpu(_x) (_x) + +#define __cpu_to_be16(_x) bswap_16(_x) +#define __cpu_to_be32(_x) bswap_32(_x) +#define __cpu_to_be64(_x) bswap_64(_x) +#define __be16_to_cpu(_x) bswap_16(_x) +#define __be32_to_cpu(_x) bswap_32(_x) +#define __be64_to_cpu(_x) bswap_64(_x) +#elif BYTE_ORDER == BIG_ENDIAN +#define __cpu_to_le16(_x) bswap_16(_x) +#define __cpu_to_le32(_x) bswap_32(_x) +#define __cpu_to_le64(_x) bswap_64(_x) +#define __le16_to_cpu(_x) bswap_16(_x) +#define __le32_to_cpu(_x) bswap_32(_x) +#define __le64_to_cpu(_x) bswap_64(_x) + +#define __cpu_to_be16(_x) (_x) +#define __cpu_to_be32(_x) (_x) +#define __cpu_to_be64(_x) (_x) +#define __be16_to_cpu(_x) (_x) +#define __be32_to_cpu(_x) (_x) +#define __be64_to_cpu(_x) (_x) +#else +# error "unknown endianness." +#endif diff --git a/include/lafs/lafs.h b/include/lafs/lafs.h new file mode 100644 index 0000000..3518523 --- /dev/null +++ b/include/lafs/lafs.h @@ -0,0 +1,149 @@ + +#include +#include + +#define u8 uint8_t +#define u16 uint16_t +#define u32 uint32_t +#define u64 uint64_t +#include +#include +#include + + +struct lafs *lafs_alloc(void); +int lafs_new(struct lafs *, int block_bytes); + +struct lafs_device *lafs_add_device(struct lafs *, char *devname, int fd, + loff_t segblocks, loff_t strideblock, + int width, int usage_inum); + +struct lafs_ino *lafs_get_itable(struct lafs *); +struct lafs_ino *lafs_add_inode(struct lafs_ino*, int inum, int type); +int lafs_imap_set(struct lafs_ino *, int inum); + +int lafs_add_free_seg(struct lafs*, int dev, loff_t seg); + +void lafs_inode_init(struct lafs *, char *, int type); +struct lafs_ino *lafs_import_inode_buf(struct lafs *fs, + char *buf, int inum, + struct lafs_ino *parent); +void lafs_dirty_inode(struct lafs_ino *); +void lafs_inode_fillblock(struct lafs_ino *ino, char *buf); +void lafs_make_iblock(struct lafs_ino *ino); + +struct lafs_dblk *lafs_dblk(struct lafs_ino *ino, + loff_t bnum); +int lafs_load_dblk(struct lafs_dblk *); +int lafs_find_dblk(struct lafs_dblk *); +struct lafs_ino *lafs_import_inode(struct lafs_dblk *db); + +int lafs_read_virtual(struct lafs *, char *, loff_t); +int lafs_sched_blk(struct lafs_blk *); + +int lafs_write_dev(struct lafs_device *dev); +int lafs_write_state(struct lafs *fs); +int lafs_checkpoint(struct lafs *fs); +void lafs_incorporate(struct lafs_iblk *ib); +void lafs_cluster_allocate(struct lafs_blk *b, int cnum); +void lafs_cluster_flush(struct lafs *fs, int cnum); +int lafs_new_segment(struct lafs *, int cnum); +struct lafs_device *lafs_dev_find(struct lafs *fs, loff_t virt); +void lafs_allocated_block(struct lafs_blk *b, loff_t addr); +void lafs_cluster_init(struct lafs *fs, int cnum, + loff_t addr, loff_t prev, loff_t seq); +void lafs_summary_update(struct lafs_ino *ino, + loff_t oldaddr, loff_t newaddr, + int is_index); +void lafs_segment_count(struct lafs *fs, loff_t addr, int diff); + +unsigned long crc32( + unsigned long crc, + const uint32_t *buf, + unsigned len); + +static inline struct lafs_dblk *dblk(struct lafs_blk *b) +{ + return container_of(b, struct lafs_dblk, b); +} + +static inline struct lafs_iblk *iblk(struct lafs_blk *b) +{ + return container_of(b, struct lafs_iblk, b); +} + +static inline int lafs_dirty_blk(struct lafs_blk *blk) +{ + blk->flags |= B_Dirty; + return lafs_sched_blk(blk); +} + +static inline uint64_t lafs_encode_timeval(struct timeval *tm) +{ + uint64_t nano = tm->tv_usec * 1000; + uint64_t sec = tm->tv_sec & (0x7FFFFFFFFULL); + nano &= ~1ULL; + return sec | (nano << 34); +} + +static inline struct lafs_device *dev_by_num(struct lafs *fs, int num) +{ + struct lafs_device *dv; + for (dv = fs->devs ; dv ; dv = dv->next) + if (dv->devnum == num) + return dv; + return NULL; +} + + +static inline void +virttoseg(struct lafs *fs, loff_t virt, int *devp, loff_t *segp, loff_t *offsetp) +{ + struct lafs_device *dv = lafs_dev_find(fs, virt); + + virt -= dv->start; + if (dv->segment_size >= dv->width * dv->stride) { + *offsetp = virt % dv->segment_size; + *segp = virt / dv->segment_size; + } else { + int of = virt % dv->stride; + int strp =virt / (dv->width * dv->stride); + + *segp = (strp * dv->stride + of) / + (dv->segment_size / dv->width); + *offsetp = virt - dv->segment_stride * *segp; + } + *devp = dv->devnum; +} + +static inline void +virttophys(struct lafs *fs, loff_t virt, int *devp, loff_t *sectp) +{ + struct lafs_device *dv = lafs_dev_find(fs, virt); + + if (dv == NULL) + return; + *devp = dv->devnum; + + virt -= dv->start; + virt *= fs->blocksize; + virt += dv->segment_offset; + *sectp = virt; +} + +#include +static inline void +de_sched(struct lafs_blk *b) +{ + if (!(b->flags & B_Sched)) + return; + b->flags &= ~B_Sched; + + if (b->parent) { + struct lafs_iblk *p = b->parent; + printf("ds %d/%ld %d\n", p->b.ino->inum, p->b.fileaddr, p->sched_cnt); + p->sched_cnt--; + if (p->sched_cnt == 0) + lafs_sched_blk(&p->b); + } +} diff --git a/include/lafs/layout.h b/include/lafs/layout.h new file mode 100644 index 0000000..c1e5ea2 --- /dev/null +++ b/include/lafs/layout.h @@ -0,0 +1,284 @@ +/* + * fs/lafs/layout.h + * Copyright (C) 2005-2009 + * Neil Brown + * Released under the GPL, version 2 + * + * layout of device superblock + * and array state block + */ + +/* All multibyte numerical values are in little-endian order + */ + +/* The "superblock" describes a particular device in the filesystem. + * different devices have different superblocks. + */ +struct lafs_dev { + char idtag[16]; /* LaFS-DeviceBlock */ + char version[16]; /* number space options */ + u8 uuid[16]; + u32 checksum; + u32 seq; + + u64 ctime; + u64 start, size; /* in array address space (array block)*/ + u64 devaddr[2]; /* (device byte) one at each "end" */ + u64 stateaddr[4]; /* (device byte) 4 state blocks, two at each end */ + + u8 statebits; /* log of size of stateblock - normally 10-14 */ + u8 blockbits; /* bits in fs block (byte)- 9 - 16 */ + u16 width; /* devices in array - 1 to a few hundred */ + u32 stride; /* Blocks in a stride - 1 to a very large number */ + u32 segment_size; /* blocks in a segment (block) */ + u32 segment_offset; /* offset of first segment (device byte) */ + u32 segment_count; + u32 usage_inum; /* inum of segment usage file */ + u32 level; +} __attribute__((packed)); +#define LAFS_DEVBLK_SIZE 1024 + +struct lafs_state { + char idtag[16]; /* LaFS-State-Block */ + char version[16]; /* number space options */ + u32 checksum; + u32 seq; + u8 uuid[16]; + u32 levels; + u32 devices; + u32 nonlog_segment; /* segment number and */ + u16 nonlog_dev; /* device number of active non-logged segment */ + u16 nonlog_offset; /* offset into above segment of next non-logged + * block to allocate + */ + u32 maxsnapshot; + u16 nextyouth; + u16 pad0; + + u64 checkpointcluster; /* (array block) */ + u64 root_inodes[0]; /* (array block) */ +} __attribute__((packed)); + +struct descriptor { + u32 block_num; /* (file block) */ + u16 block_cnt; /* int */ + u16 block_bytes; /* 0..blocksize - 0 means 'punch a hole', + * 1..blocksize means advance EOF to there + */ +} __attribute__((packed)); +#define DescHole 0 +#define DescIndex 0xffff +#define DescMiniOffset 0x8000 + +#define ROUND_UP(x) (((x)+3)&~3) + +struct miniblock { + u32 block_num; /* (file block) */ + u16 block_offset; /* (byte) */ + u16 length; /* (bytes) + 0x8000 */ + u8 data[0]; +} __attribute__((packed)); + +struct group_head { + u32 inum; + u32 fsnum; + u16 truncatenum_and_flag; + u16 group_size_words; /* 4byte words */ + union { + struct descriptor desc[0]; + struct miniblock mb[0]; + } u; +} __attribute__((packed)); + +struct cluster_head { + char idtag[8]; /* LaFSHead */ + u8 uuid[16]; + u64 seq; + u32 flags; + u16 Hlength; /* header length - (bytes) */ + u16 Clength; /* cluster length including header - (blocks) */ + u32 checksum; /* over Hlength bytes */ + u16 verify_type; + u16 pad0; + u8 verify_data[16]; + u64 next_addr; /* (Array block) */ + u64 this_addr; /* (array block) */ + u64 prev_addr; /* (array block) */ + struct group_head groups[0]; +} __attribute__((packed)); + +#define CH_Checkpoint 1 +#define CH_CheckpointStart 2 +#define CH_CheckpointEnd 4 + +/* values for verify_type */ +#define VerifyNull 0 /* if you found me, I'm valid */ +#define VerifyNext 1 /* if next head is valid, this cluster is */ +#define VerifyNext2 2 /* if next 2 heads are valid, this cluster is */ +#define VerifySum 3 /* maybe some sort of MIC is in _data */ + +struct la_inode { + /* 16 bytes is constant */ + u32 data_blocks; /* (blocks) */ + u32 index_blocks; /* (blocks) */ + u16 generation; + u16 metadata_size; /* (bytes) */ + u8 depth; + u8 trunc_gen; + u8 filetype; + u8 flags; +#define File_nonlogged 1 + union { + struct fs_metadata { + /* 116 bytes */ + u64 update_time; + u64 blocks_used; /* data+index */ + u64 blocks_allowed; + u64 creation_age; + u32 inodes_used; + u32 quota_inodes[3]; + u16 snapshot_usage_table; + u16 pad; + char name[64]; + } fs; + struct inodemap_metadata { + u32 size; + } inodemap; + struct su_metadata { + u32 table_size; /* (blocks) */ + } segmentusage; + struct file_metadata { + u16 flags; + u16 mode; + u32 userid; + u32 groupid; + u32 treeid; + u64 creationtime; + u64 modifytime; + u64 ctime; + u64 accesstime; + u64 size; + u32 parent; + u32 linkcount; + u32 attrinode; + u32 attributes[0]; + } __attribute__((packed)) file; + struct dir_metadata { + struct file_metadata h; + u32 hash_seed; + u32 attributes[0]; + } dir; + struct special_metadata { + struct file_metadata h; + u32 major; + u32 minor; + u32 attributes[0]; + } special; + struct quota_metadata { + u32 gracetime; /* typically '7' */ + u32 graceunits; /* typically 24*60*60 */ + } quota; + } metadata[0]; +} __attribute__((packed)); + +#define LAFS_INODE_LOG_START offsetof(struct la_inode, metadata[0].file.flags) +#define LAFS_INODE_LOG_END offsetof(struct la_inode, metadata[0].file.size) +#define LAFS_INODE_LOG_SIZE (LAFS_INODE_LOG_END - LAFS_INODE_LOG_START) + +#define TypeInodeFile 1 +#define TypeInodeMap 2 +#define TypeSegmentMap 3 +#define TypeQuota 4 +#define TypeOrphanList 5 +#define TypeAccessTime 6 + +#define TypeBase 16 + +#define TypeFile 16 +#define TypeDir 17 +#define TypeSymlink 18 +#define TypeSpecial 19 /* char or block, or pipe or socket */ +#define TypeAttr 20 + +#define LAFS_MAX_LINKS ((1UL<<31)-1) + +struct index { + u32 logical; + u32 phys_lo; + u16 phys_hi; +} __attribute__((packed)); + +struct extent { + u32 phys_lo; + u16 phys_hi; + u16 size; + u32 logical; +} __attribute__((packed)); +#define IBLK_INDEX (0) +#define IBLK_INDIRECT (1) +#define IBLK_EXTENT (2) + +#define MaxDirHash 0x7fffffffUL +struct dirpiece { + u32 target; /* inode number */ + u8 next[2]; /* back, fore */ + u8 type:4, chain_info:2, longer:2; +/* 'longer' is 3 if fore and back the same length + * 0 if back (next[0]) is longer + * 1 if fore (next[1]) is longer + * 'chain_info' is + * 0,1: add that number to the hash of filename + * 2 : add one trailing byte to hash + * 3 : add 4 trailing bytes (little-endian) to hash + */ +#define Neither 3 + u8 length; + char name[0]; +} __attribute__((packed)); + +#define NoBlock (0xFFFFFFFF) +struct dirheader { + u8 root; + u8 lastpiece; + u8 freepieces; + u8 pad; +} __attribute__((packed)); + +/* + * Miniblock for directory updates record the operation type + * in the block_offset + */ +#define DIROP_LINK 0 +#define DIROP_UNLINK 1 +#define DIROP_REN_SOURCE 2 +#define DIROP_REN_NEW_TARGET 3 +#define DIROP_REN_OLD_TARGET 4 + +/* + * The orphan file has a very simple structure with + * 16 byte records identifying blocks in files that + * might be orphans + */ +struct orphan { + u32 type; /* 0 if free */ + u32 filesys; + u32 inum; + u32 addr; +} __attribute__((packed)); + +/* Youth values are decayed when nextyouth gets too big */ +static int inline decay_youth(int y) +{ + if (y < 8) + return y; + if (y < 32768+8) + y = (y-8)/2 + 8; + else + y -= 16384; + return y; +} +/* This is only called on large youth values */ +static int inline decay_undo(int y) +{ + return y + 16384; +} diff --git a/include/lafs/list.h b/include/lafs/list.h new file mode 100644 index 0000000..690629f --- /dev/null +++ b/include/lafs/list.h @@ -0,0 +1,275 @@ + +/* fromlinux-2.5.43 but with prefetch removed + and container_of added +*/ +#ifndef _LINUX_LIST_H +#define _LINUX_LIST_H +/** + * container_of - cast a member of a structure out to the containing structure + * + * @ptr: the pointer to the member. + * @type: the type of the container struct this is embedded in. + * @member: the name of the member within the struct. + * + */ +#ifndef offsetof +# define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) +#endif +#define container_of(ptr, type, member) ({ \ + const typeof( ((type *)0)->member ) *__mptr = (ptr); \ + (type *)( (char *)__mptr - offsetof(type,member) );}) + + +/* + * Simple doubly linked list implementation. + * + * Some of the internal functions ("__xxx") are useful when + * manipulating whole lists rather than single entries, as + * sometimes we already know the next/prev entries and we can + * generate better code by using them directly rather than + * using the generic single-entry routines. + */ + +struct list_head { + struct list_head *next, *prev; +}; + +#define LIST_HEAD_INIT(name) { &(name), &(name) } + +#define LIST_HEAD(name) \ + struct list_head name = LIST_HEAD_INIT(name) + +#define INIT_LIST_HEAD(ptr) do { \ + (ptr)->next = (ptr); (ptr)->prev = (ptr); \ +} while (0) + +/* + * Insert a new entry between two known consecutive entries. + * + * This is only for internal list manipulation where we know + * the prev/next entries already! + */ +static inline void __list_add(struct list_head *new, + struct list_head *prev, + struct list_head *next) +{ + next->prev = new; + new->next = next; + new->prev = prev; + prev->next = new; +} + +/** + * list_add - add a new entry + * @new: new entry to be added + * @head: list head to add it after + * + * Insert a new entry after the specified head. + * This is good for implementing stacks. + */ +static inline void list_add(struct list_head *new, struct list_head *head) +{ + __list_add(new, head, head->next); +} + +/** + * list_add_tail - add a new entry + * @new: new entry to be added + * @head: list head to add it before + * + * Insert a new entry before the specified head. + * This is useful for implementing queues. + */ +static inline void list_add_tail(struct list_head *new, struct list_head *head) +{ + __list_add(new, head->prev, head); +} + +/* + * Delete a list entry by making the prev/next entries + * point to each other. + * + * This is only for internal list manipulation where we know + * the prev/next entries already! + */ +static inline void __list_del(struct list_head * prev, struct list_head * next) +{ + next->prev = prev; + prev->next = next; +} + +/** + * list_del - deletes entry from list. + * @entry: the element to delete from the list. + * Note: list_empty on entry does not return true after this, the entry is + * in an undefined state. + */ +static inline void list_del(struct list_head *entry) +{ + __list_del(entry->prev, entry->next); +} + +/** + * list_del_init - deletes entry from list and reinitialize it. + * @entry: the element to delete from the list. + */ +static inline void list_del_init(struct list_head *entry) +{ + __list_del(entry->prev, entry->next); + INIT_LIST_HEAD(entry); +} + +/** + * list_move - delete from one list and add as another's head + * @list: the entry to move + * @head: the head that will precede our entry + */ +static inline void list_move(struct list_head *list, struct list_head *head) +{ + __list_del(list->prev, list->next); + list_add(list, head); +} + +/** + * list_move_tail - delete from one list and add as another's tail + * @list: the entry to move + * @head: the head that will follow our entry + */ +static inline void list_move_tail(struct list_head *list, + struct list_head *head) +{ + __list_del(list->prev, list->next); + list_add_tail(list, head); +} + +/** + * list_empty - tests whether a list is empty + * @head: the list to test. + */ +static inline int list_empty(struct list_head *head) +{ + return head->next == head; +} + +static inline void __list_splice(struct list_head *list, + struct list_head *head) +{ + struct list_head *first = list->next; + struct list_head *last = list->prev; + struct list_head *at = head->next; + + first->prev = head; + head->next = first; + + last->next = at; + at->prev = last; +} + +/** + * list_splice - join two lists + * @list: the new list to add. + * @head: the place to add it in the first list. + */ +static inline void list_splice(struct list_head *list, struct list_head *head) +{ + if (!list_empty(list)) + __list_splice(list, head); +} + +/** + * list_splice_init - join two lists and reinitialise the emptied list. + * @list: the new list to add. + * @head: the place to add it in the first list. + * + * The list at @list is reinitialised + */ +static inline void list_splice_init(struct list_head *list, + struct list_head *head) +{ + if (!list_empty(list)) { + __list_splice(list, head); + INIT_LIST_HEAD(list); + } +} + +/** + * list_entry - get the struct for this entry + * @ptr: the &struct list_head pointer. + * @type: the type of the struct this is embedded in. + * @member: the name of the list_struct within the struct. + */ +#define list_entry(ptr, type, member) \ + container_of(ptr, type, member) + +/** + * list_first_entry - get the first element from a list + * @ptr: the list head to take the element from. + * @type: the type of the struct this is embedded in. + * @member: the name of the list_struct within the struct. + * + * Note, that list is expected to be not empty. + */ +#define list_first_entry(ptr, type, member) \ + list_entry((ptr)->next, type, member) + +/** + * list_for_each - iterate over a list + * @pos: the &struct list_head to use as a loop counter. + * @head: the head for your list. + */ +#define list_for_each(pos, head) \ + for (pos = (head)->next; pos != (head); \ + pos = pos->next) + +/** + * __list_for_each - iterate over a list + * @pos: the &struct list_head to use as a loop counter. + * @head: the head for your list. + * + * This variant differs from list_for_each() in that it's the + * simplest possible list iteration code, no prefetching is done. + * Use this for code that knows the list to be very short (empty + * or 1 entry) most of the time. + */ +#define __list_for_each(pos, head) \ + for (pos = (head)->next; pos != (head); pos = pos->next) + +/** + * list_for_each_prev - iterate over a list backwards + * @pos: the &struct list_head to use as a loop counter. + * @head: the head for your list. + */ +#define list_for_each_prev(pos, head) \ + for (pos = (head)->prev; pos != (head); \ + pos = pos->prev) + +/** + * list_for_each_safe - iterate over a list safe against removal of list entry + * @pos: the &struct list_head to use as a loop counter. + * @n: another &struct list_head to use as temporary storage + * @head: the head for your list. + */ +#define list_for_each_safe(pos, n, head) \ + for (pos = (head)->next, n = pos->next; pos != (head); \ + pos = n, n = pos->next) + +/** + * list_for_each_entry - iterate over list of given type + * @pos: the type * to use as a loop counter. + * @head: the head for your list. + * @member: the name of the list_struct within the struct. + */ +#define list_for_each_entry(pos, head, member) \ + for (pos = list_entry((head)->next, typeof(*pos), member); \ + &pos->member != (head); \ + pos = list_entry(pos->member.next, typeof(*pos), member) \ + ) + +#define list_for_each_entry_safe(pos, n, head, member) \ + for (pos = list_entry((head)->next, typeof(*pos), member), \ + n = pos->member.next; \ + &pos->member != (head); \ + pos = list_entry(n, typeof(*pos), member), \ + n= pos->member.next \ + ) +#endif diff --git a/include/lafs/struct.h b/include/lafs/struct.h new file mode 100644 index 0000000..8968d42 --- /dev/null +++ b/include/lafs/struct.h @@ -0,0 +1,224 @@ +#include +#include +#include + +#define WC_NUM 2 + +#define HASH_BITS 8 + +struct lafs { + char version[16]; + uint8_t uuid[16]; + uint32_t seq; + int devices; + int blocksize; + int statesize; + int max_segment; + + int flags; +#define LAFS_DELAY_UPDATES 1 + + int checkpointing; +#define CHECKPOINTING 1 +#define CHECKPOINT_START 2 +#define CHECKPOINT_END 4 + loff_t checkpoint_cluster; + + struct lafs_snapshot { + struct lafs_snapshot *next; + loff_t root_addr; + struct lafs_ino *root; + struct lafs_ino *rootdir; + } ss; + + struct lafs_device *devs; + int loaded_devs; + + struct lafs_cluster { + struct list_head blocks; + loff_t prev_addr; + uint64_t seq; + int remaining; + + char *chead; + int chead_size; + int chead_blocks; + + struct lafs_segpos { + int dev; + loff_t num; + + uint32_t st_table, st_row; + uint32_t nxt_table, nxt_row; + uint32_t table, row, col; + + } seg; + } wc[WC_NUM]; + + struct list_head leafs, account_leafs; + + int youth_next; + + struct list_head htable[1 << HASH_BITS]; + + /* free segment list + * If free_head == free_tail, the list is empty + * Else free_head is the first to use and free_tail + * is the next to fill. + */ + int free_head, free_tail; + struct { + int dev; + loff_t seg; + } freelist[128]; + + struct lafs_delayed { + struct lafs_delayed *next; + int dev; + loff_t seg; + int diff; + } *delayed; +}; + + +struct lafs_ino { + struct lafs *fs; + struct lafs_dblk *dblock; + struct lafs_iblk *iblock; + int inum; + struct lafs_ino *filesys; + + u32 cblocks, /* data blocks which are commited to this file */ + ablocks, /* data blocks that are allocated */ + + ciblocks; /* index blocks committed */ + + int iflags; +#define I_Phase1 1 /* set to 'this' phase when iblocks correct */ +#define I_Dirty 2 +/* next three indicate if we hold a reference on the relevant qent */ +#define I_QUid 8 +#define I_QGid 16 +#define I_QTid 32 + + int generation; + int trunc_gen; + int flags; + + int type; + int metadata_size; + int depth; + + union { + struct fs_md { + int usagetable; + uint64_t update_time; + uint64_t cblocks_used; /* blocks commited */ + uint64_t ablocks_used; /* extra blocks allocated */ + uint64_t blocks_allowed; + uint64_t blocks_unalloc; + uint64_t creation_age; + uint32_t inodes_used; + uint32_t quota_inums[3]; + struct inode *quota_inodes[3]; + char name[65]; + } fs; + struct inodemap_md { + uint32_t size; + } inodemap; + struct su_md { + uint32_t table_size; /* (blocks) */ + } segmentusage; + struct file_md { + uint16_t flags; + uint16_t mode; + uint32_t uid; + uint32_t gid; + uint32_t treeid; + uint64_t creationtime; + uint64_t modifytime; + uint64_t ctime; + uint64_t accesstime; + uint64_t i_accesstime; /* as stored in inode */ + uint64_t size; + uint32_t parent; + uint32_t linkcount; + /* for directories */ + uint32_t seed; + /* for special */ + uint32_t major, minor; + /* lists of blocks which need orphan-treatment */ + struct list_head dirorphan; + } file; + struct orphan_md { + uint32_t nextfree; + uint32_t reserved; + } orphan; + struct quota_md { + uint32_t gracetime; /* multiplier for graceunits */ + uint32_t graceunits; /* seconds per unit */ + } quota; + } md; +}; + +struct lafs_device { + struct lafs_device *next; + struct lafs *fs; + int fd; + int seq; + loff_t start, size; + loff_t devaddr[2]; + loff_t stateaddr[4]; + int devnum; + char *name; + + struct timeval ctime; + + int width; + loff_t stride, segment_size; + loff_t segment_offset, segment_stride, segment_count; + int usage_inum; + + int rows_per_table, tables_per_seg; + int tablesize; + + int recent_super, recent_state; + + struct lafs_ino *segsum; +}; + +struct lafs_blk { + char *data; + int flags; + loff_t fileaddr; + loff_t physaddr; + + struct lafs_ino *ino; + struct list_head hash; + + struct lafs_iblk *parent; + struct list_head siblings; + struct list_head leafs; + + struct lafs_blk *chain; +}; + +struct lafs_dblk { + struct lafs_blk b; + struct lafs_ino *my_inode; +}; + +struct lafs_iblk { + struct lafs_blk b; + int depth; + struct list_head children; + struct lafs_blk *uninc; + int sched_cnt; +}; + +#define B_Valid 1 +#define B_Dirty 2 +#define B_Index 4 +#define B_Sched 8 +#define B_InoIdx 16 + diff --git a/lib/Makefile b/lib/Makefile new file mode 100644 index 0000000..3c5585d --- /dev/null +++ b/lib/Makefile @@ -0,0 +1,14 @@ + + +SRC = $(wildcard *.c) +OBJ = $(patsubst %.c,%.o,$(SRC)) +INCL = $(wildcard ../include/lafs/*.h) + +CPPFLAGS = -I../include +CFLAGS = -Wall -Werror -g + +liblafs.a : $(OBJ) + ar cr liblafs.a $(OBJ) + ranlib liblafs.a + +$(OBJ): %.o : %.c $(INCL) diff --git a/lib/crc32.c b/lib/crc32.c new file mode 100644 index 0000000..12d08e5 --- /dev/null +++ b/lib/crc32.c @@ -0,0 +1,340 @@ +/* crc32.c -- compute the CRC-32 of a data stream + * Copyright (C) 1995-2003 Mark Adler + * For conditions of distribution and use, see copyright notice in zlib.h + * + * Thanks to Rodney Brown for his contribution of faster + * CRC methods: exclusive-oring 32 bits of data at a time, and pre-computing + * tables for updating the shift register in one step with three exclusive-ors + * instead of four steps with four exclusive-ors. This results about a factor + * of two increase in speed on a Power PC G4 (PPC7455) using gcc -O3. + */ + +/* @(#) $Id$ */ + +/* + Note on the use of DYNAMIC_CRC_TABLE: there is no mutex or semaphore + protection on the static variables used to control the first-use generation + of the crc tables. Therefore, if you #define DYNAMIC_CRC_TABLE, you should + first call get_crc_table() to initialize the tables before allowing more than + one thread to use crc32(). + */ + +#ifdef MAKECRCH +# include +# ifndef DYNAMIC_CRC_TABLE +# define DYNAMIC_CRC_TABLE +# endif /* !DYNAMIC_CRC_TABLE */ +#endif /* MAKECRCH */ + +/* #include "zutil.h" / * for STDC and FAR definitions */ +#define STDC +#define FAR +#define Z_NULL ((void*)0) +#define OF(X) X +#define ZEXPORT +typedef long ptrdiff_t; +#define NOBYFOUR + +#define local static + +/* Find a four-byte integer type for crc32_little() and crc32_big(). */ +#ifndef NOBYFOUR +# ifdef STDC /* need ANSI C limits.h to determine sizes */ +# include +# define BYFOUR +# if (UINT_MAX == 0xffffffffUL) + typedef unsigned int u4; +# else +# if (ULONG_MAX == 0xffffffffUL) + typedef unsigned long u4; +# else +# if (USHRT_MAX == 0xffffffffUL) + typedef unsigned short u4; +# else +# undef BYFOUR /* can't find a four-byte integer type! */ +# endif +# endif +# endif +# endif /* STDC */ +#endif /* !NOBYFOUR */ + +/* Definitions for doing the crc four data bytes at a time. */ +#ifdef BYFOUR +# define REV(w) (((w)>>24)+(((w)>>8)&0xff00)+ \ + (((w)&0xff00)<<8)+(((w)&0xff)<<24)) + local unsigned long crc32_little OF((unsigned long, + const unsigned char FAR *, unsigned)); + local unsigned long crc32_big OF((unsigned long, + const unsigned char FAR *, unsigned)); +# define TBLS 8 +#else +# define TBLS 1 +#endif /* BYFOUR */ + +#ifdef DYNAMIC_CRC_TABLE + +local volatile int crc_table_empty = 1; +local unsigned long FAR crc_table[TBLS][256]; +local void make_crc_table OF((void)); +#ifdef MAKECRCH + local void write_table OF((FILE *, const unsigned long FAR *)); +#endif /* MAKECRCH */ + +/* + Generate tables for a byte-wise 32-bit CRC calculation on the polynomial: + x^32+x^26+x^23+x^22+x^16+x^12+x^11+x^10+x^8+x^7+x^5+x^4+x^2+x+1. + + Polynomials over GF(2) are represented in binary, one bit per coefficient, + with the lowest powers in the most significant bit. Then adding polynomials + is just exclusive-or, and multiplying a polynomial by x is a right shift by + one. If we call the above polynomial p, and represent a byte as the + polynomial q, also with the lowest power in the most significant bit (so the + byte 0xb1 is the polynomial x^7+x^3+x+1), then the CRC is (q*x^32) mod p, + where a mod b means the remainder after dividing a by b. + + This calculation is done using the shift-register method of multiplying and + taking the remainder. The register is initialized to zero, and for each + incoming bit, x^32 is added mod p to the register if the bit is a one (where + x^32 mod p is p+x^32 = x^26+...+1), and the register is multiplied mod p by + x (which is shifting right by one and adding x^32 mod p if the bit shifted + out is a one). We start with the highest power (least significant bit) of + q and repeat for all eight bits of q. + + The first table is simply the CRC of all possible eight bit values. This is + all the information needed to generate CRCs on data a byte at a time for all + combinations of CRC register values and incoming bytes. The remaining tables + allow for word-at-a-time CRC calculation for both big-endian and little- + endian machines, where a word is four bytes. +*/ +local void make_crc_table() +{ + unsigned long c; + int n, k; + unsigned long poly; /* polynomial exclusive-or pattern */ + /* terms of polynomial defining this crc (except x^32): */ + static volatile int first = 1; /* flag to limit concurrent making */ + static const unsigned char p[] = {0,1,2,4,5,7,8,10,11,12,16,22,23,26}; + + /* See if another task is already doing this (not thread-safe, but better + than nothing -- significantly reduces duration of vulnerability in + case the advice about DYNAMIC_CRC_TABLE is ignored) */ + if (first) { + first = 0; + + /* make exclusive-or pattern from polynomial (0xedb88320UL) */ + poly = 0UL; + for (n = 0; n < sizeof(p)/sizeof(unsigned char); n++) + poly |= 1UL << (31 - p[n]); + + /* generate a crc for every 8-bit value */ + for (n = 0; n < 256; n++) { + c = (unsigned long)n; + for (k = 0; k < 8; k++) + c = c & 1 ? poly ^ (c >> 1) : c >> 1; + crc_table[0][n] = c; + } + +#ifdef BYFOUR + /* generate crc for each value followed by one, two, and three zeros, + and then the byte reversal of those as well as the first table */ + for (n = 0; n < 256; n++) { + c = crc_table[0][n]; + crc_table[4][n] = REV(c); + for (k = 1; k < 4; k++) { + c = crc_table[0][c & 0xff] ^ (c >> 8); + crc_table[k][n] = c; + crc_table[k + 4][n] = REV(c); + } + } +#endif /* BYFOUR */ + + crc_table_empty = 0; + } + else { /* not first */ + /* wait for the other guy to finish (not efficient, but rare) */ + while (crc_table_empty) + ; + } + +#ifdef MAKECRCH + /* write out CRC tables to crc32.h */ + { + FILE *out; + + out = fopen("crc32.h", "w"); + if (out == NULL) return; + fprintf(out, "/* crc32.h -- tables for rapid CRC calculation\n"); + fprintf(out, " * Generated automatically by crc32.c\n */\n\n"); + fprintf(out, "local const unsigned long FAR "); + fprintf(out, "crc_table[TBLS][256] =\n{\n {\n"); + write_table(out, crc_table[0]); +# ifdef BYFOUR + fprintf(out, "#ifdef BYFOUR\n"); + for (k = 1; k < 8; k++) { + fprintf(out, " },\n {\n"); + write_table(out, crc_table[k]); + } + fprintf(out, "#endif\n"); +# endif /* BYFOUR */ + fprintf(out, " }\n};\n"); + fclose(out); + } +#endif /* MAKECRCH */ +} + +#ifdef MAKECRCH +local void write_table(out, table) + FILE *out; + const unsigned long FAR *table; +{ + int n; + + for (n = 0; n < 256; n++) + fprintf(out, "%s0x%08lxUL%s", n % 5 ? "" : " ", table[n], + n == 255 ? "\n" : (n % 5 == 4 ? ",\n" : ", ")); +} +#endif /* MAKECRCH */ + +#else /* !DYNAMIC_CRC_TABLE */ +/* ======================================================================== + * Tables of CRC-32s of all single-byte values, made by make_crc_table(). + */ +#include "crc32.h" +#endif /* DYNAMIC_CRC_TABLE */ + +/* ========================================================================= + * This function can be used by asm versions of crc32() + */ +const unsigned long FAR * ZEXPORT get_crc_table(void) +{ +#ifdef DYNAMIC_CRC_TABLE + if (crc_table_empty) + make_crc_table(); +#endif /* DYNAMIC_CRC_TABLE */ + return (const unsigned long FAR *)crc_table; +} + +/* ========================================================================= */ +#define DO1 crc = crc_table[0][((int)crc ^ (*buf++)) & 0xff] ^ (crc >> 8) +#define DO8 DO1; DO1; DO1; DO1; DO1; DO1; DO1; DO1 + +/* ========================================================================= */ +unsigned long ZEXPORT crc32( + unsigned long crc, + const unsigned char FAR *buf, + unsigned len) +{ + if (buf == Z_NULL) return 0UL; + +#ifdef DYNAMIC_CRC_TABLE + if (crc_table_empty) + make_crc_table(); +#endif /* DYNAMIC_CRC_TABLE */ + +#ifdef BYFOUR + if (sizeof(void *) == sizeof(ptrdiff_t)) { + u4 endian; + + endian = 1; + if (*((unsigned char *)(&endian))) + return crc32_little(crc, buf, len); + else + return crc32_big(crc, buf, len); + } +#endif /* BYFOUR */ +/* crc = crc ^ 0xffffffffUL;*/ + while (len >= 8) { + DO8; + len -= 8; + } + if (len) do { + DO1; + } while (--len); + return crc /* ^ 0xffffffffUL*/; +} + +#ifdef BYFOUR + +/* ========================================================================= */ +#define DOLIT4 c ^= *buf4++; \ + c = crc_table[3][c & 0xff] ^ crc_table[2][(c >> 8) & 0xff] ^ \ + crc_table[1][(c >> 16) & 0xff] ^ crc_table[0][c >> 24] +#define DOLIT32 DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4; DOLIT4 + +/* ========================================================================= */ +local unsigned long crc32_little(crc, buf, len) + unsigned long crc; + const unsigned char FAR *buf; + unsigned len; +{ + register u4 c; + register const u4 FAR *buf4; + + c = (u4)crc; + c = ~c; + while (len && ((ptrdiff_t)buf & 3)) { + c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); + len--; + } + + buf4 = (const u4 FAR *)buf; + while (len >= 32) { + DOLIT32; + len -= 32; + } + while (len >= 4) { + DOLIT4; + len -= 4; + } + buf = (const unsigned char FAR *)buf4; + + if (len) do { + c = crc_table[0][(c ^ *buf++) & 0xff] ^ (c >> 8); + } while (--len); + c = ~c; + return (unsigned long)c; +} + +/* ========================================================================= */ +#define DOBIG4 c ^= *++buf4; \ + c = crc_table[4][c & 0xff] ^ crc_table[5][(c >> 8) & 0xff] ^ \ + crc_table[6][(c >> 16) & 0xff] ^ crc_table[7][c >> 24] +#define DOBIG32 DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4; DOBIG4 + +/* ========================================================================= */ +local unsigned long crc32_big(crc, buf, len) + unsigned long crc; + const unsigned char FAR *buf; + unsigned len; +{ + register u4 c; + register const u4 FAR *buf4; + + c = REV((u4)crc); + c = ~c; + while (len && ((ptrdiff_t)buf & 3)) { + c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); + len--; + } + + buf4 = (const u4 FAR *)buf; + buf4--; + while (len >= 32) { + DOBIG32; + len -= 32; + } + while (len >= 4) { + DOBIG4; + len -= 4; + } + buf4++; + buf = (const unsigned char FAR *)buf4; + + if (len) do { + c = crc_table[4][(c >> 24) ^ *buf++] ^ (c << 8); + } while (--len); + c = ~c; + return (unsigned long)(REV(c)); +} + +#endif /* BYFOUR */ diff --git a/lib/crc32.h b/lib/crc32.h new file mode 100644 index 0000000..8053b61 --- /dev/null +++ b/lib/crc32.h @@ -0,0 +1,441 @@ +/* crc32.h -- tables for rapid CRC calculation + * Generated automatically by crc32.c + */ + +local const unsigned long FAR crc_table[TBLS][256] = +{ + { + 0x00000000UL, 0x77073096UL, 0xee0e612cUL, 0x990951baUL, 0x076dc419UL, + 0x706af48fUL, 0xe963a535UL, 0x9e6495a3UL, 0x0edb8832UL, 0x79dcb8a4UL, + 0xe0d5e91eUL, 0x97d2d988UL, 0x09b64c2bUL, 0x7eb17cbdUL, 0xe7b82d07UL, + 0x90bf1d91UL, 0x1db71064UL, 0x6ab020f2UL, 0xf3b97148UL, 0x84be41deUL, + 0x1adad47dUL, 0x6ddde4ebUL, 0xf4d4b551UL, 0x83d385c7UL, 0x136c9856UL, + 0x646ba8c0UL, 0xfd62f97aUL, 0x8a65c9ecUL, 0x14015c4fUL, 0x63066cd9UL, + 0xfa0f3d63UL, 0x8d080df5UL, 0x3b6e20c8UL, 0x4c69105eUL, 0xd56041e4UL, + 0xa2677172UL, 0x3c03e4d1UL, 0x4b04d447UL, 0xd20d85fdUL, 0xa50ab56bUL, + 0x35b5a8faUL, 0x42b2986cUL, 0xdbbbc9d6UL, 0xacbcf940UL, 0x32d86ce3UL, + 0x45df5c75UL, 0xdcd60dcfUL, 0xabd13d59UL, 0x26d930acUL, 0x51de003aUL, + 0xc8d75180UL, 0xbfd06116UL, 0x21b4f4b5UL, 0x56b3c423UL, 0xcfba9599UL, + 0xb8bda50fUL, 0x2802b89eUL, 0x5f058808UL, 0xc60cd9b2UL, 0xb10be924UL, + 0x2f6f7c87UL, 0x58684c11UL, 0xc1611dabUL, 0xb6662d3dUL, 0x76dc4190UL, + 0x01db7106UL, 0x98d220bcUL, 0xefd5102aUL, 0x71b18589UL, 0x06b6b51fUL, + 0x9fbfe4a5UL, 0xe8b8d433UL, 0x7807c9a2UL, 0x0f00f934UL, 0x9609a88eUL, + 0xe10e9818UL, 0x7f6a0dbbUL, 0x086d3d2dUL, 0x91646c97UL, 0xe6635c01UL, + 0x6b6b51f4UL, 0x1c6c6162UL, 0x856530d8UL, 0xf262004eUL, 0x6c0695edUL, + 0x1b01a57bUL, 0x8208f4c1UL, 0xf50fc457UL, 0x65b0d9c6UL, 0x12b7e950UL, + 0x8bbeb8eaUL, 0xfcb9887cUL, 0x62dd1ddfUL, 0x15da2d49UL, 0x8cd37cf3UL, + 0xfbd44c65UL, 0x4db26158UL, 0x3ab551ceUL, 0xa3bc0074UL, 0xd4bb30e2UL, + 0x4adfa541UL, 0x3dd895d7UL, 0xa4d1c46dUL, 0xd3d6f4fbUL, 0x4369e96aUL, + 0x346ed9fcUL, 0xad678846UL, 0xda60b8d0UL, 0x44042d73UL, 0x33031de5UL, + 0xaa0a4c5fUL, 0xdd0d7cc9UL, 0x5005713cUL, 0x270241aaUL, 0xbe0b1010UL, + 0xc90c2086UL, 0x5768b525UL, 0x206f85b3UL, 0xb966d409UL, 0xce61e49fUL, + 0x5edef90eUL, 0x29d9c998UL, 0xb0d09822UL, 0xc7d7a8b4UL, 0x59b33d17UL, + 0x2eb40d81UL, 0xb7bd5c3bUL, 0xc0ba6cadUL, 0xedb88320UL, 0x9abfb3b6UL, + 0x03b6e20cUL, 0x74b1d29aUL, 0xead54739UL, 0x9dd277afUL, 0x04db2615UL, + 0x73dc1683UL, 0xe3630b12UL, 0x94643b84UL, 0x0d6d6a3eUL, 0x7a6a5aa8UL, + 0xe40ecf0bUL, 0x9309ff9dUL, 0x0a00ae27UL, 0x7d079eb1UL, 0xf00f9344UL, + 0x8708a3d2UL, 0x1e01f268UL, 0x6906c2feUL, 0xf762575dUL, 0x806567cbUL, + 0x196c3671UL, 0x6e6b06e7UL, 0xfed41b76UL, 0x89d32be0UL, 0x10da7a5aUL, + 0x67dd4accUL, 0xf9b9df6fUL, 0x8ebeeff9UL, 0x17b7be43UL, 0x60b08ed5UL, + 0xd6d6a3e8UL, 0xa1d1937eUL, 0x38d8c2c4UL, 0x4fdff252UL, 0xd1bb67f1UL, + 0xa6bc5767UL, 0x3fb506ddUL, 0x48b2364bUL, 0xd80d2bdaUL, 0xaf0a1b4cUL, + 0x36034af6UL, 0x41047a60UL, 0xdf60efc3UL, 0xa867df55UL, 0x316e8eefUL, + 0x4669be79UL, 0xcb61b38cUL, 0xbc66831aUL, 0x256fd2a0UL, 0x5268e236UL, + 0xcc0c7795UL, 0xbb0b4703UL, 0x220216b9UL, 0x5505262fUL, 0xc5ba3bbeUL, + 0xb2bd0b28UL, 0x2bb45a92UL, 0x5cb36a04UL, 0xc2d7ffa7UL, 0xb5d0cf31UL, + 0x2cd99e8bUL, 0x5bdeae1dUL, 0x9b64c2b0UL, 0xec63f226UL, 0x756aa39cUL, + 0x026d930aUL, 0x9c0906a9UL, 0xeb0e363fUL, 0x72076785UL, 0x05005713UL, + 0x95bf4a82UL, 0xe2b87a14UL, 0x7bb12baeUL, 0x0cb61b38UL, 0x92d28e9bUL, + 0xe5d5be0dUL, 0x7cdcefb7UL, 0x0bdbdf21UL, 0x86d3d2d4UL, 0xf1d4e242UL, + 0x68ddb3f8UL, 0x1fda836eUL, 0x81be16cdUL, 0xf6b9265bUL, 0x6fb077e1UL, + 0x18b74777UL, 0x88085ae6UL, 0xff0f6a70UL, 0x66063bcaUL, 0x11010b5cUL, + 0x8f659effUL, 0xf862ae69UL, 0x616bffd3UL, 0x166ccf45UL, 0xa00ae278UL, + 0xd70dd2eeUL, 0x4e048354UL, 0x3903b3c2UL, 0xa7672661UL, 0xd06016f7UL, + 0x4969474dUL, 0x3e6e77dbUL, 0xaed16a4aUL, 0xd9d65adcUL, 0x40df0b66UL, + 0x37d83bf0UL, 0xa9bcae53UL, 0xdebb9ec5UL, 0x47b2cf7fUL, 0x30b5ffe9UL, + 0xbdbdf21cUL, 0xcabac28aUL, 0x53b39330UL, 0x24b4a3a6UL, 0xbad03605UL, + 0xcdd70693UL, 0x54de5729UL, 0x23d967bfUL, 0xb3667a2eUL, 0xc4614ab8UL, + 0x5d681b02UL, 0x2a6f2b94UL, 0xb40bbe37UL, 0xc30c8ea1UL, 0x5a05df1bUL, + 0x2d02ef8dUL +#ifdef BYFOUR + }, + { + 0x00000000UL, 0x191b3141UL, 0x32366282UL, 0x2b2d53c3UL, 0x646cc504UL, + 0x7d77f445UL, 0x565aa786UL, 0x4f4196c7UL, 0xc8d98a08UL, 0xd1c2bb49UL, + 0xfaefe88aUL, 0xe3f4d9cbUL, 0xacb54f0cUL, 0xb5ae7e4dUL, 0x9e832d8eUL, + 0x87981ccfUL, 0x4ac21251UL, 0x53d92310UL, 0x78f470d3UL, 0x61ef4192UL, + 0x2eaed755UL, 0x37b5e614UL, 0x1c98b5d7UL, 0x05838496UL, 0x821b9859UL, + 0x9b00a918UL, 0xb02dfadbUL, 0xa936cb9aUL, 0xe6775d5dUL, 0xff6c6c1cUL, + 0xd4413fdfUL, 0xcd5a0e9eUL, 0x958424a2UL, 0x8c9f15e3UL, 0xa7b24620UL, + 0xbea97761UL, 0xf1e8e1a6UL, 0xe8f3d0e7UL, 0xc3de8324UL, 0xdac5b265UL, + 0x5d5daeaaUL, 0x44469febUL, 0x6f6bcc28UL, 0x7670fd69UL, 0x39316baeUL, + 0x202a5aefUL, 0x0b07092cUL, 0x121c386dUL, 0xdf4636f3UL, 0xc65d07b2UL, + 0xed705471UL, 0xf46b6530UL, 0xbb2af3f7UL, 0xa231c2b6UL, 0x891c9175UL, + 0x9007a034UL, 0x179fbcfbUL, 0x0e848dbaUL, 0x25a9de79UL, 0x3cb2ef38UL, + 0x73f379ffUL, 0x6ae848beUL, 0x41c51b7dUL, 0x58de2a3cUL, 0xf0794f05UL, + 0xe9627e44UL, 0xc24f2d87UL, 0xdb541cc6UL, 0x94158a01UL, 0x8d0ebb40UL, + 0xa623e883UL, 0xbf38d9c2UL, 0x38a0c50dUL, 0x21bbf44cUL, 0x0a96a78fUL, + 0x138d96ceUL, 0x5ccc0009UL, 0x45d73148UL, 0x6efa628bUL, 0x77e153caUL, + 0xbabb5d54UL, 0xa3a06c15UL, 0x888d3fd6UL, 0x91960e97UL, 0xded79850UL, + 0xc7cca911UL, 0xece1fad2UL, 0xf5facb93UL, 0x7262d75cUL, 0x6b79e61dUL, + 0x4054b5deUL, 0x594f849fUL, 0x160e1258UL, 0x0f152319UL, 0x243870daUL, + 0x3d23419bUL, 0x65fd6ba7UL, 0x7ce65ae6UL, 0x57cb0925UL, 0x4ed03864UL, + 0x0191aea3UL, 0x188a9fe2UL, 0x33a7cc21UL, 0x2abcfd60UL, 0xad24e1afUL, + 0xb43fd0eeUL, 0x9f12832dUL, 0x8609b26cUL, 0xc94824abUL, 0xd05315eaUL, + 0xfb7e4629UL, 0xe2657768UL, 0x2f3f79f6UL, 0x362448b7UL, 0x1d091b74UL, + 0x04122a35UL, 0x4b53bcf2UL, 0x52488db3UL, 0x7965de70UL, 0x607eef31UL, + 0xe7e6f3feUL, 0xfefdc2bfUL, 0xd5d0917cUL, 0xcccba03dUL, 0x838a36faUL, + 0x9a9107bbUL, 0xb1bc5478UL, 0xa8a76539UL, 0x3b83984bUL, 0x2298a90aUL, + 0x09b5fac9UL, 0x10aecb88UL, 0x5fef5d4fUL, 0x46f46c0eUL, 0x6dd93fcdUL, + 0x74c20e8cUL, 0xf35a1243UL, 0xea412302UL, 0xc16c70c1UL, 0xd8774180UL, + 0x9736d747UL, 0x8e2de606UL, 0xa500b5c5UL, 0xbc1b8484UL, 0x71418a1aUL, + 0x685abb5bUL, 0x4377e898UL, 0x5a6cd9d9UL, 0x152d4f1eUL, 0x0c367e5fUL, + 0x271b2d9cUL, 0x3e001cddUL, 0xb9980012UL, 0xa0833153UL, 0x8bae6290UL, + 0x92b553d1UL, 0xddf4c516UL, 0xc4eff457UL, 0xefc2a794UL, 0xf6d996d5UL, + 0xae07bce9UL, 0xb71c8da8UL, 0x9c31de6bUL, 0x852aef2aUL, 0xca6b79edUL, + 0xd37048acUL, 0xf85d1b6fUL, 0xe1462a2eUL, 0x66de36e1UL, 0x7fc507a0UL, + 0x54e85463UL, 0x4df36522UL, 0x02b2f3e5UL, 0x1ba9c2a4UL, 0x30849167UL, + 0x299fa026UL, 0xe4c5aeb8UL, 0xfdde9ff9UL, 0xd6f3cc3aUL, 0xcfe8fd7bUL, + 0x80a96bbcUL, 0x99b25afdUL, 0xb29f093eUL, 0xab84387fUL, 0x2c1c24b0UL, + 0x350715f1UL, 0x1e2a4632UL, 0x07317773UL, 0x4870e1b4UL, 0x516bd0f5UL, + 0x7a468336UL, 0x635db277UL, 0xcbfad74eUL, 0xd2e1e60fUL, 0xf9ccb5ccUL, + 0xe0d7848dUL, 0xaf96124aUL, 0xb68d230bUL, 0x9da070c8UL, 0x84bb4189UL, + 0x03235d46UL, 0x1a386c07UL, 0x31153fc4UL, 0x280e0e85UL, 0x674f9842UL, + 0x7e54a903UL, 0x5579fac0UL, 0x4c62cb81UL, 0x8138c51fUL, 0x9823f45eUL, + 0xb30ea79dUL, 0xaa1596dcUL, 0xe554001bUL, 0xfc4f315aUL, 0xd7626299UL, + 0xce7953d8UL, 0x49e14f17UL, 0x50fa7e56UL, 0x7bd72d95UL, 0x62cc1cd4UL, + 0x2d8d8a13UL, 0x3496bb52UL, 0x1fbbe891UL, 0x06a0d9d0UL, 0x5e7ef3ecUL, + 0x4765c2adUL, 0x6c48916eUL, 0x7553a02fUL, 0x3a1236e8UL, 0x230907a9UL, + 0x0824546aUL, 0x113f652bUL, 0x96a779e4UL, 0x8fbc48a5UL, 0xa4911b66UL, + 0xbd8a2a27UL, 0xf2cbbce0UL, 0xebd08da1UL, 0xc0fdde62UL, 0xd9e6ef23UL, + 0x14bce1bdUL, 0x0da7d0fcUL, 0x268a833fUL, 0x3f91b27eUL, 0x70d024b9UL, + 0x69cb15f8UL, 0x42e6463bUL, 0x5bfd777aUL, 0xdc656bb5UL, 0xc57e5af4UL, + 0xee530937UL, 0xf7483876UL, 0xb809aeb1UL, 0xa1129ff0UL, 0x8a3fcc33UL, + 0x9324fd72UL + }, + { + 0x00000000UL, 0x01c26a37UL, 0x0384d46eUL, 0x0246be59UL, 0x0709a8dcUL, + 0x06cbc2ebUL, 0x048d7cb2UL, 0x054f1685UL, 0x0e1351b8UL, 0x0fd13b8fUL, + 0x0d9785d6UL, 0x0c55efe1UL, 0x091af964UL, 0x08d89353UL, 0x0a9e2d0aUL, + 0x0b5c473dUL, 0x1c26a370UL, 0x1de4c947UL, 0x1fa2771eUL, 0x1e601d29UL, + 0x1b2f0bacUL, 0x1aed619bUL, 0x18abdfc2UL, 0x1969b5f5UL, 0x1235f2c8UL, + 0x13f798ffUL, 0x11b126a6UL, 0x10734c91UL, 0x153c5a14UL, 0x14fe3023UL, + 0x16b88e7aUL, 0x177ae44dUL, 0x384d46e0UL, 0x398f2cd7UL, 0x3bc9928eUL, + 0x3a0bf8b9UL, 0x3f44ee3cUL, 0x3e86840bUL, 0x3cc03a52UL, 0x3d025065UL, + 0x365e1758UL, 0x379c7d6fUL, 0x35dac336UL, 0x3418a901UL, 0x3157bf84UL, + 0x3095d5b3UL, 0x32d36beaUL, 0x331101ddUL, 0x246be590UL, 0x25a98fa7UL, + 0x27ef31feUL, 0x262d5bc9UL, 0x23624d4cUL, 0x22a0277bUL, 0x20e69922UL, + 0x2124f315UL, 0x2a78b428UL, 0x2bbade1fUL, 0x29fc6046UL, 0x283e0a71UL, + 0x2d711cf4UL, 0x2cb376c3UL, 0x2ef5c89aUL, 0x2f37a2adUL, 0x709a8dc0UL, + 0x7158e7f7UL, 0x731e59aeUL, 0x72dc3399UL, 0x7793251cUL, 0x76514f2bUL, + 0x7417f172UL, 0x75d59b45UL, 0x7e89dc78UL, 0x7f4bb64fUL, 0x7d0d0816UL, + 0x7ccf6221UL, 0x798074a4UL, 0x78421e93UL, 0x7a04a0caUL, 0x7bc6cafdUL, + 0x6cbc2eb0UL, 0x6d7e4487UL, 0x6f38fadeUL, 0x6efa90e9UL, 0x6bb5866cUL, + 0x6a77ec5bUL, 0x68315202UL, 0x69f33835UL, 0x62af7f08UL, 0x636d153fUL, + 0x612bab66UL, 0x60e9c151UL, 0x65a6d7d4UL, 0x6464bde3UL, 0x662203baUL, + 0x67e0698dUL, 0x48d7cb20UL, 0x4915a117UL, 0x4b531f4eUL, 0x4a917579UL, + 0x4fde63fcUL, 0x4e1c09cbUL, 0x4c5ab792UL, 0x4d98dda5UL, 0x46c49a98UL, + 0x4706f0afUL, 0x45404ef6UL, 0x448224c1UL, 0x41cd3244UL, 0x400f5873UL, + 0x4249e62aUL, 0x438b8c1dUL, 0x54f16850UL, 0x55330267UL, 0x5775bc3eUL, + 0x56b7d609UL, 0x53f8c08cUL, 0x523aaabbUL, 0x507c14e2UL, 0x51be7ed5UL, + 0x5ae239e8UL, 0x5b2053dfUL, 0x5966ed86UL, 0x58a487b1UL, 0x5deb9134UL, + 0x5c29fb03UL, 0x5e6f455aUL, 0x5fad2f6dUL, 0xe1351b80UL, 0xe0f771b7UL, + 0xe2b1cfeeUL, 0xe373a5d9UL, 0xe63cb35cUL, 0xe7fed96bUL, 0xe5b86732UL, + 0xe47a0d05UL, 0xef264a38UL, 0xeee4200fUL, 0xeca29e56UL, 0xed60f461UL, + 0xe82fe2e4UL, 0xe9ed88d3UL, 0xebab368aUL, 0xea695cbdUL, 0xfd13b8f0UL, + 0xfcd1d2c7UL, 0xfe976c9eUL, 0xff5506a9UL, 0xfa1a102cUL, 0xfbd87a1bUL, + 0xf99ec442UL, 0xf85cae75UL, 0xf300e948UL, 0xf2c2837fUL, 0xf0843d26UL, + 0xf1465711UL, 0xf4094194UL, 0xf5cb2ba3UL, 0xf78d95faUL, 0xf64fffcdUL, + 0xd9785d60UL, 0xd8ba3757UL, 0xdafc890eUL, 0xdb3ee339UL, 0xde71f5bcUL, + 0xdfb39f8bUL, 0xddf521d2UL, 0xdc374be5UL, 0xd76b0cd8UL, 0xd6a966efUL, + 0xd4efd8b6UL, 0xd52db281UL, 0xd062a404UL, 0xd1a0ce33UL, 0xd3e6706aUL, + 0xd2241a5dUL, 0xc55efe10UL, 0xc49c9427UL, 0xc6da2a7eUL, 0xc7184049UL, + 0xc25756ccUL, 0xc3953cfbUL, 0xc1d382a2UL, 0xc011e895UL, 0xcb4dafa8UL, + 0xca8fc59fUL, 0xc8c97bc6UL, 0xc90b11f1UL, 0xcc440774UL, 0xcd866d43UL, + 0xcfc0d31aUL, 0xce02b92dUL, 0x91af9640UL, 0x906dfc77UL, 0x922b422eUL, + 0x93e92819UL, 0x96a63e9cUL, 0x976454abUL, 0x9522eaf2UL, 0x94e080c5UL, + 0x9fbcc7f8UL, 0x9e7eadcfUL, 0x9c381396UL, 0x9dfa79a1UL, 0x98b56f24UL, + 0x99770513UL, 0x9b31bb4aUL, 0x9af3d17dUL, 0x8d893530UL, 0x8c4b5f07UL, + 0x8e0de15eUL, 0x8fcf8b69UL, 0x8a809decUL, 0x8b42f7dbUL, 0x89044982UL, + 0x88c623b5UL, 0x839a6488UL, 0x82580ebfUL, 0x801eb0e6UL, 0x81dcdad1UL, + 0x8493cc54UL, 0x8551a663UL, 0x8717183aUL, 0x86d5720dUL, 0xa9e2d0a0UL, + 0xa820ba97UL, 0xaa6604ceUL, 0xaba46ef9UL, 0xaeeb787cUL, 0xaf29124bUL, + 0xad6fac12UL, 0xacadc625UL, 0xa7f18118UL, 0xa633eb2fUL, 0xa4755576UL, + 0xa5b73f41UL, 0xa0f829c4UL, 0xa13a43f3UL, 0xa37cfdaaUL, 0xa2be979dUL, + 0xb5c473d0UL, 0xb40619e7UL, 0xb640a7beUL, 0xb782cd89UL, 0xb2cddb0cUL, + 0xb30fb13bUL, 0xb1490f62UL, 0xb08b6555UL, 0xbbd72268UL, 0xba15485fUL, + 0xb853f606UL, 0xb9919c31UL, 0xbcde8ab4UL, 0xbd1ce083UL, 0xbf5a5edaUL, + 0xbe9834edUL + }, + { + 0x00000000UL, 0xb8bc6765UL, 0xaa09c88bUL, 0x12b5afeeUL, 0x8f629757UL, + 0x37def032UL, 0x256b5fdcUL, 0x9dd738b9UL, 0xc5b428efUL, 0x7d084f8aUL, + 0x6fbde064UL, 0xd7018701UL, 0x4ad6bfb8UL, 0xf26ad8ddUL, 0xe0df7733UL, + 0x58631056UL, 0x5019579fUL, 0xe8a530faUL, 0xfa109f14UL, 0x42acf871UL, + 0xdf7bc0c8UL, 0x67c7a7adUL, 0x75720843UL, 0xcdce6f26UL, 0x95ad7f70UL, + 0x2d111815UL, 0x3fa4b7fbUL, 0x8718d09eUL, 0x1acfe827UL, 0xa2738f42UL, + 0xb0c620acUL, 0x087a47c9UL, 0xa032af3eUL, 0x188ec85bUL, 0x0a3b67b5UL, + 0xb28700d0UL, 0x2f503869UL, 0x97ec5f0cUL, 0x8559f0e2UL, 0x3de59787UL, + 0x658687d1UL, 0xdd3ae0b4UL, 0xcf8f4f5aUL, 0x7733283fUL, 0xeae41086UL, + 0x525877e3UL, 0x40edd80dUL, 0xf851bf68UL, 0xf02bf8a1UL, 0x48979fc4UL, + 0x5a22302aUL, 0xe29e574fUL, 0x7f496ff6UL, 0xc7f50893UL, 0xd540a77dUL, + 0x6dfcc018UL, 0x359fd04eUL, 0x8d23b72bUL, 0x9f9618c5UL, 0x272a7fa0UL, + 0xbafd4719UL, 0x0241207cUL, 0x10f48f92UL, 0xa848e8f7UL, 0x9b14583dUL, + 0x23a83f58UL, 0x311d90b6UL, 0x89a1f7d3UL, 0x1476cf6aUL, 0xaccaa80fUL, + 0xbe7f07e1UL, 0x06c36084UL, 0x5ea070d2UL, 0xe61c17b7UL, 0xf4a9b859UL, + 0x4c15df3cUL, 0xd1c2e785UL, 0x697e80e0UL, 0x7bcb2f0eUL, 0xc377486bUL, + 0xcb0d0fa2UL, 0x73b168c7UL, 0x6104c729UL, 0xd9b8a04cUL, 0x446f98f5UL, + 0xfcd3ff90UL, 0xee66507eUL, 0x56da371bUL, 0x0eb9274dUL, 0xb6054028UL, + 0xa4b0efc6UL, 0x1c0c88a3UL, 0x81dbb01aUL, 0x3967d77fUL, 0x2bd27891UL, + 0x936e1ff4UL, 0x3b26f703UL, 0x839a9066UL, 0x912f3f88UL, 0x299358edUL, + 0xb4446054UL, 0x0cf80731UL, 0x1e4da8dfUL, 0xa6f1cfbaUL, 0xfe92dfecUL, + 0x462eb889UL, 0x549b1767UL, 0xec277002UL, 0x71f048bbUL, 0xc94c2fdeUL, + 0xdbf98030UL, 0x6345e755UL, 0x6b3fa09cUL, 0xd383c7f9UL, 0xc1366817UL, + 0x798a0f72UL, 0xe45d37cbUL, 0x5ce150aeUL, 0x4e54ff40UL, 0xf6e89825UL, + 0xae8b8873UL, 0x1637ef16UL, 0x048240f8UL, 0xbc3e279dUL, 0x21e91f24UL, + 0x99557841UL, 0x8be0d7afUL, 0x335cb0caUL, 0xed59b63bUL, 0x55e5d15eUL, + 0x47507eb0UL, 0xffec19d5UL, 0x623b216cUL, 0xda874609UL, 0xc832e9e7UL, + 0x708e8e82UL, 0x28ed9ed4UL, 0x9051f9b1UL, 0x82e4565fUL, 0x3a58313aUL, + 0xa78f0983UL, 0x1f336ee6UL, 0x0d86c108UL, 0xb53aa66dUL, 0xbd40e1a4UL, + 0x05fc86c1UL, 0x1749292fUL, 0xaff54e4aUL, 0x322276f3UL, 0x8a9e1196UL, + 0x982bbe78UL, 0x2097d91dUL, 0x78f4c94bUL, 0xc048ae2eUL, 0xd2fd01c0UL, + 0x6a4166a5UL, 0xf7965e1cUL, 0x4f2a3979UL, 0x5d9f9697UL, 0xe523f1f2UL, + 0x4d6b1905UL, 0xf5d77e60UL, 0xe762d18eUL, 0x5fdeb6ebUL, 0xc2098e52UL, + 0x7ab5e937UL, 0x680046d9UL, 0xd0bc21bcUL, 0x88df31eaUL, 0x3063568fUL, + 0x22d6f961UL, 0x9a6a9e04UL, 0x07bda6bdUL, 0xbf01c1d8UL, 0xadb46e36UL, + 0x15080953UL, 0x1d724e9aUL, 0xa5ce29ffUL, 0xb77b8611UL, 0x0fc7e174UL, + 0x9210d9cdUL, 0x2aacbea8UL, 0x38191146UL, 0x80a57623UL, 0xd8c66675UL, + 0x607a0110UL, 0x72cfaefeUL, 0xca73c99bUL, 0x57a4f122UL, 0xef189647UL, + 0xfdad39a9UL, 0x45115eccUL, 0x764dee06UL, 0xcef18963UL, 0xdc44268dUL, + 0x64f841e8UL, 0xf92f7951UL, 0x41931e34UL, 0x5326b1daUL, 0xeb9ad6bfUL, + 0xb3f9c6e9UL, 0x0b45a18cUL, 0x19f00e62UL, 0xa14c6907UL, 0x3c9b51beUL, + 0x842736dbUL, 0x96929935UL, 0x2e2efe50UL, 0x2654b999UL, 0x9ee8defcUL, + 0x8c5d7112UL, 0x34e11677UL, 0xa9362eceUL, 0x118a49abUL, 0x033fe645UL, + 0xbb838120UL, 0xe3e09176UL, 0x5b5cf613UL, 0x49e959fdUL, 0xf1553e98UL, + 0x6c820621UL, 0xd43e6144UL, 0xc68bceaaUL, 0x7e37a9cfUL, 0xd67f4138UL, + 0x6ec3265dUL, 0x7c7689b3UL, 0xc4caeed6UL, 0x591dd66fUL, 0xe1a1b10aUL, + 0xf3141ee4UL, 0x4ba87981UL, 0x13cb69d7UL, 0xab770eb2UL, 0xb9c2a15cUL, + 0x017ec639UL, 0x9ca9fe80UL, 0x241599e5UL, 0x36a0360bUL, 0x8e1c516eUL, + 0x866616a7UL, 0x3eda71c2UL, 0x2c6fde2cUL, 0x94d3b949UL, 0x090481f0UL, + 0xb1b8e695UL, 0xa30d497bUL, 0x1bb12e1eUL, 0x43d23e48UL, 0xfb6e592dUL, + 0xe9dbf6c3UL, 0x516791a6UL, 0xccb0a91fUL, 0x740cce7aUL, 0x66b96194UL, + 0xde0506f1UL + }, + { + 0x00000000UL, 0x96300777UL, 0x2c610eeeUL, 0xba510999UL, 0x19c46d07UL, + 0x8ff46a70UL, 0x35a563e9UL, 0xa395649eUL, 0x3288db0eUL, 0xa4b8dc79UL, + 0x1ee9d5e0UL, 0x88d9d297UL, 0x2b4cb609UL, 0xbd7cb17eUL, 0x072db8e7UL, + 0x911dbf90UL, 0x6410b71dUL, 0xf220b06aUL, 0x4871b9f3UL, 0xde41be84UL, + 0x7dd4da1aUL, 0xebe4dd6dUL, 0x51b5d4f4UL, 0xc785d383UL, 0x56986c13UL, + 0xc0a86b64UL, 0x7af962fdUL, 0xecc9658aUL, 0x4f5c0114UL, 0xd96c0663UL, + 0x633d0ffaUL, 0xf50d088dUL, 0xc8206e3bUL, 0x5e10694cUL, 0xe44160d5UL, + 0x727167a2UL, 0xd1e4033cUL, 0x47d4044bUL, 0xfd850dd2UL, 0x6bb50aa5UL, + 0xfaa8b535UL, 0x6c98b242UL, 0xd6c9bbdbUL, 0x40f9bcacUL, 0xe36cd832UL, + 0x755cdf45UL, 0xcf0dd6dcUL, 0x593dd1abUL, 0xac30d926UL, 0x3a00de51UL, + 0x8051d7c8UL, 0x1661d0bfUL, 0xb5f4b421UL, 0x23c4b356UL, 0x9995bacfUL, + 0x0fa5bdb8UL, 0x9eb80228UL, 0x0888055fUL, 0xb2d90cc6UL, 0x24e90bb1UL, + 0x877c6f2fUL, 0x114c6858UL, 0xab1d61c1UL, 0x3d2d66b6UL, 0x9041dc76UL, + 0x0671db01UL, 0xbc20d298UL, 0x2a10d5efUL, 0x8985b171UL, 0x1fb5b606UL, + 0xa5e4bf9fUL, 0x33d4b8e8UL, 0xa2c90778UL, 0x34f9000fUL, 0x8ea80996UL, + 0x18980ee1UL, 0xbb0d6a7fUL, 0x2d3d6d08UL, 0x976c6491UL, 0x015c63e6UL, + 0xf4516b6bUL, 0x62616c1cUL, 0xd8306585UL, 0x4e0062f2UL, 0xed95066cUL, + 0x7ba5011bUL, 0xc1f40882UL, 0x57c40ff5UL, 0xc6d9b065UL, 0x50e9b712UL, + 0xeab8be8bUL, 0x7c88b9fcUL, 0xdf1ddd62UL, 0x492dda15UL, 0xf37cd38cUL, + 0x654cd4fbUL, 0x5861b24dUL, 0xce51b53aUL, 0x7400bca3UL, 0xe230bbd4UL, + 0x41a5df4aUL, 0xd795d83dUL, 0x6dc4d1a4UL, 0xfbf4d6d3UL, 0x6ae96943UL, + 0xfcd96e34UL, 0x468867adUL, 0xd0b860daUL, 0x732d0444UL, 0xe51d0333UL, + 0x5f4c0aaaUL, 0xc97c0dddUL, 0x3c710550UL, 0xaa410227UL, 0x10100bbeUL, + 0x86200cc9UL, 0x25b56857UL, 0xb3856f20UL, 0x09d466b9UL, 0x9fe461ceUL, + 0x0ef9de5eUL, 0x98c9d929UL, 0x2298d0b0UL, 0xb4a8d7c7UL, 0x173db359UL, + 0x810db42eUL, 0x3b5cbdb7UL, 0xad6cbac0UL, 0x2083b8edUL, 0xb6b3bf9aUL, + 0x0ce2b603UL, 0x9ad2b174UL, 0x3947d5eaUL, 0xaf77d29dUL, 0x1526db04UL, + 0x8316dc73UL, 0x120b63e3UL, 0x843b6494UL, 0x3e6a6d0dUL, 0xa85a6a7aUL, + 0x0bcf0ee4UL, 0x9dff0993UL, 0x27ae000aUL, 0xb19e077dUL, 0x44930ff0UL, + 0xd2a30887UL, 0x68f2011eUL, 0xfec20669UL, 0x5d5762f7UL, 0xcb676580UL, + 0x71366c19UL, 0xe7066b6eUL, 0x761bd4feUL, 0xe02bd389UL, 0x5a7ada10UL, + 0xcc4add67UL, 0x6fdfb9f9UL, 0xf9efbe8eUL, 0x43beb717UL, 0xd58eb060UL, + 0xe8a3d6d6UL, 0x7e93d1a1UL, 0xc4c2d838UL, 0x52f2df4fUL, 0xf167bbd1UL, + 0x6757bca6UL, 0xdd06b53fUL, 0x4b36b248UL, 0xda2b0dd8UL, 0x4c1b0aafUL, + 0xf64a0336UL, 0x607a0441UL, 0xc3ef60dfUL, 0x55df67a8UL, 0xef8e6e31UL, + 0x79be6946UL, 0x8cb361cbUL, 0x1a8366bcUL, 0xa0d26f25UL, 0x36e26852UL, + 0x95770cccUL, 0x03470bbbUL, 0xb9160222UL, 0x2f260555UL, 0xbe3bbac5UL, + 0x280bbdb2UL, 0x925ab42bUL, 0x046ab35cUL, 0xa7ffd7c2UL, 0x31cfd0b5UL, + 0x8b9ed92cUL, 0x1daede5bUL, 0xb0c2649bUL, 0x26f263ecUL, 0x9ca36a75UL, + 0x0a936d02UL, 0xa906099cUL, 0x3f360eebUL, 0x85670772UL, 0x13570005UL, + 0x824abf95UL, 0x147ab8e2UL, 0xae2bb17bUL, 0x381bb60cUL, 0x9b8ed292UL, + 0x0dbed5e5UL, 0xb7efdc7cUL, 0x21dfdb0bUL, 0xd4d2d386UL, 0x42e2d4f1UL, + 0xf8b3dd68UL, 0x6e83da1fUL, 0xcd16be81UL, 0x5b26b9f6UL, 0xe177b06fUL, + 0x7747b718UL, 0xe65a0888UL, 0x706a0fffUL, 0xca3b0666UL, 0x5c0b0111UL, + 0xff9e658fUL, 0x69ae62f8UL, 0xd3ff6b61UL, 0x45cf6c16UL, 0x78e20aa0UL, + 0xeed20dd7UL, 0x5483044eUL, 0xc2b30339UL, 0x612667a7UL, 0xf71660d0UL, + 0x4d476949UL, 0xdb776e3eUL, 0x4a6ad1aeUL, 0xdc5ad6d9UL, 0x660bdf40UL, + 0xf03bd837UL, 0x53aebca9UL, 0xc59ebbdeUL, 0x7fcfb247UL, 0xe9ffb530UL, + 0x1cf2bdbdUL, 0x8ac2bacaUL, 0x3093b353UL, 0xa6a3b424UL, 0x0536d0baUL, + 0x9306d7cdUL, 0x2957de54UL, 0xbf67d923UL, 0x2e7a66b3UL, 0xb84a61c4UL, + 0x021b685dUL, 0x942b6f2aUL, 0x37be0bb4UL, 0xa18e0cc3UL, 0x1bdf055aUL, + 0x8def022dUL + }, + { + 0x00000000UL, 0x41311b19UL, 0x82623632UL, 0xc3532d2bUL, 0x04c56c64UL, + 0x45f4777dUL, 0x86a75a56UL, 0xc796414fUL, 0x088ad9c8UL, 0x49bbc2d1UL, + 0x8ae8effaUL, 0xcbd9f4e3UL, 0x0c4fb5acUL, 0x4d7eaeb5UL, 0x8e2d839eUL, + 0xcf1c9887UL, 0x5112c24aUL, 0x1023d953UL, 0xd370f478UL, 0x9241ef61UL, + 0x55d7ae2eUL, 0x14e6b537UL, 0xd7b5981cUL, 0x96848305UL, 0x59981b82UL, + 0x18a9009bUL, 0xdbfa2db0UL, 0x9acb36a9UL, 0x5d5d77e6UL, 0x1c6c6cffUL, + 0xdf3f41d4UL, 0x9e0e5acdUL, 0xa2248495UL, 0xe3159f8cUL, 0x2046b2a7UL, + 0x6177a9beUL, 0xa6e1e8f1UL, 0xe7d0f3e8UL, 0x2483dec3UL, 0x65b2c5daUL, + 0xaaae5d5dUL, 0xeb9f4644UL, 0x28cc6b6fUL, 0x69fd7076UL, 0xae6b3139UL, + 0xef5a2a20UL, 0x2c09070bUL, 0x6d381c12UL, 0xf33646dfUL, 0xb2075dc6UL, + 0x715470edUL, 0x30656bf4UL, 0xf7f32abbUL, 0xb6c231a2UL, 0x75911c89UL, + 0x34a00790UL, 0xfbbc9f17UL, 0xba8d840eUL, 0x79dea925UL, 0x38efb23cUL, + 0xff79f373UL, 0xbe48e86aUL, 0x7d1bc541UL, 0x3c2ade58UL, 0x054f79f0UL, + 0x447e62e9UL, 0x872d4fc2UL, 0xc61c54dbUL, 0x018a1594UL, 0x40bb0e8dUL, + 0x83e823a6UL, 0xc2d938bfUL, 0x0dc5a038UL, 0x4cf4bb21UL, 0x8fa7960aUL, + 0xce968d13UL, 0x0900cc5cUL, 0x4831d745UL, 0x8b62fa6eUL, 0xca53e177UL, + 0x545dbbbaUL, 0x156ca0a3UL, 0xd63f8d88UL, 0x970e9691UL, 0x5098d7deUL, + 0x11a9ccc7UL, 0xd2fae1ecUL, 0x93cbfaf5UL, 0x5cd76272UL, 0x1de6796bUL, + 0xdeb55440UL, 0x9f844f59UL, 0x58120e16UL, 0x1923150fUL, 0xda703824UL, + 0x9b41233dUL, 0xa76bfd65UL, 0xe65ae67cUL, 0x2509cb57UL, 0x6438d04eUL, + 0xa3ae9101UL, 0xe29f8a18UL, 0x21cca733UL, 0x60fdbc2aUL, 0xafe124adUL, + 0xeed03fb4UL, 0x2d83129fUL, 0x6cb20986UL, 0xab2448c9UL, 0xea1553d0UL, + 0x29467efbUL, 0x687765e2UL, 0xf6793f2fUL, 0xb7482436UL, 0x741b091dUL, + 0x352a1204UL, 0xf2bc534bUL, 0xb38d4852UL, 0x70de6579UL, 0x31ef7e60UL, + 0xfef3e6e7UL, 0xbfc2fdfeUL, 0x7c91d0d5UL, 0x3da0cbccUL, 0xfa368a83UL, + 0xbb07919aUL, 0x7854bcb1UL, 0x3965a7a8UL, 0x4b98833bUL, 0x0aa99822UL, + 0xc9fab509UL, 0x88cbae10UL, 0x4f5def5fUL, 0x0e6cf446UL, 0xcd3fd96dUL, + 0x8c0ec274UL, 0x43125af3UL, 0x022341eaUL, 0xc1706cc1UL, 0x804177d8UL, + 0x47d73697UL, 0x06e62d8eUL, 0xc5b500a5UL, 0x84841bbcUL, 0x1a8a4171UL, + 0x5bbb5a68UL, 0x98e87743UL, 0xd9d96c5aUL, 0x1e4f2d15UL, 0x5f7e360cUL, + 0x9c2d1b27UL, 0xdd1c003eUL, 0x120098b9UL, 0x533183a0UL, 0x9062ae8bUL, + 0xd153b592UL, 0x16c5f4ddUL, 0x57f4efc4UL, 0x94a7c2efUL, 0xd596d9f6UL, + 0xe9bc07aeUL, 0xa88d1cb7UL, 0x6bde319cUL, 0x2aef2a85UL, 0xed796bcaUL, + 0xac4870d3UL, 0x6f1b5df8UL, 0x2e2a46e1UL, 0xe136de66UL, 0xa007c57fUL, + 0x6354e854UL, 0x2265f34dUL, 0xe5f3b202UL, 0xa4c2a91bUL, 0x67918430UL, + 0x26a09f29UL, 0xb8aec5e4UL, 0xf99fdefdUL, 0x3accf3d6UL, 0x7bfde8cfUL, + 0xbc6ba980UL, 0xfd5ab299UL, 0x3e099fb2UL, 0x7f3884abUL, 0xb0241c2cUL, + 0xf1150735UL, 0x32462a1eUL, 0x73773107UL, 0xb4e17048UL, 0xf5d06b51UL, + 0x3683467aUL, 0x77b25d63UL, 0x4ed7facbUL, 0x0fe6e1d2UL, 0xccb5ccf9UL, + 0x8d84d7e0UL, 0x4a1296afUL, 0x0b238db6UL, 0xc870a09dUL, 0x8941bb84UL, + 0x465d2303UL, 0x076c381aUL, 0xc43f1531UL, 0x850e0e28UL, 0x42984f67UL, + 0x03a9547eUL, 0xc0fa7955UL, 0x81cb624cUL, 0x1fc53881UL, 0x5ef42398UL, + 0x9da70eb3UL, 0xdc9615aaUL, 0x1b0054e5UL, 0x5a314ffcUL, 0x996262d7UL, + 0xd85379ceUL, 0x174fe149UL, 0x567efa50UL, 0x952dd77bUL, 0xd41ccc62UL, + 0x138a8d2dUL, 0x52bb9634UL, 0x91e8bb1fUL, 0xd0d9a006UL, 0xecf37e5eUL, + 0xadc26547UL, 0x6e91486cUL, 0x2fa05375UL, 0xe836123aUL, 0xa9070923UL, + 0x6a542408UL, 0x2b653f11UL, 0xe479a796UL, 0xa548bc8fUL, 0x661b91a4UL, + 0x272a8abdUL, 0xe0bccbf2UL, 0xa18dd0ebUL, 0x62defdc0UL, 0x23efe6d9UL, + 0xbde1bc14UL, 0xfcd0a70dUL, 0x3f838a26UL, 0x7eb2913fUL, 0xb924d070UL, + 0xf815cb69UL, 0x3b46e642UL, 0x7a77fd5bUL, 0xb56b65dcUL, 0xf45a7ec5UL, + 0x370953eeUL, 0x763848f7UL, 0xb1ae09b8UL, 0xf09f12a1UL, 0x33cc3f8aUL, + 0x72fd2493UL + }, + { + 0x00000000UL, 0x376ac201UL, 0x6ed48403UL, 0x59be4602UL, 0xdca80907UL, + 0xebc2cb06UL, 0xb27c8d04UL, 0x85164f05UL, 0xb851130eUL, 0x8f3bd10fUL, + 0xd685970dUL, 0xe1ef550cUL, 0x64f91a09UL, 0x5393d808UL, 0x0a2d9e0aUL, + 0x3d475c0bUL, 0x70a3261cUL, 0x47c9e41dUL, 0x1e77a21fUL, 0x291d601eUL, + 0xac0b2f1bUL, 0x9b61ed1aUL, 0xc2dfab18UL, 0xf5b56919UL, 0xc8f23512UL, + 0xff98f713UL, 0xa626b111UL, 0x914c7310UL, 0x145a3c15UL, 0x2330fe14UL, + 0x7a8eb816UL, 0x4de47a17UL, 0xe0464d38UL, 0xd72c8f39UL, 0x8e92c93bUL, + 0xb9f80b3aUL, 0x3cee443fUL, 0x0b84863eUL, 0x523ac03cUL, 0x6550023dUL, + 0x58175e36UL, 0x6f7d9c37UL, 0x36c3da35UL, 0x01a91834UL, 0x84bf5731UL, + 0xb3d59530UL, 0xea6bd332UL, 0xdd011133UL, 0x90e56b24UL, 0xa78fa925UL, + 0xfe31ef27UL, 0xc95b2d26UL, 0x4c4d6223UL, 0x7b27a022UL, 0x2299e620UL, + 0x15f32421UL, 0x28b4782aUL, 0x1fdeba2bUL, 0x4660fc29UL, 0x710a3e28UL, + 0xf41c712dUL, 0xc376b32cUL, 0x9ac8f52eUL, 0xada2372fUL, 0xc08d9a70UL, + 0xf7e75871UL, 0xae591e73UL, 0x9933dc72UL, 0x1c259377UL, 0x2b4f5176UL, + 0x72f11774UL, 0x459bd575UL, 0x78dc897eUL, 0x4fb64b7fUL, 0x16080d7dUL, + 0x2162cf7cUL, 0xa4748079UL, 0x931e4278UL, 0xcaa0047aUL, 0xfdcac67bUL, + 0xb02ebc6cUL, 0x87447e6dUL, 0xdefa386fUL, 0xe990fa6eUL, 0x6c86b56bUL, + 0x5bec776aUL, 0x02523168UL, 0x3538f369UL, 0x087faf62UL, 0x3f156d63UL, + 0x66ab2b61UL, 0x51c1e960UL, 0xd4d7a665UL, 0xe3bd6464UL, 0xba032266UL, + 0x8d69e067UL, 0x20cbd748UL, 0x17a11549UL, 0x4e1f534bUL, 0x7975914aUL, + 0xfc63de4fUL, 0xcb091c4eUL, 0x92b75a4cUL, 0xa5dd984dUL, 0x989ac446UL, + 0xaff00647UL, 0xf64e4045UL, 0xc1248244UL, 0x4432cd41UL, 0x73580f40UL, + 0x2ae64942UL, 0x1d8c8b43UL, 0x5068f154UL, 0x67023355UL, 0x3ebc7557UL, + 0x09d6b756UL, 0x8cc0f853UL, 0xbbaa3a52UL, 0xe2147c50UL, 0xd57ebe51UL, + 0xe839e25aUL, 0xdf53205bUL, 0x86ed6659UL, 0xb187a458UL, 0x3491eb5dUL, + 0x03fb295cUL, 0x5a456f5eUL, 0x6d2fad5fUL, 0x801b35e1UL, 0xb771f7e0UL, + 0xeecfb1e2UL, 0xd9a573e3UL, 0x5cb33ce6UL, 0x6bd9fee7UL, 0x3267b8e5UL, + 0x050d7ae4UL, 0x384a26efUL, 0x0f20e4eeUL, 0x569ea2ecUL, 0x61f460edUL, + 0xe4e22fe8UL, 0xd388ede9UL, 0x8a36abebUL, 0xbd5c69eaUL, 0xf0b813fdUL, + 0xc7d2d1fcUL, 0x9e6c97feUL, 0xa90655ffUL, 0x2c101afaUL, 0x1b7ad8fbUL, + 0x42c49ef9UL, 0x75ae5cf8UL, 0x48e900f3UL, 0x7f83c2f2UL, 0x263d84f0UL, + 0x115746f1UL, 0x944109f4UL, 0xa32bcbf5UL, 0xfa958df7UL, 0xcdff4ff6UL, + 0x605d78d9UL, 0x5737bad8UL, 0x0e89fcdaUL, 0x39e33edbUL, 0xbcf571deUL, + 0x8b9fb3dfUL, 0xd221f5ddUL, 0xe54b37dcUL, 0xd80c6bd7UL, 0xef66a9d6UL, + 0xb6d8efd4UL, 0x81b22dd5UL, 0x04a462d0UL, 0x33cea0d1UL, 0x6a70e6d3UL, + 0x5d1a24d2UL, 0x10fe5ec5UL, 0x27949cc4UL, 0x7e2adac6UL, 0x494018c7UL, + 0xcc5657c2UL, 0xfb3c95c3UL, 0xa282d3c1UL, 0x95e811c0UL, 0xa8af4dcbUL, + 0x9fc58fcaUL, 0xc67bc9c8UL, 0xf1110bc9UL, 0x740744ccUL, 0x436d86cdUL, + 0x1ad3c0cfUL, 0x2db902ceUL, 0x4096af91UL, 0x77fc6d90UL, 0x2e422b92UL, + 0x1928e993UL, 0x9c3ea696UL, 0xab546497UL, 0xf2ea2295UL, 0xc580e094UL, + 0xf8c7bc9fUL, 0xcfad7e9eUL, 0x9613389cUL, 0xa179fa9dUL, 0x246fb598UL, + 0x13057799UL, 0x4abb319bUL, 0x7dd1f39aUL, 0x3035898dUL, 0x075f4b8cUL, + 0x5ee10d8eUL, 0x698bcf8fUL, 0xec9d808aUL, 0xdbf7428bUL, 0x82490489UL, + 0xb523c688UL, 0x88649a83UL, 0xbf0e5882UL, 0xe6b01e80UL, 0xd1dadc81UL, + 0x54cc9384UL, 0x63a65185UL, 0x3a181787UL, 0x0d72d586UL, 0xa0d0e2a9UL, + 0x97ba20a8UL, 0xce0466aaUL, 0xf96ea4abUL, 0x7c78ebaeUL, 0x4b1229afUL, + 0x12ac6fadUL, 0x25c6adacUL, 0x1881f1a7UL, 0x2feb33a6UL, 0x765575a4UL, + 0x413fb7a5UL, 0xc429f8a0UL, 0xf3433aa1UL, 0xaafd7ca3UL, 0x9d97bea2UL, + 0xd073c4b5UL, 0xe71906b4UL, 0xbea740b6UL, 0x89cd82b7UL, 0x0cdbcdb2UL, + 0x3bb10fb3UL, 0x620f49b1UL, 0x55658bb0UL, 0x6822d7bbUL, 0x5f4815baUL, + 0x06f653b8UL, 0x319c91b9UL, 0xb48adebcUL, 0x83e01cbdUL, 0xda5e5abfUL, + 0xed3498beUL + }, + { + 0x00000000UL, 0x6567bcb8UL, 0x8bc809aaUL, 0xeeafb512UL, 0x5797628fUL, + 0x32f0de37UL, 0xdc5f6b25UL, 0xb938d79dUL, 0xef28b4c5UL, 0x8a4f087dUL, + 0x64e0bd6fUL, 0x018701d7UL, 0xb8bfd64aUL, 0xddd86af2UL, 0x3377dfe0UL, + 0x56106358UL, 0x9f571950UL, 0xfa30a5e8UL, 0x149f10faUL, 0x71f8ac42UL, + 0xc8c07bdfUL, 0xada7c767UL, 0x43087275UL, 0x266fcecdUL, 0x707fad95UL, + 0x1518112dUL, 0xfbb7a43fUL, 0x9ed01887UL, 0x27e8cf1aUL, 0x428f73a2UL, + 0xac20c6b0UL, 0xc9477a08UL, 0x3eaf32a0UL, 0x5bc88e18UL, 0xb5673b0aUL, + 0xd00087b2UL, 0x6938502fUL, 0x0c5fec97UL, 0xe2f05985UL, 0x8797e53dUL, + 0xd1878665UL, 0xb4e03addUL, 0x5a4f8fcfUL, 0x3f283377UL, 0x8610e4eaUL, + 0xe3775852UL, 0x0dd8ed40UL, 0x68bf51f8UL, 0xa1f82bf0UL, 0xc49f9748UL, + 0x2a30225aUL, 0x4f579ee2UL, 0xf66f497fUL, 0x9308f5c7UL, 0x7da740d5UL, + 0x18c0fc6dUL, 0x4ed09f35UL, 0x2bb7238dUL, 0xc518969fUL, 0xa07f2a27UL, + 0x1947fdbaUL, 0x7c204102UL, 0x928ff410UL, 0xf7e848a8UL, 0x3d58149bUL, + 0x583fa823UL, 0xb6901d31UL, 0xd3f7a189UL, 0x6acf7614UL, 0x0fa8caacUL, + 0xe1077fbeUL, 0x8460c306UL, 0xd270a05eUL, 0xb7171ce6UL, 0x59b8a9f4UL, + 0x3cdf154cUL, 0x85e7c2d1UL, 0xe0807e69UL, 0x0e2fcb7bUL, 0x6b4877c3UL, + 0xa20f0dcbUL, 0xc768b173UL, 0x29c70461UL, 0x4ca0b8d9UL, 0xf5986f44UL, + 0x90ffd3fcUL, 0x7e5066eeUL, 0x1b37da56UL, 0x4d27b90eUL, 0x284005b6UL, + 0xc6efb0a4UL, 0xa3880c1cUL, 0x1ab0db81UL, 0x7fd76739UL, 0x9178d22bUL, + 0xf41f6e93UL, 0x03f7263bUL, 0x66909a83UL, 0x883f2f91UL, 0xed589329UL, + 0x546044b4UL, 0x3107f80cUL, 0xdfa84d1eUL, 0xbacff1a6UL, 0xecdf92feUL, + 0x89b82e46UL, 0x67179b54UL, 0x027027ecUL, 0xbb48f071UL, 0xde2f4cc9UL, + 0x3080f9dbUL, 0x55e74563UL, 0x9ca03f6bUL, 0xf9c783d3UL, 0x176836c1UL, + 0x720f8a79UL, 0xcb375de4UL, 0xae50e15cUL, 0x40ff544eUL, 0x2598e8f6UL, + 0x73888baeUL, 0x16ef3716UL, 0xf8408204UL, 0x9d273ebcUL, 0x241fe921UL, + 0x41785599UL, 0xafd7e08bUL, 0xcab05c33UL, 0x3bb659edUL, 0x5ed1e555UL, + 0xb07e5047UL, 0xd519ecffUL, 0x6c213b62UL, 0x094687daUL, 0xe7e932c8UL, + 0x828e8e70UL, 0xd49eed28UL, 0xb1f95190UL, 0x5f56e482UL, 0x3a31583aUL, + 0x83098fa7UL, 0xe66e331fUL, 0x08c1860dUL, 0x6da63ab5UL, 0xa4e140bdUL, + 0xc186fc05UL, 0x2f294917UL, 0x4a4ef5afUL, 0xf3762232UL, 0x96119e8aUL, + 0x78be2b98UL, 0x1dd99720UL, 0x4bc9f478UL, 0x2eae48c0UL, 0xc001fdd2UL, + 0xa566416aUL, 0x1c5e96f7UL, 0x79392a4fUL, 0x97969f5dUL, 0xf2f123e5UL, + 0x05196b4dUL, 0x607ed7f5UL, 0x8ed162e7UL, 0xebb6de5fUL, 0x528e09c2UL, + 0x37e9b57aUL, 0xd9460068UL, 0xbc21bcd0UL, 0xea31df88UL, 0x8f566330UL, + 0x61f9d622UL, 0x049e6a9aUL, 0xbda6bd07UL, 0xd8c101bfUL, 0x366eb4adUL, + 0x53090815UL, 0x9a4e721dUL, 0xff29cea5UL, 0x11867bb7UL, 0x74e1c70fUL, + 0xcdd91092UL, 0xa8beac2aUL, 0x46111938UL, 0x2376a580UL, 0x7566c6d8UL, + 0x10017a60UL, 0xfeaecf72UL, 0x9bc973caUL, 0x22f1a457UL, 0x479618efUL, + 0xa939adfdUL, 0xcc5e1145UL, 0x06ee4d76UL, 0x6389f1ceUL, 0x8d2644dcUL, + 0xe841f864UL, 0x51792ff9UL, 0x341e9341UL, 0xdab12653UL, 0xbfd69aebUL, + 0xe9c6f9b3UL, 0x8ca1450bUL, 0x620ef019UL, 0x07694ca1UL, 0xbe519b3cUL, + 0xdb362784UL, 0x35999296UL, 0x50fe2e2eUL, 0x99b95426UL, 0xfcdee89eUL, + 0x12715d8cUL, 0x7716e134UL, 0xce2e36a9UL, 0xab498a11UL, 0x45e63f03UL, + 0x208183bbUL, 0x7691e0e3UL, 0x13f65c5bUL, 0xfd59e949UL, 0x983e55f1UL, + 0x2106826cUL, 0x44613ed4UL, 0xaace8bc6UL, 0xcfa9377eUL, 0x38417fd6UL, + 0x5d26c36eUL, 0xb389767cUL, 0xd6eecac4UL, 0x6fd61d59UL, 0x0ab1a1e1UL, + 0xe41e14f3UL, 0x8179a84bUL, 0xd769cb13UL, 0xb20e77abUL, 0x5ca1c2b9UL, + 0x39c67e01UL, 0x80fea99cUL, 0xe5991524UL, 0x0b36a036UL, 0x6e511c8eUL, + 0xa7166686UL, 0xc271da3eUL, 0x2cde6f2cUL, 0x49b9d394UL, 0xf0810409UL, + 0x95e6b8b1UL, 0x7b490da3UL, 0x1e2eb11bUL, 0x483ed243UL, 0x2d596efbUL, + 0xc3f6dbe9UL, 0xa6916751UL, 0x1fa9b0ccUL, 0x7ace0c74UL, 0x9461b966UL, + 0xf10605deUL +#endif + } +}; diff --git a/lib/interface b/lib/interface new file mode 100644 index 0000000..5029625 --- /dev/null +++ b/lib/interface @@ -0,0 +1,70 @@ + +The following is brief documentation for the interface provided by the lafs library. +More details can be found in the various source files. + +The lafs library allow a lafs filesystems to be created, loaded, examined, and modified. + +The key objects managed are: + + - struct lafs - a handle for the filesystem as a whole + - struct lafs_device - an individual device within a struct lafs + - struct lafs_ino - an 'inode' or file object within the filesystem + - struct lafs_blk - a block in a file/inode + + +Each subordinate structure has a reference to the whole filesystem so +mutliple pointers do not need to be passed around. + +talloc is used for memory management, so talloc_free(lafs) will free the whole filesystem. + + +A filesystem handle must be created and separately initialised, either from a device +or as a new array. + +Function all: + +struct lafs *lafs_alloc(void) + Create a new handle. Most things won't work on this. Normally either + lafs_new() or lafs_load() must be used. + + +int lafs_new(struct lafs*, int block_bytes) + Initialise the filesystem with a new random uuid etc. It will have no devices + attached and so no size + +struct lafs_device * lafs_add_device(struct lafs*, char *dev, int fd, long segment_blocks, long stride_blocks, int width, + int usage_inum) + Add a device to a lafs. A number of segments will be calculated leaving room for metadata + at start and end. The device will be inserted into the address-space and some suitable point. + +struct lafs_ino *lafs_get_itable(struct lafs *) + Return a handle on the inode file which holds all inodes + +struct lafs_ino *lafs_add_inode(struct lafs_ino *itable, long inum, int type) + Create a new file with given inode number and type. + +int lafs_write_dev(struct lafs_device*) + write the device_blocks for this device + +int lafs_checkpoint(struct lafs *) + write out a checkpoint and then the state blocks to all devices. + This involves writing all blocks that have been marked as 'sched'. + Doing that might cause other blocks (parents) to be scheduled, so + multiple write-clusters might be needed. + + +int lafs_im_set(struct lafs_ino*, int inum) + The lafs_ino must be an InodeMap file, and inum is marked as being in-use +int lafs_im_clr(struct lafs_ino*, int inum) + The lafs_ino must be an InodeMap file, and inum is marked as being available + +int lafs_add_free_seg(struct lafs*, int dev, long segment) + Add the given segment to the list of segments available for write-out. + +struct lafs_dblk(struct lafs_ino*, loff_t bnum) + Get the datablock in the inode. Data blocks always referred to their parent in + the indexing tree. + +int lafs_sched_dblk(struct lafs_dblk*) + Mark the block as dirty and to be written in the next checkpoint. This places it on a list + an references the parent to ensure the parent isn't written before this block. \ No newline at end of file diff --git a/lib/lafs_add_device.c b/lib/lafs_add_device.c new file mode 100644 index 0000000..6e8a683 --- /dev/null +++ b/lib/lafs_add_device.c @@ -0,0 +1,144 @@ + +#include +#include +#include +#include +#include +#include +#include +#include + +/* Add a new device to lafs. + * This just sets up the internal data structures. + * We at least need to lafs_write_dev() to write metadata out, and may want some + * mount call to connect this to an active lafs. + */ + +static int destroy(struct lafs_device *dev) +{ + close(dev->fd); + return 0; +} + +static int get_logical_block_size(int fd) +{ + struct stat stb; + int sfd; + int rv = -1; + char path[60]; + char buf[20]; + fstat(fd, &stb); + sprintf(path, "/sys/dev/block/%d:%d/queue/logical_block_size", + major(stb.st_rdev), minor(stb.st_rdev)); + sfd = open(path, O_RDONLY); + if (sfd >= 0) { + int n = read(sfd, buf, sizeof(buf)); + if (n >= 0) + buf[n] = 0; + close(sfd); + rv = atoi(buf); + } + if (rv > 0) + return rv; + return 512; +} + +struct lafs_device *lafs_add_device(struct lafs *fs, char *devname, int fd, + loff_t segblocks, loff_t strideblocks, + int width, int usage_inum) +{ + struct lafs_device *dev = talloc(fs, struct lafs_device); + struct lafs_device *d2; + unsigned long long size; + int devblk; + + memset(dev, 0, sizeof(*dev)); + dev->fd = fd; + dev->seq = 1; + dev->devnum = fs->loaded_devs++; + dev->name = devname; + + dev->width = width; + dev->stride = strideblocks; + dev->segment_size = segblocks; + dev->usage_inum = usage_inum; + dev->next = fs->devs; + fs->devs = dev; + dev->fs = fs; + fs->devices++; + + if (dev->segment_size > fs->max_segment) + fs->max_segment = dev->segment_size; + + if (dev->width * dev->stride <= dev->segment_size) { + dev->tables_per_seg = dev->segment_size / + dev->width / dev->stride; + dev->rows_per_table = dev->stride; + dev->segment_stride = dev->segment_size; + } else { + dev->tables_per_seg = 1; + dev->rows_per_table = dev->segment_size / dev->width; + dev->segment_stride = dev->rows_per_table; + } + gettimeofday(&dev->ctime, NULL); + + talloc_set_destructor(dev, destroy); + + /* now need to work out where the metadata goes and how much room + * is left for segments. + * Device block needs 1K and goes once at start and once at end. + * Start location is 1K in unless basic block size is larger + */ + ioctl(fd, BLKGETSIZE64, &size); + devblk = get_logical_block_size(fd); + if (devblk < LAFS_DEVBLK_SIZE) + devblk = LAFS_DEVBLK_SIZE; + + size &= ~(unsigned long long)(devblk-1); + + +#if 0 + dev->devaddr[0] = devblk; +#else + dev->devaddr[0] = 0; +#endif + dev->devaddr[1] = size - devblk; + /* State block has size set by 'fs->statesize'. + * We have two at the start of the device and two at the end. + * If stride*width < segment size we put them at multiples of width*stride + * If stride*width > segment size we put them at multiples of statesize + */ + /* FIXME just do the simple version for now */ + if (dev->width != 1 || dev->stride != 1) + abort(); + if (devblk < fs->statesize) + devblk = fs->statesize; + dev->stateaddr[0] = 2 * devblk; + dev->stateaddr[1] = 3 * devblk; + dev->stateaddr[2] = size - 2*devblk; + dev->stateaddr[3] = size - 3*devblk; + + /* segments need to align with width*stride too - later */ + dev->segment_offset = 4 * devblk; + dev->segment_stride = dev->segment_size; + dev->segment_count = (dev->stateaddr[3] - dev->segment_offset) / fs->blocksize / dev->segment_size; + + + dev->tablesize = ((dev->segment_count + fs->blocksize/2 + 1) + / (fs->blocksize/2)); + + dev->size = dev->segment_count * dev->segment_size; + + /* Need to find a suitable offset */ + dev->start = 0; + for (d2 = dev->next; d2 ; d2 = d2->next) { + if (dev->start <= d2->start + d2->size && + dev->start + dev->size > d2->start) { + dev->start = d2->start + d2->size; + /* start again from top */ + d2 = dev; + } + } + + return dev; +} diff --git a/lib/lafs_add_free_seg.c b/lib/lafs_add_free_seg.c new file mode 100644 index 0000000..8078258 --- /dev/null +++ b/lib/lafs_add_free_seg.c @@ -0,0 +1,18 @@ +/* + * Add a free segment to the freelist if there is room + */ + +#include + +int lafs_add_free_seg(struct lafs *fs, int dev, loff_t seg) +{ + int next_tail = (fs->free_tail+1) & (128-1); + + if (next_tail == fs->free_head) + /* no room */ + return 0; + fs->freelist[fs->free_tail].dev = dev; + fs->freelist[fs->free_tail].seg = seg; + fs->free_tail = next_tail; + return 1; +} diff --git a/lib/lafs_add_inode.c b/lib/lafs_add_inode.c new file mode 100644 index 0000000..a494f4f --- /dev/null +++ b/lib/lafs_add_inode.c @@ -0,0 +1,22 @@ + +/* + * Create an inode in the file with the given inum and type. + * We get the data block, file it in, and import it. + */ + +#include + + +struct lafs_ino *lafs_add_inode(struct lafs_ino *fsys, int inum, int type) +{ + struct lafs_dblk *db; + struct lafs_ino *ino; + + db = lafs_dblk(fsys, inum); + + lafs_inode_init(fsys->fs, db->b.data, type); + + ino = lafs_import_inode(db); + lafs_dirty_blk(&db->b); + return ino; +} diff --git a/lib/lafs_alloc.c b/lib/lafs_alloc.c new file mode 100644 index 0000000..45e96db --- /dev/null +++ b/lib/lafs_alloc.c @@ -0,0 +1,23 @@ + +#include +#include +#include +#include + +struct lafs *lafs_alloc(void) +{ + struct lafs *fs = talloc(NULL, struct lafs); + int i; + + memset(fs, 0, sizeof(*fs)); + + INIT_LIST_HEAD(&fs->wc[0].blocks); + INIT_LIST_HEAD(&fs->wc[1].blocks); + INIT_LIST_HEAD(&fs->leafs); + INIT_LIST_HEAD(&fs->account_leafs); + + for (i=0; i < (1<htable[i]); + + return fs; +} diff --git a/lib/lafs_allocated_block.c b/lib/lafs_allocated_block.c new file mode 100644 index 0000000..6dfcb30 --- /dev/null +++ b/lib/lafs_allocated_block.c @@ -0,0 +1,39 @@ + +/* lafs_allocated_block. + * This block has been allocated to this address (and will soon + * be written there). + * We need to update the accounting info. + */ + +#include +#include +#include + +void lafs_allocated_block(struct lafs_blk *b, loff_t addr) +{ + struct lafs *fs = b->ino->fs; + struct lafs_iblk *p; + + printf("allocate %d/%ld %s to %ld\n", b->ino->inum, b->fileaddr, + (b->flags & B_Index) ? "index":"data", addr); + if (b->parent == NULL) { + /* This is the root inode. Its address goes + * directly in the 'state' block. + */ + if (b->flags & B_Index) abort(); + + /* FIXME this might be a different snapshot */ + printf("Root inode at %ld\n", addr); + b->physaddr = addr; + fs->ss.root_addr = addr; + return; + } + + p = b->parent; + lafs_summary_update(b->ino, b->physaddr, addr, + !!(b->flags & B_Index)); + b->physaddr = addr; + b->chain = p->uninc; + p->uninc = b; +} + diff --git a/lib/lafs_checkpoint.c b/lib/lafs_checkpoint.c new file mode 100644 index 0000000..92d60c7 --- /dev/null +++ b/lib/lafs_checkpoint.c @@ -0,0 +1,85 @@ + +/* Force out a checkpoint + * All scheduled blocks get incorporated and/or written + * then we write out the new state blocks + */ + +#include +#include + +#include + +int lafs_checkpoint(struct lafs *fs) +{ + struct lafs_blk *b; + + fs->checkpointing = CHECKPOINTING | CHECKPOINT_START; + + while (!list_empty(&fs->leafs)) { + while (!list_empty(&fs->leafs)) { + b = list_first_entry(&fs->leafs, + struct lafs_blk, + leafs); + printf("checkpoint %p %d/%ld %s %p\n", b, b->ino->inum, b->fileaddr, + (b->flags & B_Index) ? "index":"data", b->parent); + list_del_init(&b->leafs); + if (!(b->flags & B_Index)) { + struct lafs_dblk *db = dblk(b); + if (b->ino->type == TypeSegmentMap) { + /* Allow parents to be processed, but freeze this */ + de_sched(b); b->flags |= B_Sched; + list_add(&b->leafs, &fs->account_leafs); + continue; + } + if (b->ino->type == TypeInodeFile && + db->my_inode && + db->my_inode->iblock && + db->my_inode->iblock->sched_cnt) { + de_sched(b); + continue; + } + } + + if (b->flags & B_Index) { + struct lafs_iblk *ib = iblk(b); + if (ib->sched_cnt) { + de_sched(b); + continue; + } + if (ib->uninc) { + de_sched(b); + lafs_incorporate(ib); + if (!(ib->b.flags & B_Sched)) + abort(); + /* We'll pick it up next time 'round */ + continue; + } + } + if (b->flags & B_Dirty) { + printf("...alloc\n"); + lafs_cluster_allocate(b, 0); + } else + de_sched(b); + } + lafs_cluster_flush(fs, 0); + } + fs->flags |= LAFS_DELAY_UPDATES; + while (!list_empty(&fs->account_leafs)) { + b = list_first_entry(&fs->account_leafs, + struct lafs_blk, leafs); + b->flags &= ~B_Sched; + printf("Account %d/%ld\n", b->ino->inum, b->fileaddr); + list_del_init(&b->leafs); + + lafs_sched_blk(b); + list_del_init(&b->leafs); + + lafs_cluster_allocate(b, 0); + } + fs->checkpointing |= CHECKPOINT_END; + lafs_cluster_flush(fs, 0); + fs->flags &= ~LAFS_DELAY_UPDATES; + + lafs_write_state(fs); + return 0; +} diff --git a/lib/lafs_cluster_allocate.c b/lib/lafs_cluster_allocate.c new file mode 100644 index 0000000..16a21fd --- /dev/null +++ b/lib/lafs_cluster_allocate.c @@ -0,0 +1,49 @@ + +/* + * allocate a dirty block to a cluster so we can easily write it + * for a flush. + * + * FIXME + * For now, don't sort the blocks and assume that the cluster head + * and segment will have adequate room for all blocks. + * That is sufficient for mkfs + */ +#include +#include + +void lafs_cluster_allocate(struct lafs_blk *b, int cnum) +{ + struct lafs *fs = b->ino->fs; + struct lafs_cluster *wc; + if (!(b->flags & B_Dirty)) + abort(); + if (!(b->flags & B_Sched)) + abort(); + + if (!(b->flags & B_Index) && + b->ino->type == TypeInodeFile && + dblk(b)->my_inode && + (dblk(b)->my_inode->iflags & I_Dirty)) + lafs_inode_fillblock(dblk(b)->my_inode, b->data); + + if ((b->flags & B_Index) && + b->ino->iblock == iblk(b)) { + /* InoIdx block - cannot write that, must write the + * data block instead */ + lafs_dirty_blk(&b->ino->dblock->b); + de_sched(b); + return; + } + + + wc = &fs->wc[cnum]; + + if (wc->remaining == 0) { + /* FIXME lafs_cluster_flush(fs, cnum) ?? */ + lafs_new_segment(fs, cnum); + if (!wc->remaining) + abort(); + } + wc->remaining--; + list_add_tail(&b->leafs, &wc->blocks); +} diff --git a/lib/lafs_cluster_flush.c b/lib/lafs_cluster_flush.c new file mode 100644 index 0000000..4c7d844 --- /dev/null +++ b/lib/lafs_cluster_flush.c @@ -0,0 +1,343 @@ + +/* + * Flush out the given cluster with a cluster head. + * We "know" there is room in the cluster head and + * in the segment + */ +#define _GNU_SOURCE +#define _FILE_OFFSET_BITS 64 + +#include +#include +#include +#include +#include + +/*----------------------------------------------------------------------- + * A segment is divided up in a slightly complicated way into + * tables, rows and columns. This allows us to align writes with + * stripes in a raid4 array or similar. + * So we have some support routines to help track our way around + * the segment. + * + * A write cluster always comprises a whole number of rows - it is padded + * with zeros if necessary. + * However block addressing follow columns, so the blocks written to + * a cluster may well not be contiguously addressed. + * + * There are 'width' blocks (or columns) in a row, and either: + * - segment_stride rows in a table, if segment_stride < segment_size or + * - one table per segment, when segment_stride > segment_size. + * These are calculated and summarised in rows_per_table and tables_per_seg + * in the 'struct lafs_device' structure. + * + * A 'seg_pos' records the current segment and where we are in it. + * It has: + * dev,num to identify the segment + * st_table,st_row to identify where the cluster starts. + * nxt_table,nxt_row to identify where the cluster ends. + * table,row,col to identify where the next block will be found + */ + +static int seg_remainder(struct lafs *fs, struct lafs_segpos *seg) +{ + /* return the number of blocks from the start of segpos to the + * end of the segment. + * i.e. remaining rows in this table, plus remaining tables in + * this segment + */ + struct lafs_device *dv = dev_by_num(fs, seg->dev); + int rows = dv->rows_per_table - seg->st_row; + + if (seg->dev < 0) abort(); + rows += dv->rows_per_table * (dv->tables_per_seg - seg->st_table - 1); + return rows * dv->width; +} + +#if 0 +static void seg_step(struct lafs *fs, struct lafs_segpos *seg) +{ + /* reposition this segpos to be immediately after it's current end + * and make the 'current' point be the start. + * Size will be empty + */ + seg->st_table = seg->nxt_table; + seg->st_row = seg->nxt_row; + seg->table = seg->st_table; + seg->row = seg->st_row; + seg->col = 0; +} +#endif + +static u32 seg_setsize(struct lafs *fs, struct lafs_segpos *seg, u32 size) +{ + /* move the 'nxt' table/row to be 'size' blocks beyond + * current start. size will be rounded up to a multiple + * of width. + */ + struct lafs_device *dv = dev_by_num(fs, seg->dev); + u32 rv; + int rows; + + if (seg->dev < 0) abort(); + rows = (size + dv->width - 1) / dv->width; + rv = rows * dv->width; + rows += seg->st_row; + seg->nxt_table = seg->st_table + rows / dv->rows_per_table; + seg->nxt_row = rows % dv->rows_per_table; + return rv; +} + +static u64 seg_addr(struct lafs *fs, struct lafs_segpos *seg) +{ + /* Return the virtual address of the blocks pointed + * to by 'seg'. + */ + struct lafs_device *dv = dev_by_num(fs, seg->dev); + u64 addr; + + if (seg->dev < 0) + /* Setting 'next' address for last cluster in + * a cleaner segment */ + return 0; + addr = seg->col * dv->stride; + addr += seg->row; + addr += seg->table * dv->rows_per_table; + addr += seg->num * dv->segment_stride; + addr += dv->start; + return addr; +} + +static u64 seg_next(struct lafs *fs, struct lafs_segpos *seg) +{ + /* step forward one block, returning the address of + * the block stepped over + */ + struct lafs_device *dv = dev_by_num(fs, seg->dev); + u64 addr = seg_addr(fs, seg); + + /* now step forward in column or table or seg */ + seg->col++; + if (seg->col >= dv->width) { + seg->col = 0; + seg->row++; + if (seg->row >= dv->rows_per_table) { + seg->row = 0; + seg->table++; + } + } + return addr; +} + +/*------------------------------------------------------------------------- + * Cluster head building. + * We build the cluster head bit by bit as we find blocks + * in the list. These routines help. + */ + +static void cluster_addhead(struct lafs_cluster *wc, struct lafs_ino *ino, + int cnum, + struct group_head **headstart) +{ + struct group_head *gh = (void*)((char *)wc->chead + + wc->chead_size); + u16 tnf; + + *headstart = gh; + gh->inum = __cpu_to_le32(ino->inum); + gh->fsnum = __cpu_to_le32(ino->filesys->inum); + tnf = ((ino->generation<<8) | (ino->trunc_gen & 0xff)) + & 0x7fff; + if (cnum) + tnf |= 0x8000; + gh->truncatenum_and_flag = __cpu_to_le16(tnf); + wc->chead_size += sizeof(struct group_head); +} + +static void cluster_closehead(struct lafs_cluster *wc, + struct group_head *headstart) +{ + int size = wc->chead_size - (((char *)headstart) - (char *)wc->chead); + + headstart->group_size_words = size / 4; +} + +#if 0 +static void cluster_addmini(struct lafs_cluster *wc, u32 addr, int offset, + int size, const char *data, + int size2, const char *data2) +{ + /* if size2 !=0, then only + * (size-size2) is at 'data' and the rest is at 'data2' + */ + struct miniblock *mb = ((struct miniblock *) + ((char *)wc->chead + wc->chead_size)); + + mb->block_num = __cpu_to_le32(addr); + mb->block_offset = __cpu_to_le16(offset); + mb->length = __cpu_to_le16(size + DescMiniOffset); + wc->chead_size += sizeof(struct miniblock); + memcpy(mb->data, data, size-size2); + if (size2) + memcpy(mb->data + size-size2, data2, size2); + memset(mb->data+size, 0, (-size)&3); + wc->chead_size += ROUND_UP(size); +} +#endif + +static void cluster_adddesc(struct lafs_cluster *wc, struct lafs_blk *blk, + struct descriptor **desc_start) +{ + struct descriptor *dh = (struct descriptor *)((char *)wc->chead + + wc->chead_size); + *desc_start = dh; + dh->block_num = __cpu_to_le32(blk->fileaddr); + dh->block_cnt = __cpu_to_le32(0); + dh->block_bytes = 0; + if (blk->flags && B_Index) + dh->block_bytes = DescIndex; + wc->chead_size += sizeof(struct descriptor); +} + +static void cluster_incdesc(struct lafs_cluster *wc, struct descriptor *desc_start, + struct lafs_blk *b, int blksz) +{ + desc_start->block_cnt = + __cpu_to_le32(__le32_to_cpu(desc_start->block_cnt)+1); + if (!(b->flags & B_Index)) { + if (b->ino->type >= TypeBase) { + u64 size = b->ino->md.file.size; + if (size > ((loff_t)b->fileaddr * blksz) && + size <= ((loff_t)(b->fileaddr + 1) * blksz)) + desc_start->block_bytes = + __cpu_to_le32(size & (blksz-1)); + else + desc_start->block_bytes = + __cpu_to_le32(blksz); + } else + desc_start->block_bytes = __cpu_to_le32(blksz); + } +} + + +static int calc_cluster_csum(struct cluster_head *head) +{ + unsigned int oldcsum = head->checksum; + unsigned long long newcsum = 0; + unsigned long csum; + int i; + unsigned int *superc = (unsigned int *) head; + head->checksum = 0; + + for (i = 0; i < __le16_to_cpu(head->Hlength)/4; i++) + newcsum += __le32_to_cpu(superc[i]); + csum = (newcsum & 0xffffffff) + (newcsum>>32); + head->checksum = oldcsum; + return __cpu_to_le32(csum); +} + +void lafs_cluster_flush(struct lafs *fs, int cnum) +{ + char chead_buf[4096]; + struct cluster_head *ch; + struct lafs_cluster *wc = &fs->wc[cnum]; + struct lafs_blk *b; + int cluster_size; + int i; + loff_t head_addr[4]; + struct lafs_ino *current_inode = NULL; + off_t current_block = NoBlock; + struct descriptor *desc_start; + struct group_head *head_start = NULL; + struct lafs_device *dv; + + printf("flush\n"); + if (list_empty(&wc->blocks) && + !(fs->checkpointing & CHECKPOINT_END)) { + printf("...skipped\n"); + return; + } + + ch = (void*)chead_buf; + + wc->chead = chead_buf; + wc->chead_size = sizeof(struct cluster_head); + wc->chead_blocks = 1; + memcpy(ch->idtag, "LaFSHead", 8); + memcpy(ch->uuid, fs->uuid, 16); + ch->seq = __cpu_to_le64(fs->wc[cnum].seq); + + cluster_size = seg_setsize(fs, &wc->seg, + seg_remainder(fs, &wc->seg) - wc->remaining); + + /* find, and step over, address header block(s) */ + for (i = 0; i < wc->chead_blocks ; i++) + head_addr[i] = seg_next(fs, &wc->seg); + + ch->flags = 0; + if (cnum == 0 && fs->checkpointing) { + ch->flags = __cpu_to_le32(fs->checkpointing); + if (fs->checkpointing & CHECKPOINT_END) + fs->checkpointing = 0; + else if (fs->checkpointing & CHECKPOINT_START) { + fs->checkpoint_cluster = head_addr[0]; + fs->checkpointing &= ~CHECKPOINT_START; + } + } + + list_for_each_entry(b, &wc->blocks, leafs) { + if (b->ino != current_inode) { + /* need to create a new group_head */ + desc_start = NULL; + if (head_start) + cluster_closehead(wc, head_start); + cluster_addhead(wc, b->ino, cnum, &head_start); + current_inode = b->ino; + current_block = NoBlock; + } + if (desc_start == NULL || b->fileaddr != current_block+1 || + (b->flags & B_Index)) { + cluster_adddesc(wc, b, &desc_start); + current_block = b->fileaddr; + } else + current_block++; + cluster_incdesc(wc, desc_start, b, fs->blocksize); + + lafs_allocated_block(b, seg_next(fs, &wc->seg)); + } + if (head_start) + cluster_closehead(wc, head_start); + + ch->Hlength = __cpu_to_le16(wc->chead_size); + ch->Clength = __cpu_to_le16(cluster_size); + ch->verify_type = VerifyNull; + + ch->next_addr = __cpu_to_le64(seg_addr(fs, &wc->seg)); + ch->prev_addr = __cpu_to_le64(wc->prev_addr); + wc->prev_addr = head_addr[i]; + ch->this_addr = __cpu_to_le64(wc->prev_addr); + ch->checksum = calc_cluster_csum(ch); + + dv = dev_by_num(fs, wc->seg.dev); + + for (i = 0; i < wc->chead_blocks; i++) { + int dev; + loff_t sect; + virttophys(fs, head_addr[i], &dev, §); + lseek64(dv->fd, sect, SEEK_SET); + write(dv->fd, chead_buf+ i * fs->blocksize, fs->blocksize); + } + while (!list_empty(&wc->blocks)) { + int dev; + loff_t sect; + + b = list_first_entry(&wc->blocks, struct lafs_blk, leafs); + list_del_init(&b->leafs); + + virttophys(fs, b->physaddr, &dev, §); + lseek64(dv->fd, sect, SEEK_SET); + b->flags &= ~B_Dirty; + write(dv->fd, b->data, fs->blocksize); + de_sched(b); + } +} diff --git a/lib/lafs_cluster_init.c b/lib/lafs_cluster_init.c new file mode 100644 index 0000000..5e1ea8f --- /dev/null +++ b/lib/lafs_cluster_init.c @@ -0,0 +1,78 @@ + +/* lafs_cluster_init. + * initialise the write-cluster structure. This can identify + * a location in the middle of a segment where write can continue + * or no valid segment, so a new one will be allocated. + */ +#include + +static int seg_remainder(struct lafs *fs, struct lafs_segpos *seg) +{ + /* return the number of blocks from the start of segpos to the + * end of the segment. + * i.e. remaining rows in this table, plus remaining tables in + * this segment + */ + struct lafs_device *dv = dev_by_num(fs, seg->dev); + int rows = dv->rows_per_table - seg->st_row; + + if (seg->dev < 0) abort(); + rows += dv->rows_per_table * (dv->tables_per_seg - seg->st_table - 1); + return rows * dv->width; +} + +static void cluster_reset(struct lafs *fs, struct lafs_cluster *wc) +{ + if (wc->seg.dev < 0) { + wc->remaining = 0; + wc->chead_size = 0; + return; + } + wc->remaining = seg_remainder(fs, &wc->seg); + wc->chead_blocks = 1; + wc->remaining--; + wc->chead_size = sizeof(struct cluster_head); +} + +static void seg_setpos(struct lafs *fs, struct lafs_segpos *seg, loff_t addr) +{ + /* set seg to start at the given virtual address, and be + * of size 0 + * 'addr' should always be at the start of a row, though + * as it might be read off storage, we need to be + * a little bit careful. + */ + loff_t offset; + u32 row, col, table; + struct lafs_device *dv; + virttoseg(fs, addr, &seg->dev, &seg->num, &offset); + dv = &fs->devs[seg->dev]; + col = offset / dv->stride; + row = offset % dv->stride; + table = col / dv->width; + col = col % dv->width; + if (col) abort(); /* FIXME return an error instead */ + + seg->st_table = table; + seg->st_row = row; + + seg->table = seg->nxt_table = seg->st_table; + seg->row = seg->nxt_row = seg->st_row; + seg->col = 0; +} + +void lafs_cluster_init(struct lafs *fs, int cnum, + loff_t addr, loff_t prev, loff_t seq) +{ + struct lafs_cluster *wc = &fs->wc[cnum]; + + INIT_LIST_HEAD(&wc->blocks); + wc->prev_addr = prev; + wc->seq = seq; + if (addr) + seg_setpos(fs, &wc->seg, addr); + else + wc->seg.dev = -1; + cluster_reset(fs, wc); + //fs->free_blocks = wc->remaining; +} diff --git a/lib/lafs_dblk.c b/lib/lafs_dblk.c new file mode 100644 index 0000000..83d2741 --- /dev/null +++ b/lib/lafs_dblk.c @@ -0,0 +1,41 @@ +/* + * find or create a dblk for a given offset in a file, + * and return it. + */ + +#include +#include +#include + + +struct lafs_dblk *lafs_dblk(struct lafs_ino *ino, loff_t bnum) +{ + struct lafs_dblk *db; + struct lafs *fs = ino->fs; + int hash = (long)ino; + + hash ^= bnum; + hash ^= hash >> 10; + hash &= (1<htable[hash], b.hash) { + if (db->b.flags & B_Index) + continue; + if (db->b.ino != ino || + db->b.fileaddr != bnum) + continue; + return db; + } + + /* Not in the table, so need to create it */ + db = talloc(ino, struct lafs_dblk); + memset(db, 0, sizeof(*db)); + + db->b.data = talloc_size(db, fs->blocksize); + db->b.fileaddr = bnum; + db->b.ino = ino; + list_add(&db->b.hash, &fs->htable[hash]); + INIT_LIST_HEAD(&db->b.siblings); + INIT_LIST_HEAD(&db->b.leafs); + return db; +} diff --git a/lib/lafs_dev_find.c b/lib/lafs_dev_find.c new file mode 100644 index 0000000..224ee74 --- /dev/null +++ b/lib/lafs_dev_find.c @@ -0,0 +1,14 @@ + +#include + +struct lafs_device * +lafs_dev_find(struct lafs *fs, loff_t virt) +{ + struct lafs_device *dev; + for (dev = fs->devs ; dev; dev = dev->next) + if (virt >= dev->start && + virt < dev->start + dev->size) + return dev; + abort(); + return NULL; +} diff --git a/lib/lafs_dirty_inode.c b/lib/lafs_dirty_inode.c new file mode 100644 index 0000000..1aba6a3 --- /dev/null +++ b/lib/lafs_dirty_inode.c @@ -0,0 +1,15 @@ +/* + * lafs_dirty_inode + * This inode has been changed so we need to ensure it gets + * written out. In particular lafs_inode_fillblock needs to be + * called at some stage. + * + */ + +#include + +void lafs_dirty_inode(struct lafs_ino *ino) +{ + ino->iflags |= I_Dirty; + lafs_sched_blk(&ino->dblock->b); +} diff --git a/lib/lafs_find_dblk.c b/lib/lafs_find_dblk.c new file mode 100644 index 0000000..4208eab --- /dev/null +++ b/lib/lafs_find_dblk.c @@ -0,0 +1,173 @@ + +/* + * Connect this dblk into indexing tree and find + * its phys addr. + * After this call, ->parent must be set, and ->physaddr must + * be correct + * + * FIXME + * For now we only work with indexing directly from the inode + * Separate index blocks are not yet supported + */ + +#include +#include +#include + +static u64 leaf_lookup(unsigned char *buf, int len, u32 target, u32 *nextp); + +int lafs_find_dblk(struct lafs_dblk *db) +{ + struct lafs_ino *ino = db->b.ino; + struct lafs *fs = ino->fs; + + if (db->b.parent) + return 0; + + if (!ino->iblock) + lafs_make_iblock(ino); + + if (ino->depth > 1) + abort(); + + if (ino->filesys != ino || + db->b.fileaddr != 0) { + db->b.parent = ino->iblock; + (void)talloc_reference(db, ino->iblock); + list_add(&db->b.siblings, &ino->iblock->children); + } else + db->b.parent = NULL; + + if (ino->depth == 0) { + db->b.physaddr = 0; + return 0; + } + + db->b.physaddr = leaf_lookup((unsigned char*)ino->dblock->b.data + ino->metadata_size, + fs->blocksize - ino->metadata_size, + db->b.fileaddr, + NULL); + return 0; +} + + +/* + * extract little-endian values out of memory. + * Each function is given a char*, and moves it forwards + */ + +#define decode16(p) ({ unsigned int _a; _a= (unsigned char)*(p++); _a + (((unsigned char)*p++)<<8); }) +#define decode32(p) ({ long _b; _b = decode16(p); _b + ((u32)decode16(p)<<16); }) +#define decode48(p) ({ u64 _c; _c = decode32(p); _c + ((u64)decode16(p)<<32); }) + + +uint64_t leaf_lookup(unsigned char *buf, int len, u32 target, u32 *nextp) +{ + /* buf starts with a 2byte header + * if 1, then 4 byte offset followed by 6byte littleending indirect entries. + * if 2, then 12byte extent entries + */ + uint64_t p; + unsigned char *cp; + int hi,lo; + int elen; + u32 addr, next; + u32 startaddr; + if (nextp) *nextp = NoBlock; + if (buf[1]) return 0; + switch (buf[0]) { + default: p = 0; + break; + + case 1: /* indirect */ + len -= 2+4; + buf += 2; + startaddr = decode32(buf); + + if ( target < startaddr) + return 0; + + next = target; + target -= startaddr; + + if (target*6 + 6 > len) + return 0; + buf += target*6; + p = decode48(buf); + if (nextp) { + /* find the next allocated block */ + uint64_t p2; + + len -= target*6; + len -= 6; + next++; + *nextp = NoBlock; + while (len > 6) { + p2 = decode48(buf); + if (p2) { + *nextp = next; + break; + } + len -= 6; + next++; + } + } + break; + + case 2: /* extent */ + /* 12 byte records: 6byte device, 2 byte length, 4 byte fileaddr */ + + len -= 2; + buf += 2; + lo = 0; + hi = len/12; + while (hi > lo+1) { + int mid = (lo+hi)/2; + int len; + cp = buf; + cp += mid*12 + 6; + + len = decode16(cp); + addr = decode32(cp); + + if (len && addr <= target) + lo = mid; + else + hi = mid; + } + cp = buf; + cp += lo*12 + 8; + + addr = decode32(cp); + + cp = buf; + cp += lo*12+6; + elen = decode16(cp); + if (target < addr || target >= addr + elen) + p = 0; + else { + cp = buf; + cp += lo*12; + p = decode48(cp); + p += target - addr; + } + + if (nextp) { + if (lo == len/12) + *nextp = NoBlock; /* next extent */ + else { + uint64_t p2; + cp = buf; + cp += (lo+1)*12; + p2 = decode48(cp); + len = decode16(cp); + if (len == 0) + *nextp = NoBlock; /* no more meaningful extents*/ + else + *nextp = decode32(cp); + } + } + } + return p; +} + diff --git a/lib/lafs_get_itable.c b/lib/lafs_get_itable.c new file mode 100644 index 0000000..5356371 --- /dev/null +++ b/lib/lafs_get_itable.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include + +/* + * Get the primary inode table for a lafs filesystem. + * If it doesn't exist we need to create it. + */ + +struct lafs_ino *lafs_get_itable(struct lafs *fs) +{ + char *buf; + struct lafs_dblk *blk; + struct lafs_ino *ino; + + if (fs->ss.root) + return fs->ss.root; + + buf = malloc(fs->blocksize); + + if (fs->ss.root_addr) + lafs_read_virtual(fs, buf, fs->ss.root_addr); + else + lafs_inode_init(fs, buf, TypeInodeFile); + + ino = lafs_import_inode_buf(fs, buf, 0, NULL); + + blk = lafs_dblk(ino, 0); + memcpy(blk->b.data, buf, fs->blocksize); + free(buf); + + ino->dblock = blk; + ino->filesys = ino; + + blk->b.flags |= B_Valid; + blk->b.physaddr = fs->ss.root_addr; + blk->my_inode = ino; + if (blk->b.physaddr == 0) + lafs_dirty_blk(&blk->b); + fs->ss.root = ino; + return ino; +} diff --git a/lib/lafs_imap_clr.c b/lib/lafs_imap_clr.c new file mode 100644 index 0000000..2f46fc9 --- /dev/null +++ b/lib/lafs_imap_clr.c @@ -0,0 +1,26 @@ + +/* + * Mark an inode number as being not-used in the inode map. + */ + +#include + +int lafs_imap_clr(struct lafs_ino *ino, int inum) +{ + struct lafs *fs = ino->fs; + struct lafs_dblk *db; + int blknum = (inum / fs->blocksize) / 8; + + if (ino->type != TypeInodeMap) + return -1; + if (blknum >= ino->md.inodemap.size) + return 0; + + db = lafs_dblk(ino, blknum); + lafs_load_dblk(db); + inum -= blknum * fs->blocksize * 8; + db->b.data[inum/8] |= (1<<(inum&7)); + /* FIXME if block is now empty, possibly contract file */ + lafs_dirty_blk(&db->b); + return 0; +} diff --git a/lib/lafs_imap_set.c b/lib/lafs_imap_set.c new file mode 100644 index 0000000..c46af05 --- /dev/null +++ b/lib/lafs_imap_set.c @@ -0,0 +1,36 @@ + +/* + * Mark an inode number as being in-use in the inode map. + * If the number is more than one block beyond end-of-file, + * we fail. + */ + +#include +#include +#include + +int lafs_imap_set(struct lafs_ino *ino, int inum) +{ + struct lafs *fs = ino->fs; + struct lafs_dblk *db; + int blknum = (inum / fs->blocksize) / 8; + + if (ino->type != TypeInodeMap) + return -1; + if (blknum > ino->md.inodemap.size) + return -1; + + printf("imap set %d %d %d\n", inum, blknum, ino->md.inodemap.size); + db = lafs_dblk(ino, blknum); + lafs_load_dblk(db); + if (blknum == ino->md.inodemap.size) { + memset(db->b.data, 0xff, fs->blocksize); + ino->md.inodemap.size++; + lafs_dirty_inode(ino); + } + inum -= blknum * fs->blocksize * 8; + db->b.data[inum/8] &= ~ (1<<(inum&7)); + /* FIXME if block is now empty, punch a hole */ + lafs_dirty_blk(&db->b); + return 0; +} diff --git a/lib/lafs_import_inode.c b/lib/lafs_import_inode.c new file mode 100644 index 0000000..c4ca4b9 --- /dev/null +++ b/lib/lafs_import_inode.c @@ -0,0 +1,11 @@ +#include + +struct lafs_ino *lafs_import_inode(struct lafs_dblk *db) +{ + struct lafs_ino *parent = db->b.ino; + struct lafs_ino *ino = lafs_import_inode_buf(parent->fs, db->b.data, db->b.fileaddr, parent); + + ino->dblock = db; + db->my_inode = ino; + return ino; +} diff --git a/lib/lafs_import_inode_buf.c b/lib/lafs_import_inode_buf.c new file mode 100644 index 0000000..3ba13b0 --- /dev/null +++ b/lib/lafs_import_inode_buf.c @@ -0,0 +1,133 @@ + +/* + * create a new inode using the disk-image info in the given + * buf. Inode is in the inodefile 'parent'. + * This should not be used directly. It is only separate from + * lafs_import_inode so that lafs_get_itable can break a loop. + */ + +#include +#include +#include + +static int inode_load(struct lafs_ino *ino, struct la_inode *lai); + + +struct lafs_ino *lafs_import_inode_buf(struct lafs *fs, + char *buf, int inum, + struct lafs_ino *parent) +{ + struct lafs_ino *ino; + + ino = talloc((void*)parent ?: (void*)fs, struct lafs_ino); + + memset(ino, 0, sizeof(*ino)); + + ino->fs = fs; + ino->inum = inum; + ino->filesys = parent; + + inode_load(ino, (struct la_inode *)buf); + /* FIXME callers of this should reparent it to themselves. */ + + return ino; +} + + +static int inode_load(struct lafs_ino *ino, struct la_inode *lai) +{ + if (lai->filetype == 0) { + ino->type = 0; + return 0; /* no inode here */ + } + + ino->cblocks = __le32_to_cpu(lai->data_blocks); + ino->ciblocks = __le32_to_cpu(lai->index_blocks); + ino->generation = __le16_to_cpu(lai->generation); + ino->trunc_gen = lai->trunc_gen; + ino->flags = lai->flags; + ino->type = lai->filetype; + ino->metadata_size = __le16_to_cpu(lai->metadata_size); + ino->depth = lai->depth; + + switch(ino->type) { + case TypeInodeFile: + { + struct fs_md *i = &ino->md.fs; + struct fs_metadata *l = &lai->metadata[0].fs; + i->usagetable = __le16_to_cpu(l->snapshot_usage_table); + i->update_time = __le64_to_cpu(l->update_time); + i->cblocks_used = __le64_to_cpu(l->blocks_used); + i->blocks_allowed = __le64_to_cpu(l->blocks_allowed); + i->creation_age = __le64_to_cpu(l->creation_age); + i->inodes_used = __le32_to_cpu(l->inodes_used); + i->quota_inums[0] = __le32_to_cpu(l->quota_inodes[0]); + i->quota_inums[1] = __le32_to_cpu(l->quota_inodes[1]); + i->quota_inums[2] = __le32_to_cpu(l->quota_inodes[2]); + i->quota_inodes[0] = i->quota_inodes[1] = i->quota_inodes[2] = NULL; + memcpy(i->name, l->name, 64); + i->name[64] = 0; + break; + } + case TypeInodeMap: + { + struct inodemap_md *m = &ino->md.inodemap; + struct inodemap_metadata *s = &lai->metadata[0].inodemap; + m->size = __le32_to_cpu(s->size); + break; + } + case TypeSegmentMap: + { + struct su_md *m = &ino->md.segmentusage; + struct su_metadata *s = &lai->metadata[0].segmentusage; + m->table_size = __le32_to_cpu(s->table_size); + break; + } + case TypeQuota: + { + struct quota_md *m = &ino->md.quota; + struct quota_metadata *s = &lai->metadata[0].quota; + m->gracetime = __le32_to_cpu(s->gracetime); + m->graceunits = __le32_to_cpu(s->graceunits); + break; + } + case TypeOrphanList: + case TypeAccessTime: + break; + default: /* TypeBase or larger */ + { + struct file_md *i = &ino->md.file; + struct file_metadata *l = &lai->metadata[0].file; + struct dir_metadata *d = &lai->metadata[0].dir; + struct special_metadata *s = &lai->metadata[0].special; + + if (ino->type < TypeBase) abort(); + i->flags = __le16_to_cpu(l->flags); + i->mode = __le16_to_cpu(l->mode); + i->uid = __le32_to_cpu(l->userid); + i->gid = __le32_to_cpu(l->groupid); + i->treeid = __le32_to_cpu(l->treeid); + i->creationtime = __le64_to_cpu(l->creationtime); + i->modifytime = __le64_to_cpu(l->modifytime); + i->ctime = __le64_to_cpu(l->ctime); + i->i_accesstime = __le64_to_cpu(l->accesstime); + i->accesstime = i->i_accesstime; /* FIXME load from accesstime file */ + i->size = __le64_to_cpu(l->size); + i->parent = __le32_to_cpu(l->parent); + i->linkcount = __le32_to_cpu(l->linkcount); + + if (ino->type == TypeDir) + i->seed = __le32_to_cpu(d->hash_seed); + if (ino->type == TypeSpecial) { + i->major = __le32_to_cpu(s->major); + i->minor = __le32_to_cpu(s->minor); + } + + INIT_LIST_HEAD(&i->dirorphan); + break; + } + } + /* FIXME should I sanity-check anything? */ + return 1; + +} diff --git a/lib/lafs_incorporate.c b/lib/lafs_incorporate.c new file mode 100644 index 0000000..c780cf7 --- /dev/null +++ b/lib/lafs_incorporate.c @@ -0,0 +1,59 @@ + +/* + * Given an index block with some ->uninc blocks, add all the addresses + * of those blocks into the index info. + * + * FIXME + * For now we only support Indirect index in the inode as that is enough + * for mkfs + */ + +#include +#include +#include + +#define encode16(p,n) ({ *(p++) = (n)&255; *(p++) = ((n)>>8) & 255; }) +#define encode32(p,n) ({ encode16(p,n); encode16(p, ((n)>>16)); }) +#define encode48(p,n) ({ encode32(p,n); encode16(p, ((n)>>32)); }) + +void lafs_incorporate(struct lafs_iblk *ib) +{ + struct lafs *fs = ib->b.ino->fs; + unsigned char *ip; + int max_addr; + + if (ib->uninc == NULL) + return; + + if (ib->depth != 1) + abort(); + + + ip = (void*)(ib->b.ino->dblock->b.data + ib->b.ino->metadata_size); + if (*(u16*)ip != __cpu_to_le16(IBLK_INDIRECT)) + abort(); + ip += 2; + + max_addr = fs->blocksize - ib->b.ino->metadata_size; + max_addr -= 2; /* for type field */ + max_addr /= 6; /* 6 bytes per address */ + + while (ib->uninc) { + struct lafs_blk *b = ib->uninc; + int addr; + unsigned char *ip2 = ip; + + ib->uninc = b->chain; + b->chain = NULL; + + addr = b->fileaddr - ib->b.fileaddr; + if (addr >= max_addr) + abort(); + ip2 += addr * 6; + encode48(ip2, b->physaddr); + } + printf("incorp %d/%ld\n", ib->b.ino->dblock->b.ino->inum, ib->b.ino->dblock->b.fileaddr); + lafs_dirty_blk(&ib->b); +} + + diff --git a/lib/lafs_inode_fillblock.c b/lib/lafs_inode_fillblock.c new file mode 100644 index 0000000..90b5da6 --- /dev/null +++ b/lib/lafs_inode_fillblock.c @@ -0,0 +1,100 @@ + +/* + * Fill a data buffer with info to look like an on-disk inode + */ + +#include +#include +#include +#include + +void lafs_inode_fillblock(struct lafs_ino *ino, char *buf) +{ + struct la_inode *lino = (void*)buf; + + lino->data_blocks = __cpu_to_le32(ino->cblocks); + lino->index_blocks = __cpu_to_le32(ino->ciblocks); + lino->generation = __cpu_to_le16(ino->generation); + lino->metadata_size = __cpu_to_le16(ino->metadata_size); + lino->depth = ino->depth; + lino->trunc_gen = ino->trunc_gen; + lino->filetype = ino->type; + lino->flags = ino->flags; + + printf("FILL %d\n", ino->type); + switch(ino->type) { + case TypeInodeFile: + { + struct fs_metadata *sfs = &lino->metadata[0].fs; + struct fs_md *mfs = &ino->md.fs; + sfs->update_time = __cpu_to_le64(mfs->update_time); + sfs->blocks_used = __cpu_to_le64(mfs->cblocks_used); + sfs->blocks_allowed = __cpu_to_le64(mfs->blocks_allowed); + sfs->creation_age = __cpu_to_le64(mfs->creation_age); + sfs->inodes_used = __cpu_to_le32(mfs->inodes_used); + sfs->snapshot_usage_table = __cpu_to_le16(mfs->usagetable); + sfs->quota_inodes[0] = __cpu_to_le32(mfs->quota_inums[0]); + sfs->quota_inodes[1] = __cpu_to_le32(mfs->quota_inums[1]); + sfs->quota_inodes[2] = __cpu_to_le32(mfs->quota_inums[2]); + memcpy(sfs->name, mfs->name, 64); + break; + } + case TypeInodeMap: + { + struct inodemap_metadata *sim = &lino->metadata[0].inodemap; + struct inodemap_md *mim = &ino->md.inodemap; + sim->size = __cpu_to_le32(mim->size); + break; + } + case TypeSegmentMap: + { + struct su_metadata *s = &lino->metadata[0].segmentusage; + struct su_md *m = &ino->md.segmentusage; + s->table_size = __cpu_to_le32(m->table_size); + break; + } + case TypeQuota: + { + struct quota_metadata *s = &lino->metadata[0].quota; + struct quota_md *m = &ino->md.quota; + s->gracetime = __cpu_to_le32(m->gracetime); + s->graceunits = __cpu_to_le32(m->graceunits); + break; + } + case TypeOrphanList: + case TypeAccessTime: + break; + default: /* external-file */ + { + struct file_metadata *sfl = & lino->metadata[0].file; + struct file_md *mfl = & ino->md.file; + struct dir_metadata *sdr = & lino->metadata[0].dir; + struct special_metadata *ssp = & lino->metadata[0].special; + + if (ino->type < TypeBase) abort(); + sfl->flags = __cpu_to_le16(mfl->flags); + sfl->mode = __cpu_to_le16(mfl->mode); + sfl->userid = __cpu_to_le32(mfl->uid); + sfl->groupid = __cpu_to_le32(mfl->gid); + sfl->treeid = __cpu_to_le32(mfl->treeid); + sfl->creationtime = __cpu_to_le64(mfl->creationtime); + sfl->modifytime = __cpu_to_le64(mfl->modifytime); + sfl->ctime = __cpu_to_le64(mfl->ctime); + sfl->accesstime = __cpu_to_le64(mfl->i_accesstime); + /* FIXME should we zero the accesstime file at this point? */ + sfl->size = __cpu_to_le64(mfl->size); + sfl->parent = __cpu_to_le32(mfl->parent); + sfl->linkcount = __cpu_to_le32(mfl->linkcount); + sfl->attrinode = 0; + + if (ino->type == TypeDir) + sdr->hash_seed = __cpu_to_le32(mfl->seed); + if (ino->type == TypeSpecial) { + ssp->major = __cpu_to_le32(mfl->major); + ssp->minor = __cpu_to_le32(mfl->minor); + } + + break; + } + } +} diff --git a/lib/lafs_inode_init.c b/lib/lafs_inode_init.c new file mode 100644 index 0000000..b1eac8f --- /dev/null +++ b/lib/lafs_inode_init.c @@ -0,0 +1,76 @@ + +/* + * Fill out a block to look like an on-disk inode + */ + +#include +#include +#include +#include +#include + +void lafs_inode_init(struct lafs *fs, char * buf, int type) +{ + struct lafs_ino ino; + unsigned char *ip; + + memset(&ino, 0, sizeof(ino)); + + ino.generation = random(); + ino.trunc_gen = random() & 0xff; + ino.flags = 0; + ino.type = type; + + ino.depth = 1; + memset(&ino.md, 0, sizeof(ino.md)); + switch(type) { + case TypeInodeFile: + ino.metadata_size = sizeof(struct la_inode) + sizeof(struct fs_metadata); + ino.depth = 1; /*never depth 0 for inode files */ + ino.md.fs.update_time = time(0); /* FIXME */ + break; + + case TypeInodeMap: + ino.metadata_size = sizeof(struct la_inode) + sizeof(struct inodemap_metadata); + ino.md.inodemap.size = 0; + break; + + case TypeSegmentMap: + ino.metadata_size = sizeof(struct la_inode) + sizeof(struct su_metadata); + ino.md.segmentusage.table_size = 0; /* FIXME */ + break; + + case TypeQuota: + ino.metadata_size = sizeof(struct la_inode) + sizeof(struct quota_metadata); + ino.md.quota.gracetime = 0; + ino.md.quota.graceunits = 0; + break; + + case TypeOrphanList: + case TypeAccessTime: + ino.metadata_size = sizeof(struct la_inode); + break; + + default: /* external file */ + if (type < TypeBase) abort(); + ino.metadata_size = sizeof(struct la_inode) + sizeof(struct file_metadata); + if (type == TypeDir) + ino.metadata_size = sizeof(struct la_inode) + sizeof(struct dir_metadata); + ino.md.file.flags = 0; + ino.md.file.mode = 0600; + ino.md.file.creationtime = time(0); + ino.md.file.modifytime = time(0); + ino.md.file.ctime = time(0); + ino.md.file.accesstime = time(0); + ino.md.file.i_accesstime = ino.md.file.accesstime; + + ino.md.file.seed = (random() & ~7) | 1; + INIT_LIST_HEAD(&ino.md.file.dirorphan); + break; + } + + ip = (void*)(buf + ino.metadata_size); + *(u16*)ip = __cpu_to_le16(IBLK_INDIRECT); + + lafs_inode_fillblock(&ino, buf); +} diff --git a/lib/lafs_load_dblk.c b/lib/lafs_load_dblk.c new file mode 100644 index 0000000..8cae7a6 --- /dev/null +++ b/lib/lafs_load_dblk.c @@ -0,0 +1,35 @@ + +/* + * load the content of this dblk from the filesystem, or maybe + * from the inode + */ +#include +#include + +int lafs_load_dblk(struct lafs_dblk *db) +{ + struct lafs_ino *ino = db->b.ino; + struct lafs *fs = ino->fs; + struct lafs_dblk *inodb; + int offset; + + if (db->b.flags & B_Valid) + return 0; + + lafs_find_dblk(db); + + db->b.flags |= B_Valid; + if (db->b.physaddr) + return lafs_read_virtual(fs, db->b.data, + db->b.physaddr); + + memset(db->b.data, 0, fs->blocksize); + if (ino->depth > 0) + return 0; + + /* content is in the inode dblock */ + inodb = ino->dblock; + offset = ino->metadata_size; + memcpy(db->b.data, inodb->b.data + offset, fs->blocksize - offset); + return 0; +} diff --git a/lib/lafs_make_iblock.c b/lib/lafs_make_iblock.c new file mode 100644 index 0000000..630caa5 --- /dev/null +++ b/lib/lafs_make_iblock.c @@ -0,0 +1,29 @@ + +/* + * Allocate an iblock for the given inode + */ +#include +#include +#include +#include + +void lafs_make_iblock(struct lafs_ino *ino) +{ + struct lafs_iblk *ib; + if (ino->iblock) + return; + + ib = talloc(ino, struct lafs_iblk); + memset(ib, 0, sizeof(*ib)); + + ib->depth = ino->depth; + INIT_LIST_HEAD(&ib->children); + ib->b.flags |= B_Index | B_InoIdx; + ib->b.ino = ino; + INIT_LIST_HEAD(&ib->b.hash); + ib->b.parent = ino->dblock->b.parent; + INIT_LIST_HEAD(&ib->b.siblings); + INIT_LIST_HEAD(&ib->b.leafs); + + ino->iblock = ib; +} diff --git a/lib/lafs_new.c b/lib/lafs_new.c new file mode 100644 index 0000000..0e89fbc --- /dev/null +++ b/lib/lafs_new.c @@ -0,0 +1,40 @@ + +#include +#include +#include + +int lafs_new(struct lafs *fs, int blockbytes) +{ + int fd; + int n; + + if (fs->blocksize != 0) + return -1; + + if (blockbytes < 512 || + blockbytes > 4096 || + (blockbytes & (blockbytes-1)) != 0) + return -1; + + + fd = open("/dev/urandom", O_RDONLY); + if (fd < 0) + return -1; + + n = read(fd, fs->uuid, 16); + close(fd); + if (n != 16) + return -1; + + fs->blocksize = blockbytes; + + fs->seq = 1; + fs->statesize = 1024; + + fs->wc[0].seq = 1; + fs->wc[1].seq = 1; + + fs->youth_next = 1; + + return 0; +} diff --git a/lib/lafs_new_segment.c b/lib/lafs_new_segment.c new file mode 100644 index 0000000..2930b00 --- /dev/null +++ b/lib/lafs_new_segment.c @@ -0,0 +1,62 @@ + +/* allocate a new segment at assign it to a lafs_cluster + */ + +#include + +static void cluster_reset(struct lafs *fs, struct lafs_cluster *wc) +{ + wc->remaining = dev_by_num(fs, wc->seg.dev)->segment_size; + wc->chead_blocks = 1; + wc->remaining--; + wc->chead_size = sizeof(struct cluster_head); +} + +void set_youth(struct lafs *fs, int dev, loff_t seg, int youth) +{ + struct lafs_device *dv; + struct lafs_dblk *db; + uint16_t *p; + loff_t addr; + + dv = dev_by_num(fs, dev); + addr = seg / (fs->blocksize/2); + + db = lafs_dblk(dv->segsum, addr); + lafs_load_dblk(db); + p = (void*)db->b.data; + p[seg % (fs->blocksize/2)] = __cpu_to_le16(youth); + lafs_dirty_blk(&db->b); +} + +int lafs_new_segment(struct lafs *fs, int cnum) +{ + int dev; + loff_t seg; + struct lafs_cluster *wc = &fs->wc[cnum]; + + if (fs->free_head == fs->free_tail) + /* list is empty */ + return 0; + + dev = fs->freelist[fs->free_head].dev; + seg = fs->freelist[fs->free_head].seg; + + fs->free_head++; + if (fs->free_head > 128) + fs->free_head -= 128; + + wc->seg.dev = dev; + wc->seg.num = seg; + wc->seg.st_table = 0; + wc->seg.st_row = 0; + wc->seg.table = wc->seg.nxt_table = wc->seg.st_table; + wc->seg.row = wc->seg.nxt_row = wc->seg.st_row; + wc->seg.col = 0; + + cluster_reset(fs, wc); + + /* Need to set the youth for this segment */ + set_youth(fs, dev, seg, fs->youth_next++); + return 1; +} diff --git a/lib/lafs_read_virtual.c b/lib/lafs_read_virtual.c new file mode 100644 index 0000000..7651a68 --- /dev/null +++ b/lib/lafs_read_virtual.c @@ -0,0 +1,24 @@ +/* + * read a block given a virtual address + */ +#define _GNU_SOURCE +#define _FILE_OFFSET_BITS 64 +#include +#include + + +int lafs_read_virtual(struct lafs *fs, char *buf, loff_t addr) +{ + struct lafs_device *d = lafs_dev_find(fs, addr); + int n; + + if (!d) + return -1; + lseek64(d->fd, (addr - d->start) * fs->blocksize, SEEK_SET); + n = read(d->fd, buf, fs->blocksize); + + + if (n == fs->blocksize) + return 0; + return -1; +} diff --git a/lib/lafs_sched_blk.c b/lib/lafs_sched_blk.c new file mode 100644 index 0000000..a00fd3d --- /dev/null +++ b/lib/lafs_sched_blk.c @@ -0,0 +1,28 @@ + +/* + * schedule a block for writeout in the next checkpoint + */ + +#include + +#include + +int lafs_sched_blk(struct lafs_blk *blk) +{ + struct lafs *fs = blk->ino->fs; + + if (blk->flags & B_Sched) + return 0; + + if (!(blk->flags & B_Index)) { + struct lafs_dblk *dblk = container_of(blk, struct lafs_dblk, b); + if (lafs_find_dblk(dblk) < 0) + return -1; + } + blk->flags |= B_Sched; + if (blk->parent) + blk->parent->sched_cnt++; + printf("add %p\n", blk); + list_add(&blk->leafs, &fs->leafs); + return 0; +} diff --git a/lib/lafs_segment_count.c b/lib/lafs_segment_count.c new file mode 100644 index 0000000..930c7ed --- /dev/null +++ b/lib/lafs_segment_count.c @@ -0,0 +1,75 @@ + +/* lafs_segment_count + * adjust the usage count for the segment containing a given block + */ + +#include + +static void delay_update(struct lafs *fs, int dev, loff_t seg, int diff) +{ + struct lafs_delayed *d = fs->delayed; + + while (d && (d->dev != dev || d->seg != seg)) + d = d->next; + if (!d) { + d = malloc(sizeof(*d)); + d->next = fs->delayed; + fs->delayed = d; + d->dev = dev; + d->seg = seg; + d->diff = 0; + } + d->diff += diff; +} + +void segment_count(struct lafs *fs, int dev, loff_t seg, int diff) +{ + struct lafs_device *dv; + struct lafs_dblk *db; + uint16_t *p; + int cnt; + loff_t addr; + + dv = dev_by_num(fs, dev); + addr = dv->tablesize + seg / (fs->blocksize/2); + + db = lafs_dblk(dv->segsum, addr); + lafs_load_dblk(db); + p = (void*)db->b.data; + cnt = __le16_to_cpu(p[seg % (fs->blocksize/2)]); + cnt += diff; + p[seg % (fs->blocksize/2)] = __cpu_to_le16(cnt); + lafs_dirty_blk(&db->b); +} + + +void lafs_segment_count(struct lafs *fs, loff_t addr, int diff) +{ + int dev; + loff_t seg; + loff_t offset; + + virttoseg(fs, addr, &dev, &seg, &offset); + + if (fs->flags & LAFS_DELAY_UPDATES) { + delay_update(fs, dev, seg, diff); + return; + } + segment_count(fs, dev, seg, diff); +} + +void lafs_segment_apply_delayed(struct lafs *fs) +{ + struct lafs_delayed *delayed = fs->delayed; + + if (fs->flags & LAFS_DELAY_UPDATES) + abort(); + fs->delayed = NULL; + + while (delayed) { + struct lafs_delayed *d = delayed; + delayed = d->next; + segment_count(fs, d->dev, d->seg, d->diff); + free(d); + } +} diff --git a/lib/lafs_summary_update.c b/lib/lafs_summary_update.c new file mode 100644 index 0000000..0fd39ca --- /dev/null +++ b/lib/lafs_summary_update.c @@ -0,0 +1,55 @@ + +/* + * Updates summary info for this newly written block. + * This includes: + * block count in the inode + * block count in the fs + * quotas + * segment usage. + */ + +#include + +void lafs_summary_update(struct lafs_ino *ino, + loff_t oldaddr, loff_t newaddr, + int is_index) +{ + struct lafs *fs = ino->fs; + int diff; + + if (oldaddr && newaddr) { + /* Not a new allocation */ + lafs_segment_count(fs, oldaddr, -1); + lafs_segment_count(fs, newaddr, 1); + return; + } + if (!oldaddr && !newaddr) + /* nothing changing! */ + return; + + if (oldaddr) + diff = -1; + else + diff = 1; + + if (is_index) + ino->ciblocks += diff; + else + ino->cblocks += diff; + + if (!is_index) + if (diff > 0) + ino->ablocks--; + + ino = ino->filesys; + ino->md.fs.cblocks_used++; + if (!is_index) + if (diff > 0) + ino->md.fs.ablocks_used--; + + if (oldaddr) + lafs_segment_count(fs, oldaddr, -1); + if (newaddr) + lafs_segment_count(fs, newaddr, 1); +} + diff --git a/lib/lafs_write_dev.c b/lib/lafs_write_dev.c new file mode 100644 index 0000000..77e9ea4 --- /dev/null +++ b/lib/lafs_write_dev.c @@ -0,0 +1,61 @@ + +#define _GNU_SOURCE +#define _FILE_OFFSET_BITS 64 +#include +#include +#include + +static int size_bits(long long size) +{ + int bits = 0; + while (size > 1) { + size >>= 1; + bits++; + } + return bits; +} + +int lafs_write_dev(struct lafs_device *dev) +{ + char buf[LAFS_DEVBLK_SIZE]; + struct lafs_dev *pd = (void*)buf; + struct lafs *fs = dev->fs; + int i; + uint32_t csum; + + memset(buf, 0, sizeof(buf)); + + memcpy(pd->idtag, "LaFS-DeviceBlock", 16); + memset(pd->version, ' ', 16); + + memcpy(pd->uuid, fs->uuid, 16); + dev->seq++; + pd->seq = __cpu_to_le32(dev->seq); + pd->ctime = lafs_encode_timeval(&dev->ctime); + pd->start = __cpu_to_le64(dev->start); + pd->size = __cpu_to_le64(dev->size); + for (i=0; i<2; i++) + pd->devaddr[i] = __cpu_to_le64(dev->devaddr[i]); + for (i=0; i<4; i++) + pd->stateaddr[i] = __cpu_to_le64(dev->stateaddr[i]); + + pd->statebits = size_bits(fs->statesize); + pd->blockbits = size_bits(fs->blocksize); + pd->width = __cpu_to_le16(dev->width); + pd->stride = __cpu_to_le32(dev->stride); + pd->segment_size = __cpu_to_le32(dev->segment_size); + pd->segment_offset = __cpu_to_le32(dev->segment_offset); + pd->segment_count = __cpu_to_le32(dev->segment_count); + pd->usage_inum = __cpu_to_le32(dev->usage_inum); + pd->level = __cpu_to_le32(1); + + csum = crc32(0, (uint32_t*)buf, LAFS_DEVBLK_SIZE); + pd->checksum = csum; + + for (i=0; i<2; i++) { + lseek64(dev->fd, dev->devaddr[i], SEEK_SET); + write(dev->fd, buf, LAFS_DEVBLK_SIZE); + } + + return 0; +} diff --git a/lib/lafs_write_state.c b/lib/lafs_write_state.c new file mode 100644 index 0000000..d564fca --- /dev/null +++ b/lib/lafs_write_state.c @@ -0,0 +1,43 @@ + +/* + * Write the state blocks twice to each device. + */ + +#define _GNU_SOURCE +#define _FILE_OFFSET_BITS 64 +#include +#include +#include + +int lafs_write_state(struct lafs *fs) +{ + char buf[4096]; + struct lafs_state *st = (void*)buf; + struct lafs_device *dev; + + memset(buf, 0, 4096); + + fs->seq++; + + memcpy(st->idtag, "LaFS-State-Block", 16); + memcpy(st->uuid, fs->uuid, 16); + memset(st->version, ' ', 16); + + st->seq = __cpu_to_le32(fs->seq); + st->nextyouth = __cpu_to_le16(fs->youth_next); + st->checkpointcluster = __cpu_to_le64(fs->checkpoint_cluster); + st->root_inodes[0] = __cpu_to_le64(fs->ss.root_addr); + st->maxsnapshot = 1; + st->devices = __cpu_to_le32(fs->devices); + + st->checksum = crc32(0, (uint32_t *)buf, fs->blocksize); + + for (dev = fs->devs ; dev ; dev = dev->next) { + int n; + for (n = (fs->seq & 1); n < 4 ; n += 2) { + lseek64(dev->fd, dev->stateaddr[n], SEEK_SET); + write(dev->fd, buf, fs->statesize); + } + } + return 1; +} diff --git a/lib/lafs_write_virtual.c b/lib/lafs_write_virtual.c new file mode 100644 index 0000000..872ed38 --- /dev/null +++ b/lib/lafs_write_virtual.c @@ -0,0 +1,24 @@ +/* + * write a block given a virtual address + */ +#define _GNU_SOURCE +#define _FILE_OFFSET_BITS 64 +#include +#include + + +int lafs_write_virtual(struct lafs *fs, char *buf, loff_t addr) +{ + struct lafs_device *d = lafs_dev_find(fs, addr); + int n; + + if (!d) + return -1; + lseek64(d->fd, (addr - d->start) * fs->blocksize, SEEK_SET); + n = write(d->fd, buf, fs->blocksize); + + + if (n == fs->blocksize) + return 0; + return -1; +} diff --git a/tools/Makefile b/tools/Makefile new file mode 100644 index 0000000..c54b1e8 --- /dev/null +++ b/tools/Makefile @@ -0,0 +1,31 @@ +# +# lafs-utils - tools for working wit LAFS filesystems. +# +# Copyright (C) 2010 Neil Brown +# +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# Author: Neil Brown +# Email: + +CPPFLAGS = -I../include +CFLAGS = -Wall -Werror -g +LDFLAGS = -L../lib +LDLIBS = -llafs -ltalloc + +all : mkfs.lafs + +mkfs.lafs : mkfs.lafs.o ../lib/liblafs.a diff --git a/tools/mkfs.lafs.c b/tools/mkfs.lafs.c new file mode 100644 index 0000000..2986d58 --- /dev/null +++ b/tools/mkfs.lafs.c @@ -0,0 +1,387 @@ +/* + * mkfs.lafs - Create an empty LAFS filesystem + * + * Copyright (C) 2010 NeilBrown + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * Author: Neil Brown + * Email: + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* + * A new filesystem must contain: + * inode 0 - TypeInodeFile with all the files listed here + * inode 1 - TypeInodeMap - empty + * inode 2 - TypeDir - empty + * inode 8 - TypeOrphanList - empty + * inode 16 - TypeSegmentMap - youth block and usage block for first segment + * + * These can all be in one checkpoint and could be in a single + * write-cluster, though that could be a bit awkward. + * + * As well as the write-clusters we need device-blocks and state-blocks. + * + * Configurable values are: + * - block size, 512 to 4096 -b or --block-size + * - segment size - up to 64k, but multiple of width --segment-size + * - width - spindles across which device is striped --width + * - stride - chunk-size when device is striped --stride + * + */ + +enum { + opt_segsize = 1000, + opt_width, + opt_stride, +}; + +char short_options[] = "-b:Vvh"; +struct option long_options[] = { + {"block-size", 1, 0, 'b'}, + {"segment-size", 1, 0, opt_segsize}, + {"width", 1, 0, opt_width}, + {"stride", 1, 0, opt_stride}, + {"version", 0, 0, 'V'}, + {"verbose", 0, 0, 'v'}, + {"help", 0, 0, 'h'}, + {0, 0, 0, 0} +}; + +char version_text[] = "mkfs.lafs - unreleased\n"; + +char help_text[] = + "mkfs.lafs: create a lafs filesystem\n" + ; + +char usage_text[] = + "Usage: mkfs.lafs [options] device-name\n" + " Options:\n" + " --block-size (-b) size of basic block, up to 4096 (e.g. 2K)\n" + " --segment-size size of segments, up to 65536 blocks (e.g. 32M)\n" + " --width number of members of a striped device (e.g. 3)\n" + " --stride chunk size of a striped device (e.g. 64K)\n" + " --help (-h) This help message\n" + " --version (-V) Report version information\n" + " --verbose (-v) Be more verbose\n" + ; + +static void get_size(long *valp, char *arg, char *name) +{ + long val; + long scale = 1; + char *endp; + + if (*valp != 0) { + fprintf(stderr, "mkfs.lafs: %s has already been given, value \"%s\" not permitted.\n", + name, arg); + exit(2); + } + + val = strtol(arg, &endp, 0); + + if (endp == arg) { + fprintf(stderr, "mkfs.lafs: Unrecognised size \"%s\" for %s\n", arg, name); + exit(2); + } + + switch(*endp) { + case 'k': + case 'K': + scale = 1024; + break; + case 'M': + scale = 1024*1024; + break; + case 'G': + scale = 1024*1024*1024; + break; + case '\0': + scale = 1; + break; + default: + fprintf(stderr, "mkfs.lafs: unrecognised modifier \"%s\" for %s\n", endp, name); + exit(2); + } + if (val == 0) { + fprintf(stderr, "mkfs.lafs: 0 is not a valid number for %s\n", name); + exit(2); + } + val *= scale; + *valp = val; +} + +void get_num(int *valp, char *arg, char *name) +{ + long val; + char *endp; + + if (*valp != 0) { + fprintf(stderr, "mkfs.lafs: %s has already been given, value \"%s\" not permitted.\n", + name, arg); + exit(2); + } + + val = strtol(arg, &endp, 0); + if (endp == arg || *endp) { + fprintf(stderr, "mkfs.lafs: Unrecognised number \"%s\" for %s\n", arg, name); + exit(2); + } + if (val == 0) { + fprintf(stderr, "mkfs.lafs: 0 is not a valid number for %s\n", name); + exit(2); + } + *valp = val; +} + +int is_pow2(long num) +{ + return (num & (num-1)) == 0; +} + + +void validate_parameters(long *block_bytes, long *segment_bytes, long *stride_bytes, int *width) +{ + + if (*block_bytes == 0) + *block_bytes = 1024; + + if (*block_bytes < 512 || *block_bytes > 4096 || + !is_pow2(*block_bytes)) { + fprintf(stderr, "lafs.mkfs: block size %ld is illegal - must be power of 2 in range 512..4096\n", + *block_bytes); + exit(2); + } + + if (*width == 0) + *width = 1; + if (*width < 1 || *width > 128) { + fprintf(stderr, "lafs.mkfs: width %d is illegal - must be in range 1..128\n", *width); + exit(2); + } + + if (*stride_bytes == 0) + *stride_bytes = *block_bytes; + + if (*stride_bytes < 0 || (*stride_bytes % *block_bytes) != 0) { + fprintf(stderr, "lafs.mkfs: stride %ld is illegal - must be a multiple of block size\n", + *stride_bytes); + exit(2); + } + + /* segment size must be a multiple of block size and of width. + * It must either be a multiple or a sub-multiple of stride size + */ + if (*segment_bytes == 0) { + /* choose maximum size ?? */ + long seg; + long blocks = 65536 / *width; + blocks *= *width; + seg = *block_bytes * blocks; + if (*stride_bytes < seg) { + seg /= *stride_bytes; + seg *= *stride_bytes; + } else { + if ((*stride_bytes % seg) != 0) { + fprintf(stderr, "lafs.mkfs: explicit segment size must be given with large stride\n"); + exit(2); + } + } + *segment_bytes = seg; + } + + if (*segment_bytes % *block_bytes) { + fprintf(stderr, "lafs.mkfs: segment size must be a multiple of block size\n"); + exit(2); + } + if (*segment_bytes % *width) { + fprintf(stderr, "lafs.mkfs: segment size must be a multiple of width\n"); + exit(2); + } + if (*segment_bytes > *stride_bytes && + (*segment_bytes % *stride_bytes)) { + fprintf(stderr, "lafs.mkfs: segment size must be a multiple of stride size\n"); + exit(2); + } + if (*segment_bytes < *stride_bytes && + (*stride_bytes % *segment_bytes)) { + fprintf(stderr, "lafs.mkfs: segment size must be a divisor of stride size\n"); + exit(2); + } +} + +int open_device(char *devname, long long *device_bytes) +{ + /* must be able to get an exclusive open on the device and its size + * must be non-trivial + * + */ + int fd; + struct stat stb; + unsigned long long size; + + fd = open(devname, O_RDWR|O_EXCL); + if (fd < 0) + fprintf(stderr, "mkfs.lafs: cannot open device %s: %s\n", + devname, strerror(errno)); + else if (fstat(fd, &stb) < 0 || + (stb.st_mode & S_IFMT) != S_IFBLK) + fprintf(stderr, "mkfs.lafs: %s is not a block device\n", + devname); + else if (ioctl(fd, BLKGETSIZE64, &size) != 0) + fprintf(stderr, "mkfs.lafs: Cannot get size of %s\n", + devname); + else if (size < 64*1024) + fprintf(stderr, "mkfs.lafs: %s is too small for a LAFS filesystem\n", + devname); + else { + *device_bytes = size; + return fd; + } + exit(2); +} + +int main(int argc, char *argv[]) +{ + int verbose = 0; + long block_bytes = 0; + long segment_bytes = 0; + int width = 0; + long stride_bytes = 0; + long long device_bytes; + char *devname = NULL; + int opt; + int dev_fd; + struct lafs *lafs; + struct lafs_device *dev; + struct lafs_ino *ifile, *imfile, *rootdir, *orphans, *segmap; + + while ((opt = getopt_long(argc, argv, + short_options, long_options, + NULL)) != -1) { + switch(opt) { + case 'h': + fputs(help_text, stdout); + fputs(usage_text, stdout); + exit(0); + case 'V': + fputs(version_text, stdout); + exit(0); + case 'v': + verbose++; + break; + case 'b': + get_size(&block_bytes, optarg, "block size"); + break; + case opt_segsize: + get_size(&segment_bytes, optarg, "segment size"); + break; + case opt_stride: + get_size(&stride_bytes, optarg, "stride size"); + break; + case opt_width: + get_num(&width, optarg, "device width"); + break; + + case 1: + if (devname == NULL) { + devname = optarg; + break; + } + fprintf(stderr, "mkfs.lafs: multiple device names not supported: %s and %s\n", + devname, optarg); + exit(2); + + case '?': + default: + fputs(usage_text, stderr); + exit(2); + } + } + + if (devname == NULL) { + fputs("mkfs.lafs: no device name given\n", stderr); + fputs(usage_text, stderr); + exit(2); + } + + /* Validate device */ + dev_fd = open_device(devname, &device_bytes); + + /* Validate parameters */ + validate_parameters(&block_bytes, &segment_bytes, &stride_bytes, &width); + + /* Create filesystem handle */ + lafs = lafs_alloc(); + + /* Initialise filesystem */ + lafs_new(lafs, block_bytes); + + /* Add device */ + dev = lafs_add_device(lafs, devname, dev_fd, + segment_bytes / block_bytes, + stride_bytes / block_bytes, + width, + 16); + + /* Write device blocks */ + lafs_write_dev(dev); + + /* create files */ + ifile = lafs_get_itable(lafs); + imfile = lafs_add_inode(ifile, 1, TypeInodeMap); + rootdir = lafs_add_inode(ifile, 2, TypeDir); + rootdir->md.file.linkcount = 2; + rootdir->md.file.mode = 0755; + rootdir->md.file.parent = 2; + lafs_dirty_inode(rootdir); + orphans = lafs_add_inode(ifile, 8, TypeOrphanList); + segmap = lafs_add_inode(ifile, 16, TypeSegmentMap); + lafs->devs->segsum = segmap; + segmap->md.segmentusage.table_size = lafs->devs->tablesize * 16; + lafs->devs->tablesize = segmap->md.segmentusage.table_size; + lafs_dirty_inode(segmap); + + lafs_imap_set(imfile, 1); + lafs_imap_set(imfile, 2); + lafs_imap_set(imfile, 8); + lafs_imap_set(imfile, 16); + + lafs_cluster_init(lafs, 0, 0, 0, 1); + lafs_add_free_seg(lafs, dev->devnum, 0); + /* Write checkpoint and state blocks */ + lafs_checkpoint(lafs); + /* Write state blocks a second time, so all 4 copies are written */ + lafs_write_state(lafs); + + talloc_free(lafs); + + exit(0); +} -- 2.39.5