]> git.hungrycats.org Git - linux/commitdiff
v2.4.9.9 -> v2.4.9.10
authorLinus Torvalds <torvalds@athlon.transmeta.com>
Tue, 5 Feb 2002 04:18:41 +0000 (20:18 -0800)
committerLinus Torvalds <torvalds@athlon.transmeta.com>
Tue, 5 Feb 2002 04:18:41 +0000 (20:18 -0800)
  - Alan Cox: continued merging
  - Mingming Cao: make msgrcv/shmat check the queue/segment ID's properly
  - Greg KH: USB serial init failure fix, Xircom serial converter driver
  - Neil Brown: nsfd/raid/md/lockd cleanups
  - Ingo Molnar: multipath RAID personality, raid xor update
  - Hugh Dickins/Marcelo Tosatti: swapin read-ahead race fix
  - Vojtech Pavlik: fix up some of the infrastructure for x86-64
  - Robert Love: AMD 761 AGP GART support
  - Jens Axboe: fix SCSI-generic queue handling race
  - me: be sane about page reference bits

233 files changed:
Documentation/Configure.help
Documentation/parisc/registers
MAINTAINERS
Makefile
arch/cris/drivers/parport.c
arch/i386/boot/bootsect.S
arch/i386/boot/install.sh
arch/i386/defconfig
arch/i386/kernel/microcode.c
arch/i386/kernel/ptrace.c
arch/i386/kernel/signal.c
arch/i386/kernel/time.c
drivers/atm/eni.c
drivers/atm/zatm.c
drivers/block/Config.in
drivers/block/ps2esdi.c
drivers/char/Config.in
drivers/char/agp/agp.h
drivers/char/agp/agpgart_be.c
drivers/char/amiserial.c
drivers/char/atarimouse.c
drivers/char/atixlmouse.c
drivers/char/cyclades.c
drivers/char/drm/ati_pcigart.h
drivers/char/drm/drm_drv.h
drivers/char/drm/radeon_cp.c
drivers/char/ftape/lowlevel/ftape-calibr.c
drivers/char/joystick/analog.c
drivers/char/joystick/gameport.c
drivers/char/lp.c
drivers/char/mem.c
drivers/char/nvram.c
drivers/char/nwbutton.c
drivers/char/random.c
drivers/char/rio/riotable.c
drivers/char/serial167.c
drivers/char/serial_amba.c
drivers/char/sh-sci.c
drivers/char/synclink.c
drivers/char/tpqic02.c
drivers/char/tty_io.c
drivers/char/vme_scc.c
drivers/fc4/fc.c
drivers/fc4/soc.c
drivers/fc4/socal.c
drivers/i2c/Config.in
drivers/isdn/divert/divert_init.c
drivers/isdn/divert/divert_procfs.c
drivers/isdn/divert/isdn_divert.c
drivers/isdn/eicon/linio.c
drivers/isdn/eicon/uxio.h
drivers/isdn/hisax/hfc_pci.c
drivers/isdn/hisax/hfc_sx.c
drivers/isdn/hisax/l3dss1.c
drivers/isdn/hisax/l3ni1.c
drivers/isdn/icn/icn.c
drivers/isdn/isdn_common.c
drivers/isdn/isdn_net.c
drivers/isdn/isdnloop/isdnloop.c
drivers/isdn/sc/debug.c
drivers/md/Config.in
drivers/md/Makefile
drivers/md/md.c
drivers/md/multipath.c [new file with mode: 0644]
drivers/mtd/devices/docprobe.c
drivers/net/dl2k.c
drivers/net/fealnx.c
drivers/net/irda/vlsi_ir.c
drivers/net/pcmcia/xircom_tulip_cb.c
drivers/net/tulip/tulip_core.c
drivers/net/wan/cycx_x25.c
drivers/net/wan/sbni.c
drivers/net/winbond-840.c
drivers/net/wireless/airo.c
drivers/scsi/NCR53C9x.h
drivers/scsi/aic7xxx/aic7xxx_osm.h
drivers/scsi/aic7xxx_old.c
drivers/scsi/seagate.c
drivers/scsi/sg.c
drivers/scsi/sym53c8xx_defs.h
drivers/sound/Config.in
drivers/sound/Makefile
drivers/sound/emu10k1/passthrough.c
drivers/sound/es1370.c
drivers/sound/es1371.c
drivers/sound/gus_wave.c
drivers/sound/sonicvibes.c
drivers/usb/acm.c
drivers/usb/bluetooth.c
drivers/usb/catc.c
drivers/usb/dabusb.c
drivers/usb/dc2xx.c
drivers/usb/hid-core.c
drivers/usb/kaweth.c
drivers/usb/microtek.c
drivers/usb/ov511.c
drivers/usb/pegasus.c
drivers/usb/pwc-ctrl.c
drivers/usb/pwc-if.c
drivers/usb/pwc-ioctl.h
drivers/usb/pwc-misc.c
drivers/usb/pwc.h
drivers/usb/rio500.c
drivers/usb/se401.c
drivers/usb/serial/Config.in
drivers/usb/serial/Makefile
drivers/usb/serial/belkin_sa.c
drivers/usb/serial/cyberjack.c
drivers/usb/serial/digi_acceleport.c
drivers/usb/serial/empeg.c
drivers/usb/serial/ftdi_sio.c
drivers/usb/serial/io_edgeport.c
drivers/usb/serial/keyspan.c
drivers/usb/serial/keyspan_pda.c
drivers/usb/serial/mct_u232.c
drivers/usb/serial/omninet.c
drivers/usb/serial/pl2303.c
drivers/usb/serial/usb-serial.h
drivers/usb/serial/usbserial.c
drivers/usb/serial/visor.c
drivers/usb/serial/whiteheat.c
drivers/usb/serial/xircom_pgs.S [new file with mode: 0644]
drivers/usb/serial/xircom_pgs_fw.h [new file with mode: 0644]
drivers/usb/storage/isd200.c
drivers/usb/storage/jumpshot.c
drivers/usb/storage/scsiglue.c
drivers/usb/storage/sddr09.c
drivers/usb/storage/transport.c
drivers/usb/storage/usb.c
drivers/usb/usb-ohci.c
drivers/usb/usb-skeleton.c
drivers/usb/usbkbd.c
drivers/usb/usbmouse.c
drivers/usb/usbnet.c
drivers/video/Config.in
drivers/video/Makefile
drivers/video/cyber2000fb.c
drivers/video/fbmem.c
drivers/video/hgafb.c
drivers/video/matrox/matroxfb_base.h
drivers/video/newport_con.c
drivers/video/offb.c
drivers/video/pmag-ba-fb.c
drivers/video/pmagb-b-fb.c
drivers/video/pvr2fb.c
drivers/video/sis/sis_main.c
drivers/video/tdfxfb.c
fs/autofs/root.c
fs/devfs/base.c
fs/exec.c
fs/inode.c
fs/jffs/inode-v23.c
fs/jffs/intrep.c
fs/jffs/jffs_fm.c
fs/jffs2/Makefile [new file with mode: 0644]
fs/jffs2/TODO [new file with mode: 0644]
fs/jffs2/background.c [new file with mode: 0644]
fs/jffs2/build.c [new file with mode: 0644]
fs/jffs2/compr.c [new file with mode: 0644]
fs/jffs2/compr_rtime.c [new file with mode: 0644]
fs/jffs2/compr_rubin.c [new file with mode: 0644]
fs/jffs2/compr_rubin.h [new file with mode: 0644]
fs/jffs2/compr_zlib.c [new file with mode: 0644]
fs/jffs2/comprtest.c [new file with mode: 0644]
fs/jffs2/crc32.c [new file with mode: 0644]
fs/jffs2/crc32.h [new file with mode: 0644]
fs/jffs2/dir.c [new file with mode: 0644]
fs/jffs2/erase.c [new file with mode: 0644]
fs/jffs2/file.c [new file with mode: 0644]
fs/jffs2/gc.c [new file with mode: 0644]
fs/jffs2/histo.h [new file with mode: 0644]
fs/jffs2/histo_mips.h [new file with mode: 0644]
fs/jffs2/ioctl.c [new file with mode: 0644]
fs/jffs2/malloc.c [new file with mode: 0644]
fs/jffs2/nodelist.c [new file with mode: 0644]
fs/jffs2/nodelist.h [new file with mode: 0644]
fs/jffs2/nodemgmt.c [new file with mode: 0644]
fs/jffs2/pushpull.c [new file with mode: 0644]
fs/jffs2/pushpull.h [new file with mode: 0644]
fs/jffs2/read.c [new file with mode: 0644]
fs/jffs2/readinode.c [new file with mode: 0644]
fs/jffs2/scan.c [new file with mode: 0644]
fs/jffs2/super.c [new file with mode: 0644]
fs/jffs2/symlink.c [new file with mode: 0644]
fs/jffs2/write.c [new file with mode: 0644]
fs/jffs2/zlib.c [new file with mode: 0644]
fs/jffs2/zlib.h [new file with mode: 0644]
fs/lockd/svc.c
fs/nfsd/lockd.c
fs/nfsd/nfsctl.c
fs/nfsd/nfsproc.c
fs/nfsd/nfssvc.c
fs/nfsd/vfs.c
fs/reiserfs/inode.c
fs/reiserfs/namei.c
include/asm-alpha/processor.h
include/asm-i386/processor.h
include/asm-i386/ptrace.h
include/asm-i386/xor.h
include/linux/agp_backend.h
include/linux/brlock.h
include/linux/fs.h
include/linux/genhd.h
include/linux/i2c-algo-ite.h [new file with mode: 0644]
include/linux/jffs2.h [new file with mode: 0644]
include/linux/jffs2_fs_i.h [new file with mode: 0644]
include/linux/jffs2_fs_sb.h [new file with mode: 0644]
include/linux/mm.h
include/linux/mtd/doc2000.h
include/linux/nfsd/nfsd.h
include/linux/raid/md_compatible.h
include/linux/raid/md_k.h
include/linux/raid/md_u.h
include/linux/raid/multipath.h [new file with mode: 0644]
include/linux/swap.h
ipc/msg.c
ipc/shm.c
kernel/fork.c
lib/vsprintf.c
mm/filemap.c
mm/memory.c
mm/mmap.c
mm/swapfile.c
mm/vmscan.c
net/ipv4/netfilter/ip_conntrack_standalone.c
net/ipv4/netfilter/ip_fw_compat_masq.c
net/ipv4/netfilter/ip_queue.c
net/ipv4/netfilter/ip_tables.c
net/ipv4/netfilter/ipchains_core.c
net/irda/af_irda.c
net/sunrpc/sched.c
net/unix/af_unix.c
scripts/mkdep.c

index e4e4490982be5af868e53305425f95c3e285171a..b3b65dd62008a3e8f65400c04b24b227f24a83bf 100644 (file)
@@ -2581,7 +2581,7 @@ CONFIG_AGP_VIA
 AMD Irongate support
 CONFIG_AGP_AMD
   This option gives you AGP support for the GLX component of the
-  XFree86 4.x on AMD Irongate chipset.
+  XFree86 4.x on AMD Irongate and 761 chipsets.
 
   For the moment, you should probably say N, unless you want to test
   the GLX component for XFree86 3.3.6, which can be downloaded from
@@ -11250,6 +11250,17 @@ CONFIG_USB_SERIAL_KEYSPAN_PDA
   The module will be called keyspan_pda.o. If you want to compile it 
   as a module, say M here and read Documentation/modules.txt.
 
+USB Xircom / Entregra Single Port Serial Driver
+CONFIG_USB_SERIAL_XIRCOM
+  Say Y here if you want to use a Xircom or Entregra single port USB to
+  serial converter device.  This driver makes use of firmware
+  developed from scratch by Brian Warner.
+
+  This code is also available as a module ( = code which can be
+  inserted in and removed from the running kernel whenever you want).
+  The module will be called keyspan_pda.o. If you want to compile it
+  as a module, say M here and read <file:Documentation/modules.txt>.
+
 USB Keyspan USA-xxx Serial Driver
 CONFIG_USB_SERIAL_KEYSPAN
   Say Y here if you want to use Keyspan USB to serial converter
index 28097fe49595729da0303e79040fcc4181073baa..ad7edd91cf237e10d0163d89af1125c3d28ae060 100644 (file)
@@ -16,17 +16,17 @@ CR10 (CCR)                  lazy FPU saving*
 CR11                           as specified by ABI
 CR14 (interruption vector)     initialized to fault_vector
 CR15 (EIEM)                    initialized to all ones*
-CR16 (Interval Timer)          timer interrupt
+CR16 (Interval Timer)          read for cycle count/write starts Interval Tmr
 CR17-CR22                      interruption parameters
-CR23 (EIRR)                    read for pending interrupts
+CR23 (EIRR)                    read for pending interrupts/write clears bits
 CR24 (TR 0)                    Kernel Space Page Directory Pointer
 CR25 (TR 1)                    User   Space Page Directory Pointer
-CR26 (TR 2)
-CR27 (TR 3)
-CR28 (TR 4)                    used by interruption handlers
-CR29 (TR 5)                    used by interruption handlers
+CR26 (TR 2)                    not used
+CR27 (TR 3)                    Thread descriptor pointer
+CR28 (TR 4)                    not used
+CR29 (TR 5)                    not used
 CR30 (TR 6)                    current / 0
-CR31 (TR 7)                    used by interruption handlers
+CR31 (TR 7)                    Temporary register, used in various places
 
        Space Registers (kernel mode)
 
index 27749f11d5c0669f4476a020e71d06c420d2a880..536fca6721e2434c15db0178f40afb32451e7261 100644 (file)
@@ -780,6 +780,13 @@ L: jffs-dev@axis.com
 W:     http://www.developer.axis.com/software/jffs/
 S:     Maintained
 
+JOURNALLING FLASH FILE SYSTEM V2 (JFFS2)
+P:     David Woodhouse
+M:     dwmw2@infradead.org
+L:     jffs-dev@axis.com
+W:     http://sources.redhat.com/jffs2/
+S:     Maintained
+
 JOYSTICK DRIVER
 P:     Vojtech Pavlik
 M:     vojtech@suse.cz
@@ -1268,6 +1275,9 @@ S:        Maintained
 SOFTWARE RAID (Multiple Disks) SUPPORT
 P:     Ingo Molnar
 M:     mingo@redhat.com
+P:     Neil Brown
+M:     neilb@cse.unsw.edu.au
+L:     linux-raid@vger.kernel.org
 S:     Maintained
 
 SONIC NETWORK DRIVER
index 6aa2cb04a10f7026a0bf7da63cfa540cc6935341..17482ca0940c963d92472b2dce072f72b1f6279d 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
 VERSION = 2
 PATCHLEVEL = 4
 SUBLEVEL = 10
-EXTRAVERSION =-pre9
+EXTRAVERSION =-pre10
 
 KERNELRELEASE=$(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
 
@@ -146,6 +146,7 @@ DRIVERS-$(CONFIG_WAN) += drivers/net/wan/wan.o
 DRIVERS-$(CONFIG_ARCNET) += drivers/net/arcnet/arcnetdrv.o
 DRIVERS-$(CONFIG_ATM) += drivers/atm/atm.o
 DRIVERS-$(CONFIG_IDE) += drivers/ide/idedriver.o
+DRIVERS-$(CONFIG_FC4) += drivers/fc4/fc4.a
 DRIVERS-$(CONFIG_SCSI) += drivers/scsi/scsidrv.o
 DRIVERS-$(CONFIG_FUSION_BOOT) += drivers/message/fusion/fusion.o
 DRIVERS-$(CONFIG_IEEE1394) += drivers/ieee1394/ieee1394drv.o
index 34b32f1c687b1ba3f657f2f6739affa8b33d3f6e..e9ba07dd9499dad62ec31ce965c3bde06a762c81 100644 (file)
@@ -23,7 +23,7 @@
 #include <linux/major.h>
 #include <linux/sched.h>
 
-#include <linux/malloc.h>
+#include <linux/slab.h>
 #include <linux/interrupt.h>
 
 #include <asm/setup.h>
index 5434a9df25f07a511e87822ba5e0cbebd7a807ce..7b6264b4d40a84ba818d7e152cb533926e87813a 100644 (file)
@@ -5,8 +5,12 @@
  *     modified by Bruce Evans (bde)
  *     modified by Chris Noe (May 1999) (as86 -> gas)
  *
- * bootsect is loaded at 0x7c00 by the bios-startup routines, and moves
- * itself out of the way to address 0x90000, and jumps there.
+ * 360k/720k disk support: Andrzej Krzysztofowicz <ankry@green.mif.pg.gda.pl>
+ *
+ * BIG FAT NOTE: We're in real mode using 64k segments.  Therefore segment
+ * addresses must be multiplied by 16 to obtain their respective linear
+ * addresses. To avoid confusion, linear addresses are written using leading
+ * hex while segment addresses are written as segment:offset.
  *
  * bde - should not jump blindly, there may be systems with only 512K low
  * memory.  Use int 0x12 to get the top of memory, etc.
@@ -43,7 +47,7 @@ SWAP_DEV      = 0                     /* SWAP_DEV is now written by "build" */
 
 #ifndef RAMDISK
 #define RAMDISK 0
-#endif 
+#endif
 
 #ifndef ROOT_RDONLY
 #define ROOT_RDONLY 1
@@ -55,14 +59,12 @@ SWAP_DEV    = 0                     /* SWAP_DEV is now written by "build" */
 .global _start
 _start:
 
-#if 0 /* hook for debugger, harmless unless BIOS is fussy (old HP) */
-       int     $0x3
-#endif
+# First things first. Move ourself from 0x7C00 -> 0x90000 and jump there.
 
        movw    $BOOTSEG, %ax
-       movw    %ax, %ds
+       movw    %ax, %ds                # %ds = BOOTSEG
        movw    $INITSEG, %ax
-       movw    %ax, %es
+       movw    %ax, %es                # %ax = %es = INITSEG
        movw    $256, %cx
        subw    %si, %si
        subw    %di, %di
@@ -81,7 +83,7 @@ go:   movw    $0x4000-12, %di         # 0x4000 is an arbitrary value >=
                                        # length of bootsect + length of
                                        # setup + room for stack;
                                        # 12 is disk parm size.
-       movw    %ax, %ds                # ax and es already contain INITSEG
+       movw    %ax, %ds                # %ax and %es already contain INITSEG
        movw    %ax, %ss
        movw    %di, %sp                # put stack at INITSEG:0x4000-12.
 
@@ -97,52 +99,29 @@ go: movw    $0x4000-12, %di         # 0x4000 is an arbitrary value >=
 #
 # High doesn't hurt.  Low does.
 #
-# Segments are as follows: ds = es = ss = cs - INITSEG, fs = 0,
-# and gs is unused.
+# Segments are as follows: %cs = %ds = %es = %ss = INITSEG, %fs = 0,
+# and %gs is unused.
 
-       movw    %cx, %fs                # set fs to 0
-       movw    $0x78, %bx              # fs:bx is parameter table address
+       movw    %cx, %fs                # %fs = 0
+       movw    $0x78, %bx              # %fs:%bx is parameter table address
        pushw   %ds
-       ldsw    %fs:(%bx), %si          # ds:si is source
+       ldsw    %fs:(%bx), %si          # %ds:%si is source
        movb    $6, %cl                 # copy 12 bytes
-       pushw   %di                     # di = 0x4000-12.
-       rep                             # don't need cld -> done on line 66
-       movsw
+       pushw   %di                     # %di = 0x4000-12.
+       rep                             # don't worry about cld
+       movsw                           # already done above
        popw    %di
        popw    %ds
        movb    $36, 0x4(%di)           # patch sector count
        movw    %di, %fs:(%bx)
        movw    %es, %fs:2(%bx)
 
-# Load the setup-sectors directly after the bootblock.
-# Note that 'es' is already set up.
-# Also, cx = 0 from rep movsw above.
-
-load_setup:
-       xorb    %ah, %ah                # reset FDC 
-       xorb    %dl, %dl
-       int     $0x13   
-       xorw    %dx, %dx                # drive 0, head 0
-       movb    $0x02, %cl              # sector 2, track 0
-       movw    $0x0200, %bx            # address = 512, in INITSEG
-       movb    $0x02, %ah              # service 2, "read sector(s)"
-       movb    setup_sects, %al        # (assume all on head 0, track 0)
-       int     $0x13                   # read it
-       jnc     ok_load_setup           # ok - continue
-
-       pushw   %ax                     # dump error code
-       call    print_nl
-       movw    %sp, %bp
-       call    print_hex
-       popw    %ax     
-       jmp     load_setup
-
-ok_load_setup:
 # Get disk drive parameters, specifically number of sectors/track.
 
 # It seems that there is no BIOS call to get the number of sectors.
 # Guess 36 sectors if sector 36 can be read, 18 sectors if sector 18
 # can be read, 15 if sector 15 can be read.  Otherwise guess 9.
+# Note that %cx = 0 from rep movsw above.
 
        movw    $disksizes, %si         # table of sizes to try
 probe_loop:
@@ -151,30 +130,56 @@ probe_loop:
        movw    %ax, sectors
        cmpw    $disksizes+4, %si
        jae     got_sectors             # If all else fails, try 9
-       
-       xchgw   %cx, %ax                # cx = track and sector
+
+       xchgw   %cx, %ax                # %cx = track and sector
        xorw    %dx, %dx                # drive 0, head 0
-       xorb    %bl, %bl
-       movb    setup_sects, %bh
-       incb    %bh
-       shlb    %bh                     # address after setup (es = cs) 
+       movw    $0x0200, %bx            # address = 512, in INITSEG (%es = %cs)
        movw    $0x0201, %ax            # service 2, 1 sector
        int     $0x13
        jc      probe_loop              # try next value
 
 got_sectors:
-       movw    $INITSEG, %ax
-       movw    %ax, %es                # set up es
        movb    $0x03, %ah              # read cursor pos
        xorb    %bh, %bh
        int     $0x10
        movw    $9, %cx
-       movw    $0x0007, %bx            # page 0, attribute 7 (normal)
-       movw    $msg1, %bp
-       movw    $0x1301, %ax            # write string, move cursor
+       movb    $0x07, %bl              # page 0, attribute 7 (normal)
+                                       # %bh is set above; int10 doesn't
+                                       # modify it
+       movw    $msg1, %bp
+       movw    $0x1301, %ax            # write string, move cursor
        int     $0x10                   # tell the user we're loading..
-       movw    $SYSSEG, %ax            # ok, we've written the message, now
-       movw    %ax, %es                # we want to load system (at 0x10000)
+
+# Load the setup-sectors directly after the moved bootblock (at 0x90200).
+# We should know the drive geometry to do it, as setup may exceed first
+# cylinder (for 9-sector 360K and 720K floppies).
+
+       movw    $0x0001, %ax            # set sread (sector-to-read) to 1 as
+       movw    $sread, %si             # the boot sector has already been read
+       movw    %ax, (%si)
+
+       xorw    %ax, %ax                # reset FDC
+       xorb    %dl, %dl
+       int     $0x13
+       movw    $0x0200, %bx            # address = 512, in INITSEG
+next_step:
+       movb    setup_sects, %al
+       movw    sectors, %cx
+       subw    (%si), %cx              # (%si) = sread
+       cmpb    %cl, %al
+       jbe     no_cyl_crossing
+       movw    sectors, %ax
+       subw    (%si), %ax              # (%si) = sread
+no_cyl_crossing:
+       call    read_track
+       pushw   %ax                     # save it
+       call    set_next                # set %bx properly; it uses %ax,%cx,%dx
+       popw    %ax                     # restore
+       subb    %al, setup_sects        # rest - for next step
+       jnz     next_step
+
+       pushw   $SYSSEG
+       popw    %es                     # %es = SYSSEG
        call    read_it
        call    kill_motor
        call    print_nl
@@ -184,23 +189,26 @@ got_sectors:
 # Otherwise, one of /dev/fd0H2880 (2,32) or /dev/PS0 (2,28) or /dev/at0 (2,8)
 # depending on the number of sectors we pretend to know we have.
 
+# Segments are as follows: %cs = %ds = %ss = INITSEG,
+#      %es = SYSSEG, %fs = 0, %gs is unused.
+
        movw    root_dev, %ax
        orw     %ax, %ax
        jne     root_defined
-       
+
        movw    sectors, %bx
        movw    $0x0208, %ax            # /dev/ps0 - 1.2Mb
        cmpw    $15, %bx
        je      root_defined
-       
+
        movb    $0x1c, %al              # /dev/PS0 - 1.44Mb
        cmpw    $18, %bx
        je      root_defined
-       
+
        movb    $0x20, %al              # /dev/fd0H2880 - 2.88Mb
        cmpw    $36, %bx
        je      root_defined
-       
+
        movb    $0, %al                 # /dev/fd0 - autodetect
 root_defined:
        movw    %ax, root_dev
@@ -210,46 +218,45 @@ root_defined:
 
        ljmp    $SETUPSEG, $0
 
-# This routine loads the system at address 0x10000, making sure
-# no 64kB boundaries are crossed. We try to load it as fast as
-# possible, loading whole tracks whenever we can.
-
-# es = starting address segment (normally 0x1000)
+# These variables are addressed via %si register as it gives shorter code.
 
 sread: .word 0                         # sectors read of current track
 head:  .word 0                         # current head
 track: .word 0                         # current track
 
+# This routine loads the system at address SYSSEG, making sure
+# no 64kB boundaries are crossed. We try to load it as fast as
+# possible, loading whole tracks whenever we can.
+
 read_it:
-       movb    setup_sects, %al
-       incb    %al
-       movb    %al, sread
-       movw    %es, %ax
+       movw    %es, %ax                # %es = SYSSEG when called
        testw   $0x0fff, %ax
-die:   jne     die                     # es must be at 64kB boundary
-
-       xorw    %bx, %bx                # bx is starting address within segment
+die:   jne     die                     # %es must be at 64kB boundary
+       xorw    %bx, %bx                # %bx is starting address within segment
 rp_read:
-#ifdef __BIG_KERNEL__
-       bootsect_kludge = 0x220         # 0x200 (size of bootsector) + 0x20 (offset
-       lcall   bootsect_kludge         # of bootsect_kludge in setup.S)
+#ifdef __BIG_KERNEL__                  # look in setup.S for bootsect_kludge
+       bootsect_kludge = 0x220         # 0x200 + 0x20 which is the size of the
+       lcall   bootsect_kludge         # bootsector + bootsect_kludge offset
 #else
        movw    %es, %ax
        subw    $SYSSEG, %ax
+       movw    %bx, %cx
+       shr     $4, %cx
+       add     %cx, %ax                # check offset
 #endif
-       cmpw    syssize, %ax            # have we loaded all yet?
+       cmpw    syssize, %ax            # have we loaded everything yet?
        jbe     ok1_read
 
        ret
 
 ok1_read:
        movw    sectors, %ax
-       subw    sread, %ax
+       subw    (%si), %ax              # (%si) = sread
        movw    %ax, %cx
        shlw    $9, %cx
        addw    %bx, %cx
        jnc     ok2_read
-       
+
        je      ok2_read
 
        xorw    %ax, %ax
@@ -257,60 +264,64 @@ ok1_read:
        shrw    $9, %ax
 ok2_read:
        call    read_track
-       movw    %ax, %cx
-       addw    sread, %ax
-       cmpw    sectors, %ax
-       jne     ok3_read
-       
-       movw    $1, %ax
-       subw    head, %ax
-       jne     ok4_read
-       
-       incw    track
-ok4_read:
-       movw    %ax, head
-       xorw    %ax, %ax
-ok3_read:
-       movw    %ax, sread
-       shlw    $9, %cx
-       addw    %cx, %bx
-       jnc     rp_read
-       
-       movw    %es, %ax
-       addb    $0x10, %ah
-       movw    %ax, %es
-       xorw    %bx, %bx
+       call    set_next
        jmp     rp_read
 
 read_track:
        pusha
        pusha   
-       movw    $0xe2e, %ax                     # loading... message 2e = .
+       movw    $0xe2e, %ax             # loading... message 2e = .
        movw    $7, %bx
        int     $0x10
        popa            
-       movw    track, %dx
-       movw    sread, %cx
+
+# Accessing head, track, sread via %si gives shorter code.
+
+       movw    4(%si), %dx             # 4(%si) = track
+       movw    (%si), %cx              # (%si)  = sread
        incw    %cx
        movb    %dl, %ch
-       movw    head, %dx
+       movw    2(%si), %dx             # 2(%si) = head
        movb    %dl, %dh
        andw    $0x0100, %dx
        movb    $2, %ah
-       pushw   %dx                             # save for error dump
+       pushw   %dx                     # save for error dump
        pushw   %cx
        pushw   %bx
        pushw   %ax
        int     $0x13
        jc      bad_rt
-       
+
        addw    $8, %sp
        popa
        ret
 
+set_next:
+       movw    %ax, %cx
+       addw    (%si), %ax              # (%si) = sread
+       cmp     sectors, %ax
+       jne     ok3_set
+       movw    $0x0001, %ax
+       xorw    %ax, 2(%si)             # change head
+       jne     ok4_set
+       incw    4(%si)                  # next track
+ok4_set:
+       xorw    %ax, %ax
+ok3_set:
+       movw    %ax, (%si)              # set sread
+       shlw    $9, %cx
+       addw    %cx, %bx
+       jnc     set_next_fin
+       movw    %es, %ax
+       addb    $0x10, %ah
+       movw    %ax, %es
+       xorw    %bx, %bx
+set_next_fin:
+       ret
+
 bad_rt:
-       pushw   %ax                             # save error code
-       call    print_all                       # ah = error, al = read
+       pushw   %ax                     # save error code
+       call    print_all               # %ah = error, %al = read
        xorb    %ah, %ah
        xorb    %dl, %dl
        int     $0x13
@@ -331,13 +342,13 @@ bad_rt:
 #      ret <- %sp
  
 print_all:
-       movw    $5, %cx                         # error code + 4 registers
+       movw    $5, %cx                 # error code + 4 registers
        movw    %sp, %bp
 print_loop:
-       pushw   %cx                             # save count left
-       call    print_nl                        # nl for readability
+       pushw   %cx                     # save count remaining
+       call    print_nl                # <-- for readability
        cmpb    $5, %cl
-       jae     no_reg                          # see if register name is needed
+       jae     no_reg                  # see if register name is needed
        
        movw    $0xe05 + 'A' - 1, %ax
        subb    %cl, %al
@@ -347,31 +358,31 @@ print_loop:
        movb    $':', %al
        int     $0x10
 no_reg:
-       addw    $2, %bp                         # next register
-       call    print_hex                       # print it
+       addw    $2, %bp                 # next register
+       call    print_hex               # print it
        popw    %cx
        loop    print_loop
        ret
 
 print_nl:
-       movw    $0xe0d, %ax                     # CR
+       movw    $0xe0d, %ax             # CR
        int     $0x10
-       movb    $0xa, %al                       # LF
+       movb    $0xa, %al               # LF
        int     $0x10
        ret
 
 # print_hex is for debugging purposes, and prints the word
-# pointed to by ss:bp in hexadecimal.
+# pointed to by %ss:%bp in hexadecimal.
 
 print_hex:
-       movw    $4, %cx                         # 4 hex digits
-       movw    (%bp), %dx                      # load word into dx
+       movw    $4, %cx                 # 4 hex digits
+       movw    (%bp), %dx              # load word into %dx
 print_digit:
-       rolw    $4, %dx                         # rotate to use low 4 bits
-       movw    $0xe0f, %ax                     # ah = request
-       andb    %dl, %al                        # al = mask for nybble
-       addb    $0x90, %al                      # convert al to ascii hex
-       daa                                     # in only four instructions!
+       rolw    $4, %dx                 # rotate to use low 4 bits
+       movw    $0xe0f, %ax             # %ah = request
+       andb    %dl, %al                # %al = mask for nybble
+       addb    $0x90, %al              # convert %al to ascii hex
+       daa                             # in only four instructions!
        adc     $0x40, %al
        daa
        int     $0x10
@@ -381,6 +392,7 @@ print_digit:
 # This procedure turns off the floppy drive motor, so
 # that we enter the kernel in a known state, and
 # don't have to worry about it later.
+# NOTE: Doesn't save %ax or %dx; do it yourself if you need to.
 
 kill_motor:
        movw    $0x3f2, %dx
index 346ea8ff72faea5e457cc3fe4a623ed7466f2fa4..90f2452b3b9e2c346d58232dd7c05d5815c4a107 100644 (file)
@@ -21,6 +21,7 @@
 
 # User may have a custom install script
 
+if [ -x ~/bin/installkernel ]; then exec ~/bin/installkernel "$@"; fi
 if [ -x /sbin/installkernel ]; then exec /sbin/installkernel "$@"; fi
 
 # Default install - same as make zlilo
index f91324f43956f500d54b71b39da271a71dd21a85..4d8b92956441ec11e541269aa7f252a493328eb8 100644 (file)
@@ -136,6 +136,7 @@ CONFIG_BLK_DEV_FD=y
 # CONFIG_MD_RAID0 is not set
 # CONFIG_MD_RAID1 is not set
 # CONFIG_MD_RAID5 is not set
+# CONFIG_MD_MULTIPATH is not set
 # CONFIG_BLK_DEV_LVM is not set
 
 #
index 0bc04116dbc72ecfd1187dc66aabef204997c2a4..1ac22ad542e304feef1b379e767b81ddfadc0809 100644 (file)
@@ -126,6 +126,7 @@ static int __init microcode_init(void)
                printk(KERN_ERR "microcode: failed to devfs_register()\n");
                goto out;
        }
+       error = 0;
        printk(KERN_INFO 
                "IA-32 Microcode Update Driver: v%s <tigran@veritas.com>\n", 
                MICROCODE_VERSION);
index 3b4f3a6b68017baefa73f0efc91b8d57ca9cfe35..716a08b6c57568398f2d94601fb4d45f81cb4a9d 100644 (file)
@@ -202,7 +202,7 @@ asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
                        break;
 
                tmp = 0;  /* Default return condition */
-               if(addr < 17*sizeof(long))
+               if(addr < FRAME_SIZE*sizeof(long))
                        tmp = getreg(child, addr);
                if(addr >= (long) &dummy->u_debugreg[0] &&
                   addr <= (long) &dummy->u_debugreg[7]){
@@ -229,7 +229,7 @@ asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
                    addr > sizeof(struct user) - 3)
                        break;
 
-               if (addr < 17*sizeof(long)) {
+               if (addr < FRAME_SIZE*sizeof(long)) {
                        ret = putreg(child, addr, data);
                        break;
                }
@@ -342,11 +342,11 @@ asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
        }
 
        case PTRACE_GETREGS: { /* Get all gp regs from the child. */
-               if (!access_ok(VERIFY_WRITE, (unsigned *)data, 17*sizeof(long))) {
+               if (!access_ok(VERIFY_WRITE, (unsigned *)data, FRAME_SIZE*sizeof(long))) {
                        ret = -EIO;
                        break;
                }
-               for ( i = 0; i < 17*sizeof(long); i += sizeof(long) ) {
+               for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
                        __put_user(getreg(child, i),(unsigned long *) data);
                        data += sizeof(long);
                }
@@ -356,11 +356,11 @@ asmlinkage int sys_ptrace(long request, long pid, long addr, long data)
 
        case PTRACE_SETREGS: { /* Set all gp regs in the child. */
                unsigned long tmp;
-               if (!access_ok(VERIFY_READ, (unsigned *)data, 17*sizeof(long))) {
+               if (!access_ok(VERIFY_READ, (unsigned *)data, FRAME_SIZE*sizeof(long))) {
                        ret = -EIO;
                        break;
                }
-               for ( i = 0; i < 17*sizeof(long); i += sizeof(long) ) {
+               for ( i = 0; i < FRAME_SIZE*sizeof(long); i += sizeof(long) ) {
                        __get_user(tmp, (unsigned long *) data);
                        putreg(child, i, tmp);
                        data += sizeof(long);
index 078a157dfe8e1f402c1c04acc8e6c0be9bf6dd10..4411af584afc1d2556bef47d8e228f83b2c30a4b 100644 (file)
@@ -666,13 +666,16 @@ int do_signal(struct pt_regs *regs, sigset_t *oldset)
                                        continue;
                                /* FALLTHRU */
 
-                       case SIGSTOP:
+                       case SIGSTOP: {
+                               struct signal_struct *sig;
                                current->state = TASK_STOPPED;
                                current->exit_code = signr;
-                               if (!(current->p_pptr->sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
+                               sig = current->p_pptr->sig;
+                               if (sig && !(sig->action[SIGCHLD-1].sa.sa_flags & SA_NOCLDSTOP))
                                        notify_parent(current, SIGCHLD);
                                schedule();
                                continue;
+                       }
 
                        case SIGQUIT: case SIGILL: case SIGTRAP:
                        case SIGABRT: case SIGFPE: case SIGSEGV:
index c7672ceed5b1caa5ff9c8c81d297b6c406246fa9..86bb215505227df4c49226cb03b34b79847ac02b 100644 (file)
@@ -178,6 +178,15 @@ static unsigned long do_slow_gettimeoffset(void)
        jiffies_t = jiffies;
 
        count |= inb_p(0x40) << 8;
+       
+        /* VIA686a test code... reset the latch if count > max + 1 */
+        if (count > LATCH) {
+                outb_p(0x34, 0x43);
+                outb_p(LATCH & 0xff, 0x40);
+                outb(LATCH >> 8, 0x40);
+                count = LATCH - 1;
+        }
+       
        spin_unlock(&i8253_lock);
 
        /*
index 7e9258e605aef96096032e920ea44490c6ccc20c..8c048e78cdc685955f7afdc86f5afa296793f237 100644 (file)
@@ -32,7 +32,7 @@
 #include "suni.h"
 #include "eni.h"
 
-#ifndef __i386__
+#if !defined(__i386__) && !defined(__x86_64__)
 #ifndef ioremap_nocache
 #define ioremap_nocache(X,Y) ioremap(X,Y)
 #endif 
index 9a62822664bc3ad744a68329691ff1f230ce066e..7595a93bf4f3f73075af1a5017207ef21f9c3db0 100644 (file)
 #define DPRINTK(format,args...)
 #endif
 
+#ifndef __i386__
+#ifdef CONFIG_ATM_ZATM_EXACT_TS
+#warning Precise timestamping only available on i386 platform
+#undef CONFIG_ATM_ZATM_EXACT_TS
+#endif
+#endif
 
 #ifndef CONFIG_ATM_ZATM_DEBUG
 
index 4f46d5f220d6033b8ec387d2941c6e3d0e5da632..7902e3fc7b2d9862431927805aea91fd9a5888a6 100644 (file)
@@ -42,7 +42,7 @@ dep_tristate 'Network block device support' CONFIG_BLK_DEV_NBD $CONFIG_NET
 
 tristate 'RAM disk support' CONFIG_BLK_DEV_RAM
 if [ "$CONFIG_BLK_DEV_RAM" = "y" -o "$CONFIG_BLK_DEV_RAM" = "m" ]; then
-       int '   Default RAM disk size' CONFIG_BLK_DEV_RAM_SIZE 4096
+   int '  Default RAM disk size' CONFIG_BLK_DEV_RAM_SIZE 4096
 fi
 dep_bool '  Initial RAM disk (initrd) support' CONFIG_BLK_DEV_INITRD $CONFIG_BLK_DEV_RAM
 
index 537d2792265071805156f232bd537cc86b5c5142..1f6c3eed3dfa89372747592c511ce42188bc0529 100644 (file)
@@ -197,6 +197,7 @@ MODULE_PARM(tp720esdi, "i");
 MODULE_PARM(cyl, "i");
 MODULE_PARM(head, "i");
 MODULE_PARM(track, "i");
+MODULE_LICENSE("GPL");
 
 int init_module(void) {
        int drive;
index ba9b9bc37631515cff39dfa35a8a5e965bdd04f6..abacfcfabcce8043556d80da2a01c447a88f4ef3 100644 (file)
@@ -208,7 +208,7 @@ if [ "$CONFIG_AGP" != "n" ]; then
    bool '  Intel 440LX/BX/GX and I815/I840/I850 support' CONFIG_AGP_INTEL
    bool '  Intel I810/I815 (on-board) support' CONFIG_AGP_I810
    bool '  VIA chipset support' CONFIG_AGP_VIA
-   bool '  AMD Irongate support' CONFIG_AGP_AMD
+   bool '  AMD Irongate and 761 support' CONFIG_AGP_AMD
    bool '  Generic SiS support' CONFIG_AGP_SIS
    bool '  ALI chipset support' CONFIG_AGP_ALI
    bool '  Serverworks LE/HE support' CONFIG_AGP_SWORKS
index 1f0666f9f47c5af7f7e11700d2252d08e2f43f6c..23e50f5ff56c82205b0a33dded1201ed7685f760 100644 (file)
@@ -196,6 +196,9 @@ struct agp_bridge_data {
 #ifndef PCI_DEVICE_ID_AMD_IRONGATE_0
 #define PCI_DEVICE_ID_AMD_IRONGATE_0    0x7006
 #endif
+#ifndef PCI_DEVICE_ID_AMD_761_0
+#define PCI_DEVICE_ID_AMD_761_0         0x700e
+#endif
 #ifndef PCI_VENDOR_ID_AL
 #define PCI_VENDOR_ID_AL               0x10b9
 #endif
index 8669b29e6d61132eb9bcdc65b89ba9998ea276f2..a5bc5f2904888b7702aa5a9be4fee93d685e7584 100644 (file)
@@ -65,7 +65,7 @@ static int agp_try_unsupported __initdata = 0;
 
 static inline void flush_cache(void)
 {
-#if defined(__i386__)
+#if defined(__i386__) || defined(__x86_64__)
        asm volatile ("wbinvd":::"memory");
 #elif defined(__alpha__) || defined(__ia64__) || defined(__sparc__)
        /* ??? I wonder if we'll really need to flush caches, or if the
@@ -385,9 +385,9 @@ int agp_unbind_memory(agp_memory * curr)
 /* 
  * Driver routines - start
  * Currently this module supports the following chipsets:
- * i810, 440lx, 440bx, 440gx, i840, i850, via vp3, via mvp3, via kx133, 
- * via kt133, amd irongate, ALi M1541, and generic support for the SiS 
- * chipsets.
+ * i810, i815, 440lx, 440bx, 440gx, i840, i850, via vp3, via mvp3,
+ * via kx133, via kt133, amd irongate, amd 761, ALi M1541, and generic
+ * support for the SiS chipsets.
  */
 
 /* Generic Agp routines - Start */
@@ -2895,6 +2895,12 @@ static struct {
                "AMD",
                "Irongate",
                amd_irongate_setup },
+       { PCI_DEVICE_ID_AMD_761_0,
+               PCI_VENDOR_ID_AMD,
+               AMD_761,
+               "AMD",
+               "761",
+               amd_irongate_setup },
        { 0,
                PCI_VENDOR_ID_AMD,
                AMD_GENERIC,
@@ -2922,7 +2928,6 @@ static struct {
                "Intel",
                "440GX",
                intel_generic_setup },
-       /* could we add support for PCI_DEVICE_ID_INTEL_815_1 too ? */
        { PCI_DEVICE_ID_INTEL_815_0,
                PCI_VENDOR_ID_INTEL,
                INTEL_I815,
index 8423d2f96cabc2e50b0b1460ecbd12c1726c48d4..32732dbcc92cd22cebd004d91615738a6bd2dc40 100644 (file)
@@ -2288,7 +2288,7 @@ static void amiga_serial_putc(char c)
  *     Print a string to the serial port trying not to disturb
  *     any possible real use of the port...
  *
- *     The console_lock must be held when we get here.
+ *     The console must be locked when we get here.
  */
 static void serial_console_write(struct console *co, const char *s,
                                unsigned count)
index ce08437259608437d8a0f89306fe25739cb29adf..eaa4b43a40ddd0c0f8c30f8da118ba59cd96cd8a 100644 (file)
@@ -133,3 +133,4 @@ static void __exit atari_mouse_cleanup (void)
 module_init(atari_mouse_init);
 module_exit(atari_mouse_cleanup);
 
+MODULE_LICENSE("GPL");
\ No newline at end of file
index e515d0fa5bad2e67160a19e52747df48fa22c7f1..c28ac578ca3be65f82bd06e5067a54b1dcf5e787 100644 (file)
@@ -148,3 +148,4 @@ static void __exit atixl_cleanup (void)
 module_init(atixl_busmouse_init);
 module_exit(atixl_cleanup);
 
+MODULE_LICENSE("GPL");
index a0a99fe49475e992293e84fea53362fd69a50b66..edf5db8e1cf89ac7c09c6a03dc95990172251734 100644 (file)
@@ -5819,3 +5819,4 @@ cy_setup(char *str, int *ints)
 } /* cy_setup */
 #endif /* MODULE */
 
+MODULE_LICENSE("GPL");
index dc35da2706f1c28f3c17b2c5ae3e37ff6f275771..8b486c10abf1155d30dd5130a6c4504b1c4cbe35 100644 (file)
@@ -148,7 +148,7 @@ int DRM(ati_pcigart_init)( drm_device_t *dev,
 
        ret = 1;
 
-#if __i386__
+#if defined(__i386__) || defined(__x86_64__)
        asm volatile ( "wbinvd" ::: "memory" );
 #else
        mb();
index e1aff06d9f048f9c849473726922820495af35d7..9b96d4b93f9245abfee257032a0d8354358c30a4 100644 (file)
@@ -234,6 +234,7 @@ static char *drm_opts = NULL;
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
 MODULE_PARM( drm_opts, "s" );
+MODULE_LICENSE("GPL and additional rights");
 
 static int DRM(setup)( drm_device_t *dev )
 {
index 0475998b84da0d3877715866600bd7b178a2bb98..904c8b779ee73a780253df4b8500e9cfb1420a74 100644 (file)
@@ -543,8 +543,7 @@ static int radeon_do_engine_reset( drm_device_t *dev )
                                                RADEON_SOFT_RESET_RE |
                                                RADEON_SOFT_RESET_PP |
                                                RADEON_SOFT_RESET_E2 |
-                                               RADEON_SOFT_RESET_RB |
-                                               RADEON_SOFT_RESET_HDP ) );
+                                               RADEON_SOFT_RESET_RB ) );
        RADEON_READ( RADEON_RBBM_SOFT_RESET );
        RADEON_WRITE( RADEON_RBBM_SOFT_RESET, ( rbbm_soft_reset &
                                                ~( RADEON_SOFT_RESET_CP |
@@ -553,8 +552,7 @@ static int radeon_do_engine_reset( drm_device_t *dev )
                                                   RADEON_SOFT_RESET_RE |
                                                   RADEON_SOFT_RESET_PP |
                                                   RADEON_SOFT_RESET_E2 |
-                                                  RADEON_SOFT_RESET_RB |
-                                                  RADEON_SOFT_RESET_HDP ) ) );
+                                                  RADEON_SOFT_RESET_RB ) ) );
        RADEON_READ( RADEON_RBBM_SOFT_RESET );
 
 
index d828e831d00f63e57d79a35d45666e441b85dea2..e21e6d2419cc88525560a661f3e20c3aeeb302bb 100644 (file)
@@ -31,7 +31,7 @@
 #include <asm/io.h>
 #if defined(__alpha__)
 # include <asm/hwrpb.h>
-#elif defined(__i386__)
+#elif defined(__i386__) || defined(__x86_64__)
 # include <linux/timex.h>
 #endif
 #include <linux/ftape.h>
@@ -41,7 +41,7 @@
 
 #undef DEBUG
 
-#if !defined(__alpha__) && !defined(__i386__)
+#if !defined(__alpha__) && !defined(__i386__) && !defined(__x86_64__)
 # error Ftape is not implemented for this architecture!
 #endif
 
@@ -70,7 +70,7 @@ unsigned int ftape_timestamp(void)
 
        asm volatile ("rpcc %0" : "=r" (r));
        return r;
-#elif defined(__i386__)
+#elif defined(__i386__) || defined(__x86_64__)
        unsigned long flags;
        __u16 lo;
        __u16 hi;
@@ -90,7 +90,7 @@ static unsigned int short_ftape_timestamp(void)
 {
 #if defined(__alpha__)
        return ftape_timestamp();
-#elif defined(__i386__)
+#elif defined(__i386__) || defined(__x86_64__)
        unsigned int count;
        unsigned long flags;
  
@@ -108,7 +108,7 @@ static unsigned int diff(unsigned int t0, unsigned int t1)
 {
 #if defined(__alpha__)
        return (t1 <= t0) ? t1 + (1UL << 32) - t0 : t1 - t0;
-#elif defined(__i386__)
+#elif defined(__i386__) || defined(__x86_64__)
        /*
         * This is tricky: to work for both short and full ftape_timestamps
         * we'll have to discriminate between these.
@@ -124,7 +124,7 @@ static unsigned int usecs(unsigned int count)
 {
 #if defined(__alpha__)
        return (ps_per_cycle * count) / 1000000UL;
-#elif defined(__i386__)
+#elif defined(__i386__) || defined(__x86_64__)
        return (10000 * count) / ((CLOCK_TICK_RATE + 50) / 100);
 #endif
 }
@@ -164,7 +164,7 @@ static void time_inb(void)
 
 static void init_clock(void)
 {
-#if defined(__i386__)
+#if defined(__i386__) || defined(__x86_64__)
        unsigned int t;
        int i;
        TRACE_FUN(ft_t_any);
@@ -214,7 +214,7 @@ void ftape_calibrate(char *name,
        unsigned int tc = 0;
        unsigned int count;
        unsigned int time;
-#if defined(__i386__)
+#if defined(__i386__) || defined(__x86_64__)
        unsigned int old_tc = 0;
        unsigned int old_count = 1;
        unsigned int old_time = 1;
@@ -265,7 +265,7 @@ void ftape_calibrate(char *name,
                if (time >= 100*1000) {
                        break;
                }
-#elif defined(__i386__)
+#elif defined(__i386__) || defined(__x86_64__)
                /*
                 * increase the count until the resulting time nears 2/HZ,
                 * then the tc will drop sharply because we lose LATCH counts.
index bed74823cfad5f2b8746746fd333d8d0c840a10e..d04cb55ea6912e8104a2319d8df517fd4aaf5fae 100644 (file)
@@ -39,6 +39,7 @@
 #include <linux/init.h>
 #include <linux/input.h>
 #include <linux/gameport.h>
+#include <asm/timex.h>
 
 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
 MODULE_DESCRIPTION("Analog joystick and gamepad driver for Linux");
@@ -136,27 +137,25 @@ struct analog_port {
  */
 
 #ifdef __i386__
-#ifdef CONFIG_X86_TSC
-#define GET_TIME(x)    __asm__ __volatile__ ( "rdtsc" : "=a" (x) : : "dx" )
+#define TSC_PRESENT    (test_bit(X86_FEATURE_TSC, &boot_cpu_data.x86_capability))
+#define GET_TIME(x)    do { if (TSC_PRESENT) rdtscl(x); else outb(0, 0x43); x = inb(0x40); x |= inb(0x40) << 8; } while (0)
+#define DELTA(x,y)     (TSC_PRESENT?((y)-(x)):((x)-(y)+((x)<(y)?1193180L/HZ:0)))
+#define TIME_NAME      (TSC_PRESENT?"TSC":"PIT")
+#elif __x86_64__
+#define GET_TIME(x)    rdtscl(x)
 #define DELTA(x,y)     ((y)-(x))
-#define TIME_NAME "TSC"
-#else
-#define GET_TIME(x)    do { outb(0, 0x43); x = inb(0x40); x |= inb(0x40) << 8; } while (0)
-#define DELTA(x,y)     ((x)-(y)+((x)<(y)?1193180L/HZ:0))
-#define TIME_NAME "PIT"
-#endif
+#define TIME_NAME      "TSC"
 #elif __alpha__
-#define GET_TIME(x)    __asm__ __volatile__ ( "rpcc %0" : "=r" (x) )
+#define GET_TIME(x)    get_cycles(x)
 #define DELTA(x,y)     ((y)-(x))
-#define TIME_NAME "PCC"
-#endif
-
-#ifndef GET_TIME
+#define TIME_NAME      "PCC"
+#else
 #define FAKE_TIME
 static unsigned long analog_faketime = 0;
 #define GET_TIME(x)     do { x = analog_faketime++; } while(0)
 #define DELTA(x,y)     ((y)-(x))
-#define TIME_NAME "Unreliable"
+#define TIME_NAME      "Unreliable"
+#warning Precise timer not defined for this architecture.
 #endif
 
 /*
@@ -498,7 +497,7 @@ static void analog_init_device(struct analog_port *port, struct analog *analog,
        if (port->cooked)
                printk(" [ADC port]\n");
        else
-               printk(" ["TIME_NAME" timer, %d %sHz clock, %d ns res]\n",
+               printk(" [%s timer, %d %sHz clock, %d ns res]\n", TIME_NAME,
                port->speed > 10000 ? (port->speed + 800) / 1000 : port->speed,
                port->speed > 10000 ? "M" : "k", (port->loop * 1000000) / port->speed);
 }
index 11066cebf37c2ef73e3ee884e6e335ed8fbbda41..5eacff46608c428eed35b7b869fb5551e910fb76 100644 (file)
@@ -62,7 +62,7 @@ static int gameport_number;
 
 static int gameport_measure_speed(struct gameport *gameport)
 {
-#ifdef __i386__
+#if defined(__i386__) || defined(__x86_64__)
 
 #define GET_TIME(x)     do { outb(0, 0x43); x = inb(0x40); x |= inb(0x40) << 8; } while (0)
 #define DELTA(x,y)      ((y)-(x)+((y)<(x)?1193180L/HZ:0))
index 5517b1a8eb4882e28ea778d99e0c3f4524a6816a..8bc6176f04c599a62a36cd677ff5840de35e7344 100644 (file)
@@ -543,7 +543,7 @@ static struct file_operations lp_fops = {
  * non-zero to get the latter behaviour. */
 #define CONSOLE_LP_STRICT 1
 
-/* The console_lock must be held when we get here. */
+/* The console must be locked when we get here. */
 
 static void lp_console_write (struct console *co, const char *s,
                              unsigned count)
index 2f340ef36bcb7dbc592bd437fc01de50b57d48c4..14f29671b5337a09fdba91629cde7b6f23eca55e 100644 (file)
@@ -41,9 +41,6 @@ extern void mda_console_init(void);
 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
 extern void tapechar_init(void);
 #endif
-#if defined(CONFIG_ADB)
-extern void adbdev_init(void);
-#endif
      
 static ssize_t do_write_mem(struct file * file, void *p, unsigned long realp,
                            const char * buf, size_t count, loff_t *ppos)
@@ -134,7 +131,7 @@ static inline pgprot_t pgprot_noncached(pgprot_t _prot)
 {
        unsigned long prot = pgprot_val(_prot);
 
-#if defined(__i386__)
+#if defined(__i386__) || defined(__x86_64__)
        /* On PPro and successors, PCD alone doesn't always mean 
            uncached because of interactions with the MTRRs. PCD | PWT
            means definitely uncached. */ 
@@ -638,9 +635,6 @@ int __init chr_dev_init(void)
 #endif
 #if defined(CONFIG_S390_TAPE) && defined(CONFIG_S390_TAPE_CHAR)
        tapechar_init();
-#endif
-#if defined(CONFIG_ADB)
-       adbdev_init();
 #endif
        return 0;
 }
index 61dd6729fc9fd8c7b25557ef2282394c93ab0086..1959e9ed31d2ce9a6045588032ceea1700d3c507 100644 (file)
@@ -43,7 +43,7 @@
 /* select machine configuration */
 #if defined(CONFIG_ATARI)
 #define MACH ATARI
-#elif defined(__i386__) || defined(__arm__) /* and others?? */
+#elif defined(__i386__) || defined(__x86_64__) || defined(__arm__) /* and others?? */
 #define MACH PC
 #else
 #error Cannot build nvram driver for this machine configuration.
index e99d928560b219d0e208798dd33cbcc3bc37fa8d..3cdce3eb28d7b3109e895299adcc5b08d8162b7a 100644 (file)
@@ -47,6 +47,8 @@ static int reboot_count = NUM_PRESSES_REBOOT; /* Number of presses to reboot */
  * Because callbacks can be unregistered at random the list can become
  * fragmented, so we need to search through the list until we find the first
  * free entry.
+ *
+ * FIXME: Has anyone spotted any locking functions int his code recently ??
  */
 
 int button_add_callback (void (*callback) (void), int count)
@@ -236,6 +238,9 @@ static void __exit nwbutton_exit (void)
        misc_deregister (&button_misc_device);
 }
 
+
+MODULE_AUTHOR("Alex Holden");
+MODULE_LICENSE("GPL");
 EXPORT_NO_SYMBOLS;
 
 module_init(nwbutton_init);
index b8c65c11de4310fb02e8d6f5001fab146ff39548..aaca14148828b96d6ca7e230fa265ccc466b2b3e 100644 (file)
@@ -717,6 +717,10 @@ static void add_timer_randomness(struct timer_rand_state *state, unsigned num)
        } else {
                time = jiffies;
        }
+#elif defined (__x86_64__)
+       __u32 high;
+       rdtsc(time, high);
+       num ^= high;
 #else
        time = jiffies;
 #endif
index 9970ecbcf7e78870586951b951b8fd59553d3fa1..e4a1a154673b9e5f2e4cd8fdaaee4693b1f38485 100644 (file)
@@ -445,7 +445,7 @@ struct rio_info *   p;
        int Next = 0;
        struct Map *MapP;
        struct Host *HostP;
-       int oldspl;
+       long oldspl;
 
        disable(oldspl);                /* strange but true! */
  
index a409f74a275f962e79440c37764a66142c77f3a6..e4071a9dde5e1a95414665665479de3db909300d 100644 (file)
@@ -2743,7 +2743,7 @@ void console_setup(char *str, int *ints)
  * Of course, once the console has been registered, we had better ensure
  * that serial167_init() doesn't leave the chip non-functional.
  *
- * The console_lock must be held when we get here.
+ * The console must be locked when we get here.
  */
 
 void serial167_console_write(struct console *co, const char *str, unsigned count)
index b4d27742d71e0037931e5d153adf1b99b3193d37..190f8c2015088848990e74c894637468c8017828 100644 (file)
@@ -1881,7 +1881,7 @@ static int ambauart_console_read(struct console *co, const char *s, u_int count)
  *     Print a string to the serial port trying not to disturb
  *     any possible real use of the port...
  *
- *     The console_lock must be held when we get here.
+ *     The console must be locked when we get here.
  */
 static void ambauart_console_write(struct console *co, const char *s, u_int count)
 {
index 4c27b37b75e89ff9bf6bd1dd09b9b3c81407a826..8af0d1eaed02d0d3ee59a83afc218d60a4581f55 100644 (file)
@@ -911,13 +911,10 @@ static int sci_ioctl(struct tty_struct * tty, struct file * filp,
                              (unsigned int *) arg);
                break;
        case TIOCSSOFTCAR:
-               if ((rc = verify_area(VERIFY_READ, (void *) arg,
-                                     sizeof(int))) == 0) {
-                       get_user(ival, (unsigned int *) arg);
+               if ((rc = get_user(ival, (unsigned int *) arg)) == 0)
                        tty->termios->c_cflag =
                                (tty->termios->c_cflag & ~CLOCAL) |
                                (ival ? CLOCAL : 0);
-               }
                break;
        case TIOCGSERIAL:
                if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
@@ -931,35 +928,23 @@ static int sci_ioctl(struct tty_struct * tty, struct file * filp,
                                          (struct serial_struct *) arg);
                break;
        case TIOCMGET:
-               if ((rc = verify_area(VERIFY_WRITE, (void *) arg,
-                                     sizeof(unsigned int))) == 0) {
-                       ival = sci_getsignals(port);
-                       put_user(ival, (unsigned int *) arg);
-               }
+               ival = sci_getsignals(port);
+               rc = put_user(ival, (unsigned int *) arg);
                break;
        case TIOCMBIS:
-               if ((rc = verify_area(VERIFY_READ, (void *) arg,
-                                     sizeof(unsigned int))) == 0) {
-                       get_user(ival, (unsigned int *) arg);
+               if ((rc = get_user(ival, (unsigned int *) arg)) == 0)
                        sci_setsignals(port, ((ival & TIOCM_DTR) ? 1 : -1),
                                             ((ival & TIOCM_RTS) ? 1 : -1));
-               }
                break;
        case TIOCMBIC:
-               if ((rc = verify_area(VERIFY_READ, (void *) arg,
-                                     sizeof(unsigned int))) == 0) {
-                       get_user(ival, (unsigned int *) arg);
+               if ((rc = get_user(ival, (unsigned int *) arg)) == 0)
                        sci_setsignals(port, ((ival & TIOCM_DTR) ? 0 : -1),
                                             ((ival & TIOCM_RTS) ? 0 : -1));
-               }
                break;
        case TIOCMSET:
-               if ((rc = verify_area(VERIFY_READ, (void *) arg,
-                                     sizeof(unsigned int))) == 0) {
-                       get_user(ival, (unsigned int *)arg);
+               if ((rc = get_user(ival, (unsigned int *)arg)) == 0)
                        sci_setsignals(port, ((ival & TIOCM_DTR) ? 1 : 0),
                                             ((ival & TIOCM_RTS) ? 1 : 0));
-               }
                break;
 
        default:
@@ -1146,16 +1131,19 @@ static void sci_free_irq(struct sci_port *port)
        }
 }
 
+static char banner[] __initdata[] =
+       KERN_INFO "SuperH SCI(F) driver initialized\n";
+
 int __init sci_init(void)
 {
        struct sci_port *port;
        int j;
 
-       printk("SuperH SCI(F) driver initialized\n");
+       printk("%s", banner);
 
        for (j=0; j<SCI_NPORTS; j++) {
                port = &sci_ports[j];
-               printk("ttySC%d at 0x%08x is a %s\n", j, port->base,
+               printk(KERN_INFO "ttySC%d at 0x%08x is a %s\n", j, port->base,
                       (port->type == PORT_SCI) ? "SCI" : "SCIF");
        }
 
index 2ee93688eaf2e08787065b3a358ed3860fb02e39..5c8d4f3fbbcea3510b143fc7641bf9cec5ae3981 100644 (file)
@@ -8015,7 +8015,8 @@ void mgsl_sppp_delete(struct mgsl_struct *info)
 int mgsl_sppp_open(struct net_device *d)
 {
        struct mgsl_struct *info = d->priv;
-       int err, flags;
+       int err;
+       long flags;
 
        if (debug_level >= DEBUG_LEVEL_INFO)
                printk("mgsl_sppp_open(%s)\n",info->netname);   
@@ -8058,7 +8059,7 @@ open_fail:
 void mgsl_sppp_tx_timeout(struct net_device *dev)
 {
        struct mgsl_struct *info = dev->priv;
-       int flags;
+       long flags;
 
        if (debug_level >= DEBUG_LEVEL_INFO)
                printk("mgsl_sppp_tx_timeout(%s)\n",info->netname);     
index a1bb133fbd5837173bec6520a9e6b1c37810775d..c4c41773dc1c2b214222330f8a1b17c42a1ecf71 100644 (file)
@@ -772,10 +772,11 @@ static int rdstatus(char *stp, unsigned size, char qcmd)
 static int get_status(volatile struct tpstatus *stp)
 {
        int stat = rdstatus((char *) stp, TPSTATSIZE, QCMD_RD_STAT);
-#ifdef __i386__
+#if defined(__i386__) || defined (__x86_64__)
        byte_swap_w(&(stp->dec));
        byte_swap_w(&(stp->urc));
 #else
+#warning Undefined architecture
        /* should probably swap status bytes #definition */
 #endif
        return stat;
index c50db6cf9f52dfaa5fc6b375be80f51ee7e8e294..59847efa44cdff92c48392572867640643a56ce5 100644 (file)
@@ -158,6 +158,8 @@ extern void rs285_console_init(void);
 extern void sa1100_rs_console_init(void);
 extern void sgi_serial_console_init(void);
 extern void sci_console_init(void);
+extern void tx3912_console_init(void);
+extern void tx3912_rs_init(void);
 
 #ifndef MIN
 #define MIN(a,b)       ((a) < (b) ? (a) : (b))
@@ -2182,6 +2184,9 @@ void __init console_init(void)
 #ifdef CONFIG_VT
        con_init();
 #endif
+#ifdef CONFIG_AU1000_SERIAL_CONSOLE
+       au1000_serial_console_init();
+#endif
 #ifdef CONFIG_SERIAL_CONSOLE
 #if (defined(CONFIG_8xx) || defined(CONFIG_8260))
        console_8xx_init();
@@ -2223,9 +2228,15 @@ void __init console_init(void)
 #ifdef CONFIG_SERIAL_SA1100_CONSOLE
        sa1100_rs_console_init();
 #endif
+#ifdef CONFIG_ARC_CONSOLE
+       arc_console_init();
+#endif
 #ifdef CONFIG_SERIAL_AMBA_CONSOLE
        ambauart_console_init();
 #endif
+#ifdef CONFIG_SERIAL_TX3912_CONSOLE
+       tx3912_console_init();
+#endif
 }
 
 static struct tty_driver dev_tty_driver, dev_syscons_driver;
@@ -2313,6 +2324,9 @@ void __init tty_init(void)
 #if defined(CONFIG_MVME162_SCC) || defined(CONFIG_BVME6000_SCC) || defined(CONFIG_MVME147_SCC)
        vme_scc_init();
 #endif
+#ifdef CONFIG_SERIAL_TX3912
+       tx3912_rs_init();
+#endif
 #ifdef CONFIG_COMPUTONE
        ip2_init();
 #endif
index 67ed42822c798d7f2f2112d6e93ae38a6b103417..63d764af9bb772b61d8234357b6d65bcd19ee47a 100644 (file)
@@ -1047,7 +1047,7 @@ static void scc_ch_write (char ch)
        *p = ch;
 }
 
-/* The console_lock must be held when we get here. */
+/* The console must be locked when we get here. */
 
 static void scc_console_write (struct console *co, const char *str, unsigned count)
 {
index 7c1ee6927e6b6013a535f97ffaba2c43693a0d41..6a128292379a79dbb49d903a70689a7a3b7592c0 100644 (file)
@@ -1155,14 +1155,3 @@ int fc_do_prli(fc_channel *fc, unsigned char alpa)
        kfree(p);
        return status;
 }
-
-#ifdef MODULE
-int init_module(void)
-{
-       return 0;
-}
-
-void cleanup_module(void)
-{
-}
-#endif
index e78f781fc48117b4ed7a90c15047984dc5846422..b16eeecdddf8b1eb6c7c7e26af04d9216b6a76e2 100644 (file)
@@ -565,14 +565,10 @@ static inline void soc_init(struct sbus_dev *sdev, int no)
             (long)socs, (long)soc_intr, (long)soc_hw_enque))   
        if (version_printed++ == 0)
                printk (version);
-#ifdef MODULE
-       s->port[0].fc.module = &__this_module;
-       s->port[1].fc.module = &__this_module;
-#else
-       s->port[0].fc.module = NULL;
-       s->port[1].fc.module = NULL;
-#endif
-                                               
+
+       s->port[0].fc.module = THIS_MODULE;
+       s->port[1].fc.module = THIS_MODULE;
+
        s->next = socs;
        socs = s;
        s->port[0].fc.dev = sdev;
@@ -713,11 +709,7 @@ static inline void soc_init(struct sbus_dev *sdev, int no)
        SOD(("Enabled SOC\n"))
 }
 
-#ifndef MODULE
-int __init soc_probe(void)
-#else
-int init_module(void)
-#endif
+static int __init soc_probe(void)
 {
        struct sbus_bus *sbus;
        struct sbus_dev *sdev = 0;
@@ -741,10 +733,7 @@ int init_module(void)
        return 0;
 }
 
-EXPORT_NO_SYMBOLS;
-
-#ifdef MODULE
-void cleanup_module(void)
+static void __exit sco_cleanup(void)
 {
        struct soc *s;
        int irq;
@@ -770,4 +759,8 @@ void cleanup_module(void)
                                     s->req_cpu, s->req_dvma);
        }
 }
-#endif
+
+EXPORT_NO_SYMBOLS;
+
+module_init(soc_probe);
+module_exit(soc_cleanup);
index 0dc30e22e0969242c6ade6b4ff21d1c09c9792cf..c52305f7117b580f5ec5adfa34833b6c59b97107 100644 (file)
@@ -673,14 +673,10 @@ static inline void socal_init(struct sbus_dev *sdev, int no)
             (long)socals, (long)socal_intr, (long)socal_hw_enque))
        if (version_printed++ == 0)
                printk (version);
-#ifdef MODULE
-       s->port[0].fc.module = &__this_module;
-       s->port[1].fc.module = &__this_module;
-#else
-       s->port[0].fc.module = NULL;
-       s->port[1].fc.module = NULL;
-#endif
-                                               
+
+       s->port[0].fc.module = THIS_MODULE;
+       s->port[1].fc.module = THIS_MODULE;
+                                       
        s->next = socals;
        socals = s;
        s->port[0].fc.dev = sdev;
@@ -850,11 +846,7 @@ static inline void socal_init(struct sbus_dev *sdev, int no)
        SOD(("Enabled SOCAL\n"))
 }
 
-#ifndef MODULE
-int __init socal_probe(void)
-#else
-int init_module(void)
-#endif
+static int __init socal_probe(void)
 {
        struct sbus_bus *sbus;
        struct sbus_dev *sdev = 0;
@@ -880,10 +872,7 @@ int init_module(void)
        return 0;
 }
 
-EXPORT_NO_SYMBOLS;
-
-#ifdef MODULE
-void cleanup_module(void)
+static void __exit socal_cleanup(void)
 {
        struct socal *s;
        int irq;
@@ -910,4 +899,8 @@ void cleanup_module(void)
                                     s->req_cpu, s->req_dvma);
        }
 }
-#endif
+
+EXPORT_NO_SYMBOLS;
+
+module_init(socal_probe);
+module_exit(socal_cleanup);
index 405121edc70a5ead68072f6e313a91263bfa1b0a..8b6cbf67a90034efb0e68ca6782e653d992f3a6d 100644 (file)
@@ -20,6 +20,13 @@ if [ "$CONFIG_I2C" != "n" ]; then
       dep_tristate '  Elektor ISA card' CONFIG_I2C_ELEKTOR $CONFIG_I2C_ALGOPCF
    fi
 
+   if [ "$CONFIG_MIPS_ITE8172" = "y" ]; then
+      dep_tristate 'ITE I2C Algorithm' CONFIG_ITE_I2C_ALGO $CONFIG_I2C
+      if [ "$CONFIG_ITE_I2C_ALGO" != "n" ]; then
+         dep_tristate '  ITE I2C Adapter' CONFIG_ITE_I2C_ADAP $CONFIG_ITE_I2C_ALGO
+      fi
+   fi
+
 # This is needed for automatic patch generation: sensors code starts here
 # This is needed for automatic patch generation: sensors code ends here
 
index c8a30954cd819fbd76ccca0cacd21bec07da5339..edb4afb8af1a34ce70afe5183191033fecbf7d73 100644 (file)
@@ -70,7 +70,7 @@ static int __init divert_init(void)
 /* Module deinit code */
 /**********************/
 static void __exit divert_exit(void)
-{ int flags;
+{ long flags;
   int i;
 
   save_flags(flags);
index fcd1245ecb3794267a9cce7ea2abc938b627f67a..b9dec5ff40eb67b97853bceb6ee6b52c0edf934e 100644 (file)
@@ -50,7 +50,7 @@ void
 put_info_buffer(char *cp)
 {
        struct divert_info *ib;
-       int flags;
+       long flags;
 
        if (if_used <= 0)
                return;
@@ -145,7 +145,7 @@ isdn_divert_poll(struct file *file, poll_table * wait)
 static int
 isdn_divert_open(struct inode *ino, struct file *filep)
 {
-       int flags;
+       long flags;
 
        lock_kernel();
        save_flags(flags);
@@ -168,7 +168,7 @@ static int
 isdn_divert_close(struct inode *ino, struct file *filep)
 {
        struct divert_info *inf;
-       int flags;
+       long flags;
 
        lock_kernel();
        save_flags(flags);
@@ -198,7 +198,8 @@ isdn_divert_ioctl(struct inode *inode, struct file *file,
                  uint cmd, ulong arg)
 {
        divert_ioctl dioctl;
-       int i, flags;
+       int i;
+       long flags;
        divert_rule *rulep;
        char *cp;
 
index 47a27977525218e21edf90daac08b25ba599ccbc..87f0c2b944978fa43d4eb0048440ae55aa5b98c7 100644 (file)
@@ -67,7 +67,7 @@ static unsigned char extern_wait_max = 4; /* maximum wait in s for external proc
 /* timer callback function */
 /***************************/
 static void deflect_timer_expire(ulong arg)
-{ int flags;
+{ long flags;
   struct call_struc *cs = (struct call_struc *) arg;
 
   save_flags(flags);
@@ -125,7 +125,8 @@ static void deflect_timer_expire(ulong arg)
 int cf_command(int drvid, int mode, 
                u_char proc, char *msn, 
                u_char service, char *fwd_nr, ulong *procid)
-{ int retval,msnlen,flags;
+{ long flags;
+  int retval,msnlen;
   int fwd_len;
   char *p,*ielenp,tmp[60];
   struct call_struc *cs;
@@ -220,7 +221,7 @@ int cf_command(int drvid, int mode,
 int deflect_extern_action(u_char cmd, ulong callid, char *to_nr)
 { struct call_struc *cs;
   isdn_ctrl ic;
-  int flags;
+  long flags;
   int i;
 
   if ((cmd & 0x7F) > 2) return(-EINVAL); /* invalid command */
@@ -291,7 +292,7 @@ int deflect_extern_action(u_char cmd, ulong callid, char *to_nr)
 /********************************/
 int insertrule(int idx, divert_rule *newrule)
 { struct deflect_struc *ds,*ds1=NULL;
-  int flags;
+  long flags;
 
   if (!(ds = (struct deflect_struc *) kmalloc(sizeof(struct deflect_struc), 
                                               GFP_KERNEL))) 
@@ -337,7 +338,7 @@ int insertrule(int idx, divert_rule *newrule)
 /***********************************/
 int deleterule(int idx)
 { struct deflect_struc *ds,*ds1;
-  int flags;
+  long flags;
   
   if (idx < 0) 
    { save_flags(flags);
@@ -405,7 +406,7 @@ divert_rule *getruleptr(int idx)
 /*************************************************/
 int isdn_divert_icall(isdn_ctrl *ic)
 { int retval = 0;
-  int flags;
+  long flags;
   struct call_struc *cs = NULL; 
   struct deflect_struc *dv;
   char *p,*p1;
@@ -557,7 +558,7 @@ int isdn_divert_icall(isdn_ctrl *ic)
 
 void deleteprocs(void)
 { struct call_struc *cs, *cs1; 
-  int flags;
+  long flags;
 
   save_flags(flags);
   cli();
@@ -714,7 +715,8 @@ int interrogate_success(isdn_ctrl *ic, struct call_struc *cs)
 /*********************************************/
 int prot_stat_callback(isdn_ctrl *ic)
 { struct call_struc *cs, *cs1;
-  int i,flags;
+  int i;
+  long flags;
 
   cs = divert_head; /* start of list */
   cs1 = NULL;
@@ -805,7 +807,8 @@ int prot_stat_callback(isdn_ctrl *ic)
 /***************************/
 int isdn_divert_stat_callback(isdn_ctrl *ic)
 { struct call_struc *cs, *cs1;
-  int flags, retval;
+  long flags;
+  int retval;
 
   retval = -1;
   cs = divert_head; /* start of list */
index 3d970abce38260643bba3633d434ac98a0df3934..a00321c0b73f0c1b333753dfa309e8a51394b523 100644 (file)
@@ -683,7 +683,7 @@ void UxFree(void *ptr)
        kfree(ptr);
 }
 
-int UxCardLock(ux_diva_card_t *card)
+long UxCardLock(ux_diva_card_t *card)
 {
        unsigned long flags;
 
@@ -695,7 +695,7 @@ int UxCardLock(ux_diva_card_t *card)
        
 }
 
-void UxCardUnlock(ux_diva_card_t *card, int ipl)
+void UxCardUnlock(ux_diva_card_t *card, long ipl)
 {
        //spin_unlock_irqrestore(&diva_lock, ipl);
 
index 7acae3e7809df704be1b95c77ad6b21e2f23123f..449f1a7d0ee9792e28cf94d99bdb306650c4b8fc 100644 (file)
@@ -75,8 +75,8 @@ void  UxCardHandleFree(ux_diva_card_t *card);
  * Lock and unlock access to a card
  */
 
-int            UxCardLock(ux_diva_card_t *card);
-void   UxCardUnlock(ux_diva_card_t *card, int ipl);
+long           UxCardLock(ux_diva_card_t *card);
+void   UxCardUnlock(ux_diva_card_t *card, long ipl);
 
 /*
  * Set the mapping address for PCI cards
index ab9b6bb740942a6745dc164fae4c28c4b93ff043..9a7c821036203ac18bac9107c2749b9f9507f131 100644 (file)
@@ -84,7 +84,7 @@ static const PCI_ENTRY id_list[] =
 void
 release_io_hfcpci(struct IsdnCardState *cs)
 {
-       int flags;
+       long flags;
 
        save_flags(flags);
        cli();
@@ -299,7 +299,8 @@ hfcpci_empty_fifo(struct BCState *bcs, bzfifo_type * bz, u_char * bdata, int cou
        u_char *ptr, *ptr1, new_f2;
        struct sk_buff *skb;
        struct IsdnCardState *cs = bcs->cs;
-       int flags, total, maxlen, new_z2;
+       long flags;
+       int total, maxlen, new_z2;
        z_type *zp;
 
        save_flags(flags);
@@ -633,7 +634,8 @@ static void
 hfcpci_fill_fifo(struct BCState *bcs)
 {
        struct IsdnCardState *cs = bcs->cs;
-       int flags, maxlen, fcnt;
+       long flags;
+       int maxlen, fcnt;
        int count, new_z1;
        bzfifo_type *bz;
        u_char *bdata;
@@ -810,7 +812,7 @@ dch_nt_l2l1(struct PStack *st, int pr, void *arg)
 static int
 hfcpci_auxcmd(struct IsdnCardState *cs, isdn_ctrl * ic)
 {
-       int flags;
+       long flags;
        int i = *(unsigned int *) ic->parm.num;
 
        if ((ic->arg == 98) &&
@@ -1160,7 +1162,7 @@ HFCPCI_l1hw(struct PStack *st, int pr, void *arg)
 {
        struct IsdnCardState *cs = (struct IsdnCardState *) st->l1.hardware;
        struct sk_buff *skb = arg;
-       int flags;
+       long flags;
 
        switch (pr) {
                case (PH_DATA | REQUEST):
@@ -1314,7 +1316,8 @@ void
 mode_hfcpci(struct BCState *bcs, int mode, int bc)
 {
        struct IsdnCardState *cs = bcs->cs;
-       int flags, fifo2;
+       long flags;
+       int fifo2;
 
        if (cs->debug & L1_DEB_HSCX)
                debugl1(cs, "HFCPCI bchannel mode %d bchan %d/%d",
@@ -1548,7 +1551,7 @@ setstack_2b(struct PStack *st, struct BCState *bcs)
 static void
 hfcpci_bh(struct IsdnCardState *cs)
 {
-       int flags;
+       long flags;
 /*      struct PStack *stptr;
  */
        if (!cs)
index 4624cec7e5feb8da7dc43405e5db52bb4e79f463..aecbb7398457d40fd0543283d66c7f963017c28e 100644 (file)
@@ -73,7 +73,7 @@ static u_char ccd_sp_irqtab[16] = {
 /******************************/
 static inline void
 Write_hfc(struct IsdnCardState *cs, u_char regnum, u_char val)
-{       register int flags;
+{       long flags;
 
         save_flags(flags);
        cli();
@@ -84,8 +84,8 @@ Write_hfc(struct IsdnCardState *cs, u_char regnum, u_char val)
 
 static inline u_char
 Read_hfc(struct IsdnCardState *cs, u_char regnum)
-{       register int flags;
-        register u_char ret; 
+{       long flags;
+        u_char ret; 
 
         save_flags(flags);
        cli();
@@ -101,7 +101,7 @@ Read_hfc(struct IsdnCardState *cs, u_char regnum)
 /**************************************************/
 static void
 fifo_select(struct IsdnCardState *cs, u_char fifo)
-{       int flags;
+{       long flags;
 
         if (fifo == cs->hw.hfcsx.last_fifo) 
          return; /* still valid */
@@ -123,7 +123,7 @@ fifo_select(struct IsdnCardState *cs, u_char fifo)
 /******************************************/
 static void
 reset_fifo(struct IsdnCardState *cs, u_char fifo)
-{       int flags;
+{       long flags;
 
         save_flags(flags); 
        cli();
@@ -337,7 +337,7 @@ read_fifo(struct IsdnCardState *cs, u_char fifo, int trans_max)
 void
 release_io_hfcsx(struct IsdnCardState *cs)
 {
-       int flags;
+       long flags;
 
        save_flags(flags);
        cli();
@@ -599,7 +599,7 @@ static void
 hfcsx_fill_fifo(struct BCState *bcs)
 {
        struct IsdnCardState *cs = bcs->cs;
-       int flags;
+       long flags;
 
        if (!bcs->tx_skb)
                return;
@@ -670,7 +670,7 @@ dch_nt_l2l1(struct PStack *st, int pr, void *arg)
 static int
 hfcsx_auxcmd(struct IsdnCardState *cs, isdn_ctrl * ic)
 {
-       int flags;
+       long flags;
        int i = *(unsigned int *) ic->parm.num;
 
        if ((ic->arg == 98) &&
@@ -729,7 +729,7 @@ hfcsx_auxcmd(struct IsdnCardState *cs, isdn_ctrl * ic)
 static void
 receive_emsg(struct IsdnCardState *cs)
 {
-       int flags;
+       long flags;
        int count = 5;
        u_char *ptr;
        struct sk_buff *skb;
@@ -961,7 +961,7 @@ HFCSX_l1hw(struct PStack *st, int pr, void *arg)
 {
        struct IsdnCardState *cs = (struct IsdnCardState *) st->l1.hardware;
        struct sk_buff *skb = arg;
-       int flags;
+       long flags;
 
        switch (pr) {
                case (PH_DATA | REQUEST):
@@ -1115,7 +1115,8 @@ void
 mode_hfcsx(struct BCState *bcs, int mode, int bc)
 {
        struct IsdnCardState *cs = bcs->cs;
-       int flags, fifo2;
+       long flags;
+       int fifo2;
 
        if (cs->debug & L1_DEB_HSCX)
                debugl1(cs, "HFCSX bchannel mode %d bchan %d/%d",
@@ -1338,7 +1339,7 @@ setstack_2b(struct PStack *st, struct BCState *bcs)
 static void
 hfcsx_bh(struct IsdnCardState *cs)
 {
-       int flags;
+       long flags;
 /*      struct PStack *stptr;
  */
        if (!cs)
@@ -1478,7 +1479,7 @@ setup_hfcsx(struct IsdnCard *card)
 {
        struct IsdnCardState *cs = card->cs;
        char tmp[64];
-       int flags;
+       long flags;
 
        strcpy(tmp, hfcsx_revision);
        printk(KERN_INFO "HiSax: HFC-SX driver Rev. %s\n", HiSax_getrev(tmp));
index 2ab9ed940e95eaa67796bfe98acaf5f115e53a69..d9dff796fb6cbd689f62a49bb27e01f280a07dca 100644 (file)
@@ -44,7 +44,8 @@ const char *dss1_revision = "$Revision: 2.30.6.1 $";
 static unsigned char new_invoke_id(struct PStack *p)
 {
        unsigned char retval;
-       int flags,i;
+       long flags;
+       int i;
   
        i = 32; /* maximum search depth */
 
@@ -72,7 +73,7 @@ static unsigned char new_invoke_id(struct PStack *p)
 /* free a used invoke id */
 /*************************/
 static void free_invoke_id(struct PStack *p, unsigned char id)
-{ int flags;
+{ long flags;
 
   if (!id) return; /* 0 = invalid value */
 
index 86245b474d086d06cab3221a59c318f783f980cb..25f25c24c12326e172fd0ad4b8779d80b7ff94f7 100644 (file)
@@ -48,7 +48,8 @@ const char *ni1_revision = "$Revision: 2.5.6.2 $";
 static unsigned char new_invoke_id(struct PStack *p)
 {
        unsigned char retval;
-       int flags,i;
+       long flags;
+       int i;
   
        i = 32; /* maximum search depth */
 
@@ -76,7 +77,7 @@ static unsigned char new_invoke_id(struct PStack *p)
 /* free a used invoke id */
 /*************************/
 static void free_invoke_id(struct PStack *p, unsigned char id)
-{ int flags;
+{ long flags;
 
   if (!id) return; /* 0 = invalid value */
 
index 9c68d92520dfe19553930397d642d15be822b526..d64b9cf7c2e19293f5e567079471a1fbf285728e 100644 (file)
@@ -607,7 +607,7 @@ icn_polldchan(unsigned long data)
        int left;
        u_char c;
        int ch;
-       int flags;
+       long flags;
        int i;
        u_char *p;
        isdn_ctrl cmd;
index e9601e0364cb514e56736991821a8e745b20ebab..e7d1ae1188e50e290be24ba43e053494f592ede2 100644 (file)
@@ -280,7 +280,7 @@ isdn_timer_funct(ulong dummy)
        }
        if (tf) 
        {
-               int flags;
+               long flags;
 
                save_flags(flags);
                cli();
@@ -292,7 +292,8 @@ isdn_timer_funct(ulong dummy)
 void
 isdn_timer_ctrl(int tf, int onoff)
 {
-       int flags, old_tflags;
+       long flags;
+       int old_tflags;
 
        save_flags(flags);
        cli();
@@ -2396,7 +2397,7 @@ static int __init isdn_init(void)
  */
 static void __exit isdn_exit(void)
 {
-       int flags;
+       long flags;
        int i;
 
 #ifdef CONFIG_ISDN_PPP
index 3f77f9c22ccabd5e524a6d5cac6d249c69d295cc..4cebdc5b7612c33cd81945f23f63e23db92e2bab 100644 (file)
@@ -583,7 +583,7 @@ isdn_net_dial(void)
        isdn_net_dev *p = dev->netdev;
        int anymore = 0;
        int i;
-       int flags;
+       ulong flags;
        isdn_ctrl cmd;
 
        while (p) {
@@ -2792,7 +2792,7 @@ isdn_net_setcfg(isdn_net_ioctl_cfg * cfg)
                        chidx = lp->pre_channel;
                }
                if (cfg->exclusive > 0) {
-                       int flags;
+                       ulong flags;
 
                        /* If binding is exclusive, try to grab the channel */
                        save_flags(flags);
@@ -3048,7 +3048,7 @@ isdn_net_delphone(isdn_net_ioctl_phone * phone)
        int inout = phone->outgoing & 1;
        isdn_net_phone *n;
        isdn_net_phone *m;
-       int flags;
+       ulong flags;
 
        if (p) {
                save_flags(flags);
@@ -3084,7 +3084,7 @@ isdn_net_rmallphone(isdn_net_dev * p)
 {
        isdn_net_phone *n;
        isdn_net_phone *m;
-       int flags;
+       ulong flags;
        int i;
 
        save_flags(flags);
@@ -3133,7 +3133,7 @@ isdn_net_force_hangup(char *name)
 static int
 isdn_net_realrm(isdn_net_dev * p, isdn_net_dev * q)
 {
-       int flags;
+       ulong flags;
 
        save_flags(flags);
        cli();
@@ -3219,7 +3219,7 @@ isdn_net_rm(char *name)
 int
 isdn_net_rmall(void)
 {
-       int flags;
+       ulong flags;
        int ret;
 
        /* Walk through netdev-chain */
index 73f4888632f43ef246652b8e931fc63e3c5488e2..f541fb653d0f5c209024e8c37e37387db0a92533 100644 (file)
@@ -323,7 +323,7 @@ isdnloop_polldchan(unsigned long data)
        int left;
        u_char c;
        int ch;
-       int flags;
+       unsigned long flags;
        u_char *p;
        isdn_ctrl cmd;
 
index 09c11f57da03fc223cd01476268243eb10b33294..2f600c06e6d385ce7c56585017863c29740e5ee2 100644 (file)
@@ -26,8 +26,7 @@
  *     +1 (416) 297-6433 Facsimile
  */
 #include <linux/kernel.h>
-
-inline char *strcpy(char *, const char *);
+#include <linux/string.h>
 
 int dbg_level = 0;
 static char dbg_funcname[255];
@@ -47,19 +46,6 @@ void dbg_func(char *func)
                printk("--> Entering function %s\n", dbg_funcname);
 }
 
-inline char *strcpy(char *dest, const char *src)
-{
-       char *i = dest;
-       char *j = (char *) src;
-
-       while(*j) {
-               *i = *j;
-               i++; j++;
-       }
-       *(++i) = 0;
-       return dest;
-}
-
 inline void pullphone(char *dn, char *str)
 {
        int i = 0;
index 30438c77fd3ce144e762ae800817e6adde5e111a..8b442ff26eae1aba8c0cc5df9a5ef726415372f4 100644 (file)
@@ -11,6 +11,7 @@ dep_tristate '  Linear (append) mode' CONFIG_MD_LINEAR $CONFIG_BLK_DEV_MD
 dep_tristate '  RAID-0 (striping) mode' CONFIG_MD_RAID0 $CONFIG_BLK_DEV_MD
 dep_tristate '  RAID-1 (mirroring) mode' CONFIG_MD_RAID1 $CONFIG_BLK_DEV_MD
 dep_tristate '  RAID-4/RAID-5 mode' CONFIG_MD_RAID5 $CONFIG_BLK_DEV_MD
+dep_tristate '  Multipath I/O support' CONFIG_MD_MULTIPATH $CONFIG_BLK_DEV_MD
 
 dep_tristate ' Logical volume manager (LVM) support' CONFIG_BLK_DEV_LVM $CONFIG_MD
 
index 041b186615d1cdc26f2c367c02b15001b49243ce..732844b8902c12bed39a2ad60e7fa3cfcc18f05e 100644 (file)
@@ -17,6 +17,7 @@ obj-$(CONFIG_MD_LINEAR)               += linear.o
 obj-$(CONFIG_MD_RAID0)         += raid0.o
 obj-$(CONFIG_MD_RAID1)         += raid1.o
 obj-$(CONFIG_MD_RAID5)         += raid5.o xor.o
+obj-$(CONFIG_MD_MULTIPATH)     += multipath.o
 obj-$(CONFIG_BLK_DEV_MD)       += md.o
 obj-$(CONFIG_BLK_DEV_LVM)      += lvm-mod.o
 
index 02472fa1410f99766457ff5696fcbf1410021729..52d88f7668fbc8df5e24fa5557b860a612c5e281 100644 (file)
@@ -46,9 +46,6 @@
 
 #include <asm/unaligned.h>
 
-extern asmlinkage int sys_sched_yield(void);
-extern asmlinkage long sys_setsid(void);
-
 #define MAJOR_NR MD_MAJOR
 #define MD_DRIVER
 
@@ -114,7 +111,7 @@ static mdk_thread_t *md_recovery_thread;
 
 int md_size[MAX_MD_DEVS];
 
-extern struct block_device_operations md_fops;
+static struct block_device_operations md_fops;
 static devfs_handle_t devfs_handle;
 
 static struct gendisk md_gendisk=
@@ -644,10 +641,9 @@ static int lock_rdev (mdk_rdev_t *rdev)
        bdev = bdget(rdev->dev);
        if (bdev == NULL)
                return -ENOMEM;
-       err = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_FILE);
-       if (!err) {
+       err = blkdev_get(bdev, FMODE_READ|FMODE_WRITE, 0, BDEV_RAW);
+       if (!err)
                rdev->bdev = bdev;
-       }
        return err;
 }
 
@@ -655,7 +651,7 @@ static void unlock_rdev (mdk_rdev_t *rdev)
 {
        if (!rdev->bdev)
                MD_BUG();
-       blkdev_put(rdev->bdev, BDEV_FILE);
+       blkdev_put(rdev->bdev, BDEV_RAW);
        bdput(rdev->bdev);
        rdev->bdev = NULL;
 }
@@ -771,8 +767,10 @@ static void print_sb(mdp_super_t *sb)
                mdp_disk_t *desc;
 
                desc = sb->disks + i;
-               printk("md:     D %2d: ", i);
-               print_desc(desc);
+               if (desc->number || desc->major || desc->minor || desc->raid_disk || (desc->state && (desc->state != 4))) {
+                       printk("     D %2d: ", i);
+                       print_desc(desc);
+               }
        }
        printk("md:     THIS: ");
        print_desc(&sb->this_disk);
@@ -830,6 +828,7 @@ static int sb_equal ( mdp_super_t *sb1, mdp_super_t *sb2)
 
        if (!tmp1 || !tmp2) {
                ret = 0;
+               printk(KERN_INFO "md.c: sb1 is not equal to sb2!\n");
                goto abort;
        }
 
@@ -910,7 +909,7 @@ static int write_disk_sb(mdk_rdev_t * rdev)
        sb_offset = calc_dev_sboffset(dev, rdev->mddev, 1);
        if (rdev->sb_offset != sb_offset) {
                printk("%s's sb offset has changed from %ld to %ld, skipping\n",
-                      partition_name(dev), rdev->sb_offset, sb_offset);
+                       partition_name(dev), rdev->sb_offset, sb_offset);
                goto skip;
        }
        /*
@@ -921,7 +920,7 @@ static int write_disk_sb(mdk_rdev_t * rdev)
        size = calc_dev_size(dev, rdev->mddev, 1);
        if (size != rdev->size) {
                printk("%s's size has changed from %ld to %ld since import, skipping\n",
-                      partition_name(dev), rdev->size, size);
+                       partition_name(dev), rdev->size, size);
                goto skip;
        }
 
@@ -982,7 +981,7 @@ static int sync_sbs(mddev_t * mddev)
        struct md_list_head *tmp;
 
        ITERATE_RDEV(mddev,rdev,tmp) {
-               if (rdev->faulty)
+               if (rdev->faulty || rdev->alias_device)
                        continue;
                sb = rdev->sb;
                *sb = *mddev->sb;
@@ -1029,8 +1028,11 @@ repeat:
                printk("md: ");
                if (rdev->faulty)
                        printk("(skipping faulty ");
+               if (rdev->alias_device)
+                       printk("(skipping alias ");
+
                printk("%s ", partition_name(rdev->dev));
-               if (!rdev->faulty) {
+               if (!rdev->faulty && !rdev->alias_device) {
                        printk("[events: %08lx]",
                                (unsigned long)rdev->sb->events_lo);
                        err += write_disk_sb(rdev);
@@ -1115,9 +1117,14 @@ static int md_import_device (kdev_t newdev, int on_disk)
                        goto abort_free;
                }
 
-               rdev->old_dev = MKDEV(rdev->sb->this_disk.major,
-                                       rdev->sb->this_disk.minor);
-               rdev->desc_nr = rdev->sb->this_disk.number;
+               if (rdev->sb->level != -4) {
+                       rdev->old_dev = MKDEV(rdev->sb->this_disk.major,
+                                               rdev->sb->this_disk.minor);
+                       rdev->desc_nr = rdev->sb->this_disk.number;
+               } else {
+                       rdev->old_dev = MKDEV(0, 0);
+                       rdev->desc_nr = -1;
+               }
        }
        md_list_add(&rdev->all, &all_raid_disks);
        MD_INIT_LIST_HEAD(&rdev->pending);
@@ -1157,7 +1164,7 @@ abort_free:
 
 static int analyze_sbs (mddev_t * mddev)
 {
-       int out_of_date = 0, i;
+       int out_of_date = 0, i, first;
        struct md_list_head *tmp, *tmp2;
        mdk_rdev_t *rdev, *rdev2, *freshest;
        mdp_super_t *sb;
@@ -1251,7 +1258,7 @@ static int analyze_sbs (mddev_t * mddev)
         */
        ITERATE_RDEV(mddev,rdev,tmp) {
                /*
-                * Kick all non-fresh devices faulty
+                * Kick all non-fresh devices
                 */
                __u64 ev1, ev2;
                ev1 = md_event(rdev->sb);
@@ -1269,9 +1276,10 @@ static int analyze_sbs (mddev_t * mddev)
         * Fix up changed device names ... but only if this disk has a
         * recent update time. Use faulty checksum ones too.
         */
+       if (mddev->sb->level != -4)
        ITERATE_RDEV(mddev,rdev,tmp) {
                __u64 ev1, ev2, ev3;
-               if (rdev->faulty) { /* REMOVEME */
+               if (rdev->faulty || rdev->alias_device) {
                        MD_BUG();
                        goto abort;
                }
@@ -1280,7 +1288,7 @@ static int analyze_sbs (mddev_t * mddev)
                ev3 = ev2;
                --ev3;
                if ((rdev->dev != rdev->old_dev) &&
-                   ((ev1 == ev2) || (ev1 == ev3))) {
+                       ((ev1 == ev2) || (ev1 == ev3))) {
                        mdp_disk_t *desc;
 
                        printk("md: device name has changed from %s to %s since last import!\n", partition_name(rdev->old_dev), partition_name(rdev->dev));
@@ -1319,8 +1327,13 @@ static int analyze_sbs (mddev_t * mddev)
 
                /*
                 * We kick faulty devices/descriptors immediately.
+                *
+                * Note: multipath devices are a special case.  Since we
+                * were able to read the superblock on the path, we don't
+                * care if it was previously marked as faulty, it's up now
+                * so enable it.
                 */
-               if (disk_faulty(desc)) {
+               if (disk_faulty(desc) && mddev->sb->level != -4) {
                        found = 0;
                        ITERATE_RDEV(mddev,rdev,tmp) {
                                if (rdev->desc_nr != desc->number)
@@ -1339,6 +1352,15 @@ static int analyze_sbs (mddev_t * mddev)
                        }
                        remove_descriptor(desc, sb);
                        continue;
+               } else if (disk_faulty(desc)) {
+                       /*
+                        * multipath entry marked as faulty, unfaulty it
+                        */
+                       rdev = find_rdev(mddev, dev);
+                       if(rdev)
+                               mark_disk_spare(desc);
+                       else
+                               remove_descriptor(desc, sb);
                }
 
                if (dev == MKDEV(0,0))
@@ -1348,6 +1370,17 @@ static int analyze_sbs (mddev_t * mddev)
                 */
                found = 0;
                ITERATE_RDEV(mddev,rdev,tmp) {
+                       /*
+                        * Multi-path IO special-case: since we have no
+                        * this_disk descriptor at auto-detect time,
+                        * we cannot check rdev->number.
+                        * We can check the device though.
+                        */
+                       if ((sb->level == -4) && (rdev->dev ==
+                                       MKDEV(desc->major,desc->minor))) {
+                               found = 1;
+                               break;
+                       }
                        if (rdev->desc_nr == desc->number) {
                                found = 1;
                                break;
@@ -1364,6 +1397,7 @@ static int analyze_sbs (mddev_t * mddev)
         * Double check wether all devices mentioned in the
         * superblock are in the rdev ring.
         */
+       first = 1;
        for (i = 0; i < MD_SB_DISKS; i++) {
                mdp_disk_t *desc;
                kdev_t dev;
@@ -1384,35 +1418,63 @@ static int analyze_sbs (mddev_t * mddev)
                        MD_BUG();
                        goto abort;
                }
-       }
-
-       /*
-        * Do a final reality check.
-        */
-       ITERATE_RDEV(mddev,rdev,tmp) {
-               if (rdev->desc_nr == -1) {
-                       MD_BUG();
-                       goto abort;
-               }
                /*
-                * is the desc_nr unique?
+                * In the case of Multipath-IO, we have no
+                * other information source to find out which
+                * disk is which, only the position of the device
+                * in the superblock:
                 */
-               ITERATE_RDEV(mddev,rdev2,tmp2) {
-                       if ((rdev2 != rdev) &&
-                                       (rdev2->desc_nr == rdev->desc_nr)) {
+               if (mddev->sb->level == -4) {
+                       if ((rdev->desc_nr != -1) && (rdev->desc_nr != i)) {
                                MD_BUG();
                                goto abort;
                        }
+                       rdev->desc_nr = i;
+                       if (!first)
+                               rdev->alias_device = 1;
+                       else
+                               first = 0;
                }
-               /*
-                * is the device unique?
-                */
-               ITERATE_RDEV(mddev,rdev2,tmp2) {
-                       if ((rdev2 != rdev) &&
-                                       (rdev2->dev == rdev->dev)) {
+       }
+       /*
+        * Kick all rdevs that are not in the
+        * descriptor array:
+        */
+       ITERATE_RDEV(mddev,rdev,tmp) {
+               if (rdev->desc_nr == -1)
+                       kick_rdev_from_array(rdev);
+       }
+       /*
+        * Do a final reality check.
+        */
+       if (mddev->sb->level != -4) {
+               ITERATE_RDEV(mddev,rdev,tmp) {
+                       if (rdev->desc_nr == -1) {
                                MD_BUG();
                                goto abort;
                        }
+                       /*
+                        * is the desc_nr unique?
+                        */
+                       ITERATE_RDEV(mddev,rdev2,tmp2) {
+                               if ((rdev2 != rdev) &&
+                                               (rdev2->desc_nr == rdev->desc_nr)) {
+                                       MD_BUG();
+                                       goto abort;
+                               }
+                       }
+                       /*
+                        * is the device unique?
+                        */
+                       ITERATE_RDEV(mddev,rdev2,tmp2) {
+                               if ((rdev2 != rdev) &&
+                                               (rdev2->dev == rdev->dev)) {
+                                       MD_BUG();
+                                       goto abort;
+                               }
+                       }
                }
        }
 
@@ -1473,6 +1535,9 @@ static int device_size_calculation (mddev_t * mddev)
        }
 
        switch (sb->level) {
+               case -4:
+                       data_disks = 1;
+                       break;
                case -3:
                        data_disks = 1;
                        break;
@@ -1507,6 +1572,7 @@ static int device_size_calculation (mddev_t * mddev)
                if (readahead < data_disks * (MAX_SECTORS>>(PAGE_SHIFT-9))*2)
                        readahead = data_disks * (MAX_SECTORS>>(PAGE_SHIFT-9))*2;
        } else {
+               // (no multipath branch - it uses the default setting)
                if (sb->level == -3)
                        readahead = 0;
        }
@@ -1569,38 +1635,41 @@ static int do_md_run (mddev_t * mddev)
        mddev->param.chunk_size = chunk_size;
        mddev->param.personality = pnum;
 
-       if (chunk_size > MAX_CHUNK_SIZE) {
-               printk(TOO_BIG_CHUNKSIZE, chunk_size, MAX_CHUNK_SIZE);
-               return -EINVAL;
-       }
-       /*
-        * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
-        */
-       if ( (1 << ffz(~chunk_size)) != chunk_size) {
-               MD_BUG();
-               return -EINVAL;
-       }
-       if (chunk_size < PAGE_SIZE) {
-               printk(TOO_SMALL_CHUNKSIZE, chunk_size, PAGE_SIZE);
-               return -EINVAL;
-       }
+       if ((pnum != MULTIPATH) && (pnum != RAID1) && (pnum != LINEAR)) {
+               if (!chunk_size) {
+                       /*
+                        * 'default chunksize' in the old md code used to
+                        * be PAGE_SIZE, baaad.
+                        * we abort here to be on the safe side. We dont
+                        * want to continue the bad practice.
+                        */
+                       printk(BAD_CHUNKSIZE);
+                       return -EINVAL;
+               }
+               if (chunk_size > MAX_CHUNK_SIZE) {
+                       printk(TOO_BIG_CHUNKSIZE, chunk_size, MAX_CHUNK_SIZE);
+                       return -EINVAL;
+               }
+               /*
+                * chunk-size has to be a power of 2 and multiples of PAGE_SIZE
+                */
+               if ( (1 << ffz(~chunk_size)) != chunk_size) {
+                       MD_BUG();
+                       return -EINVAL;
+               }
+               if (chunk_size < PAGE_SIZE) {
+                       printk(TOO_SMALL_CHUNKSIZE, chunk_size, PAGE_SIZE);
+                       return -EINVAL;
+               }
+       } else
+               if (chunk_size)
+                       printk(KERN_INFO "RAID level %d does not need chunksize! Continuing anyway.\n", mddev->sb->level);
 
        if (pnum >= MAX_PERSONALITY) {
                MD_BUG();
                return -EINVAL;
        }
 
-       if ((pnum != RAID1) && (pnum != LINEAR) && !chunk_size) {
-               /*
-                * 'default chunksize' in the old md code used to
-                * be PAGE_SIZE, baaad.
-                * we abort here to be on the safe side. We dont
-                * want to continue the bad practice.
-                */
-               printk(BAD_CHUNKSIZE);
-               return -EINVAL;
-       }
-
        if (!pers[pnum])
        {
 #ifdef CONFIG_KMOD
@@ -1609,7 +1678,11 @@ static int do_md_run (mddev_t * mddev)
                request_module (module_name);
                if (!pers[pnum])
 #endif
+               {
+                       printk(KERN_ERR "md.c: personality %d is not loaded!\n",
+                               pnum);
                        return -EINVAL;
+               }
        }
 
        if (device_size_calculation(mddev))
@@ -1627,7 +1700,7 @@ static int do_md_run (mddev_t * mddev)
                        continue;
                invalidate_device(rdev->dev, 1);
                if (get_hardsect_size(rdev->dev)
-                   > md_hardsect_sizes[mdidx(mddev)]) 
+                       > md_hardsect_sizes[mdidx(mddev)]) 
                        md_hardsect_sizes[mdidx(mddev)] =
                                get_hardsect_size(rdev->dev);
        }
@@ -1652,7 +1725,7 @@ static int do_md_run (mddev_t * mddev)
         */
        md_hd_struct[mdidx(mddev)].start_sect = 0;
        register_disk(&md_gendisk, MKDEV(MAJOR_NR,mdidx(mddev)),
-                     1, &md_fops, md_size[mdidx(mddev)]<<1);
+                       1, &md_fops, md_size[mdidx(mddev)]<<1);
 
        read_ahead[MD_MAJOR] = 1024;
        return (0);
@@ -1688,8 +1761,11 @@ static int restart_array (mddev_t *mddev)
                md_recover_arrays();
                if (mddev->pers->restart_resync)
                        mddev->pers->restart_resync(mddev);
-       } else
+       } else {
+               printk (KERN_ERR "md.c: md%d has no personality assigned.\n",
+                       mdidx(mddev));
                err = -EINVAL;
+       }
 
 out:
        return err;
@@ -1810,7 +1886,7 @@ static void autorun_array (mddev_t *mddev)
        ITERATE_RDEV(mddev,rdev,tmp) {
                printk("<%s>", partition_name(rdev->dev));
        }
-       printk("\nmd: now!\n");
+       printk("\n");
 
        err = do_md_run (mddev);
        if (err) {
@@ -2021,8 +2097,10 @@ static int get_array_info (mddev_t * mddev, void * arg)
 {
        mdu_array_info_t info;
 
-       if (!mddev->sb)
+       if (!mddev->sb) {
+               MD_BUG();
                return -EINVAL;
+       }
 
        SET_FROM_SB(major_version);
        SET_FROM_SB(minor_version);
@@ -2109,7 +2187,7 @@ static int add_new_disk (mddev_t * mddev, mdu_disk_info_t *info)
                }
                if (mddev->nb_dev) {
                        mdk_rdev_t *rdev0 = md_list_entry(mddev->disks.next,
-                                                         mdk_rdev_t, same_set);
+                                                       mdk_rdev_t, same_set);
                        if (!uuid_equal(rdev0, rdev)) {
                                printk("md: %s has different UUID to %s\n", partition_name(rdev->dev), partition_name(rdev0->dev));
                                export_rdev(rdev);
@@ -2126,8 +2204,11 @@ static int add_new_disk (mddev_t * mddev, mdu_disk_info_t *info)
        }
 
        nr = info->number;
-       if (nr >= mddev->sb->nr_disks)
+       if (nr >= mddev->sb->nr_disks) {
+               MD_BUG();
                return -EINVAL;
+       }
+
 
        SET_SB(number);
        SET_SB(major);
@@ -2155,8 +2236,6 @@ static int add_new_disk (mddev_t * mddev, mdu_disk_info_t *info)
                persistent = !mddev->sb->not_persistent;
                if (!persistent)
                        printk("md: nonpersistent superblock ...\n");
-               if (!mddev->sb->chunk_size)
-                       printk("md: no chunksize?\n");
 
                size = calc_dev_size(dev, mddev, persistent);
                rdev->sb_offset = calc_dev_sboffset(dev, mddev, persistent);
@@ -2174,6 +2253,43 @@ static int add_new_disk (mddev_t * mddev, mdu_disk_info_t *info)
 }
 #undef SET_SB
 
+static int hot_generate_error (mddev_t * mddev, kdev_t dev)
+{
+       struct request_queue *q;
+       mdk_rdev_t *rdev;
+       mdp_disk_t *disk;
+       if (!mddev->pers)
+               return -ENODEV;
+       printk("trying to generate %s error in md%d ... \n",
+               partition_name(dev), mdidx(mddev));
+       rdev = find_rdev(mddev, dev);
+       if (!rdev) {
+               MD_BUG();
+               return -ENXIO;
+       }
+       if (rdev->desc_nr == -1) {
+               MD_BUG();
+               return -EINVAL;
+       }
+       disk = &mddev->sb->disks[rdev->desc_nr];
+       if (!disk_active(disk))
+               return -ENODEV;
+       q = blk_get_queue(rdev->dev);
+       if (!q) {
+               MD_BUG();
+               return -ENODEV;
+       }
+       printk("okay, generating error!\n");
+//     q->oneshot_error = 1; // disabled for now
+       return 0;
+}
+
 static int hot_remove_disk (mddev_t * mddev, kdev_t dev)
 {
        int err;
@@ -2201,16 +2317,20 @@ static int hot_remove_disk (mddev_t * mddev, kdev_t dev)
                return -EINVAL;
        }
        disk = &mddev->sb->disks[rdev->desc_nr];
-       if (disk_active(disk))
+       if (disk_active(disk)) {
+               MD_BUG();
                goto busy;
+       }
        if (disk_removed(disk)) {
                MD_BUG();
                return -EINVAL;
        }
        
        err = mddev->pers->diskop(mddev, &disk, DISKOP_HOT_REMOVE_DISK);
-       if (err == -EBUSY)
+       if (err == -EBUSY) {
+               MD_BUG();
                goto busy;
+       }
        if (err) {
                MD_BUG();
                return -EINVAL;
@@ -2425,7 +2545,6 @@ static int set_disk_faulty (mddev_t *mddev, kdev_t dev)
 {
        int ret;
 
-       fsync_dev(mddev_to_kdev(mddev));
        ret = md_error(mddev, dev);
        return ret;
 }
@@ -2444,8 +2563,10 @@ static int md_ioctl (struct inode *inode, struct file *file,
 
        dev = inode->i_rdev;
        minor = MINOR(dev);
-       if (minor >= MAX_MD_DEVS)
+       if (minor >= MAX_MD_DEVS) {
+               MD_BUG();
                return -EINVAL;
+       }
 
        /*
         * Commands dealing with the RAID driver but not any
@@ -2469,16 +2590,17 @@ static int md_ioctl (struct inode *inode, struct file *file,
                        goto done;
 #endif
 
-               case BLKGETSIZE:   /* Return device size */
+               case BLKGETSIZE:        /* Return device size */
                        if (!arg) {
                                err = -EINVAL;
+                               MD_BUG();
                                goto abort;
                        }
                        err = md_put_user(md_hd_struct[minor].nr_sects,
                                                (long *) arg);
                        goto done;
 
-               case BLKGETSIZE64:   /* Return device size */
+               case BLKGETSIZE64:      /* Return device size */
                        err = md_put_user((u64)md_hd_struct[minor].nr_sects << 9,
                                                (u64 *) arg);
                        goto done;
@@ -2533,7 +2655,7 @@ static int md_ioctl (struct inode *inode, struct file *file,
 
                        if (mddev->sb) {
                                printk("md: array md%d already has a superblock!\n",
-                                      mdidx(mddev));
+                                       mdidx(mddev));
                                err = -EBUSY;
                                goto abort_unlock;
                        }
@@ -2662,6 +2784,9 @@ static int md_ioctl (struct inode *inode, struct file *file,
                                err = add_new_disk(mddev, &info);
                        goto done_unlock;
                }
+               case HOT_GENERATE_ERROR:
+                       err = hot_generate_error(mddev, (kdev_t)arg);
+                       goto done_unlock;
                case HOT_REMOVE_DISK:
                        err = hot_remove_disk(mddev, (kdev_t)arg);
                        goto done_unlock;
@@ -2807,7 +2932,8 @@ int md_thread(void * arg)
                remove_wait_queue(&thread->wqueue, &wait);
                clear_bit(THREAD_WAKEUP, &thread->flags);
 
-               if ((run=thread->run)) {
+               run = thread->run;
+               if (run) {
                        run(thread->data);
                        run_task_queue(&tq_disk);
                }
@@ -2905,7 +3031,7 @@ int md_error (mddev_t *mddev, kdev_t rdev)
        if (!rrdev || rrdev->faulty)
                return 0;
        if (mddev->pers->error_handler == NULL
-           || mddev->pers->error_handler(mddev,rdev) <= 0) {
+                       || mddev->pers->error_handler(mddev,rdev) <= 0) {
                free_disk_sb(rrdev);
                rrdev->faulty = 1;
        } else
@@ -2954,7 +3080,7 @@ static int status_resync (char * page, mddev_t * mddev)
        unsigned long max_blocks, resync, res, dt, db, rt;
 
        resync = (mddev->curr_resync - atomic_read(&mddev->recovery_active))/2;
-       max_blocks = mddev->sb->size;
+       max_blocks = mddev->sb->size << 1;
 
        /*
         * Should not happen.
@@ -3081,28 +3207,34 @@ static int md_status_read_proc(char *page, char **start, off_t off,
 
 int register_md_personality (int pnum, mdk_personality_t *p)
 {
-       if (pnum >= MAX_PERSONALITY)
+       if (pnum >= MAX_PERSONALITY) {
+               MD_BUG();
                return -EINVAL;
+       }
 
-       if (pers[pnum])
+       if (pers[pnum]) {
+               MD_BUG();
                return -EBUSY;
+       }
 
        pers[pnum] = p;
-       printk(KERN_INFO "md: %s personality registered\n", p->name);
+       printk(KERN_INFO "md: %s personality registered as nr %d\n", p->name, pnum);
        return 0;
 }
 
 int unregister_md_personality (int pnum)
 {
-       if (pnum >= MAX_PERSONALITY)
+       if (pnum >= MAX_PERSONALITY) {
+               MD_BUG();
                return -EINVAL;
+       }
 
        printk(KERN_INFO "md: %s personality unregistered\n", pers[pnum]->name);
        pers[pnum] = NULL;
        return 0;
 }
 
-static mdp_disk_t *get_spare(mddev_t *mddev)
+mdp_disk_t *get_spare(mddev_t *mddev)
 {
        mdp_super_t *sb = mddev->sb;
        mdp_disk_t *disk;
@@ -3434,7 +3566,7 @@ int md_notify_reboot(struct notifier_block *this,
        mddev_t *mddev;
 
        if ((code == MD_SYS_DOWN) || (code == MD_SYS_HALT)
-                                 || (code == MD_SYS_POWER_OFF)) {
+                                       || (code == MD_SYS_POWER_OFF)) {
 
                printk(KERN_INFO "md: stopping all md devices.\n");
 
@@ -3452,9 +3584,9 @@ int md_notify_reboot(struct notifier_block *this,
 }
 
 struct notifier_block md_notifier = {
-       md_notify_reboot,
-       NULL,
-       0
+       notifier_call:  md_notify_reboot,
+       next:           NULL,
+       priority:       INT_MAX, /* before any real devices */
 };
 
 static void md_geninit (void)
@@ -3565,7 +3697,7 @@ static void autostart_arrays (void)
 
                if (md_import_device(dev,1)) {
                        printk(KERN_ALERT "md: could not import %s!\n",
-                              partition_name(dev));
+                               partition_name(dev));
                        continue;
                }
                /*
@@ -3634,7 +3766,7 @@ static int md__init md_setup(char *str)
        case 2: /* could be 0 or -1.. */
                if (level == 0 || level == -1) {
                        if (get_option(&str, &factor) != 2 ||   /* Chunk Size */
-                           get_option(&str, &fault) != 2) {
+                                       get_option(&str, &fault) != 2) {
                                printk("md: Too few arguments supplied to md=.\n");
                                return 0;
                        }
@@ -3698,7 +3830,7 @@ void md__init md_setup_drive(void)
 
                        dev = name_to_kdev_t(devname);
                        handle = devfs_find_handle(NULL, devname, MAJOR (dev), MINOR (dev),
-                                                   DEVFS_SPECIAL_BLK, 1);
+                                                       DEVFS_SPECIAL_BLK, 1);
                        if (handle != 0) {
                                unsigned major, minor;
                                devfs_get_maj_min(handle, &major, &minor);
@@ -3876,4 +4008,5 @@ MD_EXPORT_SYMBOL(find_rdev_nr);
 MD_EXPORT_SYMBOL(md_interrupt_thread);
 MD_EXPORT_SYMBOL(mddev_map);
 MD_EXPORT_SYMBOL(md_check_ordering);
+MD_EXPORT_SYMBOL(get_spare);
 
diff --git a/drivers/md/multipath.c b/drivers/md/multipath.c
new file mode 100644 (file)
index 0000000..f4f3a9f
--- /dev/null
@@ -0,0 +1,1261 @@
+/*
+ * multipath.c : Multiple Devices driver for Linux
+ *
+ * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
+ *
+ * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
+ *
+ * MULTIPATH management functions.
+ *
+ * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
+ *
+ * Fixes to reconstruction by Jakob Ã˜stergaard" <jakob@ostenfeld.dk>
+ * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
+ *
+ * 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, or (at your option)
+ * any later version.
+ *
+ * You should have received a copy of the GNU General Public License
+ * (for example /usr/src/linux/COPYING); if not, write to the Free
+ * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/raid/multipath.h>
+#include <asm/atomic.h>
+
+#define MAJOR_NR MD_MAJOR
+#define MD_DRIVER
+#define MD_PERSONALITY
+
+#define MAX_WORK_PER_DISK 128
+
+/*
+ * The following can be used to debug the driver
+ */
+#define MULTIPATH_DEBUG        0
+
+#if MULTIPATH_DEBUG
+#define PRINTK(x...)   printk(x)
+#define inline
+#define __inline__
+#else
+#define PRINTK(x...)  do { } while (0)
+#endif
+
+
+static mdk_personality_t multipath_personality;
+static md_spinlock_t retry_list_lock = MD_SPIN_LOCK_UNLOCKED;
+struct multipath_bh *multipath_retry_list = NULL, **multipath_retry_tail;
+
+static int multipath_diskop(mddev_t *mddev, mdp_disk_t **d, int state);
+
+struct buffer_head *multipath_alloc_bh(multipath_conf_t *conf, int cnt)
+{
+       /* return a linked list of "cnt" struct buffer_heads.
+        * don't take any off the free list unless we know we can
+        * get all we need, otherwise we could deadlock
+        */
+       struct buffer_head *bh=NULL;
+
+       while(cnt) {
+               struct buffer_head *t;
+               md_spin_lock_irq(&conf->device_lock);
+               if (conf->freebh_cnt >= cnt)
+                       while (cnt) {
+                               t = conf->freebh;
+                               conf->freebh = t->b_next;
+                               t->b_next = bh;
+                               bh = t;
+                               t->b_state = 0;
+                               conf->freebh_cnt--;
+                               cnt--;
+                       }
+               md_spin_unlock_irq(&conf->device_lock);
+               if (cnt == 0)
+                       break;
+               t = (struct buffer_head *)kmalloc(sizeof(struct buffer_head), GFP_NOIO);
+               if (t) {
+                       memset(t, 0, sizeof(*t));
+                       t->b_next = bh;
+                       bh = t;
+                       cnt--;
+               } else {
+                       PRINTK("waiting for %d bh\n", cnt);
+                       wait_event(conf->wait_buffer, conf->freebh_cnt >= cnt);
+               }
+       }
+       return bh;
+}
+
+static inline void multipath_free_bh(multipath_conf_t *conf, struct buffer_head *bh)
+{
+       unsigned long flags;
+       spin_lock_irqsave(&conf->device_lock, flags);
+       while (bh) {
+               struct buffer_head *t = bh;
+               bh=bh->b_next;
+               if (t->b_pprev == NULL)
+                       kfree(t);
+               else {
+                       t->b_next= conf->freebh;
+                       conf->freebh = t;
+                       conf->freebh_cnt++;
+               }
+       }
+       spin_unlock_irqrestore(&conf->device_lock, flags);
+       wake_up(&conf->wait_buffer);
+}
+
+static int multipath_grow_bh(multipath_conf_t *conf, int cnt)
+{
+       /* allocate cnt buffer_heads, possibly less if kalloc fails */
+       int i = 0;
+
+       while (i < cnt) {
+               struct buffer_head *bh;
+               bh = kmalloc(sizeof(*bh), GFP_KERNEL);
+               if (!bh) break;
+               memset(bh, 0, sizeof(*bh));
+
+               md_spin_lock_irq(&conf->device_lock);
+               bh->b_pprev = &conf->freebh;
+               bh->b_next = conf->freebh;
+               conf->freebh = bh;
+               conf->freebh_cnt++;
+               md_spin_unlock_irq(&conf->device_lock);
+
+               i++;
+       }
+       return i;
+}
+
+static int multipath_shrink_bh(multipath_conf_t *conf, int cnt)
+{
+       /* discard cnt buffer_heads, if we can find them */
+       int i = 0;
+
+       md_spin_lock_irq(&conf->device_lock);
+       while ((i < cnt) && conf->freebh) {
+               struct buffer_head *bh = conf->freebh;
+               conf->freebh = bh->b_next;
+               kfree(bh);
+               i++;
+               conf->freebh_cnt--;
+       }
+       md_spin_unlock_irq(&conf->device_lock);
+       return i;
+}
+               
+
+static struct multipath_bh *multipath_alloc_mpbh(multipath_conf_t *conf)
+{
+       struct multipath_bh *r1_bh = NULL;
+
+       do {
+               md_spin_lock_irq(&conf->device_lock);
+               if (conf->freer1) {
+                       r1_bh = conf->freer1;
+                       conf->freer1 = r1_bh->next_r1;
+                       r1_bh->next_r1 = NULL;
+                       r1_bh->state = 0;
+                       r1_bh->bh_req.b_state = 0;
+               }
+               md_spin_unlock_irq(&conf->device_lock);
+               if (r1_bh)
+                       return r1_bh;
+               r1_bh = (struct multipath_bh *) kmalloc(sizeof(struct multipath_bh),
+                                       GFP_NOIO);
+               if (r1_bh) {
+                       memset(r1_bh, 0, sizeof(*r1_bh));
+                       return r1_bh;
+               }
+               wait_event(conf->wait_buffer, conf->freer1);
+       } while (1);
+}
+
+static inline void multipath_free_mpbh(struct multipath_bh *r1_bh)
+{
+       struct buffer_head *bh = r1_bh->multipath_bh_list;
+       multipath_conf_t *conf = mddev_to_conf(r1_bh->mddev);
+
+       r1_bh->multipath_bh_list = NULL;
+
+       if (test_bit(MPBH_PreAlloc, &r1_bh->state)) {
+               unsigned long flags;
+               spin_lock_irqsave(&conf->device_lock, flags);
+               r1_bh->next_r1 = conf->freer1;
+               conf->freer1 = r1_bh;
+               spin_unlock_irqrestore(&conf->device_lock, flags);
+       } else {
+               kfree(r1_bh);
+       }
+       multipath_free_bh(conf, bh);
+}
+
+static int multipath_grow_mpbh (multipath_conf_t *conf, int cnt)
+{
+       int i = 0;
+
+       while (i < cnt) {
+               struct multipath_bh *r1_bh;
+               r1_bh = (struct multipath_bh*)kmalloc(sizeof(*r1_bh), GFP_KERNEL);
+               if (!r1_bh)
+                       break;
+               memset(r1_bh, 0, sizeof(*r1_bh));
+
+               md_spin_lock_irq(&conf->device_lock);
+               set_bit(MPBH_PreAlloc, &r1_bh->state);
+               r1_bh->next_r1 = conf->freer1;
+               conf->freer1 = r1_bh;
+               md_spin_unlock_irq(&conf->device_lock);
+
+               i++;
+       }
+       return i;
+}
+
+static void multipath_shrink_mpbh(multipath_conf_t *conf)
+{
+       md_spin_lock_irq(&conf->device_lock);
+       while (conf->freer1) {
+               struct multipath_bh *r1_bh = conf->freer1;
+               conf->freer1 = r1_bh->next_r1;
+               kfree(r1_bh);
+       }
+       md_spin_unlock_irq(&conf->device_lock);
+}
+
+
+
+static inline void multipath_free_buf(struct multipath_bh *r1_bh)
+{
+       unsigned long flags;
+       struct buffer_head *bh = r1_bh->multipath_bh_list;
+       multipath_conf_t *conf = mddev_to_conf(r1_bh->mddev);
+       r1_bh->multipath_bh_list = NULL;
+       
+       spin_lock_irqsave(&conf->device_lock, flags);
+       r1_bh->next_r1 = conf->freebuf;
+       conf->freebuf = r1_bh;
+       spin_unlock_irqrestore(&conf->device_lock, flags);
+       multipath_free_bh(conf, bh);
+}
+
+static int multipath_map (mddev_t *mddev, kdev_t *rdev)
+{
+       multipath_conf_t *conf = mddev_to_conf(mddev);
+       int i, disks = MD_SB_DISKS;
+
+       /*
+        * Later we do read balancing on the read side 
+        * now we use the first available disk.
+        */
+
+       for (i = 0; i < disks; i++) {
+               if (conf->multipaths[i].operational) {
+                       *rdev = conf->multipaths[i].dev;
+                       return (0);
+               }
+       }
+
+       printk (KERN_ERR "multipath_map(): no more operational IO paths?\n");
+       return (-1);
+}
+
+static void multipath_reschedule_retry (struct multipath_bh *r1_bh)
+{
+       unsigned long flags;
+       mddev_t *mddev = r1_bh->mddev;
+       multipath_conf_t *conf = mddev_to_conf(mddev);
+
+       md_spin_lock_irqsave(&retry_list_lock, flags);
+       if (multipath_retry_list == NULL)
+               multipath_retry_tail = &multipath_retry_list;
+       *multipath_retry_tail = r1_bh;
+       multipath_retry_tail = &r1_bh->next_r1;
+       r1_bh->next_r1 = NULL;
+       md_spin_unlock_irqrestore(&retry_list_lock, flags);
+       md_wakeup_thread(conf->thread);
+}
+
+
+static void inline io_request_done(unsigned long sector, multipath_conf_t *conf, int phase)
+{
+       unsigned long flags;
+       spin_lock_irqsave(&conf->segment_lock, flags);
+       if (sector < conf->start_active)
+               conf->cnt_done--;
+       else if (sector >= conf->start_future && conf->phase == phase)
+               conf->cnt_future--;
+       else if (!--conf->cnt_pending)
+               wake_up(&conf->wait_ready);
+
+       spin_unlock_irqrestore(&conf->segment_lock, flags);
+}
+
+static void inline sync_request_done (unsigned long sector, multipath_conf_t *conf)
+{
+       unsigned long flags;
+       spin_lock_irqsave(&conf->segment_lock, flags);
+       if (sector >= conf->start_ready)
+               --conf->cnt_ready;
+       else if (sector >= conf->start_active) {
+               if (!--conf->cnt_active) {
+                       conf->start_active = conf->start_ready;
+                       wake_up(&conf->wait_done);
+               }
+       }
+       spin_unlock_irqrestore(&conf->segment_lock, flags);
+}
+
+/*
+ * multipath_end_bh_io() is called when we have finished servicing a multipathed
+ * operation and are ready to return a success/failure code to the buffer
+ * cache layer.
+ */
+static void multipath_end_bh_io (struct multipath_bh *r1_bh, int uptodate)
+{
+       struct buffer_head *bh = r1_bh->master_bh;
+
+       io_request_done(bh->b_rsector, mddev_to_conf(r1_bh->mddev),
+                       test_bit(MPBH_SyncPhase, &r1_bh->state));
+
+       bh->b_end_io(bh, uptodate);
+       multipath_free_mpbh(r1_bh);
+}
+
+void multipath_end_request (struct buffer_head *bh, int uptodate)
+{
+       struct multipath_bh * r1_bh = (struct multipath_bh *)(bh->b_private);
+
+       /*
+        * this branch is our 'one multipath IO has finished' event handler:
+        */
+       if (!uptodate)
+               md_error (r1_bh->mddev, bh->b_dev);
+       else
+               /*
+                * Set MPBH_Uptodate in our master buffer_head, so that
+                * we will return a good error code for to the higher
+                * levels even if IO on some other multipathed buffer fails.
+                *
+                * The 'master' represents the complex operation to 
+                * user-side. So if something waits for IO, then it will
+                * wait for the 'master' buffer_head.
+                */
+               set_bit (MPBH_Uptodate, &r1_bh->state);
+
+               
+       if (uptodate) {
+               multipath_end_bh_io(r1_bh, uptodate);
+               return;
+       }
+       /*
+        * oops, IO error:
+        */
+       printk(KERN_ERR "multipath: %s: rescheduling block %lu\n", 
+                partition_name(bh->b_dev), bh->b_blocknr);
+       multipath_reschedule_retry(r1_bh);
+       return;
+}
+
+/*
+ * This routine returns the disk from which the requested read should
+ * be done. It bookkeeps the last read position for every disk
+ * in array and when new read requests come, the disk which last
+ * position is nearest to the request, is chosen.
+ *
+ * TODO: now if there are 2 multipaths in the same 2 devices, performance
+ * degrades dramatically because position is multipath, not device based.
+ * This should be changed to be device based. Also atomic sequential
+ * reads should be somehow balanced.
+ */
+
+static int multipath_read_balance (multipath_conf_t *conf)
+{
+       int disk;
+
+       for (disk = 0; disk < conf->raid_disks; disk++) 
+               if (conf->multipaths[disk].operational)
+                       return disk;
+       BUG();
+       return 0;
+}
+
+static int multipath_make_request (mddev_t *mddev, int rw,
+                              struct buffer_head * bh)
+{
+       multipath_conf_t *conf = mddev_to_conf(mddev);
+       struct buffer_head *bh_req;
+       struct multipath_bh * r1_bh;
+       struct multipath_info *multipath;
+
+       if (!buffer_locked(bh))
+               BUG();
+       
+/*
+ * make_request() can abort the operation when READA is being
+ * used and no empty request is available.
+ *
+ * Currently, just replace the command with READ/WRITE.
+ */
+       if (rw == READA)
+               rw = READ;
+
+       r1_bh = multipath_alloc_mpbh (conf);
+
+       spin_lock_irq(&conf->segment_lock);
+       wait_event_lock_irq(conf->wait_done,
+                       bh->b_rsector < conf->start_active ||
+                       bh->b_rsector >= conf->start_future,
+                       conf->segment_lock);
+       if (bh->b_rsector < conf->start_active) 
+               conf->cnt_done++;
+       else {
+               conf->cnt_future++;
+               if (conf->phase)
+                       set_bit(MPBH_SyncPhase, &r1_bh->state);
+       }
+       spin_unlock_irq(&conf->segment_lock);
+       
+       /*
+        * i think the read and write branch should be separated completely,
+        * since we want to do read balancing on the read side for example.
+        * Alternative implementations? :) --mingo
+        */
+
+       r1_bh->master_bh = bh;
+       r1_bh->mddev = mddev;
+       r1_bh->cmd = rw;
+
+       /*
+        * read balancing logic:
+        */
+       multipath = conf->multipaths + multipath_read_balance(conf);
+
+       bh_req = &r1_bh->bh_req;
+       memcpy(bh_req, bh, sizeof(*bh));
+       bh_req->b_blocknr = bh->b_rsector;
+       bh_req->b_dev = multipath->dev;
+       bh_req->b_rdev = multipath->dev;
+/*     bh_req->b_rsector = bh->n_rsector; */
+       bh_req->b_end_io = multipath_end_request;
+       bh_req->b_private = r1_bh;
+       generic_make_request (rw, bh_req);
+       return 0;
+}
+
+static int multipath_status (char *page, mddev_t *mddev)
+{
+       multipath_conf_t *conf = mddev_to_conf(mddev);
+       int sz = 0, i;
+       
+       sz += sprintf (page+sz, " [%d/%d] [", conf->raid_disks,
+                                                conf->working_disks);
+       for (i = 0; i < conf->raid_disks; i++)
+               sz += sprintf (page+sz, "%s",
+                       conf->multipaths[i].operational ? "U" : "_");
+       sz += sprintf (page+sz, "]");
+       return sz;
+}
+
+#define LAST_DISK KERN_ALERT \
+"multipath: only one IO path left and IO error.\n"
+
+#define NO_SPARE_DISK KERN_ALERT \
+"multipath: no spare IO path left!\n"
+
+#define DISK_FAILED KERN_ALERT \
+"multipath: IO failure on %s, disabling IO path. \n" \
+"      Operation continuing on %d IO paths.\n"
+
+static void mark_disk_bad (mddev_t *mddev, int failed)
+{
+       multipath_conf_t *conf = mddev_to_conf(mddev);
+       struct multipath_info *multipath = conf->multipaths+failed;
+       mdp_super_t *sb = mddev->sb;
+
+       multipath->operational = 0;
+       mark_disk_faulty(sb->disks+multipath->number);
+       mark_disk_nonsync(sb->disks+multipath->number);
+       mark_disk_inactive(sb->disks+multipath->number);
+       sb->active_disks--;
+       sb->working_disks--;
+       sb->failed_disks++;
+       mddev->sb_dirty = 1;
+       md_wakeup_thread(conf->thread);
+       conf->working_disks--;
+       printk (DISK_FAILED, partition_name (multipath->dev),
+                                conf->working_disks);
+}
+
+/*
+ * Careful, this can execute in IRQ contexts as well!
+ */
+static int multipath_error (mddev_t *mddev, kdev_t dev)
+{
+       multipath_conf_t *conf = mddev_to_conf(mddev);
+       struct multipath_info * multipaths = conf->multipaths;
+       int disks = MD_SB_DISKS;
+       int other_paths = 1;
+       int i;
+
+       if (conf->working_disks == 1) {
+               other_paths = 0;
+               for (i = 0; i < disks; i++) {
+                       if (multipaths[i].spare) {
+                               other_paths = 1;
+                               break;
+                       }
+               }
+       }
+
+       if (!other_paths) {
+               /*
+                * Uh oh, we can do nothing if this is our last path, but
+                * first check if this is a queued request for a device
+                * which has just failed.
+                */
+               for (i = 0; i < disks; i++) {
+                       if (multipaths[i].dev==dev && !multipaths[i].operational)
+                               return 0;
+               }
+               printk (LAST_DISK);
+       } else {
+               /*
+                * Mark disk as unusable
+                */
+               for (i = 0; i < disks; i++) {
+                       if (multipaths[i].dev==dev && multipaths[i].operational) {
+                               mark_disk_bad(mddev, i);
+                               break;
+                       }
+               }
+               if (!conf->working_disks) {
+                       int err = 1;
+                       mdp_disk_t *spare;
+                       mdp_super_t *sb = mddev->sb;
+
+//                     MD_BUG();
+                       spare = get_spare(mddev);
+                       if (spare) {
+                               err = multipath_diskop(mddev, &spare, DISKOP_SPARE_WRITE);
+                               printk("got DISKOP_SPARE_WRITE err: %d. (spare_faulty(): %d)\n", err, disk_faulty(spare));
+//                             MD_BUG();
+                       }
+                       if (!err && !disk_faulty(spare)) {
+                               multipath_diskop(mddev, &spare, DISKOP_SPARE_ACTIVE);
+                               mark_disk_sync(spare);
+                               mark_disk_active(spare);
+                               sb->active_disks++;
+                               sb->spare_disks--;
+//                             MD_BUG();
+                       }
+               }
+       }
+       return 0;
+}
+
+#undef LAST_DISK
+#undef NO_SPARE_DISK
+#undef DISK_FAILED
+
+
+static void print_multipath_conf (multipath_conf_t *conf)
+{
+       int i;
+       struct multipath_info *tmp;
+
+       printk("MULTIPATH conf printout:\n");
+       if (!conf) {
+               printk("(conf==NULL)\n");
+               return;
+       }
+       printk(" --- wd:%d rd:%d nd:%d\n", conf->working_disks,
+                        conf->raid_disks, conf->nr_disks);
+
+       for (i = 0; i < MD_SB_DISKS; i++) {
+               tmp = conf->multipaths + i;
+               if (tmp->spare || tmp->operational || tmp->number ||
+                               tmp->raid_disk || tmp->used_slot)
+                       printk(" disk%d, s:%d, o:%d, n:%d rd:%d us:%d dev:%s\n",
+                               i, tmp->spare,tmp->operational,
+                               tmp->number,tmp->raid_disk,tmp->used_slot,
+                               partition_name(tmp->dev));
+       }
+}
+
+static int multipath_diskop(mddev_t *mddev, mdp_disk_t **d, int state)
+{
+       int err = 0;
+       int i, failed_disk=-1, spare_disk=-1, removed_disk=-1, added_disk=-1;
+       multipath_conf_t *conf = mddev->private;
+       struct multipath_info *tmp, *sdisk, *fdisk, *rdisk, *adisk;
+       mdp_super_t *sb = mddev->sb;
+       mdp_disk_t *failed_desc, *spare_desc, *added_desc;
+       mdk_rdev_t *spare_rdev, *failed_rdev;
+
+       print_multipath_conf(conf);
+       md_spin_lock_irq(&conf->device_lock);
+       /*
+        * find the disk ...
+        */
+       switch (state) {
+
+       case DISKOP_SPARE_ACTIVE:
+
+               /*
+                * Find the failed disk within the MULTIPATH configuration ...
+                * (this can only be in the first conf->working_disks part)
+                */
+               for (i = 0; i < conf->raid_disks; i++) {
+                       tmp = conf->multipaths + i;
+                       if ((!tmp->operational && !tmp->spare) ||
+                                       !tmp->used_slot) {
+                               failed_disk = i;
+                               break;
+                       }
+               }
+               /*
+                * When we activate a spare disk we _must_ have a disk in
+                * the lower (active) part of the array to replace. 
+                */
+               if ((failed_disk == -1) || (failed_disk >= conf->raid_disks)) {
+                       MD_BUG();
+                       err = 1;
+                       goto abort;
+               }
+               /* fall through */
+
+       case DISKOP_SPARE_WRITE:
+       case DISKOP_SPARE_INACTIVE:
+
+               /*
+                * Find the spare disk ... (can only be in the 'high'
+                * area of the array)
+                */
+               for (i = conf->raid_disks; i < MD_SB_DISKS; i++) {
+                       tmp = conf->multipaths + i;
+                       if (tmp->spare && tmp->number == (*d)->number) {
+                               spare_disk = i;
+                               break;
+                       }
+               }
+               if (spare_disk == -1) {
+                       MD_BUG();
+                       err = 1;
+                       goto abort;
+               }
+               break;
+
+       case DISKOP_HOT_REMOVE_DISK:
+
+               for (i = 0; i < MD_SB_DISKS; i++) {
+                       tmp = conf->multipaths + i;
+                       if (tmp->used_slot && (tmp->number == (*d)->number)) {
+                               if (tmp->operational) {
+                                       printk(KERN_ERR "hot-remove-disk, slot %d is identified to be the requested disk (number %d), but is still operational!\n", i, (*d)->number);
+                                       err = -EBUSY;
+                                       goto abort;
+                               }
+                               removed_disk = i;
+                               break;
+                       }
+               }
+               if (removed_disk == -1) {
+                       MD_BUG();
+                       err = 1;
+                       goto abort;
+               }
+               break;
+
+       case DISKOP_HOT_ADD_DISK:
+
+               for (i = conf->raid_disks; i < MD_SB_DISKS; i++) {
+                       tmp = conf->multipaths + i;
+                       if (!tmp->used_slot) {
+                               added_disk = i;
+                               break;
+                       }
+               }
+               if (added_disk == -1) {
+                       MD_BUG();
+                       err = 1;
+                       goto abort;
+               }
+               break;
+       }
+
+       switch (state) {
+       /*
+        * Switch the spare disk to write-only mode:
+        */
+       case DISKOP_SPARE_WRITE:
+               sdisk = conf->multipaths + spare_disk;
+               sdisk->operational = 1;
+               sdisk->write_only = 1;
+               break;
+       /*
+        * Deactivate a spare disk:
+        */
+       case DISKOP_SPARE_INACTIVE:
+               sdisk = conf->multipaths + spare_disk;
+               sdisk->operational = 0;
+               sdisk->write_only = 0;
+               break;
+       /*
+        * Activate (mark read-write) the (now sync) spare disk,
+        * which means we switch it's 'raid position' (->raid_disk)
+        * with the failed disk. (only the first 'conf->nr_disks'
+        * slots are used for 'real' disks and we must preserve this
+        * property)
+        */
+       case DISKOP_SPARE_ACTIVE:
+               sdisk = conf->multipaths + spare_disk;
+               fdisk = conf->multipaths + failed_disk;
+
+               spare_desc = &sb->disks[sdisk->number];
+               failed_desc = &sb->disks[fdisk->number];
+
+               if (spare_desc != *d) {
+                       MD_BUG();
+                       err = 1;
+                       goto abort;
+               }
+
+               if (spare_desc->raid_disk != sdisk->raid_disk) {
+                       MD_BUG();
+                       err = 1;
+                       goto abort;
+               }
+                       
+               if (sdisk->raid_disk != spare_disk) {
+                       MD_BUG();
+                       err = 1;
+                       goto abort;
+               }
+
+               if (failed_desc->raid_disk != fdisk->raid_disk) {
+                       MD_BUG();
+                       err = 1;
+                       goto abort;
+               }
+
+               if (fdisk->raid_disk != failed_disk) {
+                       MD_BUG();
+                       err = 1;
+                       goto abort;
+               }
+
+               /*
+                * do the switch finally
+                */
+               spare_rdev = find_rdev_nr(mddev, spare_desc->number);
+               failed_rdev = find_rdev_nr(mddev, failed_desc->number);
+               xchg_values(spare_rdev->desc_nr, failed_rdev->desc_nr);
+//             if (failed_rdev->alias_device)
+//                     MD_BUG();
+//             if (!spare_rdev->alias_device)
+//                     MD_BUG();
+               spare_rdev->alias_device = 0;
+               failed_rdev->alias_device = 1;
+
+               xchg_values(*spare_desc, *failed_desc);
+               xchg_values(*fdisk, *sdisk);
+
+               /*
+                * (careful, 'failed' and 'spare' are switched from now on)
+                *
+                * we want to preserve linear numbering and we want to
+                * give the proper raid_disk number to the now activated
+                * disk. (this means we switch back these values)
+                */
+       
+               xchg_values(spare_desc->raid_disk, failed_desc->raid_disk);
+               xchg_values(sdisk->raid_disk, fdisk->raid_disk);
+               xchg_values(spare_desc->number, failed_desc->number);
+               xchg_values(sdisk->number, fdisk->number);
+
+               *d = failed_desc;
+
+               if (sdisk->dev == MKDEV(0,0))
+                       sdisk->used_slot = 0;
+               /*
+                * this really activates the spare.
+                */
+               fdisk->spare = 0;
+               fdisk->write_only = 0;
+
+               /*
+                * if we activate a spare, we definitely replace a
+                * non-operational disk slot in the 'low' area of
+                * the disk array.
+                */
+
+               conf->working_disks++;
+
+               break;
+
+       case DISKOP_HOT_REMOVE_DISK:
+               rdisk = conf->multipaths + removed_disk;
+
+               if (rdisk->spare && (removed_disk < conf->raid_disks)) {
+                       MD_BUG();       
+                       err = 1;
+                       goto abort;
+               }
+               rdisk->dev = MKDEV(0,0);
+               rdisk->used_slot = 0;
+               conf->nr_disks--;
+               break;
+
+       case DISKOP_HOT_ADD_DISK:
+               adisk = conf->multipaths + added_disk;
+               added_desc = *d;
+
+               if (added_disk != added_desc->number) {
+                       MD_BUG();       
+                       err = 1;
+                       goto abort;
+               }
+
+               adisk->number = added_desc->number;
+               adisk->raid_disk = added_desc->raid_disk;
+               adisk->dev = MKDEV(added_desc->major,added_desc->minor);
+
+               adisk->operational = 0;
+               adisk->write_only = 0;
+               adisk->spare = 1;
+               adisk->used_slot = 1;
+               adisk->head_position = 0;
+               conf->nr_disks++;
+
+               break;
+
+       default:
+               MD_BUG();       
+               err = 1;
+               goto abort;
+       }
+abort:
+       md_spin_unlock_irq(&conf->device_lock);
+
+       print_multipath_conf(conf);
+       return err;
+}
+
+
+#define IO_ERROR KERN_ALERT \
+"multipath: %s: unrecoverable IO read error for block %lu\n"
+
+#define REDIRECT_SECTOR KERN_ERR \
+"multipath: %s: redirecting sector %lu to another IO path\n"
+
+/*
+ * This is a kernel thread which:
+ *
+ *     1.      Retries failed read operations on working multipaths.
+ *     2.      Updates the raid superblock when problems encounter.
+ *     3.      Performs writes following reads for array syncronising.
+ */
+
+static void multipathd (void *data)
+{
+       struct multipath_bh *r1_bh;
+       struct buffer_head *bh;
+       unsigned long flags;
+       mddev_t *mddev;
+       kdev_t dev;
+
+
+       for (;;) {
+               md_spin_lock_irqsave(&retry_list_lock, flags);
+               r1_bh = multipath_retry_list;
+               if (!r1_bh)
+                       break;
+               multipath_retry_list = r1_bh->next_r1;
+               md_spin_unlock_irqrestore(&retry_list_lock, flags);
+
+               mddev = r1_bh->mddev;
+               if (mddev->sb_dirty) {
+                       printk(KERN_INFO "dirty sb detected, updating.\n");
+                       mddev->sb_dirty = 0;
+                       md_update_sb(mddev);
+               }
+               bh = &r1_bh->bh_req;
+               dev = bh->b_dev;
+               
+               multipath_map (mddev, &bh->b_dev);
+               if (bh->b_dev == dev) {
+                       printk (IO_ERROR, partition_name(bh->b_dev), bh->b_blocknr);
+                       multipath_end_bh_io(r1_bh, 0);
+               } else {
+                       printk (REDIRECT_SECTOR,
+                               partition_name(bh->b_dev), bh->b_blocknr);
+                       bh->b_rdev = bh->b_dev;
+                       bh->b_rsector = bh->b_blocknr;
+                       generic_make_request (r1_bh->cmd, bh);
+               }
+       }
+       md_spin_unlock_irqrestore(&retry_list_lock, flags);
+}
+#undef IO_ERROR
+#undef REDIRECT_SECTOR
+
+/*
+ * This will catch the scenario in which one of the multipaths was
+ * mounted as a normal device rather than as a part of a raid set.
+ *
+ * check_consistency is very personality-dependent, eg. RAID5 cannot
+ * do this check, it uses another method.
+ */
+static int __check_consistency (mddev_t *mddev, int row)
+{
+       multipath_conf_t *conf = mddev_to_conf(mddev);
+       int disks = MD_SB_DISKS;
+       kdev_t dev;
+       struct buffer_head *bh = NULL;
+       int i, rc = 0;
+       char *buffer = NULL;
+
+       for (i = 0; i < disks; i++) {
+               if (!conf->multipaths[i].operational)
+                       continue;
+               printk("(checking disk %d)\n",i);
+               dev = conf->multipaths[i].dev;
+               set_blocksize(dev, 4096);
+               if ((bh = bread(dev, row / 4, 4096)) == NULL)
+                       break;
+               if (!buffer) {
+                       buffer = (char *) __get_free_page(GFP_KERNEL);
+                       if (!buffer)
+                               break;
+                       memcpy(buffer, bh->b_data, 4096);
+               } else if (memcmp(buffer, bh->b_data, 4096)) {
+                       rc = 1;
+                       break;
+               }
+               bforget(bh);
+               fsync_dev(dev);
+               invalidate_buffers(dev);
+               bh = NULL;
+       }
+       if (buffer)
+               free_page((unsigned long) buffer);
+       if (bh) {
+               dev = bh->b_dev;
+               bforget(bh);
+               fsync_dev(dev);
+               invalidate_buffers(dev);
+       }
+       return rc;
+}
+
+static int check_consistency (mddev_t *mddev)
+{
+       if (__check_consistency(mddev, 0))
+/*
+ * we do not do this currently, as it's perfectly possible to
+ * have an inconsistent array when it's freshly created. Only
+ * newly written data has to be consistent.
+ */
+               return 0;
+
+       return 0;
+}
+
+#define INVALID_LEVEL KERN_WARNING \
+"multipath: md%d: raid level not set to multipath IO (%d)\n"
+
+#define NO_SB KERN_ERR \
+"multipath: disabled IO path %s (couldn't access raid superblock)\n"
+
+#define ERRORS KERN_ERR \
+"multipath: disabled IO path %s (errors detected)\n"
+
+#define NOT_IN_SYNC KERN_ERR \
+"multipath: making IO path %s a spare path (not in sync)\n"
+
+#define INCONSISTENT KERN_ERR \
+"multipath: disabled IO path %s (inconsistent descriptor)\n"
+
+#define ALREADY_RUNNING KERN_ERR \
+"multipath: disabled IO path %s (multipath %d already operational)\n"
+
+#define OPERATIONAL KERN_INFO \
+"multipath: device %s operational as IO path %d\n"
+
+#define MEM_ERROR KERN_ERR \
+"multipath: couldn't allocate memory for md%d\n"
+
+#define SPARE KERN_INFO \
+"multipath: spare IO path %s\n"
+
+#define NONE_OPERATIONAL KERN_ERR \
+"multipath: no operational IO paths for md%d\n"
+
+#define SB_DIFFERENCES KERN_ERR \
+"multipath: detected IO path differences!\n"
+
+#define ARRAY_IS_ACTIVE KERN_INFO \
+"multipath: array md%d active with %d out of %d IO paths (%d spare IO paths)\n"
+
+#define THREAD_ERROR KERN_ERR \
+"multipath: couldn't allocate thread for md%d\n"
+
+static int multipath_run (mddev_t *mddev)
+{
+       multipath_conf_t *conf;
+       int i, j, disk_idx;
+       struct multipath_info *disk, *disk2;
+       mdp_super_t *sb = mddev->sb;
+       mdp_disk_t *desc, *desc2;
+       mdk_rdev_t *rdev, *def_rdev = NULL;
+       struct md_list_head *tmp;
+       int start_recovery = 0, num_rdevs = 0;
+
+       MOD_INC_USE_COUNT;
+
+       if (sb->level != -4) {
+               printk(INVALID_LEVEL, mdidx(mddev), sb->level);
+               goto out;
+       }
+       /*
+        * copy the already verified devices into our private MULTIPATH
+        * bookkeeping area. [whatever we allocate in multipath_run(),
+        * should be freed in multipath_stop()]
+        */
+
+       conf = kmalloc(sizeof(multipath_conf_t), GFP_KERNEL);
+       mddev->private = conf;
+       if (!conf) {
+               printk(MEM_ERROR, mdidx(mddev));
+               goto out;
+       }
+       memset(conf, 0, sizeof(*conf));
+
+       ITERATE_RDEV(mddev,rdev,tmp) {
+               if (rdev->faulty) {
+                       /* this is a "should never happen" case and if it */
+                       /* ever does happen, a continue; won't help */
+                       printk(ERRORS, partition_name(rdev->dev));
+                       continue;
+               } else {
+                       /* this is a "should never happen" case and if it */
+                       /* ever does happen, a continue; won't help */
+                       if (!rdev->sb) {
+                               MD_BUG();
+                               continue;
+                       }
+               }
+               if (rdev->desc_nr == -1) {
+                       MD_BUG();
+                       continue;
+               }
+
+               desc = &sb->disks[rdev->desc_nr];
+               disk_idx = desc->raid_disk;
+               disk = conf->multipaths + disk_idx;
+
+               if (!disk_sync(desc))
+                       printk(NOT_IN_SYNC, partition_name(rdev->dev));
+
+               /*
+                * Mark all disks as spare to start with, then pick our
+                * active disk.  If we have a disk that is marked active
+                * in the sb, then use it, else use the first rdev.
+                */
+               disk->number = desc->number;
+               disk->raid_disk = desc->raid_disk;
+               disk->dev = rdev->dev;
+               disk->sect_limit = MAX_WORK_PER_DISK;
+               disk->operational = 0;
+               disk->write_only = 0;
+               disk->spare = 1;
+               disk->used_slot = 1;
+               disk->head_position = 0;
+               mark_disk_sync(desc);
+
+               if (disk_active(desc)) {
+                       if(!conf->working_disks) {
+                               printk(OPERATIONAL, partition_name(rdev->dev),
+                                       desc->raid_disk);
+                               disk->operational = 1;
+                               disk->spare = 0;
+                               conf->working_disks++;
+                               def_rdev = rdev;
+                       } else {
+                               mark_disk_spare(desc);
+                       }
+               } else
+                       mark_disk_spare(desc);
+
+               if(!num_rdevs++) def_rdev = rdev;
+       }
+       if(!conf->working_disks && num_rdevs) {
+               desc = &sb->disks[def_rdev->desc_nr];
+               disk = conf->multipaths + desc->raid_disk;
+               printk(OPERATIONAL, partition_name(def_rdev->dev),
+                       disk->raid_disk);
+               disk->operational = 1;
+               disk->spare = 0;
+               conf->working_disks++;
+               mark_disk_active(desc);
+       }
+       /*
+        * Make sure our active path is in desc spot 0
+        */
+       if(def_rdev->desc_nr != 0) {
+               rdev = find_rdev_nr(mddev, 0);
+               desc = &sb->disks[def_rdev->desc_nr];
+               desc2 = sb->disks;
+               disk = conf->multipaths + desc->raid_disk;
+               disk2 = conf->multipaths + desc2->raid_disk;
+               xchg_values(*desc2,*desc);
+               xchg_values(*disk2,*disk);
+               xchg_values(desc2->number, desc->number);
+               xchg_values(disk2->number, disk->number);
+               xchg_values(desc2->raid_disk, desc->raid_disk);
+               xchg_values(disk2->raid_disk, disk->raid_disk);
+               if(rdev) {
+                       xchg_values(def_rdev->desc_nr,rdev->desc_nr);
+               } else {
+                       def_rdev->desc_nr = 0;
+               }
+       }
+       conf->raid_disks = sb->raid_disks = sb->active_disks = 1;
+       conf->nr_disks = sb->nr_disks = sb->working_disks = num_rdevs;
+       sb->failed_disks = 0;
+       sb->spare_disks = num_rdevs - 1;
+       mddev->sb_dirty = 1;
+       conf->mddev = mddev;
+       conf->device_lock = MD_SPIN_LOCK_UNLOCKED;
+
+       conf->segment_lock = MD_SPIN_LOCK_UNLOCKED;
+       init_waitqueue_head(&conf->wait_buffer);
+       init_waitqueue_head(&conf->wait_done);
+       init_waitqueue_head(&conf->wait_ready);
+
+       if (!conf->working_disks) {
+               printk(NONE_OPERATIONAL, mdidx(mddev));
+               goto out_free_conf;
+       }
+
+
+       /* pre-allocate some buffer_head structures.
+        * As a minimum, 1 mpbh and raid_disks buffer_heads
+        * would probably get us by in tight memory situations,
+        * but a few more is probably a good idea.
+        * For now, try 16 mpbh and 16*raid_disks bufferheads
+        * This will allow at least 16 concurrent reads or writes
+        * even if kmalloc starts failing
+        */
+       if (multipath_grow_mpbh(conf, 16) < 16 ||
+           multipath_grow_bh(conf, 16*conf->raid_disks)< 16*conf->raid_disks) {
+               printk(MEM_ERROR, mdidx(mddev));
+               goto out_free_conf;
+       }
+
+       if (!start_recovery && (sb->state & (1 << MD_SB_CLEAN))) {
+               /*
+                * we do sanity checks even if the device says
+                * it's clean ...
+                */
+               if (check_consistency(mddev)) {
+                       printk(SB_DIFFERENCES);
+                       sb->state &= ~(1 << MD_SB_CLEAN);
+               }
+       }
+
+       {
+               const char * name = "multipathd";
+
+               conf->thread = md_register_thread(multipathd, conf, name);
+               if (!conf->thread) {
+                       printk(THREAD_ERROR, mdidx(mddev));
+                       goto out_free_conf;
+               }
+       }
+
+       /*
+        * Regenerate the "device is in sync with the raid set" bit for
+        * each device.
+        */
+       for (i = 0; i < MD_SB_DISKS; i++) {
+               mark_disk_nonsync(sb->disks+i);
+               for (j = 0; j < sb->raid_disks; j++) {
+                       if (sb->disks[i].number == conf->multipaths[j].number)
+                               mark_disk_sync(sb->disks+i);
+               }
+       }
+
+       printk(ARRAY_IS_ACTIVE, mdidx(mddev), sb->active_disks,
+                       sb->raid_disks, sb->spare_disks);
+       /*
+        * Ok, everything is just fine now
+        */
+       return 0;
+
+out_free_conf:
+       multipath_shrink_mpbh(conf);
+       multipath_shrink_bh(conf, conf->freebh_cnt);
+       kfree(conf);
+       mddev->private = NULL;
+out:
+       MOD_DEC_USE_COUNT;
+       return -EIO;
+}
+
+#undef INVALID_LEVEL
+#undef NO_SB
+#undef ERRORS
+#undef NOT_IN_SYNC
+#undef INCONSISTENT
+#undef ALREADY_RUNNING
+#undef OPERATIONAL
+#undef SPARE
+#undef NONE_OPERATIONAL
+#undef SB_DIFFERENCES
+#undef ARRAY_IS_ACTIVE
+
+static int multipath_stop (mddev_t *mddev)
+{
+       multipath_conf_t *conf = mddev_to_conf(mddev);
+
+       md_unregister_thread(conf->thread);
+       multipath_shrink_mpbh(conf);
+       multipath_shrink_bh(conf, conf->freebh_cnt);
+       kfree(conf);
+       mddev->private = NULL;
+       MOD_DEC_USE_COUNT;
+       return 0;
+}
+
+static mdk_personality_t multipath_personality=
+{
+       name:           "multipath",
+       make_request:   multipath_make_request,
+       run:            multipath_run,
+       stop:           multipath_stop,
+       status:         multipath_status,
+       error_handler:  multipath_error,
+       diskop:         multipath_diskop,
+};
+
+static int md__init multipath_init (void)
+{
+       return register_md_personality (MULTIPATH, &multipath_personality);
+}
+
+static void multipath_exit (void)
+{
+       unregister_md_personality (MULTIPATH);
+}
+
+module_init(multipath_init);
+module_exit(multipath_exit);
+
index 0a2c735e92087bcbbd6133f73afdba4d4c64b6f7..91691bedc0408dc9228f2b4d970d62cf2acf5536 100644 (file)
@@ -70,7 +70,7 @@ MODULE_PARM(doc_config_location, "l");
 
 
 static unsigned long __initdata doc_locations[] = {
-#if defined (__alpha__) || defined(__i386__)
+#if defined (__alpha__) || defined(__i386__) || defined(__x86_64__)
 #ifdef CONFIG_MTD_DOCPROBE_HIGH
        0xfffc8000, 0xfffca000, 0xfffcc000, 0xfffce000, 
        0xfffd0000, 0xfffd2000, 0xfffd4000, 0xfffd6000,
index d50b4fb2c2dcf7b1a5e5774eef2e7f31a2d03c81..5e517e9906484f9bd4b012bca42466cc46c96690 100644 (file)
@@ -470,7 +470,7 @@ start_xmit (struct sk_buff *skb, struct net_device *dev)
        unsigned entry;
        u32 ioaddr;
        int tx_shift;
-       unsigned flags;
+       unsigned long flags;
 
        ioaddr = dev->base_addr;
        entry = np->cur_tx % TX_RING_SIZE;
index 4c3c2ce7c926ad44d3fe0a86429a9822f100a2df..afb46d51a0ba764c90f5d51d1e0b87b36b0ccecb 100644 (file)
@@ -892,7 +892,7 @@ static int netdev_open(struct net_device *dev)
 //   np->bcrvalue=0x04 | 0x0x38;  /* big-endian, 256 burst length */
        np->bcrvalue = 0x04 | 0x10;     /* big-endian, tx 8 burst length */
        np->crvalue = 0xe00;    /* rx 128 burst length */
-#elif defined(__alpha__)
+#elif defined(__alpha__) || defined(__x86_64__)
 // 89/9/1 modify, 
 //   np->bcrvalue=0x38;           /* little-endian, 256 burst length */
        np->bcrvalue = 0x10;    /* little-endian, 8 burst length */
index 1bcc0297beef6522a572f04592e35f6a45e122c4..06dcec26009c66c2b814acac5721fb8960e367c2 100644 (file)
@@ -728,7 +728,7 @@ static void vlsi_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
        u8              irintr;
        int             boguscount = 20;
        int             no_speed_check = 0;
-       unsigned        flags;
+       unsigned long   flags;
 
 
        iobase = ndev->base_addr;
index 10ab06121b78e85a274e4209aeee3c41650b5a99..b229607541c8da500cbbce5b758b6e9e872d1477 100644 (file)
@@ -67,7 +67,7 @@ static int rx_copybreak = 100;
        ToDo: Non-Intel setting could be better.
 */
 
-#if defined(__alpha__)
+#if defined(__alpha__) || defined(__ia64__) || defined(__x86_64__)
 static int csr0 = 0x01A00000 | 0xE000;
 #elif defined(__powerpc__)
 static int csr0 = 0x01B00000 | 0x8000;
index a55fa1d36e6b276fca5f28126fbdc343e81a078a..82efdb6ce832bdba46d670553b20fcbc29cab6f9 100644 (file)
@@ -77,7 +77,7 @@ static int rx_copybreak = 100;
        ToDo: Non-Intel setting could be better.
 */
 
-#if defined(__alpha__) || defined(__ia64__)
+#if defined(__alpha__) || defined(__ia64__) || defined(__x86_64__)
 static int csr0 = 0x01A00000 | 0xE000;
 #elif defined(__i386__) || defined(__powerpc__)
 static int csr0 = 0x01A00000 | 0x8000;
index 6699b3e6d40b3a86384b03296651df36576849d8..65eda72707b41be759a3bec58ed2745585c2d62b 100644 (file)
@@ -1347,7 +1347,7 @@ static void set_chan_state (struct net_device *dev, u8 state)
 {
        x25_channel_t *chan = dev->priv;
        cycx_t *card = chan->card;
-       u32 flags = 0;
+       long flags;
        char *string_state = NULL;
 
        spin_lock_irqsave(&card->lock, flags);
index 84a9826147b12169080bc865de8880523c01b4bd..85923bee6ede3c3c6cd52b068c1c62c7ca592923 100644 (file)
@@ -155,7 +155,9 @@ static int  enslave( struct net_device *, struct net_device * );
 static int  emancipate( struct net_device * );
 #endif
 
+#ifdef __i386__
 #define ASM_CRC 1
+#endif
 
 static const char  version[] =
        "Granch SBNI12 driver ver 5.0.1  Jun 22 2001  Denis I.Timofeev.\n";
@@ -361,7 +363,7 @@ sbni_probe1( struct net_device  *dev,  unsigned long  ioaddr,  int  irq )
        /* store MAC address (generate if that isn't known) */
        *(u16 *)dev->dev_addr = htons( 0x00ff );
        *(u32 *)(dev->dev_addr + 2) = htonl( 0x01000000 |
-               ( (mac[num]  ?  mac[num]  :  (u32)dev->priv) & 0x00ffffff) );
+               ( (mac[num]  ?  mac[num]  :  (u32)((long)dev->priv)) & 0x00ffffff) );
 
        /* store link settings (speed, receive level ) */
        nl->maxframe  = DEFAULT_FRAME_LEN;
@@ -1519,7 +1521,7 @@ cleanup_module( void )
 
 #else  /* MODULE */
 
-void __init
+static int __init
 sbni_setup( char  *p )
 {
        int  n, parm;
@@ -1530,7 +1532,7 @@ sbni_setup( char  *p )
        for( n = 0, parm = 0;  *p  &&  n < 8; ) {
                (*dest[ parm ])[ n ] = simple_strtol( p, &p, 0 );
                if( !*p  ||  *p == ')' )
-                       return;
+                       return 1;
                if( *p == ';' )
                        ++p, ++n, parm = 0;
                else if( *p++ != ',' )
@@ -1541,6 +1543,7 @@ sbni_setup( char  *p )
        }
 bad_param:
        printk( KERN_ERR "Error in sbni kernel parameter!\n" );
+       return 0;
 }
 
 __setup( "sbni=", sbni_setup );
index 9460b581a05e5a5ed7a903f934a5a8b85906c10a..25ec43b93afe6dde4e1655f14fc99dfa0744b053 100644 (file)
@@ -952,7 +952,7 @@ static void init_registers(struct net_device *dev)
        } else {
                i |= 0xE000;
        }
-#elif defined(__powerpc__) || defined(__i386__) || defined(__alpha) || defined(__ia64__)
+#elif defined(__powerpc__) || defined(__i386__) || defined(__alpha__) || defined(__ia64__) || defined(__x86_64__)
        i |= 0xE000;
 #elif defined(__sparc__)
        i |= 0x4800;
index 595aba251d2e160aed3ad7a8b4cf9faad1fbd185..1d90c05f25918597ddff4e3c237eda7cde26b6fb 100644 (file)
@@ -1228,7 +1228,7 @@ static void airo_interrupt ( int irq, void* dev_id, struct pt_regs *regs) {
        /* Check to see if there is something to receive */
        if ( status & EV_RX  ) {
                struct sk_buff *skb = NULL;
-               int flags;
+               long flags;
                u16 fc, len, hdrlen = 0;
                struct {
                        u16 status, len;
@@ -1559,7 +1559,7 @@ static u16 issuecommand(struct airo_info *ai, Cmd *pCmd, Resp *pRsp) {
         // Im really paranoid about letting it run forever!
        int max_tries = 600000;  
         int rc = SUCCESS;
-       int flags;
+       long flags;
 
        spin_lock_irqsave(&ai->cmd_lock, flags);
        OUT4500(ai, PARAM0, pCmd->parm0);
@@ -1664,7 +1664,7 @@ static int aux_bap_read(struct airo_info *ai, u16 *pu16Dst,
        u16 next;
        int words;
        int i;
-       int flags;
+       long flags;
 
        spin_lock_irqsave(&ai->aux_lock, flags);
        page = IN4500(ai, SWS0+whichbap);
@@ -1738,7 +1738,7 @@ static int PC4500_accessrid(struct airo_info *ai, u16 rid, u16 accmd)
 static int PC4500_readrid(struct airo_info *ai, u16 rid, void *pBuf, int len)
 {
        u16 status;
-        int flags;
+        long flags;
         int rc = SUCCESS;
 
        spin_lock_irqsave(&ai->bap1_lock, flags);
@@ -1780,7 +1780,7 @@ static int PC4500_writerid(struct airo_info *ai, u16 rid,
                           const void *pBuf, int len)
 {
        u16 status;
-        int flags;
+        long flags;
        int rc = SUCCESS;
 
        spin_lock_irqsave(&ai->bap1_lock, flags);
@@ -1810,7 +1810,7 @@ static u16 transmit_allocate(struct airo_info *ai, int lenPayload)
        Resp rsp;
        u16 txFid;
        u16 txControl;
-        int flags;
+        long flags;
 
        cmd.cmd = CMD_ALLOCATETX;
        cmd.parm0 = lenPayload;
index 51827fafce5db121f2d7cb927d4fb7d5fdaa7311..e481ad366a092c9b175ded3801efbff64eea899f 100644 (file)
  * Yet, they all live within the same IO space.
  */
 
-#ifndef __i386__
+#if !defined(__i386__) && !defined(__x86_64__)
 
 #ifndef MULTIPLE_PAD_SIZES
 
@@ -232,7 +232,7 @@ struct ESP_regs {
 
 #endif
 
-#else /* !defined __i386__ */
+#else /* !defined(__i386__) && !defined(__x86_64__) */
 
 #define esp_write(__reg, __val) outb((__val), (__reg))
 #define esp_read(__reg) inb((__reg))
@@ -267,7 +267,7 @@ struct ESP_regs {
 #define esp_fgrnd   io_addr + 15 /* rw  Data base for fifo             0x3c  */
 };
 
-#endif /* !defined(__i386__) */
+#endif /* !defined(__i386__) && !defined(__x86_64__) */
 
 /* Various revisions of the ESP board. */
 enum esp_rev {
index 19380d460c52f64f74c906a36ca8934b1cd97e43..75a1c54bc79e803255c686d05351631901fec731 100644 (file)
@@ -580,7 +580,7 @@ ahc_delay(long usec)
 
 
 /***************************** Low Level I/O **********************************/
-#if defined(__powerpc__) || defined(__i386__) || defined(__ia64__)
+#if defined(__powerpc__) || defined(__i386__) || defined(__ia64__) || defined(__x86_64__)
 #define MMAPIO
 #endif
 
index d753ff811300d24e5c83254629b964a621eb88e2..3f8ac6e235f55a159eb942803dc9b0319185d94f 100644 (file)
 #  define FALSE 0
 #endif
 
-#if defined(__powerpc__) || defined(__i386__)
+#if defined(__powerpc__) || defined(__i386__) || defined(__x86_64__)
 #  define MMAPIO
 #endif
 
index 5d6a3cd2c0a1c65c67cf2c178bf63f59126192ac..0725567f219b8411ee44f50b7d8dd25d00ca530c 100644 (file)
 #error Please use -DCONTROLLER=SEAGATE or -DCONTROLLER=FD to override controller type
 #endif
 
+#ifndef __i386__
+#undef SEAGATE_USE_ASM
+#endif
+
 /*
        Thanks to Brian Antoine for the example code in his Messy-Loss ST-01
                driver, and Mitsugu Suzuki for information on the ST-01
index 1b7305fa8b4712f9c07c3999dff9f24aeec485b3..acdb31529de7d8323f2418db3c4ed95d0dafa903 100644 (file)
@@ -645,6 +645,7 @@ static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
     Scsi_Request        * SRpnt;
     Sg_device           * sdp = sfp->parentdp;
     sg_io_hdr_t         * hp = &srp->header;
+    request_queue_t    * q;
 
     srp->data.cmd_opcode = cmnd[0];  /* hold opcode of command */
     hp->status = 0;
@@ -680,6 +681,7 @@ static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
     }
 
     srp->my_cmdp = SRpnt;
+    q = &SRpnt->sr_device->request_queue;
     SRpnt->sr_request.rq_dev = sdp->i_rdev;
     SRpnt->sr_request.rq_status = RQ_ACTIVE;
     SRpnt->sr_sense_buffer[0] = 0;
@@ -715,7 +717,7 @@ static int sg_common_write(Sg_fd * sfp, Sg_request * srp,
                (void *)SRpnt->sr_buffer, hp->dxfer_len,
                sg_cmd_done_bh, timeout, SG_DEFAULT_RETRIES);
     /* dxfer_len overwrites SRpnt->sr_bufflen, hence need for b_malloc_len */
-    generic_unplug_device(&SRpnt->sr_device->request_queue);
+    generic_unplug_device(q);
     return 0;
 }
 
index c0c551c47b0649be71b72bebd7af1301a6f6c979..2fa37e8e33708a87c4685a5a5b4a2f98843c5190 100644 (file)
  *  We want to be paranoid for ppc and ia64. :)
  */
 
-#if    defined __i386__
+#if    defined(__i386__) || defined(__x86_64__)
 #define MEMORY_BARRIER()       do { ; } while(0)
 #elif  defined __powerpc__
 #define MEMORY_BARRIER()       __asm__ volatile("eieio; sync" : : : "memory")
index ce064c67df8c3e4a407933f2f5944ecc4e049782..f521741825b80523ad42da83e6344d0baf1e8367 100644 (file)
@@ -201,3 +201,13 @@ if [ "$CONFIG_SOUND_OSS" = "y" -o "$CONFIG_SOUND_OSS" = "m" ]; then
 fi
 
 dep_tristate '  TV card (bt848) mixer support' CONFIG_SOUND_TVMIXER $CONFIG_SOUND $CONFIG_I2C
+
+# A cross directory dependence. The sound modules will need gameport.o compiled in,
+# but it resides in the drivers/char/joystick directory. This define_tristate takes
+# care of that. --Vojtech
+
+if [ "$CONFIG_INPUT_GAMEPORT" != "n" ]; then
+  if [ "$CONFIG_SOUND_ESSSOLO1" = "y" -o "$CONFIG_SOUND_ES1370" = "y" -o "$CONFIG_SOUND_ES1371" = "y" -o "$CONFIG_SOUND_SONICVIBES" = "y" ]; then
+    define_tristate CONFIG_INPUT_GAMEPORT y
+  fi
+fi
index ec8d300556ab4fe885541680bc2b78d5caf23ca1..b9904f7a53a2652eb82b4da3a5329ed2b45ffd4b 100644 (file)
@@ -63,15 +63,17 @@ obj-$(CONFIG_SOUND_SONICVIBES)      += sonicvibes.o
 obj-$(CONFIG_SOUND_CMPCI)      += cmpci.o
 obj-$(CONFIG_SOUND_ES1370)     += es1370.o
 obj-$(CONFIG_SOUND_ES1371)     += es1371.o ac97_codec.o
+obj-$(CONFIG_SOUND_VRC5477)    += nec_vrc5477.o ac97_codec.o
 obj-$(CONFIG_SOUND_ESSSOLO1)   += esssolo1.o
 obj-$(CONFIG_SOUND_FUSION)     += cs46xx.o ac97_codec.o
 obj-$(CONFIG_SOUND_MAESTRO)    += maestro.o
 obj-$(CONFIG_SOUND_MAESTRO3)   += maestro3.o ac97_codec.o
 obj-$(CONFIG_SOUND_TRIDENT)    += trident.o ac97_codec.o
-obj-$(CONFIG_SOUND_RME96XX)    += rme96xx.o
+obj-$(CONFIG_SOUND_EMU10K1)    += ac97_codec.o
+obj-$(CONFIG_SOUND_RME96XX)     += rme96xx.o
 obj-$(CONFIG_SOUND_BT878)      += btaudio.o
-
 obj-$(CONFIG_SOUND_EMU10K1)    += ac97_codec.o
+
 ifeq ($(CONFIG_MIDI_EMU10K1),y)
   obj-$(CONFIG_SOUND_EMU10K1)  += sound.o
 endif
index 34060614b900e30ac6769d384bc6b79e72a3211b..40657b21b3f371ae8d6e2176913949bc1711ed31 100644 (file)
@@ -32,7 +32,7 @@
 #define __NO_VERSION__
 #include <linux/module.h>
 #include <linux/poll.h>
-#include <linux/malloc.h>
+#include <linux/slab.h>
 #include <linux/version.h>
 #include <linux/bitops.h>
 #include <asm/io.h>
index b0f20ed429acd67821d12a57860f1a4699498b44..b2ea46a86fb5213de1ee8571a1bf206bb670d8f5 100644 (file)
 #include <linux/spinlock.h>
 #include <asm/uaccess.h>
 #include <asm/hardirq.h>
-
-#if defined(CONFIG_INPUT_ANALOG) || defined(CONFIG_INPUT_ANALOG_MODULE)
 #include <linux/gameport.h>
-#else
-struct gameport {
-       int io;
-       int size;
-};
-
-extern inline void gameport_register_port(struct gameport *gameport)
-{
-}
-
-extern inline void gameport_unregister_port(struct gameport *gameport)
-{
-}
-#endif
 
 /* --------------------------------------------------------------------- */
 
@@ -1240,7 +1224,7 @@ static ssize_t es1370_write(struct file *file, const char *buffer, size_t count,
 {
        struct es1370_state *s = (struct es1370_state *)file->private_data;
        DECLARE_WAITQUEUE(wait, current);
-       ssize_t ret;
+       ssize_t ret = 0;
        unsigned long flags;
        unsigned swptr;
        int cnt;
index 3254daadb9ca915f75d7b4dbbafe1577997ad487..2d0714825c77935ed15c1f1e393b75d0d6950bcc 100644 (file)
 #include <asm/dma.h>
 #include <asm/uaccess.h>
 #include <asm/hardirq.h>
-
-#if defined(CONFIG_INPUT_ANALOG) || defined(CONFIG_INPUT_ANALOG_MODULE)
 #include <linux/gameport.h>
-#else
-struct gameport {
-       int io;
-       int size;
-};
-
-extern inline void gameport_register_port(struct gameport *gameport)
-{
-}
-
-extern inline void gameport_unregister_port(struct gameport *gameport)
-{
-}
-#endif
 
 /* --------------------------------------------------------------------- */
 
index 5e765b4d69f456c61ec612c3fd2d61e0ae273e52..f18f2a1619b6f753973123a5b46e76395898396a 100644 (file)
@@ -1952,7 +1952,7 @@ static void guswave_hw_control(int dev, unsigned char *event_rec)
        int voice, cmd;
        unsigned short p1, p2;
        unsigned int plong;
-       unsigned flags;
+       unsigned long flags;
 
        cmd = event_rec[2];
        voice = event_rec[3];
index 43803826ec9b34a875269653596d4e173aa7c179..0299f99a816eb9b128b3c26aa396a7ed91e97232 100644 (file)
 #include <linux/wrapper.h>
 #include <asm/uaccess.h>
 #include <asm/hardirq.h>
-
-#include "dm.h"
-
-#if defined(CONFIG_INPUT_ANALOG) || defined(CONFIG_INPUT_ANALOG_MODULE)
 #include <linux/gameport.h>
-#else
-struct gameport {
-       int io;
-       int size;
-};
 
-extern inline void gameport_register_port(struct gameport *gameport)
-{
-}
+#include "dm.h"
 
-extern inline void gameport_unregister_port(struct gameport *gameport)
-{
-}
-#endif
 
 /* --------------------------------------------------------------------- */
 
index afef733dc87d94ab5d41d2239842c1c724bfff60..0ef220eb1986002d7b68456ba5b0025e41e731e2 100644 (file)
@@ -732,4 +732,5 @@ module_exit(acm_exit);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
index 33066f6365a24cf188c257cd34a986a01a2dab89..0726f57359ec08602b240b5a18b8ce1f965640a3 100644 (file)
@@ -1345,4 +1345,5 @@ module_exit(usb_bluetooth_exit);
 /* Module information */
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
index fa8d365e042fffa205147248095e02cd6d5854c3..bffef60b29e9dc03d950aa7f80d07d7a832983cb 100644 (file)
@@ -33,7 +33,7 @@
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/string.h>
-#include <linux/malloc.h>
+#include <linux/slab.h>
 #include <linux/netdevice.h>
 #include <linux/etherdevice.h>
 #include <linux/skbuff.h>
@@ -54,6 +54,7 @@
 
 MODULE_AUTHOR(DRIVER_AUTHOR);
 MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE("GPL");
 
 /*
  * Some defines.
index 29b792ff2625be461b3d27e99decba35bc59b228..b552bf6c3f621861f5ad0d147e5d8287d71e862c 100644 (file)
@@ -848,6 +848,7 @@ static void __exit dabusb_cleanup (void)
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 MODULE_PARM (buffers, "i");
 MODULE_PARM_DESC (buffers, "Number of buffers (default=256)");
index ebe5ce45126b9c2384cd12109a9b248ebc3e0a4d..7e0587b1016efae5c6dc3df2ec95914ad4dcc65b 100644 (file)
@@ -531,4 +531,5 @@ module_exit (usb_dc2xx_cleanup);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
index ad9ac7b2226ae348132edb4a905f6e92641a132c..47d039e4f98f35831b955594c9555d1bc8495cc9 100644 (file)
@@ -1281,10 +1281,10 @@ static struct usb_driver hid_driver = {
 
 static int __init hid_init(void)
 {
-       usb_register(&hid_driver);
 #ifdef CONFIG_USB_HIDDEV
        hiddev_init();
 #endif
+       usb_register(&hid_driver);
        info(DRIVER_VERSION " " DRIVER_AUTHOR);
        info(DRIVER_DESC);
 
index 7947033d10bd32ae0ce12a02086566382fb5aaee..6c42312f2f9fd19398768dfecc7f2cdbe538c5f8 100644 (file)
 
 MODULE_AUTHOR("Michael Zappe <zapman@interlan.net>, Stephane Alnet <stephane@u-picardie.fr> and Brad Hards <bhards@bigpond.net.au>");
 MODULE_DESCRIPTION("KL5USB101 USB Ethernet driver");
+MODULE_LICENSE("GPL");
 
 static void *kaweth_probe(
             struct usb_device *dev,             /* the device */
index ed5cc1059673683c134f130b326dd15ab64f309e..4d300c114e09bb1c429fc834f1c438aa4e95f076 100644 (file)
@@ -808,6 +808,7 @@ const static struct vendor_product mts_supported_products[] =
        { "ScanMaker V6USL",    mts_sup_unknown},
        { "ScanMaker V6USL",    mts_sup_unknown},
        { "Scanmaker V6UL",     mts_sup_unknown},
+       { "Scanmaker V6UPL",    mts_sup_alpha},
 };
 
 /* The entries of microtek_table must correspond, line-by-line to
@@ -823,6 +824,7 @@ static struct usb_device_id mts_usb_ids [] =
        { USB_DEVICE(0x5da, 0x00a3) },
        { USB_DEVICE(0x5da, 0x80a3) },
        { USB_DEVICE(0x5da, 0x80ac) },
+       { USB_DEVICE(0x5da, 0x00b6) },
        { }                                             /* Terminating entry */
 };
 
@@ -1062,4 +1064,5 @@ module_exit(microtek_drv_exit);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
index 5e67eed5ee599c421dfc532f39f719270d3336d0..a1e20fda80944a9f064aa6807a5980c2a8c7e106 100644 (file)
@@ -156,6 +156,7 @@ MODULE_PARM(video_nr,"i");
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 static struct usb_driver ov511_driver;
 
index e9809ce763ba00159d8dcae9db61e0a8116096ae..bb8d84cff5954986abea8116b05f5d0581281a42 100644 (file)
@@ -83,6 +83,7 @@ static struct usb_device_id pegasus_ids[] = {
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 MODULE_PARM(loopback, "i");
 MODULE_PARM(mii_mode, "i");
 MODULE_PARM_DESC(loopback, "Enable MAC loopback mode (bit 0)");
index 75578bf14090e4cf9aa777167ba4cfb863244b25..8c20d5f37c66c1a08757221930201cbae9e30542 100644 (file)
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
+/*
+   Changes
+   2001/08/03  Alvarado   Added methods for changing white balance and 
+                          red/green gains
+ */
+
 /* Control functions for the cam; brightness, contrast, video mode, etc. */
 
 #ifdef __KERNEL__
@@ -603,12 +609,12 @@ int pwc_get_saturation(struct pwc_device *pdev)
        char buf;
        int ret;
 
-       if (pdev->type < 730)
+       if (pdev->type < 675)
                return -1;
        ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
                GET_CHROM_CTL,
                USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
-               SATURATION_MODE_FORMATTER1,
+               pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1,
                pdev->vcinterface,
                &buf, 1, HZ / 2);
        if (ret < 0)
@@ -620,7 +626,7 @@ int pwc_set_saturation(struct pwc_device *pdev, int value)
 {
        char buf;
 
-       if (pdev->type < 730)
+       if (pdev->type < 675)
                return -EINVAL;
        if (value < 0)
                value = 0;
@@ -631,7 +637,7 @@ int pwc_set_saturation(struct pwc_device *pdev, int value)
        return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
                SET_CHROM_CTL,
                USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
-               SATURATION_MODE_FORMATTER1,
+               pdev->type < 730 ? SATURATION_MODE_FORMATTER2 : SATURATION_MODE_FORMATTER1,
                pdev->vcinterface,
                &buf, 1, HZ / 2);
 }
@@ -825,6 +831,218 @@ static inline int pwc_restore_factory(struct pwc_device *pdev)
                NULL, 0, HZ / 2);
 }
 
+ /* ************************************************* */
+ /* Patch by Alvarado: (not in the original version   */
+
+ /*
+  * the camera recognizes modes from 0 to 4:
+  *
+  * 00: indoor (incandescant lighting)
+  * 01: outdoor (sunlight)
+  * 02: fluorescent lighting
+  * 03: manual
+  * 04: auto
+  */ 
+static inline int pwc_set_awb(struct pwc_device *pdev, int mode)
+{
+       char buf;
+       int ret;
+       
+       if (mode < 0)
+           mode = 0;
+       
+       if (mode > 4)
+           mode = 4;
+       
+       buf = mode & 0x07; /* just the lowest three bits */
+       
+       ret = usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
+               SET_CHROM_CTL,
+               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
+               WB_MODE_FORMATTER,
+               pdev->vcinterface,
+               &buf, 1, HZ / 2);
+       
+       if (ret < 0)
+               return ret;
+       return 0;
+}
+
+static inline int pwc_get_awb(struct pwc_device *pdev)
+{
+       unsigned char buf;
+       int ret;
+       
+       ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
+               GET_CHROM_CTL,
+               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
+               WB_MODE_FORMATTER,
+               pdev->vcinterface,
+               &buf, 1, HZ / 2);
+
+       if (ret < 0) 
+               return ret;
+       return buf;
+}
+
+static inline int pwc_set_red_gain(struct pwc_device *pdev, int value)
+{
+        unsigned char buf;
+
+       if (value < 0)
+               value = 0;
+       if (value > 0xffff)
+               value = 0xffff;
+
+       /* only the msb are considered */
+       buf = value >> 8;
+
+       return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
+               SET_CHROM_CTL,
+               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
+               PRESET_MANUAL_RED_GAIN_FORMATTER,
+               pdev->vcinterface,
+               &buf, 1, HZ / 2);
+}
+
+static inline int pwc_get_red_gain(struct pwc_device *pdev)
+{
+       unsigned char buf;
+       int ret;
+       
+       ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
+               GET_STATUS_CTL, 
+               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
+               PRESET_MANUAL_RED_GAIN_FORMATTER,
+               pdev->vcinterface,
+               &buf, 1, HZ / 2);
+
+       if (ret < 0)
+           return ret;
+       
+       return (buf << 8);
+}
+
+
+static inline int pwc_set_blue_gain(struct pwc_device *pdev, int value)
+{
+       unsigned char buf;
+
+       if (value < 0)
+               value = 0;
+       if (value > 0xffff)
+               value = 0xffff;
+
+       /* linear mapping of 0..0xffff to -0x80..0x7f */
+       buf = (value >> 8);
+
+       return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
+               SET_CHROM_CTL,
+               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
+               PRESET_MANUAL_BLUE_GAIN_FORMATTER,
+               pdev->vcinterface,
+               &buf, 1, HZ / 2);
+}
+
+static inline int pwc_get_blue_gain(struct pwc_device *pdev)
+{
+       unsigned char buf;
+       int ret;
+       
+       ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
+               GET_STATUS_CTL,
+               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
+               PRESET_MANUAL_BLUE_GAIN_FORMATTER,
+               pdev->vcinterface,
+               &buf, 1, HZ / 2);
+
+       if (ret < 0)
+           return ret;
+       
+       return (buf << 8);
+}
+
+/* The following two functions are different, since they only read the
+   internal red/blue gains, which may be different from the manual 
+   gains set or read above.
+ */   
+static inline int pwc_read_red_gain(struct pwc_device *pdev)
+{
+       unsigned char buf;
+       int ret;
+       
+       ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
+               GET_STATUS_CTL, 
+               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
+               READ_RED_GAIN_FORMATTER,
+               pdev->vcinterface,
+               &buf, 1, HZ / 2);
+
+       if (ret < 0)
+           return ret;
+       
+       return (buf << 8);
+}
+static inline int pwc_read_blue_gain(struct pwc_device *pdev)
+{
+       unsigned char buf;
+       int ret;
+       
+       ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
+               GET_STATUS_CTL,
+               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
+               READ_BLUE_GAIN_FORMATTER,
+               pdev->vcinterface,
+               &buf, 1, HZ / 2);
+
+       if (ret < 0)
+           return ret;
+       
+       return (buf << 8);
+}
+
+/* still unused (it doesn't work yet...) */
+static inline int pwc_set_led(struct pwc_device *pdev, int value)
+{
+       unsigned char buf;
+
+       if (value < 0)
+               value = 0;
+       if (value > 0xffff)
+               value = 0xffff;
+
+       buf = (value >> 8);
+
+       return usb_control_msg(pdev->udev, usb_sndctrlpipe(pdev->udev, 0),
+               SET_STATUS_CTL,
+               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
+               LED_FORMATTER,
+               pdev->vcinterface,
+               &buf, 1, HZ / 2);
+}
+
+/* still unused (it doesn't work yet...) */
+static inline int pwc_get_led(struct pwc_device *pdev)
+{
+       unsigned char buf;
+       int ret;
+       
+       ret = usb_control_msg(pdev->udev, usb_rcvctrlpipe(pdev->udev, 0),
+               GET_STATUS_CTL,
+               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
+               LED_FORMATTER,
+               pdev->vcinterface,
+               &buf, 1, HZ / 2);
+
+       if (ret < 0)
+           return ret;
+       
+       return (buf << 8);
+}
+
+ /* End of Add-Ons                                    */
+ /* ************************************************* */
+
 int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
 {
        switch(cmd) {
@@ -910,6 +1128,74 @@ int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
                break;
        }
        
+
+ /* ************************************************* */
+ /* Begin of Add-Ons for color compensation           */
+
+        case VIDIOCPWCSAWB:
+       {
+               struct pwc_whitebalance wb;
+               int ret;
+               
+               if (copy_from_user(&wb, arg, sizeof(wb)))
+                       return -EFAULT;
+       
+               ret = pwc_set_awb(pdev, wb.mode);
+               if (ret >= 0 && wb.mode == PWC_WB_MANUAL) {
+                       pwc_set_red_gain(pdev, wb.manual_red);
+                       pwc_set_blue_gain(pdev, wb.manual_blue);
+               }
+               break;
+       }
+
+       case VIDIOCPWCGAWB:
+       {
+               struct pwc_whitebalance wb;
+               
+               memset(&wb, 0, sizeof(wb));
+               wb.mode = pwc_get_awb(pdev);
+               if (wb.mode < 0)
+                       return -EINVAL;
+               wb.manual_red = pwc_get_red_gain(pdev);
+               wb.manual_blue = pwc_get_blue_gain(pdev);
+               if (wb.mode == PWC_WB_AUTO) {
+                       wb.read_red = pwc_read_red_gain(pdev);
+                       wb.read_blue = pwc_read_blue_gain(pdev);
+               }
+               break;
+       }
+
+        case VIDIOCPWCSLED:
+       {
+           int led, ret;
+           if (copy_from_user(&led,arg,sizeof(led)))
+               return -EFAULT;
+           else {
+               /* ret = pwc_set_led(pdev, led); */
+               ret = 0;
+               if (ret<0)
+                   return ret;
+           }
+           break;
+       }
+
+
+
+       case VIDIOCPWCGLED:
+       {
+               int led;
+               
+               led = pwc_get_led(pdev); 
+               if (led < 0)
+                       return -EINVAL;
+               if (copy_to_user(arg, &led, sizeof(led)))
+                       return -EFAULT;
+               break;
+       }
+
+ /* End of Add-Ons                                    */
+ /* ************************************************* */
+
        default:
                return -ENOIOCTLCMD;
                break;
@@ -918,3 +1204,7 @@ int pwc_ioctl(struct pwc_device *pdev, unsigned int cmd, void *arg)
 }
 
 #endif
+
+
+
+
index 43de3063a1ce2b571ab1f9f795121768430cbd0a..f33d427417cdb88f04d03c7083e1a2e7deb8cc39 100644 (file)
    udev: struct usb_device *
    vdev: struct video_device *
    pdev: struct pwc_devive *
-*/   
+*/
+
+/* Contributors:
+   - Alvarado: adding whitebalance code
+   - Alistar Moire: QuickCam 3000 Pro testing
+*/
 
 #include <linux/errno.h>
 #include <linux/init.h>
@@ -68,6 +73,7 @@ static __devinitdata struct usb_device_id pwc_device_table [] = {
        { USB_DEVICE(0x0471, 0x0311) },
        { USB_DEVICE(0x0471, 0x0312) },
        { USB_DEVICE(0x069A, 0x0001) },
+       { USB_DEVICE(0x046D, 0x0b80) },
        { }
 };
 MODULE_DEVICE_TABLE(usb, pwc_device_table);
@@ -85,10 +91,10 @@ static struct usb_driver pwc_driver =
 
 static int default_size = PSZ_QCIF;
 static int default_fps = 10;
-static int default_palette = VIDEO_PALETTE_YUV420P; /* This is normal for webcams */
+static int default_palette = VIDEO_PALETTE_YUV420P; /* This format is understood by most tools */
 static int default_fbufs = 3;   /* Default number of frame buffers */
 static int default_mbufs = 2;  /* Default number of mmap() buffers */
-       int pwc_trace = TRACE_MODULE | TRACE_FLOW;
+       int pwc_trace = TRACE_MODULE | TRACE_FLOW | TRACE_PWCX;
 static int power_save = 0;
 int pwc_preferred_compression = 2; /* 0..3 = uncompressed..high */
 
@@ -167,109 +173,90 @@ static struct video_device pwc_template = {
  */
 static inline unsigned long uvirt_to_kva(pgd_t *pgd, unsigned long adr)
 {
-       unsigned long ret = 0UL;
+        unsigned long ret = 0UL;
        pmd_t *pmd;
        pte_t *ptep, pte;
-
+  
        if (!pgd_none(*pgd)) {
-               pmd = pmd_offset(pgd, adr);
-               if (!pmd_none(*pmd)) {
-                       ptep = pte_offset(pmd, adr);
-                       pte = *ptep;
-                       if (pte_present(pte)) {
-                               ret = (unsigned long) page_address(pte_page(pte));
-                               ret |= (adr & (PAGE_SIZE-1));
+                pmd = pmd_offset(pgd, adr);
+                if (!pmd_none(*pmd)) {
+                        ptep = pte_offset(pmd, adr);
+                        pte = *ptep;
+                        if(pte_present(pte)) {
+                               ret  = (unsigned long) page_address(pte_page(pte));
+                               ret |= (adr & (PAGE_SIZE - 1));
+                               
                        }
-               }
-       }
-       return ret;
-}
-
-static inline unsigned long uvirt_to_bus(unsigned long adr)
-{
-       unsigned long kva, ret;
-
-       kva = uvirt_to_kva(pgd_offset(current->mm, adr), adr);
-       ret = virt_to_bus((void *)kva);
+                }
+        }
        return ret;
 }
 
-static inline unsigned long kvirt_to_bus(unsigned long adr)
-{
-       unsigned long va, kva, ret;
 
-       va = VMALLOC_VMADDR(adr);
-       kva = uvirt_to_kva(pgd_offset_k(va), va);
-       ret = virt_to_bus((void *)kva);
-       return ret;
-}
 
 /* Here we want the physical address of the memory.
  * This is used when initializing the contents of the
  * area and marking the pages as reserved.
  */
-static inline unsigned long kvirt_to_pa(unsigned long adr)
+static inline unsigned long kvirt_to_pa(unsigned long adr) 
 {
-       unsigned long va, kva, ret;
+        unsigned long va, kva, ret;
 
-       va = VMALLOC_VMADDR(adr);
-       kva = uvirt_to_kva(pgd_offset_k(va), va);
+        va = VMALLOC_VMADDR(adr);
+        kva = uvirt_to_kva(pgd_offset_k(va), va);
        ret = __pa(kva);
-       return ret;
+        return ret;
 }
 
-static void *rvmalloc(unsigned long size)
+static void * rvmalloc(signed long size)
 {
-       void *mem;
+       void * mem;
        unsigned long adr, page;
 
-       /* Round it off to PAGE_SIZE */
-       size += (PAGE_SIZE - 1);
-       size &= ~(PAGE_SIZE - 1);
-
-       mem = vmalloc(size);
-       if (!mem)
-               return NULL;
-
-       memset(mem, 0, size); /* Clear the ram out, no junk to the user */
-       adr = (unsigned long) mem;
-       while (size > 0) {
-               page = kvirt_to_pa(adr);
-               mem_map_reserve(MAP_NR(__va(page)));
-               adr += PAGE_SIZE;
-               if (size > PAGE_SIZE)
-                       size -= PAGE_SIZE;
-               else
-                       size = 0;
+        /* Round it off to PAGE_SIZE */
+        size += (PAGE_SIZE - 1);
+        size &= ~(PAGE_SIZE - 1);      
+        
+        mem=vmalloc_32(size);
+       if (mem) 
+       {
+               memset(mem, 0, size); /* Clear the ram out, no junk to the user */
+               adr=(unsigned long) mem;
+               while (size > 0) 
+                {
+                       page = kvirt_to_pa(adr);
+                       mem_map_reserve(virt_to_page(__va(page)));
+                       adr+=PAGE_SIZE;
+                       size-=PAGE_SIZE;
+               }
        }
        return mem;
 }
 
-static void rvfree(void *mem, unsigned long size)
+static void rvfree(void * mem, signed long size)
 {
-       unsigned long adr, page;
-
-       if (!mem)
-               return;
-
-       size += (PAGE_SIZE - 1);
-       size &= ~(PAGE_SIZE - 1);
-
-       adr=(unsigned long) mem;
-       while (size > 0) {
-               page = kvirt_to_pa(adr);
-               mem_map_unreserve(MAP_NR(__va(page)));
-               adr += PAGE_SIZE;
-               if (size > PAGE_SIZE)
-                       size -= PAGE_SIZE;
-               else
-                       size = 0;
+        unsigned long adr, page;
+        
+        /* Round it off to PAGE_SIZE */
+        size += (PAGE_SIZE - 1);
+        size &= ~(PAGE_SIZE - 1);      
+       if (mem) 
+       {
+               adr=(unsigned long) mem;
+               while (size > 0) 
+                {
+                       page = kvirt_to_pa(adr);
+                       mem_map_unreserve(virt_to_page(__va(page)));
+                       adr+=PAGE_SIZE;
+                       size-=PAGE_SIZE;
+               }
+               vfree(mem);
        }
-       vfree(mem);
 }
 
 
 
+
 static int pwc_allocate_buffers(struct pwc_device *pdev)
 {
        int i;
@@ -294,6 +281,7 @@ static int pwc_allocate_buffers(struct pwc_device *pdev)
                                Err("Failed to allocate iso buffer %d.\n", i);
                                return -ENOMEM;
                        }
+                       Trace(TRACE_MEMORY, "Allocated iso buffer at %p.\n", kbuf);
                        pdev->sbuf[i].data = kbuf;
                        memset(kbuf, 0, ISO_BUFFER_SIZE);
                }
@@ -306,6 +294,7 @@ static int pwc_allocate_buffers(struct pwc_device *pdev)
                        Err("Failed to allocate frame buffer structure.\n");
                        return -ENOMEM;
                }
+               Trace(TRACE_MEMORY, "Allocated frame buffer structure at %p.\n", kbuf);
                pdev->fbuf = kbuf;
                memset(kbuf, 0, default_fbufs * sizeof(struct pwc_frame_buf));
        }
@@ -317,8 +306,9 @@ static int pwc_allocate_buffers(struct pwc_device *pdev)
                                Err("Failed to allocate frame buffer %d.\n", i);
                                return -ENOMEM;
                        }
+                       Trace(TRACE_MEMORY, "Allocated frame buffer %d at %p.\n", i, kbuf);
                        pdev->fbuf[i].data = kbuf;
-                       memset(kbuf, 0, PWC_FRAME_SIZE);
+                       memset(kbuf, 128, PWC_FRAME_SIZE);
                }
        }
        
@@ -330,23 +320,24 @@ static int pwc_allocate_buffers(struct pwc_device *pdev)
                        Err("Failed to allocate decompress table.\n");
                        return -ENOMEM;
                }
+               Trace(TRACE_MEMORY, "Allocated decompress table %p.\n", kbuf);
        }
        pdev->decompress_data = kbuf;
        
        /* Allocate image buffer; double buffer for mmap() */
-       kbuf = rvmalloc(default_mbufs * pdev->view_max.size * 4);
+       kbuf = rvmalloc(default_mbufs * pdev->len_per_image);
        if (kbuf == NULL) {
                Err("Failed to allocate image buffer(s).\n");
                return -ENOMEM;
        }
+       Trace(TRACE_MEMORY, "Allocated image buffer at %p.\n", kbuf);
        pdev->image_data = kbuf;
        for (i = 0; i < default_mbufs; i++)
-               pdev->image_ptr[i] = kbuf + (i * pdev->view_max.size * 4);
+               pdev->image_ptr[i] = kbuf + i * pdev->len_per_image;
        for (; i < MAX_IMAGES; i++)
                pdev->image_ptr[i] = NULL;
 
        Trace(TRACE_MEMORY, "Leaving pwc_allocate_buffers().\n");
-
        return 0;
 }
 
@@ -366,18 +357,18 @@ static void pwc_free_buffers(struct pwc_device *pdev)
 #endif 
 
        /* Release Iso-pipe buffers */
-       Trace(TRACE_MEMORY, "Freeing ISO buffers.\n");
        for (i = 0; i < MAX_ISO_BUFS; i++)
                if (pdev->sbuf[i].data != NULL) {
+                       Trace(TRACE_MEMORY, "Freeing ISO buffer at %p.\n", pdev->sbuf[i].data);
                        kfree(pdev->sbuf[i].data);
                        pdev->sbuf[i].data = NULL;
                }
 
        /* The same for frame buffers */
-       Trace(TRACE_MEMORY, "Freeing frame buffers.\n");
        if (pdev->fbuf != NULL) {
                for (i = 0; i < default_fbufs; i++) {
                        if (pdev->fbuf[i].data != NULL) {
+                               Trace(TRACE_MEMORY, "Freeing frame buffer %d at %p.\n", i, pdev->fbuf[i].data);
                                vfree(pdev->fbuf[i].data);
                                pdev->fbuf[i].data = NULL;
                        }
@@ -387,17 +378,18 @@ static void pwc_free_buffers(struct pwc_device *pdev)
        }
 
        /* Intermediate decompression buffer & tables */
-       Trace(TRACE_MEMORY, "Freeing decompression buffer\n");
        if (pdev->decompress_data != NULL) {
+               Trace(TRACE_MEMORY, "Freeing decompression buffer at %p.\n", pdev->decompress_data);
                kfree(pdev->decompress_data);
                pdev->decompress_data = NULL;
        }
        pdev->decompressor = NULL;
 
        /* Release image buffers */
-       Trace(TRACE_MEMORY, "Freeing image buffers\n");
-       if (pdev->image_data != NULL)
-               rvfree(pdev->image_data, default_mbufs * pdev->view_max.size * 4);
+       if (pdev->image_data != NULL) {
+               Trace(TRACE_MEMORY, "Freeing image buffer at %p.\n", pdev->image_data);
+               rvfree(pdev->image_data, default_mbufs * pdev->len_per_image);
+       }
        pdev->image_data = NULL;
        Trace(TRACE_MEMORY, "Leaving free_buffers().\n");
 }
@@ -461,7 +453,8 @@ static void pwc_free_buffers(struct pwc_device *pdev)
  */
 static inline int pwc_next_fill_frame(struct pwc_device *pdev)
 {
-       int ret, flags;
+       int ret;
+       unsigned long flags;
        
        ret = 0;
        spin_lock_irqsave(&pdev->ptrlock, flags);
@@ -512,7 +505,8 @@ static inline int pwc_next_fill_frame(struct pwc_device *pdev)
  */
 static void pwc_reset_buffers(struct pwc_device *pdev)
 {
-       int i, flags;
+       int i;
+       unsigned long flags;
 
        spin_lock_irqsave(&pdev->ptrlock, flags);
        pdev->full_frames = NULL;
@@ -541,7 +535,8 @@ static void pwc_reset_buffers(struct pwc_device *pdev)
  */
 static int pwc_handle_frame(struct pwc_device *pdev)
 {
-       int ret = 0, flags;
+       int ret = 0;
+       unsigned long flags;
        
        spin_lock_irqsave(&pdev->ptrlock, flags);
        /* First grab our read_frame; this is removed from all lists, so
@@ -610,6 +605,7 @@ static int pwc_set_palette(struct pwc_device *pdev, int pal)
                pwc_set_image_buffer_size(pdev);
                return 0;
        }
+       Trace(TRACE_READ, "Palette %d not supported.\n", pal);
        return -1;
 }
 
@@ -642,7 +638,17 @@ static void pwc_isoc_handler(purb_t urb)
                return;
        }
        if (urb->status != -EINPROGRESS && urb->status != 0) {
-               Trace(TRACE_FLOW, "pwc_isoc_handler() called with status %d.\n", urb->status);
+               char *errmsg;
+               
+               errmsg = "Unknown";
+               switch(urb->status) {
+                       case -ENOSR:            errmsg = "Buffer error (overrun)"; break;
+                       case -EPIPE:            errmsg = "Babble/stalled (bad cable?)"; break;
+                       case -EPROTO:           errmsg = "Bit-stuff error (bad cable?)"; break;
+                       case -EILSEQ:           errmsg = "CRC/Timeout"; break;
+                       case -ETIMEDOUT:        errmsg = "NAK (device does not respond)"; break;
+               }
+               Trace(TRACE_FLOW, "pwc_isoc_handler() called with status %d [%s].\n", urb->status, errmsg);
                return;
        }
 
@@ -739,9 +745,9 @@ static void pwc_isoc_handler(purb_t urb)
                                                                pdev->vframes_dumped++;
                                                                if ((pdev->vframe_count > FRAME_LOWMARK) && (pwc_trace & TRACE_FLOW)) {
                                                                        if (pdev->vframes_dumped < 20)
-                                                                               Info("Dumping frame %d.\n", pdev->vframe_count);
+                                                                               Trace(TRACE_FLOW, "Dumping frame %d.\n", pdev->vframe_count);
                                                                        if (pdev->vframes_dumped == 20)
-                                                                               Info("Dumping frame %d (last message).\n", pdev->vframe_count);
+                                                                               Trace(TRACE_FLOW, "Dumping frame %d (last message).\n", pdev->vframe_count);
                                                                }
                                                        }
                                                        fbuf = pdev->fill_frame;
@@ -1104,7 +1110,7 @@ static long pwc_video_read(struct video_device *vdev, char *buf, unsigned long c
        if (pdev == NULL)
                return -EFAULT;
        if (pdev->unplugged) {
-               Debug("pwc_video_read: Device got unplugged (1).\n");
+               Info("pwc_video_read: Device got unplugged (1).\n");
                return -EPIPE; /* unplugged device! */
        }
 
@@ -1166,7 +1172,7 @@ static unsigned int pwc_video_poll(struct video_device *vdev, struct file *file,
        
        poll_wait(file, &pdev->frameq, wait);
        if (pdev->unplugged) {
-               Debug("pwc_video_poll: Device got unplugged.\n");
+               Info("pwc_video_poll: Device got unplugged.\n");
                return POLLERR;
        }               
        if (pdev->full_frames != NULL) /* we have frames waiting */
@@ -1370,10 +1376,10 @@ static int pwc_video_ioctl(struct video_device *vdev, unsigned int cmd, void *ar
                        int i;
 
                        memset(&vm, 0, sizeof(vm));
-                       vm.size = default_mbufs * pdev->view_max.size * 4;
-                       vm.frames = default_mbufs; /* double buffering should be enough */
+                       vm.size = default_mbufs * pdev->len_per_image;
+                       vm.frames = default_mbufs; /* double buffering should be enough for most applications */
                        for (i = 0; i < default_mbufs; i++)
-                               vm.offsets[i] = i * pdev->view_max.size * 4;
+                               vm.offsets[i] = i * pdev->len_per_image;
 
                        if (copy_to_user((void *)arg, (void *)&vm, sizeof(vm)))
                                return -EFAULT;
@@ -1546,11 +1552,10 @@ static int pwc_video_mmap(struct video_device *vdev, const char *adr, unsigned l
        unsigned long start = (unsigned long)adr;
        unsigned long page, pos;
        
-       Trace(TRACE_READ, "mmap(0x%p, 0x%p, %lu) called.\n", vdev, adr, size);
+       Trace(TRACE_MEMORY, "mmap(0x%p, 0x%p, %lu) called.\n", vdev, adr, size);
        pdev = vdev->priv;
 
        /* FIXME - audit mmap during a read */          
-
        pos = (unsigned long)pdev->image_data;
        while (size > 0) {
                page = kvirt_to_pa(pos);
@@ -1651,7 +1656,18 @@ static void *usb_pwc_probe(struct usb_device *udev, unsigned int ifnum, const st
                        break;
                }
        }
-       else return NULL; /* Not Philips or Askey, for sure. */
+        else if (vendor_id == 0x046d) {
+               switch(product_id) {
+               case 0x08b0:
+                       Info("Logitech QuickCam 3000 Pro detected.\n");
+                       type_id = 730;
+                       break;
+               default:
+                       return NULL;
+                       break;
+               }
+        }
+        else return NULL; /* Not Philips or Askey, for sure. */
 
        if (udev->descriptor.bNumConfigurations > 1)
                Info("Warning: more than 1 configuration available.\n");
@@ -1718,6 +1734,7 @@ static void usb_pwc_disconnect(struct usb_device *udev, void *ptr)
 {
        struct pwc_device *pdev;
 
+       lock_kernel();
        free_mem_leak();
 
        pdev = (struct pwc_device *)ptr;
@@ -1770,6 +1787,7 @@ static void usb_pwc_disconnect(struct usb_device *udev, void *ptr)
                }
        }
        pdev->udev = NULL;
+       unlock_kernel();
        kfree(pdev);
 }
 
@@ -1848,8 +1866,6 @@ static int __init usb_pwc_init(void)
                        default_palette = VIDEO_PALETTE_YUV420P;
                else {
                        Err("Palette not recognized: try palette=yuv420 or yuv420p.\n");
-                       Info("Download the driver from http://www.smcc.demon.nl/webcam/ for in kernel\n");
-                       Info("format conversion support.\n");
                        return -EINVAL;
                }
                Info("Default palette set to %d.\n", default_palette);
index 8397b2932450e96db69416d086d7ccb1bfe25637..8a84b437a757c2aeee047f8b850bc85520807d06 100644 (file)
@@ -1,3 +1,6 @@
+#ifndef PWC_IOCTL_H
+#define PWC_IOCTL_H
+
 /* (C) 2001 Nemosoft Unv.    webcam@smcc.demon.nl
    
    This program is free software; you can redistribute it and/or modify
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
 
-#ifndef PWC_IOCTL_H
-#define PWC_IOCTL_H
+/*
+   Changes
+   2001/08/03  Alvarado   Added ioctl constants to access methods for 
+                          changing white balance and red/blue gains
+ */
 
 /* These are private ioctl() commands, specific for the Philips webcams.
    They contain functions not found in other webcams, and settings not
 #define PWC_FPS_SNAPSHOT       0x00400000
 
 
+/* pwc_whitebalance.mode values */
+#define PWC_WB_INDOOR          0
+#define PWC_WB_OUTDOOR         1
+#define PWC_WB_FL              2
+#define PWC_WB_MANUAL          3
+#define PWC_WB_AUTO            4
+
+/* Used with VIDIOCPWC[SG]AWB (Auto White Balance). 
+   Set mode to one of the PWC_WB_* values above.
+   *red and *blue are the respective gains of these colour components inside 
+   the camera; range 0..65535
+   When mode == PWC_WB_MANUAL, manual_red and manual_blue are set or read; 
+   otherwise undefined.
+   read_red and read_blue are read-only.
+*/   
+   
+struct pwc_whitebalance
+{
+       int mode;
+       int manual_red, manual_blue;    /* R/W */
+       int read_red, read_blue;        /* R/O */
+};
+
+
  /* Restore user settings */
 #define VIDIOCPWCRUSER         _IO('v', 192)
  /* Save user settings */
  /* Set shutter speed; int < 0 = auto; >= 0 = fixed, range 0..65535 */
 #define VIDIOCPWCSSHUTTER      _IOW('v', 201, int)
 
+ /* Color compensation (Auto White Balance) */
+#define VIDIOCPWCSAWB           _IOW('v', 202, struct pwc_whitebalance)
+#define VIDIOCPWCGAWB           _IOR('v', 202, struct pwc_whitebalance)
+
+ /* Turn LED on/off ; int range 0..65535 */
+#define VIDIOCPWCSLED           _IOW('v', 205, int)
+
+ /* Get state of LED; int range 0..65535 */
+#define VIDIOCPWCGLED           _IOR('v', 205, int)
+
 #endif
index e30ce95f049353072d43d6b54fdd050e44d7c8eb..254da4186dd517a917b9b8a01c7fe84d5b1b893e 100644 (file)
@@ -97,6 +97,8 @@ void pwc_construct(struct pwc_device *pdev)
        }
        pdev->view_min.size = pdev->view_min.x * pdev->view_min.y;
        pdev->view_max.size = pdev->view_max.x * pdev->view_max.y;
+       /* length of image, in YUV format */
+       pdev->len_per_image = (pdev->view_max.size * 3) / 2;
 }
 
 
index b6dc4719f47e692b20bd14f7d9d11a0e34040ac3..a8404a4ac240455d56200c9493635a257533406d 100644 (file)
@@ -24,6 +24,7 @@
 #include <linux/spinlock.h>
 #include <linux/videodev.h>
 #include <linux/wait.h>
+#include <linux/smp_lock.h>
 
 #include <asm/semaphore.h>
 #include <asm/errno.h>
 #define PWC_MAGIC 0x89DC10ABUL
 #undef PWC_MAGIC
 
-/* Debugging info on/off */
+/* Turn some debugging options on/off */
 #define PWC_DEBUG 0
 
+/* Trace certain actions in the driver */
 #define TRACE_MODULE   0x0001
 #define TRACE_PROBE    0x0002
 #define TRACE_OPEN     0x0004
@@ -58,8 +60,8 @@
 
 /* Version block */
 #define PWC_MAJOR      8
-#define PWC_MINOR      1
-#define PWC_VERSION    "8.1"
+#define PWC_MINOR      2
+#define PWC_VERSION    "8.2"
 #define PWC_NAME       "pwc"
 
 /* Turn certain features on/off */
@@ -184,6 +186,7 @@ struct pwc_device
    void *image_data;                   /* total buffer, which is subdivided into ... */
    void *image_ptr[MAX_IMAGES];                /* ...several images... */
    int fill_image;                     /* ...which are rotated. */
+   int len_per_image;                  /* length per image */
    int image_read_pos;                 /* In case we read data in pieces, keep track of were we are in the imagebuffer */
    int image_used[MAX_IMAGES];         /* For MCAPTURE and SYNC */
 
index 2f94174a8c0d5b09c94eb126e5ee9b82cb7527ee..244674864c55e6cf4fc0b1a28cdcee1a0f2cd90d 100644 (file)
@@ -546,4 +546,5 @@ module_exit(usb_rio_cleanup);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
index a0e7a9698dd9aa6a65f6cae817f702708e4749ec..91a3dc0a6074a5971efcd42a1f89447223b074c6 100644 (file)
@@ -66,6 +66,7 @@ MODULE_DEVICE_TABLE(usb, device_table);
 
 MODULE_AUTHOR("Jeroen Vreeken <pe1rxq@amsat.org>");
 MODULE_DESCRIPTION("SE401 USB Camera Driver");
+MODULE_LICENSE("GPL");
 MODULE_PARM(flickerless, "i");
 MODULE_PARM_DESC(flickerless, "Net frequency to adjust exposure time to (0/50/60)");
 MODULE_PARM(video_nr, "i");
@@ -1349,7 +1350,6 @@ static long se401_read(struct video_device *dev, char *buf, unsigned long count,
                return ret;     
        if (copy_to_user(buf, se401->frame[0].data, realcount))
                return -EFAULT;
-       return realcount;
 
        return realcount;
 }
index 976698b6c43abaf94572a4b92d671efa8c86ad43..22a25862b7e658ea40d03bbd0804e918d778c7f1 100644 (file)
@@ -30,6 +30,7 @@ if [ "$CONFIG_USB_SERIAL" != "n" ]; then
   dep_tristate '  USB MCT Single Port Serial Driver (EXPERIMENTAL)' CONFIG_USB_SERIAL_MCT_U232 $CONFIG_USB_SERIAL $CONFIG_EXPERIMENTAL
   dep_tristate '  USB Prolific 2303 Single Port Serial Driver (EXPERIMENTAL)' CONFIG_USB_SERIAL_PL2303 $CONFIG_USB_SERIAL $CONFIG_EXPERIMENTAL
   dep_tristate '  USB REINER SCT cyberJack pinpad/e-com chipcard reader (EXPERIMENTAL)' CONFIG_USB_SERIAL_CYBERJACK $CONFIG_USB_SERIAL $CONFIG_EXPERIMENTAL
+  dep_tristate '  USB Xircom / Entregra Single Port Serial Driver (EXPERIMENTAL)' CONFIG_USB_SERIAL_XIRCOM $CONFIG_USB_SERIAL $CONFIG_EXPERIMENTAL
   dep_tristate '  USB ZyXEL omni.net LCD Plus Driver (EXPERIMENTAL)' CONFIG_USB_SERIAL_OMNINET $CONFIG_USB_SERIAL $CONFIG_EXPERIMENTAL
 fi
 
index cb4256b635f67ba2dddc22fc4d4c76d4d01bc090..836e0d3bebf07534f948249c1f861f494e77a532 100644 (file)
@@ -11,6 +11,7 @@ obj-$(CONFIG_USB_SERIAL_VISOR)                        += visor.o
 obj-$(CONFIG_USB_SERIAL_WHITEHEAT)             += whiteheat.o
 obj-$(CONFIG_USB_SERIAL_FTDI_SIO)              += ftdi_sio.o
 obj-$(CONFIG_USB_SERIAL_KEYSPAN_PDA)           += keyspan_pda.o
+obj-$(CONFIG_USB_SERIAL_XIRCOM)                        += keyspan_pda.o
 obj-$(CONFIG_USB_SERIAL_KEYSPAN)               += keyspan.o
 obj-$(CONFIG_USB_SERIAL_OMNINET)               += omninet.o
 obj-$(CONFIG_USB_SERIAL_DIGI_ACCELEPORT)       += digi_acceleport.o
index 62e750cd463ab1292e72906b1bcc8a496fd7f837..556afcf3ca2f3ae8aa30381b328272f280c697b9 100644 (file)
@@ -626,6 +626,7 @@ module_exit (belkin_sa_exit);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 MODULE_PARM(debug, "i");
 MODULE_PARM_DESC(debug, "Debug enabled or not");
index c44c80df1aa8630620bcd8f0077493d4866c1763..6f6139854db0ac805144b7d1b20999e4fb8ec509 100644 (file)
@@ -515,6 +515,7 @@ module_exit(cyberjack_exit);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 MODULE_PARM(debug, "i");
 MODULE_PARM_DESC(debug, "Debug enabled or not");
index 498e8348a40635fd8dfe48e08b135b6d0d2b6319..0abba02960aee64ce3d2bc5c170e24e07ed5aafe 100644 (file)
@@ -2092,6 +2092,7 @@ module_exit(digi_exit);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 MODULE_PARM(debug, "i");
 MODULE_PARM_DESC(debug, "Debug enabled or not");
index 1f29d37a0a8a0075093927d7d24e28e9f25f7688..c83337d72d169a771616f01ac8a425b2477f6844 100644 (file)
@@ -665,6 +665,7 @@ module_exit(empeg_exit);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 MODULE_PARM(debug, "i");
 MODULE_PARM_DESC(debug, "Debug enabled or not");
index 6b01db259b2ec3465e0bc2a666f91791b885ee5b..91a814bd3b0ba4a2fe56381fa039a1e4fa1a8fdb 100644 (file)
@@ -1003,6 +1003,7 @@ module_exit(ftdi_sio_exit);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 MODULE_PARM(debug, "i");
 MODULE_PARM_DESC(debug, "Debug enabled or not");
index ad97803caefa4c3883a7bd243a8fcb5fd5e93789..42c04e5f8193d03a354a67a5d1a42df68693452b 100644 (file)
@@ -3103,6 +3103,7 @@ module_exit(edgeport_exit);
 /* Module information */
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 MODULE_PARM(debug, "i");
 MODULE_PARM_DESC(debug, "Debug enabled or not");
index 34467f9a4aa9f0d1c6856abb2693c333ec69274d..5698e078bc0e20dd2bb33bbfced06db5b4a5c476 100644 (file)
@@ -1719,6 +1719,7 @@ static void keyspan_shutdown (struct usb_serial *serial)
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 MODULE_PARM(debug, "i");
 MODULE_PARM_DESC(debug, "Debug enabled or not");
index 7eea4fa098f188537545428f6b3a7e38ced9d488..353e51f68f6b381dd0b1b03b79488f8be9c9260d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * USB Keyspan PDA Converter driver
+ * USB Keyspan PDA / Xircom / Entregra Converter driver
  *
  * Copyright (c) 1999 - 2001 Greg Kroah-Hartman        <greg@kroah.com>
  * Copyright (c) 1999, 2000 Brian Warner       <warner@lothar.com>
  *
  * See Documentation/usb/usb-serial.txt for more information on using this driver
  * 
+ * (09/07/2001) gkh
+ *     cleaned up the Xircom support.  Added ids for Entregra device which is
+ *     the same as the Xircom device.  Enabled the code to be compiled for
+ *     either Xircom or Keyspan devices.
+ *
+ * (08/11/2001) Cristian M. Craciunescu
+ *     support for Xircom PGSDB9
+ *
  * (05/31/2001) gkh
  *     switched from using spinlock to a semaphore, which fixes lots of problems.
  *
@@ -88,13 +96,32 @@ struct ezusb_hex_record {
        __u8 data[16];
 };
 
+/* make a simple define to handle if we are compiling keyspan_pda or xircom support */
+#if defined(CONFIG_USB_SERIAL_KEYSPAN_PDA) || defined(CONFIG_USB_SERIAL_KEYSPAN_PDA_MODULE)
+       #define KEYSPAN
+#else
+       #undef KEYSPAN
+#endif
+#if defined(CONFIG_USB_SERIAL_XIRCOM) || defined(CONFIG_USB_SERIAL_XIRCOM_MODULE)
+       #define XIRCOM
+#else
+       #undef XIRCOM
+#endif
+
+#ifdef KEYSPAN
 #include "keyspan_pda_fw.h"
+#endif
+
+#ifdef XIRCOM
+#include "xircom_pgs_fw.h"
+#endif
+
 #include "usb-serial.h"
 
 /*
  * Version Information
  */
-#define DRIVER_VERSION "v1.0.0"
+#define DRIVER_VERSION "v1.1"
 #define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>"
 #define DRIVER_DESC "USB Keyspan PDA Converter driver"
 
@@ -105,12 +132,25 @@ struct keyspan_pda_private {
        struct tq_struct        unthrottle_task;
 };
 
+
 #define KEYSPAN_VENDOR_ID              0x06cd
 #define KEYSPAN_PDA_FAKE_ID            0x0103
 #define KEYSPAN_PDA_ID                 0x0104 /* no clue */
 
+/* For Xircom PGSDB9 and older Entregra version of the same device */
+#define XIRCOM_VENDOR_ID               0x085a
+#define XIRCOM_FAKE_ID                 0x8027
+#define ENTREGRA_VENDOR_ID             0x1645
+#define ENTREGRA_FAKE_ID               0x8093
+
 static __devinitdata struct usb_device_id id_table_combined [] = {
+#ifdef KEYSPAN
        { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
+#endif
+#ifdef XIRCOM
+       { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
+       { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) },
+#endif
        { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
        { }                                             /* Terminating entry */
 };
@@ -122,10 +162,24 @@ static __devinitdata struct usb_device_id id_table_std [] = {
        { }                                             /* Terminating entry */
 };
 
+#ifdef KEYSPAN
 static __devinitdata struct usb_device_id id_table_fake [] = {
        { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
        { }                                             /* Terminating entry */
 };
+#endif
+
+#ifdef XIRCOM
+static __devinitdata struct usb_device_id id_table_fake_xircom [] = {
+        { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
+        { }                                             
+};
+
+static __devinitdata struct usb_device_id id_table_fake_entregra [] = {
+        { USB_DEVICE(ENTREGRA_VENDOR_ID, ENTREGRA_FAKE_ID) },
+        { }                                             
+};
+#endif
 
 static void keyspan_pda_wakeup_write( struct usb_serial_port *port )
 {
@@ -709,12 +763,25 @@ static void keyspan_pda_close(struct usb_serial_port *port, struct file *filp)
 static int keyspan_pda_fake_startup (struct usb_serial *serial)
 {
        int response;
-       const struct ezusb_hex_record *record;
+       const struct ezusb_hex_record *record = NULL;
 
        /* download the firmware here ... */
        response = ezusb_set_reset(serial, 1);
 
-       record = &keyspan_pda_firmware[0];
+#ifdef KEYSPAN
+       if (serial->dev->descriptor.idVendor == KEYSPAN_VENDOR_ID)
+               record = &keyspan_pda_firmware[0];
+#endif
+#ifdef XIRCOM
+       if ((serial->dev->descriptor.idVendor == XIRCOM_VENDOR_ID) ||
+           (serial->dev->descriptor.idVendor == ENTREGRA_VENDOR_ID))
+               record = &xircom_pgs_firmware[0];
+#endif
+       if (record == NULL) {
+               err(__FUNCTION__": unknown vendor, aborting.");
+               return -ENODEV;
+       }
+
        while(record->address != 0xffff) {
                response = ezusb_writememory(serial, record->address,
                                             (unsigned char *)record->data,
@@ -770,7 +837,8 @@ static void keyspan_pda_shutdown (struct usb_serial *serial)
        kfree(serial->port[0].private);
 }
 
-struct usb_serial_device_type keyspan_pda_fake_device = {
+#ifdef KEYSPAN
+static struct usb_serial_device_type keyspan_pda_fake_device = {
        name:                   "Keyspan PDA - (prerenumeration)",
        id_table:               id_table_fake,
        needs_interrupt_in:     DONT_CARE,
@@ -782,8 +850,37 @@ struct usb_serial_device_type keyspan_pda_fake_device = {
        num_ports:              1,
        startup:                keyspan_pda_fake_startup,
 };
+#endif
+
+#ifdef XIRCOM
+static struct usb_serial_device_type xircom_pgs_fake_device = {
+        name:                   "Xircom PGS - (prerenumeration)",
+        id_table:               id_table_fake_xircom,
+        needs_interrupt_in:     DONT_CARE,
+        needs_bulk_in:          DONT_CARE,
+        needs_bulk_out:         DONT_CARE,
+        num_interrupt_in:       NUM_DONT_CARE,
+        num_bulk_in:            NUM_DONT_CARE,
+        num_bulk_out:           NUM_DONT_CARE,
+        num_ports:              1,
+        startup:                keyspan_pda_fake_startup,
+};
 
-struct usb_serial_device_type keyspan_pda_device = {
+static struct usb_serial_device_type entregra_pgs_fake_device = {
+        name:                   "Entregra PGS - (prerenumeration)",
+        id_table:               id_table_fake_entregra,
+        needs_interrupt_in:     DONT_CARE,
+        needs_bulk_in:          DONT_CARE,
+        needs_bulk_out:         DONT_CARE,
+        num_interrupt_in:       NUM_DONT_CARE,
+        num_bulk_in:            NUM_DONT_CARE,
+        num_bulk_out:           NUM_DONT_CARE,
+        num_ports:              1,
+        startup:                keyspan_pda_fake_startup,
+};
+#endif
+
+static struct usb_serial_device_type keyspan_pda_device = {
        name:                   "Keyspan PDA",
        id_table:               id_table_std,
        needs_interrupt_in:     MUST_HAVE,
@@ -812,17 +909,29 @@ struct usb_serial_device_type keyspan_pda_device = {
 
 static int __init keyspan_pda_init (void)
 {
-       usb_serial_register (&keyspan_pda_fake_device);
        usb_serial_register (&keyspan_pda_device);
-       info(DRIVER_VERSION ":" DRIVER_DESC);
+#ifdef KEYSPAN
+       usb_serial_register (&keyspan_pda_fake_device);
+#endif
+#ifdef XIRCOM
+       usb_serial_register (&xircom_pgs_fake_device);
+       usb_serial_register (&entregra_pgs_fake_device);
+#endif
+       info(DRIVER_DESC " " DRIVER_VERSION);
        return 0;
 }
 
 
 static void __exit keyspan_pda_exit (void)
 {
-       usb_serial_deregister (&keyspan_pda_fake_device);
        usb_serial_deregister (&keyspan_pda_device);
+#ifdef KEYSPAN
+       usb_serial_deregister (&keyspan_pda_fake_device);
+#endif
+#ifdef XIRCOM
+       usb_serial_deregister (&entregra_pgs_fake_device);
+       usb_serial_deregister (&xircom_pgs_fake_device);
+#endif
 }
 
 
@@ -831,6 +940,7 @@ module_exit(keyspan_pda_exit);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 MODULE_PARM(debug, "i");
 MODULE_PARM_DESC(debug, "Debug enabled or not");
index 80f9b380630c29da9fb4ddb158fe60998bf7f56f..811a6ee3717775c8255ce850d5345bce8270be0c 100644 (file)
@@ -896,6 +896,7 @@ module_exit(mct_u232_exit);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 #ifdef FIX_WRITE_RETURN_CODE_PROBLEM
 MODULE_PARM(write_blocking, "i");
index a113a4afbe90391f1891999620435f1ca5e25c1c..0e006b11ed3dcc841ea7f36e67e18b70941d1002 100644 (file)
@@ -420,6 +420,7 @@ module_exit(omninet_exit);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 MODULE_PARM(debug, "i");
 MODULE_PARM_DESC(debug, "Debug enabled or not");
index 87a32f0b7db830611cd36bffe677d94beee70f21..660f26f792492099b0ff772e1d8a5e53c3f44611 100644 (file)
@@ -567,6 +567,7 @@ module_init(pl2303_init);
 module_exit(pl2303_exit);
 
 MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE("GPL");
 
 MODULE_PARM(debug, "i");
 MODULE_PARM_DESC(debug, "Debug enabled or not");
index f088f9fa4e7a39031d445c99702b6e12dd2b5113..f79b2c0630fe2c1e5d619de9fd8de090f3b599ac 100644 (file)
@@ -153,12 +153,22 @@ extern int  usb_serial_register(struct usb_serial_device_type *new_device);
 extern void usb_serial_deregister(struct usb_serial_device_type *device);
 
 /* determine if we should include the EzUSB loader functions */
-#if defined(CONFIG_USB_SERIAL_KEYSPAN_PDA) || defined(CONFIG_USB_SERIAL_WHITEHEAT) || defined(CONFIG_USB_SERIAL_KEYSPAN) || defined(CONFIG_USB_SERIAL_KEYSPAN_PDA_MODULE) || defined(CONFIG_USB_SERIAL_WHITEHEAT_MODULE) || defined(CONFIG_USB_SERIAL_KEYSPAN_MODULE)
-       #define USES_EZUSB_FUNCTIONS
-       extern int ezusb_writememory (struct usb_serial *serial, int address, unsigned char *data, int length, __u8 bRequest);
-       extern int ezusb_set_reset (struct usb_serial *serial, unsigned char reset_bit);
-#else
-       #undef  USES_EZUSB_FUNCTIONS
+#undef USES_EZUSB_FUNCTIONS
+#if defined(CONFIG_USB_SERIAL_KEYSPAN_PDA) || defined(CONFIG_USB_SERIAL_KEYSPAN_PDA_MODULE)
+       #define USES_EZUSB_FUNCTIONS
+#endif
+#if defined(CONFIG_USB_SERIAL_XIRCOM) || defined(CONFIG_USB_SERIAL_XIRCOM_MODULE)
+       #define USES_EZUSB_FUNCTIONS
+#endif
+#if defined(CONFIG_USB_SERIAL_KEYSPAN) || defined(CONFIG_USB_SERIAL_KEYSPAN_MODULE)
+       #define USES_EZUSB_FUNCTIONS
+#endif
+#if defined(CONFIG_USB_SERIAL_WHITEHEAT) || defined(CONFIG_USB_SERIAL_WHITEHEAT_MODULE)
+       #define USES_EZUSB_FUNCTIONS
+#endif
+#ifdef USES_EZUSB_FUNCTIONS
+extern int ezusb_writememory (struct usb_serial *serial, int address, unsigned char *data, int length, __u8 bRequest);
+extern int ezusb_set_reset (struct usb_serial *serial, unsigned char reset_bit);
 #endif
 
 
index 7b3da7bb16e1d54c30293c9217a33050caa20647..fd727b37ba38bc87f4b9b0b1debea79ecaa4a5a5 100644 (file)
  *
  * See Documentation/usb/usb-serial.txt for more information on using this driver
  * 
+ * (09/13/2001) gkh
+ *     Moved generic driver initialize after we have registered with the USB
+ *     core.  Thanks to Randy Dunlap for pointing this problem out.
+ *
  * (07/03/2001) gkh
  *     Fixed module paramater size.  Thanks to John Brockmeyer for the pointer.
  *     Fixed vendor and product getting defined through the MODULE_PARM macro
@@ -1426,14 +1430,6 @@ int usb_serial_init(void)
                return -1;
        }
 
-#ifdef CONFIG_USB_SERIAL_GENERIC
-       generic_device_ids[0].idVendor = vendor;
-       generic_device_ids[0].idProduct = product;
-       generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
-       /* register our generic driver with ourselves */
-       usb_serial_register (&generic_device);
-#endif
-       
        /* register the USB driver */
        result = usb_register(&usb_serial_driver);
        if (result < 0) {
@@ -1442,6 +1438,14 @@ int usb_serial_init(void)
                return -1;
        }
 
+#ifdef CONFIG_USB_SERIAL_GENERIC
+       generic_device_ids[0].idVendor = vendor;
+       generic_device_ids[0].idProduct = product;
+       generic_device_ids[0].match_flags = USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT;
+       /* register our generic driver with ourselves */
+       usb_serial_register (&generic_device);
+#endif
+
        info(DRIVER_DESC " " DRIVER_VERSION);
 
        return 0;
@@ -1512,6 +1516,7 @@ EXPORT_SYMBOL(usb_serial_deregister);
 /* Module information */
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 MODULE_PARM(debug, "i");
 MODULE_PARM_DESC(debug, "Debug enabled or not");
@@ -1523,4 +1528,3 @@ MODULE_PARM_DESC(vendor, "User specified USB idVendor");
 MODULE_PARM(product, "h");
 MODULE_PARM_DESC(product, "User specified USB idProduct");
 #endif
-
index 86c6ab4591985526a91895f18ded137795041489..6ddb0dff64a137d4f2c0fe80bec8e5b26d77cead 100644 (file)
@@ -877,6 +877,7 @@ module_exit(visor_exit);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 MODULE_PARM(debug, "i");
 MODULE_PARM_DESC(debug, "Debug enabled or not");
index 75d1dcda17370690c5b5375f0bde323f342b4e7b..2456e42eba1a43971f12509ce8809edf35e6cf35 100644 (file)
@@ -677,6 +677,7 @@ module_exit(whiteheat_exit);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 MODULE_PARM(debug, "i");
 MODULE_PARM_DESC(debug, "Debug enabled or not");
diff --git a/drivers/usb/serial/xircom_pgs.S b/drivers/usb/serial/xircom_pgs.S
new file mode 100644 (file)
index 0000000..dd7c457
--- /dev/null
@@ -0,0 +1,1192 @@
+/*  $Id: loop.s,v 1.23 2000/03/20 09:49:06 warner Exp $
+ * 
+ *  Firmware for the Keyspan PDA Serial Adapter, a USB serial port based on
+ *  the EzUSB microcontroller.
+ * 
+ *  (C) Copyright 2000 Brian Warner <warner@lothar.com>
+ * 
+ *     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.
+ * 
+ *  "Keyspan PDA Serial Adapter" is probably a copyright of Keyspan, the
+ *  company.
+ * 
+ *  This serial adapter is basically an EzUSB chip and an RS-232 line driver
+ *  in a little widget that has a DB-9 on one end and a USB plug on the other.
+ *  It uses the EzUSB's internal UART0 (using the pins from Port C) and timer2
+ *  as a baud-rate generator. The wiring is:
+ *   PC0/RxD0 <- rxd (DB9 pin 2)         PC4 <- dsr pin 6
+ *   PC1/TxD0 -> txd pin 3               PC5 <- ri  pin 9
+ *   PC2      -> rts pin 7               PC6 <- dcd pin 1
+ *   PC3      <- cts pin 8               PC7 -> dtr pin 4
+ *   PB1 -> line driver standby
+ *
+ *  The EzUSB register constants below come from their excellent documentation
+ *  and sample code (which used to be available at www.anchorchips.com, but
+ *  that has now been absorbed into Cypress' site and the CD-ROM contents
+ *  don't appear to be available online anymore). If we get multiple
+ *  EzUSB-based drivers into the kernel, it might be useful to pull them out
+ *  into a separate .h file.
+ * 
+ * THEORY OF OPERATION:
+ *
+ *   There are two 256-byte ring buffers, one for tx, one for rx.
+ *
+ *   EP2out is pure tx data. When it appears, the data is copied into the tx
+ *   ring and serial transmission is started if it wasn't already running. The
+ *   "tx buffer empty" interrupt may kick off another character if the ring
+ *   still has data. If the host is tx-blocked because the ring filled up,
+ *   it will request a "tx unthrottle" interrupt. If sending a serial character
+ *   empties the ring below the desired threshold, we set a bit that will send
+ *   up the tx unthrottle message as soon as the rx buffer becomes free.
+ *
+ *   EP2in (interrupt) is used to send both rx chars and rx status messages
+ *   (only "tx unthrottle" at this time) back up to the host. The first byte
+ *   of the rx message indicates data (0) or status msg (1). Status messages
+ *   are sent before any data.
+ *
+ *   Incoming serial characters are put into the rx ring by the serial
+ *   interrupt, and the EP2in buffer sent if it wasn't already in transit.
+ *   When the EP2in buffer returns, the interrupt prompts us to send more
+ *   rx chars (or status messages) if they are pending.
+ *
+ *   Device control happens through "vendor specific" control messages on EP0.
+ *   All messages are destined for the "Interface" (with the index always 0,
+ *   so that if their two-port device might someday use similar firmware, we
+ *   can use index=1 to refer to the second port). The messages defined are:
+ *
+ *    bRequest = 0 : set baud/bits/parity
+ *               1 : unused
+ *               2 : reserved for setting HW flow control (CTSRTS)
+ *               3 : get/set "modem info" (pin states: DTR, RTS, DCD, RI, etc)
+ *               4 : set break (on/off)
+ *               5 : reserved for requesting interrupts on pin state change
+ *               6 : query buffer room or chars in tx buffer
+ *               7 : request tx unthrottle interrupt
+ *
+ *  The host-side driver is set to recognize the device ID values stashed in
+ *  serial EEPROM (0x06cd, 0x0103), program this firmware into place, then
+ *  start it running. This firmware will use EzUSB's "renumeration" trick by
+ *  simulating a bus disconnect, then reconnect with a different device ID
+ *  (encoded in the desc_device descriptor below). The host driver then
+ *  recognizes the new device ID and glues it to the real serial driver code.
+ *
+ * USEFUL DOCS:
+ *  EzUSB Technical Reference Manual: <http://www.anchorchips.com>
+ *  8051 manuals: everywhere, but try www.dalsemi.com because the EzUSB is
+ *   basically the Dallas enhanced 8051 code. Remember that the EzUSB IO ports
+ *   use totally different registers!
+ *  USB 1.1 spec: www.usb.org
+ *
+ * HOW TO BUILD:
+ *  gcc -x assembler-with-cpp -P -E -o keyspan_pda.asm keyspan_pda.s
+ *  as31 -l keyspan_pda.asm
+ *  mv keyspan_pda.obj keyspan_pda.hex
+ *  perl ezusb_convert.pl keyspan_pda < keyspan_pda.hex > keyspan_pda_fw.h
+ * Get as31 from <http://www.pjrc.com/tech/8051/index.html>, and hack on it
+ * a bit to make it build.
+ *
+ * THANKS:
+ *  Greg Kroah-Hartman, for coordinating the whole usb-serial thing.
+ *  AnchorChips, for making such an incredibly useful little microcontroller.
+ *  KeySpan, for making a handy, cheap ($40) widget that was so easy to take
+ *           apart and trace with an ohmmeter.
+ *
+ * TODO:
+ *  lots. grep for TODO. Interrupt safety needs stress-testing. Better flow
+ *  control. Interrupting host upon change in DCD, etc, counting transitions.
+ *  Need to find a safe device id to use (the one used by the Keyspan firmware
+ *  under Windows would be ideal.. can anyone figure out what it is?). Parity.
+ *  More baud rates. Oh, and the string-descriptor-length silicon bug
+ *  workaround should be implemented, but I'm lazy, and the consequence is
+ *  that the device name strings that show up in your kernel log will have
+ *  lots of trailing binary garbage in them (appears as ????). Device strings
+ *  should be made more accurate.
+ *
+ * Questions, bugs, patches to Brian.
+ *
+ *  -Brian Warner <warner@lothar.com>
+ *
+ */
+       
+#define HIGH(x) (((x) & 0xff00) / 256)
+#define LOW(x) ((x) & 0xff)
+
+#define dpl1 0x84
+#define dph1 0x85
+#define dps 0x86
+
+;;; our bit assignments
+#define TX_RUNNING 0
+#define DO_TX_UNTHROTTLE 1
+       
+       ;; stack from 0x60 to 0x7f: should really set SP to 0x60-1, not 0x60
+#define STACK #0x60-1
+
+#define EXIF 0x91
+#define EIE 0xe8
+       .flag EUSB, EIE.0
+       .flag ES0, IE.4
+
+#define EP0CS #0x7fb4
+#define EP0STALLbit #0x01
+#define IN0BUF #0x7f00
+#define IN0BC #0x7fb5
+#define OUT0BUF #0x7ec0
+#define OUT0BC #0x7fc5         
+#define IN2BUF #0x7e00
+#define IN2BC #0x7fb9
+#define IN2CS #0x7fb8
+#define OUT2BC #0x7fc9
+#define OUT2CS #0x7fc8
+#define OUT2BUF #0x7dc0
+#define IN4BUF #0x7d00
+#define IN4BC #0x7fbd
+#define IN4CS #0x7fbc
+#define OEB #0x7f9d
+#define OUTB #0x7f97
+#define OEC #0x7f9e
+#define OUTC #0x7f98
+#define PINSC #0x7f9b
+#define PORTBCFG #0x7f94
+#define PORTCCFG #0x7f95
+#define OEA    #0x7f9c
+#define IN07IRQ #0x7fa9
+#define OUT07IRQ #0x7faa
+#define IN07IEN #0x7fac
+#define OUT07IEN #0x7fad
+#define USBIRQ #0x7fab
+#define USBIEN #0x7fae
+#define USBBAV #0x7faf
+#define USBCS #0x7fd6
+#define SUDPTRH #0x7fd4
+#define SUDPTRL #0x7fd5
+#define SETUPDAT #0x7fe8
+               
+       ;; usb interrupt : enable is EIE.0 (0xe8), flag is EXIF.4 (0x91)
+
+       .org 0
+       ljmp start
+       ;; interrupt vectors
+       .org 23H
+       ljmp serial_int
+       .byte 0
+       
+       .org 43H
+       ljmp USB_Jump_Table
+       .byte 0                 ; filled in by the USB core
+
+;;; local variables. These are not initialized properly: do it by hand.
+       .org 30H
+rx_ring_in:    .byte 0
+rx_ring_out:   .byte 0
+tx_ring_in:    .byte 0
+tx_ring_out:   .byte 0
+tx_unthrottle_threshold:       .byte 0
+               
+       .org 0x100H             ; wants to be on a page boundary
+USB_Jump_Table:
+       ljmp    ISR_Sudav       ; Setup Data Available
+       .byte 0
+       ljmp    0               ; Start of Frame
+       .byte 0
+       ljmp    0               ; Setup Data Loading
+       .byte 0
+       ljmp    0               ; Global Suspend
+       .byte   0
+       ljmp    0               ; USB Reset     
+       .byte   0
+       ljmp    0               ; Reserved
+       .byte   0
+       ljmp    0               ; End Point 0 In
+       .byte   0
+       ljmp    0               ; End Point 0 Out
+       .byte   0
+       ljmp    0               ; End Point 1 In
+       .byte   0
+       ljmp    0               ; End Point 1 Out
+       .byte   0
+       ljmp    ISR_Ep2in
+       .byte   0
+       ljmp    ISR_Ep2out
+       .byte   0
+
+
+       .org 0x200
+               
+start: mov SP,STACK-1 ; set stack
+       ;; clear local variables
+       clr a
+       mov tx_ring_in, a
+       mov tx_ring_out, a
+       mov rx_ring_in, a
+       mov rx_ring_out, a
+       mov tx_unthrottle_threshold, a
+       clr TX_RUNNING
+       clr DO_TX_UNTHROTTLE
+       
+       ;; clear fifo with "fe"
+       mov r1, 0
+       mov a, #0xfe
+       mov dptr, #tx_ring
+clear_tx_ring_loop:
+       movx @dptr, a
+       inc dptr
+       djnz r1, clear_tx_ring_loop
+
+       mov a, #0xfd
+       mov dptr, #rx_ring
+clear_rx_ring_loop:
+       movx @dptr, a
+       inc dptr
+       djnz r1, clear_rx_ring_loop
+
+;;; turn on the RS-232 driver chip (bring the STANDBY pin low)
+;;; on Xircom the STANDBY is wired to PB6 and PC4 
+       mov dptr, PORTBCFG
+        mov a, #0xBf
+        movx @dptr, a
+       mov dptr, PORTCCFG
+        mov a, #0xef
+        movx @dptr, a
+       
+       ;; set OEC.4
+        mov a, #0x10
+        mov dptr,OEC
+        movx @dptr,a
+
+        ;; clear PC4
+        mov a, #0x00
+        mov dptr,OUTC
+        movx @dptr,a
+
+       ;; set OEB.6
+       mov a, #0x40
+       mov dptr,OEB
+       movx @dptr,a
+
+       ;; clear PB6
+       mov a, #0x00
+       mov dptr,OUTB
+       movx @dptr,a
+
+       ;; set OEC.[17]
+       mov a, #0x82
+       mov dptr,OEC
+       movx @dptr,a
+
+
+       ;; set PORTCCFG.[01] to route TxD0,RxD0 to serial port
+       mov dptr, PORTCCFG
+       mov a, #0x03
+       movx @dptr, a
+       
+       ;; set up interrupts, autovectoring
+       ;; set BKPT
+       mov dptr, USBBAV
+       movx a,@dptr
+       setb acc.0              ; AVEN bit to 0
+       movx @dptr, a
+
+       mov a,#0x01             ; enable SUDAV: setup data available (for ep0)
+       mov dptr, USBIRQ
+       movx @dptr, a           ; clear SUDAVI
+       mov dptr, USBIEN
+       movx @dptr, a
+       
+       mov dptr, IN07IEN
+       mov a,#0x04             ; enable IN2 int
+       movx @dptr, a
+       
+       mov dptr, OUT07IEN
+       mov a,#0x04             ; enable OUT2 int
+       movx @dptr, a
+       mov dptr, OUT2BC
+       movx @dptr, a           ; arm OUT2
+
+;;     mov a, #0x84            ; turn on RTS, DTR
+;;     mov dptr,OUTC
+;;     movx @dptr, a
+
+       mov a, #0x7             ; turn on  DTR
+        mov dptr,USBBAV
+        movx @dptr, a
+
+       mov a, #0x20             ; turn on the RED led 
+        mov dptr,OEA
+        movx @dptr, a
+
+       mov a, #0x80            ; turn on  RTS
+        mov dptr,OUTC
+        movx @dptr, a
+
+       ;; setup the serial port. 9600 8N1.
+       mov a,#0x53             ; mode 1, enable rx, clear int
+       mov SCON, a
+       ;;  using timer2, in 16-bit baud-rate-generator mode
+       ;;   (xtal 12MHz, internal fosc 24MHz)
+       ;;  RCAP2H,RCAP2L = 65536 - fosc/(32*baud)
+       ;;  57600: 0xFFF2.F, say 0xFFF3
+       ;;   9600: 0xFFB1.E, say 0xFFB2
+       ;;    300: 0xF63C
+#define BAUD 9600
+#define BAUD_TIMEOUT(rate) (65536 - (24 * 1000 * 1000) / (32 * rate))
+#define BAUD_HIGH(rate) HIGH(BAUD_TIMEOUT(rate))
+#define BAUD_LOW(rate) LOW(BAUD_TIMEOUT(rate))
+               
+       mov T2CON, #030h        ; rclk=1,tclk=1,cp=0,tr2=0(enable later)
+       mov r3, #5
+       acall set_baud
+       setb TR2
+       mov SCON, #050h
+       
+#if 0
+       mov r1, #0x40
+       mov a, #0x41
+send:  
+       mov SBUF, a
+       inc a
+       anl a, #0x3F
+       orl a, #0x40
+;      xrl a, #0x02
+wait1: 
+       jnb TI, wait1
+       clr TI
+       djnz r1, send
+;done: sjmp done
+
+#endif
+       
+       setb EUSB
+       setb EA
+       setb ES0
+       ;acall dump_stat
+
+       ;; hey, what say we RENUMERATE! (TRM p.62)
+       mov a, #0
+       mov dps, a
+       mov dptr, USBCS
+       mov a, #0x02            ; DISCON=0, DISCOE=0, RENUM=1
+       movx @dptr, a
+       ;; now presence pin is floating, simulating disconnect. wait 0.5s
+       mov r1, #46
+renum_wait1:
+       mov r2, #0
+renum_wait2:
+       mov r3, #0
+renum_wait3:
+       djnz r3, renum_wait3
+       djnz r2, renum_wait2
+       djnz r1, renum_wait1    ; wait about n*(256^2) 6MHz clocks
+       mov a, #0x06            ; DISCON=0, DISCOE=1, RENUM=1
+       movx @dptr, a
+       ;; we are back online. the host device will now re-query us
+       
+       
+main:  sjmp main
+
+       
+
+ISR_Sudav:
+       push dps
+       push dpl
+       push dph
+       push dpl1
+       push dph1
+       push acc
+       mov a,EXIF
+       clr acc.4
+       mov EXIF,a              ; clear INT2 first
+       mov dptr, USBIRQ        ; clear USB int
+       mov a,#01h
+       movx @dptr,a
+
+       ;; get request type
+       mov dptr, SETUPDAT
+       movx a, @dptr
+       mov r1, a               ; r1 = bmRequestType
+       inc dptr
+       movx a, @dptr
+       mov r2, a               ; r2 = bRequest
+       inc dptr
+       movx a, @dptr
+       mov r3, a               ; r3 = wValueL
+       inc dptr
+       movx a, @dptr
+       mov r4, a               ; r4 = wValueH
+
+       ;; main switch on bmRequest.type: standard or vendor
+       mov a, r1
+       anl a, #0x60
+       cjne a, #0x00, setup_bmreq_type_not_standard
+       ;; standard request: now main switch is on bRequest
+       ljmp setup_bmreq_is_standard
+       
+setup_bmreq_type_not_standard: 
+       ;; a still has bmreq&0x60
+       cjne a, #0x40, setup_bmreq_type_not_vendor
+       ;; Anchor reserves bRequest 0xa0-0xaf, we use small ones
+       ;; switch on bRequest. bmRequest will always be 0x41 or 0xc1
+       cjne r2, #0x00, setup_ctrl_not_00
+       ;; 00 is set baud, wValue[0] has baud rate index
+       lcall set_baud          ; index in r3, carry set if error
+       jc setup_bmreq_type_not_standard__do_stall
+       ljmp setup_done_ack
+setup_bmreq_type_not_standard__do_stall:
+       ljmp setup_stall
+setup_ctrl_not_00:
+       cjne r2, #0x01, setup_ctrl_not_01
+       ;; 01 is reserved for set bits (parity). TODO
+       ljmp setup_stall
+setup_ctrl_not_01:
+       cjne r2, #0x02, setup_ctrl_not_02
+       ;; 02 is set HW flow control. TODO
+       ljmp setup_stall
+setup_ctrl_not_02:
+       cjne r2, #0x03, setup_ctrl_not_03
+       ;; 03 is control pins (RTS, DTR).
+       ljmp control_pins       ; will jump to setup_done_ack,
+                               ;  or setup_return_one_byte
+setup_ctrl_not_03:
+       cjne r2, #0x04, setup_ctrl_not_04
+       ;; 04 is send break (really "turn break on/off"). TODO
+       cjne r3, #0x00, setup_ctrl_do_break_on
+       ;; do break off: restore PORTCCFG.1 to reconnect TxD0 to serial port
+       mov dptr, PORTCCFG
+       movx a, @dptr
+       orl a, #0x02
+       movx @dptr, a
+       ljmp setup_done_ack
+setup_ctrl_do_break_on:
+       ;; do break on: clear PORTCCFG.0, set TxD high(?) (b1 low)
+       mov dptr, OUTC
+       movx a, @dptr
+       anl a, #0xfd            ; ~0x02
+       movx @dptr, a
+       mov dptr, PORTCCFG
+       movx a, @dptr
+       anl a, #0xfd            ; ~0x02
+       movx @dptr, a
+       ljmp setup_done_ack
+setup_ctrl_not_04:
+       cjne r2, #0x05, setup_ctrl_not_05
+       ;; 05 is set desired interrupt bitmap. TODO
+       ljmp setup_stall
+setup_ctrl_not_05:
+       cjne r2, #0x06, setup_ctrl_not_06
+       ;; 06 is query room
+       cjne r3, #0x00, setup_ctrl_06_not_00
+       ;; 06, wValue[0]=0 is query write_room
+       mov a, tx_ring_out
+       setb c
+       subb a, tx_ring_in      ; out-1-in = 255 - (in-out)
+       ljmp setup_return_one_byte
+setup_ctrl_06_not_00:
+       cjne r3, #0x01, setup_ctrl_06_not_01
+       ;; 06, wValue[0]=1 is query chars_in_buffer
+       mov a, tx_ring_in
+       clr c
+       subb a, tx_ring_out     ; in-out
+       ljmp setup_return_one_byte
+setup_ctrl_06_not_01:  
+       ljmp setup_stall
+setup_ctrl_not_06:
+       cjne r2, #0x07, setup_ctrl_not_07
+       ;; 07 is request tx unthrottle interrupt
+       mov tx_unthrottle_threshold, r3; wValue[0] is threshold value
+       ljmp setup_done_ack
+setup_ctrl_not_07:
+       ljmp setup_stall
+       
+setup_bmreq_type_not_vendor:
+       ljmp setup_stall
+
+
+setup_bmreq_is_standard:       
+       cjne r2, #0x00, setup_breq_not_00
+       ;; 00:  Get_Status (sub-switch on bmRequestType: device, ep, int)
+       cjne r1, #0x80, setup_Get_Status_not_device
+       ;; Get_Status(device)
+       ;;  are we self-powered? no. can we do remote wakeup? no
+       ;;   so return two zero bytes. This is reusable
+setup_return_two_zero_bytes:
+       mov dptr, IN0BUF
+       clr a
+       movx @dptr, a
+       inc dptr
+       movx @dptr, a
+       mov dptr, IN0BC
+       mov a, #2
+       movx @dptr, a
+       ljmp setup_done_ack
+setup_Get_Status_not_device:
+       cjne r1, #0x82, setup_Get_Status_not_endpoint
+       ;; Get_Status(endpoint)
+       ;;  must get stall bit for ep[wIndexL], return two bytes, bit in lsb 0
+       ;; for now: cheat. TODO
+       sjmp setup_return_two_zero_bytes
+setup_Get_Status_not_endpoint:
+       cjne r1, #0x81, setup_Get_Status_not_interface
+       ;; Get_Status(interface): return two zeros
+       sjmp setup_return_two_zero_bytes
+setup_Get_Status_not_interface:        
+       ljmp setup_stall
+       
+setup_breq_not_00:
+       cjne r2, #0x01, setup_breq_not_01
+       ;; 01:  Clear_Feature (sub-switch on wValueL: stall, remote wakeup)
+       cjne r3, #0x00, setup_Clear_Feature_not_stall
+       ;; Clear_Feature(stall). should clear a stall bit. TODO
+       ljmp setup_stall
+setup_Clear_Feature_not_stall:
+       cjne r3, #0x01, setup_Clear_Feature_not_rwake
+       ;; Clear_Feature(remote wakeup). ignored.
+       ljmp setup_done_ack
+setup_Clear_Feature_not_rwake:
+       ljmp setup_stall
+       
+setup_breq_not_01:
+       cjne r2, #0x03, setup_breq_not_03
+       ;; 03:  Set_Feature (sub-switch on wValueL: stall, remote wakeup)
+       cjne r3, #0x00, setup_Set_Feature_not_stall
+       ;; Set_Feature(stall). Should set a stall bit. TODO
+       ljmp setup_stall
+setup_Set_Feature_not_stall:
+       cjne r3, #0x01, setup_Set_Feature_not_rwake
+       ;; Set_Feature(remote wakeup). ignored.
+       ljmp setup_done_ack
+setup_Set_Feature_not_rwake:
+       ljmp setup_stall
+       
+setup_breq_not_03:     
+       cjne r2, #0x06, setup_breq_not_06
+       ;; 06:  Get_Descriptor (s-switch on wValueH: dev, config[n], string[n])
+       cjne r4, #0x01, setup_Get_Descriptor_not_device
+       ;; Get_Descriptor(device)
+       mov dptr, SUDPTRH
+       mov a, #HIGH(desc_device)
+       movx @dptr, a
+       mov dptr, SUDPTRL
+       mov a, #LOW(desc_device)
+       movx @dptr, a
+       ljmp setup_done_ack
+setup_Get_Descriptor_not_device:
+       cjne r4, #0x02, setup_Get_Descriptor_not_config
+       ;; Get_Descriptor(config[n])
+       cjne r3, #0x00, setup_stall; only handle n==0
+       ;; Get_Descriptor(config[0])
+       mov dptr, SUDPTRH
+       mov a, #HIGH(desc_config1)
+       movx @dptr, a
+       mov dptr, SUDPTRL
+       mov a, #LOW(desc_config1)
+       movx @dptr, a
+       ljmp setup_done_ack
+setup_Get_Descriptor_not_config:
+       cjne r4, #0x03, setup_Get_Descriptor_not_string
+       ;; Get_Descriptor(string[wValueL])
+       ;;  if (wValueL >= maxstrings) stall
+       mov a, #((desc_strings_end-desc_strings)/2)
+       clr c
+       subb a,r3               ; a=4, r3 = 0..3 . if a<=0 then stall
+       jc  setup_stall
+       jz  setup_stall
+       mov a, r3
+       add a, r3               ; a = 2*wValueL
+       mov dptr, #desc_strings
+       add a, dpl
+       mov dpl, a
+       mov a, #0
+       addc a, dph
+       mov dph, a              ; dph = desc_strings[a]. big endian! (handy)
+       ;; it looks like my adapter uses a revision of the EZUSB that
+       ;; contains "rev D errata number 8", as hinted in the EzUSB example
+       ;; code. I cannot find an actual errata description on the Cypress
+       ;; web site, but from the example code it looks like this bug causes
+       ;; the length of string descriptors to be read incorrectly, possibly
+       ;; sending back more characters than the descriptor has. The workaround
+       ;; is to manually send out all of the data. The consequence of not
+       ;; using the workaround is that the strings gathered by the kernel
+       ;; driver are too long and are filled with trailing garbage (including
+       ;; leftover strings). Writing this out by hand is a nuisance, so for
+       ;; now I will just live with the bug.
+       movx a, @dptr
+       mov r1, a
+       inc dptr
+       movx a, @dptr
+       mov r2, a
+       mov dptr, SUDPTRH
+       mov a, r1
+       movx @dptr, a
+       mov dptr, SUDPTRL
+       mov a, r2
+       movx @dptr, a
+       ;; done
+       ljmp setup_done_ack
+       
+setup_Get_Descriptor_not_string:
+       ljmp setup_stall
+       
+setup_breq_not_06:
+       cjne r2, #0x08, setup_breq_not_08
+       ;; Get_Configuration. always 1. return one byte.
+       ;; this is reusable
+       mov a, #1
+setup_return_one_byte: 
+       mov dptr, IN0BUF
+       movx @dptr, a
+       mov a, #1
+       mov dptr, IN0BC
+       movx @dptr, a
+       ljmp setup_done_ack
+setup_breq_not_08:
+       cjne r2, #0x09, setup_breq_not_09
+       ;; 09: Set_Configuration. ignored.
+       ljmp setup_done_ack
+setup_breq_not_09:
+       cjne r2, #0x0a, setup_breq_not_0a
+       ;; 0a: Get_Interface. get the current altsetting for int[wIndexL]
+       ;;  since we only have one interface, ignore wIndexL, return a 0
+       mov a, #0
+       ljmp setup_return_one_byte
+setup_breq_not_0a:
+       cjne r2, #0x0b, setup_breq_not_0b
+       ;; 0b: Set_Interface. set altsetting for interface[wIndexL]. ignored
+       ljmp setup_done_ack
+setup_breq_not_0b:
+       ljmp setup_stall
+
+               
+setup_done_ack:        
+       ;; now clear HSNAK
+       mov dptr, EP0CS
+       mov a, #0x02
+       movx @dptr, a
+       sjmp setup_done
+setup_stall:   
+       ;; unhandled. STALL
+       ;EP0CS |= bmEPSTALL
+       mov dptr, EP0CS
+       movx a, @dptr
+       orl a, EP0STALLbit
+       movx @dptr, a
+       sjmp setup_done
+       
+setup_done:    
+       pop acc
+       pop dph1
+       pop dpl1
+       pop dph
+       pop dpl
+       pop dps
+       reti
+
+;;; ==============================================================
+       
+set_baud:                      ; baud index in r3
+       ;; verify a < 10
+       mov a, r3
+       jb ACC.7, set_baud__badbaud
+       clr c
+       subb a, #10
+       jnc set_baud__badbaud
+       mov a, r3
+       rl a                    ; a = index*2
+       add a, #LOW(baud_table)
+       mov dpl, a
+       mov a, #HIGH(baud_table)
+       addc a, #0
+       mov dph, a
+       ;; TODO: shut down xmit/receive
+       ;; TODO: wait for current xmit char to leave
+       ;; TODO: shut down timer to avoid partial-char glitch
+       movx a,@dptr            ; BAUD_HIGH
+       mov RCAP2H, a
+       mov TH2, a
+       inc dptr
+       movx a,@dptr            ; BAUD_LOW
+       mov RCAP2L, a
+       mov TL2, a
+       ;; TODO: restart xmit/receive
+       ;; TODO: reenable interrupts, resume tx if pending
+       clr c                   ; c=0: success
+       ret
+set_baud__badbaud:
+       setb c                  ; c=1: failure
+       ret
+       
+;;; ==================================================
+control_pins:
+       cjne r1, #0x41, control_pins_in
+control_pins_out:
+               ;TODO BKPT is DTR
+       mov a, r3 ; wValue[0] holds new bits:   b7 is new RTS
+       xrl a, #0xff            ; 1 means active, 0V, +12V ?
+       anl a, #0x80
+       mov r3, a
+       mov dptr, OUTC
+       movx a, @dptr           ; only change bit 7 
+       anl a, #0x7F            ; ~0x84
+       orl a, r3
+       movx @dptr, a           ; other pins are inputs, bits ignored
+       ljmp setup_done_ack
+control_pins_in:
+       mov dptr, PINSC
+       movx a, @dptr
+       xrl a, #0xff
+       ljmp setup_return_one_byte
+
+;;; ========================================
+       
+ISR_Ep2in:
+       push dps
+       push dpl
+       push dph
+       push dpl1
+       push dph1
+       push acc
+       mov a,EXIF
+       clr acc.4
+       mov EXIF,a              ; clear INT2 first
+       mov dptr, IN07IRQ       ; clear USB int
+       mov a,#04h
+       movx @dptr,a
+
+       mov a, #0x20             ; Turn off the green LED
+        mov dptr,OEA
+        movx @dptr, a
+
+
+       ;; do stuff
+       lcall start_in
+
+       mov a, #0x20             ; Turn off the green LED
+        mov dptr,OEA
+        movx @dptr, a
+
+
+       
+       pop acc
+       pop dph1
+       pop dpl1
+       pop dph
+       pop dpl
+       pop dps
+       reti
+
+ISR_Ep2out:
+       push dps
+       push dpl
+       push dph
+       push dpl1
+       push dph1
+       push acc
+
+        mov a, #0x10             ; Turn the green LED
+        mov dptr,OEA
+        movx @dptr, a
+
+
+
+       mov a,EXIF
+       clr acc.4
+       mov EXIF,a              ; clear INT2 first
+       mov dptr, OUT07IRQ      ; clear USB int
+       mov a,#04h
+       movx @dptr,a
+
+       ;; do stuff
+
+       ;; copy data into buffer. for now, assume we will have enough space
+       mov dptr, OUT2BC        ; get byte count
+       movx a,@dptr
+       mov r1, a
+       clr a
+       mov dps, a
+       mov dptr, OUT2BUF       ; load DPTR0 with source
+       mov dph1, #HIGH(tx_ring)        ; load DPTR1 with target
+       mov dpl1, tx_ring_in
+OUT_loop:
+       movx a,@dptr            ; read
+       inc dps                 ; switch to DPTR1: target
+       inc dpl1                ; target = tx_ring_in+1
+       movx @dptr,a            ; store
+       mov a,dpl1
+       cjne a, tx_ring_out, OUT_no_overflow
+       sjmp OUT_overflow
+OUT_no_overflow:       
+       inc tx_ring_in          ; tx_ring_in++
+       inc dps                 ; switch to DPTR0: source
+       inc dptr
+       djnz r1, OUT_loop
+       sjmp OUT_done
+OUT_overflow:
+       ;; signal overflow
+       ;; fall through
+OUT_done:      
+       ;; ack
+       mov dptr,OUT2BC
+       movx @dptr,a
+
+       ;; start tx
+       acall maybe_start_tx
+       ;acall dump_stat
+
+        mov a, #0x20             ; Turn off the green LED
+        mov dptr,OEA
+        movx @dptr, a
+       
+       pop acc
+       pop dph1
+       pop dpl1
+       pop dph
+       pop dpl
+       pop dps
+       reti
+
+dump_stat:
+       ;; fill in EP4in with a debugging message:
+       ;;   tx_ring_in, tx_ring_out, rx_ring_in, rx_ring_out
+       ;;   tx_active
+       ;;   tx_ring[0..15]
+       ;;   0xfc
+       ;;   rx_ring[0..15]
+       clr a
+       mov dps, a
+       
+       mov dptr, IN4CS
+       movx a, @dptr
+       jb acc.1, dump_stat__done; busy: cannot dump, old one still pending
+       mov dptr, IN4BUF
+       
+       mov a, tx_ring_in
+       movx @dptr, a
+       inc dptr
+       mov a, tx_ring_out
+       movx @dptr, a
+       inc dptr
+
+       mov a, rx_ring_in
+       movx @dptr, a
+       inc dptr
+       mov a, rx_ring_out
+       movx @dptr, a
+       inc dptr
+       
+       clr a
+       jnb TX_RUNNING, dump_stat__no_tx_running
+       inc a
+dump_stat__no_tx_running:
+       movx @dptr, a
+       inc dptr
+       ;; tx_ring[0..15]
+       inc dps
+       mov dptr, #tx_ring      ; DPTR1: source
+       mov r1, #16
+dump_stat__tx_ring_loop:
+       movx a, @dptr
+       inc dptr
+       inc dps
+       movx @dptr, a
+       inc dptr
+       inc dps
+       djnz r1, dump_stat__tx_ring_loop
+       inc dps
+       
+       mov a, #0xfc
+       movx @dptr, a
+       inc dptr
+       
+       ;; rx_ring[0..15]
+       inc dps
+       mov dptr, #rx_ring      ; DPTR1: source
+       mov r1, #16
+dump_stat__rx_ring_loop:
+       movx a, @dptr
+       inc dptr
+       inc dps
+       movx @dptr, a
+       inc dptr
+       inc dps
+       djnz r1, dump_stat__rx_ring_loop
+       
+       ;; now send it
+       clr a
+       mov dps, a
+       mov dptr, IN4BC
+       mov a, #38
+       movx @dptr, a
+dump_stat__done:       
+       ret
+               
+;;; ============================================================
+       
+maybe_start_tx:
+       ;; make sure the tx process is running.
+       jb TX_RUNNING, start_tx_done
+start_tx:
+       ;; is there work to be done?
+       mov a, tx_ring_in
+       cjne a,tx_ring_out, start_tx__work
+       ret                     ; no work
+start_tx__work:        
+       ;; tx was not running. send the first character, setup the TI int
+       inc tx_ring_out         ; [++tx_ring_out]
+       mov dph, #HIGH(tx_ring)
+       mov dpl, tx_ring_out
+       movx a, @dptr
+       mov sbuf, a
+       setb TX_RUNNING
+start_tx_done:
+       ;; can we unthrottle the host tx process?
+       ;;  step 1: do we care?
+       mov a, #0
+       cjne a, tx_unthrottle_threshold, start_tx__maybe_unthrottle_tx
+       ;; nope
+start_tx_really_done:
+       ret
+start_tx__maybe_unthrottle_tx:
+       ;;  step 2: is there now room?
+       mov a, tx_ring_out
+       setb c
+       subb a, tx_ring_in
+       ;; a is now write_room. If thresh >= a, we can unthrottle
+       clr c
+       subb a, tx_unthrottle_threshold
+       jc start_tx_really_done ; nope
+       ;; yes, we can unthrottle. remove the threshold and mark a request
+       mov tx_unthrottle_threshold, #0
+       setb DO_TX_UNTHROTTLE
+       ;; prod rx, which will actually send the message when in2 becomes free
+       ljmp start_in
+       
+
+serial_int:
+       push dps
+       push dpl
+       push dph
+       push dpl1
+       push dph1
+       push acc
+       jnb TI, serial_int__not_tx
+       ;; tx finished. send another character if we have one
+       clr TI                  ; clear int
+       clr TX_RUNNING
+       lcall start_tx
+serial_int__not_tx:
+       jnb RI, serial_int__not_rx
+       lcall get_rx_char
+       clr RI                  ; clear int
+serial_int__not_rx:    
+       ;; return
+       pop acc
+       pop dph1
+       pop dpl1
+       pop dph
+       pop dpl
+       pop dps
+       reti
+
+get_rx_char:
+       mov dph, #HIGH(rx_ring)
+       mov dpl, rx_ring_in
+       inc dpl                 ; target = rx_ring_in+1
+       mov a, sbuf
+       movx @dptr, a
+       ;; check for overflow before incrementing rx_ring_in
+       mov a, dpl
+       cjne a, rx_ring_out, get_rx_char__no_overflow
+       ;; signal overflow
+       ret
+get_rx_char__no_overflow:      
+       inc rx_ring_in
+       ;; kick off USB INpipe
+       acall start_in
+       ret
+
+start_in:
+       ;; check if the inpipe is already running.
+       mov  a,#0x10
+       mov dptr, OEA
+       movx @dptr,a
+
+       mov dptr, IN2CS
+       movx a, @dptr
+       jb acc.1, start_in__done; int will handle it
+       jb DO_TX_UNTHROTTLE, start_in__do_tx_unthrottle
+       ;; see if there is any work to do. a serial interrupt might occur
+       ;; during this sequence?
+       mov a, rx_ring_in
+       cjne a, rx_ring_out, start_in__have_work
+       ret                     ; nope
+start_in__have_work:   
+       ;; now copy as much data as possible into the pipe. 63 bytes max.
+       clr a
+       mov dps, a
+       mov dph, #HIGH(rx_ring) ; load DPTR0 with source
+       inc dps
+       mov dptr, IN2BUF        ; load DPTR1 with target
+       movx @dptr, a           ; in[0] signals that rest of IN is rx data
+       inc dptr
+       inc dps
+       ;; loop until we run out of data, or we have copied 64 bytes
+       mov r1, #1              ; INbuf size counter
+start_in__loop:
+       mov a, rx_ring_in
+       cjne a, rx_ring_out, start_in__still_copying
+       sjmp start_in__kick
+start_in__still_copying:
+       inc rx_ring_out
+       mov dpl, rx_ring_out
+       movx a, @dptr
+       inc dps
+       movx @dptr, a           ; write into IN buffer
+       inc dptr
+       inc dps
+       inc r1
+       cjne r1, #64, start_in__loop; loop
+start_in__kick:
+       ;; either we ran out of data, or we copied 64 bytes. r1 has byte count
+       ;; kick off IN
+       mov a, #0x10             ; Turn the green LED
+        mov dptr,OEA
+        movx @dptr, a
+       mov dptr, IN2BC
+       mov a, r1
+       jz start_in__done
+       movx @dptr, a
+       ;; done
+start_in__done:
+       ;acall dump_stat
+       ret
+start_in__do_tx_unthrottle:
+       ;; special sequence: send a tx unthrottle message
+       clr DO_TX_UNTHROTTLE
+       clr a
+       mov dps, a
+       mov dptr, IN2BUF
+       mov a, #1
+       movx @dptr, a
+       inc dptr
+       mov a, #2
+       movx @dptr, a
+       mov dptr, IN2BC
+       movx @dptr, a
+       ret
+       
+putchar:
+       clr TI
+       mov SBUF, a
+putchar_wait:
+       jnb TI, putchar_wait
+       clr TI
+       ret
+
+       
+baud_table:                    ; baud_high, then baud_low
+       ;; baud[0]: 110
+       .byte BAUD_HIGH(110)
+       .byte BAUD_LOW(110)
+       ;; baud[1]: 300
+       .byte BAUD_HIGH(300)
+       .byte BAUD_LOW(300)
+       ;; baud[2]: 1200
+       .byte BAUD_HIGH(1200)
+       .byte BAUD_LOW(1200)
+       ;; baud[3]: 2400
+       .byte BAUD_HIGH(2400)
+       .byte BAUD_LOW(2400)
+       ;; baud[4]: 4800
+       .byte BAUD_HIGH(4800)
+       .byte BAUD_LOW(4800)
+       ;; baud[5]: 9600
+       .byte BAUD_HIGH(9600)
+       .byte BAUD_LOW(9600)
+       ;; baud[6]: 19200
+       .byte BAUD_HIGH(19200)
+       .byte BAUD_LOW(19200)
+       ;; baud[7]: 38400
+       .byte BAUD_HIGH(38400)
+       .byte BAUD_LOW(38400)
+       ;; baud[8]: 57600
+       .byte BAUD_HIGH(57600)
+       .byte BAUD_LOW(57600)
+       ;; baud[9]: 115200
+       .byte BAUD_HIGH(115200)
+       .byte BAUD_LOW(115200)
+
+desc_device:
+       .byte 0x12, 0x01, 0x00, 0x01, 0xff, 0xff, 0xff, 0x40
+       .byte 0xcd, 0x06, 0x04, 0x01, 0x89, 0xab, 1, 2, 3, 0x01
+;;; The "real" device id, which must match the host driver, is that
+;;; "0xcd 0x06 0x04 0x01" sequence, which is 0x06cd, 0x0104
+       
+desc_config1:
+       .byte 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0x80, 0x32
+       .byte 0x09, 0x04, 0x00, 0x00, 0x02, 0xff, 0xff, 0xff, 0x00
+       .byte 0x07, 0x05, 0x82, 0x03, 0x40, 0x00, 0x01
+       .byte 0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00
+
+desc_strings:
+       .word string_langids, string_mfg, string_product, string_serial
+desc_strings_end:
+
+string_langids:        .byte string_langids_end-string_langids
+       .byte 3
+       .word 0
+string_langids_end:
+
+       ;; sigh. These strings are Unicode, meaning UTF16? 2 bytes each. Now
+       ;; *that* is a pain in the ass to encode. And they are little-endian
+       ;; too. Use this perl snippet to get the bytecodes:
+       /* while (<>) {
+           @c = split(//);
+           foreach $c (@c) {
+            printf("0x%02x, 0x00, ", ord($c));
+           }
+          }
+       */
+
+string_mfg:    .byte string_mfg_end-string_mfg
+       .byte 3
+;      .byte "ACME usb widgets"
+       .byte 0x41, 0x00, 0x43, 0x00, 0x4d, 0x00, 0x45, 0x00, 0x20, 0x00, 0x75, 0x00, 0x73, 0x00, 0x62, 0x00, 0x20, 0x00, 0x77, 0x00, 0x69, 0x00, 0x64, 0x00, 0x67, 0x00, 0x65, 0x00, 0x74, 0x00, 0x73, 0x00
+string_mfg_end:
+       
+string_product:        .byte string_product_end-string_product
+       .byte 3
+;      .byte "ACME USB serial widget"
+       .byte 0x41, 0x00, 0x43, 0x00, 0x4d, 0x00, 0x45, 0x00, 0x20, 0x00, 0x55, 0x00, 0x53, 0x00, 0x42, 0x00, 0x20, 0x00, 0x73, 0x00, 0x65, 0x00, 0x72, 0x00, 0x69, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x20, 0x00, 0x77, 0x00, 0x69, 0x00, 0x64, 0x00, 0x67, 0x00, 0x65, 0x00, 0x74, 0x00
+string_product_end:
+       
+string_serial: .byte string_serial_end-string_serial
+       .byte 3
+;      .byte "47"
+       .byte 0x34, 0x00, 0x37, 0x00
+string_serial_end:
+               
+;;; ring buffer memory
+       ;; tx_ring_in+1 is where the next input byte will go
+       ;; [tx_ring_out] has been sent
+       ;; if tx_ring_in == tx_ring_out, theres no work to do
+       ;; there are (tx_ring_in - tx_ring_out) chars to be written
+       ;; dont let _in lap _out
+       ;;   cannot inc if tx_ring_in+1 == tx_ring_out
+       ;;  write [tx_ring_in+1] then tx_ring_in++
+       ;;   if (tx_ring_in+1 == tx_ring_out), overflow
+       ;;   else tx_ring_in++
+       ;;  read/send [tx_ring_out+1], then tx_ring_out++
+
+       ;; rx_ring_in works the same way
+       
+       .org 0x1000
+tx_ring:
+       .skip 0x100             ; 256 bytes
+rx_ring:
+       .skip 0x100             ; 256 bytes
+       
+       
+       .END
+       
diff --git a/drivers/usb/serial/xircom_pgs_fw.h b/drivers/usb/serial/xircom_pgs_fw.h
new file mode 100644 (file)
index 0000000..3caeb7d
--- /dev/null
@@ -0,0 +1,103 @@
+/*
+ * USB Xircom PGS Firmware
+ *
+ * Copyright (c) 1999, 2000 Brian Warner        <warner@lothar.com>
+ * Copyright (c) 2001 Cristian M. Craciunescu
+ *
+ *      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.
+ *
+ * Generated from xircom_pgs.S by ezusb_convert_x.pl
+ */
+
+static const struct ezusb_hex_record xircom_pgs_firmware[] = {
+{ 0x0000,      3,      {0x02, 0x02, 0x00} },
+{ 0x0023,      4,      {0x02, 0x05, 0x9b, 0x00} },
+{ 0x0030,      5,      {0x00, 0x00, 0x00, 0x00, 0x00} },
+{ 0x0043,      4,      {0x02, 0x01, 0x00, 0x00} },
+{ 0x0100,      16,     {0x02, 0x02, 0xba, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00} },
+{ 0x0110,      16,     {0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00} },
+{ 0x0120,      16,     {0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x04, 0x85, 0x00, 0x02, 0x04, 0xb9, 0x00} },
+{ 0x0200,      16,     {0x75, 0x81, 0x5e, 0xe4, 0xf5, 0x32, 0xf5, 0x33, 0xf5, 0x30, 0xf5, 0x31, 0xf5, 0x34, 0xc2, 0x00} },
+{ 0x0210,      16,     {0xc2, 0x01, 0xa9, 0x00, 0x74, 0xfe, 0x90, 0x10, 0x00, 0xf0, 0xa3, 0xd9, 0xfc, 0x74, 0xfd, 0x90} },
+{ 0x0220,      16,     {0x11, 0x00, 0xf0, 0xa3, 0xd9, 0xfc, 0x90, 0x7f, 0x94, 0x74, 0xbf, 0xf0, 0x90, 0x7f, 0x95, 0x74} },
+{ 0x0230,      16,     {0xef, 0xf0, 0x74, 0x10, 0x90, 0x7f, 0x9e, 0xf0, 0x74, 0x00, 0x90, 0x7f, 0x98, 0xf0, 0x74, 0x40} },
+{ 0x0240,      16,     {0x90, 0x7f, 0x9d, 0xf0, 0x74, 0x00, 0x90, 0x7f, 0x97, 0xf0, 0x74, 0x82, 0x90, 0x7f, 0x9e, 0xf0} },
+{ 0x0250,      16,     {0x90, 0x7f, 0x95, 0x74, 0x03, 0xf0, 0x90, 0x7f, 0xaf, 0xe0, 0xd2, 0xe0, 0xf0, 0x74, 0x01, 0x90} },
+{ 0x0260,      16,     {0x7f, 0xab, 0xf0, 0x90, 0x7f, 0xae, 0xf0, 0x90, 0x7f, 0xac, 0x74, 0x04, 0xf0, 0x90, 0x7f, 0xad} },
+{ 0x0270,      16,     {0x74, 0x04, 0xf0, 0x90, 0x7f, 0xc9, 0xf0, 0x74, 0x07, 0x90, 0x7f, 0xaf, 0xf0, 0x74, 0x20, 0x90} },
+{ 0x0280,      16,     {0x7f, 0x9c, 0xf0, 0x74, 0x80, 0x90, 0x7f, 0x98, 0xf0, 0x74, 0x53, 0xf5, 0x98, 0x75, 0xc8, 0x30} },
+{ 0x0290,      16,     {0x7b, 0x05, 0x91, 0x44, 0xd2, 0xca, 0x75, 0x98, 0x50, 0xd2, 0xe8, 0xd2, 0xaf, 0xd2, 0xac, 0x74} },
+{ 0x02a0,      16,     {0x00, 0xf5, 0x86, 0x90, 0x7f, 0xd6, 0x74, 0x02, 0xf0, 0x79, 0x2e, 0x7a, 0x00, 0x7b, 0x00, 0xdb} },
+{ 0x02b0,      16,     {0xfe, 0xda, 0xfa, 0xd9, 0xf6, 0x74, 0x06, 0xf0, 0x80, 0xfe, 0xc0, 0x86, 0xc0, 0x82, 0xc0, 0x83} },
+{ 0x02c0,      16,     {0xc0, 0x84, 0xc0, 0x85, 0xc0, 0xe0, 0xe5, 0x91, 0xc2, 0xe4, 0xf5, 0x91, 0x90, 0x7f, 0xab, 0x74} },
+{ 0x02d0,      16,     {0x01, 0xf0, 0x90, 0x7f, 0xe8, 0xe0, 0xf9, 0xa3, 0xe0, 0xfa, 0xa3, 0xe0, 0xfb, 0xa3, 0xe0, 0xfc} },
+{ 0x02e0,      16,     {0xe9, 0x54, 0x60, 0xb4, 0x00, 0x03, 0x02, 0x03, 0x5d, 0xb4, 0x40, 0x6e, 0xba, 0x00, 0x0b, 0x12} },
+{ 0x02f0,      16,     {0x04, 0x44, 0x40, 0x03, 0x02, 0x04, 0x26, 0x02, 0x04, 0x2e, 0xba, 0x01, 0x03, 0x02, 0x04, 0x2e} },
+{ 0x0300,      16,     {0xba, 0x02, 0x03, 0x02, 0x04, 0x2e, 0xba, 0x03, 0x03, 0x02, 0x04, 0x68, 0xba, 0x04, 0x1e, 0xbb} },
+{ 0x0310,      16,     {0x00, 0x0a, 0x90, 0x7f, 0x95, 0xe0, 0x44, 0x02, 0xf0, 0x02, 0x04, 0x26, 0x90, 0x7f, 0x98, 0xe0} },
+{ 0x0320,      16,     {0x54, 0xfd, 0xf0, 0x90, 0x7f, 0x95, 0xe0, 0x54, 0xfd, 0xf0, 0x02, 0x04, 0x26, 0xba, 0x05, 0x03} },
+{ 0x0330,      16,     {0x02, 0x04, 0x2e, 0xba, 0x06, 0x19, 0xbb, 0x00, 0x08, 0xe5, 0x33, 0xd3, 0x95, 0x32, 0x02, 0x04} },
+{ 0x0340,      16,     {0x02, 0xbb, 0x01, 0x08, 0xe5, 0x32, 0xc3, 0x95, 0x33, 0x02, 0x04, 0x02, 0x02, 0x04, 0x2e, 0xba} },
+{ 0x0350,      16,     {0x07, 0x05, 0x8b, 0x34, 0x02, 0x04, 0x26, 0x02, 0x04, 0x2e, 0x02, 0x04, 0x2e, 0xba, 0x00, 0x20} },
+{ 0x0360,      16,     {0xb9, 0x80, 0x10, 0x90, 0x7f, 0x00, 0xe4, 0xf0, 0xa3, 0xf0, 0x90, 0x7f, 0xb5, 0x74, 0x02, 0xf0} },
+{ 0x0370,      16,     {0x02, 0x04, 0x26, 0xb9, 0x82, 0x02, 0x80, 0xeb, 0xb9, 0x81, 0x02, 0x80, 0xe6, 0x02, 0x04, 0x2e} },
+{ 0x0380,      16,     {0xba, 0x01, 0x0f, 0xbb, 0x00, 0x03, 0x02, 0x04, 0x2e, 0xbb, 0x01, 0x03, 0x02, 0x04, 0x26, 0x02} },
+{ 0x0390,      16,     {0x04, 0x2e, 0xba, 0x03, 0x0f, 0xbb, 0x00, 0x03, 0x02, 0x04, 0x2e, 0xbb, 0x01, 0x03, 0x02, 0x04} },
+{ 0x03a0,      16,     {0x26, 0x02, 0x04, 0x2e, 0xba, 0x06, 0x56, 0xbc, 0x01, 0x0f, 0x90, 0x7f, 0xd4, 0x74, 0x06, 0xf0} },
+{ 0x03b0,      16,     {0x90, 0x7f, 0xd5, 0x74, 0x5a, 0xf0, 0x02, 0x04, 0x26, 0xbc, 0x02, 0x12, 0xbb, 0x00, 0x6f, 0x90} },
+{ 0x03c0,      16,     {0x7f, 0xd4, 0x74, 0x06, 0xf0, 0x90, 0x7f, 0xd5, 0x74, 0x6c, 0xf0, 0x02, 0x04, 0x26, 0xbc, 0x03} },
+{ 0x03d0,      16,     {0x29, 0x74, 0x04, 0xc3, 0x9b, 0x40, 0x57, 0x60, 0x55, 0xeb, 0x2b, 0x90, 0x06, 0x8c, 0x25, 0x82} },
+{ 0x03e0,      16,     {0xf5, 0x82, 0x74, 0x00, 0x35, 0x83, 0xf5, 0x83, 0xe0, 0xf9, 0xa3, 0xe0, 0xfa, 0x90, 0x7f, 0xd4} },
+{ 0x03f0,      16,     {0xe9, 0xf0, 0x90, 0x7f, 0xd5, 0xea, 0xf0, 0x02, 0x04, 0x26, 0x02, 0x04, 0x2e, 0xba, 0x08, 0x0f} },
+{ 0x0400,      16,     {0x74, 0x01, 0x90, 0x7f, 0x00, 0xf0, 0x74, 0x01, 0x90, 0x7f, 0xb5, 0xf0, 0x02, 0x04, 0x26, 0xba} },
+{ 0x0410,      16,     {0x09, 0x03, 0x02, 0x04, 0x26, 0xba, 0x0a, 0x05, 0x74, 0x00, 0x02, 0x04, 0x02, 0xba, 0x0b, 0x03} },
+{ 0x0420,      16,     {0x02, 0x04, 0x26, 0x02, 0x04, 0x2e, 0x90, 0x7f, 0xb4, 0x74, 0x02, 0xf0, 0x80, 0x09, 0x90, 0x7f} },
+{ 0x0430,      16,     {0xb4, 0xe0, 0x44, 0x01, 0xf0, 0x80, 0x00, 0xd0, 0xe0, 0xd0, 0x85, 0xd0, 0x84, 0xd0, 0x83, 0xd0} },
+{ 0x0440,      16,     {0x82, 0xd0, 0x86, 0x32, 0xeb, 0x20, 0xe7, 0x1e, 0xc3, 0x94, 0x0a, 0x50, 0x19, 0xeb, 0x23, 0x24} },
+{ 0x0450,      16,     {0x46, 0xf5, 0x82, 0x74, 0x06, 0x34, 0x00, 0xf5, 0x83, 0xe0, 0xf5, 0xcb, 0xf5, 0xcd, 0xa3, 0xe0} },
+{ 0x0460,      16,     {0xf5, 0xca, 0xf5, 0xcc, 0xc3, 0x22, 0xd3, 0x22, 0xb9, 0x41, 0x11, 0xeb, 0x64, 0xff, 0x54, 0x80} },
+{ 0x0470,      16,     {0xfb, 0x90, 0x7f, 0x98, 0xe0, 0x54, 0x7f, 0x4b, 0xf0, 0x02, 0x04, 0x26, 0x90, 0x7f, 0x9b, 0xe0} },
+{ 0x0480,      16,     {0x64, 0xff, 0x02, 0x04, 0x02, 0xc0, 0x86, 0xc0, 0x82, 0xc0, 0x83, 0xc0, 0x84, 0xc0, 0x85, 0xc0} },
+{ 0x0490,      16,     {0xe0, 0xe5, 0x91, 0xc2, 0xe4, 0xf5, 0x91, 0x90, 0x7f, 0xa9, 0x74, 0x04, 0xf0, 0x74, 0x20, 0x90} },
+{ 0x04a0,      16,     {0x7f, 0x9c, 0xf0, 0x12, 0x05, 0xdc, 0x74, 0x20, 0x90, 0x7f, 0x9c, 0xf0, 0xd0, 0xe0, 0xd0, 0x85} },
+{ 0x04b0,      16,     {0xd0, 0x84, 0xd0, 0x83, 0xd0, 0x82, 0xd0, 0x86, 0x32, 0xc0, 0x86, 0xc0, 0x82, 0xc0, 0x83, 0xc0} },
+{ 0x04c0,      16,     {0x84, 0xc0, 0x85, 0xc0, 0xe0, 0x74, 0x10, 0x90, 0x7f, 0x9c, 0xf0, 0xe5, 0x91, 0xc2, 0xe4, 0xf5} },
+{ 0x04d0,      16,     {0x91, 0x90, 0x7f, 0xaa, 0x74, 0x04, 0xf0, 0x90, 0x7f, 0xc9, 0xe0, 0xf9, 0xe4, 0xf5, 0x86, 0x90} },
+{ 0x04e0,      16,     {0x7d, 0xc0, 0x75, 0x85, 0x10, 0x85, 0x32, 0x84, 0xe0, 0x05, 0x86, 0x05, 0x84, 0xf0, 0xe5, 0x84} },
+{ 0x04f0,      16,     {0xb5, 0x33, 0x02, 0x80, 0x09, 0x05, 0x32, 0x05, 0x86, 0xa3, 0xd9, 0xec, 0x80, 0x00, 0x90, 0x7f} },
+{ 0x0500,      16,     {0xc9, 0xf0, 0xb1, 0x6d, 0x74, 0x20, 0x90, 0x7f, 0x9c, 0xf0, 0xd0, 0xe0, 0xd0, 0x85, 0xd0, 0x84} },
+{ 0x0510,      16,     {0xd0, 0x83, 0xd0, 0x82, 0xd0, 0x86, 0x32, 0xe4, 0xf5, 0x86, 0x90, 0x7f, 0xbc, 0xe0, 0x20, 0xe1} },
+{ 0x0520,      16,     {0x4b, 0x90, 0x7d, 0x00, 0xe5, 0x32, 0xf0, 0xa3, 0xe5, 0x33, 0xf0, 0xa3, 0xe5, 0x30, 0xf0, 0xa3} },
+{ 0x0530,      16,     {0xe5, 0x31, 0xf0, 0xa3, 0xe4, 0x30, 0x00, 0x01, 0x04, 0xf0, 0xa3, 0x05, 0x86, 0x90, 0x10, 0x00} },
+{ 0x0540,      16,     {0x79, 0x10, 0xe0, 0xa3, 0x05, 0x86, 0xf0, 0xa3, 0x05, 0x86, 0xd9, 0xf6, 0x05, 0x86, 0x74, 0xfc} },
+{ 0x0550,      16,     {0xf0, 0xa3, 0x05, 0x86, 0x90, 0x11, 0x00, 0x79, 0x10, 0xe0, 0xa3, 0x05, 0x86, 0xf0, 0xa3, 0x05} },
+{ 0x0560,      16,     {0x86, 0xd9, 0xf6, 0xe4, 0xf5, 0x86, 0x90, 0x7f, 0xbd, 0x74, 0x26, 0xf0, 0x22, 0x20, 0x00, 0x13} },
+{ 0x0570,      16,     {0xe5, 0x32, 0xb5, 0x33, 0x01, 0x22, 0x05, 0x33, 0x75, 0x83, 0x10, 0x85, 0x33, 0x82, 0xe0, 0xf5} },
+{ 0x0580,      16,     {0x99, 0xd2, 0x00, 0x74, 0x00, 0xb5, 0x34, 0x01, 0x22, 0xe5, 0x33, 0xd3, 0x95, 0x32, 0xc3, 0x95} },
+{ 0x0590,      16,     {0x34, 0x40, 0xf5, 0x75, 0x34, 0x00, 0xd2, 0x01, 0x02, 0x05, 0xdc, 0xc0, 0x86, 0xc0, 0x82, 0xc0} },
+{ 0x05a0,      16,     {0x83, 0xc0, 0x84, 0xc0, 0x85, 0xc0, 0xe0, 0x30, 0x99, 0x07, 0xc2, 0x99, 0xc2, 0x00, 0x12, 0x05} },
+{ 0x05b0,      16,     {0x70, 0x30, 0x98, 0x05, 0x12, 0x05, 0xc6, 0xc2, 0x98, 0xd0, 0xe0, 0xd0, 0x85, 0xd0, 0x84, 0xd0} },
+{ 0x05c0,      16,     {0x83, 0xd0, 0x82, 0xd0, 0x86, 0x32, 0x75, 0x83, 0x11, 0x85, 0x30, 0x82, 0x05, 0x82, 0xe5, 0x99} },
+{ 0x05d0,      16,     {0xf0, 0xe5, 0x82, 0xb5, 0x31, 0x01, 0x22, 0x05, 0x30, 0xb1, 0xdc, 0x22, 0x74, 0x10, 0x90, 0x7f} },
+{ 0x05e0,      16,     {0x9c, 0xf0, 0x90, 0x7f, 0xb8, 0xe0, 0x20, 0xe1, 0x3e, 0x20, 0x01, 0x3c, 0xe5, 0x30, 0xb5, 0x31} },
+{ 0x05f0,      16,     {0x01, 0x22, 0xe4, 0xf5, 0x86, 0x75, 0x83, 0x11, 0x05, 0x86, 0x90, 0x7e, 0x00, 0xf0, 0xa3, 0x05} },
+{ 0x0600,      16,     {0x86, 0x79, 0x01, 0xe5, 0x30, 0xb5, 0x31, 0x02, 0x80, 0x10, 0x05, 0x31, 0x85, 0x31, 0x82, 0xe0} },
+{ 0x0610,      16,     {0x05, 0x86, 0xf0, 0xa3, 0x05, 0x86, 0x09, 0xb9, 0x40, 0xe9, 0x74, 0x10, 0x90, 0x7f, 0x9c, 0xf0} },
+{ 0x0620,      16,     {0x90, 0x7f, 0xb9, 0xe9, 0x60, 0x01, 0xf0, 0x22, 0xc2, 0x01, 0xe4, 0xf5, 0x86, 0x90, 0x7e, 0x00} },
+{ 0x0630,      16,     {0x74, 0x01, 0xf0, 0xa3, 0x74, 0x02, 0xf0, 0x90, 0x7f, 0xb9, 0xf0, 0x22, 0xc2, 0x99, 0xf5, 0x99} },
+{ 0x0640,      16,     {0x30, 0x99, 0xfd, 0xc2, 0x99, 0x22, 0xe5, 0x5e, 0xf6, 0x3c, 0xfd, 0x8f, 0xfe, 0xc8, 0xff, 0x64} },
+{ 0x0650,      16,     {0xff, 0xb2, 0xff, 0xd9, 0xff, 0xed, 0xff, 0xf3, 0xff, 0xfa, 0x12, 0x01, 0x00, 0x01, 0xff, 0xff} },
+{ 0x0660,      16,     {0xff, 0x40, 0xcd, 0x06, 0x04, 0x01, 0x89, 0xab, 0x01, 0x02, 0x03, 0x01, 0x09, 0x02, 0x20, 0x00} },
+{ 0x0670,      16,     {0x01, 0x01, 0x00, 0x80, 0x32, 0x09, 0x04, 0x00, 0x00, 0x02, 0xff, 0xff, 0xff, 0x00, 0x07, 0x05} },
+{ 0x0680,      16,     {0x82, 0x03, 0x40, 0x00, 0x01, 0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00, 0x06, 0x94, 0x06, 0x98} },
+{ 0x0690,      16,     {0x06, 0xba, 0x06, 0xe8, 0x04, 0x03, 0x00, 0x00, 0x22, 0x03, 0x41, 0x00, 0x43, 0x00, 0x4d, 0x00} },
+{ 0x06a0,      16,     {0x45, 0x00, 0x20, 0x00, 0x75, 0x00, 0x73, 0x00, 0x62, 0x00, 0x20, 0x00, 0x77, 0x00, 0x69, 0x00} },
+{ 0x06b0,      16,     {0x64, 0x00, 0x67, 0x00, 0x65, 0x00, 0x74, 0x00, 0x73, 0x00, 0x2e, 0x03, 0x41, 0x00, 0x43, 0x00} },
+{ 0x06c0,      16,     {0x4d, 0x00, 0x45, 0x00, 0x20, 0x00, 0x55, 0x00, 0x53, 0x00, 0x42, 0x00, 0x20, 0x00, 0x73, 0x00} },
+{ 0x06d0,      16,     {0x65, 0x00, 0x72, 0x00, 0x69, 0x00, 0x61, 0x00, 0x6c, 0x00, 0x20, 0x00, 0x77, 0x00, 0x69, 0x00} },
+{ 0x06e0,      14,     {0x64, 0x00, 0x67, 0x00, 0x65, 0x00, 0x74, 0x00, 0x06, 0x03, 0x34, 0x00, 0x37, 0x00} },
+{ 0xffff,      0,      {0x00} }
+};
index 146b52fbcb46578369a2c07017bc95e9fb8d2bff..ce084a2a9a7d6017a9041162962453d6c2ee2c71 100644 (file)
@@ -41,7 +41,7 @@
 
 #include <linux/sched.h>
 #include <linux/errno.h>
-#include <linux/malloc.h>
+#include <linux/slab.h>
 #include <linux/hdreg.h>
 #include <linux/ide.h>
 
index ea05a32ff04c5e8e7be63ddc6f5c0fcdc3151c8d..6b17cd36df561672f0125878dfec3a3cb2836c31 100644 (file)
@@ -45,7 +45,7 @@
 
 #include <linux/sched.h>
 #include <linux/errno.h>
-#include <linux/malloc.h>
+#include <linux/slab.h>
 
 extern int usb_stor_control_msg(struct us_data *us, unsigned int pipe,
                                u8 request, u8 requesttype, u16 value,
index 2f26d8dc48a82c3e3410de7bc27ccde236e1e05c..6a1a4eeb0439f65af497be71a6684107e5186c78 100644 (file)
@@ -49,7 +49,7 @@
 #include "debug.h"
 #include "transport.h"
 
-#include <linux/malloc.h>
+#include <linux/slab.h>
 
 /*
  * kernel thread actions
index 227f61922feb869c7c9d12d6380ccf9eecd5a4e9..db62a9e65fd3d48ec92aac82dfba334acddd10d6 100644 (file)
@@ -965,7 +965,7 @@ int sddr09_transport(Scsi_Cmnd *srb, struct us_data *us)
                                (info->pageshift + info->blockshift) ) ) {
 
                        US_DEBUGP("Error: Requested LBA %04X exceeds maximum "
-                         "block %04X\n", lba,
+                         "block %04lX\n", lba,
                          (info->capacity >> (info->pageshift + info->blockshift))-1);
 
                        // FIXME: sense buffer?
index 92a7ca9a14a02fa1a339836d76eefde37ac0b036..34564297e15255e57aeaf100bba58ac701e7edd8 100644 (file)
@@ -52,7 +52,7 @@
 
 #include <linux/sched.h>
 #include <linux/errno.h>
-#include <linux/malloc.h>
+#include <linux/slab.h>
 
 /***********************************************************************
  * Helper routines
index e90dab3e7df40c0ef2be387db3d7179ba73add33..a1751f196fcf68fe7dad8406317b2a2155143dac 100644 (file)
 #include <linux/sched.h>
 #include <linux/errno.h>
 #include <linux/init.h>
-#include <linux/malloc.h>
+#include <linux/slab.h>
 
 /* Some informational data */
 MODULE_AUTHOR("Matthew Dharm <mdharm-usb@one-eyed-alien.net>");
 MODULE_DESCRIPTION("USB Mass Storage driver for Linux");
+MODULE_LICENSE("GPL");
 
 /*
  * Per device data
index b8b5d07321660635acd1cb76f26bcb87dc843d1d..da7007f02923ef60a88319105d2d56c885ab7c90 100644 (file)
@@ -2864,3 +2864,4 @@ module_exit (ohci_hcd_cleanup);
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
index 8c5732782faf3ef7c6ccaacac01ffd199d6f488c..babac970c19e24555b44dc8a594c3940c05c8d14 100644 (file)
@@ -650,4 +650,5 @@ module_exit (usb_skel_exit);
 
 MODULE_AUTHOR(DRIVER_AUTHOR);
 MODULE_DESCRIPTION(DRIVER_DESC);
+MODULE_LICENSE("GPL");
 
index 9a6d8395b6af06b4429ef47f11c2570b81d67d73..c2c742d4b1578a047fd8dd1e31d52717d5007c89 100644 (file)
@@ -44,6 +44,7 @@
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 static unsigned char usb_kbd_keycode[256] = {
          0,  0,  0,  0, 30, 48, 46, 32, 18, 33, 34, 35, 23, 36, 37, 38,
index 42666c5e2c369503a97782505b4f7d132f822889..9eb4fb275b07b39b170ec2197e9d58cd1a8b84b2 100644 (file)
@@ -44,6 +44,7 @@
 
 MODULE_AUTHOR( DRIVER_AUTHOR );
 MODULE_DESCRIPTION( DRIVER_DESC );
+MODULE_LICENSE("GPL");
 
 struct usb_mouse {
        signed char data[8];
index c63def551d1dc3b453d8f19a9fa0da41efb0d3ab..32d9f6936e2eed021cd26e7cec5063057606e758 100644 (file)
@@ -1565,3 +1565,4 @@ module_exit (usbnet_exit);
 
 MODULE_AUTHOR ("David Brownell <dbrownell@users.sourceforge.net>");
 MODULE_DESCRIPTION ("USB Host-to-Host Link Drivers (Belkin, Linux, NetChip, Prolific, ...)");
+MODULE_LICENSE("GPL");
index e95d91a8e9f918008bb95f6a4e58fa832f1f5085..1f81c821f6dba6cbf4ea579b4aabb2b7a4efac66 100644 (file)
@@ -73,6 +73,9 @@ if [ "$CONFIG_FB" = "y" ]; then
       bool '  S3 Trio display support' CONFIG_FB_S3TRIO
       tristate '  VGA 16-color graphics console' CONFIG_FB_VGA16
    fi
+   if [ "$CONFIG_PARISC" = "y" ]; then
+      bool '  Generic STI frame buffer device support' CONFIG_FB_STI
+   fi
    if [ "$CONFIG_MAC" = "y" ]; then
       define_bool CONFIG_FB_MAC y
       bool '  Apple "valkyrie" display support' CONFIG_FB_VALKYRIE
@@ -101,12 +104,16 @@ if [ "$CONFIG_FB" = "y" ]; then
          bool '    CGsix (GX,TurboGX) support' CONFIG_FB_CGSIX
       fi
    fi
-   tristate '  NEC PowerVR 2 display support' CONFIG_FB_PVR2
-   dep_bool '    Debug pvr2fb' CONFIG_FB_PVR2_DEBUG $CONFIG_FB_PVR2
-   bool '  Epson 1355 framebuffer support' CONFIG_FB_E1355
-   if [ "$CONFIG_FB_E1355" = "y" ]; then
-      hex '    Register Base Address' CONFIG_E1355_REG_BASE a8000000
-      hex '    Framebuffer Base Address' CONFIG_E1355_FB_BASE a8200000
+   if [ "$CONFIG_SH_DREAMCAST" = "y" ]; then
+      tristate '  NEC PowerVR 2 display support' CONFIG_FB_PVR2
+      dep_bool '    Debug pvr2fb' CONFIG_FB_PVR2_DEBUG $CONFIG_FB_PVR2
+   fi
+   if [ "$CONFIG_SUPERH" = "y" ]; then
+      bool '  Epson 1355 framebuffer support' CONFIG_FB_E1355
+      if [ "$CONFIG_FB_E1355" = "y" ]; then
+         hex '    Register Base Address' CONFIG_E1355_REG_BASE a8000000
+         hex '    Framebuffer Base Address' CONFIG_E1355_FB_BASE a8200000
+      fi
    fi
    if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
       if [ "$CONFIG_PCI" != "n" ]; then
@@ -128,10 +135,19 @@ if [ "$CONFIG_FB" = "y" ]; then
         if [ "$CONFIG_FB_ATY" != "n" ]; then
            bool '    Mach64 GX support (EXPERIMENTAL)' CONFIG_FB_ATY_GX
            bool '    Mach64 CT/VT/GT/LT (incl. 3D RAGE) support' CONFIG_FB_ATY_CT
+              if [ "$CONFIG_FB_ATY_CT" = "y" ]; then
+                 bool '      Sony Vaio C1VE 1024x480 LCD support' CONFIG_FB_ATY_CT_VAIO_LCD
+              fi
+        fi
+        tristate '  ATI Radeon display support (EXPERIMENTAL)' CONFIG_FB_RADEON
+        tristate '  ATI Rage128 display support (EXPERIMENTAL)' CONFIG_FB_ATY128
+        tristate '  SIS acceleration (EXPERIMENTAL)' CONFIG_FB_SIS
+        if [ "$CONFIG_FB_SIS" != "n" ]; then
+           bool '    SIS 630/540/730 support' CONFIG_FB_SIS_300
+           bool '    SIS 315H/315 support' CONFIG_FB_SIS_315
         fi
-        tristate '  ATI Rage 128 display support (EXPERIMENTAL)' CONFIG_FB_ATY128
         tristate '  3Dfx Banshee/Voodoo3 display support (EXPERIMENTAL)' CONFIG_FB_3DFX
-        tristate '  SIS 630/540 display support (EXPERIMENTAL)' CONFIG_FB_SIS
+        tristate '  3Dfx Voodoo Graphics (sst1) support (EXPERIMENTAL)' CONFIG_FB_VOODOO1
       fi
    fi
    if [ "$ARCH" = "sparc" -o "$ARCH" = "sparc64" ]; then
@@ -173,6 +189,23 @@ if [ "$CONFIG_FB" = "y" ]; then
    if [ "$CONFIG_HD64461" = "y" ]; then
       tristate '  HD64461 Frame Buffer support' CONFIG_FB_HIT
    fi
+   if [ "$CONFIG_DECSTATION" = "y" ]; then
+     if [ "$CONFIG_TC" = "y" ]; then
+       bool '  PMAG-BA TURBOchannel framebuffer support' CONFIG_FB_PMAG_BA
+       bool '  PMAGB-B TURBOchannel framebuffer spport' CONFIG_FB_PMAGB_B
+       bool '  Maxine (Personal DECstation) onboard framebuffer spport' CONFIG_FB_MAXINE
+     fi
+   fi
+   if [ "$CONFIG_NINO" = "y" ]; then
+      bool '  TMPTX3912/PR31700 frame buffer support' CONFIG_FB_TX3912
+   fi
+   if [ "$CONFIG_DECSTATION" = "y" ]; then
+     if [ "$CONFIG_TC" = "y" ]; then
+       bool '  PMAG-BA TURBOchannel framebuffer support' CONFIG_FB_PMAG_BA
+       bool '  PMAGB-B TURBOchannel framebuffer spport' CONFIG_FB_PMAGB_B
+       bool '  Maxine (Personal DECstation) onboard framebuffer spport' CONFIG_FB_MAXINE
+     fi
+   fi
    if [ "$CONFIG_EXPERIMENTAL" = "y" ]; then
       tristate '  Virtual Frame Buffer support (ONLY FOR TESTING!)' CONFIG_FB_VIRTUAL
    fi
@@ -202,24 +235,28 @@ if [ "$CONFIG_FB" = "y" ]; then
           "$CONFIG_FB_ATARI" = "y" -o "$CONFIG_FB_CYBER" = "y" -o \
           "$CONFIG_FB_MAC" = "y" -o "$CONFIG_FB_RETINAZ3" = "y" -o \
           "$CONFIG_FB_VIRGE" = "y" -o "$CONFIG_FB_VIRTUAL" = "y" -o \
-          "$CONFIG_FB_BWTWO" = "y" -o "$CONFIG_FB_CLGEN" = "y" ]; then
+          "$CONFIG_FB_BWTWO" = "y" -o "$CONFIG_FB_CLGEN" = "y"  -o \
+          "$CONFIG_FB_TX3912" = "y" ]; then
         define_tristate CONFIG_FBCON_MFB y
       else
         if [ "$CONFIG_FB_ACORN" = "m" -o "$CONFIG_FB_AMIGA" = "m" -o \
              "$CONFIG_FB_ATARI" = "m" -o "$CONFIG_FB_CYBER" = "m" -o \
              "$CONFIG_FB_MAC" = "m" -o "$CONFIG_FB_RETINAZ3" = "m" -o \
              "$CONFIG_FB_VIRGE" = "m" -o "$CONFIG_FB_VIRTUAL" = "m" -o \
-             "$CONFIG_FB_BWTWO" = "m" -o "$CONFIG_FB_CLGEN" = "m" ]; then
+             "$CONFIG_FB_BWTWO" = "m" -o "$CONFIG_FB_CLGEN" = "m" -o \
+             "$CONFIG_FB_TX3912" = "m" ]; then
            define_tristate CONFIG_FBCON_MFB m
         fi
       fi
       if [ "$CONFIG_FB_ACORN" = "y" -o "$CONFIG_FB_MAC" = "y" -o \
-          "$CONFIG_FB_SA1100" = "y" -o "$CONFIG_FB_VIRTUAL" = "y" ]; then
+          "$CONFIG_FB_SA1100" = "y" -o "$CONFIG_FB_VIRTUAL" = "y" -o \
+          "$CONFIG_FB_TX3912" = "y" ]; then
         define_tristate CONFIG_FBCON_CFB2 y
         define_tristate CONFIG_FBCON_CFB4 y
       else
         if [ "$CONFIG_FB_ACORN" = "m" -o "$CONFIG_FB_MAC" = "m" -o \
-             "$CONFIG_FB_SA1100" = "m" -o "$CONFIG_FB_VIRTUAL" = "m" ]; then
+             "$CONFIG_FB_SA1100" = "m" -o "$CONFIG_FB_VIRTUAL" = "m" -o \
+             "$CONFIG_FB_TX3912" = "m" ]; then
            define_tristate CONFIG_FBCON_CFB2 m
            define_tristate CONFIG_FBCON_CFB4 m
         fi
@@ -236,9 +273,11 @@ if [ "$CONFIG_FB" = "y" ]; then
            "$CONFIG_FB_IGA" = "y" -o "$CONFIG_FB_MATROX" = "y" -o \
           "$CONFIG_FB_CT65550" = "y" -o "$CONFIG_FB_PM2" = "y" -o \
           "$CONFIG_FB_P9100" = "y" -o "$CONFIG_FB_ATY128" = "y" -o \
-          "$CONFIG_FB_RIVA" = "y" -o \
+          "$CONFIG_FB_RIVA" = "y" -o "$CONFIG_FB_RADEON" = "y" -o \
           "$CONFIG_FB_SGIVW" = "y" -o "$CONFIG_FB_CYBER2000" = "y" -o \
           "$CONFIG_FB_SA1100" = "y" -o "$CONFIG_FB_3DFX" = "y" -o \
+          "$CONFIG_FB_PMAG_BA" = "y" -o "$CONFIG_FB_PMAGB_B" = "y" -o \
+          "$CONFIG_FB_MAXINE" = "y" -o "$CONFIG_FB_TX3912" = "y" -o \
           "$CONFIG_FB_SIS" = "y" ]; then
         define_tristate CONFIG_FBCON_CFB8 y
       else
@@ -256,14 +295,17 @@ if [ "$CONFIG_FB" = "y" ]; then
              "$CONFIG_FB_P9100" = "m" -o "$CONFIG_FB_ATY128" = "m" -o \
              "$CONFIG_FB_RIVA" = "m" -o "$CONFIG_FB_3DFX" = "m" -o \
              "$CONFIG_FB_SGIVW" = "m" -o "$CONFIG_FB_CYBER2000" = "m" -o \
-             "$CONFIG_FB_SA1100" = "m" -o "$CONFIG_FB_SIS" = "m" ]; then
+             "$CONFIG_FB_PMAG_BA" = "m" -o "$CONFIG_FB_PMAGB_B" = "m" -o \
+             "$CONFIG_FB_MAXINE" = "m" -o "$CONFIG_FB_RADEON" = "m" -o \
+             "$CONFIG_FB_SA1100" = "m" -o "$CONFIG_FB_SIS" = "m" -o \
+             "$CONFIG_FB_TX3912" = "m" ]; then
            define_tristate CONFIG_FBCON_CFB8 m
         fi
       fi
       if [ "$CONFIG_FB_ATARI" = "y" -o "$CONFIG_FB_ATY" = "y" -o \
           "$CONFIG_FB_MAC" = "y" -o "$CONFIG_FB_VESA" = "y" -o \
           "$CONFIG_FB_VIRTUAL" = "y" -o "$CONFIG_FB_TBOX" = "y" -o \
-          "$CONFIG_FB_Q40" = "y" -o \
+          "$CONFIG_FB_Q40" = "y" -o "$CONFIG_FB_RADEON" = "y" -o \
           "$CONFIG_FB_CONTROL" = "y" -o "$CONFIG_FB_CLGEN" = "y" -o \
           "$CONFIG_FB_VIRGE" = "y" -o "$CONFIG_FB_CYBER" = "y" -o \
           "$CONFIG_FB_VALKYRIE" = "y" -o "$CONFIG_FB_PLATINUM" = "y" -o \
@@ -272,7 +314,7 @@ if [ "$CONFIG_FB" = "y" ]; then
           "$CONFIG_FB_RIVA" = "y" -o "$CONFIG_FB_ATY128" = "y" -o \
           "$CONFIG_FB_CYBER2000" = "y" -o "$CONFIG_FB_3DFX" = "y"  -o \
           "$CONFIG_FB_SIS" = "y" -o "$CONFIG_FB_SA1100" = "y" -o \
-          "$CONFIG_FB_PVR2" = "y" ]; then
+          "$CONFIG_FB_PVR2" = "y" -o "$CONFIG_FB_VOODOO1" = "y" ]; then
         define_tristate CONFIG_FBCON_CFB16 y
       else
         if [ "$CONFIG_FB_ATARI" = "m" -o "$CONFIG_FB_ATY" = "m" -o \
@@ -286,22 +328,25 @@ if [ "$CONFIG_FB" = "y" ]; then
              "$CONFIG_FB_PM2" = "m" -o "$CONFIG_FB_SGIVW" = "m" -o \
              "$CONFIG_FB_RIVA" = "m" -o "$CONFIG_FB_ATY128" = "m" -o \
              "$CONFIG_FB_CYBER2000" = "m" -o "$CONFIG_FB_SIS" = "m" -o \
-             "$CONFIG_FB_SA1100" = "m" -o "$CONFIG_FB_PVR2" = "m" ]; then
+             "$CONFIG_FB_SA1100" = "m" -o "$CONFIG_FB_RADEON" = "m" -o \
+             "$CONFIG_FB_PVR2" = "m" -o "$CONFIG_FB_VOODOO1" = "m" ]; then
            define_tristate CONFIG_FBCON_CFB16 m
         fi
       fi
       if [ "$CONFIG_FB_ATY" = "y" -o "$CONFIG_FB_VIRTUAL" = "y" -o \
           "$CONFIG_FB_CLGEN" = "y" -o "$CONFIG_FB_VESA" = "y" -o \
           "$CONFIG_FB_MATROX" = "y" -o "$CONFIG_FB_PM2" = "y" -o \
-           "$CONFIG_FB_ATY128" = "y" -o \
-          "$CONFIG_FB_CYBER2000" = "y" -o "$CONFIG_FB_PVR2" = "y" ]; then
+           "$CONFIG_FB_ATY128" = "y" -o "$CONFIG_FB_RADEON" = "y" -o \
+          "$CONFIG_FB_CYBER2000" = "y" -o "$CONFIG_FB_PVR2" = "y" -o \
+          "$CONFIG_FB_VOODOO1" = "y" ]; then
         define_tristate CONFIG_FBCON_CFB24 y
       else
         if [ "$CONFIG_FB_ATY" = "m" -o "$CONFIG_FB_VIRTUAL" = "m" -o \
              "$CONFIG_FB_CLGEN" = "m" -o "$CONFIG_FB_VESA" = "m" -o \
              "$CONFIG_FB_MATROX" = "m" -o "$CONFIG_FB_PM2" = "m" -o \
-             "$CONFIG_FB_ATY128" = "m" -o \
-             "$CONFIG_FB_CYBER2000" = "m" -o "$CONFIG_FB_PVR2" = "m" ]; then
+             "$CONFIG_FB_ATY128" = "m" -o "$CONFIG_FB_RADEON" = "m" -o \
+             "$CONFIG_FB_CYBER2000" = "m" -o "$CONFIG_FB_PVR2" = "m" -o \
+             "$CONFIG_FB_VOODOO1" = "m" ]; then
            define_tristate CONFIG_FBCON_CFB24 m
         fi
       fi
@@ -312,8 +357,9 @@ if [ "$CONFIG_FB" = "y" ]; then
           "$CONFIG_FB_MATROX" = "y" -o "$CONFIG_FB_PM2" = "y" -o \
           "$CONFIG_FB_RIVA" = "y" -o "$CONFIG_FB_ATY128" = "y" -o \
           "$CONFIG_FB_FM2" = "y" -o "$CONFIG_FB_SGIVW" = "y" -o \
+          "$CONFIG_FB_RADEON" = "y" -o "$CONFIG_FB_PVR2" = "y" -o \
           "$CONFIG_FB_3DFX" = "y" -o "$CONFIG_FB_SIS" = "y" -o \
-          "$CONFIG_FB_PVR2" = "y" ]; then
+          "$CONFIG_FB_VOODOO1" = "y" ]; then
         define_tristate CONFIG_FBCON_CFB32 y
       else
         if [ "$CONFIG_FB_ATARI" = "m" -o "$CONFIG_FB_ATY" = "m" -o \
@@ -322,9 +368,9 @@ if [ "$CONFIG_FB" = "y" ]; then
              "$CONFIG_FB_TGA" = "m" -o "$CONFIG_FB_PLATINUM" = "m" -o \
              "$CONFIG_FB_MATROX" = "m" -o "$CONFIG_FB_PM2" = "m" -o \
              "$CONFIG_FB_RIVA" = "m" -o "$CONFIG_FB_ATY128" = "m" -o \
-             "$CONFIG_FB_3DFX" = "m" -o \
+             "$CONFIG_FB_3DFX" = "m" -o "$CONFIG_FB_RADEON" = "m" -o \
              "$CONFIG_FB_SGIVW" = "m" -o "$CONFIG_FB_SIS" = "m" -o \
-             "$CONFIG_FB_PVR2" = "m" ]; then
+             "$CONFIG_FB_PVR2" = "m" -o "$CONFIG_FB_VOODOO1" = "m" ]; then
            define_tristate CONFIG_FBCON_CFB32 m
         fi
       fi
@@ -371,6 +417,9 @@ if [ "$CONFIG_FB" = "y" ]; then
            define_tristate CONFIG_FBCON_HGA m
         fi
       fi
+      if [ "$CONFIG_FB_STI" = "y" ]; then
+        define_tristate CONFIG_FBCON_STI y
+      fi
    fi
    bool '  Support only 8 pixels wide fonts' CONFIG_FBCON_FONTWIDTH8_ONLY
    if [ "$ARCH" = "sparc" -o "$ARCH" = "sparc64" ]; then
index f98fd81c4ca7fb4e57c3cf879469d15b01201bc3..b6743c39ba2d9954603b009cb17032e6ebb7ea53 100644 (file)
@@ -22,6 +22,7 @@ export-objs    := fbmem.o fbcmap.o fbcon.o fbmon.o modedb.o \
 obj-$(CONFIG_DUMMY_CONSOLE)       += dummycon.o
 obj-$(CONFIG_SGI_NEWPORT_CONSOLE) += newport_con.o
 obj-$(CONFIG_PROM_CONSOLE)        += promcon.o promcon_tbl.o
+obj-$(CONFIG_STI_CONSOLE)         += sticon.o sticon-bmode.o sticore.o
 obj-$(CONFIG_VGA_CONSOLE)         += vgacon.o
 obj-$(CONFIG_MDA_CONSOLE)         += mdacon.o
 
@@ -47,6 +48,7 @@ obj-$(CONFIG_FB_APOLLO)           += dnfb.o
 obj-$(CONFIG_FB_Q40)              += q40fb.o
 obj-$(CONFIG_FB_ATARI)            += atafb.o
 obj-$(CONFIG_FB_ATY128)           += aty128fb.o
+obj-$(CONFIG_FB_RADEON)                  += radeonfb.o
 obj-$(CONFIG_FB_IGA)              += igafb.o
 obj-$(CONFIG_FB_CONTROL)          += controlfb.o
 obj-$(CONFIG_FB_PLATINUM)         += platinumfb.o
@@ -77,6 +79,12 @@ obj-$(CONFIG_FB_TCX)              += tcxfb.o sbusfb.o
 obj-$(CONFIG_FB_CGFOURTEEN)       += cgfourteenfb.o sbusfb.o
 obj-$(CONFIG_FB_P9100)            += p9100fb.o sbusfb.o
 obj-$(CONFIG_FB_LEO)              += leofb.o sbusfb.o
+obj-$(CONFIG_FB_STI)             += stifb.o sticore.o fbgen.o
+obj-$(CONFIG_FB_PMAG_BA)          += pmag-ba-fb.o
+obj-$(CONFIG_FB_PMAGB_B)          += pmagb-b-fb.o
+obj-$(CONFIG_FB_MAXINE)           += maxinefb.o
+obj-$(CONFIG_FB_TX3912)           += tx3912fb.o
+
 
 subdir-$(CONFIG_FB_MATROX)       += matrox
 ifeq ($(CONFIG_FB_MATROX),y)
@@ -106,6 +114,7 @@ obj-$(CONFIG_FB_VIRTUAL)          += vfb.o
 obj-$(CONFIG_FB_HIT)              += hitfb.o fbgen.o
 obj-$(CONFIG_FB_E1355)            += epson1355fb.o fbgen.o
 obj-$(CONFIG_FB_PVR2)             += pvr2fb.o
+obj-$(CONFIG_FB_VOODOO1)          += sstfb.o
 
 # Generic Low Level Drivers
 
@@ -125,6 +134,7 @@ obj-$(CONFIG_FBCON_MAC)           += fbcon-mac.o
 obj-$(CONFIG_FBCON_MFB)           += fbcon-mfb.o
 obj-$(CONFIG_FBCON_VGA)           += fbcon-vga.o
 obj-$(CONFIG_FBCON_HGA)           += fbcon-hga.o
+obj-$(CONFIG_FBCON_STI)           += fbcon-sti.o
 
 include $(TOPDIR)/Rules.make
 
@@ -140,3 +150,4 @@ promcon_tbl.c: prom.uni ../char/conmakehash
            -e 's/dfont\(_uni.*\]\)/promfont\1 __initdata/' > promcon_tbl.c
 
 promcon_tbl.o: promcon_tbl.c $(TOPDIR)/include/linux/types.h
+
index 3d0a0797b4ff6dfd108ca0946ee1422561acfb03..33b7078d7b52b4e5736f26e28599eccaaa599291 100644 (file)
@@ -1285,7 +1285,7 @@ static inline void cyberpro_init_hw(struct cfb_info *cfb, int at_boot)
                cfb->mclk_div  = cyber2000_grphr(MCLK_DIV);
        }
 #endif
-#ifdef __i386__
+#if defined(__i386__) || defined(__x86_64__)
        /*
         * x86 is simple, we just do regular outb's instead of
         * cyber2000_outb.
index ab5696291ded0fa41d642911cfa75fd50e5b808b..461ac8740b26883a2da8862c3fc91bdf0df4f7d9 100644 (file)
@@ -593,7 +593,7 @@ fb_mmap(struct file *file, struct vm_area_struct * vma)
        pgprot_val(vma->vm_page_prot) |= _PAGE_NO_CACHE|_PAGE_GUARDED;
 #elif defined(__alpha__)
        /* Caching is off in the I/O space quadrant by design.  */
-#elif defined(__i386__)
+#elif defined(__i386__) || defined(__x86_64__)
        if (boot_cpu_data.x86 > 3)
                pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
 #elif defined(__mips__)
index 93d91e434aa71ea55bb63d4b34a05798874ddaab..1ecbb2c808b28d6da23bbbe044f078eae10b670e 100644 (file)
@@ -106,7 +106,7 @@ static char *hga_type_name;
 
 /* Global locks */
 
-spinlock_t hga_reg_lock = SPIN_LOCK_UNLOCKED;
+static spinlock_t hga_reg_lock = SPIN_LOCK_UNLOCKED;
 
 /* Framebuffer driver structures */
 
index a6048a7931acd16380f439cbc339d8be8341c492..cd93ad2bcb7b35b8eb172a81419e0d88361a9f29 100644 (file)
 
 #endif /* MATROXFB_DEBUG */
 
-#ifndef __i386__
+#if !defined(__i386__) && !defined(__x86_64__)
 #ifndef ioremap_nocache
 #define ioremap_nocache(X,Y) ioremap(X,Y)
 #endif
 /* I benchmarked PII/350MHz with G200... MEMCPY, MEMCPYTOIO and WRITEL are on same speed ( <2% diff) */
 /* so that means that G200 speed (or AGP speed?) is our limit... I do not have benchmark to test, how */
 /* much of PCI bandwidth is used during transfers... */
-#if defined(__i386__)
+#if defined(__i386__) || defined(__x86_64__)
 #define MEMCPYTOIO_MEMCPY
 #else
 #define MEMCPYTOIO_WRITEL
index 4e056d2eadc5ce29668f4e7bbc8f49efbf9db7c2..a72eb15e9bc6f8a7060f9963d16a4eb295772bba 100644 (file)
@@ -20,6 +20,7 @@
 #include <linux/vt_kern.h>
 #include <linux/mm.h>
 #include <linux/module.h>
+#include <linux/slab.h>
 
 #include <asm/uaccess.h>
 #include <asm/system.h>
@@ -38,6 +39,14 @@ extern struct fbcon_font_desc font_vga_8x16;
 
 #define FONT_DATA ((unsigned char *)font_vga_8x16.data)
 
+/* borrowed from fbcon.c */
+#define REFCOUNT(fd)   (((int *)(fd))[-1])
+#define FNTSIZE(fd)    (((int *)(fd))[-2])
+#define FNTCHARCNT(fd) (((int *)(fd))[-3])
+#define FONT_EXTRA_WORDS 3
+
+static unsigned char *font_data[MAX_NR_CONSOLES];
+
 extern struct newport_regs *npregs;
 
 static int logo_active;
@@ -65,114 +74,117 @@ static int newport_ysize;
 static inline void newport_render_background(int xstart, int ystart,
                                             int xend, int yend, int ci)
 {
-    newport_wait();
-    npregs->set.wrmask = 0xffffffff;
-    npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
-                            NPORT_DMODE0_DOSETUP | NPORT_DMODE0_STOPX |
-                            NPORT_DMODE0_STOPY);
-    npregs->set.colori = ci;
-    npregs->set.xystarti = (xstart << 16) | ((ystart + topscan) & 0x3ff);
-    npregs->go.xyendi = ((xend + 7) << 16) | ((yend + topscan + 15) & 0x3ff);
+       newport_wait();
+       npregs->set.wrmask = 0xffffffff;
+       npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
+                                NPORT_DMODE0_DOSETUP | NPORT_DMODE0_STOPX
+                                | NPORT_DMODE0_STOPY);
+       npregs->set.colori = ci;
+       npregs->set.xystarti =
+           (xstart << 16) | ((ystart + topscan) & 0x3ff);
+       npregs->go.xyendi =
+           ((xend + 7) << 16) | ((yend + topscan + 15) & 0x3ff);
 }
 
 static inline void newport_init_cmap(void)
 {
-    unsigned short i;
-    
-    for(i = 0; i < 16; i++) {
-       newport_bfwait();
-       newport_cmap_setaddr(npregs, color_table[i]);
-       newport_cmap_setrgb(npregs,
-                           default_red[i],
-                           default_grn[i],
-                           default_blu[i]);
-    }
+       unsigned short i;
+
+       for (i = 0; i < 16; i++) {
+               newport_bfwait();
+               newport_cmap_setaddr(npregs, color_table[i]);
+               newport_cmap_setrgb(npregs,
+                                   default_red[i],
+                                   default_grn[i], default_blu[i]);
+       }
 }
 
 static inline void newport_show_logo(void)
 {
-    unsigned long i;
-
-    for(i = 0; i < LINUX_LOGO_COLORS; i++) {
-       newport_bfwait();
-       newport_cmap_setaddr(npregs, i + 0x20);
-       newport_cmap_setrgb(npregs,
-                           linux_logo_red[i],
-                           linux_logo_green[i],
-                           linux_logo_blue[i]);
-    }
-
-    newport_wait();
-    npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
-                            NPORT_DMODE0_CHOST);
-    
-    npregs->set.xystarti = ((newport_xsize - LOGO_W) << 16) | (0);
-    npregs->set.xyendi = ((newport_xsize - 1) << 16);
-    newport_wait();
-
-    for (i = 0; i < LOGO_W * LOGO_H; i++)
-       npregs->go.hostrw0 = linux_logo[i] << 24;
+       unsigned long i;
+
+       for (i = 0; i < LINUX_LOGO_COLORS; i++) {
+               newport_bfwait();
+               newport_cmap_setaddr(npregs, i + 0x20);
+               newport_cmap_setrgb(npregs,
+                                   linux_logo_red[i],
+                                   linux_logo_green[i],
+                                   linux_logo_blue[i]);
+       }
+
+       newport_wait();
+       npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
+                                NPORT_DMODE0_CHOST);
+
+       npregs->set.xystarti = ((newport_xsize - LOGO_W) << 16) | (0);
+       npregs->set.xyendi = ((newport_xsize - 1) << 16);
+       newport_wait();
+
+       for (i = 0; i < LOGO_W * LOGO_H; i++)
+               npregs->go.hostrw0 = linux_logo[i] << 24;
 }
 
 static inline void newport_clear_screen(int xstart, int ystart, int xend,
-                                       int yend, int ci) {
-    if (logo_active)
-       return;
-
-    newport_wait();
-    npregs->set.wrmask = 0xffffffff;
-    npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
-                            NPORT_DMODE0_DOSETUP | NPORT_DMODE0_STOPX |
-                            NPORT_DMODE0_STOPY);
-    npregs->set.colori = ci;
-    npregs->set.xystarti = (xstart << 16) | ystart;
-    npregs->go.xyendi = (xend << 16) | yend;
+                                       int yend, int ci)
+{
+       if (logo_active)
+               return;
+
+       newport_wait();
+       npregs->set.wrmask = 0xffffffff;
+       npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
+                                NPORT_DMODE0_DOSETUP | NPORT_DMODE0_STOPX
+                                | NPORT_DMODE0_STOPY);
+       npregs->set.colori = ci;
+       npregs->set.xystarti = (xstart << 16) | ystart;
+       npregs->go.xyendi = (xend << 16) | yend;
 }
 
 static inline void newport_clear_lines(int ystart, int yend, int ci)
 {
-    ystart = ((ystart << 4) + topscan) & 0x3ff;
-    yend = ((yend << 4) + topscan + 15) & 0x3ff;    
-    newport_clear_screen (0, ystart, 1280+63, yend, ci);
+       ystart = ((ystart << 4) + topscan) & 0x3ff;
+       yend = ((yend << 4) + topscan + 15) & 0x3ff;
+       newport_clear_screen(0, ystart, 1280 + 63, yend, ci);
 }
 
-void newport_reset (void)
+void newport_reset(void)
 {
-    unsigned short treg;    
-    int i;
-    
-    newport_wait();
-    treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
-    newport_vc2_set(npregs, VC2_IREG_CONTROL, (treg | VC2_CTRL_EVIDEO));
-
-    treg = newport_vc2_get(npregs, VC2_IREG_CENTRY);
-    newport_vc2_set(npregs, VC2_IREG_RADDR, treg);
-    npregs->set.dcbmode = (NPORT_DMODE_AVC2 | VC2_REGADDR_RAM |
-                          NPORT_DMODE_W2 | VC2_PROTOCOL);
-    for(i = 0; i < 128; i++) {
-       newport_bfwait();
-       if (i == 92 || i == 94)
-           npregs->set.dcbdata0.byshort.s1 = 0xff00;
-       else
-           npregs->set.dcbdata0.byshort.s1 = 0x0000;
-    }
-
-    newport_init_cmap();
-
-    /* turn off popup plane */
-    npregs->set.dcbmode = (DCB_XMAP0 | R_DCB_XMAP9_PROTOCOL |
-                           XM9_CRS_CONFIG | NPORT_DMODE_W1);
-    npregs->set.dcbdata0.bybytes.b3 &= ~XM9_PUPMODE;
-    npregs->set.dcbmode = (DCB_XMAP1 | R_DCB_XMAP9_PROTOCOL |
-                           XM9_CRS_CONFIG | NPORT_DMODE_W1);
-    npregs->set.dcbdata0.bybytes.b3 &= ~XM9_PUPMODE;
-    
-    topscan = 0;
-    npregs->cset.topscan = 0x3ff;
-    npregs->cset.xywin = (4096 << 16) | 4096;
-
-    /* Clear the screen. */
-    newport_clear_screen(0,0,1280+63,1024,0);
+       unsigned short treg;
+       int i;
+
+       newport_wait();
+       treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
+       newport_vc2_set(npregs, VC2_IREG_CONTROL,
+                       (treg | VC2_CTRL_EVIDEO));
+
+       treg = newport_vc2_get(npregs, VC2_IREG_CENTRY);
+       newport_vc2_set(npregs, VC2_IREG_RADDR, treg);
+       npregs->set.dcbmode = (NPORT_DMODE_AVC2 | VC2_REGADDR_RAM |
+                              NPORT_DMODE_W2 | VC2_PROTOCOL);
+       for (i = 0; i < 128; i++) {
+               newport_bfwait();
+               if (i == 92 || i == 94)
+                       npregs->set.dcbdata0.byshort.s1 = 0xff00;
+               else
+                       npregs->set.dcbdata0.byshort.s1 = 0x0000;
+       }
+
+       newport_init_cmap();
+
+       /* turn off popup plane */
+       npregs->set.dcbmode = (DCB_XMAP0 | R_DCB_XMAP9_PROTOCOL |
+                              XM9_CRS_CONFIG | NPORT_DMODE_W1);
+       npregs->set.dcbdata0.bybytes.b3 &= ~XM9_PUPMODE;
+       npregs->set.dcbmode = (DCB_XMAP1 | R_DCB_XMAP9_PROTOCOL |
+                              XM9_CRS_CONFIG | NPORT_DMODE_W1);
+       npregs->set.dcbdata0.bybytes.b3 &= ~XM9_PUPMODE;
+
+       topscan = 0;
+       npregs->cset.topscan = 0x3ff;
+       npregs->cset.xywin = (4096 << 16) | 4096;
+
+       /* Clear the screen. */
+       newport_clear_screen(0, 0, 1280 + 63, 1024, 0);
 }
 
 /*
@@ -181,395 +193,508 @@ void newport_reset (void)
  */
 void newport_get_screensize(void)
 {
-    int i,cols;
-    unsigned short ventry,treg;
-    unsigned short linetable[128]; /* should be enough */
-
-    ventry = newport_vc2_get (npregs, VC2_IREG_VENTRY);
-    newport_vc2_set(npregs, VC2_IREG_RADDR, ventry);
-    npregs->set.dcbmode = (NPORT_DMODE_AVC2 | VC2_REGADDR_RAM |
-                          NPORT_DMODE_W2 | VC2_PROTOCOL);
-    for(i = 0; i < 128; i++) {
-       newport_bfwait();
-       linetable[i] = npregs->set.dcbdata0.byshort.s1;
-    }
-
-    newport_xsize = newport_ysize = 0;
-    for (i = 0; linetable[i+1] && (i < sizeof(linetable)); i+=2) {
-       cols = 0;
-        newport_vc2_set(npregs, VC2_IREG_RADDR, linetable[i]);
-        npregs->set.dcbmode = (NPORT_DMODE_AVC2 | VC2_REGADDR_RAM |
+       int i, cols;
+       unsigned short ventry, treg;
+       unsigned short linetable[128];  /* should be enough */
+
+       ventry = newport_vc2_get(npregs, VC2_IREG_VENTRY);
+       newport_vc2_set(npregs, VC2_IREG_RADDR, ventry);
+       npregs->set.dcbmode = (NPORT_DMODE_AVC2 | VC2_REGADDR_RAM |
                               NPORT_DMODE_W2 | VC2_PROTOCOL);
-       do {
-           newport_bfwait();
-           treg = npregs->set.dcbdata0.byshort.s1;
-           if ((treg & 1) == 0)
-               cols += (treg >> 7) & 0xfe;
-           if ((treg & 0x80) == 0) {
+       for (i = 0; i < 128; i++) {
                newport_bfwait();
-               treg = npregs->set.dcbdata0.byshort.s1;
-           } 
-       } while ((treg & 0x8000) == 0);
-       if (cols) {
-           if (cols > newport_xsize)
-               newport_xsize = cols;
-           newport_ysize += linetable[i+1];
+               linetable[i] = npregs->set.dcbdata0.byshort.s1;
+       }
+
+       newport_xsize = newport_ysize = 0;
+       for (i = 0; linetable[i + 1] && (i < sizeof(linetable)); i += 2) {
+               cols = 0;
+               newport_vc2_set(npregs, VC2_IREG_RADDR, linetable[i]);
+               npregs->set.dcbmode = (NPORT_DMODE_AVC2 | VC2_REGADDR_RAM |
+                                      NPORT_DMODE_W2 | VC2_PROTOCOL);
+               do {
+                       newport_bfwait();
+                       treg = npregs->set.dcbdata0.byshort.s1;
+                       if ((treg & 1) == 0)
+                               cols += (treg >> 7) & 0xfe;
+                       if ((treg & 0x80) == 0) {
+                               newport_bfwait();
+                               treg = npregs->set.dcbdata0.byshort.s1;
+                       }
+               } while ((treg & 0x8000) == 0);
+               if (cols) {
+                       if (cols > newport_xsize)
+                               newport_xsize = cols;
+                       newport_ysize += linetable[i + 1];
+               }
        }
-    }
-    printk ("NG1: Screensize %dx%d\n",newport_xsize,newport_ysize);
+       printk("NG1: Screensize %dx%d\n", newport_xsize, newport_ysize);
 }
 
 static void newport_get_revisions(void)
 {
-    unsigned int tmp;
-    unsigned int board_rev;
-    unsigned int rex3_rev;
-    unsigned int vc2_rev;
-    unsigned int cmap_rev;
-    unsigned int xmap9_rev;
-    unsigned int bt445_rev;
-    unsigned int bitplanes;
-
-    rex3_rev = npregs->cset.status & NPORT_STAT_VERS;
-
-    npregs->set.dcbmode = (DCB_CMAP0 | NCMAP_PROTOCOL |
-                           NCMAP_REGADDR_RREG | NPORT_DMODE_W1);
-    tmp = npregs->set.dcbdata0.bybytes.b3;
-    cmap_rev = tmp & 7;
-    board_rev = (tmp >> 4) & 7;
-    bitplanes = ((board_rev > 1) && (tmp & 0x80)) ? 8 : 24; 
-
-    npregs->set.dcbmode = (DCB_CMAP1 | NCMAP_PROTOCOL |
-                           NCMAP_REGADDR_RREG | NPORT_DMODE_W1);
-    tmp = npregs->set.dcbdata0.bybytes.b3;
-    if ((tmp & 7) < cmap_rev)
-       cmap_rev = (tmp & 7);
-
-    vc2_rev = (newport_vc2_get(npregs, VC2_IREG_CONFIG) >> 5) & 7;
-
-    npregs->set.dcbmode = (DCB_XMAP0 | R_DCB_XMAP9_PROTOCOL |
-                           XM9_CRS_REVISION | NPORT_DMODE_W1);
-    xmap9_rev = npregs->set.dcbdata0.bybytes.b3 & 7;
-
-    npregs->set.dcbmode = (DCB_BT445 | BT445_PROTOCOL |
-                           BT445_CSR_ADDR_REG | NPORT_DMODE_W1);
-    npregs->set.dcbdata0.bybytes.b3 = BT445_REVISION_REG;
-    npregs->set.dcbmode = (DCB_BT445 | BT445_PROTOCOL |
-                           BT445_CSR_REVISION | NPORT_DMODE_W1);
-    bt445_rev = (npregs->set.dcbdata0.bybytes.b3 >> 4) - 0x0a;
+       unsigned int tmp;
+       unsigned int board_rev;
+       unsigned int rex3_rev;
+       unsigned int vc2_rev;
+       unsigned int cmap_rev;
+       unsigned int xmap9_rev;
+       unsigned int bt445_rev;
+       unsigned int bitplanes;
+
+       rex3_rev = npregs->cset.status & NPORT_STAT_VERS;
+
+       npregs->set.dcbmode = (DCB_CMAP0 | NCMAP_PROTOCOL |
+                              NCMAP_REGADDR_RREG | NPORT_DMODE_W1);
+       tmp = npregs->set.dcbdata0.bybytes.b3;
+       cmap_rev = tmp & 7;
+       board_rev = (tmp >> 4) & 7;
+       bitplanes = ((board_rev > 1) && (tmp & 0x80)) ? 8 : 24;
+
+       npregs->set.dcbmode = (DCB_CMAP1 | NCMAP_PROTOCOL |
+                              NCMAP_REGADDR_RREG | NPORT_DMODE_W1);
+       tmp = npregs->set.dcbdata0.bybytes.b3;
+       if ((tmp & 7) < cmap_rev)
+               cmap_rev = (tmp & 7);
+
+       vc2_rev = (newport_vc2_get(npregs, VC2_IREG_CONFIG) >> 5) & 7;
+
+       npregs->set.dcbmode = (DCB_XMAP0 | R_DCB_XMAP9_PROTOCOL |
+                              XM9_CRS_REVISION | NPORT_DMODE_W1);
+       xmap9_rev = npregs->set.dcbdata0.bybytes.b3 & 7;
+
+       npregs->set.dcbmode = (DCB_BT445 | BT445_PROTOCOL |
+                              BT445_CSR_ADDR_REG | NPORT_DMODE_W1);
+       npregs->set.dcbdata0.bybytes.b3 = BT445_REVISION_REG;
+       npregs->set.dcbmode = (DCB_BT445 | BT445_PROTOCOL |
+                              BT445_CSR_REVISION | NPORT_DMODE_W1);
+       bt445_rev = (npregs->set.dcbdata0.bybytes.b3 >> 4) - 0x0a;
 
 #define L(a)     (char)('A'+(a))
-    printk ("NG1: Revision %d, %d bitplanes, REX3 revision %c, VC2 revision %c, xmap9 revision %c, cmap revision %c, bt445 revision %c\n",
-           board_rev,bitplanes,L(rex3_rev),L(vc2_rev), L(xmap9_rev),
-           L(cmap_rev ? (cmap_rev+1):0),L(bt445_rev));
+       printk
+           ("NG1: Revision %d, %d bitplanes, REX3 revision %c, VC2 revision %c, xmap9 revision %c, cmap revision %c, bt445 revision %c\n",
+            board_rev, bitplanes, L(rex3_rev), L(vc2_rev), L(xmap9_rev),
+            L(cmap_rev ? (cmap_rev + 1) : 0), L(bt445_rev));
 #undef L
 
-    if (board_rev == 3) /* I don't know all affected revisions */
-       xcurs_correction = 21;
+       if (board_rev == 3)     /* I don't know all affected revisions */
+               xcurs_correction = 21;
 }
 
 #ifdef MODULE
 static const char *newport_startup(void)
 #else
-static const char * __init newport_startup(void)
+static const char *__init newport_startup(void)
 #endif
 {
-    struct newport_regs *p;
+       int i;
+       struct newport_regs *p;
+
+       npregs = (struct newport_regs *) (KSEG1 + 0x1f0f0000);
+
+       p = npregs;
+       p->cset.config = NPORT_CFG_GD0;
+
+       if (newport_wait()) {
+               return NULL;
+       }
 
-    npregs = (struct newport_regs *) (KSEG1 + 0x1f0f0000);
-       
-    p = npregs;
-    p->cset.config = NPORT_CFG_GD0;
+       p->set.xstarti = TESTVAL;
+       if (p->set._xstart.word != XSTI_TO_FXSTART(TESTVAL))
+               return NULL;
 
-    if(newport_wait()) {
-       return NULL;
-    }
+       for (i = 0; i < MAX_NR_CONSOLES; i++)
+               font_data[i] = FONT_DATA;
 
-    p->set.xstarti = TESTVAL; if(p->set._xstart.word != XSTI_TO_FXSTART(TESTVAL)) {
-       return NULL;
-    }
+       newport_reset();
+       newport_get_revisions();
+       newport_get_screensize();
 
-    newport_reset ();
-    newport_get_revisions();
-    newport_get_screensize();
+       /* gfx_init (display_desc); */
 
-    // gfx_init (display_desc);
-    
-    return "SGI Newport";
+       return "SGI Newport";
 }
 
 static void newport_init(struct vc_data *vc, int init)
 {
-    vc->vc_cols = newport_xsize / 8;
-    vc->vc_rows = newport_ysize / 16;
-    vc->vc_can_do_color = 1;
+       vc->vc_cols = newport_xsize / 8;
+       vc->vc_rows = newport_ysize / 16;
+       vc->vc_can_do_color = 1;
 }
 
-static void newport_clear(struct vc_data *vc, int sy, int sx, int height, int width)
+static void newport_clear(struct vc_data *vc, int sy, int sx, int height,
+                         int width)
 {
-    int xend = ((sx + width) << 3) - 1;
-    int ystart = ((sy << 4) + topscan) & 0x3ff;
-    int yend = (((sy + height) << 4) + topscan - 1) & 0x3ff;
-
-    if (logo_active)
-       return;
-    
-    if (ystart < yend) {
-       newport_clear_screen(sx << 3, ystart, xend, yend,
-                            (vc->vc_color & 0xf0) >> 4);
-    } else {
-       newport_clear_screen(sx << 3, ystart, xend, 1023,
-                            (vc->vc_color & 0xf0) >> 4);
-       newport_clear_screen(sx << 3, 0, xend, yend,
-                            (vc->vc_color & 0xf0) >> 4);
-    }
-}
+       int xend = ((sx + width) << 3) - 1;
+       int ystart = ((sy << 4) + topscan) & 0x3ff;
+       int yend = (((sy + height) << 4) + topscan - 1) & 0x3ff;
 
-static void newport_putc(struct vc_data *vc, int charattr, int ypos, int xpos)
-{
-    unsigned char *p;
-    
-    p = &FONT_DATA[(charattr & 0xff) << 4];
-    charattr = (charattr >> 8) & 0xff;
-    xpos <<= 3;
-    ypos <<= 4;
-
-    newport_render_background(xpos, ypos, xpos, ypos, (charattr & 0xf0) >> 4);
-    
-    /* Set the color and drawing mode. */
-    newport_wait();
-    npregs->set.colori = charattr & 0xf;
-    npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
-                            NPORT_DMODE0_STOPX | NPORT_DMODE0_ZPENAB |
-                            NPORT_DMODE0_L32);
-    
-    /* Set coordinates for bitmap operation. */
-    npregs->set.xystarti = (xpos << 16) | ((ypos + topscan) & 0x3ff);
-    npregs->set.xyendi = ((xpos + 7) << 16);
-    newport_wait();
-    
-    /* Go, baby, go... */
-    RENDER(npregs, p);
+       if (logo_active)
+               return;
+
+       if (ystart < yend) {
+               newport_clear_screen(sx << 3, ystart, xend, yend,
+                                    (vc->vc_color & 0xf0) >> 4);
+       } else {
+               newport_clear_screen(sx << 3, ystart, xend, 1023,
+                                    (vc->vc_color & 0xf0) >> 4);
+               newport_clear_screen(sx << 3, 0, xend, yend,
+                                    (vc->vc_color & 0xf0) >> 4);
+       }
 }
 
-static void newport_putcs(struct vc_data *vc, const unsigned short *s,
-                         int count, int ypos, int xpos)
+static void newport_putc(struct vc_data *vc, int charattr, int ypos,
+                        int xpos)
 {
-    int i;
-    int charattr;
-    unsigned char *p; 
-
-    charattr = (*s >> 8) & 0xff;
+       unsigned char *p;
 
-    xpos <<= 3;
-    ypos <<= 4;
+       p = &font_data[vc->vc_num][(charattr & 0xff) << 4];
+       charattr = (charattr >> 8) & 0xff;
+       xpos <<= 3;
+       ypos <<= 4;
 
-    if (!logo_active)
-       /* Clear the area behing the string */
-       newport_render_background(xpos, ypos, xpos + ((count-1) << 3), ypos,
+       newport_render_background(xpos, ypos, xpos, ypos,
                                  (charattr & 0xf0) >> 4);
 
-    newport_wait();
-
-    /* Set the color and drawing mode. */
-    npregs->set.colori = charattr & 0xf;
-    npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
-                            NPORT_DMODE0_STOPX | NPORT_DMODE0_ZPENAB |
-                            NPORT_DMODE0_L32);
-    
-    for (i = 0; i < count; i++, xpos += 8) {
-       p = &FONT_DATA[(s[i] & 0xff) << 4];
-
+       /* Set the color and drawing mode. */
        newport_wait();
+       npregs->set.colori = charattr & 0xf;
+       npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
+                                NPORT_DMODE0_STOPX | NPORT_DMODE0_ZPENAB |
+                                NPORT_DMODE0_L32);
 
        /* Set coordinates for bitmap operation. */
        npregs->set.xystarti = (xpos << 16) | ((ypos + topscan) & 0x3ff);
        npregs->set.xyendi = ((xpos + 7) << 16);
+       newport_wait();
 
        /* Go, baby, go... */
        RENDER(npregs, p);
-    }
 }
 
-static void newport_cursor(struct vc_data *vc, int mode)
+static void newport_putcs(struct vc_data *vc, const unsigned short *s,
+                         int count, int ypos, int xpos)
 {
-    unsigned short treg;
-    int xcurs, ycurs;
-    
-    switch (mode) {
-     case CM_ERASE:
-       treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
-       newport_vc2_set(npregs, VC2_IREG_CONTROL, (treg & ~(VC2_CTRL_ECDISP)));
-       break;
+       int i;
+       int charattr;
+       unsigned char *p;
 
-     case CM_MOVE:
-     case CM_DRAW:
-       treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
-       newport_vc2_set(npregs, VC2_IREG_CONTROL, (treg | VC2_CTRL_ECDISP));
-       xcurs = (vc->vc_pos - vc->vc_visible_origin) / 2;
-       ycurs = ((xcurs / vc->vc_cols) << 4) + 31;
-       xcurs = ((xcurs % vc->vc_cols) << 3) + xcurs_correction;
-       newport_vc2_set(npregs, VC2_IREG_CURSX, xcurs);
-       newport_vc2_set(npregs, VC2_IREG_CURSY, ycurs);
-    }
+       charattr = (*s >> 8) & 0xff;
+
+       xpos <<= 3;
+       ypos <<= 4;
+
+       if (!logo_active)
+               /* Clear the area behing the string */
+               newport_render_background(xpos, ypos,
+                                         xpos + ((count - 1) << 3), ypos,
+                                         (charattr & 0xf0) >> 4);
+
+       newport_wait();
+
+       /* Set the color and drawing mode. */
+       npregs->set.colori = charattr & 0xf;
+       npregs->set.drawmode0 = (NPORT_DMODE0_DRAW | NPORT_DMODE0_BLOCK |
+                                NPORT_DMODE0_STOPX | NPORT_DMODE0_ZPENAB |
+                                NPORT_DMODE0_L32);
+
+       for (i = 0; i < count; i++, xpos += 8) {
+               p = &font_data[vc->vc_num][(s[i] & 0xff) << 4];
+
+               newport_wait();
+
+               /* Set coordinates for bitmap operation. */
+               npregs->set.xystarti =
+                   (xpos << 16) | ((ypos + topscan) & 0x3ff);
+               npregs->set.xyendi = ((xpos + 7) << 16);
+
+               /* Go, baby, go... */
+               RENDER(npregs, p);
+       }
+}
+
+static void newport_cursor(struct vc_data *vc, int mode)
+{
+       unsigned short treg;
+       int xcurs, ycurs;
+
+       switch (mode) {
+       case CM_ERASE:
+               treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
+               newport_vc2_set(npregs, VC2_IREG_CONTROL,
+                               (treg & ~(VC2_CTRL_ECDISP)));
+               break;
+
+       case CM_MOVE:
+       case CM_DRAW:
+               treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
+               newport_vc2_set(npregs, VC2_IREG_CONTROL,
+                               (treg | VC2_CTRL_ECDISP));
+               xcurs = (vc->vc_pos - vc->vc_visible_origin) / 2;
+               ycurs = ((xcurs / vc->vc_cols) << 4) + 31;
+               xcurs = ((xcurs % vc->vc_cols) << 3) + xcurs_correction;
+               newport_vc2_set(npregs, VC2_IREG_CURSX, xcurs);
+               newport_vc2_set(npregs, VC2_IREG_CURSY, ycurs);
+       }
 }
 
 static int newport_switch(struct vc_data *vc)
 {
-    static int logo_drawn = 0;
+       static int logo_drawn = 0;
 
-    topscan = 0;
-    npregs->cset.topscan = 0x3ff;
+       topscan = 0;
+       npregs->cset.topscan = 0x3ff;
 
-    if (!logo_drawn) {
-       newport_show_logo();
-       logo_drawn = 1;
-       logo_active = 1;
-    }
+       if (!logo_drawn) {
+               newport_show_logo();
+               logo_drawn = 1;
+               logo_active = 1;
+       }
 
-    return 1;
+       return 1;
 }
 
 static int newport_blank(struct vc_data *c, int blank)
 {
-    unsigned short treg;
-    
-    if (blank == 0) {
-       /* unblank console */
-       treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
-       newport_vc2_set(npregs, VC2_IREG_CONTROL, (treg | VC2_CTRL_EDISP));
-    } else {
-       /* blank console */
-       treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
-       newport_vc2_set(npregs, VC2_IREG_CONTROL, (treg & ~(VC2_CTRL_EDISP)));
-    }
-    return 1;
+       unsigned short treg;
+
+       if (blank == 0) {
+               /* unblank console */
+               treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
+               newport_vc2_set(npregs, VC2_IREG_CONTROL,
+                               (treg | VC2_CTRL_EDISP));
+       } else {
+               /* blank console */
+               treg = newport_vc2_get(npregs, VC2_IREG_CONTROL);
+               newport_vc2_set(npregs, VC2_IREG_CONTROL,
+                               (treg & ~(VC2_CTRL_EDISP)));
+       }
+       return 1;
+}
+
+static int newport_set_font(int unit, struct console_font_op *op)
+{
+       int w = op->width;
+       int h = op->height;
+       int size = h * op->charcount;
+       int i;
+       unsigned char *new_data, *data = op->data, *p;
+
+       /* ladis: when I grow up, there will be a day... and more sizes will
+        * be supported ;-) */
+       if ((w != 8) || (h != 16)
+           || (op->charcount != 256 && op->charcount != 512))
+               return -EINVAL;
+
+       if (!(new_data = kmalloc(FONT_EXTRA_WORDS * sizeof(int) + size,
+            GFP_USER))) return -ENOMEM;
+
+       new_data += FONT_EXTRA_WORDS * sizeof(int);
+       FNTSIZE(new_data) = size;
+       FNTCHARCNT(new_data) = op->charcount;
+       REFCOUNT(new_data) = 0; /* usage counter */
+
+       p = new_data;
+       for (i = 0; i < op->charcount; i++) {
+               memcpy(p, data, h);
+               data += 32;
+               p += h;
+       }
+
+       /* check if font is already used by other console */
+       for (i = 0; i < MAX_NR_CONSOLES; i++) {
+               if (font_data[i] != FONT_DATA
+                   && FNTSIZE(font_data[i]) == size
+                   && !memcmp(font_data[i], new_data, size)) {
+                       kfree(new_data - FONT_EXTRA_WORDS * sizeof(int));
+                       /* current font is the same as the new one */
+                       if (i == unit)
+                               return 0;
+                       new_data = font_data[i];
+                       break;
+               }
+       }
+       /* old font is user font */
+       if (font_data[unit] != FONT_DATA) {
+               if (--REFCOUNT(font_data[unit]) == 0)
+                       kfree(font_data[unit] -
+                             FONT_EXTRA_WORDS * sizeof(int));
+       }
+       REFCOUNT(new_data)++;
+       font_data[unit] = new_data;
+
+       return 0;
 }
 
-static int newport_font_op(struct vc_data *vc, struct console_font_op *f)
+static int newport_set_def_font(int unit, struct console_font_op *op)
 {
-    return -ENOSYS;
+       if (font_data[unit] != FONT_DATA) {
+               if (--REFCOUNT(font_data[unit]) == 0)
+                       kfree(font_data[unit] -
+                             FONT_EXTRA_WORDS * sizeof(int));
+               font_data[unit] = FONT_DATA;
+       }
+
+       return 0;
+}
+
+static int newport_font_op(struct vc_data *vc, struct console_font_op *op)
+{
+       int unit = vc->vc_num;
+
+       switch (op->op) {
+       case KD_FONT_OP_SET:
+               return newport_set_font(unit, op);
+       case KD_FONT_OP_SET_DEFAULT:
+               return newport_set_def_font(unit, op);
+       default:
+               return -ENOSYS;
+       }
 }
 
 static int newport_set_palette(struct vc_data *vc, unsigned char *table)
 {
-    return -EINVAL;
+       return -EINVAL;
 }
 
 static int newport_scrolldelta(struct vc_data *vc, int lines)
 {
-    /* there is (nearly) no off-screen memory, so we can't scroll back */
-    return 0;
+       /* there is (nearly) no off-screen memory, so we can't scroll back */
+       return 0;
 }
 
-static int newport_scroll(struct vc_data *vc, int t, int b, int dir, int lines)
+static int newport_scroll(struct vc_data *vc, int t, int b, int dir,
+                         int lines)
 {
-    int count,x,y;
-    unsigned short *s, *d;
-    unsigned short chattr;
-
-    logo_active = 0;   /* it's time to disable the logo now.. */
+       int count, x, y;
+       unsigned short *s, *d;
+       unsigned short chattr;
+
+       logo_active = 0;        /* it's time to disable the logo now.. */
+
+       if (t == 0 && b == vc->vc_rows) {
+               if (dir == SM_UP) {
+                       topscan = (topscan + (lines << 4)) & 0x3ff;
+                       newport_clear_lines(vc->vc_rows - lines,
+                                           vc->vc_rows - 1,
+                                           (vc->vc_color & 0xf0) >> 4);
+               } else {
+                       topscan = (topscan + (-lines << 4)) & 0x3ff;
+                       newport_clear_lines(0, lines - 1,
+                                           (vc->vc_color & 0xf0) >> 4);
+               }
+               npregs->cset.topscan = (topscan - 1) & 0x3ff;
+               return 0;
+       }
 
-    if (t == 0 && b == vc->vc_rows) {
+       count = (b - t - lines) * vc->vc_cols;
        if (dir == SM_UP) {
-           topscan = (topscan + (lines << 4)) & 0x3ff;
-           newport_clear_lines (vc->vc_rows-lines,vc->vc_rows-1,
-                                (vc->vc_color & 0xf0) >> 4);
+               x = 0;
+               y = t;
+               s = (unsigned short *) (vc->vc_origin +
+                                       vc->vc_size_row * (t + lines));
+               d = (unsigned short *) (vc->vc_origin +
+                                       vc->vc_size_row * t);
+               while (count--) {
+                       chattr = scr_readw(s++);
+                       if (chattr != scr_readw(d)) {
+                               newport_putc(vc, chattr, y, x);
+                               scr_writew(chattr, d);
+                       }
+                       d++;
+                       if (++x == vc->vc_cols) {
+                               x = 0;
+                               y++;
+                       }
+               }
+               d = (unsigned short *) (vc->vc_origin +
+                                       vc->vc_size_row * (b - lines));
+               x = 0;
+               y = b - lines;
+               for (count = 0; count < (lines * vc->vc_cols); count++) {
+                       if (scr_readw(d) != vc->vc_video_erase_char) {
+                               newport_putc(vc, vc->vc_video_erase_char,
+                                            y, x);
+                               scr_writew(vc->vc_video_erase_char, d);
+                       }
+                       d++;
+                       if (++x == vc->vc_cols) {
+                               x = 0;
+                               y++;
+                       }
+               }
        } else {
-           topscan = (topscan + (-lines << 4)) & 0x3ff;
-           newport_clear_lines (0,lines-1, (vc->vc_color & 0xf0) >> 4);
-       }
-       npregs->cset.topscan = (topscan - 1) & 0x3ff;
-       return 0;
-    }
-    
-    count = (b-t-lines) * vc->vc_cols;
-    if (dir == SM_UP) {
-       x = 0; y = t;
-       s = (unsigned short *)(vc->vc_origin + vc->vc_size_row*(t+lines));
-       d = (unsigned short *)(vc->vc_origin + vc->vc_size_row*t);
-       while (count--) {
-           chattr = scr_readw (s++);
-           if (chattr != scr_readw(d)) {
-               newport_putc (vc, chattr, y, x);
-               scr_writew (chattr, d);
-           }
-           d++;
-           if (++x == vc->vc_cols) {
-               x = 0; y++;
-           }
-       }
-       d = (unsigned short *)(vc->vc_origin + vc->vc_size_row*(b-lines));
-       x = 0; y = b-lines;
-       for (count = 0; count < (lines * vc->vc_cols); count++) {
-           if (scr_readw(d) != vc->vc_video_erase_char) {
-               newport_putc (vc, vc->vc_video_erase_char, y, x);
-               scr_writew (vc->vc_video_erase_char, d);
-           }
-           d++;
-           if (++x == vc->vc_cols) {
-               x = 0; y++;
-           }
-       }
-    } else {
-       x = vc->vc_cols-1; y = b-1;
-       s = (unsigned short *)(vc->vc_origin + vc->vc_size_row*(b-lines)-2);
-       d = (unsigned short *)(vc->vc_origin + vc->vc_size_row*b-2);
-       while (count--) {
-           chattr = scr_readw (s--);
-           if (chattr != scr_readw(d)) {
-               newport_putc (vc, chattr, y, x);
-               scr_writew (chattr, d);
-           }
-           d--;
-           if (x-- == 0) {
-               x = vc->vc_cols-1; y--;
-           }
+               x = vc->vc_cols - 1;
+               y = b - 1;
+               s = (unsigned short *) (vc->vc_origin +
+                                       vc->vc_size_row * (b - lines) - 2);
+               d = (unsigned short *) (vc->vc_origin +
+                                       vc->vc_size_row * b - 2);
+               while (count--) {
+                       chattr = scr_readw(s--);
+                       if (chattr != scr_readw(d)) {
+                               newport_putc(vc, chattr, y, x);
+                               scr_writew(chattr, d);
+                       }
+                       d--;
+                       if (x-- == 0) {
+                               x = vc->vc_cols - 1;
+                               y--;
+                       }
+               }
+               d = (unsigned short *) (vc->vc_origin +
+                                       vc->vc_size_row * t);
+               x = 0;
+               y = t;
+               for (count = 0; count < (lines * vc->vc_cols); count++) {
+                       if (scr_readw(d) != vc->vc_video_erase_char) {
+                               newport_putc(vc, vc->vc_video_erase_char,
+                                            y, x);
+                               scr_writew(vc->vc_video_erase_char, d);
+                       }
+                       d++;
+                       if (++x == vc->vc_cols) {
+                               x = 0;
+                               y++;
+                       }
+               }
        }
-       d = (unsigned short *)(vc->vc_origin + vc->vc_size_row*t);
-       x = 0; y = t;
-       for (count = 0; count < (lines * vc->vc_cols); count++) {
-           if (scr_readw(d) != vc->vc_video_erase_char) {
-               newport_putc (vc, vc->vc_video_erase_char, y, x);
-               scr_writew (vc->vc_video_erase_char, d);
-           }
-           d++;
-           if (++x == vc->vc_cols) {
-               x = 0; y++;
-           }
-       }
-    }
-    return 1;
+       return 1;
 }
 
-static void newport_bmove(struct vc_data *vc, int sy, int sx, int dy, int dx, int h, int w)
+static void newport_bmove(struct vc_data *vc, int sy, int sx, int dy,
+                         int dx, int h, int w)
 {
-    short xs, ys, xe, ye, xoffs, yoffs, tmp;
-
-    xs = sx << 3; xe = ((sx+w) << 3)-1;
-    /*
-     * as bmove is only used to move stuff around in the same line
-     * (h == 1), we don't care about wrap arounds caused by topscan != 0
-     */
-    ys = ((sy << 4) + topscan) & 0x3ff; ye = (((sy+h) << 4)-1+topscan) & 0x3ff;
-    xoffs = (dx - sx) << 3;
-    yoffs = (dy - sy) << 4;
-    if (xoffs > 0) {
-       /* move to the right, exchange starting points */
-       tmp = xe; xe = xs; xs = tmp;
-    }
-    newport_wait();
-    npregs->set.drawmode0 = (NPORT_DMODE0_S2S | NPORT_DMODE0_BLOCK |
-                            NPORT_DMODE0_DOSETUP | NPORT_DMODE0_STOPX |
-                            NPORT_DMODE0_STOPY);
-    npregs->set.xystarti = (xs << 16) | ys;
-    npregs->set.xyendi = (xe << 16) | ye;
-    npregs->go.xymove = (xoffs << 16) | yoffs;
+       short xs, ys, xe, ye, xoffs, yoffs, tmp;
+
+       xs = sx << 3;
+       xe = ((sx + w) << 3) - 1;
+       /*
+        * as bmove is only used to move stuff around in the same line
+        * (h == 1), we don't care about wrap arounds caused by topscan != 0
+        */
+       ys = ((sy << 4) + topscan) & 0x3ff;
+       ye = (((sy + h) << 4) - 1 + topscan) & 0x3ff;
+       xoffs = (dx - sx) << 3;
+       yoffs = (dy - sy) << 4;
+       if (xoffs > 0) {
+               /* move to the right, exchange starting points */
+               tmp = xe;
+               xe = xs;
+               xs = tmp;
+       }
+       newport_wait();
+       npregs->set.drawmode0 = (NPORT_DMODE0_S2S | NPORT_DMODE0_BLOCK |
+                                NPORT_DMODE0_DOSETUP | NPORT_DMODE0_STOPX
+                                | NPORT_DMODE0_STOPY);
+       npregs->set.xystarti = (xs << 16) | ys;
+       npregs->set.xyendi = (xe << 16) | ye;
+       npregs->go.xymove = (xoffs << 16) | yoffs;
 }
 
 static int newport_dummy(struct vc_data *c)
 {
-    return 0;
+       return 0;
 }
 
 #define DUMMY (void *) newport_dummy
@@ -590,25 +715,32 @@ const struct consw newport_con = {
     con_set_palette:   newport_set_palette,
     con_scrolldelta:   newport_scrolldelta,
     con_set_origin:    DUMMY,
-    con_save_screen:   DUMMY,
+    con_save_screen:   DUMMY
 };
 
 #ifdef MODULE
+MODULE_LICENSE("GPL");
 
-int init_module(void) {
-    if (!newport_startup()) 
-       printk("Error loading SGI Newport Console driver\n");
-    else 
-       printk("Loading SGI Newport Console Driver\n");
-
-    take_over_console(&newport_con,0,MAX_NR_CONSOLES-1,1);
+int init_module(void)
+{
+       if (!newport_startup())
+               printk("Error loading SGI Newport Console driver\n");
+       else
+               printk("Loading SGI Newport Console Driver\n");
+       take_over_console(&newport_con, 0, MAX_NR_CONSOLES - 1, 1);
 
-    return 0;
+       return 0;
 }
 
-int cleanup_module(void) {
-    printk("Unloading SGI Newport Console Driver\n");
-    return 0;
-}
+int cleanup_module(void)
+{
+       int i;
 
+       printk("Unloading SGI Newport Console Driver\n");
+       /* free memory used by user font */
+       for (i = 0; i < MAX_NR_CONSOLES; i++)
+               newport_set_def_font(i, NULL);
+
+       return 0;
+}
 #endif
index b0397b1f82f7006dac8b034d54408af333036f42..4aa1d3f6b36cf5444134beb5e1cc8e55a57c9281 100644 (file)
@@ -49,7 +49,8 @@ enum {
        cmap_m64,       /* ATI Mach64 */
        cmap_r128,      /* ATI Rage128 */
        cmap_M3A,       /* ATI Rage Mobility M3 Head A */
-       cmap_M3B        /* ATI Rage Mobility M3 Head B */
+       cmap_M3B,       /* ATI Rage Mobility M3 Head B */
+       cmap_radeon     /* ATI Radeon */
 };
 
 struct fb_info_offb {
@@ -433,6 +434,10 @@ static void __init offb_init_fb(const char *name, const char *full_name,
                unsigned long regbase = dp->parent->addrs[2].address;
                info->cmap_adr = ioremap(regbase, 0x1FFF);
                info->cmap_type = cmap_M3B;
+       } else if (dp && !strncmp(name, "ATY,Rage6", 9)) {
+               unsigned long regbase = dp->addrs[1].address;
+               info->cmap_adr = ioremap(regbase, 0x1FFF);
+               info->cmap_type = cmap_radeon;
        } else if (!strncmp(name, "ATY,", 4)) {
                unsigned long base = address & 0xff000000UL;
                info->cmap_adr = ioremap(base + 0x7ff000, 0x1000) + 0xcc0;
@@ -669,6 +674,10 @@ static void offbcon_blank(int blank, struct fb_info *info)
                out_8(info2->cmap_adr + 0xb0, i);
                out_le32((unsigned *)(info2->cmap_adr + 0xb4), 0);
                break;
+           case cmap_radeon:
+               out_8(info2->cmap_adr + 0xb0, i);
+               out_le32((unsigned *)(info2->cmap_adr + 0xb4), 0);
+               break;
            }
        }
     else
@@ -748,6 +757,12 @@ static int offb_setcolreg(u_int regno, u_int red, u_int green, u_int blue,
        out_le32((unsigned *)(info2->cmap_adr + 0xb4),
                (red << 16 | green << 8 | blue));
        break;
+    case cmap_radeon:
+       /* Set palette index & data (could be smarter) */
+       out_8(info2->cmap_adr + 0xb0, regno);
+       out_le32((unsigned *)(info2->cmap_adr + 0xb4),
+               (red << 16 | green << 8 | blue));
+       break;
     }
 
     if (regno < 16)
@@ -783,3 +798,5 @@ static void do_install_cmap(int con, struct fb_info *info)
        fb_set_cmap(fb_default_cmap(size), 1, offb_setcolreg, info);
     }
 }
+
+MODULE_LICENSE("GPL");
index 1ba65e4468c72227dfaa68206fcaac6d47adb9a3..6a42bec215697db1e6f08d995788b1122693673b 100644 (file)
@@ -13,7 +13,7 @@
  *      DECstation related code Copyright (C) 1999, 2000, 2001 by
  *      Michael Engel <engel@unix-ag.org>, 
  *      Karsten Merker <merker@linuxtag.org> and
- *     Harald Koerfgen <harald@unix-ag.org>.
+ *     Harald Koerfgen.
  *      This file is subject to the terms and conditions of the GNU General
  *      Public License.  See the file COPYING in the main directory of this
  *      archive for more details.
@@ -27,7 +27,7 @@
 #include <linux/timer.h>
 #include <linux/mm.h>
 #include <linux/tty.h>
-#include <linux/malloc.h>
+#include <linux/slab.h>
 #include <linux/delay.h>
 #include <linux/init.h>
 #include <linux/fb.h>
@@ -429,3 +429,5 @@ int __init pmagbafb_setup(char *options)
 {
        return 0;
 }
+
+MODULE_LICENSE("GPL");
index 3b642b1733c42f2bf70f4811422ee30dda8c2c22..2c3f7e7b18705fcfef197e992c9a1295e70c2172 100644 (file)
@@ -9,7 +9,7 @@
  *      DECstation related code Copyright (C) 1999, 2000, 2001 by
  *      Michael Engel <engel@unix-ag.org>,
  *      Karsten Merker <merker@linuxtag.org> and 
- *     Harald Koerfgen <harald@unix-ag.org>.
+ *     Harald Koerfgen.
  *      This file is subject to the terms and conditions of the GNU General
  *      Public License.  See the file COPYING in the main directory of this
  *      archive for more details.
@@ -30,7 +30,7 @@
 #include <linux/timer.h>
 #include <linux/mm.h>
 #include <linux/tty.h>
-#include <linux/malloc.h>
+#include <linux/slab.h>
 #include <linux/delay.h>
 #include <linux/init.h>
 #include <linux/fb.h>
@@ -432,3 +432,5 @@ int __init pmagbbfb_setup(char *options)
 {
        return 0;
 }
+
+MODULE_LICENSE("GPL");
index 55554938758fcb00896fa9830afdc1a8409f7e31..e22c91470a502b7e9a00769e2427fc6892ab3708 100644 (file)
@@ -51,7 +51,7 @@
 #include <linux/string.h>
 #include <linux/mm.h>
 #include <linux/tty.h>
-#include <linux/malloc.h>
+#include <linux/slab.h>
 #include <linux/delay.h>
 #include <linux/config.h>
 #include <linux/interrupt.h>
@@ -299,7 +299,7 @@ static struct fb_videomode pvr2_modedb[] __initdata = {
 #define DEFMODE_VGA    2
 
 static int defmode = DEFMODE_NTSC;
-static const char *mode_option __initdata = NULL;
+static char *mode_option __initdata = NULL;
 
 /* Get the fixed part of the display */
 
@@ -1165,6 +1165,7 @@ int __init pvr2fb_setup(char *options)
 #endif
 
 #ifdef MODULE
+MODULE_LICENSE("GPL");
 module_init(pvr2fb_init);
 #endif
 module_exit(pvr2fb_exit);
index ec622c820068c362af4125071c8ef69ce1bfb684..4483b2c70c3df103396950f2055bb7d0d6f1f883 100644 (file)
@@ -593,7 +593,7 @@ static void sisfb_set_disp(int con, struct fb_var_screeninfo *var)
        struct fb_fix_screeninfo fix;
        struct display *display;
        struct display_switch *sw;
-       u32 flags;
+       long flags;
 
        if (con >= 0)
                display = &fb_display[con];
@@ -1695,7 +1695,7 @@ static int sisfb_mmap(struct fb_info *info, struct file *file,
        off += start;
        vma->vm_pgoff = off >> PAGE_SHIFT;
 
-#if defined(__i386__)
+#if defined(__i386__) || defined(__x86_64__)
        if (boot_cpu_data.x86 > 3)
                pgprot_val(vma->vm_page_prot) |= _PAGE_PCD;
 #endif
index 2e94ebfbfdef79d0e10c310282b9bee320f7c0b0..a413a3bdc8ccd42edf167e63d6c40546a1f39636 100644 (file)
@@ -529,7 +529,7 @@ static int  nomtrr = 0;
 #endif
 static int  nohwcursor = 0;
 static char __initdata fontname[40] = { 0 };
-static const char *mode_option __initdata = NULL;
+static char *mode_option __initdata = NULL;
 
 /* ------------------------------------------------------------------------- 
  *                      Hardware-specific funcions
index 0df47764883e92f5e4855898a40251873e403795..fe3db57a046e1ae4f5004dfeb49c26fa0ca62e12 100644 (file)
@@ -422,12 +422,11 @@ static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode)
 static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
                                         unsigned long *p)
 {
-       int rv;
        unsigned long ntimeout;
 
-       if ( (rv = get_user(ntimeout, p)) ||
-            (rv = put_user(sbi->exp_timeout/HZ, p)) )
-               return rv;
+       if (get_user(ntimeout, p) ||
+           put_user(sbi->exp_timeout / HZ, p))
+               return -EFAULT;
 
        if ( ntimeout > ULONG_MAX/HZ )
                sbi->exp_timeout = 0;
index 4d9852d5fd26e69c0037e9fd114305a5fbd3a2d2..d0c15648a9e7a2a66c8ad3d572a99429dac4620b 100644 (file)
@@ -2361,7 +2361,8 @@ static int devfs_notify_change (struct dentry *dentry, struct iattr *iattr)
     if (de == NULL) return -ENODEV;
     retval = inode_change_ok (inode, iattr);
     if (retval != 0) return retval;
-    inode_setattr (inode, iattr);
+    retval = inode_setattr (inode, iattr);
+    if (retval != 0) return retval;
     if ( iattr->ia_valid & (ATTR_MODE | ATTR_UID | ATTR_GID) )
        devfsd_notify_one (de, DEVFSD_NOTIFY_CHANGE, inode->i_mode,
                           inode->i_uid, inode->i_gid, fs_info);
index 1cbaaae663caf9f1275438f5e01e61b070bee1e0..401571db831e6eeb92702c8296969a367d4917ca 100644 (file)
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -350,6 +350,8 @@ struct file *open_exec(const char *name)
                if (!(nd.mnt->mnt_flags & MNT_NOEXEC) &&
                    S_ISREG(inode->i_mode)) {
                        int err = permission(inode, MAY_EXEC);
+                       if (!err && !(inode->i_mode & 0111))
+                               err = -EACCES;
                        file = ERR_PTR(err);
                        if (!err) {
                                file = dentry_open(nd.dentry, nd.mnt, O_RDONLY);
index cf855e0eebcc540e7a70507fa338ad2ab79d6385..599a10c94b210440e9c5473d4faecafe336f10c9 100644 (file)
@@ -15,6 +15,7 @@
 #include <linux/cache.h>
 #include <linux/swap.h>
 #include <linux/swapctl.h>
+#include <linux/prefetch.h>
 
 /*
  * New inode.c implementation.
@@ -125,8 +126,9 @@ static void init_once(void * foo, kmem_cache_t * cachep, unsigned long flags)
 /**
  *     __mark_inode_dirty -    internal function
  *     @inode: inode to mark
- *
- *     Mark an inode as dirty. Callers should use mark_inode_dirty.
+ *     @flags: what kind of dirty (i.e. I_DIRTY_SYNC)
+ *     Mark an inode as dirty. Callers should use mark_inode_dirty or
+ *     mark_inode_dirty_sync.
  */
  
 void __mark_inode_dirty(struct inode *inode, int flags)
@@ -405,8 +407,6 @@ static void try_to_sync_unused_inodes(void)
        spin_lock(&sb_lock);
        sb = sb_entry(super_blocks.next);
        for (; sb != sb_entry(&super_blocks); sb = sb_entry(sb->s_list.next)) {
-               if (!sb->s_dev)
-                       continue;
                spin_unlock(&sb_lock);
                if (!try_to_sync_unused_list(&sb->s_dirty))
                        return;
@@ -435,7 +435,7 @@ void write_inode_now(struct inode *inode, int sync)
                spin_unlock(&inode_lock);
        }
        else
-               printk("write_inode_now: no super block\n");
+               printk(KERN_ERR "write_inode_now: no super block\n");
 }
 
 /**
@@ -805,6 +805,8 @@ struct inode * get_empty_inode(void)
        static unsigned long last_ino;
        struct inode * inode;
 
+       spin_lock_prefetch(&inode_lock);
+       
        inode = alloc_inode();
        if (inode)
        {
index 7210319f9756ad2791a53d8bdb180e15da3b3319..62d3abd34eacb80b39e319f3047201cdc81b6429 100644 (file)
@@ -327,6 +327,7 @@ jffs_new_inode(const struct inode * dir, struct jffs_raw_inode *raw_inode,
        struct super_block * sb;
        struct inode * inode;
        struct jffs_control *c;
+       struct jffs_file *f;
 
        sb = dir->i_sb;
        inode = new_inode(sb);
@@ -350,8 +351,10 @@ jffs_new_inode(const struct inode * dir, struct jffs_raw_inode *raw_inode,
        inode->i_blksize = PAGE_SIZE;
        inode->i_blocks = (inode->i_size + 511) >> 9;
        inode->i_version = 0;
-       inode->u.generic_ip = (void *)jffs_find_file(c, raw_inode->ino);
 
+       f = jffs_find_file(c, raw_inode->ino);
+
+       inode->u.generic_ip = (void *)f;
        insert_inode_hash(inode);
 
        return inode;
@@ -577,10 +580,13 @@ jffs_readdir(struct file *filp, void *dirent, filldir_t filldir)
                filp->f_pos++;
        }
        f = ((struct jffs_file *)inode->u.generic_ip)->children;
-       for (j = 2; (j < filp->f_pos) && f; j++) {
-               f = f->sibling_next;
+
+       j=2;
+       while(f && (f->deleted || j++ < filp->f_pos )) {
+               f = f->sibling_next;
        }
-       for (; f ; f = f->sibling_next) {
+
+       while (f) {
                D3(printk("jffs_readdir(): \"%s\" ino: %u\n",
                          (f->name ? f->name : ""), f->ino));
                if (filldir(dirent, f->name, f->nsize,
@@ -590,6 +596,9 @@ jffs_readdir(struct file *filp, void *dirent, filldir_t filldir)
                        return 0;
                }
                filp->f_pos++;
+               do {
+                       f = f->sibling_next;
+               } while(f && f->deleted);
        }
        D3(printk (KERN_NOTICE "readdir(): up biglock\n"));
        up(&c->fmc->biglock);
@@ -929,11 +938,15 @@ jffs_remove(struct inode *dir, struct dentry *dentry, int type)
        }
 
        if (S_ISDIR(type)) {
-               if (del_f->children) {
-                       result = -ENOTEMPTY;
-                       goto jffs_remove_end;
+               struct jffs_file *child = del_f->children;
+               while(child) {
+                       if( !child->deleted ) {
+                               result = -ENOTEMPTY;
+                               goto jffs_remove_end;
+                       }
+                       child = child->sibling_next;
                }
-       }
+       }            
        else if (S_ISDIR(del_f->mode)) {
                D(printk("jffs_remove(): node is a directory "
                         "but it shouldn't be.\n"));
@@ -1000,9 +1013,6 @@ jffs_remove(struct inode *dir, struct dentry *dentry, int type)
        dir->i_ctime = dir->i_mtime = CURRENT_TIME;
        mark_inode_dirty(dir);
        inode->i_nlink--;
-       if (inode->i_nlink == 0) {
-               inode->u.generic_ip = 0;
-       }
        inode->i_ctime = dir->i_ctime;
        mark_inode_dirty(inode);
 
@@ -1321,11 +1331,10 @@ jffs_file_write(struct file *filp, const char *buf, size_t count,
        struct jffs_node *node;
        struct dentry *dentry = filp->f_dentry;
        struct inode *inode = dentry->d_inode;
-       unsigned char *vbuf;
        int recoverable = 0;
        size_t written = 0;
        __u32 thiscount = count;
-       loff_t pos;
+       loff_t pos = *ppos;
        int err;
 
        inode = filp->f_dentry->d_inode;
@@ -1334,20 +1343,13 @@ jffs_file_write(struct file *filp, const char *buf, size_t count,
                  "filp: 0x%p, buf: 0x%p, count: %d\n",
                  inode, inode->i_ino, filp, buf, count));
 
-       err = filp->f_error;
-       if (err) {
-               filp->f_error = 0;
-               return err;
-       }
-
-       down(&inode->i_sem);
-
+#if 0
        if (inode->i_sb->s_flags & MS_RDONLY) {
                D(printk("jffs_file_write(): MS_RDONLY\n"));
                err = -EROFS;
                goto out_isem;
        }
-
+#endif
        err = -EINVAL;
 
        if (!S_ISREG(inode->i_mode)) {
@@ -1364,24 +1366,16 @@ jffs_file_write(struct file *filp, const char *buf, size_t count,
 
        c = f->c;
 
-       if (filp->f_flags & O_APPEND)
-               pos = inode->i_size;
-       else
-               pos = *ppos;
-       
-       if (pos < 0) {
-               goto out_isem;
-       }
+       /*
+        * This will never trigger with sane page sizes.  leave it in anyway,
+        * since I'm thinking about how to merge larger writes (the current idea
+        * is to poke a thread that does the actual I/O and starts by doing a
+        * down(&inode->i_sem).  then we would need to get the page cache pages
+        * and have a list of I/O requests and do write-merging here.
+        * -- prumpf
+        */
        
-       thiscount = min_t(unsigned int,
-                       c->fmc->max_chunk_size - sizeof(struct jffs_raw_inode),
-                       count);
-
-       if (!(vbuf = kmalloc(thiscount, GFP_KERNEL))) {
-               D(printk("jffs_file_write(): failed to allocate bounce buffer. Fix me to use page cache\n"));
-               err = -ENOMEM;
-               goto out_isem;
-       }
+       thiscount = min(c->fmc->max_chunk_size - sizeof(struct jffs_raw_inode), count);
 
        D3(printk (KERN_NOTICE "file_write(): down biglock\n"));
        down(&c->fmc->biglock);
@@ -1393,17 +1387,6 @@ jffs_file_write(struct file *filp, const char *buf, size_t count,
         * <_Anarchy_> posix and reality are not interconnected on this issue
         */
        while (count) {
-
-               /* FIXME: This is entirely gratuitous use of bounce buffers.
-                  Get a clue and use the page cache. 
-                  /me wanders off to get a crash course on Linux VFS
-                  dwmw2
-               */
-               if (copy_from_user(vbuf, buf, thiscount)) {
-                       err = -EFAULT;
-                       goto out;
-               }
-               
                /* Things are going to be written so we could allocate and
                   initialize the necessary data structures now.  */
                if (!(node = (struct jffs_node *) kmalloc(sizeof(struct jffs_node),
@@ -1459,10 +1442,11 @@ jffs_file_write(struct file *filp, const char *buf, size_t count,
                   smaller node than we were expecting. There's no need for it 
                   to waste the space at the end of the flash just because it's 
                   a little smaller than what we asked for. But that's a whole
-                  new can of worms which I'm not going to open this week. dwmw2.
+                  new can of worms which I'm not going to open this week.
+                  -- dwmw2.
                */
                if ((err = jffs_write_node(c, node, &raw_inode, f->name,
-                                          (const unsigned char *)vbuf,
+                                          (const unsigned char *)buf,
                                           recoverable, f)) < 0) {
                        D(printk("jffs_file_write(): jffs_write_node() failed.\n"));
                        kfree(node);
@@ -1482,15 +1466,11 @@ jffs_file_write(struct file *filp, const char *buf, size_t count,
 
                D3(printk("jffs_file_write(): new f_pos %ld.\n", (long)pos));
 
-               thiscount = min_t(unsigned int,
-                       c->fmc->max_chunk_size - sizeof(struct jffs_raw_inode),
-                       count);
+               thiscount = min_t(unsigned int, c->fmc->max_chunk_size - sizeof(struct jffs_raw_inode), count);
        }
  out:
        D3(printk (KERN_NOTICE "file_write(): up biglock\n"));
        up(&c->fmc->biglock);
-       *ppos = pos;
-       kfree(vbuf);
 
        /* Fix things in the real inode.  */
        if (pos > inode->i_size) {
@@ -1502,15 +1482,28 @@ jffs_file_write(struct file *filp, const char *buf, size_t count,
        invalidate_inode_pages(inode);
 
  out_isem:
-       up(&inode->i_sem);
-       
-       /* What if there was an error, _and_ we've written some data. */
-       if (written)
-               return written;
-       else
-               return err;
+       return err;
 } /* jffs_file_write()  */
 
+static ssize_t
+jffs_prepare_write(struct file *filp, struct page *page,
+                  unsigned from, unsigned to)
+{
+       /* FIXME: we should detect some error conditions here */
+       
+       return 0;
+} /* jffs_prepare_write() */
+
+static ssize_t
+jffs_commit_write(struct file *filp, struct page *page,
+                 unsigned from, unsigned to)
+{
+       void *addr = page_address(page) + from;
+       /* XXX: PAGE_CACHE_SHIFT or PAGE_SHIFT */
+       loff_t pos = (page->index<<PAGE_CACHE_SHIFT) + from;
+       
+       return jffs_file_write(filp, addr, to-from, &pos);
+} /* jffs_commit_write() */
 
 /* This is our ioctl() routine.  */
 static int
@@ -1578,6 +1571,8 @@ jffs_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
 
 static struct address_space_operations jffs_address_operations = {
        readpage: jffs_readpage,
+       prepare_write: jffs_prepare_write,
+       commit_write: jffs_commit_write,
 };
 
 static int jffs_fsync(struct file *f, struct dentry *d, int datasync)
@@ -1591,10 +1586,12 @@ static int jffs_fsync(struct file *f, struct dentry *d, int datasync)
 
 static struct file_operations jffs_file_operations =
 {
+       llseek: generic_file_llseek,  /* llseek */
        read:  generic_file_read,    /* read */
-       write: jffs_file_write,      /* write */
+       write: generic_file_write,   /* write */
        ioctl: jffs_ioctl,           /* ioctl */
        mmap:  generic_file_mmap,    /* mmap */
+       open:  generic_file_open,
        fsync: jffs_fsync,
 };
 
@@ -1682,6 +1679,7 @@ jffs_read_inode(struct inode *inode)
                jffs_read_data(f, (char *)&rdev, 0, sizeof(kdev_t));
                init_special_inode(inode, inode->i_mode, kdev_t_to_nr(rdev));
        }
+
        D3(printk (KERN_NOTICE "read_inode(): up biglock\n"));
        up(&c->fmc->biglock);
 }
@@ -1690,13 +1688,23 @@ jffs_read_inode(struct inode *inode)
 void
 jffs_delete_inode(struct inode *inode)
 {
-       D3(printk("jffs_delete_inode(): inode->i_ino == %lu\n",
+       struct jffs_file *f;
+       struct jffs_control *c;
+       D1(printk("jffs_delete_inode(): inode->i_ino == %lu\n",
                  inode->i_ino));
 
        lock_kernel();
+
        inode->i_size = 0;
        inode->i_blocks = 0;
+       inode->u.generic_ip = 0;
        clear_inode(inode);
+       if (inode->i_nlink == 0) {
+               c = (struct jffs_control *) inode->i_sb->u.generic_sbp;
+               f = (struct jffs_file *) jffs_find_file (c, inode->i_ino);
+               jffs_possibly_delete_file(f);
+       }
+
        unlock_kernel();
 }
 
@@ -1725,6 +1733,9 @@ static DECLARE_FSTYPE_DEV(jffs_fs_type, "jffs", jffs_read_super);
 static int __init
 init_jffs_fs(void)
 {
+       printk("JFFS version "
+              JFFS_VERSION_STRING
+              ", (C) 1999, 2000  Axis Communications AB\n");
        return register_filesystem(&jffs_fs_type);
 }
 
index 96cea613a2230f60374309d409adc6ff86855b7e..2dd2093874e4f548157b331eadc41f4119342a88 100644 (file)
@@ -10,8 +10,7 @@
  * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
  *
- * - Based on Id: intrep.c,v 1.71 2000/10/27 16:51:29 dwmw2 Exp
- * - With the ctype() changes from v1.77.
+ * $Id: intrep.c,v 1.71.2.4 2001/01/08 23:27:02 dwmw2 Exp $
  *
  * Ported to Linux 2.3.x and MTD:
  * Copyright (C) 2000  Alexander Larsson (alex@cendio.se), Cendio Systems AB
@@ -640,10 +639,10 @@ jffs_scan_flash(struct jffs_control *c)
                                   (or from start) 
                                */
                                if (start < (pos & ~(fmc->sector_size-1))) {
-                                       D1(printk("Reducing start to 0x%x from 0x%x\n", pos & ~(fmc->sector_size-1), start));
+                                       D1(printk("Reducing start to 0x%lx from 0x%lx\n", (unsigned long)pos & ~(fmc->sector_size-1), (unsigned long)start));
                                        start = pos & ~(fmc->sector_size-1);
                                }
-                               D1(printk("Dirty space: 0x%x for 0x%x bytes\n", start, (pos - start)));
+                               D1(printk("Dirty space: 0x%lx for 0x%lx bytes\n", (unsigned long)start, (unsigned long)(pos - start)));
                                jffs_fmalloced(fmc, (__u32) start,
                                               (__u32) (pos - start), 0);
                        }
@@ -1003,12 +1002,11 @@ jffs_insert_node(struct jffs_control *c, struct jffs_file *f,
                f->atime = raw_inode->atime;
                f->mtime = raw_inode->mtime;
                f->ctime = raw_inode->ctime;
-               f->deleted = raw_inode->deleted;
        }
        else if ((f->highest_version < node->version)
                 || (node->version == 0)) {
                /* Insert at the end of the list.  I.e. this node is the
-                  oldest one so far.  */
+                  newest one so far.  */
                node->version_prev = f->version_tail;
                node->version_next = 0;
                f->version_tail->version_next = node;
@@ -1022,7 +1020,6 @@ jffs_insert_node(struct jffs_control *c, struct jffs_file *f,
                f->atime = raw_inode->atime;
                f->mtime = raw_inode->mtime;
                f->ctime = raw_inode->ctime;
-               f->deleted = raw_inode->deleted;
        }
        else if (f->version_head->version > node->version) {
                /* Insert at the bottom of the list.  */
@@ -1033,9 +1030,6 @@ jffs_insert_node(struct jffs_control *c, struct jffs_file *f,
                if (!f->name) {
                        update_name = 1;
                }
-               if (raw_inode->deleted) {
-                       f->deleted = raw_inode->deleted;
-               }
        }
        else {
                struct jffs_node *n;
@@ -1059,6 +1053,11 @@ jffs_insert_node(struct jffs_control *c, struct jffs_file *f,
                }
        }
 
+       /* Deletion is irreversible. If any 'deleted' node is ever
+          written, the file is deleted */
+       if (raw_inode->deleted)
+               f->deleted = raw_inode->deleted;
+
        /* Perhaps update the name.  */
        if (raw_inode->nsize && update_name && name && *name && (name != f->name)) {
                if (f->name) {
@@ -1083,16 +1082,15 @@ jffs_insert_node(struct jffs_control *c, struct jffs_file *f,
                if (insert_into_tree) {
                        jffs_insert_file_into_tree(f);
                }
-               if (f->deleted) {
-                       /* Mark all versions of the node as obsolete.  */
-                       jffs_possibly_delete_file(f);
-               }
-               else {
-                       if (node->data_size || node->removed_size) {
-                               jffs_update_file(f, node);
-                       }
-                       jffs_remove_redundant_nodes(f);
+               /* Once upon a time, we would call jffs_possibly_delete_file()
+                  here. That causes an oops if someone's still got the file
+                  open, so now we only do it in jffs_delete_inode()
+                  -- dwmw2
+               */
+               if (node->data_size || node->removed_size) {
+                       jffs_update_file(f, node);
                }
+               jffs_remove_redundant_nodes(f);
 
                jffs_garbage_collect_trigger(c);
 
@@ -1331,7 +1329,7 @@ jffs_find_child(struct jffs_file *dir, const char *name, int len)
        D3(printk("jffs_find_child()\n"));
 
        for (f = dir->children; f; f = f->sibling_next) {
-               if (f->name
+               if (!f->deleted && f->name
                    && !strncmp(f->name, name, len)
                    && f->name[len] == '\0') {
                        break;
@@ -1573,6 +1571,8 @@ jffs_write_node(struct jffs_control *c, struct jffs_node *node,
                }
                pos += total_name_size;
        }
+       if (raw_inode->deleted)
+               f->deleted = 1;
 
        /* Step 3: Append the actual data, if any.  */
        if (raw_inode->dsize) {
@@ -2654,8 +2654,8 @@ jffs_garbage_collect_next(struct jffs_control *c)
           what's available */
        if (size > JFFS_PAD(node->data_size) + total_name_size + 
            sizeof(struct jffs_raw_inode) + extra_available) {
-               D1(printk("Reducing size of new node from %d to %ld to avoid "
-                      "catching our tail\n", size, 
+               D1(printk("Reducing size of new node from %d to %d to avoid "
+                         "catching our tail\n", size,
                          JFFS_PAD(node->data_size) + JFFS_PAD(node->name_size) + 
                          sizeof(struct jffs_raw_inode) + extra_available));
                D1(printk("space_needed = %d, extra_available = %d\n", 
index 2f0a94bc2efafa70e95e2da7745a8f60f6ed353a..6aa5bd2fa2f6ac1c03e4b4966ae7804f04b7fc05 100644 (file)
@@ -631,11 +631,11 @@ jffs_flash_erasable_size(struct mtd_info *mtd, __u32 offset, __u32 size)
                return -1;
        }
        else if (offset > mtd->size) {
-               printk(KERN_WARNING "jffs_flash_erasable_size given offset off the end of device (%x > %lx)\n", offset, mtd->size);
+               printk(KERN_WARNING "jffs_flash_erasable_size given offset off the end of device (%x > %x)\n", offset, mtd->size);
                return -2;
        }
        else if (offset + size > mtd->size) {
-               printk(KERN_WARNING "jffs_flash_erasable_size() given length which runs off the end of device (ofs %x + len %x = %x, > %lx)\n", offset,size, offset+size, mtd->size);
+               printk(KERN_WARNING "jffs_flash_erasable_size() given length which runs off the end of device (ofs %x + len %x = %x, > %x)\n", offset,size, offset+size, mtd->size);
                return -3;
        }
 
diff --git a/fs/jffs2/Makefile b/fs/jffs2/Makefile
new file mode 100644 (file)
index 0000000..ba4bc07
--- /dev/null
@@ -0,0 +1,38 @@
+#
+# Makefile for the linux Journalling Flash FileSystem (JFFS) routines.
+#
+# $Id: Makefile,v 1.21 2001/03/25 22:36:12 dwmw2 Exp $
+#
+# Note! Dependencies are done automagically by 'make dep', which also
+# removes any old dependencies. DON'T put your own dependencies here
+# unless it's something special (ie not a .c file).
+#
+# Note 2! The CFLAGS definitions are now in the main makefile...
+
+ifndef CONFIG_JFFS2_FS
+
+CC += -I$(shell pwd)/../../include
+
+obj-m := jffs2.o comprmod.o
+# We're being invoked outside a normal kernel build. Fake it
+EXTRA_CFLAGS= -DCONFIG_JFFS2_FS_DEBUG=1 -g
+endif
+
+obj-$(CONFIG_JFFS2_FS) += jffs2.o
+
+COMPR_OBJS     := compr.o compr_rubin.o compr_rtime.o pushpull.o \
+                       compr_zlib.o zlib.o
+JFFS2_OBJS     := crc32.o dir.o file.o ioctl.o nodelist.o malloc.o \
+       read.o nodemgmt.o readinode.o super.o write.o scan.o gc.o \
+       symlink.o build.o erase.o background.o
+
+jffs2-objs     := $(COMPR_OBJS) $(JFFS2_OBJS)
+comprmod-objs  := $(COMPR_OBJS) comprtest.o
+
+jffs2.o: $(jffs2-objs)
+       $(LD) -r -o $@ $(jffs2-objs)
+
+comprmod.o: $(comprmod-objs)
+       $(LD) -r -o $@ $(comprmod-objs)
+
+include $(TOPDIR)/Rules.make
diff --git a/fs/jffs2/TODO b/fs/jffs2/TODO
new file mode 100644 (file)
index 0000000..f5066b1
--- /dev/null
@@ -0,0 +1,20 @@
+$Id: TODO,v 1.3 2001/03/01 23:26:48 dwmw2 Exp $
+
+ - disable compression in commit_write()? Or at least optimise the 'always write
+       whole page' bit.
+ - fix zlib. It's ugly as hell and there are at least three copies in the kernel tree
+ - fine-tune the allocation / GC thresholds
+ - chattr support - turning on/off and tuning compression per-inode
+ - checkpointing (do we need this? scan is quite fast)
+ - make the scan code populate real inodes so read_inode just after 
+       mount doesn't have to read the flash twice for large files.
+       Make this a per-inode option, changable with chattr, so you can
+       decide which inodes should be in-core immediately after mount.
+ - stop it depending on a block device. mount(8) needs a change for this.
+ - make it work on NAND flash. We need to know when we can GC
+       deletion dirents, etc. And think about holes/truncation. It can
+       all be done reasonably simply, but it need implementing.
+ - NAND flash will require new dirent/dnode structures on the medium with
+       ECC data in rather than just the CRC we're using ATM.
+ - test, test, test
+
diff --git a/fs/jffs2/background.c b/fs/jffs2/background.c
new file mode 100644 (file)
index 0000000..fe2df8c
--- /dev/null
@@ -0,0 +1,176 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: background.c,v 1.10 2001/03/15 15:38:23 dwmw2 Exp $
+ *
+ */
+
+#define __KERNEL_SYSCALLS__
+
+#include <linux/kernel.h>
+#include <linux/sched.h>
+#include <linux/unistd.h>
+#include <linux/jffs2.h>
+#include <linux/mtd/mtd.h>
+#include <linux/interrupt.h>
+#include <linux/smp_lock.h>
+#include "nodelist.h"
+
+
+static int jffs2_garbage_collect_thread(void *);
+static int thread_should_wake(struct jffs2_sb_info *c);
+
+void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c)
+{
+       spin_lock_bh(&c->erase_completion_lock);
+        if (c->gc_task && thread_should_wake(c))
+                send_sig(SIGHUP, c->gc_task, 1);
+       spin_unlock_bh(&c->erase_completion_lock);
+}
+
+int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c)
+{
+       pid_t pid;
+       int ret = 0;
+       init_MUTEX_LOCKED(&c->gc_thread_sem);
+       init_completion(&c->gc_thread_exit);
+       
+       pid = kernel_thread(jffs2_garbage_collect_thread, c, CLONE_FS|CLONE_FILES);
+       if (pid < 0) {
+               printk(KERN_WARNING "fork failed for JFFS2 garbage collect thread: %d\n", -pid);
+               ret = pid;
+       } else {
+               /* Wait for it... */
+               D1(printk(KERN_DEBUG "JFFS2: Garbage collect thread is pid %d\n", pid));
+               down(&c->gc_thread_sem);
+       }
+       up(&c->gc_thread_sem);
+       return ret;
+}
+
+void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c)
+{
+       spin_lock_bh(&c->erase_completion_lock);
+       if (c->gc_task) {
+               D1(printk(KERN_DEBUG "jffs2: Killing GC task %d\n", c->gc_task->pid));
+               send_sig(SIGKILL, c->gc_task, 1);
+       }
+       spin_unlock_bh(&c->erase_completion_lock);
+       down(&c->gc_thread_sem);
+       wait_for_completion(&c->gc_thread_exit);
+}
+
+static int jffs2_garbage_collect_thread(void *_c)
+{
+       struct jffs2_sb_info *c = _c;
+
+       daemonize();
+       current->tty = NULL;
+       c->gc_task = current;
+       up(&c->gc_thread_sem);
+
+        sprintf(current->comm, "jffs2_gcd_mtd%d", c->mtd->index);
+
+       /* FIXME in the 2.2 backport */
+       current->nice = 10;
+
+       for (;;) {
+               spin_lock_irq(&current->sigmask_lock);
+               siginitsetinv (&current->blocked, sigmask(SIGHUP) | sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGCONT));
+               recalc_sigpending(current);
+               spin_unlock_irq(&current->sigmask_lock);
+
+               if (!thread_should_wake(c)) {
+                        set_current_state (TASK_INTERRUPTIBLE);
+                       D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread sleeping...\n"));
+               }
+                
+                schedule(); /* Yes, we do this even if we want to go
+                              on immediately - we're a low priority 
+                              background task. */
+
+                /* Put_super will send a SIGKILL and then wait on the sem. 
+                 */
+                while (signal_pending(current)) {
+                        siginfo_t info;
+                        unsigned long signr;
+
+                        spin_lock_irq(&current->sigmask_lock);
+                        signr = dequeue_signal(&current->blocked, &info);
+                        spin_unlock_irq(&current->sigmask_lock);
+
+                        switch(signr) {
+                        case SIGSTOP:
+                                D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGSTOP received.\n"));
+                                set_current_state(TASK_STOPPED);
+                                schedule();
+                                break;
+
+                        case SIGKILL:
+                                D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGKILL received.\n"));
+                               spin_lock_bh(&c->erase_completion_lock);
+                                c->gc_task = NULL;
+                               spin_unlock_bh(&c->erase_completion_lock);
+                               up(&c->gc_thread_sem);
+                               complete_and_exit(&c->gc_thread_exit,0 );
+
+                       case SIGHUP:
+                               D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): SIGHUP received.\n"));
+                               break;
+                       default:
+                               D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): signal %ld received\n", signr));
+
+                        }
+                }
+               /* We don't want SIGHUP to interrupt us. STOP and KILL are OK though. */
+               spin_lock_irq(&current->sigmask_lock);
+               siginitsetinv (&current->blocked, sigmask(SIGKILL) | sigmask(SIGSTOP) | sigmask(SIGCONT));
+               recalc_sigpending(current);
+               spin_unlock_irq(&current->sigmask_lock);
+
+               D1(printk(KERN_DEBUG "jffs2_garbage_collect_thread(): pass\n"));
+               jffs2_garbage_collect_pass(c);
+       }
+}
+
+static int thread_should_wake(struct jffs2_sb_info *c)
+{
+       D1(printk(KERN_DEBUG "thread_should_wake(): nr_free_blocks %d, nr_erasing_blocks %d, dirty_size 0x%x\n", 
+                 c->nr_free_blocks, c->nr_erasing_blocks, c->dirty_size));
+       if (c->nr_free_blocks + c->nr_erasing_blocks < JFFS2_RESERVED_BLOCKS_GCTRIGGER &&
+           c->dirty_size > c->sector_size)
+               return 1;
+       else 
+               return 0;
+}
diff --git a/fs/jffs2/build.c b/fs/jffs2/build.c
new file mode 100644 (file)
index 0000000..30ef908
--- /dev/null
@@ -0,0 +1,257 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: build.c,v 1.16 2001/03/15 15:38:23 dwmw2 Exp $
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/jffs2.h>
+#include <linux/slab.h>
+#include "nodelist.h"
+
+int jffs2_build_inode_pass1(struct jffs2_sb_info *, struct jffs2_inode_cache *);
+int jffs2_build_remove_unlinked_inode(struct jffs2_sb_info *, struct jffs2_inode_cache *);
+
+
+#define for_each_inode(i, c, ic) for (i=0; i<INOCACHE_HASHSIZE; i++) for (ic=c->inocache_list[i]; ic; ic=ic->next) 
+
+/* Scan plan:
+ - Scan physical nodes. Build map of inodes/dirents. Allocate inocaches as we go
+ - Scan directory tree from top down, setting nlink in inocaches
+ - Scan inocaches for inodes with nlink==0
+*/
+int jffs2_build_filesystem(struct jffs2_sb_info *c)
+{
+       int ret;
+       int i;
+       struct jffs2_inode_cache *ic;
+
+       /* First, scan the medium and build all the inode caches with
+          lists of physical nodes */
+       ret = jffs2_scan_medium(c);
+       if (ret)
+               return ret;
+
+       D1(printk(KERN_DEBUG "Scanned flash completely\n"));
+       /* Now build the data map for each inode, marking obsoleted nodes
+          as such, and also increase nlink of any children. */
+       for_each_inode(i, c, ic) {
+               D1(printk(KERN_DEBUG "Pass 1: ino #%u\n", ic->ino));
+               ret = jffs2_build_inode_pass1(c, ic);
+               if (ret) {
+                       D1(printk(KERN_WARNING "Eep. jffs2_build_inode_pass1 for ino %d returned %d\n", ic->ino, ret));
+                       return ret;
+               }
+       }
+       D1(printk(KERN_DEBUG "Pass 1 complete\n"));
+
+       /* Next, scan for inodes with nlink == 0 and remove them. If
+          they were directories, then decrement the nlink of their
+          children too, and repeat the scan. As that's going to be
+          a fairly uncommon occurrence, it's not so evil to do it this
+          way. Recursion bad. */
+       do { 
+               D1(printk(KERN_DEBUG "Pass 2 (re)starting\n"));
+               ret = 0;
+               for_each_inode(i, c, ic) {
+                       D1(printk(KERN_DEBUG "Pass 2: ino #%u, nlink %d, ic %p, nodes %p\n", ic->ino, ic->nlink, ic, ic->nodes));
+                       if (ic->nlink)
+                               continue;
+                       
+                       ret = jffs2_build_remove_unlinked_inode(c, ic);
+                       if (ret)
+                               break;
+               /* -EAGAIN means the inode's nlink was zero, so we deleted it,
+                  and furthermore that it had children and their nlink has now
+                  gone to zero too. So we have to restart the scan. */
+               } 
+       } while(ret == -EAGAIN);
+       
+       D1(printk(KERN_DEBUG "Pass 2 complete\n"));
+       
+       /* Finally, we can scan again and free the dirent nodes and scan_info structs */
+       for_each_inode(i, c, ic) {
+               struct jffs2_scan_info *scan = ic->scan;
+               struct jffs2_full_dirent *fd;
+               D1(printk(KERN_DEBUG "Pass 3: ino #%u, ic %p, nodes %p\n", ic->ino, ic, ic->nodes));
+               if (!scan) {
+                       if (ic->nlink) {
+                               D1(printk(KERN_WARNING "Why no scan struct for ino #%u which has nlink %d?\n", ic->ino, ic->nlink));
+                       }
+                       continue;
+               }
+               ic->scan = NULL;
+               while(scan->dents) {
+                       fd = scan->dents;
+                       scan->dents = fd->next;
+                       jffs2_free_full_dirent(fd);
+               }
+               kfree(scan);
+       }
+       D1(printk(KERN_DEBUG "Pass 3 complete\n"));
+
+       return ret;
+}
+       
+int jffs2_build_inode_pass1(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
+{
+       struct jffs2_tmp_dnode_info *tn;
+       struct jffs2_full_dirent *fd;
+       struct jffs2_node_frag *fraglist = NULL;
+       struct jffs2_tmp_dnode_info *metadata = NULL;
+
+       D1(printk(KERN_DEBUG "jffs2_build_inode building inode #%u\n", ic->ino));
+       if (ic->ino > c->highest_ino)
+               c->highest_ino = ic->ino;
+
+       if (!ic->scan->tmpnodes && ic->ino != 1) {
+               D1(printk(KERN_DEBUG "jffs2_build_inode: ino #%u has no data nodes!\n", ic->ino));
+       }
+       /* Build the list to make sure any obsolete nodes are marked as such */
+       while(ic->scan->tmpnodes) {
+               tn = ic->scan->tmpnodes;
+               ic->scan->tmpnodes = tn->next;
+               
+               if (metadata && tn->version > metadata->version) {
+                       D1(printk(KERN_DEBUG "jffs2_build_inode_pass1 ignoring old metadata at 0x%08x\n",
+                                 metadata->fn->raw->flash_offset &~3));
+                       
+                       jffs2_free_full_dnode(metadata->fn);
+                       jffs2_free_tmp_dnode_info(metadata);
+                       metadata = NULL;
+               }
+                       
+               if (tn->fn->size) {
+                       jffs2_add_full_dnode_to_fraglist (c, &fraglist, tn->fn);
+                       jffs2_free_tmp_dnode_info(tn);
+               } else {
+                       if (!metadata) {
+                               metadata = tn;
+                       } else {
+                               D1(printk(KERN_DEBUG "jffs2_build_inode_pass1 ignoring new metadata at 0x%08x\n",
+                                         tn->fn->raw->flash_offset &~3));
+                               
+                               jffs2_free_full_dnode(tn->fn);
+                               jffs2_free_tmp_dnode_info(tn);
+                       }
+               }
+       }
+               
+       /* OK. Now clear up */
+       if (metadata) {
+               jffs2_free_full_dnode(metadata->fn);
+               jffs2_free_tmp_dnode_info(metadata);
+       }
+       metadata = NULL;
+       
+       while (fraglist) {
+               struct jffs2_node_frag *frag;
+               frag = fraglist;
+               fraglist = fraglist->next;
+               
+               if (frag->node && !(--frag->node->frags)) {
+                       jffs2_free_full_dnode(frag->node);
+               }
+               jffs2_free_node_frag(frag);
+       }
+
+       /* Now for each child, increase nlink */
+       for(fd=ic->scan->dents; fd; fd = fd->next) {
+               struct jffs2_inode_cache *child_ic;
+               if (!fd->ino)
+                       continue;
+
+               child_ic = jffs2_get_ino_cache(c, fd->ino);
+               if (!child_ic) {
+                       printk(KERN_NOTICE "Eep. Child \"%s\" (ino #%u) of dir ino #%u doesn't exist!\n",
+                                 fd->name, fd->ino, ic->ino);
+                       continue;
+               }
+
+               if (child_ic->nlink++ && fd->type == DT_DIR) {
+                       printk(KERN_NOTICE "Child dir \"%s\" (ino #%u) of dir ino #%u appears to be a hard link\n", fd->name, fd->ino, ic->ino);
+                       /* What do we do about it? */
+               }
+               D1(printk(KERN_DEBUG "Increased nlink for child \"%s\" (ino #%u)\n", fd->name, fd->ino));
+               /* Can't free them. We might need them in pass 2 */
+       }
+       return 0;
+}
+
+int jffs2_build_remove_unlinked_inode(struct jffs2_sb_info *c, struct jffs2_inode_cache *ic)
+{
+       struct jffs2_raw_node_ref *raw;
+       struct jffs2_full_dirent *fd;
+       int ret = 0;
+
+       if(!ic->scan) {
+               D1(printk(KERN_DEBUG "ino #%u was already removed\n", ic->ino));
+               return 0;
+       }
+
+       D1(printk(KERN_DEBUG "JFFS2: Removing ino #%u with nlink == zero.\n", ic->ino));
+       
+       for (raw = ic->nodes; raw != (void *)ic; raw = raw->next_in_ino) {
+               D1(printk(KERN_DEBUG "obsoleting node at 0x%08x\n", raw->flash_offset&~3));
+               jffs2_mark_node_obsolete(c, raw);
+       }
+
+       if (ic->scan->dents) {
+               printk(KERN_NOTICE "Inode #%u was a directory with children - removing those too...\n", ic->ino);
+       
+               while(ic->scan->dents) {
+                       struct jffs2_inode_cache *child_ic;
+
+                       fd = ic->scan->dents;
+                       ic->scan->dents = fd->next;
+
+                       D1(printk(KERN_DEBUG "Removing child \"%s\", ino #%u\n",
+                                 fd->name, fd->ino));
+                       
+                       child_ic = jffs2_get_ino_cache(c, fd->ino);
+                       if (!child_ic) {
+                               printk(KERN_NOTICE "Cannot remove child \"%s\", ino #%u, because it doesn't exist\n", fd->name, fd->ino);
+                               continue;
+                       }
+                       jffs2_free_full_dirent(fd);
+                       child_ic->nlink--;
+               }
+               ret = -EAGAIN;
+       }
+       kfree(ic->scan);
+       ic->scan = NULL;
+       //      jffs2_del_ino_cache(c, ic);
+       //      jffs2_free_inode_cache(ic);
+       return ret;
+}
diff --git a/fs/jffs2/compr.c b/fs/jffs2/compr.c
new file mode 100644 (file)
index 0000000..1c09439
--- /dev/null
@@ -0,0 +1,143 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by Arjan van de Ven <arjanv@redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: compr.c,v 1.16 2001/03/15 15:38:23 dwmw2 Exp $
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/types.h>
+#include <linux/errno.h>
+#include <linux/jffs2.h>
+
+int zlib_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 *sourcelen, __u32 *dstlen);
+void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
+int rtime_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 *sourcelen, __u32 *dstlen);
+void rtime_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
+int rubinmips_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 *sourcelen, __u32 *dstlen);
+void rubinmips_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
+int dynrubin_compress(unsigned char *data_in, unsigned char *cpage_out, __u32 *sourcelen, __u32 *dstlen);
+void dynrubin_decompress(unsigned char *data_in, unsigned char *cpage_out, __u32 srclen, __u32 destlen);
+
+
+/* jffs2_compress:
+ * @data: Pointer to uncompressed data
+ * @cdata: Pointer to buffer for compressed data
+ * @datalen: On entry, holds the amount of data available for compression.
+ *     On exit, expected to hold the amount of data actually compressed.
+ * @cdatalen: On entry, holds the amount of space available for compressed
+ *     data. On exit, expected to hold the actual size of the compressed
+ *     data.
+ *
+ * Returns: Byte to be stored with data indicating compression type used.
+ * Zero is used to show that the data could not be compressed - the 
+ * compressed version was actually larger than the original.
+ *
+ * If the cdata buffer isn't large enough to hold all the uncompressed data,
+ * jffs2_compress should compress as much as will fit, and should set 
+ * *datalen accordingly to show the amount of data which were compressed.
+ */
+unsigned char jffs2_compress(unsigned char *data_in, unsigned char *cpage_out, 
+                   __u32 *datalen, __u32 *cdatalen)
+{
+       int ret;
+
+       ret = zlib_compress(data_in, cpage_out, datalen, cdatalen);
+       if (!ret) {
+               return JFFS2_COMPR_ZLIB;
+       }
+
+       ret = dynrubin_compress(data_in, cpage_out, datalen, cdatalen);
+       if (!ret) {
+               return JFFS2_COMPR_DYNRUBIN;
+       }
+
+#if 0 /* Phase this one out */
+       ret = rubinmips_compress(data_in, cpage_out, datalen, cdatalen);
+       if (!ret) {
+               return JFFS2_COMPR_RUBINMIPS;
+       }
+#endif
+       ret = rtime_compress(data_in, cpage_out, datalen, cdatalen);
+       if (!ret) {
+               return JFFS2_COMPR_RTIME;
+       }
+#if 0
+       /* We don't need to copy. Let the caller special-case the COMPR_NONE case. */
+       /* If we get here, no compression is going to work */
+       /* But we might want to use the fragmentation part -- Arjan */
+       memcpy(cpage_out,data_in,min(*datalen,*cdatalen));
+       if (*datalen > *cdatalen)
+               *datalen = *cdatalen;
+#endif         
+       return JFFS2_COMPR_NONE; /* We failed to compress */
+
+}
+
+
+int jffs2_decompress(unsigned char comprtype, unsigned char *cdata_in, 
+                    unsigned char *data_out, __u32 cdatalen, __u32 datalen)
+{
+       switch (comprtype) {
+       case JFFS2_COMPR_NONE:
+               /* This should be special-cased elsewhere, but we might as well deal with it */
+               memcpy(data_out, cdata_in, datalen);
+               break;
+
+       case JFFS2_COMPR_ZERO:
+               memset(data_out, 0, datalen);
+               break;
+
+       case JFFS2_COMPR_ZLIB:
+               zlib_decompress(cdata_in, data_out, cdatalen, datalen);
+               break;
+
+       case JFFS2_COMPR_RTIME:
+               rtime_decompress(cdata_in, data_out, cdatalen, datalen);
+               break;
+#if 1 /* Phase this one out */
+       case JFFS2_COMPR_RUBINMIPS:
+               rubinmips_decompress(cdata_in, data_out, cdatalen, datalen);
+               break;
+#endif
+       case JFFS2_COMPR_DYNRUBIN:
+               dynrubin_decompress(cdata_in, data_out, cdatalen, datalen);
+               break;
+
+       default:
+               printk(KERN_NOTICE "Unknown JFFS2 compression type 0x%02x\n", comprtype);
+               return -EIO;
+       }
+       return 0;
+}
diff --git a/fs/jffs2/compr_rtime.c b/fs/jffs2/compr_rtime.c
new file mode 100644 (file)
index 0000000..1a71020
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by Arjan van de Ven <arjanv@redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: compr_rtime.c,v 1.5 2001/03/15 15:38:23 dwmw2 Exp $
+ *
+ *
+ * Very simple lz77-ish encoder.
+ *
+ * Theory of operation: Both encoder and decoder have a list of "last
+ * occurances" for every possible source-value; after sending the
+ * first source-byte, the second byte indicated the "run" length of
+ * matches
+ *
+ * The algorithm is intended to only send "whole bytes", no bit-messing.
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/types.h>
+#include <linux/errno.h>
+#include <linux/string.h> 
+
+/* _compress returns the compressed size, -1 if bigger */
+int rtime_compress(unsigned char *data_in, unsigned char *cpage_out, 
+                  __u32 *sourcelen, __u32 *dstlen)
+{
+       int positions[256];
+       int outpos = 0;
+       int pos=0;
+
+       memset(positions,0,sizeof(positions)); 
+       
+       while (pos < (*sourcelen) && outpos <= (*dstlen)-2) {
+               int backpos, runlen=0;
+               unsigned char value;
+               
+               value = data_in[pos];
+
+               cpage_out[outpos++] = data_in[pos++];
+               
+               backpos = positions[value];
+               positions[value]=pos;
+               
+               while ((backpos < pos) && (pos < (*sourcelen)) &&
+                      (data_in[pos]==data_in[backpos++]) && (runlen<255)) {
+                       pos++;
+                       runlen++;
+               }
+               cpage_out[outpos++] = runlen;
+       }
+
+       if (outpos >= pos) {
+               /* We failed */
+               return -1;
+       }
+       
+       /* Tell the caller how much we managed to compress, and how much space it took */
+       *sourcelen = pos;
+       *dstlen = outpos;
+       return 0;
+}                 
+
+
+void rtime_decompress(unsigned char *data_in, unsigned char *cpage_out,
+                     __u32 srclen, __u32 destlen)
+{
+       int positions[256];
+       int outpos = 0;
+       int pos=0;
+       
+       memset(positions,0,sizeof(positions)); 
+       
+       while (outpos<destlen) {
+               unsigned char value;
+               int backoffs;
+               int repeat;
+               
+               value = data_in[pos++];
+               cpage_out[outpos++] = value; /* first the verbatim copied byte */
+               repeat = data_in[pos++];
+               backoffs = positions[value];
+               
+               positions[value]=outpos;
+               if (repeat) {
+                       if (backoffs + repeat >= outpos) {
+                               while(repeat) {
+                                       cpage_out[outpos++] = cpage_out[backoffs++];
+                                       repeat--;
+                               }
+                       } else {
+                               memcpy(&cpage_out[outpos],&cpage_out[backoffs],repeat);
+                               outpos+=repeat;         
+                       }
+               }
+       }               
+}                 
+
+
diff --git a/fs/jffs2/compr_rubin.c b/fs/jffs2/compr_rubin.c
new file mode 100644 (file)
index 0000000..1ce4d17
--- /dev/null
@@ -0,0 +1,326 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by Arjan van de Ven <arjanv@redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: compr_rubin.c,v 1.11 2001/03/21 16:20:48 dwmw2 Exp $
+ *
+ */
+
+#include <linux/string.h>
+#include "compr_rubin.h"
+#include "histo_mips.h"
+
+
+
+void init_rubin(struct rubin_state *rs, int div, int *bits)
+{      
+       int c;
+
+       rs->q = 0;
+       rs->p = (long) (2 * UPPER_BIT_RUBIN);
+       rs->bit_number = (long) 0;
+       rs->bit_divider = div;
+       for (c=0; c<8; c++)
+               rs->bits[c] = bits[c];
+}
+
+
+int encode(struct rubin_state *rs, long A, long B, int symbol)
+{
+
+       long i0, i1;
+       int ret;
+
+       while ((rs->q >= UPPER_BIT_RUBIN) || ((rs->p + rs->q) <= UPPER_BIT_RUBIN)) {
+               rs->bit_number++;
+               
+               ret = pushbit(&rs->pp, (rs->q & UPPER_BIT_RUBIN) ? 1 : 0, 0);
+               if (ret)
+                       return ret;
+               rs->q &= LOWER_BITS_RUBIN;
+               rs->q <<= 1;
+               rs->p <<= 1;
+       }
+       i0 = A * rs->p / (A + B);
+       if (i0 <= 0) {
+               i0 = 1;
+       }
+       if (i0 >= rs->p) {
+               i0 = rs->p - 1;
+       }
+       i1 = rs->p - i0;
+
+       if (symbol == 0)
+               rs->p = i0;
+       else {
+               rs->p = i1;
+               rs->q += i0;
+       }
+       return 0;
+}
+
+
+void end_rubin(struct rubin_state *rs)
+{                              
+
+       int i;
+
+       for (i = 0; i < RUBIN_REG_SIZE; i++) {
+               pushbit(&rs->pp, (UPPER_BIT_RUBIN & rs->q) ? 1 : 0, 1);
+               rs->q &= LOWER_BITS_RUBIN;
+               rs->q <<= 1;
+       }
+}
+
+
+void init_decode(struct rubin_state *rs, int div, int *bits)
+{
+       init_rubin(rs, div, bits);              
+
+       /* behalve lower */
+       rs->rec_q = 0;
+
+       for (rs->bit_number = 0; rs->bit_number++ < RUBIN_REG_SIZE; rs->rec_q = rs->rec_q * 2 + (long) (pullbit(&rs->pp)))
+               ;
+}
+
+
+
+int decode(struct rubin_state *rs, long A, long B)
+{
+
+       char c;
+       long i0, i1, threshold;
+       int symbol;
+       
+
+       while ((rs->q >= UPPER_BIT_RUBIN) || ((rs->p + rs->q) <= UPPER_BIT_RUBIN)) {
+               c = pullbit(&rs->pp);
+               rs->bit_number++;
+               rs->q &= LOWER_BITS_RUBIN;
+               rs->q <<= 1;
+               rs->p <<= 1;
+               rs->rec_q &= LOWER_BITS_RUBIN;
+               rs->rec_q <<= 1;
+               rs->rec_q += c;
+       };
+       i0 = A * rs->p / (A + B);
+       if (i0 <= 0) {
+               i0 = 1;
+       }
+       if (i0 >= rs->p) {
+               i0 = rs->p - 1;
+       }
+       i1 = rs->p - i0;
+
+
+       threshold = rs->q + i0;
+       if (rs->rec_q < threshold) {
+               symbol = 0;
+               rs->p = i0;
+       } else {
+               symbol = 1;
+               rs->p = i1;
+               rs->q += i0;
+       }
+       
+       return symbol;
+}
+
+
+
+static int out_byte(struct rubin_state *rs, unsigned char byte)
+{
+       int i, ret;
+       struct rubin_state rs_copy;
+       rs_copy = *rs;
+
+       for (i=0;i<8;i++) {
+               ret = encode(rs, rs->bit_divider-rs->bits[i],rs->bits[i],byte&1);
+               if (ret) {
+                       /* Failed. Restore old state */
+                       *rs = rs_copy;
+                       return ret;
+               }
+               byte=byte>>1;
+       }
+       return 0;
+}
+
+static int in_byte(struct rubin_state *rs)
+{
+       int i;
+       int result=0;
+       for (i=0;i<8;i++) {
+               result |=  decode(rs, rs->bit_divider-rs->bits[i],rs->bits[i])<<i;
+       }
+       return result;
+}
+
+
+
+int rubin_do_compress(int bit_divider, int *bits, unsigned char *data_in, 
+                     unsigned char *cpage_out, __u32 *sourcelen, __u32 *dstlen)
+       {
+       int outpos = 0;
+       int pos=0;
+       struct rubin_state rs;
+
+       init_pushpull(&rs.pp, cpage_out, *dstlen * 8, 0, 32);
+
+       init_rubin(&rs, bit_divider, bits);
+       
+       while (pos < (*sourcelen) && !out_byte(&rs, data_in[pos]))
+               pos++;
+       
+       end_rubin(&rs);
+
+       if (outpos > pos) {
+               /* We failed */
+               return -1;
+       }
+       
+       /* Tell the caller how much we managed to compress, 
+        * and how much space it took */
+       
+       outpos = (pushedbits(&rs.pp)+7)/8;
+       
+       if (outpos >= pos)
+               return -1; /* We didn't actually compress */
+       *sourcelen = pos;
+       *dstlen = outpos;
+       return 0;
+}                 
+#if 0
+/* _compress returns the compressed size, -1 if bigger */
+int rubinmips_compress(unsigned char *data_in, unsigned char *cpage_out, 
+                  __u32 *sourcelen, __u32 *dstlen)
+{
+       return rubin_do_compress(BIT_DIVIDER_MIPS, bits_mips, data_in, cpage_out, sourcelen, dstlen);
+}
+#endif
+int dynrubin_compress(unsigned char *data_in, unsigned char *cpage_out, 
+                  __u32 *sourcelen, __u32 *dstlen)
+{
+       int bits[8];
+       unsigned char histo[256];
+       int i;
+       int ret;
+       __u32 mysrclen, mydstlen;
+
+       mysrclen = *sourcelen;
+       mydstlen = *dstlen - 8;
+
+       if (*dstlen <= 12)
+               return -1;
+
+       memset(histo, 0, 256);
+       for (i=0; i<mysrclen; i++) {
+               histo[data_in[i]]++;
+       }
+       memset(bits, 0, sizeof(int)*8);
+       for (i=0; i<256; i++) {
+               if (i&128)
+                       bits[7] += histo[i];
+               if (i&64)
+                       bits[6] += histo[i];
+               if (i&32)
+                       bits[5] += histo[i];
+               if (i&16)
+                       bits[4] += histo[i];
+               if (i&8)
+                       bits[3] += histo[i];
+               if (i&4)
+                       bits[2] += histo[i];
+               if (i&2)
+                       bits[1] += histo[i];
+               if (i&1)
+                       bits[0] += histo[i];
+       }
+
+       for (i=0; i<8; i++) {
+               bits[i] = (bits[i] * 256) / mysrclen;
+               if (!bits[i]) bits[i] = 1;
+               if (bits[i] > 255) bits[i] = 255;
+               cpage_out[i] = bits[i];
+       }
+
+       ret = rubin_do_compress(256, bits, data_in, cpage_out+8, &mysrclen, &mydstlen);
+       if (ret) 
+               return ret;
+
+       /* Add back the 8 bytes we took for the probabilities */
+       mydstlen += 8;
+
+       if (mysrclen <= mydstlen) {
+               /* We compressed */
+               return -1;
+       }
+
+       *sourcelen = mysrclen;
+       *dstlen = mydstlen;
+       return 0;
+}
+
+void rubin_do_decompress(int bit_divider, int *bits, unsigned char *cdata_in, 
+                        unsigned char *page_out, __u32 srclen, __u32 destlen)
+{
+       int outpos = 0;
+       struct rubin_state rs;
+       
+       init_pushpull(&rs.pp, cdata_in, srclen, 0, 0);
+       init_decode(&rs, bit_divider, bits);
+       
+       while (outpos < destlen) {
+               page_out[outpos++] = in_byte(&rs);
+       }
+}                 
+
+
+void rubinmips_decompress(unsigned char *data_in, unsigned char *cpage_out, 
+                  __u32 sourcelen, __u32 dstlen)
+{
+       rubin_do_decompress(BIT_DIVIDER_MIPS, bits_mips, data_in, cpage_out, sourcelen, dstlen);
+}
+
+void dynrubin_decompress(unsigned char *data_in, unsigned char *cpage_out, 
+                  __u32 sourcelen, __u32 dstlen)
+{
+       int bits[8];
+       int c;
+
+       for (c=0; c<8; c++)
+               bits[c] = data_in[c];
+
+       rubin_do_decompress(256, bits, data_in+8, cpage_out, sourcelen-8, dstlen);
+}
diff --git a/fs/jffs2/compr_rubin.h b/fs/jffs2/compr_rubin.h
new file mode 100644 (file)
index 0000000..58ccc2b
--- /dev/null
@@ -0,0 +1,28 @@
+/* Rubin encoder/decoder header       */
+/* work started at   : aug   3, 1994  */
+/* last modification : aug  15, 1994  */
+/* $Id: compr_rubin.h,v 1.5 2001/02/26 13:50:01 dwmw2 Exp $ */
+
+#include "pushpull.h"
+
+#define RUBIN_REG_SIZE   16
+#define UPPER_BIT_RUBIN    (((long) 1)<<(RUBIN_REG_SIZE-1))
+#define LOWER_BITS_RUBIN   ((((long) 1)<<(RUBIN_REG_SIZE-1))-1)
+
+
+struct rubin_state {
+       unsigned long p;                
+       unsigned long q;        
+       unsigned long rec_q;
+       long bit_number;
+       struct pushpull pp;
+       int bit_divider;
+       int bits[8];
+};
+
+
+void init_rubin (struct rubin_state *rs, int div, int *bits);
+int encode (struct rubin_state *, long, long, int);
+void end_rubin (struct rubin_state *);
+void init_decode (struct rubin_state *, int div, int *bits);
+int decode (struct rubin_state *, long, long);
diff --git a/fs/jffs2/compr_zlib.c b/fs/jffs2/compr_zlib.c
new file mode 100644 (file)
index 0000000..2b1ccdd
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: compr_zlib.c,v 1.6 2001/04/18 15:04:00 dwmw2 Exp $
+ *
+ */
+
+#include "zlib.h"
+
+#ifdef __KERNEL__
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/jffs2.h>
+#include "nodelist.h"
+
+static void *zalloc(void *opaque, unsigned nr, unsigned size)
+{
+       /* How much does it request? Should we use vmalloc? Or be dynamic? */
+       return kmalloc(nr * size, GFP_KERNEL);
+}
+
+static void zfree(void *opaque, void *addr)
+{
+       kfree(addr);
+}
+#else
+#define min(x,y) ((x)<(y)?(x):(y))
+#ifndef D1
+#define D1(x)
+#endif
+#define KERN_DEBUG
+#define KERN_NOTICE
+#define KERN_WARNING
+#define printk printf
+#include <stdio.h>
+#include <asm/types.h>
+#endif
+
+       /* Plan: call deflate() with avail_in == *sourcelen, 
+               avail_out = *dstlen - 12 and flush == Z_FINISH. 
+               If it doesn't manage to finish, call it again with
+               avail_in == 0 and avail_out set to the remaining 12
+               bytes for it to clean up. 
+          Q: Is 12 bytes sufficient?
+       */
+#define STREAM_END_SPACE 12
+
+int zlib_compress(unsigned char *data_in, unsigned char *cpage_out, 
+                  __u32 *sourcelen, __u32 *dstlen)
+{
+       z_stream strm;
+       int ret;
+
+       if (*dstlen <= STREAM_END_SPACE)
+               return -1;
+
+#ifdef __KERNEL__
+       strm.zalloc = zalloc;
+       strm.zfree = zfree;
+#else
+       strm.zalloc = (void *)0;
+       strm.zfree = (void *)0;
+#endif
+
+       if (Z_OK != deflateInit(&strm, 3)) {
+               printk(KERN_WARNING "deflateInit failed\n");
+               return -1;
+       }
+       strm.next_in = data_in;
+       strm.total_in = 0;
+       
+       strm.next_out = cpage_out;
+       strm.total_out = 0;
+
+       while (strm.total_out < *dstlen - STREAM_END_SPACE && strm.total_in < *sourcelen) {
+               strm.avail_out = *dstlen - (strm.total_out + STREAM_END_SPACE);
+               strm.avail_in = min(*sourcelen-strm.total_in, strm.avail_out);
+               D1(printk(KERN_DEBUG "calling deflate with avail_in %d, avail_out %d\n",
+                         strm.avail_in, strm.avail_out));
+               ret = deflate(&strm, Z_PARTIAL_FLUSH);
+               D1(printk(KERN_DEBUG "deflate returned with avail_in %d, avail_out %d, total_in %ld, total_out %ld\n", 
+                         strm.avail_in, strm.avail_out, strm.total_in, strm.total_out));
+               if (ret != Z_OK) {
+                       D1(printk(KERN_DEBUG "deflate in loop returned %d\n", ret));
+                       deflateEnd(&strm);
+                       return -1;
+               }
+       }
+       strm.avail_out += STREAM_END_SPACE;
+       strm.avail_in = 0;
+       ret = deflate(&strm, Z_FINISH);
+       if (ret != Z_STREAM_END) {
+               D1(printk(KERN_DEBUG "final deflate returned %d\n", ret));
+               deflateEnd(&strm);
+               return -1;
+       }
+       deflateEnd(&strm);
+
+       D1(printk(KERN_DEBUG "zlib compressed %ld bytes into %ld\n", strm.total_in, strm.total_out));
+
+       if (strm.total_out >= strm.total_in)
+               return -1;
+
+
+       *dstlen = strm.total_out;
+       *sourcelen = strm.total_in;
+       return 0;
+}
+
+void zlib_decompress(unsigned char *data_in, unsigned char *cpage_out,
+                     __u32 srclen, __u32 destlen)
+{
+       z_stream strm;
+       int ret;
+
+#ifdef __KERNEL__
+       strm.zalloc = zalloc;
+       strm.zfree = zfree;
+#else
+       strm.zalloc = (void *)0;
+       strm.zfree = (void *)0;
+#endif
+
+       if (Z_OK != inflateInit(&strm)) {
+               printk(KERN_WARNING "inflateInit failed\n");
+               return;
+       }
+       strm.next_in = data_in;
+       strm.avail_in = srclen;
+       strm.total_in = 0;
+       
+       strm.next_out = cpage_out;
+       strm.avail_out = destlen;
+       strm.total_out = 0;
+
+       while((ret = inflate(&strm, Z_FINISH)) == Z_OK)
+               ;
+       if (ret != Z_STREAM_END) {
+               printk(KERN_NOTICE "inflate returned %d\n", ret);
+       }
+       inflateEnd(&strm);
+}
diff --git a/fs/jffs2/comprtest.c b/fs/jffs2/comprtest.c
new file mode 100644 (file)
index 0000000..dbc12cc
--- /dev/null
@@ -0,0 +1,307 @@
+/* $Id: comprtest.c,v 1.4 2001/02/21 14:03:20 dwmw2 Exp $ */
+
+#include <linux/kernel.h>
+#include <linux/string.h>
+#include <linux/module.h>
+#include <asm/types.h>
+#if 0
+#define TESTDATA_LEN 512
+static unsigned char testdata[TESTDATA_LEN] = {
+ 0x7f, 0x45, 0x4c, 0x46, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x02, 0x00, 0x03, 0x00, 0x01, 0x00, 0x00, 0x00, 0x60, 0x83, 0x04, 0x08, 0x34, 0x00, 0x00, 0x00,
+ 0xb0, 0x29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x34, 0x00, 0x20, 0x00, 0x06, 0x00, 0x28, 0x00,
+ 0x1e, 0x00, 0x1b, 0x00, 0x06, 0x00, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x34, 0x80, 0x04, 0x08,
+ 0x34, 0x80, 0x04, 0x08, 0xc0, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
+ 0x04, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xf4, 0x00, 0x00, 0x00, 0xf4, 0x80, 0x04, 0x08,
+ 0xf4, 0x80, 0x04, 0x08, 0x13, 0x00, 0x00, 0x00, 0x13, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
+ 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80, 0x04, 0x08,
+ 0x00, 0x80, 0x04, 0x08, 0x0d, 0x05, 0x00, 0x00, 0x0d, 0x05, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00,
+ 0x00, 0x10, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x10, 0x05, 0x00, 0x00, 0x10, 0x95, 0x04, 0x08,
+ 0x10, 0x95, 0x04, 0x08, 0xe8, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
+ 0x00, 0x10, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x58, 0x05, 0x00, 0x00, 0x58, 0x95, 0x04, 0x08,
+ 0x58, 0x95, 0x04, 0x08, 0xa0, 0x00, 0x00, 0x00, 0xa0, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00,
+ 0x04, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x08, 0x01, 0x00, 0x00, 0x08, 0x81, 0x04, 0x08,
+ 0x08, 0x81, 0x04, 0x08, 0x20, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
+ 0x04, 0x00, 0x00, 0x00, 0x2f, 0x6c, 0x69, 0x62, 0x2f, 0x6c, 0x64, 0x2d, 0x6c, 0x69, 0x6e, 0x75,
+ 0x78, 0x2e, 0x73, 0x6f, 0x2e, 0x32, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
+ 0x01, 0x00, 0x00, 0x00, 0x47, 0x4e, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
+ 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x04, 0x00, 0x00, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x69, 0x00, 0x00, 0x00,
+ 0x0c, 0x83, 0x04, 0x08, 0x81, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
+ 0x1c, 0x83, 0x04, 0x08, 0xac, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x57, 0x00, 0x00, 0x00,
+ 0x2c, 0x83, 0x04, 0x08, 0xdd, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00, 0x00,
+ 0x3c, 0x83, 0x04, 0x08, 0x2e, 0x00, 0x00, 0x00, 0x12, 0x00, 0x00, 0x00, 0x21, 0x00, 0x00, 0x00,
+ 0x4c, 0x83, 0x04, 0x08, 0x7d, 0x00, 0x00, 0x00, 0x22, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00,
+ 0x00, 0x85, 0x04, 0x08, 0x04, 0x00, 0x00, 0x00, 0x11, 0x00, 0x0e, 0x00, 0x01, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x5f, 0x5f, 0x67,
+ 0x6d, 0x6f, 0x6e, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x5f, 0x5f, 0x00, 0x6c, 0x69, 0x62, 0x63,
+ 0x2e, 0x73, 0x6f, 0x2e, 0x36, 0x00, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x00, 0x5f, 0x5f, 0x63};
+#else
+#define TESTDATA_LEN 3481
+static unsigned char testdata[TESTDATA_LEN] = {
+ 0x23, 0x69, 0x6e, 0x63, 0x6c, 0x75, 0x64, 0x65, 0x20, 0x22, 0x64, 0x62, 0x65, 0x6e, 0x63, 0x68,
+ 0x2e, 0x68, 0x22, 0x0a, 0x0a, 0x23, 0x64, 0x65, 0x66, 0x69, 0x6e, 0x65, 0x20, 0x4d, 0x41, 0x58,
+ 0x5f, 0x46, 0x49, 0x4c, 0x45, 0x53, 0x20, 0x31, 0x30, 0x30, 0x30, 0x0a, 0x0a, 0x73, 0x74, 0x61,
+ 0x74, 0x69, 0x63, 0x20, 0x63, 0x68, 0x61, 0x72, 0x20, 0x62, 0x75, 0x66, 0x5b, 0x37, 0x30, 0x30,
+ 0x30, 0x30, 0x5d, 0x3b, 0x0a, 0x65, 0x78, 0x74, 0x65, 0x72, 0x6e, 0x20, 0x69, 0x6e, 0x74, 0x20,
+ 0x6c, 0x69, 0x6e, 0x65, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x3b, 0x0a, 0x0a, 0x73, 0x74, 0x61,
+ 0x74, 0x69, 0x63, 0x20, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x7b, 0x0a, 0x09, 0x69, 0x6e,
+ 0x74, 0x20, 0x66, 0x64, 0x3b, 0x0a, 0x09, 0x69, 0x6e, 0x74, 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c,
+ 0x65, 0x3b, 0x0a, 0x7d, 0x20, 0x66, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x5b, 0x4d, 0x41, 0x58, 0x5f,
+ 0x46, 0x49, 0x4c, 0x45, 0x53, 0x5d, 0x3b, 0x0a, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x64, 0x6f,
+ 0x5f, 0x75, 0x6e, 0x6c, 0x69, 0x6e, 0x6b, 0x28, 0x63, 0x68, 0x61, 0x72, 0x20, 0x2a, 0x66, 0x6e,
+ 0x61, 0x6d, 0x65, 0x29, 0x0a, 0x7b, 0x0a, 0x09, 0x73, 0x74, 0x72, 0x75, 0x70, 0x70, 0x65, 0x72,
+ 0x28, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x29, 0x3b, 0x0a, 0x0a, 0x09, 0x69, 0x66, 0x20, 0x28, 0x75,
+ 0x6e, 0x6c, 0x69, 0x6e, 0x6b, 0x28, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x29, 0x20, 0x21, 0x3d, 0x20,
+ 0x30, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x28, 0x22, 0x28,
+ 0x25, 0x64, 0x29, 0x20, 0x75, 0x6e, 0x6c, 0x69, 0x6e, 0x6b, 0x20, 0x25, 0x73, 0x20, 0x66, 0x61,
+ 0x69, 0x6c, 0x65, 0x64, 0x20, 0x28, 0x25, 0x73, 0x29, 0x5c, 0x6e, 0x22, 0x2c, 0x20, 0x0a, 0x09,
+ 0x09, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x5f, 0x63, 0x6f, 0x75,
+ 0x6e, 0x74, 0x2c, 0x20, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x2c, 0x20, 0x73, 0x74, 0x72, 0x65, 0x72,
+ 0x72, 0x6f, 0x72, 0x28, 0x65, 0x72, 0x72, 0x6e, 0x6f, 0x29, 0x29, 0x3b, 0x0a, 0x09, 0x7d, 0x0a,
+ 0x7d, 0x0a, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x65, 0x78, 0x70, 0x61, 0x6e, 0x64, 0x5f, 0x66,
+ 0x69, 0x6c, 0x65, 0x28, 0x69, 0x6e, 0x74, 0x20, 0x66, 0x64, 0x2c, 0x20, 0x69, 0x6e, 0x74, 0x20,
+ 0x73, 0x69, 0x7a, 0x65, 0x29, 0x0a, 0x7b, 0x0a, 0x09, 0x69, 0x6e, 0x74, 0x20, 0x73, 0x3b, 0x0a,
+ 0x09, 0x77, 0x68, 0x69, 0x6c, 0x65, 0x20, 0x28, 0x73, 0x69, 0x7a, 0x65, 0x29, 0x20, 0x7b, 0x0a,
+ 0x09, 0x09, 0x73, 0x20, 0x3d, 0x20, 0x4d, 0x49, 0x4e, 0x28, 0x73, 0x69, 0x7a, 0x65, 0x6f, 0x66,
+ 0x28, 0x62, 0x75, 0x66, 0x29, 0x2c, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x29, 0x3b, 0x0a, 0x09, 0x09,
+ 0x77, 0x72, 0x69, 0x74, 0x65, 0x28, 0x66, 0x64, 0x2c, 0x20, 0x62, 0x75, 0x66, 0x2c, 0x20, 0x73,
+ 0x29, 0x3b, 0x0a, 0x09, 0x09, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x2d, 0x3d, 0x20, 0x73, 0x3b, 0x0a,
+ 0x09, 0x7d, 0x0a, 0x7d, 0x0a, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x64, 0x6f, 0x5f, 0x6f, 0x70,
+ 0x65, 0x6e, 0x28, 0x63, 0x68, 0x61, 0x72, 0x20, 0x2a, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x2c, 0x20,
+ 0x69, 0x6e, 0x74, 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x2c, 0x20, 0x69, 0x6e, 0x74, 0x20,
+ 0x73, 0x69, 0x7a, 0x65, 0x29, 0x0a, 0x7b, 0x0a, 0x09, 0x69, 0x6e, 0x74, 0x20, 0x66, 0x64, 0x2c,
+ 0x20, 0x69, 0x3b, 0x0a, 0x09, 0x69, 0x6e, 0x74, 0x20, 0x66, 0x6c, 0x61, 0x67, 0x73, 0x20, 0x3d,
+ 0x20, 0x4f, 0x5f, 0x52, 0x44, 0x57, 0x52, 0x7c, 0x4f, 0x5f, 0x43, 0x52, 0x45, 0x41, 0x54, 0x3b,
+ 0x0a, 0x09, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x20, 0x73, 0x74, 0x61, 0x74, 0x20, 0x73, 0x74,
+ 0x3b, 0x0a, 0x09, 0x73, 0x74, 0x61, 0x74, 0x69, 0x63, 0x20, 0x69, 0x6e, 0x74, 0x20, 0x63, 0x6f,
+ 0x75, 0x6e, 0x74, 0x3b, 0x0a, 0x0a, 0x09, 0x73, 0x74, 0x72, 0x75, 0x70, 0x70, 0x65, 0x72, 0x28,
+ 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x29, 0x3b, 0x0a, 0x0a, 0x09, 0x69, 0x66, 0x20, 0x28, 0x73, 0x69,
+ 0x7a, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x30, 0x29, 0x20, 0x66, 0x6c, 0x61, 0x67, 0x73, 0x20, 0x7c,
+ 0x3d, 0x20, 0x4f, 0x5f, 0x54, 0x52, 0x55, 0x4e, 0x43, 0x3b, 0x0a, 0x0a, 0x09, 0x66, 0x64, 0x20,
+ 0x3d, 0x20, 0x6f, 0x70, 0x65, 0x6e, 0x28, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x2c, 0x20, 0x66, 0x6c,
+ 0x61, 0x67, 0x73, 0x2c, 0x20, 0x30, 0x36, 0x30, 0x30, 0x29, 0x3b, 0x0a, 0x09, 0x69, 0x66, 0x20,
+ 0x28, 0x66, 0x64, 0x20, 0x3d, 0x3d, 0x20, 0x2d, 0x31, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x70,
+ 0x72, 0x69, 0x6e, 0x74, 0x66, 0x28, 0x22, 0x28, 0x25, 0x64, 0x29, 0x20, 0x6f, 0x70, 0x65, 0x6e,
+ 0x20, 0x25, 0x73, 0x20, 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x20, 0x66, 0x6f, 0x72, 0x20, 0x68,
+ 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x20, 0x25, 0x64, 0x20, 0x28, 0x25, 0x73, 0x29, 0x5c, 0x6e, 0x22,
+ 0x2c, 0x20, 0x0a, 0x09, 0x09, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x69, 0x6e, 0x65,
+ 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x2c, 0x20, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x2c, 0x20, 0x68,
+ 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x2c, 0x20, 0x73, 0x74, 0x72, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x28,
+ 0x65, 0x72, 0x72, 0x6e, 0x6f, 0x29, 0x29, 0x3b, 0x0a, 0x09, 0x09, 0x72, 0x65, 0x74, 0x75, 0x72,
+ 0x6e, 0x3b, 0x0a, 0x09, 0x7d, 0x0a, 0x09, 0x66, 0x73, 0x74, 0x61, 0x74, 0x28, 0x66, 0x64, 0x2c,
+ 0x20, 0x26, 0x73, 0x74, 0x29, 0x3b, 0x0a, 0x09, 0x69, 0x66, 0x20, 0x28, 0x73, 0x69, 0x7a, 0x65,
+ 0x20, 0x3e, 0x20, 0x73, 0x74, 0x2e, 0x73, 0x74, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x29, 0x20, 0x7b,
+ 0x0a, 0x23, 0x69, 0x66, 0x20, 0x44, 0x45, 0x42, 0x55, 0x47, 0x0a, 0x09, 0x09, 0x70, 0x72, 0x69,
+ 0x6e, 0x74, 0x66, 0x28, 0x22, 0x28, 0x25, 0x64, 0x29, 0x20, 0x65, 0x78, 0x70, 0x61, 0x6e, 0x64,
+ 0x69, 0x6e, 0x67, 0x20, 0x25, 0x73, 0x20, 0x74, 0x6f, 0x20, 0x25, 0x64, 0x20, 0x66, 0x72, 0x6f,
+ 0x6d, 0x20, 0x25, 0x64, 0x5c, 0x6e, 0x22, 0x2c, 0x20, 0x0a, 0x09, 0x09, 0x20, 0x20, 0x20, 0x20,
+ 0x20, 0x20, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x2c, 0x20, 0x66,
+ 0x6e, 0x61, 0x6d, 0x65, 0x2c, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x2c, 0x20, 0x28, 0x69, 0x6e, 0x74,
+ 0x29, 0x73, 0x74, 0x2e, 0x73, 0x74, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x29, 0x3b, 0x0a, 0x23, 0x65,
+ 0x6e, 0x64, 0x69, 0x66, 0x0a, 0x09, 0x09, 0x65, 0x78, 0x70, 0x61, 0x6e, 0x64, 0x5f, 0x66, 0x69,
+ 0x6c, 0x65, 0x28, 0x66, 0x64, 0x2c, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x2d, 0x20, 0x73, 0x74,
+ 0x2e, 0x73, 0x74, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x29, 0x3b, 0x0a, 0x09, 0x7d, 0x20, 0x65, 0x6c,
+ 0x73, 0x65, 0x20, 0x69, 0x66, 0x20, 0x28, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x3c, 0x20, 0x73, 0x74,
+ 0x2e, 0x73, 0x74, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x70, 0x72,
+ 0x69, 0x6e, 0x74, 0x66, 0x28, 0x22, 0x74, 0x72, 0x75, 0x6e, 0x63, 0x61, 0x74, 0x69, 0x6e, 0x67,
+ 0x20, 0x25, 0x73, 0x20, 0x74, 0x6f, 0x20, 0x25, 0x64, 0x20, 0x66, 0x72, 0x6f, 0x6d, 0x20, 0x25,
+ 0x64, 0x5c, 0x6e, 0x22, 0x2c, 0x20, 0x0a, 0x09, 0x09, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
+ 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x2c, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x2c, 0x20, 0x28, 0x69, 0x6e,
+ 0x74, 0x29, 0x73, 0x74, 0x2e, 0x73, 0x74, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x29, 0x3b, 0x0a, 0x09,
+ 0x09, 0x66, 0x74, 0x72, 0x75, 0x6e, 0x63, 0x61, 0x74, 0x65, 0x28, 0x66, 0x64, 0x2c, 0x20, 0x73,
+ 0x69, 0x7a, 0x65, 0x29, 0x3b, 0x0a, 0x09, 0x7d, 0x0a, 0x09, 0x66, 0x6f, 0x72, 0x20, 0x28, 0x69,
+ 0x3d, 0x30, 0x3b, 0x69, 0x3c, 0x4d, 0x41, 0x58, 0x5f, 0x46, 0x49, 0x4c, 0x45, 0x53, 0x3b, 0x69,
+ 0x2b, 0x2b, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x69, 0x66, 0x20, 0x28, 0x66, 0x74, 0x61, 0x62,
+ 0x6c, 0x65, 0x5b, 0x69, 0x5d, 0x2e, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x20, 0x3d, 0x3d, 0x20,
+ 0x30, 0x29, 0x20, 0x62, 0x72, 0x65, 0x61, 0x6b, 0x3b, 0x0a, 0x09, 0x7d, 0x0a, 0x09, 0x69, 0x66,
+ 0x20, 0x28, 0x69, 0x20, 0x3d, 0x3d, 0x20, 0x4d, 0x41, 0x58, 0x5f, 0x46, 0x49, 0x4c, 0x45, 0x53,
+ 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x28, 0x22, 0x66, 0x69,
+ 0x6c, 0x65, 0x20, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x66, 0x75, 0x6c, 0x6c, 0x20, 0x66, 0x6f,
+ 0x72, 0x20, 0x25, 0x73, 0x5c, 0x6e, 0x22, 0x2c, 0x20, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x29, 0x3b,
+ 0x0a, 0x09, 0x09, 0x65, 0x78, 0x69, 0x74, 0x28, 0x31, 0x29, 0x3b, 0x0a, 0x09, 0x7d, 0x0a, 0x09,
+ 0x66, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x5b, 0x69, 0x5d, 0x2e, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65,
+ 0x20, 0x3d, 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x3b, 0x0a, 0x09, 0x66, 0x74, 0x61, 0x62,
+ 0x6c, 0x65, 0x5b, 0x69, 0x5d, 0x2e, 0x66, 0x64, 0x20, 0x3d, 0x20, 0x66, 0x64, 0x3b, 0x0a, 0x09,
+ 0x69, 0x66, 0x20, 0x28, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x2b, 0x2b, 0x20, 0x25, 0x20, 0x31, 0x30,
+ 0x30, 0x20, 0x3d, 0x3d, 0x20, 0x30, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x70, 0x72, 0x69, 0x6e,
+ 0x74, 0x66, 0x28, 0x22, 0x2e, 0x22, 0x29, 0x3b, 0x0a, 0x09, 0x7d, 0x0a, 0x7d, 0x0a, 0x0a, 0x76,
+ 0x6f, 0x69, 0x64, 0x20, 0x64, 0x6f, 0x5f, 0x77, 0x72, 0x69, 0x74, 0x65, 0x28, 0x69, 0x6e, 0x74,
+ 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x2c, 0x20, 0x69, 0x6e, 0x74, 0x20, 0x73, 0x69, 0x7a,
+ 0x65, 0x2c, 0x20, 0x69, 0x6e, 0x74, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x29, 0x0a, 0x7b,
+ 0x0a, 0x09, 0x69, 0x6e, 0x74, 0x20, 0x69, 0x3b, 0x0a, 0x0a, 0x09, 0x69, 0x66, 0x20, 0x28, 0x62,
+ 0x75, 0x66, 0x5b, 0x30, 0x5d, 0x20, 0x3d, 0x3d, 0x20, 0x30, 0x29, 0x20, 0x6d, 0x65, 0x6d, 0x73,
+ 0x65, 0x74, 0x28, 0x62, 0x75, 0x66, 0x2c, 0x20, 0x31, 0x2c, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x6f,
+ 0x66, 0x28, 0x62, 0x75, 0x66, 0x29, 0x29, 0x3b, 0x0a, 0x0a, 0x09, 0x66, 0x6f, 0x72, 0x20, 0x28,
+ 0x69, 0x3d, 0x30, 0x3b, 0x69, 0x3c, 0x4d, 0x41, 0x58, 0x5f, 0x46, 0x49, 0x4c, 0x45, 0x53, 0x3b,
+ 0x69, 0x2b, 0x2b, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x69, 0x66, 0x20, 0x28, 0x66, 0x74, 0x61,
+ 0x62, 0x6c, 0x65, 0x5b, 0x69, 0x5d, 0x2e, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x20, 0x3d, 0x3d,
+ 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x29, 0x20, 0x62, 0x72, 0x65, 0x61, 0x6b, 0x3b, 0x0a,
+ 0x09, 0x7d, 0x0a, 0x09, 0x69, 0x66, 0x20, 0x28, 0x69, 0x20, 0x3d, 0x3d, 0x20, 0x4d, 0x41, 0x58,
+ 0x5f, 0x46, 0x49, 0x4c, 0x45, 0x53, 0x29, 0x20, 0x7b, 0x0a, 0x23, 0x69, 0x66, 0x20, 0x31, 0x0a,
+ 0x09, 0x09, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x28, 0x22, 0x28, 0x25, 0x64, 0x29, 0x20, 0x64,
+ 0x6f, 0x5f, 0x77, 0x72, 0x69, 0x74, 0x65, 0x3a, 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x20,
+ 0x25, 0x64, 0x20, 0x77, 0x61, 0x73, 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x6f, 0x70, 0x65, 0x6e, 0x20,
+ 0x73, 0x69, 0x7a, 0x65, 0x3d, 0x25, 0x64, 0x20, 0x6f, 0x66, 0x73, 0x3d, 0x25, 0x64, 0x5c, 0x6e,
+ 0x22, 0x2c, 0x20, 0x0a, 0x09, 0x09, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x69, 0x6e,
+ 0x65, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x2c, 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x2c,
+ 0x20, 0x73, 0x69, 0x7a, 0x65, 0x2c, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x29, 0x3b, 0x0a,
+ 0x23, 0x65, 0x6e, 0x64, 0x69, 0x66, 0x0a, 0x09, 0x09, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x3b,
+ 0x0a, 0x09, 0x7d, 0x0a, 0x09, 0x6c, 0x73, 0x65, 0x65, 0x6b, 0x28, 0x66, 0x74, 0x61, 0x62, 0x6c,
+ 0x65, 0x5b, 0x69, 0x5d, 0x2e, 0x66, 0x64, 0x2c, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x2c,
+ 0x20, 0x53, 0x45, 0x45, 0x4b, 0x5f, 0x53, 0x45, 0x54, 0x29, 0x3b, 0x0a, 0x09, 0x69, 0x66, 0x20,
+ 0x28, 0x77, 0x72, 0x69, 0x74, 0x65, 0x28, 0x66, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x5b, 0x69, 0x5d,
+ 0x2e, 0x66, 0x64, 0x2c, 0x20, 0x62, 0x75, 0x66, 0x2c, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x29, 0x20,
+ 0x21, 0x3d, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x70, 0x72, 0x69,
+ 0x6e, 0x74, 0x66, 0x28, 0x22, 0x77, 0x72, 0x69, 0x74, 0x65, 0x20, 0x66, 0x61, 0x69, 0x6c, 0x65,
+ 0x64, 0x20, 0x6f, 0x6e, 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x20, 0x25, 0x64, 0x5c, 0x6e,
+ 0x22, 0x2c, 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x29, 0x3b, 0x0a, 0x09, 0x7d, 0x0a, 0x7d,
+ 0x0a, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x64, 0x6f, 0x5f, 0x72, 0x65, 0x61, 0x64, 0x28, 0x69,
+ 0x6e, 0x74, 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x2c, 0x20, 0x69, 0x6e, 0x74, 0x20, 0x73,
+ 0x69, 0x7a, 0x65, 0x2c, 0x20, 0x69, 0x6e, 0x74, 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x29,
+ 0x0a, 0x7b, 0x0a, 0x09, 0x69, 0x6e, 0x74, 0x20, 0x69, 0x3b, 0x0a, 0x09, 0x66, 0x6f, 0x72, 0x20,
+ 0x28, 0x69, 0x3d, 0x30, 0x3b, 0x69, 0x3c, 0x4d, 0x41, 0x58, 0x5f, 0x46, 0x49, 0x4c, 0x45, 0x53,
+ 0x3b, 0x69, 0x2b, 0x2b, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x69, 0x66, 0x20, 0x28, 0x66, 0x74,
+ 0x61, 0x62, 0x6c, 0x65, 0x5b, 0x69, 0x5d, 0x2e, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x20, 0x3d,
+ 0x3d, 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x29, 0x20, 0x62, 0x72, 0x65, 0x61, 0x6b, 0x3b,
+ 0x0a, 0x09, 0x7d, 0x0a, 0x09, 0x69, 0x66, 0x20, 0x28, 0x69, 0x20, 0x3d, 0x3d, 0x20, 0x4d, 0x41,
+ 0x58, 0x5f, 0x46, 0x49, 0x4c, 0x45, 0x53, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x70, 0x72, 0x69,
+ 0x6e, 0x74, 0x66, 0x28, 0x22, 0x28, 0x25, 0x64, 0x29, 0x20, 0x64, 0x6f, 0x5f, 0x72, 0x65, 0x61,
+ 0x64, 0x3a, 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x20, 0x25, 0x64, 0x20, 0x77, 0x61, 0x73,
+ 0x20, 0x6e, 0x6f, 0x74, 0x20, 0x6f, 0x70, 0x65, 0x6e, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x3d, 0x25,
+ 0x64, 0x20, 0x6f, 0x66, 0x73, 0x3d, 0x25, 0x64, 0x5c, 0x6e, 0x22, 0x2c, 0x20, 0x0a, 0x09, 0x09,
+ 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x5f, 0x63, 0x6f, 0x75, 0x6e,
+ 0x74, 0x2c, 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x2c, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x2c,
+ 0x20, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x29, 0x3b, 0x0a, 0x09, 0x09, 0x72, 0x65, 0x74, 0x75,
+ 0x72, 0x6e, 0x3b, 0x0a, 0x09, 0x7d, 0x0a, 0x09, 0x6c, 0x73, 0x65, 0x65, 0x6b, 0x28, 0x66, 0x74,
+ 0x61, 0x62, 0x6c, 0x65, 0x5b, 0x69, 0x5d, 0x2e, 0x66, 0x64, 0x2c, 0x20, 0x6f, 0x66, 0x66, 0x73,
+ 0x65, 0x74, 0x2c, 0x20, 0x53, 0x45, 0x45, 0x4b, 0x5f, 0x53, 0x45, 0x54, 0x29, 0x3b, 0x0a, 0x09,
+ 0x72, 0x65, 0x61, 0x64, 0x28, 0x66, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x5b, 0x69, 0x5d, 0x2e, 0x66,
+ 0x64, 0x2c, 0x20, 0x62, 0x75, 0x66, 0x2c, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x29, 0x3b, 0x0a, 0x7d,
+ 0x0a, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x64, 0x6f, 0x5f, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x28,
+ 0x69, 0x6e, 0x74, 0x20, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x29, 0x0a, 0x7b, 0x0a, 0x09, 0x69,
+ 0x6e, 0x74, 0x20, 0x69, 0x3b, 0x0a, 0x09, 0x66, 0x6f, 0x72, 0x20, 0x28, 0x69, 0x3d, 0x30, 0x3b,
+ 0x69, 0x3c, 0x4d, 0x41, 0x58, 0x5f, 0x46, 0x49, 0x4c, 0x45, 0x53, 0x3b, 0x69, 0x2b, 0x2b, 0x29,
+ 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x69, 0x66, 0x20, 0x28, 0x66, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x5b,
+ 0x69, 0x5d, 0x2e, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x20, 0x3d, 0x3d, 0x20, 0x68, 0x61, 0x6e,
+ 0x64, 0x6c, 0x65, 0x29, 0x20, 0x62, 0x72, 0x65, 0x61, 0x6b, 0x3b, 0x0a, 0x09, 0x7d, 0x0a, 0x09,
+ 0x69, 0x66, 0x20, 0x28, 0x69, 0x20, 0x3d, 0x3d, 0x20, 0x4d, 0x41, 0x58, 0x5f, 0x46, 0x49, 0x4c,
+ 0x45, 0x53, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x28, 0x22,
+ 0x28, 0x25, 0x64, 0x29, 0x20, 0x64, 0x6f, 0x5f, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x3a, 0x20, 0x68,
+ 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x20, 0x25, 0x64, 0x20, 0x77, 0x61, 0x73, 0x20, 0x6e, 0x6f, 0x74,
+ 0x20, 0x6f, 0x70, 0x65, 0x6e, 0x5c, 0x6e, 0x22, 0x2c, 0x20, 0x0a, 0x09, 0x09, 0x20, 0x20, 0x20,
+ 0x20, 0x20, 0x20, 0x20, 0x6c, 0x69, 0x6e, 0x65, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x2c, 0x20,
+ 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x29, 0x3b, 0x0a, 0x09, 0x09, 0x72, 0x65, 0x74, 0x75, 0x72,
+ 0x6e, 0x3b, 0x0a, 0x09, 0x7d, 0x0a, 0x09, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x28, 0x66, 0x74, 0x61,
+ 0x62, 0x6c, 0x65, 0x5b, 0x69, 0x5d, 0x2e, 0x66, 0x64, 0x29, 0x3b, 0x0a, 0x09, 0x66, 0x74, 0x61,
+ 0x62, 0x6c, 0x65, 0x5b, 0x69, 0x5d, 0x2e, 0x68, 0x61, 0x6e, 0x64, 0x6c, 0x65, 0x20, 0x3d, 0x20,
+ 0x30, 0x3b, 0x0a, 0x7d, 0x0a, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x64, 0x6f, 0x5f, 0x6d, 0x6b,
+ 0x64, 0x69, 0x72, 0x28, 0x63, 0x68, 0x61, 0x72, 0x20, 0x2a, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x29,
+ 0x0a, 0x7b, 0x0a, 0x09, 0x73, 0x74, 0x72, 0x75, 0x70, 0x70, 0x65, 0x72, 0x28, 0x66, 0x6e, 0x61,
+ 0x6d, 0x65, 0x29, 0x3b, 0x0a, 0x0a, 0x09, 0x69, 0x66, 0x20, 0x28, 0x6d, 0x6b, 0x64, 0x69, 0x72,
+ 0x28, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x2c, 0x20, 0x30, 0x37, 0x30, 0x30, 0x29, 0x20, 0x21, 0x3d,
+ 0x20, 0x30, 0x29, 0x20, 0x7b, 0x0a, 0x23, 0x69, 0x66, 0x20, 0x44, 0x45, 0x42, 0x55, 0x47, 0x0a,
+ 0x09, 0x09, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x28, 0x22, 0x6d, 0x6b, 0x64, 0x69, 0x72, 0x20,
+ 0x25, 0x73, 0x20, 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x20, 0x28, 0x25, 0x73, 0x29, 0x5c, 0x6e,
+ 0x22, 0x2c, 0x20, 0x0a, 0x09, 0x09, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x6e, 0x61,
+ 0x6d, 0x65, 0x2c, 0x20, 0x73, 0x74, 0x72, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x28, 0x65, 0x72, 0x72,
+ 0x6e, 0x6f, 0x29, 0x29, 0x3b, 0x0a, 0x23, 0x65, 0x6e, 0x64, 0x69, 0x66, 0x0a, 0x09, 0x7d, 0x0a,
+ 0x7d, 0x0a, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x64, 0x6f, 0x5f, 0x72, 0x6d, 0x64, 0x69, 0x72,
+ 0x28, 0x63, 0x68, 0x61, 0x72, 0x20, 0x2a, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x29, 0x0a, 0x7b, 0x0a,
+ 0x09, 0x73, 0x74, 0x72, 0x75, 0x70, 0x70, 0x65, 0x72, 0x28, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x29,
+ 0x3b, 0x0a, 0x0a, 0x09, 0x69, 0x66, 0x20, 0x28, 0x72, 0x6d, 0x64, 0x69, 0x72, 0x28, 0x66, 0x6e,
+ 0x61, 0x6d, 0x65, 0x29, 0x20, 0x21, 0x3d, 0x20, 0x30, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x70,
+ 0x72, 0x69, 0x6e, 0x74, 0x66, 0x28, 0x22, 0x72, 0x6d, 0x64, 0x69, 0x72, 0x20, 0x25, 0x73, 0x20,
+ 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x20, 0x28, 0x25, 0x73, 0x29, 0x5c, 0x6e, 0x22, 0x2c, 0x20,
+ 0x0a, 0x09, 0x09, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x2c,
+ 0x20, 0x73, 0x74, 0x72, 0x65, 0x72, 0x72, 0x6f, 0x72, 0x28, 0x65, 0x72, 0x72, 0x6e, 0x6f, 0x29,
+ 0x29, 0x3b, 0x0a, 0x09, 0x7d, 0x0a, 0x7d, 0x0a, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x64, 0x6f,
+ 0x5f, 0x72, 0x65, 0x6e, 0x61, 0x6d, 0x65, 0x28, 0x63, 0x68, 0x61, 0x72, 0x20, 0x2a, 0x6f, 0x6c,
+ 0x64, 0x2c, 0x20, 0x63, 0x68, 0x61, 0x72, 0x20, 0x2a, 0x6e, 0x65, 0x77, 0x29, 0x0a, 0x7b, 0x0a,
+ 0x09, 0x73, 0x74, 0x72, 0x75, 0x70, 0x70, 0x65, 0x72, 0x28, 0x6f, 0x6c, 0x64, 0x29, 0x3b, 0x0a,
+ 0x09, 0x73, 0x74, 0x72, 0x75, 0x70, 0x70, 0x65, 0x72, 0x28, 0x6e, 0x65, 0x77, 0x29, 0x3b, 0x0a,
+ 0x0a, 0x09, 0x69, 0x66, 0x20, 0x28, 0x72, 0x65, 0x6e, 0x61, 0x6d, 0x65, 0x28, 0x6f, 0x6c, 0x64,
+ 0x2c, 0x20, 0x6e, 0x65, 0x77, 0x29, 0x20, 0x21, 0x3d, 0x20, 0x30, 0x29, 0x20, 0x7b, 0x0a, 0x09,
+ 0x09, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x28, 0x22, 0x72, 0x65, 0x6e, 0x61, 0x6d, 0x65, 0x20,
+ 0x25, 0x73, 0x20, 0x25, 0x73, 0x20, 0x66, 0x61, 0x69, 0x6c, 0x65, 0x64, 0x20, 0x28, 0x25, 0x73,
+ 0x29, 0x5c, 0x6e, 0x22, 0x2c, 0x20, 0x0a, 0x09, 0x09, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
+ 0x6f, 0x6c, 0x64, 0x2c, 0x20, 0x6e, 0x65, 0x77, 0x2c, 0x20, 0x73, 0x74, 0x72, 0x65, 0x72, 0x72,
+ 0x6f, 0x72, 0x28, 0x65, 0x72, 0x72, 0x6e, 0x6f, 0x29, 0x29, 0x3b, 0x0a, 0x09, 0x7d, 0x0a, 0x7d,
+ 0x0a, 0x0a, 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x64, 0x6f, 0x5f, 0x73, 0x74, 0x61, 0x74, 0x28,
+ 0x63, 0x68, 0x61, 0x72, 0x20, 0x2a, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x2c, 0x20, 0x69, 0x6e, 0x74,
+ 0x20, 0x73, 0x69, 0x7a, 0x65, 0x29, 0x0a, 0x7b, 0x0a, 0x09, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74,
+ 0x20, 0x73, 0x74, 0x61, 0x74, 0x20, 0x73, 0x74, 0x3b, 0x0a, 0x0a, 0x09, 0x73, 0x74, 0x72, 0x75,
+ 0x70, 0x70, 0x65, 0x72, 0x28, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x29, 0x3b, 0x0a, 0x0a, 0x09, 0x69,
+ 0x66, 0x20, 0x28, 0x73, 0x74, 0x61, 0x74, 0x28, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x2c, 0x20, 0x26,
+ 0x73, 0x74, 0x29, 0x20, 0x21, 0x3d, 0x20, 0x30, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x70, 0x72,
+ 0x69, 0x6e, 0x74, 0x66, 0x28, 0x22, 0x28, 0x25, 0x64, 0x29, 0x20, 0x64, 0x6f, 0x5f, 0x73, 0x74,
+ 0x61, 0x74, 0x3a, 0x20, 0x25, 0x73, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x3d, 0x25, 0x64, 0x20, 0x25,
+ 0x73, 0x5c, 0x6e, 0x22, 0x2c, 0x20, 0x0a, 0x09, 0x09, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
+ 0x6c, 0x69, 0x6e, 0x65, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x2c, 0x20, 0x66, 0x6e, 0x61, 0x6d,
+ 0x65, 0x2c, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x2c, 0x20, 0x73, 0x74, 0x72, 0x65, 0x72, 0x72, 0x6f,
+ 0x72, 0x28, 0x65, 0x72, 0x72, 0x6e, 0x6f, 0x29, 0x29, 0x3b, 0x0a, 0x09, 0x09, 0x72, 0x65, 0x74,
+ 0x75, 0x72, 0x6e, 0x3b, 0x0a, 0x09, 0x7d, 0x0a, 0x09, 0x69, 0x66, 0x20, 0x28, 0x53, 0x5f, 0x49,
+ 0x53, 0x44, 0x49, 0x52, 0x28, 0x73, 0x74, 0x2e, 0x73, 0x74, 0x5f, 0x6d, 0x6f, 0x64, 0x65, 0x29,
+ 0x29, 0x20, 0x72, 0x65, 0x74, 0x75, 0x72, 0x6e, 0x3b, 0x0a, 0x0a, 0x09, 0x69, 0x66, 0x20, 0x28,
+ 0x73, 0x74, 0x2e, 0x73, 0x74, 0x5f, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x21, 0x3d, 0x20, 0x73, 0x69,
+ 0x7a, 0x65, 0x29, 0x20, 0x7b, 0x0a, 0x09, 0x09, 0x70, 0x72, 0x69, 0x6e, 0x74, 0x66, 0x28, 0x22,
+ 0x28, 0x25, 0x64, 0x29, 0x20, 0x64, 0x6f, 0x5f, 0x73, 0x74, 0x61, 0x74, 0x3a, 0x20, 0x25, 0x73,
+ 0x20, 0x77, 0x72, 0x6f, 0x6e, 0x67, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x20, 0x25, 0x64, 0x20, 0x25,
+ 0x64, 0x5c, 0x6e, 0x22, 0x2c, 0x20, 0x0a, 0x09, 0x09, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
+ 0x6c, 0x69, 0x6e, 0x65, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x2c, 0x20, 0x66, 0x6e, 0x61, 0x6d,
+ 0x65, 0x2c, 0x20, 0x28, 0x69, 0x6e, 0x74, 0x29, 0x73, 0x74, 0x2e, 0x73, 0x74, 0x5f, 0x73, 0x69,
+ 0x7a, 0x65, 0x2c, 0x20, 0x73, 0x69, 0x7a, 0x65, 0x29, 0x3b, 0x0a, 0x09, 0x7d, 0x0a, 0x7d, 0x0a,
+ 0x0a, 0x76, 0x6f, 0x69, 0x64, 0x20, 0x64, 0x6f, 0x5f, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x28,
+ 0x63, 0x68, 0x61, 0x72, 0x20, 0x2a, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x2c, 0x20, 0x69, 0x6e, 0x74,
+ 0x20, 0x73, 0x69, 0x7a, 0x65, 0x29, 0x0a, 0x7b, 0x0a, 0x09, 0x64, 0x6f, 0x5f, 0x6f, 0x70, 0x65,
+ 0x6e, 0x28, 0x66, 0x6e, 0x61, 0x6d, 0x65, 0x2c, 0x20, 0x35, 0x30, 0x30, 0x30, 0x2c, 0x20, 0x73,
+ 0x69, 0x7a, 0x65, 0x29, 0x3b, 0x0a, 0x09, 0x64, 0x6f, 0x5f, 0x63, 0x6c, 0x6f, 0x73, 0x65, 0x28,
+ 0x35, 0x30, 0x30, 0x30, 0x29, 0x3b, 0x0a, 0x7d, 0x0a
+};
+#endif
+static unsigned char comprbuf[TESTDATA_LEN];
+static unsigned char decomprbuf[TESTDATA_LEN];
+
+int jffs2_decompress(unsigned char comprtype, unsigned char *cdata_in, 
+                    unsigned char *data_out, __u32 cdatalen, __u32 datalen);
+unsigned char jffs2_compress(unsigned char *data_in, unsigned char *cpage_out, 
+                            __u32 *datalen, __u32 *cdatalen);
+
+int init_module(void ) {
+       unsigned char comprtype;
+       __u32 c, d;
+       int ret;
+
+       printk("Original data: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
+              testdata[0],testdata[1],testdata[2],testdata[3], 
+              testdata[4],testdata[5],testdata[6],testdata[7], 
+              testdata[8],testdata[9],testdata[10],testdata[11], 
+              testdata[12],testdata[13],testdata[14],testdata[15]); 
+       d = TESTDATA_LEN;
+       c = TESTDATA_LEN;
+       comprtype = jffs2_compress(testdata, comprbuf, &d, &c);
+
+       printk("jffs2_compress used compression type %d. Compressed size %d, uncompressed size %d\n",
+              comprtype, c, d);
+       printk("Compressed data: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
+              comprbuf[0],comprbuf[1],comprbuf[2],comprbuf[3], 
+              comprbuf[4],comprbuf[5],comprbuf[6],comprbuf[7], 
+              comprbuf[8],comprbuf[9],comprbuf[10],comprbuf[11], 
+              comprbuf[12],comprbuf[13],comprbuf[14],comprbuf[15]); 
+
+       ret = jffs2_decompress(comprtype, comprbuf, decomprbuf, c, d);
+       printk("jffs2_decompress returned %d\n", ret);
+       printk("Decompressed data:  %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
+              decomprbuf[0],decomprbuf[1],decomprbuf[2],decomprbuf[3], 
+              decomprbuf[4],decomprbuf[5],decomprbuf[6],decomprbuf[7], 
+              decomprbuf[8],decomprbuf[9],decomprbuf[10],decomprbuf[11], 
+              decomprbuf[12],decomprbuf[13],decomprbuf[14],decomprbuf[15]); 
+       if (memcmp(decomprbuf, testdata, d))
+               printk("Compression and decompression corrupted data\n");
+       else
+               printk("Compression good for %d bytes\n", d);
+       return 1;
+}
diff --git a/fs/jffs2/crc32.c b/fs/jffs2/crc32.c
new file mode 100644 (file)
index 0000000..b3b6a81
--- /dev/null
@@ -0,0 +1,97 @@
+/*
+ *  COPYRIGHT (C) 1986 Gary S. Brown.  You may use this program, or
+ *  code or tables extracted from it, as desired without restriction.
+ *
+ *  First, the polynomial itself and its table of feedback terms.  The
+ *  polynomial is
+ *  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+X^0
+ *
+ *  Note that we take it "backwards" and put the highest-order term in
+ *  the lowest-order bit.  The X^32 term is "implied"; the LSB is the
+ *  X^31 term, etc.  The X^0 term (usually shown as "+1") results in
+ *  the MSB being 1
+ *
+ *  Note that the usual hardware shift register implementation, which
+ *  is what we're using (we're merely optimizing it by doing eight-bit
+ *  chunks at a time) shifts bits into the lowest-order term.  In our
+ *  implementation, that means shifting towards the right.  Why do we
+ *  do it this way?  Because the calculated CRC must be transmitted in
+ *  order from highest-order term to lowest-order term.  UARTs transmit
+ *  characters in order from LSB to MSB.  By storing the CRC this way
+ *  we hand it to the UART in the order low-byte to high-byte; the UART
+ *  sends each low-bit to hight-bit; and the result is transmission bit
+ *  by bit from highest- to lowest-order term without requiring any bit
+ *  shuffling on our part.  Reception works similarly
+ *
+ *  The feedback terms table consists of 256, 32-bit entries.  Notes
+ *
+ *      The table can be generated at runtime if desired; code to do so
+ *      is shown later.  It might not be obvious, but the feedback
+ *      terms simply represent the results of eight shift/xor opera
+ *      tions for all combinations of data and CRC register values
+ *
+ *      The values must be right-shifted by eight bits by the "updcrc
+ *      logic; the shift must be unsigned (bring in zeroes).  On some
+ *      hardware you could probably optimize the shift in assembler by
+ *      using byte-swap instructions
+ *      polynomial $edb88320
+ */
+
+/* $Id: crc32.c,v 1.3 2001/02/07 16:45:32 dwmw2 Exp $ */
+
+#include "crc32.h"
+
+const __u32 crc32_table[256] = {
+       0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
+       0x706af48fL, 0xe963a535L, 0x9e6495a3L, 0x0edb8832L, 0x79dcb8a4L,
+       0xe0d5e91eL, 0x97d2d988L, 0x09b64c2bL, 0x7eb17cbdL, 0xe7b82d07L,
+       0x90bf1d91L, 0x1db71064L, 0x6ab020f2L, 0xf3b97148L, 0x84be41deL,
+       0x1adad47dL, 0x6ddde4ebL, 0xf4d4b551L, 0x83d385c7L, 0x136c9856L,
+       0x646ba8c0L, 0xfd62f97aL, 0x8a65c9ecL, 0x14015c4fL, 0x63066cd9L,
+       0xfa0f3d63L, 0x8d080df5L, 0x3b6e20c8L, 0x4c69105eL, 0xd56041e4L,
+       0xa2677172L, 0x3c03e4d1L, 0x4b04d447L, 0xd20d85fdL, 0xa50ab56bL,
+       0x35b5a8faL, 0x42b2986cL, 0xdbbbc9d6L, 0xacbcf940L, 0x32d86ce3L,
+       0x45df5c75L, 0xdcd60dcfL, 0xabd13d59L, 0x26d930acL, 0x51de003aL,
+       0xc8d75180L, 0xbfd06116L, 0x21b4f4b5L, 0x56b3c423L, 0xcfba9599L,
+       0xb8bda50fL, 0x2802b89eL, 0x5f058808L, 0xc60cd9b2L, 0xb10be924L,
+       0x2f6f7c87L, 0x58684c11L, 0xc1611dabL, 0xb6662d3dL, 0x76dc4190L,
+       0x01db7106L, 0x98d220bcL, 0xefd5102aL, 0x71b18589L, 0x06b6b51fL,
+       0x9fbfe4a5L, 0xe8b8d433L, 0x7807c9a2L, 0x0f00f934L, 0x9609a88eL,
+       0xe10e9818L, 0x7f6a0dbbL, 0x086d3d2dL, 0x91646c97L, 0xe6635c01L,
+       0x6b6b51f4L, 0x1c6c6162L, 0x856530d8L, 0xf262004eL, 0x6c0695edL,
+       0x1b01a57bL, 0x8208f4c1L, 0xf50fc457L, 0x65b0d9c6L, 0x12b7e950L,
+       0x8bbeb8eaL, 0xfcb9887cL, 0x62dd1ddfL, 0x15da2d49L, 0x8cd37cf3L,
+       0xfbd44c65L, 0x4db26158L, 0x3ab551ceL, 0xa3bc0074L, 0xd4bb30e2L,
+       0x4adfa541L, 0x3dd895d7L, 0xa4d1c46dL, 0xd3d6f4fbL, 0x4369e96aL,
+       0x346ed9fcL, 0xad678846L, 0xda60b8d0L, 0x44042d73L, 0x33031de5L,
+       0xaa0a4c5fL, 0xdd0d7cc9L, 0x5005713cL, 0x270241aaL, 0xbe0b1010L,
+       0xc90c2086L, 0x5768b525L, 0x206f85b3L, 0xb966d409L, 0xce61e49fL,
+       0x5edef90eL, 0x29d9c998L, 0xb0d09822L, 0xc7d7a8b4L, 0x59b33d17L,
+       0x2eb40d81L, 0xb7bd5c3bL, 0xc0ba6cadL, 0xedb88320L, 0x9abfb3b6L,
+       0x03b6e20cL, 0x74b1d29aL, 0xead54739L, 0x9dd277afL, 0x04db2615L,
+       0x73dc1683L, 0xe3630b12L, 0x94643b84L, 0x0d6d6a3eL, 0x7a6a5aa8L,
+       0xe40ecf0bL, 0x9309ff9dL, 0x0a00ae27L, 0x7d079eb1L, 0xf00f9344L,
+       0x8708a3d2L, 0x1e01f268L, 0x6906c2feL, 0xf762575dL, 0x806567cbL,
+       0x196c3671L, 0x6e6b06e7L, 0xfed41b76L, 0x89d32be0L, 0x10da7a5aL,
+       0x67dd4accL, 0xf9b9df6fL, 0x8ebeeff9L, 0x17b7be43L, 0x60b08ed5L,
+       0xd6d6a3e8L, 0xa1d1937eL, 0x38d8c2c4L, 0x4fdff252L, 0xd1bb67f1L,
+       0xa6bc5767L, 0x3fb506ddL, 0x48b2364bL, 0xd80d2bdaL, 0xaf0a1b4cL,
+       0x36034af6L, 0x41047a60L, 0xdf60efc3L, 0xa867df55L, 0x316e8eefL,
+       0x4669be79L, 0xcb61b38cL, 0xbc66831aL, 0x256fd2a0L, 0x5268e236L,
+       0xcc0c7795L, 0xbb0b4703L, 0x220216b9L, 0x5505262fL, 0xc5ba3bbeL,
+       0xb2bd0b28L, 0x2bb45a92L, 0x5cb36a04L, 0xc2d7ffa7L, 0xb5d0cf31L,
+       0x2cd99e8bL, 0x5bdeae1dL, 0x9b64c2b0L, 0xec63f226L, 0x756aa39cL,
+       0x026d930aL, 0x9c0906a9L, 0xeb0e363fL, 0x72076785L, 0x05005713L,
+       0x95bf4a82L, 0xe2b87a14L, 0x7bb12baeL, 0x0cb61b38L, 0x92d28e9bL,
+       0xe5d5be0dL, 0x7cdcefb7L, 0x0bdbdf21L, 0x86d3d2d4L, 0xf1d4e242L,
+       0x68ddb3f8L, 0x1fda836eL, 0x81be16cdL, 0xf6b9265bL, 0x6fb077e1L,
+       0x18b74777L, 0x88085ae6L, 0xff0f6a70L, 0x66063bcaL, 0x11010b5cL,
+       0x8f659effL, 0xf862ae69L, 0x616bffd3L, 0x166ccf45L, 0xa00ae278L,
+       0xd70dd2eeL, 0x4e048354L, 0x3903b3c2L, 0xa7672661L, 0xd06016f7L,
+       0x4969474dL, 0x3e6e77dbL, 0xaed16a4aL, 0xd9d65adcL, 0x40df0b66L,
+       0x37d83bf0L, 0xa9bcae53L, 0xdebb9ec5L, 0x47b2cf7fL, 0x30b5ffe9L,
+       0xbdbdf21cL, 0xcabac28aL, 0x53b39330L, 0x24b4a3a6L, 0xbad03605L,
+       0xcdd70693L, 0x54de5729L, 0x23d967bfL, 0xb3667a2eL, 0xc4614ab8L,
+       0x5d681b02L, 0x2a6f2b94L, 0xb40bbe37L, 0xc30c8ea1L, 0x5a05df1bL,
+       0x2d02ef8dL
+};
diff --git a/fs/jffs2/crc32.h b/fs/jffs2/crc32.h
new file mode 100644 (file)
index 0000000..cd8979f
--- /dev/null
@@ -0,0 +1,21 @@
+#ifndef CRC32_H
+#define CRC32_H
+
+/* $Id: crc32.h,v 1.3 2001/02/26 14:44:37 dwmw2 Exp $ */
+
+#include <linux/types.h>
+
+extern const __u32 crc32_table[256];
+
+/* Return a 32-bit CRC of the contents of the buffer. */
+
+static inline __u32 
+crc32(__u32 val, const void *ss, int len)
+{
+       const unsigned char *s = ss;
+        while (--len >= 0)
+                val = crc32_table[(val ^ *s++) & 0xff] ^ (val >> 8);
+        return val;
+}
+
+#endif
diff --git a/fs/jffs2/dir.c b/fs/jffs2/dir.c
new file mode 100644 (file)
index 0000000..1b0c24d
--- /dev/null
@@ -0,0 +1,951 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: dir.c,v 1.42 2001/05/24 22:24:39 dwmw2 Exp $
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/fs.h>
+#include <linux/jffs2.h>
+#include <linux/jffs2_fs_i.h>
+#include <linux/jffs2_fs_sb.h>
+#include "nodelist.h"
+#include "crc32.h"
+
+static int jffs2_readdir (struct file *, void *, filldir_t);
+
+static int jffs2_create (struct inode *,struct dentry *,int);
+static struct dentry *jffs2_lookup (struct inode *,struct dentry *);
+static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
+static int jffs2_unlink (struct inode *,struct dentry *);
+static int jffs2_symlink (struct inode *,struct dentry *,const char *);
+static int jffs2_mkdir (struct inode *,struct dentry *,int);
+static int jffs2_rmdir (struct inode *,struct dentry *);
+static int jffs2_mknod (struct inode *,struct dentry *,int,int);
+static int jffs2_rename (struct inode *, struct dentry *,
+                        struct inode *, struct dentry *);
+
+struct file_operations jffs2_dir_operations =
+{
+       read:           generic_read_dir,
+       readdir:        jffs2_readdir,
+       ioctl:          jffs2_ioctl,
+       fsync:          jffs2_null_fsync
+};
+
+
+struct inode_operations jffs2_dir_inode_operations =
+{
+       create:         jffs2_create,
+       lookup:         jffs2_lookup,
+       link:           jffs2_link,
+       unlink:         jffs2_unlink,
+       symlink:        jffs2_symlink,
+       mkdir:          jffs2_mkdir,
+       rmdir:          jffs2_rmdir,
+       mknod:          jffs2_mknod,
+       rename:         jffs2_rename,
+       setattr:        jffs2_setattr,
+};
+
+/***********************************************************************/
+
+
+/* We keep the dirent list sorted in increasing order of name hash,
+   and we use the same hash function as the dentries. Makes this 
+   nice and simple
+*/
+static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target)
+{
+       struct jffs2_inode_info *dir_f;
+       struct jffs2_sb_info *c;
+       struct jffs2_full_dirent *fd = NULL, *fd_list;
+       __u32 ino = 0;
+       struct inode *inode = NULL;
+
+       D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
+
+       dir_f = JFFS2_INODE_INFO(dir_i);
+       c = JFFS2_SB_INFO(dir_i->i_sb);
+
+       down(&dir_f->sem);
+
+       /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
+       for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) {
+               if (fd_list->nhash == target->d_name.hash && 
+                   (!fd || fd_list->version > fd->version) &&
+                   strlen(fd_list->name) == target->d_name.len &&
+                   !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
+                       fd = fd_list;
+               }
+       }
+       if (fd)
+               ino = fd->ino;
+       up(&dir_f->sem);
+       if (ino) {
+               inode = iget(dir_i->i_sb, ino);
+               if (!inode) {
+                       printk(KERN_WARNING "iget() failed for ino #%u\n", ino);
+                       return (ERR_PTR(-EIO));
+               }
+       }
+
+       d_add(target, inode);
+
+       return NULL;
+}
+
+/***********************************************************************/
+
+
+static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
+{
+       struct jffs2_inode_info *f;
+       struct jffs2_sb_info *c;
+       struct inode *inode = filp->f_dentry->d_inode;
+       struct jffs2_full_dirent *fd;
+       unsigned long offset, curofs;
+
+       D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_dentry->d_inode->i_ino));
+
+       f = JFFS2_INODE_INFO(inode);
+       c = JFFS2_SB_INFO(inode->i_sb);
+
+       offset = filp->f_pos;
+
+       if (offset == 0) {
+               D1(printk(KERN_DEBUG "Dirent 0: \".\", ino #%lu\n", inode->i_ino));
+               if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
+                       goto out;
+               offset++;
+       }
+       if (offset == 1) {
+               D1(printk(KERN_DEBUG "Dirent 1: \"..\", ino #%lu\n", filp->f_dentry->d_parent->d_inode->i_ino));
+               if (filldir(dirent, "..", 2, 1, filp->f_dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
+                       goto out;
+               offset++;
+       }
+
+       curofs=1;
+       down(&f->sem);
+       for (fd = f->dents; fd; fd = fd->next) {
+
+               curofs++;
+               /* First loop: curofs = 2; offset = 2 */
+               if (curofs < offset) {
+                       D2(printk(KERN_DEBUG "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n", 
+                                 fd->name, fd->ino, fd->type, curofs, offset));
+                       continue;
+               }
+               if (!fd->ino) {
+                       D2(printk(KERN_DEBUG "Skipping deletion dirent \"%s\"\n", fd->name));
+                       offset++;
+                       continue;
+               }
+               D2(printk(KERN_DEBUG "Dirent %ld: \"%s\", ino #%u, type %d\n", offset, fd->name, fd->ino, fd->type));
+               if (filldir(dirent, fd->name, strlen(fd->name), offset, fd->ino, fd->type) < 0)
+                       goto out;
+               offset++;
+       }
+ out:
+       up(&f->sem);
+       filp->f_pos = offset;
+       return 0;
+}
+
+/***********************************************************************/
+
+static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode)
+{
+       struct jffs2_inode_info *f, *dir_f;
+       struct jffs2_sb_info *c;
+       struct inode *inode;
+       struct jffs2_raw_inode *ri;
+       struct jffs2_raw_dirent *rd;
+       struct jffs2_full_dnode *fn;
+       struct jffs2_full_dirent *fd;
+       int namelen;
+       __u32 alloclen, phys_ofs;
+       __u32 writtenlen;
+       int ret;
+
+       ri = jffs2_alloc_raw_inode();
+       if (!ri)
+               return -ENOMEM;
+       
+       c = JFFS2_SB_INFO(dir_i->i_sb);
+
+       D1(printk(KERN_DEBUG "jffs2_create()\n"));
+
+       /* Try to reserve enough space for both node and dirent. 
+        * Just the node will do for now, though 
+        */
+       namelen = dentry->d_name.len;
+       ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
+       D1(printk(KERN_DEBUG "jffs2_create(): reserved 0x%x bytes\n", alloclen));
+       if (ret) {
+               jffs2_free_raw_inode(ri);
+               return ret;
+       }
+
+       inode = jffs2_new_inode(dir_i, mode, ri);
+
+       if (IS_ERR(inode)) {
+               D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n"));
+               jffs2_free_raw_inode(ri);
+               jffs2_complete_reservation(c);
+               return PTR_ERR(inode);
+       }
+
+       inode->i_op = &jffs2_file_inode_operations;
+       inode->i_fop = &jffs2_file_operations;
+       inode->i_mapping->a_ops = &jffs2_file_address_operations;
+       inode->i_mapping->nrpages = 0;
+
+       f = JFFS2_INODE_INFO(inode);
+
+       ri->data_crc = 0;
+       ri->node_crc = crc32(0, ri, sizeof(*ri)-8);
+
+       fn = jffs2_write_dnode(inode, ri, NULL, 0, phys_ofs, &writtenlen);
+       D1(printk(KERN_DEBUG "jffs2_create created file with mode 0x%x\n", ri->mode));
+       jffs2_free_raw_inode(ri);
+
+       if (IS_ERR(fn)) {
+               D1(printk(KERN_DEBUG "jffs2_write_dnode() failed\n"));
+               /* Eeek. Wave bye bye */
+               up(&f->sem);
+               jffs2_complete_reservation(c);
+               jffs2_clear_inode(inode);
+               return PTR_ERR(fn);
+       }
+       /* No data here. Only a metadata node, which will be 
+          obsoleted by the first data write
+       */
+       f->metadata = fn;
+
+       /* Work out where to put the dirent node now. */
+       writtenlen = PAD(writtenlen);
+       phys_ofs += writtenlen;
+       alloclen -= writtenlen;
+       up(&f->sem);
+
+       if (alloclen < sizeof(*rd)+namelen) {
+               /* Not enough space left in this chunk. Get some more */
+               jffs2_complete_reservation(c);
+               ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
+               
+               if (ret) {
+                       /* Eep. */
+                       D1(printk(KERN_DEBUG "jffs2_reserve_space() for dirent failed\n"));
+                       jffs2_clear_inode(inode);
+                       return ret;
+               }
+       }
+
+       rd = jffs2_alloc_raw_dirent();
+       if (!rd) {
+               /* Argh. Now we treat it like a normal delete */
+               jffs2_complete_reservation(c);
+               jffs2_clear_inode(inode);
+               return -ENOMEM;
+       }
+
+       dir_f = JFFS2_INODE_INFO(dir_i);
+       down(&dir_f->sem);
+
+       rd->magic = JFFS2_MAGIC_BITMASK;
+       rd->nodetype = JFFS2_NODETYPE_DIRENT;
+       rd->totlen = sizeof(*rd) + namelen;
+       rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4);
+
+       rd->pino = dir_i->i_ino;
+       rd->version = dir_f->highest_version++;
+       rd->ino = inode->i_ino;
+       rd->mctime = CURRENT_TIME;
+       rd->nsize = namelen;
+       rd->type = DT_REG;
+       rd->node_crc = crc32(0, rd, sizeof(*rd)-8);
+       rd->name_crc = crc32(0, dentry->d_name.name, namelen);
+
+       fd = jffs2_write_dirent(dir_i, rd, dentry->d_name.name, namelen, phys_ofs, &writtenlen);
+
+       jffs2_complete_reservation(c);
+       jffs2_free_raw_dirent(rd);
+       
+       if (IS_ERR(fd)) {
+               /* dirent failed to write. Delete the inode normally 
+                  as if it were the final unlink() */
+               up(&dir_f->sem);
+               jffs2_clear_inode(inode);
+               return PTR_ERR(fd);
+       }
+
+       /* Link the fd into the inode's list, obsoleting an old
+          one if necessary. */
+       jffs2_add_fd_to_list(c, fd, &dir_f->dents);
+       up(&dir_f->sem);
+
+       d_instantiate(dentry, inode);
+
+       D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
+                 inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages));
+       return 0;
+}
+
+/***********************************************************************/
+
+static int jffs2_do_unlink(struct inode *dir_i, struct dentry *dentry, int rename)
+{
+       struct jffs2_inode_info *dir_f, *f;
+       struct jffs2_sb_info *c;
+       struct jffs2_raw_dirent *rd;
+       struct jffs2_full_dirent *fd;
+       __u32 alloclen, phys_ofs;
+       int ret;
+
+       c = JFFS2_SB_INFO(dir_i->i_sb);
+
+       rd = jffs2_alloc_raw_dirent();
+       if (!rd)
+               return -ENOMEM;
+
+       ret = jffs2_reserve_space(c, sizeof(*rd)+dentry->d_name.len, &phys_ofs, &alloclen, ALLOC_DELETION);
+       if (ret) {
+               jffs2_free_raw_dirent(rd);
+               return ret;
+       }
+
+       dir_f = JFFS2_INODE_INFO(dir_i);
+       down(&dir_f->sem);
+
+       /* Build a deletion node */
+       rd->magic = JFFS2_MAGIC_BITMASK;
+       rd->nodetype = JFFS2_NODETYPE_DIRENT;
+       rd->totlen = sizeof(*rd) + dentry->d_name.len;
+       rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4);
+
+       rd->pino = dir_i->i_ino;
+       rd->version = dir_f->highest_version++;
+       rd->ino = 0;
+       rd->mctime = CURRENT_TIME;
+       rd->nsize = dentry->d_name.len;
+       rd->type = DT_UNKNOWN;
+       rd->node_crc = crc32(0, rd, sizeof(*rd)-8);
+       rd->name_crc = crc32(0, dentry->d_name.name, dentry->d_name.len);
+
+       fd = jffs2_write_dirent(dir_i, rd, dentry->d_name.name, dentry->d_name.len, phys_ofs, NULL);
+       
+       jffs2_complete_reservation(c);
+       jffs2_free_raw_dirent(rd);
+
+       if (IS_ERR(fd)) {
+               up(&dir_f->sem);
+               return PTR_ERR(fd);
+       }
+
+       /* File it. This will mark the old one obsolete. */
+       jffs2_add_fd_to_list(c, fd, &dir_f->dents);
+       up(&dir_f->sem);
+       
+       if (!rename) {
+               f = JFFS2_INODE_INFO(dentry->d_inode);
+               down(&f->sem);
+
+               while (f->dents) {
+                       /* There can be only deleted ones */
+                       fd = f->dents;
+                       
+                       f->dents = fd->next;
+                       
+                       if (fd->ino) {
+                               printk(KERN_WARNING "Deleting inode #%u with active dentry \"%s\"->ino #%u\n",
+                                      f->inocache->ino, fd->name, fd->ino);
+                       } else {
+                               D1(printk(KERN_DEBUG "Removing deletion dirent for \"%s\" from dir ino #%u\n", fd->name, f->inocache->ino));
+                       }
+                       jffs2_mark_node_obsolete(c, fd->raw);
+                       jffs2_free_full_dirent(fd);
+               }
+
+               f->inocache->nlink--;
+               dentry->d_inode->i_nlink--;
+               up(&f->sem);
+       }
+
+       return 0;
+}
+
+static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
+{
+       return jffs2_do_unlink(dir_i, dentry, 0);
+}
+/***********************************************************************/
+
+static int jffs2_do_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry, int rename)
+{
+       struct jffs2_inode_info *dir_f, *f;
+       struct jffs2_sb_info *c;
+       struct jffs2_raw_dirent *rd;
+       struct jffs2_full_dirent *fd;
+       __u32 alloclen, phys_ofs;
+       int ret;
+
+       c = JFFS2_SB_INFO(dir_i->i_sb);
+
+       rd = jffs2_alloc_raw_dirent();
+       if (!rd)
+               return -ENOMEM;
+
+       ret = jffs2_reserve_space(c, sizeof(*rd)+dentry->d_name.len, &phys_ofs, &alloclen, ALLOC_NORMAL);
+       if (ret) {
+               jffs2_free_raw_dirent(rd);
+               return ret;
+       }
+       
+       dir_f = JFFS2_INODE_INFO(dir_i);
+       down(&dir_f->sem);
+
+       /* Build a deletion node */
+       rd->magic = JFFS2_MAGIC_BITMASK;
+       rd->nodetype = JFFS2_NODETYPE_DIRENT;
+       rd->totlen = sizeof(*rd) + dentry->d_name.len;
+       rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4);
+
+       rd->pino = dir_i->i_ino;
+       rd->version = dir_f->highest_version++;
+       rd->ino = old_dentry->d_inode->i_ino;
+       rd->mctime = CURRENT_TIME;
+       rd->nsize = dentry->d_name.len;
+
+       /* XXX: This is ugly. */
+       rd->type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
+       if (!rd->type) rd->type = DT_REG;
+
+       rd->node_crc = crc32(0, rd, sizeof(*rd)-8);
+       rd->name_crc = crc32(0, dentry->d_name.name, dentry->d_name.len);
+
+       fd = jffs2_write_dirent(dir_i, rd, dentry->d_name.name, dentry->d_name.len, phys_ofs, NULL);
+       
+       jffs2_complete_reservation(c);
+       jffs2_free_raw_dirent(rd);
+
+       if (IS_ERR(fd)) {
+               up(&dir_f->sem);
+               return PTR_ERR(fd);
+       }
+
+       /* File it. This will mark the old one obsolete. */
+       jffs2_add_fd_to_list(c, fd, &dir_f->dents);
+       up(&dir_f->sem);
+
+       if (!rename) {
+               f = JFFS2_INODE_INFO(old_dentry->d_inode);
+               down(&f->sem);
+               old_dentry->d_inode->i_nlink = ++f->inocache->nlink;
+               up(&f->sem);
+       }
+       return 0;
+}
+
+static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
+{
+       int ret = jffs2_do_link(old_dentry, dir_i, dentry, 0);
+       if (!ret) {
+               d_instantiate(dentry, old_dentry->d_inode);
+               atomic_inc(&old_dentry->d_inode->i_count);
+       }
+       return ret;
+}
+
+/***********************************************************************/
+
+static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
+{
+       struct jffs2_inode_info *f, *dir_f;
+       struct jffs2_sb_info *c;
+       struct inode *inode;
+       struct jffs2_raw_inode *ri;
+       struct jffs2_raw_dirent *rd;
+       struct jffs2_full_dnode *fn;
+       struct jffs2_full_dirent *fd;
+       int namelen;
+       __u32 alloclen, phys_ofs;
+       __u32 writtenlen;
+       int ret;
+
+       /* FIXME: If you care. We'd need to use frags for the target
+          if it grows much more than this */
+       if (strlen(target) > 254)
+               return -EINVAL;
+
+       ri = jffs2_alloc_raw_inode();
+
+       if (!ri)
+               return -ENOMEM;
+       
+       c = JFFS2_SB_INFO(dir_i->i_sb);
+       
+       /* Try to reserve enough space for both node and dirent. 
+        * Just the node will do for now, though 
+        */
+       namelen = dentry->d_name.len;
+       ret = jffs2_reserve_space(c, sizeof(*ri) + strlen(target), &phys_ofs, &alloclen, ALLOC_NORMAL);
+
+       if (ret) {
+               jffs2_free_raw_inode(ri);
+               return ret;
+       }
+
+       inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
+
+       if (IS_ERR(inode)) {
+               jffs2_free_raw_inode(ri);
+               jffs2_complete_reservation(c);
+               return PTR_ERR(inode);
+       }
+
+       inode->i_op = &jffs2_symlink_inode_operations;
+
+       f = JFFS2_INODE_INFO(inode);
+
+       ri->dsize = ri->csize = strlen(target);
+       ri->totlen = sizeof(*ri) + ri->dsize;
+       ri->hdr_crc = crc32(0, ri, sizeof(struct jffs2_unknown_node)-4);
+
+       ri->compr = JFFS2_COMPR_NONE;
+       ri->data_crc = crc32(0, target, strlen(target));
+       ri->node_crc = crc32(0, ri, sizeof(*ri)-8);
+       
+       fn = jffs2_write_dnode(inode, ri, target, strlen(target), phys_ofs, &writtenlen);
+
+       jffs2_free_raw_inode(ri);
+
+       if (IS_ERR(fn)) {
+               /* Eeek. Wave bye bye */
+               up(&f->sem);
+               jffs2_complete_reservation(c);
+               jffs2_clear_inode(inode);
+               return PTR_ERR(fn);
+       }
+       /* No data here. Only a metadata node, which will be 
+          obsoleted by the first data write
+       */
+       f->metadata = fn;
+       up(&f->sem);
+
+       /* Work out where to put the dirent node now. */
+       writtenlen = (writtenlen+3)&~3;
+       phys_ofs += writtenlen;
+       alloclen -= writtenlen;
+
+       if (alloclen < sizeof(*rd)+namelen) {
+               /* Not enough space left in this chunk. Get some more */
+               jffs2_complete_reservation(c);
+               ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
+               if (ret) {
+                       /* Eep. */
+                       jffs2_clear_inode(inode);
+                       return ret;
+               }
+       }
+
+       rd = jffs2_alloc_raw_dirent();
+       if (!rd) {
+               /* Argh. Now we treat it like a normal delete */
+               jffs2_complete_reservation(c);
+               jffs2_clear_inode(inode);
+               return -ENOMEM;
+       }
+
+       dir_f = JFFS2_INODE_INFO(dir_i);
+       down(&dir_f->sem);
+
+       rd->magic = JFFS2_MAGIC_BITMASK;
+       rd->nodetype = JFFS2_NODETYPE_DIRENT;
+       rd->totlen = sizeof(*rd) + namelen;
+       rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4);
+
+       rd->pino = dir_i->i_ino;
+       rd->version = dir_f->highest_version++;
+       rd->ino = inode->i_ino;
+       rd->mctime = CURRENT_TIME;
+       rd->nsize = namelen;
+       rd->type = DT_LNK;
+       rd->node_crc = crc32(0, rd, sizeof(*rd)-8);
+       rd->name_crc = crc32(0, dentry->d_name.name, namelen);
+
+       fd = jffs2_write_dirent(dir_i, rd, dentry->d_name.name, namelen, phys_ofs, &writtenlen);
+       
+       jffs2_complete_reservation(c);
+       jffs2_free_raw_dirent(rd);
+       
+       if (IS_ERR(fd)) {
+               /* dirent failed to write. Delete the inode normally 
+                  as if it were the final unlink() */
+               up(&dir_f->sem);
+               jffs2_clear_inode(inode);
+               return PTR_ERR(fd);
+       }
+
+       /* Link the fd into the inode's list, obsoleting an old
+          one if necessary. */
+       jffs2_add_fd_to_list(c, fd, &dir_f->dents);
+       up(&dir_f->sem);
+
+       d_instantiate(dentry, inode);
+       return 0;
+}
+
+
+static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
+{
+       struct jffs2_inode_info *f, *dir_f;
+       struct jffs2_sb_info *c;
+       struct inode *inode;
+       struct jffs2_raw_inode *ri;
+       struct jffs2_raw_dirent *rd;
+       struct jffs2_full_dnode *fn;
+       struct jffs2_full_dirent *fd;
+       int namelen;
+       __u32 alloclen, phys_ofs;
+       __u32 writtenlen;
+       int ret;
+
+       mode |= S_IFDIR;
+
+       ri = jffs2_alloc_raw_inode();
+       if (!ri)
+               return -ENOMEM;
+       
+       c = JFFS2_SB_INFO(dir_i->i_sb);
+
+       /* Try to reserve enough space for both node and dirent. 
+        * Just the node will do for now, though 
+        */
+       namelen = dentry->d_name.len;
+       ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
+
+       if (ret) {
+               jffs2_free_raw_inode(ri);
+               return ret;
+       }
+
+       inode = jffs2_new_inode(dir_i, mode, ri);
+
+       if (IS_ERR(inode)) {
+               jffs2_free_raw_inode(ri);
+               jffs2_complete_reservation(c);
+               return PTR_ERR(inode);
+       }
+
+       inode->i_op = &jffs2_dir_inode_operations;
+       inode->i_fop = &jffs2_dir_operations;
+
+       f = JFFS2_INODE_INFO(inode);
+
+       ri->data_crc = 0;
+       ri->node_crc = crc32(0, ri, sizeof(*ri)-8);
+       
+       fn = jffs2_write_dnode(inode, ri, NULL, 0, phys_ofs, &writtenlen);
+
+       jffs2_free_raw_inode(ri);
+
+       if (IS_ERR(fn)) {
+               /* Eeek. Wave bye bye */
+               up(&f->sem);
+               jffs2_complete_reservation(c);
+               jffs2_clear_inode(inode);
+               return PTR_ERR(fn);
+       }
+       /* No data here. Only a metadata node, which will be 
+          obsoleted by the first data write
+       */
+       f->metadata = fn;
+       up(&f->sem);
+
+       /* Work out where to put the dirent node now. */
+       writtenlen = PAD(writtenlen);
+       phys_ofs += writtenlen;
+       alloclen -= writtenlen;
+
+       if (alloclen < sizeof(*rd)+namelen) {
+               /* Not enough space left in this chunk. Get some more */
+               jffs2_complete_reservation(c);
+               ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
+               if (ret) {
+                       /* Eep. */
+                       jffs2_clear_inode(inode);
+                       return ret;
+               }
+       }
+       
+       rd = jffs2_alloc_raw_dirent();
+       if (!rd) {
+               /* Argh. Now we treat it like a normal delete */
+               jffs2_complete_reservation(c);
+               jffs2_clear_inode(inode);
+               return -ENOMEM;
+       }
+
+       dir_f = JFFS2_INODE_INFO(dir_i);
+       down(&dir_f->sem);
+
+       rd->magic = JFFS2_MAGIC_BITMASK;
+       rd->nodetype = JFFS2_NODETYPE_DIRENT;
+       rd->totlen = sizeof(*rd) + namelen;
+       rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4);
+
+       rd->pino = dir_i->i_ino;
+       rd->version = dir_f->highest_version++;
+       rd->ino = inode->i_ino;
+       rd->mctime = CURRENT_TIME;
+       rd->nsize = namelen;
+       rd->type = DT_DIR;
+       rd->node_crc = crc32(0, rd, sizeof(*rd)-8);
+       rd->name_crc = crc32(0, dentry->d_name.name, namelen);
+
+       fd = jffs2_write_dirent(dir_i, rd, dentry->d_name.name, namelen, phys_ofs, &writtenlen);
+       
+       jffs2_complete_reservation(c);
+       jffs2_free_raw_dirent(rd);
+       
+       if (IS_ERR(fd)) {
+               /* dirent failed to write. Delete the inode normally 
+                  as if it were the final unlink() */
+               up(&dir_f->sem);
+               jffs2_clear_inode(inode);
+               return PTR_ERR(fd);
+       }
+
+       /* Link the fd into the inode's list, obsoleting an old
+          one if necessary. */
+       jffs2_add_fd_to_list(c, fd, &dir_f->dents);
+       up(&dir_f->sem);
+
+       d_instantiate(dentry, inode);
+       return 0;
+}
+
+static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
+{
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
+       struct jffs2_full_dirent *fd;
+
+       for (fd = f->dents ; fd; fd = fd->next) {
+               if (fd->ino)
+                       return -ENOTEMPTY;
+       }
+       return jffs2_unlink(dir_i, dentry);
+}
+
+static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, int rdev)
+{
+       struct jffs2_inode_info *f, *dir_f;
+       struct jffs2_sb_info *c;
+       struct inode *inode;
+       struct jffs2_raw_inode *ri;
+       struct jffs2_raw_dirent *rd;
+       struct jffs2_full_dnode *fn;
+       struct jffs2_full_dirent *fd;
+       int namelen;
+       unsigned short dev;
+       int devlen = 0;
+       __u32 alloclen, phys_ofs;
+       __u32 writtenlen;
+       int ret;
+
+       ri = jffs2_alloc_raw_inode();
+       if (!ri)
+               return -ENOMEM;
+       
+       c = JFFS2_SB_INFO(dir_i->i_sb);
+       
+       if ((mode & S_IFMT) == S_IFBLK ||
+           (mode & S_IFMT) == S_IFCHR) {
+               dev = (MAJOR(to_kdev_t(rdev)) << 8) | MINOR(to_kdev_t(rdev));
+               devlen = sizeof(dev);
+       }
+       
+       /* Try to reserve enough space for both node and dirent. 
+        * Just the node will do for now, though 
+        */
+       namelen = dentry->d_name.len;
+       ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &phys_ofs, &alloclen, ALLOC_NORMAL);
+
+       if (ret) {
+               jffs2_free_raw_inode(ri);
+               return ret;
+       }
+
+       inode = jffs2_new_inode(dir_i, mode, ri);
+
+       if (IS_ERR(inode)) {
+               jffs2_free_raw_inode(ri);
+               jffs2_complete_reservation(c);
+               return PTR_ERR(inode);
+       }
+       inode->i_op = &jffs2_file_inode_operations;
+       init_special_inode(inode, inode->i_mode, rdev);
+
+       f = JFFS2_INODE_INFO(inode);
+
+       ri->dsize = ri->csize = devlen;
+       ri->totlen = sizeof(*ri) + ri->csize;
+       ri->hdr_crc = crc32(0, ri, sizeof(struct jffs2_unknown_node)-4);
+
+       ri->compr = JFFS2_COMPR_NONE;
+       ri->data_crc = crc32(0, &dev, devlen);
+       ri->node_crc = crc32(0, ri, sizeof(*ri)-8);
+       
+       fn = jffs2_write_dnode(inode, ri, (char *)&dev, devlen, phys_ofs, &writtenlen);
+
+       jffs2_free_raw_inode(ri);
+
+       if (IS_ERR(fn)) {
+               /* Eeek. Wave bye bye */
+               up(&f->sem);
+               jffs2_complete_reservation(c);
+               jffs2_clear_inode(inode);
+               return PTR_ERR(fn);
+       }
+       /* No data here. Only a metadata node, which will be 
+          obsoleted by the first data write
+       */
+       f->metadata = fn;
+       up(&f->sem);
+
+       /* Work out where to put the dirent node now. */
+       writtenlen = (writtenlen+3)&~3;
+       phys_ofs += writtenlen;
+       alloclen -= writtenlen;
+
+       if (alloclen < sizeof(*rd)+namelen) {
+               /* Not enough space left in this chunk. Get some more */
+               jffs2_complete_reservation(c);
+               ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
+               if (ret) {
+                       /* Eep. */
+                       jffs2_clear_inode(inode);
+                       return ret;
+               }
+       }
+
+       rd = jffs2_alloc_raw_dirent();
+       if (!rd) {
+               /* Argh. Now we treat it like a normal delete */
+               jffs2_complete_reservation(c);
+               jffs2_clear_inode(inode);
+               return -ENOMEM;
+       }
+
+       dir_f = JFFS2_INODE_INFO(dir_i);
+       down(&dir_f->sem);
+
+       rd->magic = JFFS2_MAGIC_BITMASK;
+       rd->nodetype = JFFS2_NODETYPE_DIRENT;
+       rd->totlen = sizeof(*rd) + namelen;
+       rd->hdr_crc = crc32(0, rd, sizeof(struct jffs2_unknown_node)-4);
+
+       rd->pino = dir_i->i_ino;
+       rd->version = dir_f->highest_version++;
+       rd->ino = inode->i_ino;
+       rd->mctime = CURRENT_TIME;
+       rd->nsize = namelen;
+
+       /* XXX: This is ugly. */
+       rd->type = (mode & S_IFMT) >> 12;
+
+       rd->node_crc = crc32(0, rd, sizeof(*rd)-8);
+       rd->name_crc = crc32(0, dentry->d_name.name, namelen);
+
+       fd = jffs2_write_dirent(dir_i, rd, dentry->d_name.name, namelen, phys_ofs, &writtenlen);
+       
+       jffs2_complete_reservation(c);
+       jffs2_free_raw_dirent(rd);
+       
+       if (IS_ERR(fd)) {
+               /* dirent failed to write. Delete the inode normally 
+                  as if it were the final unlink() */
+               up(&dir_f->sem);
+               jffs2_clear_inode(inode);
+               return PTR_ERR(fd);
+       }
+
+       /* Link the fd into the inode's list, obsoleting an old
+          one if necessary. */
+       jffs2_add_fd_to_list(c, fd, &dir_f->dents);
+       up(&dir_f->sem);
+
+       d_instantiate(dentry, inode);
+       return 0;
+}
+
+static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
+                        struct inode *new_dir_i, struct dentry *new_dentry)
+{
+       int ret;
+       /* XXX: We probably ought to alloc enough space for
+          both nodes at the same time. Writing the new link, 
+          then getting -ENOSPC, is quite bad :)
+       */
+
+       /* Make a hard link */
+       ret = jffs2_do_link(old_dentry, new_dir_i, new_dentry, 1);
+       if (ret)
+               return ret;
+
+       /* Unlink the original */
+       ret = jffs2_do_unlink(old_dir_i, old_dentry, 1);
+       
+       if (ret) {
+               /* Try to delete the _new_ link to return a clean failure */
+               int ret2 = jffs2_do_unlink(new_dir_i, new_dentry, 1);
+               if (ret2) {
+                       struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
+                       down(&f->sem);
+                       old_dentry->d_inode->i_nlink = f->inocache->nlink++;
+                       up(&f->sem);
+                      
+                       printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (old err %d, new err %d). You now have a hard link\n", ret, ret2);
+                       /* Might as well let the VFS know */
+                       d_instantiate(new_dentry, old_dentry->d_inode);
+                       atomic_inc(&old_dentry->d_inode->i_count);
+               }
+               
+       }
+       return ret;
+}
+
diff --git a/fs/jffs2/erase.c b/fs/jffs2/erase.c
new file mode 100644 (file)
index 0000000..cac673a
--- /dev/null
@@ -0,0 +1,346 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: erase.c,v 1.19 2001/03/25 22:36:12 dwmw2 Exp $
+ *
+ */
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/mtd/mtd.h>
+#include <linux/jffs2.h>
+#include <linux/interrupt.h>
+#include "nodelist.h"
+#include "crc32.h"
+
+struct erase_priv_struct {
+       struct jffs2_eraseblock *jeb;
+       struct jffs2_sb_info *c;
+};
+      
+static void jffs2_erase_callback(struct erase_info *);
+static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
+
+void jffs2_erase_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
+{
+       struct erase_info *instr;
+       int ret;
+
+       instr = kmalloc(sizeof(struct erase_info) + sizeof(struct erase_priv_struct), GFP_KERNEL);
+       if (!instr) {
+               printk(KERN_WARNING "kmalloc for struct erase_info in jffs2_erase_block failed. Refiling block for later\n");
+               spin_lock_bh(&c->erase_completion_lock);
+               list_del(&jeb->list);
+               list_add(&jeb->list, &c->erase_pending_list);
+               c->erasing_size -= c->sector_size;
+               spin_unlock_bh(&c->erase_completion_lock);
+               return;
+       }
+
+       memset(instr, 0, sizeof(*instr));
+
+       instr->mtd = c->mtd;
+       instr->addr = jeb->offset;
+       instr->len = c->sector_size;
+       instr->callback = jffs2_erase_callback;
+       instr->priv = (unsigned long)(&instr[1]);
+       
+       ((struct erase_priv_struct *)instr->priv)->jeb = jeb;
+       ((struct erase_priv_struct *)instr->priv)->c = c;
+
+       ret = c->mtd->erase(c->mtd, instr);
+       if (!ret) {
+               return;
+       }
+       if (ret == -ENOMEM || ret == -EAGAIN) {
+               /* Erase failed immediately. Refile it on the list */
+               D1(printk(KERN_DEBUG "Erase at 0x%08x failed: %d. Refiling on erase_pending_list\n", jeb->offset, ret));
+               spin_lock_bh(&c->erase_completion_lock);
+               list_del(&jeb->list);
+               list_add(&jeb->list, &c->erase_pending_list);
+               c->erasing_size -= c->sector_size;
+               spin_unlock_bh(&c->erase_completion_lock);
+               kfree(instr);
+               return;
+       }
+
+       printk(KERN_WARNING "Erase at 0x%08x failed immediately: %d\n", jeb->offset, ret);
+       spin_lock_bh(&c->erase_completion_lock);
+       list_del(&jeb->list);
+       list_add(&jeb->list, &c->bad_list);
+       c->nr_erasing_blocks--;
+       c->bad_size += c->sector_size;
+       c->erasing_size -= c->sector_size;
+       spin_unlock_bh(&c->erase_completion_lock);
+       wake_up(&c->erase_wait);
+       kfree(instr);
+}
+
+void jffs2_erase_pending_blocks(struct jffs2_sb_info *c)
+{
+       struct jffs2_eraseblock *jeb;
+
+       spin_lock_bh(&c->erase_completion_lock);
+       while (!list_empty(&c->erase_pending_list)) {
+
+               jeb = list_entry(c->erase_pending_list.next, struct jffs2_eraseblock, list);
+
+               D1(printk(KERN_DEBUG "Starting erase of pending block 0x%08x\n", jeb->offset));
+
+               list_del(&jeb->list);
+               c->erasing_size += c->sector_size;
+               c->free_size -= jeb->free_size;
+               c->used_size -= jeb->used_size;
+               c->dirty_size -= jeb->dirty_size;
+               jeb->used_size = jeb->dirty_size = jeb->free_size = 0;
+               jffs2_free_all_node_refs(c, jeb);
+               list_add(&jeb->list, &c->erasing_list);
+               spin_unlock_bh(&c->erase_completion_lock);
+               
+               jffs2_erase_block(c, jeb);
+               /* Be nice */
+               if (current->need_resched)
+                       schedule();
+               spin_lock_bh(&c->erase_completion_lock);
+       }
+       spin_unlock_bh(&c->erase_completion_lock);
+       D1(printk(KERN_DEBUG "jffs2_erase_pending_blocks completed\n"));
+}
+
+
+static void jffs2_erase_callback(struct erase_info *instr)
+{
+       struct erase_priv_struct *priv = (void *)instr->priv;
+
+       if(instr->state != MTD_ERASE_DONE) {
+               printk(KERN_WARNING "Erase at 0x%08x finished, but state != MTD_ERASE_DONE. State is 0x%x instead.\n", instr->addr, instr->state);
+               spin_lock(&priv->c->erase_completion_lock);
+               priv->c->erasing_size -= priv->c->sector_size;
+               priv->c->bad_size += priv->c->sector_size;
+               list_del(&priv->jeb->list);
+               list_add(&priv->jeb->list, &priv->c->bad_list);
+               priv->c->nr_erasing_blocks--;
+               spin_unlock(&priv->c->erase_completion_lock);
+               wake_up(&priv->c->erase_wait);
+       } else {
+               D1(printk(KERN_DEBUG "Erase completed successfully at 0x%08lx\n", instr->addr));
+               spin_lock(&priv->c->erase_completion_lock);
+               list_del(&priv->jeb->list);
+               list_add_tail(&priv->jeb->list, &priv->c->erase_complete_list);
+               spin_unlock(&priv->c->erase_completion_lock);
+       }       
+       /* Make sure someone picks up the block off the erase_complete list */
+       OFNI_BS_2SFFJ(priv->c)->s_dirt = 1;
+       kfree(instr);
+}
+
+/* Hmmm. Maybe we should accept the extra space it takes and make
+   this a standard doubly-linked list? */
+static inline void jffs2_remove_node_ref_from_ino_list(struct jffs2_sb_info *sbinfo, struct jffs2_raw_node_ref *ref)
+{
+       struct jffs2_inode_cache *ic;
+       struct jffs2_raw_node_ref **prev, *this;
+       D2(int c=0);
+
+       this = ref;
+       while(this->next_in_ino)
+                this = this->next_in_ino;
+
+       ic = (struct jffs2_inode_cache *)this;
+
+       D1(printk(KERN_DEBUG "Removing node at phys 0x%08x from ino #%u\n", ref->flash_offset &~3, ic->ino));
+
+       prev = &ic->nodes;
+       if (!*prev) {
+               printk(KERN_WARNING "Eep. ic->nodes == NULL.\n");
+               return;
+       }
+       while (*prev != ref) {
+               if (!(*prev)->next_in_ino) {
+                       printk(KERN_WARNING "Eep. node at phys 0x%08x, mem %p. next_in_ino is NULL.\n", (*prev)->flash_offset &~3, 
+                              *prev);
+                       return;
+               }
+               prev = &(*prev)->next_in_ino;
+       }
+       *prev = ref->next_in_ino;
+       this = ic->nodes;
+       D2(printk(KERN_DEBUG "After remove_node_ref_from_ino_list: \n" KERN_DEBUG);
+       while(this) {
+               printk( "0x%08x(%d)->", this->flash_offset & ~3, this->flash_offset &3);
+               if (++c == 5) {
+                       printk("\n" KERN_DEBUG);
+                       c=0;
+               }
+               this = this->next_in_ino;
+       }
+       printk("\n"););
+       if (ic->nodes == (void *)ic) {
+               D1(printk(KERN_DEBUG "inocache for ino #%u is all gone now. Freeing\n", ic->ino));
+               jffs2_del_ino_cache(sbinfo, ic);
+               jffs2_free_inode_cache(ic);
+       }
+}
+
+static void jffs2_free_all_node_refs(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb)
+{
+       struct jffs2_raw_node_ref *ref;
+       D1(printk(KERN_DEBUG "Freeing all node refs for eraseblock offset 0x%08x\n", jeb->offset));
+       while(jeb->first_node) {
+               ref = jeb->first_node;
+               jeb->first_node = ref->next_phys;
+               
+               /* Remove from the inode-list */
+               if (ref->next_in_ino)
+                       jffs2_remove_node_ref_from_ino_list(c, ref);
+               /* else it was a non-inode node so don't bother */
+
+               jffs2_free_raw_node_ref(ref);
+       }
+       jeb->last_node = NULL;
+}
+
+void jffs2_erase_pending_trigger(struct jffs2_sb_info *c)
+{
+       OFNI_BS_2SFFJ(c)->s_dirt = 1;
+}
+
+void jffs2_mark_erased_blocks(struct jffs2_sb_info *c)
+{
+       static struct jffs2_unknown_node marker = {JFFS2_MAGIC_BITMASK, JFFS2_NODETYPE_CLEANMARKER, sizeof(struct jffs2_unknown_node)};
+       struct jffs2_eraseblock *jeb;
+       struct jffs2_raw_node_ref *marker_ref;
+       unsigned char *ebuf;
+       ssize_t retlen;
+       int ret;
+
+       marker.hdr_crc = crc32(0, &marker, sizeof(struct jffs2_unknown_node)-4);
+
+       spin_lock_bh(&c->erase_completion_lock);
+       while (!list_empty(&c->erase_complete_list)) {
+               jeb = list_entry(c->erase_complete_list.next, struct jffs2_eraseblock, list);
+               list_del(&jeb->list);
+               spin_unlock_bh(&c->erase_completion_lock);
+
+               marker_ref = jffs2_alloc_raw_node_ref();
+               if (!marker_ref) {
+                       printk(KERN_WARNING "Failed to allocate raw node ref for clean marker\n");
+                       /* Come back later */
+                       jffs2_erase_pending_trigger(c);
+                       return;
+               }
+
+               ebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
+               if (!ebuf) {
+                       printk(KERN_WARNING "Failed to allocate page buffer for verifying erase at 0x%08x. Assuming it worked\n", jeb->offset);
+               } else {
+                       __u32 ofs = jeb->offset;
+
+                       D1(printk(KERN_DEBUG "Verifying erase at 0x%08x\n", jeb->offset));
+                       while(ofs < jeb->offset + c->sector_size) {
+                               __u32 readlen = min(PAGE_SIZE, jeb->offset + c->sector_size - ofs);
+                               int i;
+
+                               ret = c->mtd->read(c->mtd, ofs, readlen, &retlen, ebuf);
+                               if (ret < 0) {
+                                       printk(KERN_WARNING "Read of newly-erased block at 0x%08x failed: %d. Putting on bad_list\n", ofs, ret);
+                                       goto bad;
+                               }
+                               if (retlen != readlen) {
+                                       printk(KERN_WARNING "Short read from newly-erased block at 0x%08x. Wanted %d, got %d\n", ofs, readlen, retlen);
+                                       goto bad;
+                               }
+                               for (i=0; i<readlen; i += sizeof(unsigned long)) {
+                                       /* It's OK. We know it's properly aligned */
+                                       unsigned long datum = *(unsigned long *)(&ebuf[i]);
+                                       if (datum + 1) {
+                                               printk(KERN_WARNING "Newly-erased block contained word 0x%lx at offset 0x%08x\n", datum, ofs + i);
+                                       bad: 
+                                               jffs2_free_raw_node_ref(marker_ref);
+                                               kfree(ebuf);
+                                       bad2:
+                                               spin_lock_bh(&c->erase_completion_lock);
+                                               c->erasing_size -= c->sector_size;
+                                               c->bad_size += c->sector_size;
+
+                                               list_add_tail(&jeb->list, &c->bad_list);
+                                               c->nr_erasing_blocks--;
+                                               spin_unlock_bh(&c->erase_completion_lock);
+                                               wake_up(&c->erase_wait);
+                                               return;
+                                       }
+                               }
+                               ofs += readlen;
+                       }
+                       kfree(ebuf);
+               }
+                                       
+               /* Write the erase complete marker */   
+               D1(printk(KERN_DEBUG "Writing erased marker to block at 0x%08x\n", jeb->offset));
+               ret = c->mtd->write(c->mtd, jeb->offset, sizeof(marker), &retlen, (char *)&marker);
+               if (ret) {
+                       printk(KERN_WARNING "Write clean marker to block at 0x%08x failed: %d\n",
+                              jeb->offset, ret);
+                       goto bad2;
+               }
+               if (retlen != sizeof(marker)) {
+                       printk(KERN_WARNING "Short write to newly-erased block at 0x%08x: Wanted %d, got %d\n",
+                              jeb->offset, sizeof(marker), retlen);
+                       goto bad2;
+               }
+
+               marker_ref->next_in_ino = NULL;
+               marker_ref->next_phys = NULL;
+               marker_ref->flash_offset = jeb->offset;
+               marker_ref->totlen = PAD(sizeof(marker));
+
+               jeb->first_node = jeb->last_node = marker_ref;
+
+               jeb->free_size = c->sector_size - marker_ref->totlen;
+               jeb->used_size = marker_ref->totlen;
+               jeb->dirty_size = 0;
+
+               spin_lock_bh(&c->erase_completion_lock);
+               c->erasing_size -= c->sector_size;
+               c->free_size += jeb->free_size;
+               c->used_size += jeb->used_size;
+
+               ACCT_SANITY_CHECK(c,jeb);
+               ACCT_PARANOIA_CHECK(jeb);
+
+               list_add_tail(&jeb->list, &c->free_list);
+               c->nr_erasing_blocks--;
+               c->nr_free_blocks++;
+               wake_up(&c->erase_wait);
+       }
+       spin_unlock_bh(&c->erase_completion_lock);
+}
diff --git a/fs/jffs2/file.c b/fs/jffs2/file.c
new file mode 100644 (file)
index 0000000..f6c2619
--- /dev/null
@@ -0,0 +1,541 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: file.c,v 1.55 2001/05/29 09:19:24 dwmw2 Exp $
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/fs.h>
+#include <linux/pagemap.h>
+#include <linux/jffs2.h>
+#include "nodelist.h"
+#include "crc32.h"
+
+extern int generic_file_open(struct inode *, struct file *) __attribute__((weak));
+
+
+int jffs2_null_fsync(struct file *filp, struct dentry *dentry, int datasync)
+{
+       /* Move along. Nothing to see here */
+       return 0;
+}
+
+struct file_operations jffs2_file_operations =
+{
+       llseek:         generic_file_llseek,
+       open:           generic_file_open,
+       read:           generic_file_read,
+       write:          generic_file_write,
+       ioctl:          jffs2_ioctl,
+       mmap:           generic_file_mmap,
+       fsync:          jffs2_null_fsync
+};
+
+/* jffs2_file_inode_operations */
+
+struct inode_operations jffs2_file_inode_operations =
+{
+       setattr:        jffs2_setattr
+};
+
+struct address_space_operations jffs2_file_address_operations =
+{
+       readpage:       jffs2_readpage,
+       prepare_write:  jffs2_prepare_write,
+       commit_write:   jffs2_commit_write
+};
+
+int jffs2_setattr (struct dentry *dentry, struct iattr *iattr)
+{
+       struct jffs2_full_dnode *old_metadata, *new_metadata;
+       struct inode *inode = dentry->d_inode;
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
+       struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
+       struct jffs2_raw_inode *ri;
+       unsigned short dev;
+       unsigned char *mdata = NULL;
+       int mdatalen = 0;
+       unsigned int ivalid;
+       __u32 phys_ofs, alloclen;
+       int ret;
+       D1(printk(KERN_DEBUG "jffs2_setattr(): ino #%lu\n", inode->i_ino));
+       ret = inode_change_ok(inode, iattr);
+       if (ret) 
+               return ret;
+
+       /* Special cases - we don't want more than one data node
+          for these types on the medium at any time. So setattr
+          must read the original data associated with the node
+          (i.e. the device numbers or the target name) and write
+          it out again with the appropriate data attached */
+       if ((inode->i_mode & S_IFMT) == S_IFBLK ||
+           (inode->i_mode & S_IFMT) == S_IFCHR) {
+               /* For these, we don't actually need to read the old node */
+               dev =  (MAJOR(to_kdev_t(dentry->d_inode->i_rdev)) << 8) | 
+                       MINOR(to_kdev_t(dentry->d_inode->i_rdev));
+               mdata = (char *)&dev;
+               mdatalen = sizeof(dev);
+               D1(printk(KERN_DEBUG "jffs2_setattr(): Writing %d bytes of kdev_t\n", mdatalen));
+       } else if ((inode->i_mode & S_IFMT) == S_IFLNK) {
+               mdatalen = f->metadata->size;
+               mdata = kmalloc(f->metadata->size, GFP_USER);
+               if (!mdata)
+                       return -ENOMEM;
+               ret = jffs2_read_dnode(c, f->metadata, mdata, 0, mdatalen);
+               if (ret) {
+                       kfree(mdata);
+                       return ret;
+               }
+               D1(printk(KERN_DEBUG "jffs2_setattr(): Writing %d bytes of symlink target\n", mdatalen));
+       }
+
+       ri = jffs2_alloc_raw_inode();
+       if (!ri) {
+               if ((inode->i_mode & S_IFMT) == S_IFLNK)
+                       kfree(mdata);
+               return -ENOMEM;
+       }
+               
+       ret = jffs2_reserve_space(c, sizeof(*ri) + mdatalen, &phys_ofs, &alloclen, ALLOC_NORMAL);
+       if (ret) {
+               jffs2_free_raw_inode(ri);
+               if ((inode->i_mode & S_IFMT) == S_IFLNK)
+                        kfree(mdata);
+               return ret;
+       }
+       down(&f->sem);
+        ivalid = iattr->ia_valid;
+       
+       ri->magic = JFFS2_MAGIC_BITMASK;
+       ri->nodetype = JFFS2_NODETYPE_INODE;
+       ri->totlen = sizeof(*ri) + mdatalen;
+       ri->hdr_crc = crc32(0, ri, sizeof(struct jffs2_unknown_node)-4);
+
+       ri->ino = inode->i_ino;
+       ri->version = ++f->highest_version;
+
+       ri->mode = (ivalid & ATTR_MODE)?iattr->ia_mode:inode->i_mode;
+       ri->uid = (ivalid & ATTR_UID)?iattr->ia_uid:inode->i_uid;
+       ri->gid = (ivalid & ATTR_GID)?iattr->ia_gid:inode->i_gid;
+
+       if (ivalid & ATTR_MODE && ri->mode & S_ISGID &&
+           !in_group_p(ri->gid) && !capable(CAP_FSETID))
+               ri->mode &= ~S_ISGID;
+
+       ri->isize = (ivalid & ATTR_SIZE)?iattr->ia_size:inode->i_size;
+       ri->atime = (ivalid & ATTR_ATIME)?iattr->ia_atime:inode->i_atime;
+       ri->mtime = (ivalid & ATTR_MTIME)?iattr->ia_mtime:inode->i_mtime;
+       ri->ctime = (ivalid & ATTR_CTIME)?iattr->ia_ctime:inode->i_ctime;
+
+       ri->offset = 0;
+       ri->csize = ri->dsize = mdatalen;
+       ri->compr = JFFS2_COMPR_NONE;
+       if (inode->i_size < ri->isize) {
+               /* It's an extension. Make it a hole node */
+               ri->compr = JFFS2_COMPR_ZERO;
+               ri->dsize = ri->isize - inode->i_size;
+               ri->offset = inode->i_size;
+       }
+       ri->node_crc = crc32(0, ri, sizeof(*ri)-8);
+       if (mdatalen)
+               ri->data_crc = crc32(0, mdata, mdatalen);
+       else
+               ri->data_crc = 0;
+
+       new_metadata = jffs2_write_dnode(inode, ri, mdata, mdatalen, phys_ofs, NULL);
+       if ((inode->i_mode & S_IFMT) == S_IFLNK)
+               kfree(mdata);
+
+       jffs2_complete_reservation(c);
+       
+       if (IS_ERR(new_metadata)) {
+               jffs2_free_raw_inode(ri);
+               up(&f->sem);
+               return PTR_ERR(new_metadata);
+       }
+       /* It worked. Update the inode */
+       inode->i_atime = ri->atime;
+       inode->i_ctime = ri->ctime;
+       inode->i_mtime = ri->mtime;
+       inode->i_mode = ri->mode;
+       inode->i_uid = ri->uid;
+       inode->i_gid = ri->gid;
+
+
+       old_metadata = f->metadata;
+
+       if (inode->i_size > ri->isize) {
+               vmtruncate(inode, ri->isize);
+               jffs2_truncate_fraglist (c, &f->fraglist, ri->isize);
+       }
+
+       if (inode->i_size < ri->isize) {
+               jffs2_add_full_dnode_to_inode(c, f, new_metadata);
+               inode->i_size = ri->isize;
+               f->metadata = NULL;
+       } else {
+               f->metadata = new_metadata;
+       }
+       if (old_metadata) {
+               jffs2_mark_node_obsolete(c, old_metadata->raw);
+               jffs2_free_full_dnode(old_metadata);
+       }
+       jffs2_free_raw_inode(ri);
+       up(&f->sem);
+       return 0;
+}
+
+int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg)
+{
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
+       struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
+       struct jffs2_node_frag *frag = f->fraglist;
+       __u32 offset = pg->index << PAGE_CACHE_SHIFT;
+       __u32 end = offset + PAGE_CACHE_SIZE;
+       unsigned char *pg_buf;
+       int ret;
+
+       D1(printk(KERN_DEBUG "jffs2_do_readpage_nolock(): ino #%lu, page at offset 0x%x\n", inode->i_ino, offset));
+
+       if (!PageLocked(pg))
+                PAGE_BUG(pg);
+
+       while(frag && frag->ofs + frag->size  <= offset) {
+               //              D1(printk(KERN_DEBUG "skipping frag %d-%d; before the region we care about\n", frag->ofs, frag->ofs + frag->size));
+               frag = frag->next;
+       }
+
+       pg_buf = kmap(pg);
+
+       /* XXX FIXME: Where a single physical node actually shows up in two
+          frags, we read it twice. Don't do that. */
+       /* Now we're pointing at the first frag which overlaps our page */
+       while(offset < end) {
+               D2(printk(KERN_DEBUG "jffs2_readpage: offset %d, end %d\n", offset, end));
+               if (!frag || frag->ofs > offset) {
+                       __u32 holesize = end - offset;
+                       if (frag) {
+                               D1(printk(KERN_NOTICE "Eep. Hole in ino %ld fraglist. frag->ofs = 0x%08x, offset = 0x%08x\n", inode->i_ino, frag->ofs, offset));
+                               holesize = min(holesize, frag->ofs - offset);
+                               D1(jffs2_print_frag_list(f));
+                       }
+                       D1(printk(KERN_DEBUG "Filling non-frag hole from %d-%d\n", offset, offset+holesize));
+                       memset(pg_buf, 0, holesize);
+                       pg_buf += holesize;
+                       offset += holesize;
+                       continue;
+               } else if (frag->ofs < offset && (offset & (PAGE_CACHE_SIZE-1)) != 0) {
+                       D1(printk(KERN_NOTICE "Eep. Overlap in ino #%ld fraglist. frag->ofs = 0x%08x, offset = 0x%08x\n",
+                                 inode->i_ino, frag->ofs, offset));
+                       D1(jffs2_print_frag_list(f));
+                       memset(pg_buf, 0, end - offset);
+                       ClearPageUptodate(pg);
+                       SetPageError(pg);
+                       kunmap(pg);
+                       return -EIO;
+               } else if (!frag->node) {
+                       __u32 holeend = min(end, frag->ofs + frag->size);
+                       D1(printk(KERN_DEBUG "Filling frag hole from %d-%d (frag 0x%x 0x%x)\n", offset, holeend, frag->ofs, frag->ofs + frag->size));
+                       memset(pg_buf, 0, holeend - offset);
+                       pg_buf += holeend - offset;
+                       offset = holeend;
+                       frag = frag->next;
+                       continue;
+               } else {
+                       __u32 readlen;
+                       readlen = min(frag->size, end - offset);
+                       D1(printk(KERN_DEBUG "Reading %d-%d from node at 0x%x\n", frag->ofs, frag->ofs+readlen, frag->node->raw->flash_offset & ~3));
+                       ret = jffs2_read_dnode(c, frag->node, pg_buf, frag->ofs - frag->node->ofs, readlen);
+                       D2(printk(KERN_DEBUG "node read done\n"));
+                       if (ret) {
+                               D1(printk(KERN_DEBUG"jffs2_readpage error %d\n",ret));
+                               memset(pg_buf, 0, frag->size);
+                               ClearPageUptodate(pg);
+                               SetPageError(pg);
+                               kunmap(pg);
+                               return ret;
+                       }
+               }
+               pg_buf += frag->size;
+               offset += frag->size;
+               frag = frag->next;
+               D2(printk(KERN_DEBUG "node read was OK. Looping\n"));
+       }
+       D2(printk(KERN_DEBUG "readpage finishing\n"));
+       SetPageUptodate(pg);
+       ClearPageError(pg);
+
+       flush_dcache_page(pg);
+
+       kunmap(pg);
+       D1(printk(KERN_DEBUG "readpage finished\n"));
+       return 0;
+}
+
+int jffs2_do_readpage_unlock(struct inode *inode, struct page *pg)
+{
+       int ret = jffs2_do_readpage_nolock(inode, pg);
+       UnlockPage(pg);
+       return ret;
+}
+
+
+int jffs2_readpage (struct file *filp, struct page *pg)
+{
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(filp->f_dentry->d_inode);
+       int ret;
+       
+       down(&f->sem);
+       ret = jffs2_do_readpage_unlock(filp->f_dentry->d_inode, pg);
+       up(&f->sem);
+       return ret;
+}
+
+int jffs2_prepare_write (struct file *filp, struct page *pg, unsigned start, unsigned end)
+{
+       struct inode *inode = filp->f_dentry->d_inode;
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
+       __u32 pageofs = pg->index << PAGE_CACHE_SHIFT;
+       int ret = 0;
+
+       down(&f->sem);
+       D1(printk(KERN_DEBUG "jffs2_prepare_write() nrpages %ld\n", inode->i_mapping->nrpages));
+
+       if (pageofs > inode->i_size) {
+               /* Make new hole frag from old EOF to new page */
+               struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
+               struct jffs2_raw_inode ri;
+               struct jffs2_full_dnode *fn;
+               __u32 phys_ofs, alloc_len;
+               
+               D1(printk(KERN_DEBUG "Writing new hole frag 0x%x-0x%x between current EOF and new page\n",
+                         (unsigned int)inode->i_size, pageofs));
+
+               ret = jffs2_reserve_space(c, sizeof(ri), &phys_ofs, &alloc_len, ALLOC_NORMAL);
+               if (ret) {
+                       up(&f->sem);
+                       return ret;
+               }
+               memset(&ri, 0, sizeof(ri));
+
+               ri.magic = JFFS2_MAGIC_BITMASK;
+               ri.nodetype = JFFS2_NODETYPE_INODE;
+               ri.totlen = sizeof(ri);
+               ri.hdr_crc = crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4);
+
+               ri.ino = f->inocache->ino;
+               ri.version = ++f->highest_version;
+               ri.mode = inode->i_mode;
+               ri.uid = inode->i_uid;
+               ri.gid = inode->i_gid;
+               ri.isize = max(inode->i_size, pageofs);
+               ri.atime = ri.ctime = ri.mtime = CURRENT_TIME;
+               ri.offset = inode->i_size;
+               ri.dsize = pageofs - inode->i_size;
+               ri.csize = 0;
+               ri.compr = JFFS2_COMPR_ZERO;
+               ri.node_crc = crc32(0, &ri, sizeof(ri)-8);
+               ri.data_crc = 0;
+               
+               fn = jffs2_write_dnode(inode, &ri, NULL, 0, phys_ofs, NULL);
+               jffs2_complete_reservation(c);
+               if (IS_ERR(fn)) {
+                       ret = PTR_ERR(fn);
+                       up(&f->sem);
+                       return ret;
+               }
+               ret = jffs2_add_full_dnode_to_inode(c, f, fn);
+               if (f->metadata) {
+                       jffs2_mark_node_obsolete(c, f->metadata->raw);
+                       jffs2_free_full_dnode(f->metadata);
+                       f->metadata = NULL;
+               }
+               if (ret) {
+                       D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in prepare_write, returned %d\n", ret));
+                       jffs2_mark_node_obsolete(c, fn->raw);
+                       jffs2_free_full_dnode(fn);
+                       up(&f->sem);
+                       return ret;
+               }
+               inode->i_size = pageofs;
+       }
+       
+
+       /* Read in the page if it wasn't already present */
+       if (!Page_Uptodate(pg) && (start || end < PAGE_SIZE))
+               ret = jffs2_do_readpage_nolock(inode, pg);
+       D1(printk(KERN_DEBUG "end prepare_write(). nrpages %ld\n", inode->i_mapping->nrpages));
+       up(&f->sem);
+       return ret;
+}
+
+int jffs2_commit_write (struct file *filp, struct page *pg, unsigned start, unsigned end)
+{
+       /* Actually commit the write from the page cache page we're looking at.
+        * For now, we write the full page out each time. It sucks, but it's simple
+        */
+       struct inode *inode = filp->f_dentry->d_inode;
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
+       struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
+       ssize_t newsize = max(filp->f_dentry->d_inode->i_size, (pg->index << PAGE_CACHE_SHIFT) + end);
+       __u32 file_ofs = (pg->index << PAGE_CACHE_SHIFT);
+       unsigned writelen = min(PAGE_CACHE_SIZE, newsize - file_ofs);
+       struct jffs2_raw_inode *ri;
+       int ret = 0;
+       ssize_t writtenlen = 0;
+
+       D1(printk(KERN_DEBUG "jffs2_commit_write(): ino #%lu, page at 0x%lx, range %d-%d, nrpages %ld\n", inode->i_ino, pg->index << PAGE_CACHE_SHIFT, start, end, filp->f_dentry->d_inode->i_mapping->nrpages));
+
+       ri = jffs2_alloc_raw_inode();
+       if (!ri)
+               return -ENOMEM;
+
+       while(writelen) {
+               struct jffs2_full_dnode *fn;
+               unsigned char *comprbuf = NULL;
+               unsigned char comprtype = JFFS2_COMPR_NONE;
+               __u32 phys_ofs, alloclen;
+               __u32 datalen, cdatalen;
+
+               D2(printk(KERN_DEBUG "jffs2_commit_write() loop: 0x%x to write to 0x%x\n", writelen, file_ofs));
+
+               ret = jffs2_reserve_space(c, sizeof(*ri) + JFFS2_MIN_DATA_LEN, &phys_ofs, &alloclen, ALLOC_NORMAL);
+               if (ret) {
+                       SetPageError(pg);
+                       D1(printk(KERN_DEBUG "jffs2_reserve_space returned %d\n", ret));
+                       break;
+               }
+               down(&f->sem);
+               datalen = writelen;
+               cdatalen = min(alloclen - sizeof(*ri), writelen);
+
+               comprbuf = kmalloc(cdatalen, GFP_KERNEL);
+               if (comprbuf) {
+                       comprtype = jffs2_compress(page_address(pg)+ (file_ofs & (PAGE_CACHE_SIZE-1)), comprbuf, &datalen, &cdatalen);
+               }
+               if (comprtype == JFFS2_COMPR_NONE) {
+                       /* Either compression failed, or the allocation of comprbuf failed */
+                       if (comprbuf)
+                               kfree(comprbuf);
+                       comprbuf = page_address(pg) + (file_ofs & (PAGE_CACHE_SIZE -1));
+                       datalen = cdatalen;
+               }
+               /* Now comprbuf points to the data to be written, be it compressed or not.
+                  comprtype holds the compression type, and comprtype == JFFS2_COMPR_NONE means
+                  that the comprbuf doesn't need to be kfree()d. 
+               */
+
+               ri->magic = JFFS2_MAGIC_BITMASK;
+               ri->nodetype = JFFS2_NODETYPE_INODE;
+               ri->totlen = sizeof(*ri) + cdatalen;
+               ri->hdr_crc = crc32(0, ri, sizeof(struct jffs2_unknown_node)-4);
+
+               ri->ino = inode->i_ino;
+               ri->version = ++f->highest_version;
+               ri->mode = inode->i_mode;
+               ri->uid = inode->i_uid;
+               ri->gid = inode->i_gid;
+               ri->isize = max(inode->i_size, file_ofs + datalen);
+               ri->atime = ri->ctime = ri->mtime = CURRENT_TIME;
+               ri->offset = file_ofs;
+               ri->csize = cdatalen;
+               ri->dsize = datalen;
+               ri->compr = comprtype;
+               ri->node_crc = crc32(0, ri, sizeof(*ri)-8);
+               ri->data_crc = crc32(0, comprbuf, cdatalen);
+
+               fn = jffs2_write_dnode(inode, ri, comprbuf, cdatalen, phys_ofs, NULL);
+
+               jffs2_complete_reservation(c);
+
+               if (comprtype != JFFS2_COMPR_NONE)
+                       kfree(comprbuf);
+
+               if (IS_ERR(fn)) {
+                       ret = PTR_ERR(fn);
+                       up(&f->sem);
+                       SetPageError(pg);
+                       break;
+               }
+               ret = jffs2_add_full_dnode_to_inode(c, f, fn);
+               if (f->metadata) {
+                       jffs2_mark_node_obsolete(c, f->metadata->raw);
+                       jffs2_free_full_dnode(f->metadata);
+                       f->metadata = NULL;
+               }
+               up(&f->sem);
+               if (ret) {
+                       /* Eep */
+                       D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in commit_write, returned %d\n", ret));
+                       jffs2_mark_node_obsolete(c, fn->raw);
+                       jffs2_free_full_dnode(fn);
+                       SetPageError(pg);
+                       break;
+               }
+               inode->i_size = ri->isize;
+               inode->i_blocks = (inode->i_size + 511) >> 9;
+               inode->i_ctime = inode->i_mtime = ri->ctime;
+               if (!datalen) {
+                       printk(KERN_WARNING "Eep. We didn't actually write any bloody data\n");
+                       ret = -EIO;
+                       SetPageError(pg);
+                       break;
+               }
+               D1(printk(KERN_DEBUG "increasing writtenlen by %d\n", datalen));
+               writtenlen += datalen;
+               file_ofs += datalen;
+               writelen -= datalen;
+       }
+
+       jffs2_free_raw_inode(ri);
+
+       if (writtenlen < end) {
+               /* generic_file_write has written more to the page cache than we've
+                  actually written to the medium. Mark the page !Uptodate so that 
+                  it gets reread */
+               D1(printk(KERN_DEBUG "jffs2_commit_write(): Not all bytes written. Marking page !uptodate\n"));
+               SetPageError(pg);
+               ClearPageUptodate(pg);
+       }
+       if (writtenlen <= start) {
+               /* We didn't even get to the start of the affected part */
+               ret = ret?ret:-ENOSPC;
+               D1(printk(KERN_DEBUG "jffs2_commit_write(): Only %x bytes written to page. start (%x) not reached, returning %d\n", writtenlen, start, ret));
+       }
+       writtenlen = min(end-start, writtenlen-start);
+
+       D1(printk(KERN_DEBUG "jffs2_commit_write() returning %d. nrpages is %ld\n",writtenlen?writtenlen:ret, inode->i_mapping->nrpages));
+       return writtenlen?writtenlen:ret;
+}
diff --git a/fs/jffs2/gc.c b/fs/jffs2/gc.c
new file mode 100644 (file)
index 0000000..6c7305f
--- /dev/null
@@ -0,0 +1,660 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: gc.c,v 1.51 2001/05/24 22:24:39 dwmw2 Exp $
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/mtd/mtd.h>
+#include <linux/slab.h>
+#include <linux/jffs2.h>
+#include <linux/sched.h>
+#include <linux/interrupt.h>
+#include <linux/pagemap.h>
+#include "nodelist.h"
+#include "crc32.h"
+
+static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
+                                       struct inode *inode, struct jffs2_full_dnode *fd);
+static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
+                                       struct inode *inode, struct jffs2_full_dirent *fd);
+static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
+                                       struct inode *inode, struct jffs2_full_dirent *fd);
+static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
+                                     struct inode *indeo, struct jffs2_full_dnode *fn,
+                                     __u32 start, __u32 end);
+static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
+                                      struct inode *inode, struct jffs2_full_dnode *fn,
+                                      __u32 start, __u32 end);
+
+/* Called with erase_completion_lock held */
+static struct jffs2_eraseblock *jffs2_find_gc_block(struct jffs2_sb_info *c)
+{
+       struct jffs2_eraseblock *ret;
+       struct list_head *nextlist = NULL;
+
+       /* Pick an eraseblock to garbage collect next. This is where we'll
+          put the clever wear-levelling algorithms. Eventually.  */
+       if (!list_empty(&c->bad_used_list) && c->nr_free_blocks > JFFS2_RESERVED_BLOCKS_GCBAD) {
+               D1(printk(KERN_DEBUG "Picking block from bad_used_list to GC next\n"));
+               nextlist = &c->bad_used_list;
+       } else if (jiffies % 100 && !list_empty(&c->dirty_list)) {
+               /* Most of the time, pick one off the dirty list */
+               D1(printk(KERN_DEBUG "Picking block from dirty_list to GC next\n"));
+               nextlist = &c->dirty_list;
+       } else if (!list_empty(&c->clean_list)) {
+               D1(printk(KERN_DEBUG "Picking block from clean_list to GC next\n"));
+               nextlist = &c->clean_list;
+       } else if (!list_empty(&c->dirty_list)) {
+               D1(printk(KERN_DEBUG "Picking block from dirty_list to GC next (clean_list was empty)\n"));
+
+               nextlist = &c->dirty_list;
+       } else {
+               /* Eep. Both were empty */
+               printk(KERN_NOTICE "jffs2: No clean _or_ dirty blocks to GC from! Where are they all?\n");
+               return NULL;
+       }
+
+       ret = list_entry(nextlist->next, struct jffs2_eraseblock, list);
+       list_del(&ret->list);
+       c->gcblock = ret;
+       ret->gc_node = ret->first_node;
+       if (!ret->gc_node) {
+               printk(KERN_WARNING "Eep. ret->gc_node for block at 0x%08x is NULL\n", ret->offset);
+               BUG();
+       }
+       return ret;
+}
+
+/* jffs2_garbage_collect_pass
+ * Make a single attempt to progress GC. Move one node, and possibly
+ * start erasing one eraseblock.
+ */
+int jffs2_garbage_collect_pass(struct jffs2_sb_info *c)
+{
+       struct jffs2_eraseblock *jeb;
+       struct jffs2_inode_info *f;
+       struct jffs2_raw_node_ref *raw;
+       struct jffs2_node_frag *frag;
+       struct jffs2_full_dnode *fn = NULL;
+       struct jffs2_full_dirent *fd;
+       __u32 start = 0, end = 0, nrfrags = 0;
+       __u32 inum;
+       struct inode *inode;
+       int ret = 0;
+
+       if (down_interruptible(&c->alloc_sem))
+               return -EINTR;
+
+       spin_lock_bh(&c->erase_completion_lock);
+
+       /* First, work out which block we're garbage-collecting */
+       jeb = c->gcblock;
+
+       if (!jeb)
+               jeb = jffs2_find_gc_block(c);
+
+       if (!jeb) {
+               printk(KERN_NOTICE "jffs2: Couldn't find erase block to garbage collect!\n");
+               spin_unlock_bh(&c->erase_completion_lock);
+               up(&c->alloc_sem);
+               return -EIO;
+       }
+
+       D1(printk(KERN_DEBUG "garbage collect from block at phys 0x%08x\n", jeb->offset));
+
+       if (!jeb->used_size)
+               goto eraseit;
+
+       raw = jeb->gc_node;
+                       
+       while(raw->flash_offset & 1) {
+               D1(printk(KERN_DEBUG "Node at 0x%08x is obsolete... skipping\n", raw->flash_offset &~3));
+               jeb->gc_node = raw = raw->next_phys;
+               if (!raw) {
+                       printk(KERN_WARNING "eep. End of raw list while still supposedly nodes to GC\n");
+                       printk(KERN_WARNING "erase block at 0x%08x. free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x\n", 
+                              jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size);
+                       spin_unlock_bh(&c->erase_completion_lock);
+                       up(&c->alloc_sem);
+                       BUG();
+               }
+       }
+       D1(printk(KERN_DEBUG "Going to garbage collect node at 0x%08x\n", raw->flash_offset &~3));
+       if (!raw->next_in_ino) {
+               /* Inode-less node. Clean marker, snapshot or something like that */
+               spin_unlock_bh(&c->erase_completion_lock);
+               jffs2_mark_node_obsolete(c, raw);
+               goto eraseit_lock;
+       }
+                                                    
+       inum = jffs2_raw_ref_to_inum(raw);
+       D1(printk(KERN_DEBUG "Inode number is #%u\n", inum));
+
+       spin_unlock_bh(&c->erase_completion_lock);
+
+       D1(printk(KERN_DEBUG "jffs2_garbage_collect_pass collecting from block @0x%08x. Node @0x%08x, ino #%u\n", jeb->offset, raw->flash_offset&~3, inum));
+
+       inode = iget(OFNI_BS_2SFFJ(c), inum);
+       if (is_bad_inode(inode)) {
+               printk(KERN_NOTICE "Eep. read_inode() failed for ino #%u\n", inum);
+               /* NB. This will happen again. We need to do something appropriate here. */
+               iput(inode);
+               up(&c->alloc_sem);
+               return -EIO;
+       }
+
+       f = JFFS2_INODE_INFO(inode);
+       down(&f->sem);
+       /* Now we have the lock for this inode. Check that it's still the one at the head
+          of the list. */
+
+       if (raw->flash_offset & 1) {
+               D1(printk(KERN_DEBUG "node to be GC'd was obsoleted in the meantime.\n"));
+               /* They'll call again */
+               goto upnout;
+       }
+       /* OK. Looks safe. And nobody can get us now because we have the semaphore. Move the block */
+       if (f->metadata && f->metadata->raw == raw) {
+               fn = f->metadata;
+               ret = jffs2_garbage_collect_metadata(c, jeb, inode, fn);
+               goto upnout;
+       }
+       
+       for (frag = f->fraglist; frag; frag = frag->next) {
+               if (frag->node && frag->node->raw == raw) {
+                       fn = frag->node;
+                       end = frag->ofs + frag->size;
+                       if (!nrfrags++)
+                               start = frag->ofs;
+                       if (nrfrags == frag->node->frags)
+                               break; /* We've found them all */
+               }
+       }
+       if (fn) {
+               /* We found a datanode. Do the GC */
+               if((start >> PAGE_CACHE_SHIFT) < ((end-1) >> PAGE_CACHE_SHIFT)) {
+                       /* It crosses a page boundary. Therefore, it must be a hole. */
+                       ret = jffs2_garbage_collect_hole(c, jeb, inode, fn, start, end);
+               } else {
+                       /* It could still be a hole. But we GC the page this way anyway */
+                       ret = jffs2_garbage_collect_dnode(c, jeb, inode, fn, start, end);
+               }
+               goto upnout;
+       }
+       
+       /* Wasn't a dnode. Try dirent */
+       for (fd = f->dents; fd; fd=fd->next) {
+               if (fd->raw == raw)
+                       break;
+       }
+
+       if (fd && fd->ino) {
+               ret = jffs2_garbage_collect_dirent(c, jeb, inode, fd);
+       } else if (fd) {
+               ret = jffs2_garbage_collect_deletion_dirent(c, jeb, inode, fd);
+       } else {
+               printk(KERN_WARNING "Raw node at 0x%08x wasn't in node lists for ino #%lu\n", raw->flash_offset&~3, inode->i_ino);
+               if (raw->flash_offset & 1) {
+                       printk(KERN_WARNING "But it's obsolete so we don't mind too much\n");
+               } else {
+                       ret = -EIO;
+               }
+       }
+ upnout:
+       up(&f->sem);
+       iput(inode);
+
+ eraseit_lock:
+       /* If we've finished this block, start it erasing */
+       spin_lock_bh(&c->erase_completion_lock);
+
+ eraseit:
+       if (c->gcblock && !c->gcblock->used_size) {
+               D1(printk(KERN_DEBUG "Block at 0x%08x completely obsoleted by GC. Moving to erase_pending_list\n", c->gcblock->offset));
+               /* We're GC'ing an empty block? */
+               list_add_tail(&c->gcblock->list, &c->erase_pending_list);
+               c->gcblock = NULL;
+               c->nr_erasing_blocks++;
+               jffs2_erase_pending_trigger(c);
+       }
+       spin_unlock_bh(&c->erase_completion_lock);
+       up(&c->alloc_sem);
+
+       return ret;
+}
+
+static int jffs2_garbage_collect_metadata(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
+                                       struct inode *inode, struct jffs2_full_dnode *fn)
+{
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
+       struct jffs2_full_dnode *new_fn;
+       struct jffs2_raw_inode ri;
+       unsigned short dev;
+       char *mdata = NULL, mdatalen = 0;
+       __u32 alloclen, phys_ofs;
+       int ret;
+
+       if ((inode->i_mode & S_IFMT) == S_IFBLK ||
+           (inode->i_mode & S_IFMT) == S_IFCHR) {
+               /* For these, we don't actually need to read the old node */
+               dev =  (MAJOR(to_kdev_t(inode->i_rdev)) << 8) | 
+                       MINOR(to_kdev_t(inode->i_rdev));
+               mdata = (char *)&dev;
+               mdatalen = sizeof(dev);
+               D1(printk(KERN_DEBUG "jffs2_garbage_collect_metadata(): Writing %d bytes of kdev_t\n", mdatalen));
+       } else if ((inode->i_mode & S_IFMT) == S_IFLNK) {
+               mdatalen = fn->size;
+               mdata = kmalloc(fn->size, GFP_KERNEL);
+               if (!mdata) {
+                       printk(KERN_WARNING "kmalloc of mdata failed in jffs2_garbage_collect_metadata()\n");
+                       return -ENOMEM;
+               }
+               ret = jffs2_read_dnode(c, fn, mdata, 0, mdatalen);
+               if (ret) {
+                       printk(KERN_WARNING "read of old metadata failed in jffs2_garbage_collect_metadata(): %d\n", ret);
+                       kfree(mdata);
+                       return ret;
+               }
+               D1(printk(KERN_DEBUG "jffs2_garbage_collect_metadata(): Writing %d bites of symlink target\n", mdatalen));
+
+       }
+       
+       ret = jffs2_reserve_space_gc(c, sizeof(ri) + mdatalen, &phys_ofs, &alloclen);
+       if (ret) {
+               printk(KERN_WARNING "jffs2_reserve_space_gc of %d bytes for garbage_collect_metadata failed: %d\n",
+                      sizeof(ri)+ mdatalen, ret);
+               goto out;
+       }
+       
+       memset(&ri, 0, sizeof(ri));
+       ri.magic = JFFS2_MAGIC_BITMASK;
+       ri.nodetype = JFFS2_NODETYPE_INODE;
+       ri.totlen = sizeof(ri) + mdatalen;
+       ri.hdr_crc = crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4);
+
+       ri.ino = inode->i_ino;
+       ri.version = ++f->highest_version;
+       ri.mode = inode->i_mode;
+       ri.uid = inode->i_uid;
+       ri.gid = inode->i_gid;
+       ri.isize = inode->i_size;
+       ri.atime = inode->i_atime;
+       ri.ctime = inode->i_ctime;
+       ri.mtime = inode->i_mtime;
+       ri.offset = 0;
+       ri.csize = mdatalen;
+       ri.dsize = mdatalen;
+       ri.compr = JFFS2_COMPR_NONE;
+       ri.node_crc = crc32(0, &ri, sizeof(ri)-8);
+       ri.data_crc = crc32(0, mdata, mdatalen);
+
+       new_fn = jffs2_write_dnode(inode, &ri, mdata, mdatalen, phys_ofs, NULL);
+
+       if (IS_ERR(new_fn)) {
+               printk(KERN_WARNING "Error writing new dnode: %ld\n", PTR_ERR(new_fn));
+               ret = PTR_ERR(new_fn);
+               goto out;
+       }
+       jffs2_mark_node_obsolete(c, fn->raw);
+       jffs2_free_full_dnode(fn);
+       f->metadata = new_fn;
+ out:
+       if ((inode->i_mode & S_IFMT) == S_IFLNK)
+               kfree(mdata);
+       return ret;
+}
+
+static int jffs2_garbage_collect_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
+                                       struct inode *inode, struct jffs2_full_dirent *fd)
+{
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
+       struct jffs2_full_dirent *new_fd;
+       struct jffs2_raw_dirent rd;
+       __u32 alloclen, phys_ofs;
+       int ret;
+
+       rd.magic = JFFS2_MAGIC_BITMASK;
+       rd.nodetype = JFFS2_NODETYPE_DIRENT;
+       rd.nsize = strlen(fd->name);
+       rd.totlen = sizeof(rd) + rd.nsize;
+       rd.hdr_crc = crc32(0, &rd, sizeof(struct jffs2_unknown_node)-4);
+
+       rd.pino = inode->i_ino;
+       rd.version = ++f->highest_version;
+       rd.ino = fd->ino;
+       rd.mctime = max(inode->i_mtime, inode->i_ctime);
+       rd.type = fd->type;
+       rd.node_crc = crc32(0, &rd, sizeof(rd)-8);
+       rd.name_crc = crc32(0, fd->name, rd.nsize);
+       
+       ret = jffs2_reserve_space_gc(c, sizeof(rd)+rd.nsize, &phys_ofs, &alloclen);
+       if (ret) {
+               printk(KERN_WARNING "jffs2_reserve_space_gc of %d bytes for garbage_collect_dirent failed: %d\n",
+                      sizeof(rd)+rd.nsize, ret);
+               return ret;
+       }
+       new_fd = jffs2_write_dirent(inode, &rd, fd->name, rd.nsize, phys_ofs, NULL);
+
+       if (IS_ERR(new_fd)) {
+               printk(KERN_WARNING "jffs2_write_dirent in garbage_collect_dirent failed: %ld\n", PTR_ERR(new_fd));
+               return PTR_ERR(new_fd);
+       }
+       jffs2_add_fd_to_list(c, new_fd, &f->dents);
+       return 0;
+}
+
+static int jffs2_garbage_collect_deletion_dirent(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, 
+                                       struct inode *inode, struct jffs2_full_dirent *fd)
+{
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
+       struct jffs2_full_dirent **fdp = &f->dents;
+       int found = 0;
+
+       /* FIXME: When we run on NAND flash, we need to work out whether
+          this deletion dirent is still needed to actively delete a
+          'real' dirent with the same name that's still somewhere else
+          on the flash. For now, we know that we've actually obliterated
+          all the older dirents when they became obsolete, so we didn't
+          really need to write the deletion to flash in the first place.
+       */
+       while (*fdp) {
+               if ((*fdp) == fd) {
+                       found = 1;
+                       *fdp = fd->next;
+                       break;
+               }
+               fdp = &(*fdp)->next;
+       }
+       if (!found) {
+               printk(KERN_WARNING "Deletion dirent \"%s\" not found in list for ino #%lu\n", fd->name, inode->i_ino);
+       }
+       jffs2_mark_node_obsolete(c, fd->raw);
+       jffs2_free_full_dirent(fd);
+       return 0;
+}
+
+static int jffs2_garbage_collect_hole(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
+                                     struct inode *inode, struct jffs2_full_dnode *fn,
+                                     __u32 start, __u32 end)
+{
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
+       struct jffs2_raw_inode ri;
+       struct jffs2_node_frag *frag;
+       struct jffs2_full_dnode *new_fn;
+       __u32 alloclen, phys_ofs;
+       int ret;
+
+       D1(printk(KERN_DEBUG "Writing replacement hole node for ino #%lu from offset 0x%x to 0x%x\n",
+                 inode->i_ino, start, end));
+       
+       memset(&ri, 0, sizeof(ri));
+
+       if(fn->frags > 1) {
+               size_t readlen;
+               __u32 crc;
+               /* It's partially obsoleted by a later write. So we have to 
+                  write it out again with the _same_ version as before */
+               ret = c->mtd->read(c->mtd, fn->raw->flash_offset & ~3, sizeof(ri), &readlen, (char *)&ri);
+               if (readlen != sizeof(ri) || ret) {
+                       printk(KERN_WARNING "Node read failed in jffs2_garbage_collect_hole. Ret %d, retlen %d. Data will be lost by writing new hold node\n", ret, readlen);
+                       goto fill;
+               }
+               if (ri.nodetype != JFFS2_NODETYPE_INODE) {
+                       printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had node type 0x%04x instead of JFFS2_NODETYPE_INODE(0x%04x)\n",
+                              fn->raw->flash_offset & ~3, ri.nodetype, JFFS2_NODETYPE_INODE);
+                       return -EIO;
+               }
+               if (ri.totlen != sizeof(ri)) {
+                       printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had totlen 0x%x instead of expected 0x%x\n",
+                              fn->raw->flash_offset & ~3, ri.totlen, sizeof(ri));
+                       return -EIO;
+               }
+               crc = crc32(0, &ri, sizeof(ri)-8);
+               if (crc != ri.node_crc) {
+                       printk(KERN_WARNING "jffs2_garbage_collect_hole: Node at 0x%08x had CRC 0x%08x which doesn't match calculated CRC 0x%08x\n",
+                              fn->raw->flash_offset & ~3, ri.node_crc, crc);
+                       /* FIXME: We could possibly deal with this by writing new holes for each frag */
+                       printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%lu will be lost\n", 
+                              start, end, inode->i_ino);
+                       goto fill;
+               }
+               if (ri.compr != JFFS2_COMPR_ZERO) {
+                       printk(KERN_WARNING "jffs2_garbage_collect_hole: Node 0x%08x wasn't a hole node!\n", fn->raw->flash_offset & ~3);
+                       printk(KERN_WARNING "Data in the range 0x%08x to 0x%08x of inode #%lu will be lost\n", 
+                              start, end, inode->i_ino);
+                       goto fill;
+               }
+       } else {
+       fill:
+               ri.magic = JFFS2_MAGIC_BITMASK;
+               ri.nodetype = JFFS2_NODETYPE_INODE;
+               ri.totlen = sizeof(ri);
+               ri.hdr_crc = crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4);
+
+               ri.ino = inode->i_ino;
+               ri.version = ++f->highest_version;
+               ri.offset = start;
+               ri.csize = end - start;
+               ri.dsize = 0;
+               ri.compr = JFFS2_COMPR_ZERO;
+       }
+       ri.mode = inode->i_mode;
+       ri.uid = inode->i_uid;
+       ri.gid = inode->i_gid;
+       ri.isize = inode->i_size;
+       ri.atime = inode->i_atime;
+       ri.ctime = inode->i_ctime;
+       ri.mtime = inode->i_mtime;
+       ri.data_crc = 0;
+       ri.node_crc = crc32(0, &ri, sizeof(ri)-8);
+
+       ret = jffs2_reserve_space_gc(c, sizeof(ri), &phys_ofs, &alloclen);
+       if (ret) {
+               printk(KERN_WARNING "jffs2_reserve_space_gc of %d bytes for garbage_collect_hole failed: %d\n",
+                      sizeof(ri), ret);
+               return ret;
+       }
+       new_fn = jffs2_write_dnode(inode, &ri, NULL, 0, phys_ofs, NULL);
+
+       if (IS_ERR(new_fn)) {
+               printk(KERN_WARNING "Error writing new hole node: %ld\n", PTR_ERR(new_fn));
+               return PTR_ERR(new_fn);
+       }
+       if (ri.version == f->highest_version) {
+               jffs2_add_full_dnode_to_inode(c, f, new_fn);
+               if (f->metadata) {
+                       jffs2_mark_node_obsolete(c, f->metadata->raw);
+                       jffs2_free_full_dnode(f->metadata);
+                       f->metadata = NULL;
+                       return 0;
+               }
+       }
+       for (frag = f->fraglist; frag; frag = frag->next) {
+               if (frag->ofs > fn->size + fn->ofs)
+                       break;
+               if (frag->node == fn) {
+                       frag->node = new_fn;
+                       new_fn->frags++;
+                       fn->frags--;
+               }
+       }
+       if (fn->frags) {
+               printk(KERN_WARNING "jffs2_garbage_collect_hole: Old node still has frags!\n");
+               BUG();
+       }
+       if (!new_fn->frags) {
+               printk(KERN_WARNING "jffs2_garbage_collect_hole: New node has no frags!\n");
+               BUG();
+       }
+               
+       jffs2_mark_node_obsolete(c, fn->raw);
+       jffs2_free_full_dnode(fn);
+       
+       return 0;
+}
+
+static int jffs2_garbage_collect_dnode(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb,
+                                      struct inode *inode, struct jffs2_full_dnode *fn,
+                                      __u32 start, __u32 end)
+{
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
+       struct jffs2_full_dnode *new_fn;
+       struct jffs2_raw_inode ri;
+       __u32 alloclen, phys_ofs, offset, orig_end;     
+       int ret = 0;
+       unsigned char *comprbuf = NULL, *writebuf;
+       struct page *pg;
+       unsigned char *pg_ptr;
+
+
+       memset(&ri, 0, sizeof(ri));
+
+       D1(printk(KERN_DEBUG "Writing replacement dnode for ino #%lu from offset 0x%x to 0x%x\n",
+                 inode->i_ino, start, end));
+
+       orig_end = end;
+
+
+       /* If we're looking at the last node in the block we're
+          garbage-collecting, we allow ourselves to merge as if the
+          block was already erasing. We're likely to be GC'ing a
+          partial page, and the next block we GC is likely to have
+          the other half of this page right at the beginning, which
+          means we'd expand it _then_, as nr_erasing_blocks would have
+          increased since we checked, and in doing so would obsolete 
+          the partial node which we'd have written here. Meaning that 
+          the GC would churn and churn, and just leave dirty blocks in
+          it's wake.
+       */
+       if(c->nr_free_blocks + c->nr_erasing_blocks > JFFS2_RESERVED_BLOCKS_GCMERGE - (fn->raw->next_phys?0:1)) {
+               /* Shitloads of space */
+               /* FIXME: Integrate this properly with GC calculations */
+               start &= ~(PAGE_CACHE_SIZE-1);
+               end = min(start + PAGE_CACHE_SIZE, inode->i_size);
+               D1(printk(KERN_DEBUG "Plenty of free space, so expanding to write from offset 0x%x to 0x%x\n",
+                         start, end));
+               if (end < orig_end) {
+                       printk(KERN_WARNING "Eep. jffs2_garbage_collect_dnode extended node to write, but it got smaller: start 0x%x, orig_end 0x%x, end 0x%x\n", start, orig_end, end);
+                       end = orig_end;
+               }
+       }
+       
+       /* First, use readpage() to read the appropriate page into the page cache */
+       /* Q: What happens if we actually try to GC the _same_ page for which commit_write()
+        *    triggered garbage collection in the first place?
+        * A: I _think_ it's OK. read_cache_page shouldn't deadlock, we'll write out the
+        *    page OK. We'll actually write it out again in commit_write, which is a little
+        *    suboptimal, but at least we're correct.
+        */
+       pg = read_cache_page(inode->i_mapping, start >> PAGE_CACHE_SHIFT, (void *)jffs2_do_readpage_unlock, inode);
+
+       if (IS_ERR(pg)) {
+               printk(KERN_WARNING "read_cache_page() returned error: %ld\n", PTR_ERR(pg));
+               return PTR_ERR(pg);
+       }
+       pg_ptr = (char *)kmap(pg);
+       comprbuf = kmalloc(end - start, GFP_KERNEL);
+
+       offset = start;
+       while(offset < orig_end) {
+               __u32 datalen;
+               __u32 cdatalen;
+               char comprtype = JFFS2_COMPR_NONE;
+
+               ret = jffs2_reserve_space_gc(c, sizeof(ri) + JFFS2_MIN_DATA_LEN, &phys_ofs, &alloclen);
+
+               if (ret) {
+                       printk(KERN_WARNING "jffs2_reserve_space_gc of %d bytes for garbage_collect_dnode failed: %d\n",
+                              sizeof(ri)+ JFFS2_MIN_DATA_LEN, ret);
+                       break;
+               }
+               cdatalen = min(alloclen - sizeof(ri), end - offset);
+               datalen = end - offset;
+
+               writebuf = pg_ptr + (offset & (PAGE_CACHE_SIZE -1));
+
+               if (comprbuf) {
+                       comprtype = jffs2_compress(writebuf, comprbuf, &datalen, &cdatalen);
+               }
+               if (comprtype) {
+                       writebuf = comprbuf;
+               } else {
+                       datalen = cdatalen;
+               }
+               ri.magic = JFFS2_MAGIC_BITMASK;
+               ri.nodetype = JFFS2_NODETYPE_INODE;
+               ri.totlen = sizeof(ri) + cdatalen;
+               ri.hdr_crc = crc32(0, &ri, sizeof(struct jffs2_unknown_node)-4);
+
+               ri.ino = inode->i_ino;
+               ri.version = ++f->highest_version;
+               ri.mode = inode->i_mode;
+               ri.uid = inode->i_uid;
+               ri.gid = inode->i_gid;
+               ri.isize = inode->i_size;
+               ri.atime = inode->i_atime;
+               ri.ctime = inode->i_ctime;
+               ri.mtime = inode->i_mtime;
+               ri.offset = offset;
+               ri.csize = cdatalen;
+               ri.dsize = datalen;
+               ri.compr = comprtype;
+               ri.node_crc = crc32(0, &ri, sizeof(ri)-8);
+               ri.data_crc = crc32(0, writebuf, cdatalen);
+       
+               new_fn = jffs2_write_dnode(inode, &ri, writebuf, cdatalen, phys_ofs, NULL);
+
+               if (IS_ERR(new_fn)) {
+                       printk(KERN_WARNING "Error writing new dnode: %ld\n", PTR_ERR(new_fn));
+                       ret = PTR_ERR(new_fn);
+                       break;
+               }
+               ret = jffs2_add_full_dnode_to_inode(c, f, new_fn);
+               offset += datalen;
+               if (f->metadata) {
+                       jffs2_mark_node_obsolete(c, f->metadata->raw);
+                       jffs2_free_full_dnode(f->metadata);
+                       f->metadata = NULL;
+               }
+       }
+       if (comprbuf) kfree(comprbuf);
+
+       kunmap(pg);
+       /* XXX: Does the page get freed automatically? */
+       /* AAA: Judging by the unmount getting stuck in __wait_on_page, nope. */
+       page_cache_release(pg);
+       return ret;
+}
+
diff --git a/fs/jffs2/histo.h b/fs/jffs2/histo.h
new file mode 100644 (file)
index 0000000..84f184f
--- /dev/null
@@ -0,0 +1,3 @@
+/* This file provides the bit-probabilities for the input file */
+#define BIT_DIVIDER 629 
+static int bits[9] = { 179,167,183,165,159,198,178,119,}; /* ia32 .so files */
diff --git a/fs/jffs2/histo_mips.h b/fs/jffs2/histo_mips.h
new file mode 100644 (file)
index 0000000..9a44326
--- /dev/null
@@ -0,0 +1,2 @@
+#define BIT_DIVIDER_MIPS 1043 
+static int bits_mips[8] = { 277,249,290,267,229,341,212,241}; /* mips32 */
diff --git a/fs/jffs2/ioctl.c b/fs/jffs2/ioctl.c
new file mode 100644 (file)
index 0000000..38a52d3
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: ioctl.c,v 1.5 2001/03/15 15:38:24 dwmw2 Exp $
+ *
+ */
+
+#include <linux/fs.h>
+
+int jffs2_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, 
+               unsigned long arg)
+{
+       /* Later, this will provide for lsattr.jffs2 and chattr.jffs2, which
+          will include compression support etc. */
+       return -EINVAL;
+}
+       
diff --git a/fs/jffs2/malloc.c b/fs/jffs2/malloc.c
new file mode 100644 (file)
index 0000000..6ce14c9
--- /dev/null
@@ -0,0 +1,220 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: malloc.c,v 1.16 2001/03/15 15:38:24 dwmw2 Exp $
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/init.h>
+#include <linux/jffs2.h>
+#include "nodelist.h"
+
+#if 0
+#define JFFS2_SLAB_POISON SLAB_POISON
+#else
+#define JFFS2_SLAB_POISON 0
+#endif
+
+/* These are initialised to NULL in the kernel startup code.
+   If you're porting to other operating systems, beware */
+static kmem_cache_t *full_dnode_slab;
+static kmem_cache_t *raw_dirent_slab;
+static kmem_cache_t *raw_inode_slab;
+static kmem_cache_t *tmp_dnode_info_slab;
+static kmem_cache_t *raw_node_ref_slab;
+static kmem_cache_t *node_frag_slab;
+static kmem_cache_t *inode_cache_slab;
+
+void jffs2_free_tmp_dnode_info_list(struct jffs2_tmp_dnode_info *tn)
+{
+       struct jffs2_tmp_dnode_info *next;
+
+       while (tn) {
+               next = tn;
+               tn = tn->next;
+               jffs2_free_full_dnode(next->fn);
+               jffs2_free_tmp_dnode_info(next);
+       }
+}
+
+void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd)
+{
+       struct jffs2_full_dirent *next;
+
+       while (fd) {
+               next = fd->next;
+               jffs2_free_full_dirent(fd);
+               fd = next;
+       }
+}
+
+int __init jffs2_create_slab_caches(void)
+{
+       full_dnode_slab = kmem_cache_create("jffs2_full_dnode", sizeof(struct jffs2_full_dnode), 0, JFFS2_SLAB_POISON, NULL, NULL);
+       if (!full_dnode_slab)
+               goto err;
+
+       raw_dirent_slab = kmem_cache_create("jffs2_raw_dirent", sizeof(struct jffs2_raw_dirent), 0, JFFS2_SLAB_POISON, NULL, NULL);
+       if (!raw_dirent_slab)
+               goto err;
+
+       raw_inode_slab = kmem_cache_create("jffs2_raw_inode", sizeof(struct jffs2_raw_inode), 0, JFFS2_SLAB_POISON, NULL, NULL);
+       if (!raw_inode_slab)
+               goto err;
+
+       tmp_dnode_info_slab = kmem_cache_create("jffs2_tmp_dnode", sizeof(struct jffs2_tmp_dnode_info), 0, JFFS2_SLAB_POISON, NULL, NULL);
+       if (!tmp_dnode_info_slab)
+               goto err;
+
+       raw_node_ref_slab = kmem_cache_create("jffs2_raw_node_ref", sizeof(struct jffs2_raw_node_ref), 0, JFFS2_SLAB_POISON, NULL, NULL);
+       if (!raw_node_ref_slab)
+               goto err;
+
+       node_frag_slab = kmem_cache_create("jffs2_node_frag", sizeof(struct jffs2_node_frag), 0, JFFS2_SLAB_POISON, NULL, NULL);
+       if (!node_frag_slab)
+               goto err;
+
+       inode_cache_slab = kmem_cache_create("jffs2_inode_cache", sizeof(struct jffs2_inode_cache), 0, JFFS2_SLAB_POISON, NULL, NULL);
+
+       if (inode_cache_slab)
+               return 0;
+ err:
+       jffs2_destroy_slab_caches();
+       return -ENOMEM;
+}
+
+void jffs2_destroy_slab_caches(void)
+{
+       if(full_dnode_slab)
+               kmem_cache_destroy(full_dnode_slab);
+       if(raw_dirent_slab)
+               kmem_cache_destroy(raw_dirent_slab);
+       if(raw_inode_slab)
+               kmem_cache_destroy(raw_inode_slab);
+       if(tmp_dnode_info_slab)
+               kmem_cache_destroy(tmp_dnode_info_slab);
+       if(raw_node_ref_slab)
+               kmem_cache_destroy(raw_node_ref_slab);
+       if(node_frag_slab)
+               kmem_cache_destroy(node_frag_slab);
+       if(inode_cache_slab)
+               kmem_cache_destroy(inode_cache_slab);
+
+}
+
+struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize)
+{
+       return kmalloc(sizeof(struct jffs2_full_dirent) + namesize, GFP_KERNEL);
+}
+
+void jffs2_free_full_dirent(struct jffs2_full_dirent *x)
+{
+       kfree(x);
+}
+
+struct jffs2_full_dnode *jffs2_alloc_full_dnode(void)
+{
+       void *ret = kmem_cache_alloc(full_dnode_slab, GFP_KERNEL);
+       return ret;
+}
+
+void jffs2_free_full_dnode(struct jffs2_full_dnode *x)
+{
+       kmem_cache_free(full_dnode_slab, x);
+}
+
+struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void)
+{
+       return kmem_cache_alloc(raw_dirent_slab, GFP_KERNEL);
+}
+
+void jffs2_free_raw_dirent(struct jffs2_raw_dirent *x)
+{
+       kmem_cache_free(raw_dirent_slab, x);
+}
+
+struct jffs2_raw_inode *jffs2_alloc_raw_inode(void)
+{
+       return kmem_cache_alloc(raw_inode_slab, GFP_KERNEL);
+}
+
+void jffs2_free_raw_inode(struct jffs2_raw_inode *x)
+{
+       kmem_cache_free(raw_inode_slab, x);
+}
+
+struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void)
+{
+       return kmem_cache_alloc(tmp_dnode_info_slab, GFP_KERNEL);
+}
+
+void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *x)
+{
+       kmem_cache_free(tmp_dnode_info_slab, x);
+}
+
+struct jffs2_raw_node_ref *jffs2_alloc_raw_node_ref(void)
+{
+       return kmem_cache_alloc(raw_node_ref_slab, GFP_KERNEL);
+}
+
+void jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *x)
+{
+       kmem_cache_free(raw_node_ref_slab, x);
+}
+
+struct jffs2_node_frag *jffs2_alloc_node_frag(void)
+{
+       return kmem_cache_alloc(node_frag_slab, GFP_KERNEL);
+}
+
+void jffs2_free_node_frag(struct jffs2_node_frag *x)
+{
+       kmem_cache_free(node_frag_slab, x);
+}
+
+struct jffs2_inode_cache *jffs2_alloc_inode_cache(void)
+{
+       struct jffs2_inode_cache *ret = kmem_cache_alloc(inode_cache_slab, GFP_KERNEL);
+       D1(printk(KERN_DEBUG "Allocated inocache at %p\n", ret));
+       return ret;
+}
+
+void jffs2_free_inode_cache(struct jffs2_inode_cache *x)
+{
+       D1(printk(KERN_DEBUG "Freeing inocache at %p\n", x));
+       kmem_cache_free(inode_cache_slab, x);
+}
+
diff --git a/fs/jffs2/nodelist.c b/fs/jffs2/nodelist.c
new file mode 100644 (file)
index 0000000..d17e25e
--- /dev/null
@@ -0,0 +1,358 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: nodelist.c,v 1.28 2001/03/27 06:20:04 dwmw2 Exp $
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/jffs2.h>
+#include <linux/fs.h>
+#include <linux/mtd/mtd.h>
+#include "nodelist.h"
+
+#if 0
+/**
+ *     jffs2_add_raw_node_ref - Add a jffs2_raw_node_ref to the cached
+ *                             node list for the filesystem
+ *     @sb: Pointer to filesystem information structure
+ *     @ref: New node to add
+ *
+ *     Adds a new node reference to the filesystem\'s node cache, which
+ *     is the only permanent storage required for the filesystem.
+ */
+void jffs2_add_raw_node_ref(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref)
+{
+       /* Sort the list, hash it or do _something_ useful with it */
+       spin_lock(&c->nodelist_lock);
+       ref->next = c->nodelist;
+       c->nodelist = ref;
+       spin_unlock(&c->nodelist_lock);
+       return 0;
+}
+#endif
+void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list)
+{
+       struct jffs2_full_dirent **prev = list;
+       D1(printk(KERN_DEBUG "jffs2_add_fd_to_list( %p, %p (->%p))\n", new, list, *list));
+
+       while ((*prev) && (*prev)->nhash <= new->nhash) {
+               if ((*prev)->nhash == new->nhash && !strcmp((*prev)->name, new->name)) {
+                       /* Duplicate. Free one */
+                       if (new->version < (*prev)->version) {
+                               D1(printk(KERN_DEBUG "Eep! Marking new dirent node obsolete\n"));
+                               D1(printk(KERN_DEBUG "New dirent is \"%s\"->ino #%u. Old is \"%s\"->ino #%u\n", new->name, new->ino, (*prev)->name, (*prev)->ino));
+                               jffs2_mark_node_obsolete(c, new->raw);
+                               jffs2_free_full_dirent(new);
+                       } else {
+                               D1(printk(KERN_DEBUG "Marking old dirent node (ino #%u) obsolete\n", (*prev)->ino));
+                               new->next = (*prev)->next;
+                               jffs2_mark_node_obsolete(c, ((*prev)->raw));
+                               jffs2_free_full_dirent(*prev);
+                               *prev = new;
+                       }
+                       goto out;
+               }
+               prev = &((*prev)->next);
+       }
+       new->next = *prev;
+       *prev = new;
+
+ out:
+       D1(while(*list) {
+               printk(KERN_DEBUG "Dirent \"%s\" (hash 0x%08x, ino #%u\n", (*list)->name, (*list)->nhash, (*list)->ino);
+               list = &(*list)->next;
+       })
+}
+
+/* Put a new tmp_dnode_info into the list, keeping the list in 
+   order of increasing version
+*/
+void jffs2_add_tn_to_list(struct jffs2_tmp_dnode_info *tn, struct jffs2_tmp_dnode_info **list)
+{
+       struct jffs2_tmp_dnode_info **prev = list;
+       
+       while ((*prev) && (*prev)->version < tn->version) {
+               prev = &((*prev)->next);
+       }
+       tn->next = (*prev);
+        *prev = tn;
+}
+
+/* Get tmp_dnode_info and full_dirent for all non-obsolete nodes associated
+   with this ino, returning the former in order of version */
+
+int jffs2_get_inode_nodes(struct jffs2_sb_info *c, ino_t ino, struct jffs2_inode_info *f,
+                         struct jffs2_tmp_dnode_info **tnp, struct jffs2_full_dirent **fdp,
+                         __u32 *highest_version)
+{
+       struct jffs2_raw_node_ref *ref = f->inocache->nodes;
+       struct jffs2_tmp_dnode_info *tn, *ret_tn = NULL;
+       struct jffs2_full_dirent *fd, *ret_fd = NULL;
+
+       union jffs2_node_union node;
+       size_t retlen;
+       int err;
+
+       D1(printk(KERN_DEBUG "jffs2_get_inode_nodes(): ino #%lu\n", ino));
+       if (!f->inocache->nodes) {
+               printk(KERN_WARNING "Eep. no nodes for ino #%lu\n", ino);
+       }
+       for (ref = f->inocache->nodes; ref && ref->next_in_ino; ref = ref->next_in_ino) {
+               /* Work out whether it's a data node or a dirent node */
+               if (ref->flash_offset & 1) {
+                       /* FIXME: On NAND flash we may need to read these */
+                       D1(printk(KERN_DEBUG "node at 0x%08x is obsoleted. Ignoring.\n", ref->flash_offset &~3));
+                       continue;
+               }
+               err = c->mtd->read(c->mtd, (ref->flash_offset & ~3), sizeof(node), &retlen, (void *)&node);
+               if (err) {
+                       printk(KERN_WARNING "error %d reading node at 0x%08x in get_inode_nodes()\n", err, (ref->flash_offset) & ~3);
+                       goto free_out;
+               }
+                       
+
+                       /* Check we've managed to read at least the common node header */
+               if (retlen < sizeof(node.u)) {
+                       printk(KERN_WARNING "short read in get_inode_nodes()\n");
+                       err = -EIO;
+                       goto free_out;
+               }
+                       
+               switch (node.u.nodetype) {
+               case JFFS2_NODETYPE_DIRENT:
+                       D1(printk(KERN_DEBUG "Node at %08x is a dirent node\n", ref->flash_offset &~3));
+                       if (retlen < sizeof(node.d)) {
+                               printk(KERN_WARNING "short read in get_inode_nodes()\n");
+                               err = -EIO;
+                               goto free_out;
+                       }
+                       if (node.d.version > *highest_version)
+                               *highest_version = node.d.version;
+                       if (ref->flash_offset & 1) {
+                               /* Obsoleted */
+                               continue;
+                       }
+                       fd = jffs2_alloc_full_dirent(node.d.nsize+1);
+                       if (!fd) {
+                               err = -ENOMEM;
+                               goto free_out;
+                       }
+                       memset(fd,0,sizeof(struct jffs2_full_dirent) + node.d.nsize+1);
+                       fd->raw = ref;
+                       fd->version = node.d.version;
+                       fd->ino = node.d.ino;
+                       fd->type = node.d.type;
+                               /* memcpy as much of the name as possible from the raw
+                                  dirent we've already read from the flash
+                               */
+                       if (retlen > sizeof(struct jffs2_raw_dirent))
+                               memcpy(&fd->name[0], &node.d.name[0], min(node.d.nsize, retlen-sizeof(struct jffs2_raw_dirent)));
+                               
+                               /* Do we need to copy any more of the name directly
+                                  from the flash?
+                               */
+                       if (node.d.nsize + sizeof(struct jffs2_raw_dirent) > retlen) {
+                               int already = retlen - sizeof(struct jffs2_raw_dirent);
+                                       
+                               err = c->mtd->read(c->mtd, (ref->flash_offset & ~3) + retlen, 
+                                                  node.d.nsize - already, &retlen, &fd->name[already]);
+                               if (!err && retlen != node.d.nsize - already)
+                                       err = -EIO;
+                                       
+                               if (err) {
+                                       printk(KERN_WARNING "Read remainder of name in jffs2_get_inode_nodes(): error %d\n", err);
+                                       jffs2_free_full_dirent(fd);
+                                       goto free_out;
+                               }
+                       }
+                       fd->nhash = full_name_hash(fd->name, node.d.nsize);
+                       fd->next = NULL;
+                               /* Wheee. We now have a complete jffs2_full_dirent structure, with
+                                  the name in it and everything. Link it into the list 
+                               */
+                       D1(printk(KERN_DEBUG "Adding fd \"%s\", ino #%u\n", fd->name, fd->ino));
+                       jffs2_add_fd_to_list(c, fd, &ret_fd);
+                       break;
+
+               case JFFS2_NODETYPE_INODE:
+                       D1(printk(KERN_DEBUG "Node at %08x is a data node\n", ref->flash_offset &~3));
+                       if (retlen < sizeof(node.i)) {
+                               printk(KERN_WARNING "read too short for dnode\n");
+                               err = -EIO;
+                               goto free_out;
+                       }
+                       if (node.d.version > *highest_version)
+                               *highest_version = node.i.version;
+                       D1(printk(KERN_DEBUG "version %d, highest_version now %d\n", node.d.version, *highest_version));
+
+                       if (ref->flash_offset & 1) {
+                               D1(printk(KERN_DEBUG "obsoleted\n"));
+                               /* Obsoleted */
+                               continue;
+                       }
+                       tn = jffs2_alloc_tmp_dnode_info();
+                       if (!tn) {
+                               D1(printk(KERN_DEBUG "alloc tn failed\n"));
+                               err = -ENOMEM;
+                               goto free_out;
+                       }
+
+                       tn->fn = jffs2_alloc_full_dnode();
+                       if (!tn->fn) {
+                               D1(printk(KERN_DEBUG "alloc fn failed\n"));
+                               err = -ENOMEM;
+                               jffs2_free_tmp_dnode_info(tn);
+                               goto free_out;
+                       }
+                       tn->version = node.i.version;
+                       tn->fn->ofs = node.i.offset;
+                       tn->fn->size = node.i.dsize;
+                       tn->fn->raw = ref;
+                       D1(printk(KERN_DEBUG "dnode @%08x: ver %u, offset %04x, dsize %04x\n", ref->flash_offset &~3, node.i.version, node.i.offset, node.i.dsize));
+                       jffs2_add_tn_to_list(tn, &ret_tn);
+                       break;
+
+               default:
+                       switch(node.u.nodetype & JFFS2_COMPAT_MASK) {
+                       case JFFS2_FEATURE_INCOMPAT:
+                               printk(KERN_NOTICE "Unknown INCOMPAT nodetype %04X at %08X\n", node.u.nodetype, ref->flash_offset & ~3);
+                               break;
+                       case JFFS2_FEATURE_ROCOMPAT:
+                               printk(KERN_NOTICE "Unknown ROCOMPAT nodetype %04X at %08X\n", node.u.nodetype, ref->flash_offset & ~3);
+                               break;
+                       case JFFS2_FEATURE_RWCOMPAT_COPY:
+                               printk(KERN_NOTICE "Unknown RWCOMPAT_COPY nodetype %04X at %08X\n", node.u.nodetype, ref->flash_offset & ~3);
+                               break;
+                       case JFFS2_FEATURE_RWCOMPAT_DELETE:
+                               printk(KERN_NOTICE "Unknown RWCOMPAT_DELETE nodetype %04X at %08X\n", node.u.nodetype, ref->flash_offset & ~3);
+                               break;
+                       }
+               }
+       }
+       *tnp = ret_tn;
+       *fdp = ret_fd;
+       return 0;
+
+ free_out:
+       jffs2_free_tmp_dnode_info_list(ret_tn);
+       jffs2_free_full_dirent_list(ret_fd);
+       return err;
+}
+
+struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, int ino)
+{
+       struct jffs2_inode_cache *ret;
+
+       D2(printk(KERN_DEBUG "jffs2_get_ino_cache(): ino %u\n", ino));
+       spin_lock (&c->inocache_lock);
+       ret = c->inocache_list[ino % INOCACHE_HASHSIZE];
+       while (ret && ret->ino < ino) {
+               ret = ret->next;
+       }
+
+       spin_unlock(&c->inocache_lock);
+
+       if (ret && ret->ino != ino)
+               ret = NULL;
+
+       D2(printk(KERN_DEBUG "jffs2_get_ino_cache found %p for ino %u\n", ret, ino));
+       return ret;
+}
+
+void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new)
+{
+       struct jffs2_inode_cache **prev;
+       D2(printk(KERN_DEBUG "jffs2_add_ino_cache: Add %p (ino #%u)\n", new, new->ino));
+       spin_lock(&c->inocache_lock);
+       
+       prev = &c->inocache_list[new->ino % INOCACHE_HASHSIZE];
+
+       while ((*prev) && (*prev)->ino < new->ino) {
+               prev = &(*prev)->next;
+       }
+       new->next = *prev;
+       *prev = new;
+       spin_unlock(&c->inocache_lock);
+}
+
+void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old)
+{
+       struct jffs2_inode_cache **prev;
+       D2(printk(KERN_DEBUG "jffs2_del_ino_cache: Del %p (ino #%u)\n", old, old->ino));
+       spin_lock(&c->inocache_lock);
+       
+       prev = &c->inocache_list[old->ino % INOCACHE_HASHSIZE];
+       
+       while ((*prev) && (*prev)->ino < old->ino) {
+               prev = &(*prev)->next;
+       }
+       if ((*prev) == old) {
+               *prev = old->next;
+       }
+       spin_unlock(&c->inocache_lock);
+}
+
+void jffs2_free_ino_caches(struct jffs2_sb_info *c)
+{
+       int i;
+       struct jffs2_inode_cache *this, *next;
+       
+       for (i=0; i<INOCACHE_HASHSIZE; i++) {
+               this = c->inocache_list[i];
+               while (this) {
+                       next = this->next;
+                       D2(printk(KERN_DEBUG "jffs2_free_ino_caches: Freeing ino #%u at %p\n", this->ino, this));
+                       jffs2_free_inode_cache(this);
+                       this = next;
+               }
+               c->inocache_list[i] = NULL;
+       }
+}
+
+void jffs2_free_raw_node_refs(struct jffs2_sb_info *c)
+{
+       int i;
+       struct jffs2_raw_node_ref *this, *next;
+
+       for (i=0; i<c->nr_blocks; i++) {
+               this = c->blocks[i].first_node;
+               while(this) {
+                       next = this->next_phys;
+                       jffs2_free_raw_node_ref(this);
+                       this = next;
+               }
+               c->blocks[i].first_node = c->blocks[i].last_node = NULL;
+       }
+}
+       
diff --git a/fs/jffs2/nodelist.h b/fs/jffs2/nodelist.h
new file mode 100644 (file)
index 0000000..045d6e6
--- /dev/null
@@ -0,0 +1,354 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: nodelist.h,v 1.45 2001/03/20 17:43:57 dwmw2 Exp $
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/fs.h>
+
+#include <linux/jffs2_fs_sb.h>
+#include <linux/jffs2_fs_i.h>
+
+#ifndef CONFIG_JFFS2_FS_DEBUG
+#define CONFIG_JFFS2_FS_DEBUG 2
+#endif
+
+#if CONFIG_JFFS2_FS_DEBUG > 0
+#define D1(x) x
+#else
+#define D1(x)
+#endif
+
+#if CONFIG_JFFS2_FS_DEBUG > 1
+#define D2(x) x
+#else
+#define D2(x)
+#endif
+
+/*
+  This is all we need to keep in-core for each raw node during normal
+  operation. As and when we do read_inode on a particular inode, we can
+  scan the nodes which are listed for it and build up a proper map of 
+  which nodes are currently valid. JFFSv1 always used to keep that whole
+  map in core for each inode.
+*/
+struct jffs2_raw_node_ref
+{
+       struct jffs2_raw_node_ref *next_in_ino; /* Points to the next raw_node_ref
+               for this inode. If this is the last, it points to the inode_cache
+               for this inode instead. The inode_cache will have NULL in the first
+               word so you know when you've got there :) */
+       struct jffs2_raw_node_ref *next_phys;
+       //      __u32 ino;
+       __u32 flash_offset;
+       __u32 totlen;
+//     __u16 nodetype;
+       
+        /* flash_offset & 3 always has to be zero, because nodes are
+          always aligned at 4 bytes. So we have a couple of extra bits
+          to play with. So we set the least significant bit to 1 to
+          signify that the node is obsoleted by later nodes.
+       */
+};
+
+/* 
+   Used for keeping track of deletion nodes &c, which can only be marked
+   as obsolete when the node which they mark as deleted has actually been 
+   removed from the flash.
+*/
+struct jffs2_raw_node_ref_list {
+       struct jffs2_raw_node_ref *rew;
+       struct jffs2_raw_node_ref_list *next;
+};
+
+/* For each inode in the filesystem, we need to keep a record of
+   nlink, because it would be a PITA to scan the whole directory tree
+   at read_inode() time to calculate it, and to keep sufficient information
+   in the raw_node_ref (basically both parent and child inode number for 
+   dirent nodes) would take more space than this does. We also keep
+   a pointer to the first physical node which is part of this inode, too.
+*/
+struct jffs2_inode_cache {
+       struct jffs2_scan_info *scan; /* Used during scan to hold
+               temporary lists of nodes, and later must be set to
+               NULL to mark the end of the raw_node_ref->next_in_ino
+               chain. */
+       struct jffs2_inode_cache *next;
+       struct jffs2_raw_node_ref *nodes;
+       __u32 ino;
+       int nlink;
+};
+
+struct jffs2_scan_info {
+       struct jffs2_full_dirent *dents;
+       struct jffs2_tmp_dnode_info *tmpnodes;
+};
+/*
+  Larger representation of a raw node, kept in-core only when the 
+  struct inode for this particular ino is instantiated.
+*/
+
+struct jffs2_full_dnode
+{
+       struct jffs2_raw_node_ref *raw;
+       __u32 ofs; /* Don't really need this, but optimisation */
+       __u32 size;
+       __u32 frags; /* Number of fragments which currently refer
+                       to this node. When this reaches zero, 
+                       the node is obsolete.
+                    */
+};
+
+/* 
+   Even larger representation of a raw node, kept in-core only while
+   we're actually building up the original map of which nodes go where,
+   in read_inode()
+*/
+struct jffs2_tmp_dnode_info
+{
+       struct jffs2_tmp_dnode_info *next;
+       struct jffs2_full_dnode *fn;
+       __u32 version;
+};       
+
+struct jffs2_full_dirent
+{
+       struct jffs2_raw_node_ref *raw;
+       struct jffs2_full_dirent *next;
+       __u32 version;
+       __u32 ino; /* == zero for unlink */
+       unsigned int nhash;
+       unsigned char type;
+       unsigned char name[0];
+};
+/*
+  Fragments - used to build a map of which raw node to obtain 
+  data from for each part of the ino
+*/
+struct jffs2_node_frag
+{
+       struct jffs2_node_frag *next;
+       struct jffs2_full_dnode *node; /* NULL for holes */
+       __u32 size;
+       __u32 ofs; /* Don't really need this, but optimisation */
+       __u32 node_ofs; /* offset within the physical node */
+};
+
+struct jffs2_eraseblock
+{
+       struct list_head list;
+       int bad_count;
+       __u32 offset;           /* of this block in the MTD */
+
+       __u32 used_size;
+       __u32 dirty_size;
+       __u32 free_size;        /* Note that sector_size - free_size
+                                  is the address of the first free space */
+       struct jffs2_raw_node_ref *first_node;
+       struct jffs2_raw_node_ref *last_node;
+
+       struct jffs2_raw_node_ref *gc_node;     /* Next node to be garbage collected */
+
+       /* For deletia. When a dirent node in this eraseblock is
+          deleted by a node elsewhere, that other node can only 
+          be marked as obsolete when this block is actually erased.
+          So we keep a list of the nodes to mark as obsolete when
+          the erase is completed.
+       */
+       // MAYBE        struct jffs2_raw_node_ref_list *deletia;
+};
+
+#define ACCT_SANITY_CHECK(c, jeb) do { \
+       if (jeb->used_size + jeb->dirty_size + jeb->free_size != c->sector_size) { \
+               printk(KERN_NOTICE "Eeep. Space accounting for block at 0x%08x is screwed\n", jeb->offset); \
+               printk(KERN_NOTICE "free 0x%08x + dirty 0x%08x + used %08x != total %08x\n", \
+               jeb->free_size, jeb->dirty_size, jeb->used_size, c->sector_size); \
+               BUG(); \
+       } \
+       if (c->used_size + c->dirty_size + c->free_size + c->erasing_size + c->bad_size != c->flash_size) { \
+               printk(KERN_NOTICE "Eeep. Space accounting superblock info is screwed\n"); \
+               printk(KERN_NOTICE "free 0x%08x + dirty 0x%08x + used %08x + erasing %08x + bad %08x != total %08x\n", \
+               c->free_size, c->dirty_size, c->used_size, c->erasing_size, c->bad_size, c->flash_size); \
+               BUG(); \
+       } \
+} while(0)
+
+#define ACCT_PARANOIA_CHECK(jeb) do { \
+               __u32 my_used_size = 0; \
+               struct jffs2_raw_node_ref *ref2 = jeb->first_node; \
+               while (ref2) { \
+                       if (!(ref2->flash_offset & 1)) \
+                               my_used_size += ref2->totlen; \
+                       ref2 = ref2->next_phys; \
+               } \
+               if (my_used_size != jeb->used_size) { \
+                       printk(KERN_NOTICE "Calculated used size %08x != stored used size %08x\n", my_used_size, jeb->used_size); \
+                       BUG(); \
+               } \
+       } while(0)
+
+#define ALLOC_NORMAL   0       /* Normal allocation */
+#define ALLOC_DELETION 1       /* Deletion node. Best to allow it */
+#define ALLOC_GC       2       /* Space requested for GC. Give it or die */
+
+#define JFFS2_RESERVED_BLOCKS_BASE 3                                           /* Number of free blocks there must be before we... */
+#define JFFS2_RESERVED_BLOCKS_WRITE (JFFS2_RESERVED_BLOCKS_BASE + 2)           /* ... allow a normal filesystem write */
+#define JFFS2_RESERVED_BLOCKS_DELETION (JFFS2_RESERVED_BLOCKS_BASE + 1)                /* ... allow a normal filesystem deletion */
+#define JFFS2_RESERVED_BLOCKS_GCTRIGGER (JFFS2_RESERVED_BLOCKS_BASE + 3)       /* ... wake up the GC thread */
+#define JFFS2_RESERVED_BLOCKS_GCBAD (JFFS2_RESERVED_BLOCKS_BASE + 1)           /* ... pick a block from the bad_list to GC */
+#define JFFS2_RESERVED_BLOCKS_GCMERGE (JFFS2_RESERVED_BLOCKS_BASE)             /* ... merge pages when garbage collecting */
+
+
+#define PAD(x) (((x)+3)&~3)
+
+/* These probably ought to be somewhere central. */
+//#define min(x,y) ({ typeof((x)) _x = (x); typeof((y)) _y = (y); (_x>_y)?_y:_x; })
+//#define max(x,y) ({ typeof((x)) _x = (x); typeof((y)) _y = (y); (_x>_y)?_x:_y; })
+
+static inline int jffs2_raw_ref_to_inum(struct jffs2_raw_node_ref *raw)
+{
+       while(raw->next_in_ino) {
+               raw = raw->next_in_ino;
+       }
+
+       return ((struct jffs2_inode_cache *)raw)->ino;
+}
+
+/* nodelist.c */
+D1(void jffs2_print_frag_list(struct jffs2_inode_info *f));
+void jffs2_add_fd_to_list(struct jffs2_sb_info *c, struct jffs2_full_dirent *new, struct jffs2_full_dirent **list);
+void jffs2_add_tn_to_list(struct jffs2_tmp_dnode_info *tn, struct jffs2_tmp_dnode_info **list);
+int jffs2_get_inode_nodes(struct jffs2_sb_info *c, ino_t ino, struct jffs2_inode_info *f,
+                         struct jffs2_tmp_dnode_info **tnp, struct jffs2_full_dirent **fdp,
+                         __u32 *highest_version);
+struct jffs2_inode_cache *jffs2_get_ino_cache(struct jffs2_sb_info *c, int ino);
+void jffs2_add_ino_cache (struct jffs2_sb_info *c, struct jffs2_inode_cache *new);
+void jffs2_del_ino_cache(struct jffs2_sb_info *c, struct jffs2_inode_cache *old);
+void jffs2_free_ino_caches(struct jffs2_sb_info *c);
+void jffs2_free_raw_node_refs(struct jffs2_sb_info *c);
+
+/* nodemgmt.c */
+int jffs2_reserve_space(struct jffs2_sb_info *c, __u32 minsize, __u32 *ofs, __u32 *len, int prio);
+int jffs2_reserve_space_gc(struct jffs2_sb_info *c, __u32 minsize, __u32 *ofs, __u32 *len);
+int jffs2_add_physical_node_ref(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *new, __u32 len, int dirty);
+void jffs2_complete_reservation(struct jffs2_sb_info *c);
+void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *raw);
+
+/* write.c */
+struct inode *jffs2_new_inode (struct inode *dir_i, int mode, struct jffs2_raw_inode *ri);
+struct jffs2_full_dnode *jffs2_write_dnode(struct inode *inode, struct jffs2_raw_inode *ri, const unsigned char *data, __u32 datalen, __u32 flash_ofs,  __u32 *writelen);
+struct jffs2_full_dirent *jffs2_write_dirent(struct inode *inode, struct jffs2_raw_dirent *rd, const unsigned char *name, __u32 namelen, __u32 flash_ofs,  __u32 *writelen);
+
+/* readinode.c */
+void jffs2_truncate_fraglist (struct jffs2_sb_info *c, struct jffs2_node_frag **list, __u32 size);
+int jffs2_add_full_dnode_to_fraglist(struct jffs2_sb_info *c, struct jffs2_node_frag **list, struct jffs2_full_dnode *fn);
+int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn);
+void jffs2_read_inode (struct inode *);
+void jffs2_clear_inode (struct inode *);
+
+/* malloc.c */
+void jffs2_free_tmp_dnode_info_list(struct jffs2_tmp_dnode_info *tn);
+void jffs2_free_full_dirent_list(struct jffs2_full_dirent *fd);
+
+int jffs2_create_slab_caches(void);
+void jffs2_destroy_slab_caches(void);
+
+struct jffs2_full_dirent *jffs2_alloc_full_dirent(int namesize);
+void jffs2_free_full_dirent(struct jffs2_full_dirent *);
+struct jffs2_full_dnode *jffs2_alloc_full_dnode(void);
+void jffs2_free_full_dnode(struct jffs2_full_dnode *);
+struct jffs2_raw_dirent *jffs2_alloc_raw_dirent(void);
+void jffs2_free_raw_dirent(struct jffs2_raw_dirent *);
+struct jffs2_raw_inode *jffs2_alloc_raw_inode(void);
+void jffs2_free_raw_inode(struct jffs2_raw_inode *);
+struct jffs2_tmp_dnode_info *jffs2_alloc_tmp_dnode_info(void);
+void jffs2_free_tmp_dnode_info(struct jffs2_tmp_dnode_info *);
+struct jffs2_raw_node_ref *jffs2_alloc_raw_node_ref(void);
+void jffs2_free_raw_node_ref(struct jffs2_raw_node_ref *);
+struct jffs2_node_frag *jffs2_alloc_node_frag(void);
+void jffs2_free_node_frag(struct jffs2_node_frag *);
+struct jffs2_inode_cache *jffs2_alloc_inode_cache(void);
+void jffs2_free_inode_cache(struct jffs2_inode_cache *);
+
+/* gc.c */
+int jffs2_garbage_collect_pass(struct jffs2_sb_info *c);
+
+/* background.c */
+int jffs2_start_garbage_collect_thread(struct jffs2_sb_info *c);
+void jffs2_stop_garbage_collect_thread(struct jffs2_sb_info *c);
+void jffs2_garbage_collect_trigger(struct jffs2_sb_info *c);
+
+/* dir.c */
+extern struct file_operations jffs2_dir_operations;
+extern struct inode_operations jffs2_dir_inode_operations;
+
+/* file.c */
+extern struct file_operations jffs2_file_operations;
+extern struct inode_operations jffs2_file_inode_operations;
+extern struct address_space_operations jffs2_file_address_operations;
+int jffs2_null_fsync(struct file *, struct dentry *, int);
+int jffs2_setattr (struct dentry *dentry, struct iattr *iattr);
+int jffs2_do_readpage_nolock (struct inode *inode, struct page *pg);
+int jffs2_do_readpage_unlock (struct inode *inode, struct page *pg);
+int jffs2_readpage (struct file *, struct page *);
+int jffs2_prepare_write (struct file *, struct page *, unsigned, unsigned);
+int jffs2_commit_write (struct file *, struct page *, unsigned, unsigned);
+
+/* ioctl.c */
+int jffs2_ioctl(struct inode *, struct file *, unsigned int, unsigned long);
+
+/* read.c */
+int jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_full_dnode *fd, unsigned char *buf, int ofs, int len);
+
+/* compr.c */
+unsigned char jffs2_compress(unsigned char *data_in, unsigned char *cpage_out, 
+                            __u32 *datalen, __u32 *cdatalen);
+int jffs2_decompress(unsigned char comprtype, unsigned char *cdata_in, 
+                    unsigned char *data_out, __u32 cdatalen, __u32 datalen);
+
+/* scan.c */
+int jffs2_scan_medium(struct jffs2_sb_info *c);
+
+/* build.c */
+int jffs2_build_filesystem(struct jffs2_sb_info *c);
+
+/* symlink.c */
+extern struct inode_operations jffs2_symlink_inode_operations;
+
+/* erase.c */
+void jffs2_erase_block(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
+void jffs2_erase_pending_blocks(struct jffs2_sb_info *c);
+void jffs2_mark_erased_blocks(struct jffs2_sb_info *c);
+void jffs2_erase_pending_trigger(struct jffs2_sb_info *c);
diff --git a/fs/jffs2/nodemgmt.c b/fs/jffs2/nodemgmt.c
new file mode 100644 (file)
index 0000000..cc0babe
--- /dev/null
@@ -0,0 +1,380 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: nodemgmt.c,v 1.39 2001/04/11 22:20:26 dwmw2 Exp $
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/jffs2.h>
+#include <linux/mtd/mtd.h>
+#include <linux/interrupt.h>
+#include "nodelist.h"
+
+/**
+ *     jffs2_reserve_space - request physical space to write nodes to flash
+ *     @c: superblock info
+ *     @minsize: Minimum acceptable size of allocation
+ *     @ofs: Returned value of node offset
+ *     @len: Returned value of allocation length
+ *     @prio: Allocation type - ALLOC_{NORMAL,DELETION}
+ *
+ *     Requests a block of physical space on the flash. Returns zero for success
+ *     and puts 'ofs' and 'len' into the appriopriate place, or returns -ENOSPC
+ *     or other error if appropriate.
+ *
+ *     If it returns zero, jffs2_reserve_space() also downs the per-filesystem
+ *     allocation semaphore, to prevent more than one allocation from being
+ *     active at any time. The semaphore is later released by jffs2_commit_allocation()
+ *
+ *     jffs2_reserve_space() may trigger garbage collection in order to make room
+ *     for the requested allocation.
+ */
+
+static int jffs2_do_reserve_space(struct jffs2_sb_info *c,  __u32 minsize, __u32 *ofs, __u32 *len);
+
+int jffs2_reserve_space(struct jffs2_sb_info *c, __u32 minsize, __u32 *ofs, __u32 *len, int prio)
+{
+       int ret = -EAGAIN;
+       int blocksneeded = JFFS2_RESERVED_BLOCKS_WRITE;
+       /* align it */
+       minsize = PAD(minsize);
+
+       if (prio == ALLOC_DELETION)
+               blocksneeded = JFFS2_RESERVED_BLOCKS_DELETION;
+
+       D1(printk(KERN_DEBUG "jffs2_reserve_space(): Requested 0x%x bytes\n", minsize));
+       down(&c->alloc_sem);
+
+       D1(printk(KERN_DEBUG "jffs2_reserve_space(): alloc sem got\n"));
+
+       spin_lock_bh(&c->erase_completion_lock);
+
+       /* this needs a little more thought */
+       while(ret == -EAGAIN) {
+               while(c->nr_free_blocks + c->nr_erasing_blocks < blocksneeded) {
+                       int ret;
+
+                       up(&c->alloc_sem);
+                       if (c->dirty_size < c->sector_size) {
+                               D1(printk(KERN_DEBUG "Short on space, but total dirty size 0x%08x < sector size 0x%08x, so -ENOSPC\n", c->dirty_size, c->sector_size));
+                               spin_unlock_bh(&c->erase_completion_lock);
+                               return -ENOSPC;
+                       }
+                       D1(printk(KERN_DEBUG "Triggering GC pass. nr_free_blocks %d, nr_erasing_blocks %d, free_size 0x%08x, dirty_size 0x%08x, used_size 0x%08x, erasing_size 0x%08x, bad_size 0x%08x (total 0x%08x of 0x%08x)\n",
+                                 c->nr_free_blocks, c->nr_erasing_blocks, c->free_size, c->dirty_size, c->used_size, c->erasing_size, c->bad_size,
+                                 c->free_size + c->dirty_size + c->used_size + c->erasing_size + c->bad_size, c->flash_size));
+                       spin_unlock_bh(&c->erase_completion_lock);
+                       
+                       ret = jffs2_garbage_collect_pass(c);
+                       if (ret)
+                               return ret;
+                       if (signal_pending(current)) {
+                               return -EINTR;
+                       }
+                       if(current->need_resched) {
+                               schedule();
+                       }
+                       down(&c->alloc_sem);
+                       spin_lock_bh(&c->erase_completion_lock);
+               }
+
+               ret = jffs2_do_reserve_space(c, minsize, ofs, len);
+               if (ret) {
+                       D1(printk(KERN_DEBUG "jffs2_reserve_space: ret is %d\n", ret));
+               }
+       }
+       spin_unlock_bh(&c->erase_completion_lock);
+       if (ret)
+               up(&c->alloc_sem);
+       return ret;
+}
+
+int jffs2_reserve_space_gc(struct jffs2_sb_info *c, __u32 minsize, __u32 *ofs, __u32 *len)
+{
+       int ret = -EAGAIN;
+       minsize = PAD(minsize);
+
+       D1(printk(KERN_DEBUG "jffs2_reserve_space_gc(): Requested 0x%x bytes\n", minsize));
+
+       spin_lock_bh(&c->erase_completion_lock);
+       while(ret == -EAGAIN) {
+               ret = jffs2_do_reserve_space(c, minsize, ofs, len);
+               if (ret) {
+                       D1(printk(KERN_DEBUG "jffs2_reserve_space_gc: looping, ret is %d\n", ret));
+               }
+       }
+       spin_unlock_bh(&c->erase_completion_lock);
+       return ret;
+}
+
+/* Called with alloc sem _and_ erase_completion_lock */
+static int jffs2_do_reserve_space(struct jffs2_sb_info *c,  __u32 minsize, __u32 *ofs, __u32 *len)
+{
+       struct jffs2_eraseblock *jeb = c->nextblock;
+       
+ restart:
+       if (jeb && minsize > jeb->free_size) {
+               /* Skip the end of this block and file it as having some dirty space */
+               c->dirty_size += jeb->free_size;
+               c->free_size -= jeb->free_size;
+               jeb->dirty_size += jeb->free_size;
+               jeb->free_size = 0;
+               D1(printk(KERN_DEBUG "Adding full erase block at 0x%08x to dirty_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
+                         jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
+               list_add_tail(&jeb->list, &c->dirty_list);
+               c->nextblock = jeb = NULL;
+       }
+       
+       if (!jeb) {
+               struct list_head *next;
+               /* Take the next block off the 'free' list */
+
+               if (list_empty(&c->free_list)) {
+
+                       DECLARE_WAITQUEUE(wait, current);
+                       
+                       if (!c->nr_erasing_blocks) {
+//                     if (list_empty(&c->erasing_list) && list_empty(&c->erase_pending_list) && list_empty(c->erase_complete_list)) {
+                               /* Ouch. We're in GC, or we wouldn't have got here.
+                                  And there's no space left. At all. */
+                               printk(KERN_CRIT "Argh. No free space left for GC. nr_erasing_blocks is %d. nr_free_blocks is %d. (erasingempty: %s, erasependingempty: %s)\n", 
+                                      c->nr_erasing_blocks, c->nr_free_blocks, list_empty(&c->erasing_list)?"yes":"no", list_empty(&c->erase_pending_list)?"yes":"no");
+                               return -ENOSPC;
+                       }
+                       /* Make sure this can't deadlock. Someone has to start the erases
+                          of erase_pending blocks */
+                       set_current_state(TASK_INTERRUPTIBLE);
+                       add_wait_queue(&c->erase_wait, &wait);
+                       D1(printk(KERN_DEBUG "Waiting for erases to complete. erasing_blocks is %d. (erasingempty: %s, erasependingempty: %s)\n", 
+                                 c->nr_erasing_blocks, list_empty(&c->erasing_list)?"yes":"no", list_empty(&c->erase_pending_list)?"yes":"no"));
+                       if (!list_empty(&c->erase_pending_list)) {
+                               D1(printk(KERN_DEBUG "Triggering pending erases\n"));
+                               jffs2_erase_pending_trigger(c);
+                       }
+                       spin_unlock_bh(&c->erase_completion_lock);
+                       schedule();
+                       remove_wait_queue(&c->erase_wait, &wait);
+                       spin_lock_bh(&c->erase_completion_lock);
+                       if (signal_pending(current)) {
+                               return -EINTR;
+                       }
+                       /* An erase may have failed, decreasing the
+                          amount of free space available. So we must
+                          restart from the beginning */
+                       return -EAGAIN;
+               }
+
+               next = c->free_list.next;
+               list_del(next);
+               c->nextblock = jeb = list_entry(next, struct jffs2_eraseblock, list);
+               c->nr_free_blocks--;
+               if (jeb->free_size != c->sector_size - sizeof(struct jffs2_unknown_node)) {
+                       printk(KERN_WARNING "Eep. Block 0x%08x taken from free_list had free_size of 0x%08x!!\n", jeb->offset, jeb->free_size);
+                       goto restart;
+               }
+       }
+       /* OK, jeb (==c->nextblock) is now pointing at a block which definitely has
+          enough space */
+       *ofs = jeb->offset + (c->sector_size - jeb->free_size);
+       *len = jeb->free_size;
+       D1(printk(KERN_DEBUG "jffs2_do_reserve_space(): Giving 0x%x bytes at 0x%x\n", *len, *ofs));
+       return 0;
+}
+
+/**
+ *     jffs2_add_physical_node_ref - add a physical node reference to the list
+ *     @c: superblock info
+ *     @ofs: physical location of this physical node
+ *     @len: length of this physical node
+ *     @ino: inode number with which this physical node is associated
+ *
+ *     Should only be used to report nodes for which space has been allocated 
+ *     by jffs2_reserve_space.
+ *
+ *     Must be called with the alloc_sem held.
+ */
+int jffs2_add_physical_node_ref(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *new, __u32 len, int dirty)
+{
+       struct jffs2_eraseblock *jeb;
+
+       len = PAD(len);
+       jeb = &c->blocks[(new->flash_offset & ~3) / c->sector_size];
+       D1(printk(KERN_DEBUG "jffs2_add_physical_node_ref(): Node at 0x%x, size 0x%x\n", new->flash_offset & ~3, len));
+#if 1
+       if (jeb != c->nextblock || (new->flash_offset & ~3) != jeb->offset + (c->sector_size - jeb->free_size)) {
+               printk(KERN_WARNING "argh. node added in wrong place\n");
+               jffs2_free_raw_node_ref(new);
+               return -EINVAL;
+       }
+#endif
+       if (!jeb->first_node)
+               jeb->first_node = new;
+       if (jeb->last_node)
+               jeb->last_node->next_phys = new;
+       jeb->last_node = new;
+
+       spin_lock_bh(&c->erase_completion_lock);
+       jeb->free_size -= len;
+       c->free_size -= len;
+       if (dirty) {
+               new->flash_offset |= 1;
+               jeb->dirty_size += len;
+               c->dirty_size += len;
+       } else {
+               jeb->used_size += len;
+               c->used_size += len;
+       }
+       spin_unlock_bh(&c->erase_completion_lock);
+       if (!jeb->free_size && !jeb->dirty_size) {
+               /* If it lives on the dirty_list, jffs2_reserve_space will put it there */
+               D1(printk(KERN_DEBUG "Adding full erase block at 0x%08x to clean_list (free 0x%08x, dirty 0x%08x, used 0x%08x\n",
+                         jeb->offset, jeb->free_size, jeb->dirty_size, jeb->used_size));
+               list_add_tail(&jeb->list, &c->clean_list);
+               c->nextblock = NULL;
+       }
+       ACCT_SANITY_CHECK(c,jeb);
+       ACCT_PARANOIA_CHECK(jeb);
+
+       return 0;
+}
+
+
+void jffs2_complete_reservation(struct jffs2_sb_info *c)
+{
+       D1(printk(KERN_DEBUG "jffs2_complete_reservation()\n"));
+       jffs2_garbage_collect_trigger(c);
+       up(&c->alloc_sem);
+}
+
+void jffs2_mark_node_obsolete(struct jffs2_sb_info *c, struct jffs2_raw_node_ref *ref)
+{
+       struct jffs2_eraseblock *jeb;
+       int blocknr;
+       struct jffs2_unknown_node n;
+       int ret;
+       ssize_t retlen;
+
+       if(!ref) {
+               printk(KERN_NOTICE "EEEEEK. jffs2_mark_node_obsolete called with NULL node\n");
+               return;
+       }
+       if (ref->flash_offset & 1) {
+               D1(printk(KERN_DEBUG "jffs2_mark_node_obsolete called with already obsolete node at 0x%08x\n", ref->flash_offset &~3));
+               return;
+       }
+       blocknr = ref->flash_offset / c->sector_size;
+       if (blocknr >= c->nr_blocks) {
+               printk(KERN_NOTICE "raw node at 0x%08x is off the end of device!\n", ref->flash_offset);
+               BUG();
+       }
+       jeb = &c->blocks[blocknr];
+       if (jeb->used_size < ref->totlen) {
+               printk(KERN_NOTICE "raw node of size 0x%08x freed from erase block %d at 0x%08x, but used_size was already 0x%08x\n",
+                      ref->totlen, blocknr, ref->flash_offset, jeb->used_size);
+               BUG();
+       }
+
+       spin_lock_bh(&c->erase_completion_lock);
+       jeb->used_size -= ref->totlen;
+       jeb->dirty_size += ref->totlen;
+       c->used_size -= ref->totlen;
+       c->dirty_size += ref->totlen;
+       ref->flash_offset |= 1;
+       
+       ACCT_SANITY_CHECK(c, jeb);
+
+       ACCT_PARANOIA_CHECK(jeb);
+
+       if (jeb == c->nextblock) {
+               D2(printk(KERN_DEBUG "Not moving nextblock 0x%08x to dirty/erase_pending list\n", jeb->offset));
+       } else if (jeb == c->gcblock) {
+               D2(printk(KERN_DEBUG "Not moving gcblock 0x%08x to dirty/erase_pending list\n", jeb->offset));
+#if 0 /* We no longer do this here. It can screw the wear levelling. If you have a lot of static
+        data and a few blocks free, and you just create new files and keep deleting/overwriting
+        them, then you'd keep erasing and reusing those blocks without ever moving stuff around.
+        So we leave completely obsoleted blocks on the dirty_list and let the GC delete them 
+        when it finds them there. That way, we still get the 'once in a while, take a clean block'
+        to spread out the flash usage */
+       } else if (!jeb->used_size) {
+               D1(printk(KERN_DEBUG "Eraseblock at 0x%08x completely dirtied. Removing from (dirty?) list...\n", jeb->offset));
+               list_del(&jeb->list);
+               D1(printk(KERN_DEBUG "...and adding to erase_pending_list\n"));
+               list_add_tail(&jeb->list, &c->erase_pending_list);
+               c->nr_erasing_blocks++;
+               jffs2_erase_pending_trigger(c);
+               //              OFNI_BS_2SFFJ(c)->s_dirt = 1;
+               D1(printk(KERN_DEBUG "Done OK\n"));
+#endif
+       } else if (jeb->dirty_size == ref->totlen) {
+               D1(printk(KERN_DEBUG "Eraseblock at 0x%08x is freshly dirtied. Removing from clean list...\n", jeb->offset));
+               list_del(&jeb->list);
+               D1(printk(KERN_DEBUG "...and adding to dirty_list\n"));
+               list_add_tail(&jeb->list, &c->dirty_list);
+       }
+       spin_unlock_bh(&c->erase_completion_lock);
+
+       if (c->mtd->type != MTD_NORFLASH && c->mtd->type != MTD_RAM)
+               return;
+
+       D1(printk(KERN_DEBUG "obliterating obsoleted node at 0x%08x\n", ref->flash_offset &~3));
+       ret = c->mtd->read(c->mtd, ref->flash_offset &~3, sizeof(n), &retlen, (char *)&n);
+       if (ret) {
+               printk(KERN_WARNING "Read error reading from obsoleted node at 0x%08x: %d\n", ref->flash_offset &~3, ret);
+               return;
+       }
+       if (retlen != sizeof(n)) {
+               printk(KERN_WARNING "Short read from obsoleted node at 0x%08x: %d\n", ref->flash_offset &~3, retlen);
+               return;
+       }
+       if (PAD(n.totlen) != PAD(ref->totlen)) {
+               printk(KERN_WARNING "Node totlen on flash (0x%08x) != totlen in node ref (0x%08x)\n", n.totlen, ref->totlen);
+               return;
+       }
+       if (!(n.nodetype & JFFS2_NODE_ACCURATE)) {
+               D1(printk(KERN_DEBUG "Node at 0x%08x was already marked obsolete (nodetype 0x%04x\n", ref->flash_offset &~3, n.nodetype));
+               return;
+       }
+       n.nodetype &= ~JFFS2_NODE_ACCURATE;
+       ret = c->mtd->write(c->mtd, ref->flash_offset&~3, sizeof(n), &retlen, (char *)&n);
+       if (ret) {
+               printk(KERN_WARNING "Write error in obliterating obsoleted node at 0x%08x: %d\n", ref->flash_offset &~3, ret);
+               return;
+       }
+       if (retlen != sizeof(n)) {
+               printk(KERN_WARNING "Short write in obliterating obsoleted node at 0x%08x: %d\n", ref->flash_offset &~3, retlen);
+               return;
+       }
+}
diff --git a/fs/jffs2/pushpull.c b/fs/jffs2/pushpull.c
new file mode 100644 (file)
index 0000000..fed9a02
--- /dev/null
@@ -0,0 +1,88 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: pushpull.c,v 1.6 2001/03/15 15:38:24 dwmw2 Exp $
+ *
+ */
+
+#include <linux/string.h>
+#include "pushpull.h"
+#include <linux/errno.h>
+
+void init_pushpull(struct pushpull *pp, char *buf, unsigned buflen, unsigned ofs, unsigned reserve)
+{
+       pp->buf = buf;
+       pp->buflen = buflen;
+       pp->ofs = ofs;
+       pp->reserve = reserve;
+}
+     
+
+int pushbit(struct pushpull *pp, int bit, int use_reserved)
+{
+       if (pp->ofs >= pp->buflen - (use_reserved?0:pp->reserve)) {
+               return -ENOSPC;
+       }
+
+       if (bit) {
+               pp->buf[pp->ofs >> 3] |= (1<<(7-(pp->ofs &7)));
+       }
+       else {
+               pp->buf[pp->ofs >> 3] &= ~(1<<(7-(pp->ofs &7)));
+       }
+       pp->ofs++;
+
+       return 0;
+}
+
+int pushedbits(struct pushpull *pp)
+{
+       return pp->ofs;
+}
+       
+
+int pullbit(struct pushpull *pp)
+{
+       int bit;
+
+       bit = (pp->buf[pp->ofs >> 3] >> (7-(pp->ofs & 7))) & 1;
+
+       pp->ofs++;
+       return bit;
+}
+
+int pulledbits (struct pushpull *pp)
+{
+       return pp->ofs;
+}
+     
diff --git a/fs/jffs2/pushpull.h b/fs/jffs2/pushpull.h
new file mode 100644 (file)
index 0000000..f5be9ec
--- /dev/null
@@ -0,0 +1,53 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: pushpull.h,v 1.4 2001/03/15 15:38:24 dwmw2 Exp $
+ *
+ */
+
+#ifndef __PUSHPULL_H__
+#define __PUSHPULL_H__
+struct pushpull {
+       unsigned char *buf;
+       unsigned int buflen;
+       unsigned int ofs;
+       unsigned int reserve;
+};
+
+void init_pushpull(struct pushpull *, char *, unsigned, unsigned, unsigned);
+int pushbit(struct pushpull *pp, int bit, int use_reserved);
+int pushedbits(struct pushpull *pp);
+int pullbit(struct pushpull *pp);
+int pulledbits(struct pushpull *);
+
+#endif /* __PUSHPULL_H__ */
diff --git a/fs/jffs2/read.c b/fs/jffs2/read.c
new file mode 100644 (file)
index 0000000..cce4256
--- /dev/null
@@ -0,0 +1,157 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: read.c,v 1.13 2001/05/01 16:24:44 dwmw2 Exp $
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/jffs2.h>
+#include <linux/mtd/mtd.h>
+#include "nodelist.h"
+#include "crc32.h"
+
+int jffs2_read_dnode(struct jffs2_sb_info *c, struct jffs2_full_dnode *fd, unsigned char *buf, int ofs, int len)
+{
+       struct jffs2_raw_inode *ri;
+       size_t readlen;
+       __u32 crc;
+       unsigned char *decomprbuf = NULL;
+       unsigned char *readbuf = NULL;
+       int ret = 0;
+
+       ri = jffs2_alloc_raw_inode();
+       if (!ri)
+               return -ENOMEM;
+
+       ret = c->mtd->read(c->mtd, fd->raw->flash_offset & ~3, sizeof(*ri), &readlen, (char *)ri);
+       if (ret) {
+               jffs2_free_raw_inode(ri);
+               printk(KERN_WARNING "Error reading node from 0x%08x: %d\n", fd->raw->flash_offset & ~3, ret);
+               return ret;
+       }
+       if (readlen != sizeof(*ri)) {
+               jffs2_free_raw_inode(ri);
+               printk(KERN_WARNING "Short read from 0x%08x: wanted 0x%x bytes, got 0x%x\n", 
+                      fd->raw->flash_offset & ~3, sizeof(*ri), readlen);
+               return -EIO;
+       }
+       crc = crc32(0, ri, sizeof(*ri)-8);
+
+       D1(printk(KERN_DEBUG "Node read from %08x: node_crc %08x, calculated CRC %08x. dsize %x, csize %x, offset %x, buf %p\n", fd->raw->flash_offset & ~3, ri->node_crc, crc, ri->dsize, ri->csize, ri->offset, buf));
+       if (crc != ri->node_crc) {
+               printk(KERN_WARNING "Node CRC %08x != calculated CRC %08x for node at %08x\n", ri->node_crc, crc, fd->raw->flash_offset & ~3);
+               ret = -EIO;
+               goto out_ri;
+       }
+
+       D1(if(ofs + len > ri->dsize) {
+               printk(KERN_WARNING "jffs2_read_dnode() asked for %d bytes at %d from %d-byte node\n", len, ofs, ri->dsize);
+               ret = -EINVAL;
+               goto out_ri;
+       });
+
+       
+       if (ri->compr == JFFS2_COMPR_ZERO) {
+               memset(buf, 0, len);
+               goto out_ri;
+       }
+
+       /* Cases:
+          Reading whole node and it's uncompressed - read directly to buffer provided, check CRC.
+          Reading whole node and it's compressed - read into comprbuf, check CRC and decompress to buffer provided 
+          Reading partial node and it's uncompressed - read into readbuf, check CRC, and copy 
+          Reading partial node and it's compressed - read into readbuf, check checksum, decompress to decomprbuf and copy
+       */
+       if (ri->compr == JFFS2_COMPR_NONE && len == ri->dsize) {
+               readbuf = buf;
+       } else {
+               readbuf = kmalloc(ri->csize, GFP_KERNEL);
+               if (!readbuf) {
+                       ret = -ENOMEM;
+                       goto out_ri;
+               }
+       }
+       if (ri->compr != JFFS2_COMPR_NONE) {
+               if (len < ri->dsize) {
+                       decomprbuf = kmalloc(ri->dsize, GFP_KERNEL);
+                       if (!decomprbuf) {
+                               ret = -ENOMEM;
+                               goto out_readbuf;
+                       }
+               } else {
+                       decomprbuf = buf;
+               }
+       } else {
+               decomprbuf = readbuf;
+       }
+
+       D2(printk(KERN_DEBUG "Read %d bytes to %p\n", ri->csize, readbuf));
+       ret = c->mtd->read(c->mtd, (fd->raw->flash_offset &~3) + sizeof(*ri), ri->csize, &readlen, readbuf);
+
+       if (!ret && readlen != ri->csize)
+               ret = -EIO;
+       if (ret)
+               goto out_decomprbuf;
+
+       crc = crc32(0, readbuf, ri->csize);
+       if (crc != ri->data_crc) {
+               printk(KERN_WARNING "Data CRC %08x != calculated CRC %08x for node at %08x\n", ri->data_crc, crc, fd->raw->flash_offset & ~3);
+               ret = -EIO;
+               goto out_decomprbuf;
+       }
+       D2(printk(KERN_DEBUG "Data CRC matches calculated CRC %08x\n", crc));
+       if (ri->compr != JFFS2_COMPR_NONE) {
+               D2(printk(KERN_DEBUG "Decompress %d bytes from %p to %d bytes at %p\n", ri->csize, readbuf, ri->dsize, decomprbuf)); 
+               ret = jffs2_decompress(ri->compr, readbuf, decomprbuf, ri->csize, ri->dsize);
+               if (ret) {
+                       printk(KERN_WARNING "Error: jffs2_decompress returned %d\n", ret);
+                       goto out_decomprbuf;
+               }
+       }
+
+       if (len < ri->dsize) {
+               memcpy(buf, decomprbuf+ofs, len);
+       }
+ out_decomprbuf:
+       if(decomprbuf != buf && decomprbuf != readbuf)
+               kfree(decomprbuf);
+ out_readbuf:
+       if(readbuf != buf)
+               kfree(readbuf);
+ out_ri:
+       jffs2_free_raw_inode(ri);
+
+       return ret;
+}
diff --git a/fs/jffs2/readinode.c b/fs/jffs2/readinode.c
new file mode 100644 (file)
index 0000000..2e1d93d
--- /dev/null
@@ -0,0 +1,494 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: readinode.c,v 1.56 2001/07/26 20:32:39 dwmw2 Exp $
+ *
+ */
+
+/* Given an inode, probably with existing list of fragments, add the new node
+ * to the fragment list.
+ */
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/fs.h>
+#include <linux/mtd/mtd.h>
+#include <linux/jffs2.h>
+#include "nodelist.h"
+#include "crc32.h"
+
+
+D1(void jffs2_print_frag_list(struct jffs2_inode_info *f)
+{
+       struct jffs2_node_frag *this = f->fraglist;
+
+       while(this) {
+               if (this->node)
+                       printk(KERN_DEBUG "frag %04x-%04x: 0x%08x on flash (*%p->%p)\n", this->ofs, this->ofs+this->size, this->node->raw->flash_offset &~3, this, this->next);
+               else 
+                       printk(KERN_DEBUG "frag %04x-%04x: hole (*%p->%p)\n", this->ofs, this->ofs+this->size, this, this->next);
+               this = this->next;
+       }
+       if (f->metadata) {
+               printk(KERN_DEBUG "metadata at 0x%08x\n", f->metadata->raw->flash_offset &~3);
+       }
+})
+
+
+int jffs2_add_full_dnode_to_inode(struct jffs2_sb_info *c, struct jffs2_inode_info *f, struct jffs2_full_dnode *fn)
+{
+       int ret;
+       D1(printk(KERN_DEBUG "jffs2_add_full_dnode_to_inode(ino #%u, f %p, fn %p)\n", f->inocache->ino, f, fn));
+
+       ret = jffs2_add_full_dnode_to_fraglist(c, &f->fraglist, fn);
+
+       D2(jffs2_print_frag_list(f));
+       return ret;
+}
+
+static void jffs2_obsolete_node_frag(struct jffs2_sb_info *c, struct jffs2_node_frag *this)
+{
+       if (this->node) {
+               this->node->frags--;
+               if (!this->node->frags) {
+                       /* The node has no valid frags left. It's totally obsoleted */
+                       D2(printk(KERN_DEBUG "Marking old node @0x%08x (0x%04x-0x%04x) obsolete\n",
+                                 this->node->raw->flash_offset &~3, this->node->ofs, this->node->ofs+this->node->size));
+                       jffs2_mark_node_obsolete(c, this->node->raw);
+                       jffs2_free_full_dnode(this->node);
+               } else {
+                       D2(printk(KERN_DEBUG "Not marking old node @0x%08x (0x%04x-0x%04x) obsolete. frags is %d\n",
+                                 this->node->raw->flash_offset &~3, this->node->ofs, this->node->ofs+this->node->size,
+                                 this->node->frags));
+               }
+               
+       }
+       jffs2_free_node_frag(this);
+}
+
+/* Doesn't set inode->i_size */
+int jffs2_add_full_dnode_to_fraglist(struct jffs2_sb_info *c, struct jffs2_node_frag **list, struct jffs2_full_dnode *fn)
+{
+       
+       struct jffs2_node_frag *this, **prev, *old;
+       struct jffs2_node_frag *newfrag, *newfrag2;
+       __u32 lastend = 0;
+
+
+       newfrag = jffs2_alloc_node_frag();
+       if (!newfrag) {
+               return -ENOMEM;
+       }
+
+       D2(if (fn->raw)
+               printk(KERN_DEBUG "adding node %04x-%04x @0x%08x on flash, newfrag *%p\n", fn->ofs, fn->ofs+fn->size, fn->raw->flash_offset &~3, newfrag);
+       else
+               printk(KERN_DEBUG "adding hole node %04x-%04x on flash, newfrag *%p\n", fn->ofs, fn->ofs+fn->size, newfrag));
+       
+       prev = list;
+       this = *list;
+
+       if (!fn->size) {
+               jffs2_free_node_frag(newfrag);
+               return 0;
+       }
+
+       newfrag->ofs = fn->ofs;
+       newfrag->size = fn->size;
+       newfrag->node = fn;
+       newfrag->node->frags = 1;
+       newfrag->next = (void *)0xdeadbeef;
+
+       /* Skip all the nodes which are completed before this one starts */
+       while(this && fn->ofs >= this->ofs+this->size) {
+               lastend = this->ofs + this->size;
+
+               D2(printk(KERN_DEBUG "j_a_f_d_t_f: skipping frag 0x%04x-0x%04x; phys 0x%08x (*%p->%p)\n", 
+                         this->ofs, this->ofs+this->size, this->node?(this->node->raw->flash_offset &~3):0xffffffff, this, this->next));
+               prev = &this->next;
+               this = this->next;
+       }
+
+       /* See if we ran off the end of the list */
+       if (!this) {
+               /* We did */
+               if (lastend < fn->ofs) {
+                       /* ... and we need to put a hole in before the new node */
+                       struct jffs2_node_frag *holefrag = jffs2_alloc_node_frag();
+                       if (!holefrag)
+                               return -ENOMEM;
+                       holefrag->ofs = lastend;
+                       holefrag->size = fn->ofs - lastend;
+                       holefrag->next = NULL;
+                       holefrag->node = NULL;
+                       *prev = holefrag;
+                       prev = &holefrag->next;
+               }
+               newfrag->next = NULL;
+               *prev = newfrag;
+               return 0;
+       }
+
+       D2(printk(KERN_DEBUG "j_a_f_d_t_f: dealing with frag 0x%04x-0x%04x; phys 0x%08x (*%p->%p)\n", 
+                 this->ofs, this->ofs+this->size, this->node?(this->node->raw->flash_offset &~3):0xffffffff, this, this->next));
+
+       /* OK. 'this' is pointing at the first frag that fn->ofs at least partially obsoletes,
+        * - i.e. fn->ofs < this->ofs+this->size && fn->ofs >= this->ofs  
+        */
+       if (fn->ofs > this->ofs) {
+               /* This node isn't completely obsoleted. The start of it remains valid */
+               if (this->ofs + this->size > fn->ofs + fn->size) {
+                       /* The new node splits 'this' frag into two */
+                       newfrag2 = jffs2_alloc_node_frag();
+                       if (!newfrag2) {
+                               jffs2_free_node_frag(newfrag);
+                               return -ENOMEM;
+                       }
+                       printk(KERN_DEBUG "split old frag 0x%04x-0x%04x -->", this->ofs, this->ofs+this->size);
+                       if (this->node)
+                               printk("phys 0x%08x\n", this->node->raw->flash_offset &~3);
+                       else 
+                               printk("hole\n");
+
+                       newfrag2->ofs = fn->ofs + fn->size;
+                       newfrag2->size = (this->ofs+this->size) - newfrag2->ofs;
+                       newfrag2->next = this->next;
+                       newfrag2->node = this->node;
+                       if (this->node)
+                               this->node->frags++;
+                       newfrag->next = newfrag2;
+                       this->next = newfrag;
+                       this->size = newfrag->ofs - this->ofs;
+                       return 0;
+               }
+               /* New node just reduces 'this' frag in size, doesn't split it */
+               this->size = fn->ofs - this->ofs;
+               newfrag->next = this->next;
+               this->next = newfrag;
+               this = newfrag->next;
+       } else {
+               D2(printk(KERN_DEBUG "Inserting newfrag (*%p) in before 'this' (*%p)\n", newfrag, this));
+               *prev = newfrag;
+               newfrag->next = this;
+       }
+       /* OK, now we have newfrag added in the correct place in the list, but
+          newfrag->next points to a fragment which may be overlapping it
+       */
+       while (this && newfrag->ofs + newfrag->size >= this->ofs + this->size) {
+               /* 'this' frag is obsoleted. */
+               old = this;
+               this = old->next;
+               jffs2_obsolete_node_frag(c, old);
+       }
+       /* Now we're pointing at the first frag which isn't totally obsoleted by 
+          the new frag */
+       newfrag->next = this;
+
+       if (!this || newfrag->ofs + newfrag->size == this->ofs) {
+               return 0;
+       }
+       /* Still some overlap */
+       this->size = (this->ofs + this->size) - (newfrag->ofs + newfrag->size);
+       this->ofs = newfrag->ofs + newfrag->size;
+       return 0;
+}
+
+void jffs2_truncate_fraglist (struct jffs2_sb_info *c, struct jffs2_node_frag **list, __u32 size)
+{
+       D1(printk(KERN_DEBUG "Truncating fraglist to 0x%08x bytes\n", size));
+
+       while (*list) {
+               if ((*list)->ofs >= size) {
+                       struct jffs2_node_frag *this = *list;
+                       *list = this->next;
+                       D1(printk(KERN_DEBUG "Removing frag 0x%08x-0x%08x\n", this->ofs, this->ofs+this->size));
+                       jffs2_obsolete_node_frag(c, this);
+                       continue;
+               } else if ((*list)->ofs + (*list)->size > size) {
+                       D1(printk(KERN_DEBUG "Truncating frag 0x%08x-0x%08x\n", (*list)->ofs, (*list)->ofs + (*list)->size));
+                       (*list)->size = size - (*list)->ofs;
+               }
+               list = &(*list)->next;
+       }
+}
+
+/* Scan the list of all nodes present for this ino, build map of versions, etc. */
+
+void jffs2_read_inode (struct inode *inode)
+{
+       struct jffs2_tmp_dnode_info *tn_list, *tn;
+       struct jffs2_full_dirent *fd_list;
+       struct jffs2_inode_info *f;
+       struct jffs2_full_dnode *fn = NULL;
+       struct jffs2_sb_info *c;
+       struct jffs2_raw_inode latest_node;
+       int ret;
+       ssize_t retlen;
+
+       D1(printk(KERN_DEBUG "jffs2_read_inode(): inode->i_ino == %lu\n", inode->i_ino));
+
+       f = JFFS2_INODE_INFO(inode);
+       c = JFFS2_SB_INFO(inode->i_sb);
+
+       memset(f, 0, sizeof(*f));
+       D2(printk(KERN_DEBUG "getting inocache\n"));
+       init_MUTEX(&f->sem);
+       f->inocache = jffs2_get_ino_cache(c, inode->i_ino);
+       D2(printk(KERN_DEBUG "jffs2_read_inode(): Got inocache at %p\n", f->inocache));
+
+       if (!f->inocache && inode->i_ino == 1) {
+               /* Special case - no root inode on medium */
+               f->inocache = jffs2_alloc_inode_cache();
+               if (!f->inocache) {
+                       printk(KERN_CRIT "jffs2_read_inode(): Cannot allocate inocache for root inode\n");
+                       make_bad_inode(inode);
+                       return;
+               }
+               D1(printk(KERN_DEBUG "jffs2_read_inode(): Creating inocache for root inode\n"));
+               memset(f->inocache, 0, sizeof(struct jffs2_inode_cache));
+               f->inocache->ino = f->inocache->nlink = 1;
+               f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
+               jffs2_add_ino_cache(c, f->inocache);
+       }
+       if (!f->inocache) {
+               printk(KERN_WARNING "jffs2_read_inode() on nonexistent ino %lu\n", (unsigned long)inode->i_ino);
+               make_bad_inode(inode);
+               return;
+       }
+       D1(printk(KERN_DEBUG "jffs2_read_inode(): ino #%lu nlink is %d\n", (unsigned long)inode->i_ino, f->inocache->nlink));
+       inode->i_nlink = f->inocache->nlink;
+
+       /* Grab all nodes relevant to this ino */
+       ret = jffs2_get_inode_nodes(c, inode->i_ino, f, &tn_list, &fd_list, &f->highest_version);
+
+       if (ret) {
+               printk(KERN_CRIT "jffs2_get_inode_nodes() for ino %lu returned %d\n", inode->i_ino, ret);
+               make_bad_inode(inode);
+               return;
+       }
+       f->dents = fd_list;
+
+       while (tn_list) {
+               static __u32 mdata_ver = 0;
+
+               tn = tn_list;
+
+               fn = tn->fn;
+
+               if (f->metadata && tn->version > mdata_ver) {
+                       D1(printk(KERN_DEBUG "Obsoleting old metadata at 0x%08x\n", f->metadata->raw->flash_offset &~3));
+                       jffs2_mark_node_obsolete(c, f->metadata->raw);
+                       jffs2_free_full_dnode(f->metadata);
+                       f->metadata = NULL;
+                       
+                       mdata_ver = 0;
+               }
+
+               if (fn->size) {
+                       jffs2_add_full_dnode_to_inode(c, f, fn);
+               } else {
+                       /* Zero-sized node at end of version list. Just a metadata update */
+                       D1(printk(KERN_DEBUG "metadata @%08x: ver %d\n", fn->raw->flash_offset &~3, tn->version));
+                       f->metadata = fn;
+                       mdata_ver = tn->version;
+               }
+               tn_list = tn->next;
+               jffs2_free_tmp_dnode_info(tn);
+       }
+       if (!fn) {
+               /* No data nodes for this inode. */
+               if (inode->i_ino != 1) {
+                       printk(KERN_WARNING "jffs2_read_inode(): No data nodes found for ino #%lu\n", inode->i_ino);
+                       if (!fd_list) {
+                               make_bad_inode(inode);
+                               return;
+                       }
+                       printk(KERN_WARNING "jffs2_read_inode(): But it has children so we fake some modes for it\n");
+               }
+               inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
+               inode->i_atime = inode->i_ctime = inode->i_mtime = CURRENT_TIME;
+               inode->i_nlink = f->inocache->nlink;
+               inode->i_size = 0;
+       } else {
+               __u32 crc;
+
+               ret = c->mtd->read(c->mtd, fn->raw->flash_offset & ~3, sizeof(latest_node), &retlen, (void *)&latest_node);
+               if (ret || retlen != sizeof(latest_node)) {
+                       printk(KERN_NOTICE "MTD read in jffs2_read_inode() failed: Returned %d, %ld of %d bytes read\n",
+                              ret, (long)retlen, sizeof(latest_node));
+                       jffs2_clear_inode(inode);
+                       make_bad_inode(inode);
+                       return;
+               }
+
+               crc = crc32(0, &latest_node, sizeof(latest_node)-8);
+               if (crc != latest_node.node_crc) {
+                       printk(KERN_NOTICE "CRC failed for read_inode of inode %ld at physical location 0x%x\n", inode->i_ino, fn->raw->flash_offset & ~3);
+                       jffs2_clear_inode(inode);
+                       make_bad_inode(inode);
+                       return;
+               }
+
+               inode->i_mode = latest_node.mode;
+               inode->i_uid = latest_node.uid;
+               inode->i_gid = latest_node.gid;
+               inode->i_size = latest_node.isize;
+               if ((inode->i_mode & S_IFMT) == S_IFREG)
+                       jffs2_truncate_fraglist(c, &f->fraglist, latest_node.isize);
+               inode->i_atime = latest_node.atime;
+               inode->i_mtime = latest_node.mtime;
+               inode->i_ctime = latest_node.ctime;
+       }
+
+       /* OK, now the special cases. Certain inode types should
+          have only one data node, and it's kept as the metadata
+          node */
+       if ((inode->i_mode & S_IFMT) == S_IFBLK ||
+           (inode->i_mode & S_IFMT) == S_IFCHR ||
+           (inode->i_mode & S_IFMT) == S_IFLNK) {
+               if (f->metadata) {
+                       printk(KERN_WARNING "Argh. Special inode #%lu with mode 0%o had metadata node\n", inode->i_ino, inode->i_mode);
+                       jffs2_clear_inode(inode);
+                       make_bad_inode(inode);
+                       return;
+               }
+               if (!f->fraglist) {
+                       printk(KERN_WARNING "Argh. Special inode #%lu with mode 0%o has no fragments\n", inode->i_ino, inode->i_mode);
+                       jffs2_clear_inode(inode);
+                       make_bad_inode(inode);
+                       return;
+               }
+               /* ASSERT: f->fraglist != NULL */
+               if (f->fraglist->next) {
+                       printk(KERN_WARNING "Argh. Special inode #%lu had more than one node\n", inode->i_ino);
+                       jffs2_clear_inode(inode);
+                       make_bad_inode(inode);
+                       return;
+               }
+               /* OK. We're happy */
+               f->metadata = f->fraglist->node;
+               jffs2_free_node_frag(f->fraglist);
+               f->fraglist = NULL;
+       }                       
+           
+       inode->i_blksize = PAGE_SIZE;
+       inode->i_blocks = (inode->i_size + 511) >> 9;
+       
+       switch (inode->i_mode & S_IFMT) {
+               unsigned short rdev;
+
+       case S_IFLNK:
+               inode->i_op = &jffs2_symlink_inode_operations;
+               break;
+               
+       case S_IFDIR:
+               inode->i_op = &jffs2_dir_inode_operations;
+               inode->i_fop = &jffs2_dir_operations;
+               break;
+
+       case S_IFREG:
+               inode->i_op = &jffs2_file_inode_operations;
+               inode->i_fop = &jffs2_file_operations;
+               inode->i_mapping->a_ops = &jffs2_file_address_operations;
+               inode->i_mapping->nrpages = 0;
+               break;
+
+       case S_IFBLK:
+       case S_IFCHR:
+               /* Read the device numbers from the media */
+               D1(printk(KERN_DEBUG "Reading device numbers from flash\n"));
+               if (jffs2_read_dnode(c, f->metadata, (char *)&rdev, 0, sizeof(rdev)) < 0) {
+                       /* Eep */
+                       printk(KERN_NOTICE "Read device numbers for inode %lu failed\n", (unsigned long)inode->i_ino);
+                       jffs2_clear_inode(inode);
+                       make_bad_inode(inode);
+                       return;
+               }                       
+
+       case S_IFSOCK:
+       case S_IFIFO:
+               inode->i_op = &jffs2_file_inode_operations;
+               init_special_inode(inode, inode->i_mode, kdev_t_to_nr(MKDEV(rdev>>8, rdev&0xff)));
+               break;
+
+       default:
+               printk(KERN_WARNING "jffs2_read_inode(): Bogus imode %o for ino %lu", inode->i_mode, (unsigned long)inode->i_ino);
+       }
+       D1(printk(KERN_DEBUG "jffs2_read_inode() returning\n"));
+}
+
+void jffs2_clear_inode (struct inode *inode)
+{
+       /* We can forget about this inode for now - drop all 
+        *  the nodelists associated with it, etc.
+        */
+       struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
+       struct jffs2_node_frag *frag, *frags;
+       struct jffs2_full_dirent *fd, *fds;
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
+
+       D1(printk(KERN_DEBUG "jffs2_clear_inode(): ino #%lu mode %o\n", inode->i_ino, inode->i_mode));
+
+       frags = f->fraglist;
+       fds = f->dents;
+       if (f->metadata) {
+               if (!f->inocache->nlink)
+                       jffs2_mark_node_obsolete(c, f->metadata->raw);
+               jffs2_free_full_dnode(f->metadata);
+       }
+
+       while (frags) {
+               frag = frags;
+               frags = frag->next;
+               D2(printk(KERN_DEBUG "jffs2_clear_inode: frag at 0x%x-0x%x: node %p, frags %d--\n", frag->ofs, frag->ofs+frag->size, frag->node, frag->node?frag->node->frags:0));
+
+               if (frag->node && !(--frag->node->frags)) {
+                       /* Not a hole, and it's the final remaining frag of this node. Free the node */
+                       if (!f->inocache->nlink)
+                               jffs2_mark_node_obsolete(c, frag->node->raw);
+
+                       jffs2_free_full_dnode(frag->node);
+               }
+               jffs2_free_node_frag(frag);
+       }
+       while(fds) {
+               fd = fds;
+               fds = fd->next;
+               jffs2_free_full_dirent(fd);
+       }
+       //      if (!f->inocache->nlink) {
+               //              D1(printk(KERN_DEBUG "jffs2_clear_inode() deleting inode #%lu\n", inode->i_ino));
+               //              jffs2_del_ino_cache(JFFS2_SB_INFO(inode->i_sb), f->inocache);
+               //              jffs2_free_inode_cache(f->inocache);
+       //      }
+};
+
diff --git a/fs/jffs2/scan.c b/fs/jffs2/scan.c
new file mode 100644 (file)
index 0000000..7e031d5
--- /dev/null
@@ -0,0 +1,658 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: scan.c,v 1.44 2001/04/13 00:46:41 nico Exp $
+ *
+ */
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/jffs2.h>
+#include <linux/mtd/mtd.h>
+#include <linux/pagemap.h>
+#include "nodelist.h"
+#include "crc32.h"
+
+
+#define DIRTY_SPACE(x) do { typeof(x) _x = (x); \
+               c->free_size -= _x; c->dirty_size += _x; \
+               jeb->free_size -= _x ; jeb->dirty_size += _x; \
+               }while(0)
+#define USED_SPACE(x) do { typeof(x) _x = (x); \
+               c->free_size -= _x; c->used_size += _x; \
+               jeb->free_size -= _x ; jeb->used_size += _x; \
+               }while(0)
+
+#define noisy_printk(noise, args...) do { \
+       if (*(noise)) { \
+               printk(KERN_NOTICE args); \
+                (*(noise))--; \
+                if (!(*(noise))) { \
+                        printk(KERN_NOTICE "Further such events for this erase block will not be printed\n"); \
+                } \
+       } \
+} while(0)
+
+static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb);
+
+/* These helper functions _must_ increase ofs and also do the dirty/used space accounting. 
+ * Returning an error will abort the mount - bad checksums etc. should just mark the space
+ * as dirty.
+ */
+static int jffs2_scan_empty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, __u32 *ofs, int *noise);
+static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, __u32 *ofs);
+static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, __u32 *ofs);
+
+
+int jffs2_scan_medium(struct jffs2_sb_info *c)
+{
+       int i, ret;
+       if (!c->blocks) {
+               printk(KERN_WARNING "EEEK! c->blocks is NULL!\n");
+               return -EINVAL;
+       }
+       for (i=0; i<c->nr_blocks; i++) {
+               struct jffs2_eraseblock *jeb = &c->blocks[i];
+
+               ret = jffs2_scan_eraseblock(c, jeb);
+               if (ret)
+                       return ret;
+
+               ACCT_PARANOIA_CHECK(jeb);
+
+                  /* Now decide which list to put it on */
+               if (jeb->used_size == PAD(sizeof(struct jffs2_unknown_node)) && !jeb->first_node->next_in_ino) {
+                       /* Only a CLEANMARKER node is valid */
+                       if (!jeb->dirty_size) {
+                               /* It's actually free */
+                               list_add(&jeb->list, &c->free_list);
+                               c->nr_free_blocks++;
+                       } else {
+                               /* Dirt */
+                               D1(printk(KERN_DEBUG "Adding all-dirty block at 0x%08x to erase_pending_list\n", jeb->offset));
+                               list_add(&jeb->list, &c->erase_pending_list);
+                               c->nr_erasing_blocks++;
+                       }
+               } else if (jeb->used_size > c->sector_size - (2*sizeof(struct jffs2_raw_inode))) {
+                        /* Full (or almost full) of clean data. Clean list */
+                        list_add(&jeb->list, &c->clean_list);
+                } else if (jeb->used_size) {
+                        /* Some data, but not full. Dirty list. */
+                        /* Except that we want to remember the block with most free space,
+                           and stick it in the 'nextblock' position to start writing to it.
+                           Later when we do snapshots, this must be the most recent block,
+                           not the one with most free space.
+                        */
+                        if (jeb->free_size > 2*sizeof(struct jffs2_raw_inode) && 
+                                (!c->nextblock || c->nextblock->free_size < jeb->free_size)) {
+                                /* Better candidate for the next writes to go to */
+                                if (c->nextblock)
+                                        list_add(&c->nextblock->list, &c->dirty_list);
+                                c->nextblock = jeb;
+                        } else {
+                                list_add(&jeb->list, &c->dirty_list);
+                        }
+               } else {
+                       /* Nothing valid - not even a clean marker. Needs erasing. */
+                        /* For now we just put it on the erasing list. We'll start the erases later */
+                       printk(KERN_NOTICE "JFFS2: Erase block at 0x%08x is not formatted. It will be erased\n", jeb->offset);
+                        list_add(&jeb->list, &c->erase_pending_list);
+                       c->nr_erasing_blocks++;
+                }
+        }
+       if (c->nr_erasing_blocks)
+               jffs2_erase_pending_trigger(c);
+
+       return 0;
+}
+
+static int jffs2_scan_eraseblock (struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb) {
+       struct jffs2_unknown_node node;
+       __u32 ofs, prevofs;
+       __u32 hdr_crc, nodetype;
+       int err;
+       int noise = 10;
+
+       ofs = jeb->offset;
+       prevofs = jeb->offset - 1;
+
+       D1(printk(KERN_DEBUG "jffs2_scan_eraseblock(): Scanning block at 0x%x\n", ofs));
+
+       while(ofs < jeb->offset + c->sector_size) {
+               ssize_t retlen;
+               ACCT_PARANOIA_CHECK(jeb);
+               
+               if (ofs & 3) {
+                       printk(KERN_WARNING "Eep. ofs 0x%08x not word-aligned!\n", ofs);
+                       ofs = (ofs+3)&~3;
+                       continue;
+               }
+               if (ofs == prevofs) {
+                       printk(KERN_WARNING "ofs 0x%08x has already been seen. Skipping\n", ofs);
+                       DIRTY_SPACE(4);
+                       ofs += 4;
+                       continue;
+               }
+               prevofs = ofs;
+               
+               if (jeb->offset + c->sector_size < ofs + sizeof(node)) {
+                       D1(printk(KERN_DEBUG "Fewer than %d bytes left to end of block. Not reading\n", sizeof(struct jffs2_unknown_node)));
+                       DIRTY_SPACE((jeb->offset + c->sector_size)-ofs);
+                       break;
+               }
+
+               err = c->mtd->read(c->mtd, ofs, sizeof(node), &retlen, (char *)&node);
+               
+               if (err) {
+                       D1(printk(KERN_WARNING "mtd->read(0x%x bytes from 0x%x) returned %d\n", sizeof(node), ofs, err));
+                       return err;
+               }
+               if (retlen < sizeof(node)) {
+                       D1(printk(KERN_WARNING "Read at 0x%x gave only 0x%x bytes\n", ofs, retlen));
+                       DIRTY_SPACE(retlen);
+                       ofs += retlen;
+                       continue;
+               }
+
+               if (node.magic == JFFS2_EMPTY_BITMASK && node.nodetype == JFFS2_EMPTY_BITMASK) {
+                       D1(printk(KERN_DEBUG "Found empty flash at 0x%x\n", ofs));
+                       err = jffs2_scan_empty(c, jeb, &ofs, &noise);
+                       if (err) return err;
+                       continue;
+               }
+
+               if (ofs == jeb->offset && node.magic == KSAMTIB_CIGAM_2SFFJ) {
+                       printk(KERN_WARNING "Magic bitmask is backwards at offset 0x%08x. Wrong endian filesystem?\n", ofs);
+                       DIRTY_SPACE(4);
+                       ofs += 4;
+                       continue;
+               }
+               if (node.magic == JFFS2_DIRTY_BITMASK) {
+                       D1(printk(KERN_DEBUG "Empty bitmask at 0x%08x\n", ofs));
+                       DIRTY_SPACE(4);
+                       ofs += 4;
+                       continue;
+               }
+               if (node.magic == JFFS2_OLD_MAGIC_BITMASK) {
+                       printk(KERN_WARNING "Old JFFS2 bitmask found at 0x%08x\n", ofs);
+                       printk(KERN_WARNING "You cannot use older JFFS2 filesystems with newer kernels\n");
+                       DIRTY_SPACE(4);
+                       ofs += 4;
+                       continue;
+               }
+               if (node.magic != JFFS2_MAGIC_BITMASK) {
+                       /* OK. We're out of possibilities. Whinge and move on */
+                       noisy_printk(&noise, "jffs2_scan_eraseblock(): Magic bitmask 0x%04x not found at 0x%08x: 0x%04x instead\n", JFFS2_MAGIC_BITMASK, ofs, node.magic);
+                       DIRTY_SPACE(4);
+                       ofs += 4;
+                       continue;
+               }
+               /* We seem to have a node of sorts. Check the CRC */
+               nodetype = node.nodetype;
+               node.nodetype |= JFFS2_NODE_ACCURATE;
+               hdr_crc = crc32(0, &node, sizeof(node)-4);
+               node.nodetype = nodetype;
+               if (hdr_crc != node.hdr_crc) {
+                       noisy_printk(&noise, "jffs2_scan_eraseblock(): Node at 0x%08x {0x%04x, 0x%04x, 0x%08x) has invalid CRC 0x%08x (calculated 0x%08x)\n",
+                                    ofs, node.magic, node.nodetype, node.totlen, node.hdr_crc, hdr_crc);
+                       DIRTY_SPACE(4);
+                       ofs += 4;
+                       continue;
+               }
+
+               switch(node.nodetype | JFFS2_NODE_ACCURATE) {
+               case JFFS2_NODETYPE_INODE:
+                       err = jffs2_scan_inode_node(c, jeb, &ofs);
+                       if (err) return err;
+                       break;
+                       
+               case JFFS2_NODETYPE_DIRENT:
+                       err = jffs2_scan_dirent_node(c, jeb, &ofs);
+                       if (err) return err;
+                       break;
+
+               case JFFS2_NODETYPE_CLEANMARKER:
+                       if (node.totlen != sizeof(struct jffs2_unknown_node)) {
+                               printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x has totlen 0x%x != normal 0x%x\n", 
+                                      ofs, node.totlen, sizeof(struct jffs2_unknown_node));
+                               DIRTY_SPACE(PAD(sizeof(struct jffs2_unknown_node)));
+                       } else if (jeb->first_node) {
+                               printk(KERN_NOTICE "CLEANMARKER node found at 0x%08x, not first node in block (0x%08x)\n", ofs, jeb->offset);
+                               DIRTY_SPACE(PAD(sizeof(struct jffs2_unknown_node)));
+                               ofs += PAD(sizeof(struct jffs2_unknown_node));
+                               continue;
+                       } else {
+                               struct jffs2_raw_node_ref *marker_ref = jffs2_alloc_raw_node_ref();
+                               if (!marker_ref) {
+                                       printk(KERN_NOTICE "Failed to allocate node ref for clean marker\n");
+                                       return -ENOMEM;
+                               }
+                               marker_ref->next_in_ino = NULL;
+                               marker_ref->next_phys = NULL;
+                               marker_ref->flash_offset = ofs;
+                               marker_ref->totlen = sizeof(struct jffs2_unknown_node);
+                               jeb->first_node = jeb->last_node = marker_ref;
+                            
+                               USED_SPACE(PAD(sizeof(struct jffs2_unknown_node)));
+                       }
+                       ofs += PAD(sizeof(struct jffs2_unknown_node));
+                       break;
+
+               default:
+                       switch (node.nodetype & JFFS2_COMPAT_MASK) {
+                       case JFFS2_FEATURE_ROCOMPAT:
+                               printk(KERN_NOTICE "Read-only compatible feature node (0x%04x) found at offset 0x%08x\n", node.nodetype, ofs);
+                               c->flags |= JFFS2_SB_FLAG_RO;
+                               DIRTY_SPACE(PAD(node.totlen));
+                               ofs += PAD(node.totlen);
+                               continue;
+
+                       case JFFS2_FEATURE_INCOMPAT:
+                               printk(KERN_NOTICE "Incompatible feature node (0x%04x) found at offset 0x%08x\n", node.nodetype, ofs);
+                               return -EINVAL;
+
+                       case JFFS2_FEATURE_RWCOMPAT_DELETE:
+                               printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", node.nodetype, ofs);
+                               DIRTY_SPACE(PAD(node.totlen));
+                               ofs += PAD(node.totlen);
+                               break;
+
+                       case JFFS2_FEATURE_RWCOMPAT_COPY:
+                               printk(KERN_NOTICE "Unknown but compatible feature node (0x%04x) found at offset 0x%08x\n", node.nodetype, ofs);
+                               USED_SPACE(PAD(node.totlen));
+                               ofs += PAD(node.totlen);
+                               break;
+                       }
+               }
+       }
+       D1(printk(KERN_DEBUG "Block at 0x%08x: free 0x%08x, dirty 0x%08x, used 0x%08x\n", jeb->offset, 
+                 jeb->free_size, jeb->dirty_size, jeb->used_size));
+       return 0;
+}
+
+/* We're pointing at the first empty word on the flash. Scan and account for the whole dirty region */
+static int jffs2_scan_empty(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, __u32 *startofs, int *noise)
+{
+       __u32 *buf;
+       __u32 scanlen = (jeb->offset + c->sector_size) - *startofs;
+       __u32 curofs = *startofs;
+       
+       buf = kmalloc(min(PAGE_SIZE, scanlen), GFP_KERNEL);
+       if (!buf) {
+               printk(KERN_WARNING "Scan buffer allocation failed\n");
+               return -ENOMEM;
+       }
+       while(scanlen) {
+               ssize_t retlen;
+               int ret, i;
+               
+               ret = c->mtd->read(c->mtd, curofs, min(PAGE_SIZE, scanlen), &retlen, (char *)buf);
+               if(ret) {
+                       D1(printk(KERN_WARNING "jffs2_scan_empty(): Read 0x%lx bytes at 0x%08x returned %d\n", min(PAGE_SIZE, scanlen), curofs, ret));
+                       kfree(buf);
+                       return ret;
+               }
+               if (retlen < 4) {
+                       D1(printk(KERN_WARNING "Eep. too few bytes read in scan_empty()\n"));
+                       kfree(buf);
+                       return -EIO;
+               }
+               for (i=0; i<(retlen / 4); i++) {
+                       if (buf[i] != 0xffffffff) {
+                               curofs += i*4;
+
+                               noisy_printk(noise, "jffs2_scan_empty(): Empty block at 0x%08x ends at 0x%08x (with 0x%08x)! Marking dirty\n", *startofs, curofs, buf[i]);
+                               DIRTY_SPACE(curofs - (*startofs));
+                               *startofs = curofs;
+                               kfree(buf);
+                               return 0;
+                       }
+               }
+               scanlen -= retlen&~3;
+               curofs += retlen&~3;
+       }
+
+       D1(printk(KERN_DEBUG "Empty flash detected from 0x%08x to 0x%08x\n", *startofs, curofs));
+       kfree(buf);
+       *startofs = curofs;
+       return 0;
+}
+
+static struct jffs2_inode_cache *jffs2_scan_make_ino_cache(struct jffs2_sb_info *c, __u32 ino)
+{
+       struct jffs2_inode_cache *ic;
+
+       ic = jffs2_get_ino_cache(c, ino);
+       if (ic)
+               return ic;
+
+       ic = jffs2_alloc_inode_cache();
+       if (!ic) {
+               printk(KERN_NOTICE "jffs2_scan_make_inode_cache(): allocation of inode cache failed\n");
+               return NULL;
+       }
+       memset(ic, 0, sizeof(*ic));
+       ic->scan = kmalloc(sizeof(struct jffs2_scan_info), GFP_KERNEL);
+       if (!ic->scan) {
+               printk(KERN_NOTICE "jffs2_scan_make_inode_cache(): allocation of scan info for inode cache failed\n");
+               jffs2_free_inode_cache(ic);
+               return NULL;
+       }
+       memset(ic->scan, 0, sizeof(*ic->scan));
+       ic->ino = ino;
+       ic->nodes = (void *)ic;
+       jffs2_add_ino_cache(c, ic);
+       if (ino == 1)
+               ic->nlink=1;
+       return ic;
+}
+
+static int jffs2_scan_inode_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, __u32 *ofs)
+{
+       struct jffs2_raw_node_ref *raw;
+       struct jffs2_full_dnode *fn;
+       struct jffs2_tmp_dnode_info *tn, **tn_list;
+       struct jffs2_inode_cache *ic;
+       struct jffs2_raw_inode ri;
+       __u32 crc;
+       __u16 oldnodetype;
+       int ret;
+       ssize_t retlen;
+
+       D1(printk(KERN_DEBUG "jffs2_scan_inode_node(): Node at 0x%08x\n", *ofs));
+
+       ret = c->mtd->read(c->mtd, *ofs, sizeof(ri), &retlen, (char *)&ri);
+       if (ret) {
+               printk(KERN_NOTICE "jffs2_scan_inode_node(): Read error at 0x%08x: %d\n", *ofs, ret);
+               return ret;
+       }
+       if (retlen != sizeof(ri)) {
+               printk(KERN_NOTICE "Short read: 0x%x bytes at 0x%08x instead of requested %x\n", 
+                      retlen, *ofs, sizeof(ri));
+               return -EIO;
+       }
+
+       /* We sort of assume that the node was accurate when it was 
+          first written to the medium :) */
+       oldnodetype = ri.nodetype;
+       ri.nodetype |= JFFS2_NODE_ACCURATE;
+       crc = crc32(0, &ri, sizeof(ri)-8);
+       ri.nodetype = oldnodetype;
+
+       if(crc != ri.node_crc) {
+               printk(KERN_NOTICE "jffs2_scan_inode_node(): CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
+                      *ofs, ri.node_crc, crc);
+               /* FIXME: Why do we believe totlen? */
+               DIRTY_SPACE(4);
+               *ofs += 4;
+               return 0;
+       }
+
+       if (ri.csize) {
+               /* Check data CRC too */
+               unsigned char *dbuf;
+               __u32 crc;
+
+               dbuf = kmalloc(PAGE_CACHE_SIZE, GFP_KERNEL);
+               if (!dbuf) {
+                       printk(KERN_NOTICE "jffs2_scan_inode_node(): allocation of temporary data buffer for CRC check failed\n");
+                       return -ENOMEM;
+               }
+               ret = c->mtd->read(c->mtd, *ofs+sizeof(ri), ri.csize, &retlen, dbuf);
+               if (ret) {
+                       printk(KERN_NOTICE "jffs2_scan_inode_node(): Read error at 0x%08x: %d\n", *ofs+sizeof(ri), ret);
+                       kfree(dbuf);
+                       return ret;
+               }
+               if (retlen != ri.csize) {
+                       printk(KERN_NOTICE "Short read: 0x%x bytes at 0x%08x instead of requested %x\n", 
+                              retlen, *ofs+ sizeof(ri), ri.csize);
+                       kfree(dbuf);
+                       return -EIO;
+               }
+               crc = crc32(0, dbuf, ri.csize);
+               kfree(dbuf);
+               if (crc != ri.data_crc) {
+                       printk(KERN_NOTICE "jffs2_scan_inode_node(): Data CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
+                              *ofs, ri.data_crc, crc);
+                       DIRTY_SPACE(PAD(ri.totlen));
+                       *ofs += PAD(ri.totlen);
+                       return -0;
+               }
+       }
+
+       /* Wheee. It worked */
+       raw = jffs2_alloc_raw_node_ref();
+       if (!raw) {
+               printk(KERN_NOTICE "jffs2_scan_inode_node(): allocation of node reference failed\n");
+               return -ENOMEM;
+       }
+       tn = jffs2_alloc_tmp_dnode_info();
+       if (!tn) {
+               jffs2_free_raw_node_ref(raw);
+               return -ENOMEM;
+       }
+       fn = jffs2_alloc_full_dnode();
+       if (!fn) {
+               jffs2_free_tmp_dnode_info(tn);
+               jffs2_free_raw_node_ref(raw);
+               return -ENOMEM;
+       }
+       ic = jffs2_scan_make_ino_cache(c, ri.ino);
+       if (!ic) {
+               jffs2_free_full_dnode(fn);
+               jffs2_free_tmp_dnode_info(tn);
+               jffs2_free_raw_node_ref(raw);
+               return -ENOMEM;
+       }
+
+       /* Build the data structures and file them for later */
+       raw->flash_offset = *ofs;
+       raw->totlen = PAD(ri.totlen);
+       raw->next_phys = NULL;
+       raw->next_in_ino = ic->nodes;
+       ic->nodes = raw;
+       if (!jeb->first_node)
+               jeb->first_node = raw;
+       if (jeb->last_node)
+               jeb->last_node->next_phys = raw;
+       jeb->last_node = raw;
+
+       D1(printk(KERN_DEBUG "Node is ino #%u, version %d. Range 0x%x-0x%x\n", 
+                 ri.ino, ri.version, ri.offset, ri.offset+ri.dsize));
+
+       for (tn_list = &ic->scan->tmpnodes; *tn_list; tn_list = &((*tn_list)->next)) {
+               if ((*tn_list)->version < ri.version)
+                       continue;
+               if ((*tn_list)->version > ri.version) 
+                       break;
+               /* Wheee. We've found another instance of the same version number.
+                  We should obsolete one of them. 
+               */
+               D1(printk(KERN_DEBUG "Duplicate version %d found in ino #%u. Previous one is at 0x%08x\n", ri.version, ic->ino, (*tn_list)->fn->raw->flash_offset &~3));
+               if (!jeb->used_size) {
+                       D1(printk(KERN_DEBUG "No valid nodes yet found in this eraseblock 0x%08x, so obsoleting the new instance at 0x%08x\n", 
+                                 jeb->offset, raw->flash_offset & ~3));
+                       ri.nodetype &= ~JFFS2_NODE_ACCURATE;
+                       /* Perhaps we could also mark it as such on the medium. Maybe later */
+               }
+               break;
+       }
+
+       if (ri.nodetype & JFFS2_NODE_ACCURATE) {
+               memset(fn,0,sizeof(*fn));
+
+               fn->ofs = ri.offset;
+               fn->size = ri.dsize;
+               fn->frags = 0;
+               fn->raw = raw;
+
+               tn->next = NULL;
+               tn->fn = fn;
+               tn->version = ri.version;
+
+               USED_SPACE(PAD(ri.totlen));
+               jffs2_add_tn_to_list(tn, &ic->scan->tmpnodes);
+               /* Make sure the one we just added is the _last_ in the list
+                  with this version number, so the older ones get obsoleted */
+               while (tn->next && tn->next->version == tn->version) {
+
+                       D1(printk(KERN_DEBUG "Shifting new node at 0x%08x after other node at 0x%08x for version %d in list\n",
+                                 fn->raw->flash_offset&~3, tn->next->fn->raw->flash_offset &~3, ri.version));
+
+                       if(tn->fn != fn)
+                               BUG();
+                       tn->fn = tn->next->fn;
+                       tn->next->fn = fn;
+                       tn = tn->next;
+               }
+       } else {
+               jffs2_free_full_dnode(fn);
+               jffs2_free_tmp_dnode_info(tn);
+               raw->flash_offset |= 1;
+               DIRTY_SPACE(PAD(ri.totlen));
+       }               
+       *ofs += PAD(ri.totlen);
+       return 0;
+}
+
+static int jffs2_scan_dirent_node(struct jffs2_sb_info *c, struct jffs2_eraseblock *jeb, __u32 *ofs)
+{
+       struct jffs2_raw_node_ref *raw;
+       struct jffs2_full_dirent *fd;
+       struct jffs2_inode_cache *ic;
+       struct jffs2_raw_dirent rd;
+       __u16 oldnodetype;
+       int ret;
+       __u32 crc;
+       ssize_t retlen;
+
+       D1(printk(KERN_DEBUG "jffs2_scan_dirent_node(): Node at 0x%08x\n", *ofs));
+
+       ret = c->mtd->read(c->mtd, *ofs, sizeof(rd), &retlen, (char *)&rd);
+       if (ret) {
+               printk(KERN_NOTICE "jffs2_scan_dirent_node(): Read error at 0x%08x: %d\n", *ofs, ret);
+               return ret;
+       }
+       if (retlen != sizeof(rd)) {
+               printk(KERN_NOTICE "Short read: 0x%x bytes at 0x%08x instead of requested %x\n", 
+                      retlen, *ofs, sizeof(rd));
+               return -EIO;
+       }
+
+       /* We sort of assume that the node was accurate when it was 
+          first written to the medium :) */
+       oldnodetype = rd.nodetype;
+       rd.nodetype |= JFFS2_NODE_ACCURATE;
+       crc = crc32(0, &rd, sizeof(rd)-8);
+       rd.nodetype = oldnodetype;
+
+       if (crc != rd.node_crc) {
+               printk(KERN_NOTICE "jffs2_scan_dirent_node(): Node CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
+                      *ofs, rd.node_crc, crc);
+               /* FIXME: Why do we believe totlen? */
+               DIRTY_SPACE(4);
+               *ofs += 4;
+               return 0;
+       }
+
+       fd = jffs2_alloc_full_dirent(rd.nsize+1);
+       if (!fd) {
+               return -ENOMEM;
+}
+       ret = c->mtd->read(c->mtd, *ofs + sizeof(rd), rd.nsize, &retlen, &fd->name[0]);
+       if (ret) {
+               jffs2_free_full_dirent(fd);
+               printk(KERN_NOTICE "jffs2_scan_dirent_node(): Read error at 0x%08x: %d\n", 
+                      *ofs + sizeof(rd), ret);
+               return ret;
+       }
+       if (retlen != rd.nsize) {
+               jffs2_free_full_dirent(fd);
+               printk(KERN_NOTICE "Short read: 0x%x bytes at 0x%08x instead of requested %x\n", 
+                      retlen, *ofs + sizeof(rd), rd.nsize);
+               return -EIO;
+       }
+       crc = crc32(0, fd->name, rd.nsize);
+       if (crc != rd.name_crc) {
+               printk(KERN_NOTICE "jffs2_scan_dirent_node(): Name CRC failed on node at 0x%08x: Read 0x%08x, calculated 0x%08x\n",
+                      *ofs, rd.name_crc, crc); 
+               jffs2_free_full_dirent(fd);
+               /* FIXME: Why do we believe totlen? */
+               DIRTY_SPACE(PAD(rd.totlen));
+               *ofs += PAD(rd.totlen);
+               return 0;
+       }
+       raw = jffs2_alloc_raw_node_ref();
+       if (!raw) {
+               jffs2_free_full_dirent(fd);
+               printk(KERN_NOTICE "jffs2_scan_dirent_node(): allocation of node reference failed\n");
+               return -ENOMEM;
+       }
+       ic = jffs2_scan_make_ino_cache(c, rd.pino);
+       if (!ic) {
+               jffs2_free_full_dirent(fd);
+               jffs2_free_raw_node_ref(raw);
+               return -ENOMEM;
+       }
+       
+       raw->totlen = PAD(rd.totlen);
+       raw->flash_offset = *ofs;
+       raw->next_phys = NULL;
+       raw->next_in_ino = ic->nodes;
+       ic->nodes = raw;
+       if (!jeb->first_node)
+               jeb->first_node = raw;
+       if (jeb->last_node)
+               jeb->last_node->next_phys = raw;
+       jeb->last_node = raw;
+
+       if (rd.nodetype & JFFS2_NODE_ACCURATE) {
+               fd->raw = raw;
+               fd->next = NULL;
+               fd->version = rd.version;
+               fd->ino = rd.ino;
+               fd->name[rd.nsize]=0;
+               fd->nhash = full_name_hash(fd->name, rd.nsize);
+               fd->type = rd.type;
+
+               USED_SPACE(PAD(rd.totlen));
+               jffs2_add_fd_to_list(c, fd, &ic->scan->dents);
+       } else {
+               raw->flash_offset |= 1;
+               jffs2_free_full_dirent(fd);
+
+               DIRTY_SPACE(PAD(rd.totlen));
+       } 
+       *ofs += PAD(rd.totlen);
+       return 0;
+}
diff --git a/fs/jffs2/super.c b/fs/jffs2/super.c
new file mode 100644 (file)
index 0000000..5148ecb
--- /dev/null
@@ -0,0 +1,355 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: super.c,v 1.43 2001/05/29 08:59:47 dwmw2 Exp $
+ *
+ */
+
+#include <linux/config.h>
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/version.h>
+#include <linux/slab.h>
+#include <linux/init.h>
+#include <linux/list.h>
+#include <linux/fs.h>
+#include <linux/jffs2.h>
+#include <linux/pagemap.h>
+#include <linux/mtd/mtd.h>
+#include <linux/interrupt.h>
+
+#include "nodelist.h"
+
+#ifndef MTD_BLOCK_MAJOR
+#define MTD_BLOCK_MAJOR 31
+#endif
+
+extern void jffs2_read_inode (struct inode *);
+void jffs2_put_super (struct super_block *);
+void jffs2_write_super (struct super_block *);
+static int jffs2_statfs (struct super_block *, struct statfs *);
+int jffs2_remount_fs (struct super_block *, int *, char *);
+extern void jffs2_clear_inode (struct inode *);
+
+static struct super_operations jffs2_super_operations =
+{
+       read_inode:     jffs2_read_inode,
+//     delete_inode:   jffs2_delete_inode,
+       put_super:      jffs2_put_super,
+       write_super:    jffs2_write_super,
+       statfs:         jffs2_statfs,
+//     remount_fs:     jffs2_remount_fs,
+       clear_inode:    jffs2_clear_inode
+};
+
+static int jffs2_statfs(struct super_block *sb, struct statfs *buf)
+{
+       struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
+       unsigned long avail;
+
+       buf->f_type = JFFS2_SUPER_MAGIC;
+       buf->f_bsize = 1 << PAGE_SHIFT;
+       buf->f_blocks = c->flash_size >> PAGE_SHIFT;
+       buf->f_files = 0;
+       buf->f_ffree = 0;
+       buf->f_namelen = JFFS2_MAX_NAME_LEN;
+
+       spin_lock_bh(&c->erase_completion_lock);
+
+       avail = c->dirty_size + c->free_size;
+       if (avail > c->sector_size * JFFS2_RESERVED_BLOCKS_WRITE)
+               avail -= c->sector_size * JFFS2_RESERVED_BLOCKS_WRITE;
+       else
+               avail = 0;
+
+       buf->f_bavail = buf->f_bfree = avail >> PAGE_SHIFT;
+
+#if CONFIG_JFFS2_FS_DEBUG > 0
+       printk(KERN_DEBUG "STATFS:\n");
+       printk(KERN_DEBUG "flash_size: %08x\n", c->flash_size);
+       printk(KERN_DEBUG "used_size: %08x\n", c->used_size);
+       printk(KERN_DEBUG "dirty_size: %08x\n", c->dirty_size);
+       printk(KERN_DEBUG "free_size: %08x\n", c->free_size);
+       printk(KERN_DEBUG "erasing_size: %08x\n", c->erasing_size);
+       printk(KERN_DEBUG "bad_size: %08x\n", c->bad_size);
+       printk(KERN_DEBUG "sector_size: %08x\n", c->sector_size);
+
+       if (c->nextblock) {
+               printk(KERN_DEBUG "nextblock: 0x%08x\n", c->nextblock->offset);
+       } else {
+               printk(KERN_DEBUG "nextblock: NULL\n");
+       }
+       if (c->gcblock) {
+               printk(KERN_DEBUG "gcblock: 0x%08x\n", c->gcblock->offset);
+       } else {
+               printk(KERN_DEBUG "gcblock: NULL\n");
+       }
+       if (list_empty(&c->clean_list)) {
+               printk(KERN_DEBUG "clean_list: empty\n");
+       } else {
+               struct list_head *this;
+
+               list_for_each(this, &c->clean_list) {
+                       struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
+                       printk(KERN_DEBUG "clean_list: %08x\n", jeb->offset);
+               }
+       }
+       if (list_empty(&c->dirty_list)) {
+               printk(KERN_DEBUG "dirty_list: empty\n");
+       } else {
+               struct list_head *this;
+
+               list_for_each(this, &c->dirty_list) {
+                       struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
+                       printk(KERN_DEBUG "dirty_list: %08x\n", jeb->offset);
+               }
+       }
+       if (list_empty(&c->erasing_list)) {
+               printk(KERN_DEBUG "erasing_list: empty\n");
+       } else {
+               struct list_head *this;
+
+               list_for_each(this, &c->erasing_list) {
+                       struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
+                       printk(KERN_DEBUG "erasing_list: %08x\n", jeb->offset);
+               }
+       }
+       if (list_empty(&c->erase_pending_list)) {
+               printk(KERN_DEBUG "erase_pending_list: empty\n");
+       } else {
+               struct list_head *this;
+
+               list_for_each(this, &c->erase_pending_list) {
+                       struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
+                       printk(KERN_DEBUG "erase_pending_list: %08x\n", jeb->offset);
+               }
+       }
+       if (list_empty(&c->free_list)) {
+               printk(KERN_DEBUG "free_list: empty\n");
+       } else {
+               struct list_head *this;
+
+               list_for_each(this, &c->free_list) {
+                       struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
+                       printk(KERN_DEBUG "free_list: %08x\n", jeb->offset);
+               }
+       }
+       if (list_empty(&c->bad_list)) {
+               printk(KERN_DEBUG "bad_list: empty\n");
+       } else {
+               struct list_head *this;
+
+               list_for_each(this, &c->bad_list) {
+                       struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
+                       printk(KERN_DEBUG "bad_list: %08x\n", jeb->offset);
+               }
+       }
+       if (list_empty(&c->bad_used_list)) {
+               printk(KERN_DEBUG "bad_used_list: empty\n");
+       } else {
+               struct list_head *this;
+
+               list_for_each(this, &c->bad_used_list) {
+                       struct jffs2_eraseblock *jeb = list_entry(this, struct jffs2_eraseblock, list);
+                       printk(KERN_DEBUG "bad_used_list: %08x\n", jeb->offset);
+               }
+       }
+#endif /* CONFIG_JFFS2_FS_DEBUG */
+
+       spin_unlock_bh(&c->erase_completion_lock);
+
+
+       return 0;
+}
+
+static struct super_block *jffs2_read_super(struct super_block *sb, void *data, int silent)
+{
+       struct jffs2_sb_info *c;
+       struct inode *root_i;
+       int i;
+
+       D1(printk(KERN_DEBUG "jffs2: read_super for device %s\n", kdevname(sb->s_dev)));
+
+       if (MAJOR(sb->s_dev) != MTD_BLOCK_MAJOR) {
+               if (!silent)
+                       printk(KERN_DEBUG "jffs2: attempt to mount non-MTD device %s\n", kdevname(sb->s_dev));
+               return NULL;
+       }
+
+       c = JFFS2_SB_INFO(sb);
+       memset(c, 0, sizeof(*c));
+       
+       c->mtd = get_mtd_device(NULL, MINOR(sb->s_dev));
+       if (!c->mtd) {
+               D1(printk(KERN_DEBUG "jffs2: MTD device #%u doesn't appear to exist\n", MINOR(sb->s_dev)));
+               return NULL;
+       }
+       c->sector_size = c->mtd->erasesize;
+       c->free_size = c->flash_size = c->mtd->size;
+       c->nr_blocks = c->mtd->size / c->mtd->erasesize;
+       c->blocks = kmalloc(sizeof(struct jffs2_eraseblock) * c->nr_blocks, GFP_KERNEL);
+       if (!c->blocks)
+               goto out_mtd;
+       for (i=0; i<c->nr_blocks; i++) {
+               INIT_LIST_HEAD(&c->blocks[i].list);
+               c->blocks[i].offset = i * c->sector_size;
+               c->blocks[i].free_size = c->sector_size;
+               c->blocks[i].dirty_size = 0;
+               c->blocks[i].used_size = 0;
+               c->blocks[i].first_node = NULL;
+               c->blocks[i].last_node = NULL;
+       }
+               
+       spin_lock_init(&c->nodelist_lock);
+       init_MUTEX(&c->alloc_sem);
+       init_waitqueue_head(&c->erase_wait);
+       spin_lock_init(&c->erase_completion_lock);
+       spin_lock_init(&c->inocache_lock);
+
+       INIT_LIST_HEAD(&c->clean_list);
+       INIT_LIST_HEAD(&c->dirty_list);
+       INIT_LIST_HEAD(&c->erasing_list);
+       INIT_LIST_HEAD(&c->erase_pending_list);
+       INIT_LIST_HEAD(&c->erase_complete_list);
+       INIT_LIST_HEAD(&c->free_list);
+       INIT_LIST_HEAD(&c->bad_list);
+       INIT_LIST_HEAD(&c->bad_used_list);
+       c->highest_ino = 1;
+
+       if (jffs2_build_filesystem(c)) {
+               D1(printk(KERN_DEBUG "build_fs failed\n"));
+               goto out_nodes;
+       }
+       sb->s_op = &jffs2_super_operations;
+
+       D1(printk(KERN_DEBUG "jffs2_read_super(): Getting root inode\n"));
+       root_i = iget(sb, 1);
+       if (is_bad_inode(root_i)) {
+               D1(printk(KERN_WARNING "get root inode failed\n"));
+               goto out_nodes;
+       }
+
+       D1(printk(KERN_DEBUG "jffs2_read_super(): d_alloc_root()\n"));
+       sb->s_root = d_alloc_root(root_i);
+       if (!sb->s_root)
+               goto out_root_i;
+
+#if LINUX_VERSION_CODE >= 0x20403
+       sb->s_maxbytes = 0xFFFFFFFF;
+#endif
+       sb->s_blocksize = PAGE_CACHE_SIZE;
+       sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
+       sb->s_magic = JFFS2_SUPER_MAGIC;
+       jffs2_start_garbage_collect_thread(c);
+       return sb;
+
+ out_root_i:
+       iput(root_i);
+ out_nodes:
+       jffs2_free_ino_caches(c);
+       jffs2_free_raw_node_refs(c);
+       kfree(c->blocks);
+ out_mtd:
+       put_mtd_device(c->mtd);
+       return NULL;
+}
+
+void jffs2_put_super (struct super_block *sb)
+{
+       struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
+
+       D2(printk(KERN_DEBUG "jffs2: jffs2_put_super()\n"));
+
+       jffs2_stop_garbage_collect_thread(c);
+       jffs2_free_ino_caches(c);
+       jffs2_free_raw_node_refs(c);
+       kfree(c->blocks);
+       put_mtd_device(c->mtd);
+       
+       D1(printk(KERN_DEBUG "jffs2_put_super returning\n"));
+}
+
+void jffs2_write_super (struct super_block *sb)
+{
+       struct jffs2_sb_info *c = JFFS2_SB_INFO(sb);
+       sb->s_dirt = 0;
+       jffs2_garbage_collect_trigger(c);
+       jffs2_erase_pending_blocks(c);
+       jffs2_mark_erased_blocks(c);
+}
+
+
+static DECLARE_FSTYPE_DEV(jffs2_fs_type, "jffs2", jffs2_read_super);
+
+static int __init init_jffs2_fs(void)
+{
+       int ret;
+
+       printk(KERN_NOTICE "JFFS2 version 2.1. (C) 2001 Red Hat, Inc., designed by Axis Communications AB.\n");
+
+#ifdef JFFS2_OUT_OF_KERNEL
+       /* sanity checks. Could we do these at compile time? */
+       if (sizeof(struct jffs2_sb_info) > sizeof (((struct super_block *)NULL)->u)) {
+               printk(KERN_ERR "JFFS2 error: struct jffs2_sb_info (%d bytes) doesn't fit in the super_block union (%d bytes)\n", 
+                      sizeof(struct jffs2_sb_info), sizeof (((struct super_block *)NULL)->u));
+               return -EIO;
+       }
+
+       if (sizeof(struct jffs2_inode_info) > sizeof (((struct inode *)NULL)->u)) {
+               printk(KERN_ERR "JFFS2 error: struct jffs2_inode_info (%d bytes) doesn't fit in the inode union (%d bytes)\n", 
+                      sizeof(struct jffs2_inode_info), sizeof (((struct inode *)NULL)->u));
+               return -EIO;
+       }
+#endif
+
+       ret = jffs2_create_slab_caches();
+       if (ret) {
+               printk(KERN_ERR "JFFS2 error: Failed to initialise slab caches\n");
+               return ret;
+       }
+       ret = register_filesystem(&jffs2_fs_type);
+       if (ret) {
+               printk(KERN_ERR "JFFS2 error: Failed to register filesystem\n");
+               jffs2_destroy_slab_caches();
+       }
+       return ret;
+}
+
+static void __exit exit_jffs2_fs(void)
+{
+       jffs2_destroy_slab_caches();
+       unregister_filesystem(&jffs2_fs_type);
+}
+
+module_init(init_jffs2_fs);
+module_exit(exit_jffs2_fs);
diff --git a/fs/jffs2/symlink.c b/fs/jffs2/symlink.c
new file mode 100644 (file)
index 0000000..c6746ed
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: symlink.c,v 1.5 2001/03/15 15:38:24 dwmw2 Exp $
+ *
+ */
+
+
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/fs.h>
+#include <linux/jffs2.h>
+#include "nodelist.h"
+
+int jffs2_readlink(struct dentry *dentry, char *buffer, int buflen);
+int jffs2_follow_link(struct dentry *dentry, struct nameidata *nd);
+
+struct inode_operations jffs2_symlink_inode_operations =
+{      
+       readlink:       jffs2_readlink,
+       follow_link:    jffs2_follow_link,
+       setattr:        jffs2_setattr
+};
+
+static char *jffs2_getlink(struct dentry *dentry)
+{
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
+       char *buf;
+       int ret;
+
+       if (!f->metadata) {
+               printk(KERN_NOTICE "No metadata for symlink inode #%lu\n", dentry->d_inode->i_ino);
+               return ERR_PTR(-EINVAL);
+       }
+       buf = kmalloc(f->metadata->size+1, GFP_USER);
+       if (!buf)
+               return ERR_PTR(-ENOMEM);
+       buf[f->metadata->size]=0;
+
+       ret = jffs2_read_dnode(JFFS2_SB_INFO(dentry->d_inode->i_sb), f->metadata, buf, 0, f->metadata->size);
+       if (ret) {
+               kfree(buf);
+               return ERR_PTR(ret);
+       }
+       return buf;
+
+}
+int jffs2_readlink(struct dentry *dentry, char *buffer, int buflen)
+{
+       unsigned char *kbuf;
+       int ret;
+
+       kbuf = jffs2_getlink(dentry);
+       if (IS_ERR(kbuf))
+               return PTR_ERR(kbuf);
+
+       ret = vfs_readlink(dentry, buffer, buflen, kbuf);
+       kfree(kbuf);
+       return ret;
+}
+
+int jffs2_follow_link(struct dentry *dentry, struct nameidata *nd)
+{
+       unsigned char *buf;
+       int ret;
+
+       buf = jffs2_getlink(dentry);
+
+       if (IS_ERR(buf))
+               return PTR_ERR(buf);
+
+       ret = vfs_follow_link(nd, buf);
+       kfree(buf);
+       return ret;
+}
diff --git a/fs/jffs2/write.c b/fs/jffs2/write.c
new file mode 100644 (file)
index 0000000..0e8e401
--- /dev/null
@@ -0,0 +1,322 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: write.c,v 1.27 2001/04/11 15:29:34 dwmw2 Exp $
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/fs.h>
+#include <linux/jffs2.h>
+#include <linux/mtd/mtd.h>
+#include "nodelist.h"
+#include "crc32.h"
+
+/* jffs2_new_inode: allocate a new inode and inocache, add it to the hash,
+   fill in the raw_inode while you're at it. */
+struct inode *jffs2_new_inode (struct inode *dir_i, int mode, struct jffs2_raw_inode *ri)
+{
+       struct inode *inode;
+       struct super_block *sb = dir_i->i_sb;
+       struct jffs2_inode_cache *ic;
+       struct jffs2_sb_info *c;
+       struct jffs2_inode_info *f;
+
+       D1(printk(KERN_DEBUG "jffs2_new_inode(): dir_i %ld, mode 0x%x\n", dir_i->i_ino, mode));
+
+       c = JFFS2_SB_INFO(sb);
+       memset(ri, 0, sizeof(*ri));
+
+       ic = jffs2_alloc_inode_cache();
+       if (!ic) {
+               return ERR_PTR(-ENOMEM);
+       }
+       memset(ic, 0, sizeof(*ic));
+       
+       inode = new_inode(sb);
+       
+       if (!inode) {
+               jffs2_free_inode_cache(ic);
+               return ERR_PTR(-ENOMEM);
+       }
+
+       /* Alloc jffs2_inode_info when that's split in 2.5 */
+
+       f = JFFS2_INODE_INFO(inode);
+       memset(f, 0, sizeof(*f));
+       init_MUTEX_LOCKED(&f->sem);
+       f->inocache = ic;
+       inode->i_nlink = f->inocache->nlink = 1;
+       f->inocache->nodes = (struct jffs2_raw_node_ref *)f->inocache;
+       f->inocache->ino = ri->ino = inode->i_ino = ++c->highest_ino;
+       D1(printk(KERN_DEBUG "jffs2_new_inode(): Assigned ino# %d\n", ri->ino));
+       jffs2_add_ino_cache(c, f->inocache);
+
+       ri->magic = JFFS2_MAGIC_BITMASK;
+       ri->nodetype = JFFS2_NODETYPE_INODE;
+       ri->totlen = PAD(sizeof(*ri));
+       ri->hdr_crc = crc32(0, ri, sizeof(struct jffs2_unknown_node)-4);
+       ri->mode = mode;
+       f->highest_version = ri->version = 1;
+       ri->uid = current->fsuid;
+       if (dir_i->i_mode & S_ISGID) {
+               ri->gid = dir_i->i_gid;
+               if (S_ISDIR(mode))
+                       ri->mode |= S_ISGID;
+       } else {
+               ri->gid = current->fsgid;
+       }
+       inode->i_mode = ri->mode;
+       inode->i_gid = ri->gid;
+       inode->i_uid = ri->uid;
+       inode->i_atime = inode->i_ctime = inode->i_mtime = 
+               ri->atime = ri->mtime = ri->ctime = CURRENT_TIME;
+       inode->i_blksize = PAGE_SIZE;
+       inode->i_blocks = 0;
+       inode->i_size = 0;
+
+       insert_inode_hash(inode);
+
+       return inode;
+}
+
+/* This ought to be in core MTD code. All registered MTD devices without writev should have
+   this put in place. Bug the MTD maintainer */
+static int mtd_fake_writev(struct mtd_info *mtd, const struct iovec *vecs, unsigned long count, loff_t to, size_t *retlen)
+{
+       unsigned long i;
+       size_t totlen = 0, thislen;
+       int ret = 0;
+
+       for (i=0; i<count; i++) {
+               mtd->write(mtd, to, vecs[i].iov_len, &thislen, vecs[i].iov_base);
+               totlen += thislen;
+               if (ret || thislen != vecs[i].iov_len)
+                       break;
+               to += vecs[i].iov_len;
+       }
+       if (retlen)
+               *retlen = totlen;
+       return ret;
+}
+
+
+static inline int mtd_writev(struct mtd_info *mtd, const struct iovec *vecs, unsigned long count, loff_t to, size_t *retlen)
+{
+       if (mtd->writev)
+               return mtd->writev(mtd,vecs,count,to,retlen);
+       else
+               return mtd_fake_writev(mtd, vecs, count, to, retlen);
+}
+
+static void writecheck(struct mtd_info *mtd, __u32 ofs)
+{
+       unsigned char buf[16];
+       ssize_t retlen;
+       int ret, i;
+
+       ret = mtd->read(mtd, ofs, 16, &retlen, buf);
+       if (ret && retlen != 16) {
+               D1(printk(KERN_DEBUG "read failed or short in writecheck(). ret %d, retlen %d\n", ret, retlen));
+               return;
+       }
+       ret = 0;
+       for (i=0; i<16; i++) {
+               if (buf[i] != 0xff)
+                       ret = 1;
+       }
+       if (ret) {
+               printk(KERN_WARNING "ARGH. About to write node to 0x%08x on flash, but there's data already there:\n", ofs);
+               printk(KERN_WARNING "0x%08x: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", 
+                      ofs,
+                      buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7],
+                      buf[8], buf[9], buf[10], buf[11], buf[12], buf[13], buf[14], buf[15]);
+       }
+}
+
+       
+       
+
+/* jffs2_write_dnode - given a raw_inode, allocate a full_dnode for it, 
+   write it to the flash, link it into the existing inode/fragment list */
+
+struct jffs2_full_dnode *jffs2_write_dnode(struct inode *inode, struct jffs2_raw_inode *ri, const unsigned char *data, __u32 datalen, __u32 flash_ofs,  __u32 *writelen)
+
+{
+       struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
+       struct jffs2_raw_node_ref *raw;
+       struct jffs2_full_dnode *fn;
+       ssize_t retlen;
+       struct iovec vecs[2];
+       int ret;
+
+       D1(if(ri->hdr_crc != crc32(0, ri, sizeof(struct jffs2_unknown_node)-4)) {
+               printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dnode()\n");
+               BUG();
+       }
+          );
+       vecs[0].iov_base = ri;
+       vecs[0].iov_len = sizeof(*ri);
+       vecs[1].iov_base = (unsigned char *)data;
+       vecs[1].iov_len = datalen;
+
+       writecheck(c->mtd, flash_ofs);
+
+       if (ri->totlen != sizeof(*ri) + datalen) {
+               printk(KERN_WARNING "jffs2_write_dnode: ri->totlen (0x%08x) != sizeof(*ri) (0x%08x) + datalen (0x%08x)\n", ri->totlen, sizeof(*ri), datalen);
+       }
+       raw = jffs2_alloc_raw_node_ref();
+       if (!raw)
+               return ERR_PTR(-ENOMEM);
+       
+       fn = jffs2_alloc_full_dnode();
+       if (!fn) {
+               jffs2_free_raw_node_ref(raw);
+               return ERR_PTR(-ENOMEM);
+       }
+       raw->flash_offset = flash_ofs;
+       raw->totlen = PAD(ri->totlen);
+       raw->next_in_ino = f->inocache->nodes;
+       f->inocache->nodes = raw;
+       raw->next_phys = NULL;
+
+       fn->ofs = ri->offset;
+       fn->size = ri->dsize;
+       fn->frags = 0;
+       fn->raw = raw;
+
+       ret = mtd_writev(c->mtd, vecs, 2, flash_ofs, &retlen);
+       if (ret || (retlen != sizeof(*ri) + datalen)) {
+               printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %d\n", 
+                      sizeof(*ri)+datalen, flash_ofs, ret, retlen);
+               /* Mark the space as dirtied */
+               if (retlen) {
+                       jffs2_add_physical_node_ref(c, raw, sizeof(*ri)+datalen, 1);
+                       jffs2_mark_node_obsolete(c, raw);
+               } else {
+                       printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
+                       jffs2_free_raw_node_ref(raw);
+               }
+
+               /* Release the full_dnode which is now useless, and return */
+               jffs2_free_full_dnode(fn);
+               if (writelen)
+                       *writelen = retlen;
+               return ERR_PTR(ret?ret:-EIO);
+       }
+       /* Mark the space used */
+       jffs2_add_physical_node_ref(c, raw, retlen, 0);
+       D1(printk(KERN_DEBUG "jffs2_write_dnode wrote node at 0x%08x with dsize 0x%x, csize 0x%x, node_crc 0x%08x, data_crc 0x%08x, totlen 0x%08x\n", flash_ofs, ri->dsize, ri->csize, ri->node_crc, ri->data_crc, ri->totlen));
+       if (writelen)
+               *writelen = retlen;
+
+       f->inocache->nodes = raw;
+       return fn;
+}
+
+struct jffs2_full_dirent *jffs2_write_dirent(struct inode *inode, struct jffs2_raw_dirent *rd, const unsigned char *name, __u32 namelen, __u32 flash_ofs,  __u32 *writelen)
+{
+       struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
+       struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
+       struct jffs2_raw_node_ref *raw;
+       struct jffs2_full_dirent *fd;
+       ssize_t retlen;
+       struct iovec vecs[2];
+       int ret;
+
+       D1(printk(KERN_DEBUG "jffs2_write_dirent(ino #%u, name \"%s\"->ino #%u\n", rd->pino, name, rd->ino));
+       writecheck(c->mtd, flash_ofs);
+
+       D1(if(rd->hdr_crc != crc32(0, rd, sizeof(struct jffs2_unknown_node)-4)) {
+               printk(KERN_CRIT "Eep. CRC not correct in jffs2_write_dirent()\n");
+               BUG();
+       }
+          );
+
+       vecs[0].iov_base = rd;
+       vecs[0].iov_len = sizeof(*rd);
+       vecs[1].iov_base = (unsigned char *)name;
+       vecs[1].iov_len = namelen;
+       
+       raw = jffs2_alloc_raw_node_ref();
+
+       if (!raw)
+               return ERR_PTR(-ENOMEM);
+
+       fd = jffs2_alloc_full_dirent(namelen+1);
+       if (!fd) {
+               jffs2_free_raw_node_ref(raw);
+               return ERR_PTR(-ENOMEM);
+       }
+       raw->flash_offset = flash_ofs;
+       raw->totlen = PAD(rd->totlen);
+       raw->next_in_ino = f->inocache->nodes;
+       f->inocache->nodes = raw;
+       raw->next_phys = NULL;
+
+       fd->version = rd->version;
+       fd->ino = rd->ino;
+       fd->nhash = full_name_hash(name, strlen(name));
+       fd->type = rd->type;
+       memcpy(fd->name, name, namelen);
+       fd->name[namelen]=0;
+       fd->raw = raw;
+
+       ret = mtd_writev(c->mtd, vecs, 2, flash_ofs, &retlen);
+               if (ret || (retlen != sizeof(*rd) + namelen)) {
+                       printk(KERN_NOTICE "Write of %d bytes at 0x%08x failed. returned %d, retlen %d\n", 
+                              sizeof(*rd)+namelen, flash_ofs, ret, retlen);
+               /* Mark the space as dirtied */
+                       if (retlen) {
+                               jffs2_add_physical_node_ref(c, raw, sizeof(*rd)+namelen, 1);
+                               jffs2_mark_node_obsolete(c, raw);
+                       } else {
+                               printk(KERN_NOTICE "Not marking the space at 0x%08x as dirty because the flash driver returned retlen zero\n", raw->flash_offset);
+                               jffs2_free_raw_node_ref(raw);
+                       }
+
+               /* Release the full_dnode which is now useless, and return */
+               jffs2_free_full_dirent(fd);
+               if (writelen)
+                       *writelen = retlen;
+               return ERR_PTR(ret?ret:-EIO);
+       }
+       /* Mark the space used */
+       jffs2_add_physical_node_ref(c, raw, retlen, 0);
+       if (writelen)
+               *writelen = retlen;
+
+       f->inocache->nodes = raw;
+       return fd;
+}
diff --git a/fs/jffs2/zlib.c b/fs/jffs2/zlib.c
new file mode 100644 (file)
index 0000000..6595b3e
--- /dev/null
@@ -0,0 +1,5371 @@
+/*
+ * This file is derived from various .h and .c files from the zlib-1.0.4
+ * distribution by Jean-loup Gailly and Mark Adler, with some additions
+ * by Paul Mackerras to aid in implementing Deflate compression and
+ * decompression for PPP packets.  See zlib.h for conditions of
+ * distribution and use.
+ *
+ * Changes that have been made include:
+ * - added Z_PACKET_FLUSH (see zlib.h for details)
+ * - added inflateIncomp and deflateOutputPending
+ * - allow strm->next_out to be NULL, meaning discard the output
+ *
+ * $Id: zlib.c,v 1.3 1997/12/23 10:47:42 paulus Exp $
+ */
+
+/* 
+ *  ==FILEVERSION 971210==
+ *
+ * This marker is used by the Linux installation script to determine
+ * whether an up-to-date version of this file is already installed.
+ */
+
+#define NO_DUMMY_DECL
+#define NO_ZCFUNCS
+#define MY_ZCALLOC
+
+#if defined(__FreeBSD__) && (defined(KERNEL) || defined(_KERNEL))
+#define inflate        inflate_ppp     /* FreeBSD already has an inflate :-( */
+#endif
+
+
+/* +++ zutil.h */
+/* zutil.h -- internal interface and configuration of the compression library
+ * Copyright (C) 1995-1996 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+   part of the implementation of the compression library and is
+   subject to change. Applications should only use zlib.h.
+ */
+
+/* From: zutil.h,v 1.16 1996/07/24 13:41:13 me Exp $ */
+
+#ifndef _Z_UTIL_H
+#define _Z_UTIL_H
+
+#include "zlib.h"
+
+#if defined(KERNEL) || defined(_KERNEL)
+/* Assume this is a *BSD or SVR4 kernel */
+#include <sys/types.h>
+#include <sys/time.h>
+#include <sys/systm.h>
+#  define HAVE_MEMCPY
+#  define memcpy(d, s, n)      bcopy((s), (d), (n))
+#  define memset(d, v, n)      bzero((d), (n))
+#  define memcmp               bcmp
+
+#else
+#if defined(__KERNEL__)
+/* Assume this is a Linux kernel */
+#include <linux/string.h>
+#define HAVE_MEMCPY
+
+#else /* not kernel */
+
+#if defined(MSDOS)||defined(VMS)||defined(CRAY)||defined(WIN32)||defined(RISCOS)
+#   include <stddef.h>
+#   include <errno.h>
+#else
+    extern int errno;
+#endif
+#ifdef STDC
+#  include <string.h>
+#  include <stdlib.h>
+#endif
+#endif /* __KERNEL__ */
+#endif /* _KERNEL || KERNEL */
+
+#ifndef local
+#  define local static
+#endif
+/* compile with -Dlocal if your debugger can't find static symbols */
+
+typedef unsigned char  uch;
+typedef uch FAR uchf;
+typedef unsigned short ush;
+typedef ush FAR ushf;
+typedef unsigned long  ulg;
+
+extern const char *z_errmsg[10]; /* indexed by 2-zlib_error */
+/* (size given to avoid silly warnings with Visual C++) */
+
+#define ERR_MSG(err) z_errmsg[Z_NEED_DICT-(err)]
+
+#define ERR_RETURN(strm,err) \
+  return (strm->msg = (char*)ERR_MSG(err), (err))
+/* To be used only when the state is known to be valid */
+
+        /* common constants */
+
+#ifndef DEF_WBITS
+#  define DEF_WBITS MAX_WBITS
+#endif
+/* default windowBits for decompression. MAX_WBITS is for compression only */
+
+#if MAX_MEM_LEVEL >= 8
+#  define DEF_MEM_LEVEL 8
+#else
+#  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
+#endif
+/* default memLevel */
+
+#define STORED_BLOCK 0
+#define STATIC_TREES 1
+#define DYN_TREES    2
+/* The three kinds of block type */
+
+#define MIN_MATCH  3
+#define MAX_MATCH  258
+/* The minimum and maximum match lengths */
+
+#define PRESET_DICT 0x20 /* preset dictionary flag in zlib header */
+
+        /* target dependencies */
+
+#ifdef MSDOS
+#  define OS_CODE  0x00
+#  ifdef __TURBOC__
+#    include <alloc.h>
+#  else /* MSC or DJGPP */
+#    include <malloc.h>
+#  endif
+#endif
+
+#ifdef OS2
+#  define OS_CODE  0x06
+#endif
+
+#ifdef WIN32 /* Window 95 & Windows NT */
+#  define OS_CODE  0x0b
+#endif
+
+#if defined(VAXC) || defined(VMS)
+#  define OS_CODE  0x02
+#  define FOPEN(name, mode) \
+     fopen((name), (mode), "mbc=60", "ctx=stm", "rfm=fix", "mrs=512")
+#endif
+
+#ifdef AMIGA
+#  define OS_CODE  0x01
+#endif
+
+#if defined(ATARI) || defined(atarist)
+#  define OS_CODE  0x05
+#endif
+
+#ifdef MACOS
+#  define OS_CODE  0x07
+#endif
+
+#ifdef __50SERIES /* Prime/PRIMOS */
+#  define OS_CODE  0x0F
+#endif
+
+#ifdef TOPS20
+#  define OS_CODE  0x0a
+#endif
+
+#if defined(_BEOS_) || defined(RISCOS)
+#  define fdopen(fd,mode) NULL /* No fdopen() */
+#endif
+
+        /* Common defaults */
+
+#ifndef OS_CODE
+#  define OS_CODE  0x03  /* assume Unix */
+#endif
+
+#ifndef FOPEN
+#  define FOPEN(name, mode) fopen((name), (mode))
+#endif
+
+         /* functions */
+
+#ifdef HAVE_STRERROR
+   extern char *strerror OF((int));
+#  define zstrerror(errnum) strerror(errnum)
+#else
+#  define zstrerror(errnum) ""
+#endif
+
+#if defined(pyr)
+#  define NO_MEMCPY
+#endif
+#if (defined(M_I86SM) || defined(M_I86MM)) && !defined(_MSC_VER)
+ /* Use our own functions for small and medium model with MSC <= 5.0.
+  * You may have to use the same strategy for Borland C (untested).
+  */
+#  define NO_MEMCPY
+#endif
+#if defined(STDC) && !defined(HAVE_MEMCPY) && !defined(NO_MEMCPY)
+#  define HAVE_MEMCPY
+#endif
+#ifdef HAVE_MEMCPY
+#  ifdef SMALL_MEDIUM /* MSDOS small or medium model */
+#    define zmemcpy _fmemcpy
+#    define zmemcmp _fmemcmp
+#    define zmemzero(dest, len) _fmemset(dest, 0, len)
+#  else
+#    define zmemcpy memcpy
+#    define zmemcmp memcmp
+#    define zmemzero(dest, len) memset(dest, 0, len)
+#  endif
+#else
+   extern void zmemcpy  OF((Bytef* dest, Bytef* source, uInt len));
+   extern int  zmemcmp  OF((Bytef* s1,   Bytef* s2, uInt len));
+   extern void zmemzero OF((Bytef* dest, uInt len));
+#endif
+
+/* Diagnostic functions */
+#ifdef DEBUG_ZLIB
+#  include <stdio.h>
+#  ifndef verbose
+#    define verbose 0
+#  endif
+   extern void z_error    OF((char *m));
+#  define Assert(cond,msg) {if(!(cond)) z_error(msg);}
+#  define Trace(x) fprintf x
+#  define Tracev(x) {if (verbose) fprintf x ;}
+#  define Tracevv(x) {if (verbose>1) fprintf x ;}
+#  define Tracec(c,x) {if (verbose && (c)) fprintf x ;}
+#  define Tracecv(c,x) {if (verbose>1 && (c)) fprintf x ;}
+#else
+#  define Assert(cond,msg)
+#  define Trace(x)
+#  define Tracev(x)
+#  define Tracevv(x)
+#  define Tracec(c,x)
+#  define Tracecv(c,x)
+#endif
+
+
+typedef uLong (*check_func) OF((uLong check, const Bytef *buf, uInt len));
+
+voidpf zcalloc OF((voidpf opaque, unsigned items, unsigned size));
+void   zcfree  OF((voidpf opaque, voidpf ptr));
+
+#define ZALLOC(strm, items, size) \
+           (*((strm)->zalloc))((strm)->opaque, (items), (size))
+#define ZFREE(strm, addr)  (*((strm)->zfree))((strm)->opaque, (voidpf)(addr))
+#define TRY_FREE(s, p) {if (p) ZFREE(s, p);}
+
+#endif /* _Z_UTIL_H */
+/* --- zutil.h */
+
+/* +++ deflate.h */
+/* deflate.h -- internal compression state
+ * Copyright (C) 1995-1996 Jean-loup Gailly
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+   part of the implementation of the compression library and is
+   subject to change. Applications should only use zlib.h.
+ */
+
+/* From: deflate.h,v 1.10 1996/07/02 12:41:00 me Exp $ */
+
+#ifndef _DEFLATE_H
+#define _DEFLATE_H
+
+/* #include "zutil.h" */
+
+/* ===========================================================================
+ * Internal compression state.
+ */
+
+#define LENGTH_CODES 29
+/* number of length codes, not counting the special END_BLOCK code */
+
+#define LITERALS  256
+/* number of literal bytes 0..255 */
+
+#define L_CODES (LITERALS+1+LENGTH_CODES)
+/* number of Literal or Length codes, including the END_BLOCK code */
+
+#define D_CODES   30
+/* number of distance codes */
+
+#define BL_CODES  19
+/* number of codes used to transfer the bit lengths */
+
+#define HEAP_SIZE (2*L_CODES+1)
+/* maximum heap size */
+
+#define MAX_BITS 15
+/* All codes must not exceed MAX_BITS bits */
+
+#define INIT_STATE    42
+#define BUSY_STATE   113
+#define FINISH_STATE 666
+/* Stream status */
+
+
+/* Data structure describing a single value and its code string. */
+typedef struct ct_data_s {
+    union {
+        ush  freq;       /* frequency count */
+        ush  code;       /* bit string */
+    } fc;
+    union {
+        ush  dad;        /* father node in Huffman tree */
+        ush  len;        /* length of bit string */
+    } dl;
+} FAR ct_data;
+
+#define Freq fc.freq
+#define Code fc.code
+#define Dad  dl.dad
+#define Len  dl.len
+
+typedef struct static_tree_desc_s  static_tree_desc;
+
+typedef struct tree_desc_s {
+    ct_data *dyn_tree;           /* the dynamic tree */
+    int     max_code;            /* largest code with non zero frequency */
+    static_tree_desc *stat_desc; /* the corresponding static tree */
+} FAR tree_desc;
+
+typedef ush Pos;
+typedef Pos FAR Posf;
+typedef unsigned IPos;
+
+/* A Pos is an index in the character window. We use short instead of int to
+ * save space in the various tables. IPos is used only for parameter passing.
+ */
+
+typedef struct deflate_state {
+    z_streamp strm;      /* pointer back to this zlib stream */
+    int   status;        /* as the name implies */
+    Bytef *pending_buf;  /* output still pending */
+    ulg   pending_buf_size; /* size of pending_buf */
+    Bytef *pending_out;  /* next pending byte to output to the stream */
+    int   pending;       /* nb of bytes in the pending buffer */
+    int   noheader;      /* suppress zlib header and adler32 */
+    Byte  data_type;     /* UNKNOWN, BINARY or ASCII */
+    Byte  method;        /* STORED (for zip only) or DEFLATED */
+    int   last_flush;    /* value of flush param for previous deflate call */
+
+                /* used by deflate.c: */
+
+    uInt  w_size;        /* LZ77 window size (32K by default) */
+    uInt  w_bits;        /* log2(w_size)  (8..16) */
+    uInt  w_mask;        /* w_size - 1 */
+
+    Bytef *window;
+    /* Sliding window. Input bytes are read into the second half of the window,
+     * and move to the first half later to keep a dictionary of at least wSize
+     * bytes. With this organization, matches are limited to a distance of
+     * wSize-MAX_MATCH bytes, but this ensures that IO is always
+     * performed with a length multiple of the block size. Also, it limits
+     * the window size to 64K, which is quite useful on MSDOS.
+     * To do: use the user input buffer as sliding window.
+     */
+
+    ulg window_size;
+    /* Actual size of window: 2*wSize, except when the user input buffer
+     * is directly used as sliding window.
+     */
+
+    Posf *prev;
+    /* Link to older string with same hash index. To limit the size of this
+     * array to 64K, this link is maintained only for the last 32K strings.
+     * An index in this array is thus a window index modulo 32K.
+     */
+
+    Posf *head; /* Heads of the hash chains or NIL. */
+
+    uInt  ins_h;          /* hash index of string to be inserted */
+    uInt  hash_size;      /* number of elements in hash table */
+    uInt  hash_bits;      /* log2(hash_size) */
+    uInt  hash_mask;      /* hash_size-1 */
+
+    uInt  hash_shift;
+    /* Number of bits by which ins_h must be shifted at each input
+     * step. It must be such that after MIN_MATCH steps, the oldest
+     * byte no longer takes part in the hash key, that is:
+     *   hash_shift * MIN_MATCH >= hash_bits
+     */
+
+    long block_start;
+    /* Window position at the beginning of the current output block. Gets
+     * negative when the window is moved backwards.
+     */
+
+    uInt match_length;           /* length of best match */
+    IPos prev_match;             /* previous match */
+    int match_available;         /* set if previous match exists */
+    uInt strstart;               /* start of string to insert */
+    uInt match_start;            /* start of matching string */
+    uInt lookahead;              /* number of valid bytes ahead in window */
+
+    uInt prev_length;
+    /* Length of the best match at previous step. Matches not greater than this
+     * are discarded. This is used in the lazy match evaluation.
+     */
+
+    uInt max_chain_length;
+    /* To speed up deflation, hash chains are never searched beyond this
+     * length.  A higher limit improves compression ratio but degrades the
+     * speed.
+     */
+
+    uInt max_lazy_match;
+    /* Attempt to find a better match only when the current match is strictly
+     * smaller than this value. This mechanism is used only for compression
+     * levels >= 4.
+     */
+#   define max_insert_length  max_lazy_match
+    /* Insert new strings in the hash table only if the match length is not
+     * greater than this length. This saves time but degrades compression.
+     * max_insert_length is used only for compression levels <= 3.
+     */
+
+    int level;    /* compression level (1..9) */
+    int strategy; /* favor or force Huffman coding*/
+
+    uInt good_match;
+    /* Use a faster search when the previous match is longer than this */
+
+    int nice_match; /* Stop searching when current match exceeds this */
+
+                /* used by trees.c: */
+    /* Didn't use ct_data typedef below to supress compiler warning */
+    struct ct_data_s dyn_ltree[HEAP_SIZE];   /* literal and length tree */
+    struct ct_data_s dyn_dtree[2*D_CODES+1]; /* distance tree */
+    struct ct_data_s bl_tree[2*BL_CODES+1];  /* Huffman tree for bit lengths */
+
+    struct tree_desc_s l_desc;               /* desc. for literal tree */
+    struct tree_desc_s d_desc;               /* desc. for distance tree */
+    struct tree_desc_s bl_desc;              /* desc. for bit length tree */
+
+    ush bl_count[MAX_BITS+1];
+    /* number of codes at each bit length for an optimal tree */
+
+    int heap[2*L_CODES+1];      /* heap used to build the Huffman trees */
+    int heap_len;               /* number of elements in the heap */
+    int heap_max;               /* element of largest frequency */
+    /* The sons of heap[n] are heap[2*n] and heap[2*n+1]. heap[0] is not used.
+     * The same heap array is used to build all trees.
+     */
+
+    uch depth[2*L_CODES+1];
+    /* Depth of each subtree used as tie breaker for trees of equal frequency
+     */
+
+    uchf *l_buf;          /* buffer for literals or lengths */
+
+    uInt  lit_bufsize;
+    /* Size of match buffer for literals/lengths.  There are 4 reasons for
+     * limiting lit_bufsize to 64K:
+     *   - frequencies can be kept in 16 bit counters
+     *   - if compression is not successful for the first block, all input
+     *     data is still in the window so we can still emit a stored block even
+     *     when input comes from standard input.  (This can also be done for
+     *     all blocks if lit_bufsize is not greater than 32K.)
+     *   - if compression is not successful for a file smaller than 64K, we can
+     *     even emit a stored file instead of a stored block (saving 5 bytes).
+     *     This is applicable only for zip (not gzip or zlib).
+     *   - creating new Huffman trees less frequently may not provide fast
+     *     adaptation to changes in the input data statistics. (Take for
+     *     example a binary file with poorly compressible code followed by
+     *     a highly compressible string table.) Smaller buffer sizes give
+     *     fast adaptation but have of course the overhead of transmitting
+     *     trees more frequently.
+     *   - I can't count above 4
+     */
+
+    uInt last_lit;      /* running index in l_buf */
+
+    ushf *d_buf;
+    /* Buffer for distances. To simplify the code, d_buf and l_buf have
+     * the same number of elements. To use different lengths, an extra flag
+     * array would be necessary.
+     */
+
+    ulg opt_len;        /* bit length of current block with optimal trees */
+    ulg static_len;     /* bit length of current block with static trees */
+    ulg compressed_len; /* total bit length of compressed file */
+    uInt matches;       /* number of string matches in current block */
+    int last_eob_len;   /* bit length of EOB code for last block */
+
+#ifdef DEBUG_ZLIB
+    ulg bits_sent;      /* bit length of the compressed data */
+#endif
+
+    ush bi_buf;
+    /* Output buffer. bits are inserted starting at the bottom (least
+     * significant bits).
+     */
+    int bi_valid;
+    /* Number of valid bits in bi_buf.  All bits above the last valid bit
+     * are always zero.
+     */
+
+} FAR deflate_state;
+
+/* Output a byte on the stream.
+ * IN assertion: there is enough room in pending_buf.
+ */
+#define put_byte(s, c) {s->pending_buf[s->pending++] = (c);}
+
+
+#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
+/* Minimum amount of lookahead, except at the end of the input file.
+ * See deflate.c for comments about the MIN_MATCH+1.
+ */
+
+#define MAX_DIST(s)  ((s)->w_size-MIN_LOOKAHEAD)
+/* In order to simplify the code, particularly on 16 bit machines, match
+ * distances are limited to MAX_DIST instead of WSIZE.
+ */
+
+        /* in trees.c */
+void _tr_init         OF((deflate_state *s));
+int  _tr_tally        OF((deflate_state *s, unsigned dist, unsigned lc));
+ulg  _tr_flush_block  OF((deflate_state *s, charf *buf, ulg stored_len,
+                         int eof));
+void _tr_align        OF((deflate_state *s));
+void _tr_stored_block OF((deflate_state *s, charf *buf, ulg stored_len,
+                          int eof));
+void _tr_stored_type_only OF((deflate_state *));
+
+#endif
+/* --- deflate.h */
+
+/* +++ deflate.c */
+/* deflate.c -- compress data using the deflation algorithm
+ * Copyright (C) 1995-1996 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/*
+ *  ALGORITHM
+ *
+ *      The "deflation" process depends on being able to identify portions
+ *      of the input text which are identical to earlier input (within a
+ *      sliding window trailing behind the input currently being processed).
+ *
+ *      The most straightforward technique turns out to be the fastest for
+ *      most input files: try all possible matches and select the longest.
+ *      The key feature of this algorithm is that insertions into the string
+ *      dictionary are very simple and thus fast, and deletions are avoided
+ *      completely. Insertions are performed at each input character, whereas
+ *      string matches are performed only when the previous match ends. So it
+ *      is preferable to spend more time in matches to allow very fast string
+ *      insertions and avoid deletions. The matching algorithm for small
+ *      strings is inspired from that of Rabin & Karp. A brute force approach
+ *      is used to find longer strings when a small match has been found.
+ *      A similar algorithm is used in comic (by Jan-Mark Wams) and freeze
+ *      (by Leonid Broukhis).
+ *         A previous version of this file used a more sophisticated algorithm
+ *      (by Fiala and Greene) which is guaranteed to run in linear amortized
+ *      time, but has a larger average cost, uses more memory and is patented.
+ *      However the F&G algorithm may be faster for some highly redundant
+ *      files if the parameter max_chain_length (described below) is too large.
+ *
+ *  ACKNOWLEDGEMENTS
+ *
+ *      The idea of lazy evaluation of matches is due to Jan-Mark Wams, and
+ *      I found it in 'freeze' written by Leonid Broukhis.
+ *      Thanks to many people for bug reports and testing.
+ *
+ *  REFERENCES
+ *
+ *      Deutsch, L.P.,"DEFLATE Compressed Data Format Specification".
+ *      Available in ftp://ds.internic.net/rfc/rfc1951.txt
+ *
+ *      A description of the Rabin and Karp algorithm is given in the book
+ *         "Algorithms" by R. Sedgewick, Addison-Wesley, p252.
+ *
+ *      Fiala,E.R., and Greene,D.H.
+ *         Data Compression with Finite Windows, Comm.ACM, 32,4 (1989) 490-595
+ *
+ */
+
+/* From: deflate.c,v 1.15 1996/07/24 13:40:58 me Exp $ */
+
+/* #include "deflate.h" */
+
+char deflate_copyright[] = " deflate 1.0.4 Copyright 1995-1996 Jean-loup Gailly ";
+/*
+  If you use the zlib library in a product, an acknowledgment is welcome
+  in the documentation of your product. If for some reason you cannot
+  include such an acknowledgment, I would appreciate that you keep this
+  copyright string in the executable of your product.
+ */
+
+/* ===========================================================================
+ *  Function prototypes.
+ */
+typedef enum {
+    need_more,      /* block not completed, need more input or more output */
+    block_done,     /* block flush performed */
+    finish_started, /* finish started, need only more output at next deflate */
+    finish_done     /* finish done, accept no more input or output */
+} block_state;
+
+typedef block_state (*compress_func) OF((deflate_state *s, int flush));
+/* Compression function. Returns the block state after the call. */
+
+local void fill_window    OF((deflate_state *s));
+local block_state deflate_stored OF((deflate_state *s, int flush));
+local block_state deflate_fast   OF((deflate_state *s, int flush));
+local block_state deflate_slow   OF((deflate_state *s, int flush));
+local void lm_init        OF((deflate_state *s));
+local void putShortMSB    OF((deflate_state *s, uInt b));
+local void flush_pending  OF((z_streamp strm));
+local int read_buf        OF((z_streamp strm, charf *buf, unsigned size));
+#ifdef ASMV
+      void match_init OF((void)); /* asm code initialization */
+      uInt longest_match  OF((deflate_state *s, IPos cur_match));
+#else
+local uInt longest_match  OF((deflate_state *s, IPos cur_match));
+#endif
+
+#ifdef DEBUG_ZLIB
+local  void check_match OF((deflate_state *s, IPos start, IPos match,
+                            int length));
+#endif
+
+/* ===========================================================================
+ * Local data
+ */
+
+#define NIL 0
+/* Tail of hash chains */
+
+#ifndef TOO_FAR
+#  define TOO_FAR 4096
+#endif
+/* Matches of length 3 are discarded if their distance exceeds TOO_FAR */
+
+#define MIN_LOOKAHEAD (MAX_MATCH+MIN_MATCH+1)
+/* Minimum amount of lookahead, except at the end of the input file.
+ * See deflate.c for comments about the MIN_MATCH+1.
+ */
+
+/* Values for max_lazy_match, good_match and max_chain_length, depending on
+ * the desired pack level (0..9). The values given below have been tuned to
+ * exclude worst case performance for pathological files. Better values may be
+ * found for specific files.
+ */
+typedef struct config_s {
+   ush good_length; /* reduce lazy search above this match length */
+   ush max_lazy;    /* do not perform lazy search above this match length */
+   ush nice_length; /* quit search above this match length */
+   ush max_chain;
+   compress_func func;
+} config;
+
+local config configuration_table[10] = {
+/*      good lazy nice chain */
+/* 0 */ {0,    0,  0,    0, deflate_stored},  /* store only */
+/* 1 */ {4,    4,  8,    4, deflate_fast}, /* maximum speed, no lazy matches */
+/* 2 */ {4,    5, 16,    8, deflate_fast},
+/* 3 */ {4,    6, 32,   32, deflate_fast},
+
+/* 4 */ {4,    4, 16,   16, deflate_slow},  /* lazy matches */
+/* 5 */ {8,   16, 32,   32, deflate_slow},
+/* 6 */ {8,   16, 128, 128, deflate_slow},
+/* 7 */ {8,   32, 128, 256, deflate_slow},
+/* 8 */ {32, 128, 258, 1024, deflate_slow},
+/* 9 */ {32, 258, 258, 4096, deflate_slow}}; /* maximum compression */
+
+/* Note: the deflate() code requires max_lazy >= MIN_MATCH and max_chain >= 4
+ * For deflate_fast() (levels <= 3) good is ignored and lazy has a different
+ * meaning.
+ */
+
+#define EQUAL 0
+/* result of memcmp for equal strings */
+
+#ifndef NO_DUMMY_DECL
+struct static_tree_desc_s {int dummy;}; /* for buggy compilers */
+#endif
+
+/* ===========================================================================
+ * Update a hash value with the given input byte
+ * IN  assertion: all calls to to UPDATE_HASH are made with consecutive
+ *    input characters, so that a running hash key can be computed from the
+ *    previous key instead of complete recalculation each time.
+ */
+#define UPDATE_HASH(s,h,c) (h = (((h)<<s->hash_shift) ^ (c)) & s->hash_mask)
+
+
+/* ===========================================================================
+ * Insert string str in the dictionary and set match_head to the previous head
+ * of the hash chain (the most recent string with same hash key). Return
+ * the previous length of the hash chain.
+ * IN  assertion: all calls to to INSERT_STRING are made with consecutive
+ *    input characters and the first MIN_MATCH bytes of str are valid
+ *    (except for the last MIN_MATCH-1 bytes of the input file).
+ */
+#define INSERT_STRING(s, str, match_head) \
+   (UPDATE_HASH(s, s->ins_h, s->window[(str) + (MIN_MATCH-1)]), \
+    s->prev[(str) & s->w_mask] = match_head = s->head[s->ins_h], \
+    s->head[s->ins_h] = (Pos)(str))
+
+/* ===========================================================================
+ * Initialize the hash table (avoiding 64K overflow for 16 bit systems).
+ * prev[] will be initialized on the fly.
+ */
+#define CLEAR_HASH(s) \
+    s->head[s->hash_size-1] = NIL; \
+    zmemzero((charf *)s->head, (unsigned)(s->hash_size-1)*sizeof(*s->head));
+
+/* ========================================================================= */
+int deflateInit_(strm, level, version, stream_size)
+    z_streamp strm;
+    int level;
+    const char *version;
+    int stream_size;
+{
+    return deflateInit2_(strm, level, Z_DEFLATED, MAX_WBITS, DEF_MEM_LEVEL,
+                        Z_DEFAULT_STRATEGY, version, stream_size);
+    /* To do: ignore strm->next_in if we use it as window */
+}
+
+/* ========================================================================= */
+int deflateInit2_(strm, level, method, windowBits, memLevel, strategy,
+                 version, stream_size)
+    z_streamp strm;
+    int  level;
+    int  method;
+    int  windowBits;
+    int  memLevel;
+    int  strategy;
+    const char *version;
+    int stream_size;
+{
+    deflate_state *s;
+    int noheader = 0;
+    static char* my_version = ZLIB_VERSION;
+
+    ushf *overlay;
+    /* We overlay pending_buf and d_buf+l_buf. This works since the average
+     * output size for (length,distance) codes is <= 24 bits.
+     */
+
+    if (version == Z_NULL || version[0] != my_version[0] ||
+        stream_size != sizeof(z_stream)) {
+       return Z_VERSION_ERROR;
+    }
+    if (strm == Z_NULL) return Z_STREAM_ERROR;
+
+    strm->msg = Z_NULL;
+#ifndef NO_ZCFUNCS
+    if (strm->zalloc == Z_NULL) {
+       strm->zalloc = zcalloc;
+       strm->opaque = (voidpf)0;
+    }
+    if (strm->zfree == Z_NULL) strm->zfree = zcfree;
+#endif
+
+    if (level == Z_DEFAULT_COMPRESSION) level = 6;
+
+    if (windowBits < 0) { /* undocumented feature: suppress zlib header */
+        noheader = 1;
+        windowBits = -windowBits;
+    }
+    if (memLevel < 1 || memLevel > MAX_MEM_LEVEL || method != Z_DEFLATED ||
+        windowBits < 8 || windowBits > 15 || level < 0 || level > 9 ||
+       strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
+        return Z_STREAM_ERROR;
+    }
+    s = (deflate_state *) ZALLOC(strm, 1, sizeof(deflate_state));
+    if (s == Z_NULL) return Z_MEM_ERROR;
+    strm->state = (struct internal_state FAR *)s;
+    s->strm = strm;
+
+    s->noheader = noheader;
+    s->w_bits = windowBits;
+    s->w_size = 1 << s->w_bits;
+    s->w_mask = s->w_size - 1;
+
+    s->hash_bits = memLevel + 7;
+    s->hash_size = 1 << s->hash_bits;
+    s->hash_mask = s->hash_size - 1;
+    s->hash_shift =  ((s->hash_bits+MIN_MATCH-1)/MIN_MATCH);
+
+    s->window = (Bytef *) ZALLOC(strm, s->w_size, 2*sizeof(Byte));
+    s->prev   = (Posf *)  ZALLOC(strm, s->w_size, sizeof(Pos));
+    s->head   = (Posf *)  ZALLOC(strm, s->hash_size, sizeof(Pos));
+
+    s->lit_bufsize = 1 << (memLevel + 6); /* 16K elements by default */
+
+    overlay = (ushf *) ZALLOC(strm, s->lit_bufsize, sizeof(ush)+2);
+    s->pending_buf = (uchf *) overlay;
+    s->pending_buf_size = (ulg)s->lit_bufsize * (sizeof(ush)+2L);
+
+    if (s->window == Z_NULL || s->prev == Z_NULL || s->head == Z_NULL ||
+        s->pending_buf == Z_NULL) {
+        strm->msg = (char*)ERR_MSG(Z_MEM_ERROR);
+        deflateEnd (strm);
+        return Z_MEM_ERROR;
+    }
+    s->d_buf = overlay + s->lit_bufsize/sizeof(ush);
+    s->l_buf = s->pending_buf + (1+sizeof(ush))*s->lit_bufsize;
+
+    s->level = level;
+    s->strategy = strategy;
+    s->method = (Byte)method;
+
+    return deflateReset(strm);
+}
+
+/* ========================================================================= */
+int deflateSetDictionary (strm, dictionary, dictLength)
+    z_streamp strm;
+    const Bytef *dictionary;
+    uInt  dictLength;
+{
+    deflate_state *s;
+    uInt length = dictLength;
+    uInt n;
+    IPos hash_head = 0;
+
+    if (strm == Z_NULL || strm->state == Z_NULL || dictionary == Z_NULL)
+       return Z_STREAM_ERROR;
+
+    s = (deflate_state *) strm->state;
+    if (s->status != INIT_STATE) return Z_STREAM_ERROR;
+
+    strm->adler = adler32(strm->adler, dictionary, dictLength);
+
+    if (length < MIN_MATCH) return Z_OK;
+    if (length > MAX_DIST(s)) {
+       length = MAX_DIST(s);
+#ifndef USE_DICT_HEAD
+       dictionary += dictLength - length; /* use the tail of the dictionary */
+#endif
+    }
+    zmemcpy((charf *)s->window, dictionary, length);
+    s->strstart = length;
+    s->block_start = (long)length;
+
+    /* Insert all strings in the hash table (except for the last two bytes).
+     * s->lookahead stays null, so s->ins_h will be recomputed at the next
+     * call of fill_window.
+     */
+    s->ins_h = s->window[0];
+    UPDATE_HASH(s, s->ins_h, s->window[1]);
+    for (n = 0; n <= length - MIN_MATCH; n++) {
+       INSERT_STRING(s, n, hash_head);
+    }
+    if (hash_head) hash_head = 0;  /* to make compiler happy */
+    return Z_OK;
+}
+
+/* ========================================================================= */
+int deflateReset (strm)
+    z_streamp strm;
+{
+    deflate_state *s;
+    
+    if (strm == Z_NULL || strm->state == Z_NULL ||
+        strm->zalloc == Z_NULL || strm->zfree == Z_NULL) return Z_STREAM_ERROR;
+
+    strm->total_in = strm->total_out = 0;
+    strm->msg = Z_NULL; /* use zfree if we ever allocate msg dynamically */
+    strm->data_type = Z_UNKNOWN;
+
+    s = (deflate_state *)strm->state;
+    s->pending = 0;
+    s->pending_out = s->pending_buf;
+
+    if (s->noheader < 0) {
+        s->noheader = 0; /* was set to -1 by deflate(..., Z_FINISH); */
+    }
+    s->status = s->noheader ? BUSY_STATE : INIT_STATE;
+    strm->adler = 1;
+    s->last_flush = Z_NO_FLUSH;
+
+    _tr_init(s);
+    lm_init(s);
+
+    return Z_OK;
+}
+
+/* ========================================================================= */
+int deflateParams(strm, level, strategy)
+    z_streamp strm;
+    int level;
+    int strategy;
+{
+    deflate_state *s;
+    compress_func func;
+    int err = Z_OK;
+
+    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+    s = (deflate_state *) strm->state;
+
+    if (level == Z_DEFAULT_COMPRESSION) {
+       level = 6;
+    }
+    if (level < 0 || level > 9 || strategy < 0 || strategy > Z_HUFFMAN_ONLY) {
+       return Z_STREAM_ERROR;
+    }
+    func = configuration_table[s->level].func;
+
+    if (func != configuration_table[level].func && strm->total_in != 0) {
+       /* Flush the last buffer: */
+       err = deflate(strm, Z_PARTIAL_FLUSH);
+    }
+    if (s->level != level) {
+       s->level = level;
+       s->max_lazy_match   = configuration_table[level].max_lazy;
+       s->good_match       = configuration_table[level].good_length;
+       s->nice_match       = configuration_table[level].nice_length;
+       s->max_chain_length = configuration_table[level].max_chain;
+    }
+    s->strategy = strategy;
+    return err;
+}
+
+/* =========================================================================
+ * Put a short in the pending buffer. The 16-bit value is put in MSB order.
+ * IN assertion: the stream state is correct and there is enough room in
+ * pending_buf.
+ */
+local void putShortMSB (s, b)
+    deflate_state *s;
+    uInt b;
+{
+    put_byte(s, (Byte)(b >> 8));
+    put_byte(s, (Byte)(b & 0xff));
+}   
+
+/* =========================================================================
+ * Flush as much pending output as possible. All deflate() output goes
+ * through this function so some applications may wish to modify it
+ * to avoid allocating a large strm->next_out buffer and copying into it.
+ * (See also read_buf()).
+ */
+local void flush_pending(strm)
+    z_streamp strm;
+{
+    deflate_state *s = (deflate_state *) strm->state;
+    unsigned len = s->pending;
+
+    if (len > strm->avail_out) len = strm->avail_out;
+    if (len == 0) return;
+
+    if (strm->next_out != Z_NULL) {
+       zmemcpy(strm->next_out, s->pending_out, len);
+       strm->next_out += len;
+    }
+    s->pending_out += len;
+    strm->total_out += len;
+    strm->avail_out  -= len;
+    s->pending -= len;
+    if (s->pending == 0) {
+        s->pending_out = s->pending_buf;
+    }
+}
+
+/* ========================================================================= */
+int deflate (strm, flush)
+    z_streamp strm;
+    int flush;
+{
+    int old_flush; /* value of flush param for previous deflate call */
+    deflate_state *s;
+
+    if (strm == Z_NULL || strm->state == Z_NULL ||
+       flush > Z_FINISH || flush < 0) {
+        return Z_STREAM_ERROR;
+    }
+    s = (deflate_state *) strm->state;
+
+    if ((strm->next_in == Z_NULL && strm->avail_in != 0) ||
+       (s->status == FINISH_STATE && flush != Z_FINISH)) {
+        ERR_RETURN(strm, Z_STREAM_ERROR);
+    }
+    if (strm->avail_out == 0) ERR_RETURN(strm, Z_BUF_ERROR);
+
+    s->strm = strm; /* just in case */
+    old_flush = s->last_flush;
+    s->last_flush = flush;
+
+    /* Write the zlib header */
+    if (s->status == INIT_STATE) {
+
+        uInt header = (Z_DEFLATED + ((s->w_bits-8)<<4)) << 8;
+        uInt level_flags = (s->level-1) >> 1;
+
+        if (level_flags > 3) level_flags = 3;
+        header |= (level_flags << 6);
+       if (s->strstart != 0) header |= PRESET_DICT;
+        header += 31 - (header % 31);
+
+        s->status = BUSY_STATE;
+        putShortMSB(s, header);
+
+       /* Save the adler32 of the preset dictionary: */
+       if (s->strstart != 0) {
+           putShortMSB(s, (uInt)(strm->adler >> 16));
+           putShortMSB(s, (uInt)(strm->adler & 0xffff));
+       }
+       strm->adler = 1L;
+    }
+
+    /* Flush as much pending output as possible */
+    if (s->pending != 0) {
+        flush_pending(strm);
+        if (strm->avail_out == 0) {
+           /* Since avail_out is 0, deflate will be called again with
+            * more output space, but possibly with both pending and
+            * avail_in equal to zero. There won't be anything to do,
+            * but this is not an error situation so make sure we
+            * return OK instead of BUF_ERROR at next call of deflate:
+             */
+           s->last_flush = -1;
+           return Z_OK;
+       }
+
+    /* Make sure there is something to do and avoid duplicate consecutive
+     * flushes. For repeated and useless calls with Z_FINISH, we keep
+     * returning Z_STREAM_END instead of Z_BUFF_ERROR.
+     */
+    } else if (strm->avail_in == 0 && flush <= old_flush &&
+              flush != Z_FINISH) {
+        ERR_RETURN(strm, Z_BUF_ERROR);
+    }
+
+    /* User must not provide more input after the first FINISH: */
+    if (s->status == FINISH_STATE && strm->avail_in != 0) {
+        ERR_RETURN(strm, Z_BUF_ERROR);
+    }
+
+    /* Start a new block or continue the current one.
+     */
+    if (strm->avail_in != 0 || s->lookahead != 0 ||
+        (flush != Z_NO_FLUSH && s->status != FINISH_STATE)) {
+        block_state bstate;
+
+       bstate = (*(configuration_table[s->level].func))(s, flush);
+
+        if (bstate == finish_started || bstate == finish_done) {
+            s->status = FINISH_STATE;
+        }
+        if (bstate == need_more || bstate == finish_started) {
+           if (strm->avail_out == 0) {
+               s->last_flush = -1; /* avoid BUF_ERROR next call, see above */
+           }
+           return Z_OK;
+           /* If flush != Z_NO_FLUSH && avail_out == 0, the next call
+            * of deflate should use the same flush parameter to make sure
+            * that the flush is complete. So we don't have to output an
+            * empty block here, this will be done at next call. This also
+            * ensures that for a very small output buffer, we emit at most
+            * one empty block.
+            */
+       }
+        if (bstate == block_done) {
+            if (flush == Z_PARTIAL_FLUSH) {
+                _tr_align(s);
+           } else if (flush == Z_PACKET_FLUSH) {
+               /* Output just the 3-bit `stored' block type value,
+                  but not a zero length. */
+               _tr_stored_type_only(s);
+            } else { /* FULL_FLUSH or SYNC_FLUSH */
+                _tr_stored_block(s, (char*)0, 0L, 0);
+                /* For a full flush, this empty block will be recognized
+                 * as a special marker by inflate_sync().
+                 */
+                if (flush == Z_FULL_FLUSH) {
+                    CLEAR_HASH(s);             /* forget history */
+                }
+            }
+            flush_pending(strm);
+           if (strm->avail_out == 0) {
+             s->last_flush = -1; /* avoid BUF_ERROR at next call, see above */
+             return Z_OK;
+           }
+        }
+    }
+    Assert(strm->avail_out > 0, "bug2");
+
+    if (flush != Z_FINISH) return Z_OK;
+    if (s->noheader) return Z_STREAM_END;
+
+    /* Write the zlib trailer (adler32) */
+    putShortMSB(s, (uInt)(strm->adler >> 16));
+    putShortMSB(s, (uInt)(strm->adler & 0xffff));
+    flush_pending(strm);
+    /* If avail_out is zero, the application will call deflate again
+     * to flush the rest.
+     */
+    s->noheader = -1; /* write the trailer only once! */
+    return s->pending != 0 ? Z_OK : Z_STREAM_END;
+}
+
+/* ========================================================================= */
+int deflateEnd (strm)
+    z_streamp strm;
+{
+    int status;
+    deflate_state *s;
+
+    if (strm == Z_NULL || strm->state == Z_NULL) return Z_STREAM_ERROR;
+    s = (deflate_state *) strm->state;
+
+    status = s->status;
+    if (status != INIT_STATE && status != BUSY_STATE &&
+       status != FINISH_STATE) {
+      return Z_STREAM_ERROR;
+    }
+
+    /* Deallocate in reverse order of allocations: */
+    TRY_FREE(strm, s->pending_buf);
+    TRY_FREE(strm, s->head);
+    TRY_FREE(strm, s->prev);
+    TRY_FREE(strm, s->window);
+
+    ZFREE(strm, s);
+    strm->state = Z_NULL;
+
+    return status == BUSY_STATE ? Z_DATA_ERROR : Z_OK;
+}
+
+/* =========================================================================
+ * Copy the source state to the destination state.
+ */
+int deflateCopy (dest, source)
+    z_streamp dest;
+    z_streamp source;
+{
+    deflate_state *ds;
+    deflate_state *ss;
+    ushf *overlay;
+
+    if (source == Z_NULL || dest == Z_NULL || source->state == Z_NULL)
+        return Z_STREAM_ERROR;
+    ss = (deflate_state *) source->state;
+
+    *dest = *source;
+
+    ds = (deflate_state *) ZALLOC(dest, 1, sizeof(deflate_state));
+    if (ds == Z_NULL) return Z_MEM_ERROR;
+    dest->state = (struct internal_state FAR *) ds;
+    *ds = *ss;
+    ds->strm = dest;
+
+    ds->window = (Bytef *) ZALLOC(dest, ds->w_size, 2*sizeof(Byte));
+    ds->prev   = (Posf *)  ZALLOC(dest, ds->w_size, sizeof(Pos));
+    ds->head   = (Posf *)  ZALLOC(dest, ds->hash_size, sizeof(Pos));
+    overlay = (ushf *) ZALLOC(dest, ds->lit_bufsize, sizeof(ush)+2);
+    ds->pending_buf = (uchf *) overlay;
+
+    if (ds->window == Z_NULL || ds->prev == Z_NULL || ds->head == Z_NULL ||
+        ds->pending_buf == Z_NULL) {
+        deflateEnd (dest);
+        return Z_MEM_ERROR;
+    }
+    /* ??? following zmemcpy doesn't work for 16-bit MSDOS */
+    zmemcpy(ds->window, ss->window, ds->w_size * 2 * sizeof(Byte));
+    zmemcpy(ds->prev, ss->prev, ds->w_size * sizeof(Pos));
+    zmemcpy(ds->head, ss->head, ds->hash_size * sizeof(Pos));
+    zmemcpy(ds->pending_buf, ss->pending_buf, (uInt)ds->pending_buf_size);
+
+    ds->pending_out = ds->pending_buf + (ss->pending_out - ss->pending_buf);
+    ds->d_buf = overlay + ds->lit_bufsize/sizeof(ush);
+    ds->l_buf = ds->pending_buf + (1+sizeof(ush))*ds->lit_bufsize;
+
+    ds->l_desc.dyn_tree = ds->dyn_ltree;
+    ds->d_desc.dyn_tree = ds->dyn_dtree;
+    ds->bl_desc.dyn_tree = ds->bl_tree;
+
+    return Z_OK;
+}
+
+/* ===========================================================================
+ * Return the number of bytes of output which are immediately available
+ * for output from the decompressor.
+ */
+int deflateOutputPending (strm)
+    z_streamp strm;
+{
+    if (strm == Z_NULL || strm->state == Z_NULL) return 0;
+    
+    return ((deflate_state *)(strm->state))->pending;
+}
+
+/* ===========================================================================
+ * Read a new buffer from the current input stream, update the adler32
+ * and total number of bytes read.  All deflate() input goes through
+ * this function so some applications may wish to modify it to avoid
+ * allocating a large strm->next_in buffer and copying from it.
+ * (See also flush_pending()).
+ */
+local int read_buf(strm, buf, size)
+    z_streamp strm;
+    charf *buf;
+    unsigned size;
+{
+    unsigned len = strm->avail_in;
+
+    if (len > size) len = size;
+    if (len == 0) return 0;
+
+    strm->avail_in  -= len;
+
+    if (!((deflate_state *)(strm->state))->noheader) {
+        strm->adler = adler32(strm->adler, strm->next_in, len);
+    }
+    zmemcpy(buf, strm->next_in, len);
+    strm->next_in  += len;
+    strm->total_in += len;
+
+    return (int)len;
+}
+
+/* ===========================================================================
+ * Initialize the "longest match" routines for a new zlib stream
+ */
+local void lm_init (s)
+    deflate_state *s;
+{
+    s->window_size = (ulg)2L*s->w_size;
+
+    CLEAR_HASH(s);
+
+    /* Set the default configuration parameters:
+     */
+    s->max_lazy_match   = configuration_table[s->level].max_lazy;
+    s->good_match       = configuration_table[s->level].good_length;
+    s->nice_match       = configuration_table[s->level].nice_length;
+    s->max_chain_length = configuration_table[s->level].max_chain;
+
+    s->strstart = 0;
+    s->block_start = 0L;
+    s->lookahead = 0;
+    s->match_length = s->prev_length = MIN_MATCH-1;
+    s->match_available = 0;
+    s->ins_h = 0;
+#ifdef ASMV
+    match_init(); /* initialize the asm code */
+#endif
+}
+
+/* ===========================================================================
+ * Set match_start to the longest match starting at the given string and
+ * return its length. Matches shorter or equal to prev_length are discarded,
+ * in which case the result is equal to prev_length and match_start is
+ * garbage.
+ * IN assertions: cur_match is the head of the hash chain for the current
+ *   string (strstart) and its distance is <= MAX_DIST, and prev_length >= 1
+ * OUT assertion: the match length is not greater than s->lookahead.
+ */
+#ifndef ASMV
+/* For 80x86 and 680x0, an optimized version will be provided in match.asm or
+ * match.S. The code will be functionally equivalent.
+ */
+local uInt longest_match(s, cur_match)
+    deflate_state *s;
+    IPos cur_match;                             /* current match */
+{
+    unsigned chain_length = s->max_chain_length;/* max hash chain length */
+    register Bytef *scan = s->window + s->strstart; /* current string */
+    register Bytef *match;                       /* matched string */
+    register int len;                           /* length of current match */
+    int best_len = s->prev_length;              /* best match length so far */
+    int nice_match = s->nice_match;             /* stop if match long enough */
+    IPos limit = s->strstart > (IPos)MAX_DIST(s) ?
+        s->strstart - (IPos)MAX_DIST(s) : NIL;
+    /* Stop when cur_match becomes <= limit. To simplify the code,
+     * we prevent matches with the string of window index 0.
+     */
+    Posf *prev = s->prev;
+    uInt wmask = s->w_mask;
+
+#ifdef UNALIGNED_OK
+    /* Compare two bytes at a time. Note: this is not always beneficial.
+     * Try with and without -DUNALIGNED_OK to check.
+     */
+    register Bytef *strend = s->window + s->strstart + MAX_MATCH - 1;
+    register ush scan_start = *(ushf*)scan;
+    register ush scan_end   = *(ushf*)(scan+best_len-1);
+#else
+    register Bytef *strend = s->window + s->strstart + MAX_MATCH;
+    register Byte scan_end1  = scan[best_len-1];
+    register Byte scan_end   = scan[best_len];
+#endif
+
+    /* The code is optimized for HASH_BITS >= 8 and MAX_MATCH-2 multiple of 16.
+     * It is easy to get rid of this optimization if necessary.
+     */
+    Assert(s->hash_bits >= 8 && MAX_MATCH == 258, "Code too clever");
+
+    /* Do not waste too much time if we already have a good match: */
+    if (s->prev_length >= s->good_match) {
+        chain_length >>= 2;
+    }
+    /* Do not look for matches beyond the end of the input. This is necessary
+     * to make deflate deterministic.
+     */
+    if ((uInt)nice_match > s->lookahead) nice_match = s->lookahead;
+
+    Assert((ulg)s->strstart <= s->window_size-MIN_LOOKAHEAD, "need lookahead");
+
+    do {
+        Assert(cur_match < s->strstart, "no future");
+        match = s->window + cur_match;
+
+        /* Skip to next match if the match length cannot increase
+         * or if the match length is less than 2:
+         */
+#if (defined(UNALIGNED_OK) && MAX_MATCH == 258)
+        /* This code assumes sizeof(unsigned short) == 2. Do not use
+         * UNALIGNED_OK if your compiler uses a different size.
+         */
+        if (*(ushf*)(match+best_len-1) != scan_end ||
+            *(ushf*)match != scan_start) continue;
+
+        /* It is not necessary to compare scan[2] and match[2] since they are
+         * always equal when the other bytes match, given that the hash keys
+         * are equal and that HASH_BITS >= 8. Compare 2 bytes at a time at
+         * strstart+3, +5, ... up to strstart+257. We check for insufficient
+         * lookahead only every 4th comparison; the 128th check will be made
+         * at strstart+257. If MAX_MATCH-2 is not a multiple of 8, it is
+         * necessary to put more guard bytes at the end of the window, or
+         * to check more often for insufficient lookahead.
+         */
+        Assert(scan[2] == match[2], "scan[2]?");
+        scan++, match++;
+        do {
+        } while (*(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+                 *(ushf*)(scan+=2) == *(ushf*)(match+=2) &&
+                 scan < strend);
+        /* The funny "do {}" generates better code on most compilers */
+
+        /* Here, scan <= window+strstart+257 */
+        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
+        if (*scan == *match) scan++;
+
+        len = (MAX_MATCH - 1) - (int)(strend-scan);
+        scan = strend - (MAX_MATCH-1);
+
+#else /* UNALIGNED_OK */
+
+        if (match[best_len]   != scan_end  ||
+            match[best_len-1] != scan_end1 ||
+            *match            != *scan     ||
+            *++match          != scan[1])      continue;
+
+        /* The check at best_len-1 can be removed because it will be made
+         * again later. (This heuristic is not always a win.)
+         * It is not necessary to compare scan[2] and match[2] since they
+         * are always equal when the other bytes match, given that
+         * the hash keys are equal and that HASH_BITS >= 8.
+         */
+        scan += 2, match++;
+        Assert(*scan == *match, "match[2]?");
+
+        /* We check for insufficient lookahead only every 8th comparison;
+         * the 256th check will be made at strstart+258.
+         */
+        do {
+        } while (*++scan == *++match && *++scan == *++match &&
+                 *++scan == *++match && *++scan == *++match &&
+                 *++scan == *++match && *++scan == *++match &&
+                 *++scan == *++match && *++scan == *++match &&
+                 scan < strend);
+
+        Assert(scan <= s->window+(unsigned)(s->window_size-1), "wild scan");
+
+        len = MAX_MATCH - (int)(strend - scan);
+        scan = strend - MAX_MATCH;
+
+#endif /* UNALIGNED_OK */
+
+        if (len > best_len) {
+            s->match_start = cur_match;
+            best_len = len;
+            if (len >= nice_match) break;
+#ifdef UNALIGNED_OK
+            scan_end = *(ushf*)(scan+best_len-1);
+#else
+            scan_end1  = scan[best_len-1];
+            scan_end   = scan[best_len];
+#endif
+        }
+    } while ((cur_match = prev[cur_match & wmask]) > limit
+             && --chain_length != 0);
+
+    if ((uInt)best_len <= s->lookahead) return best_len;
+    return s->lookahead;
+}
+#endif /* ASMV */
+
+#ifdef DEBUG_ZLIB
+/* ===========================================================================
+ * Check that the match at match_start is indeed a match.
+ */
+local void check_match(s, start, match, length)
+    deflate_state *s;
+    IPos start, match;
+    int length;
+{
+    /* check that the match is indeed a match */
+    if (zmemcmp((charf *)s->window + match,
+                (charf *)s->window + start, length) != EQUAL) {
+        fprintf(stderr, " start %u, match %u, length %d\n",
+               start, match, length);
+        do {
+           fprintf(stderr, "%c%c", s->window[match++], s->window[start++]);
+       } while (--length != 0);
+        z_error("invalid match");
+    }
+    if (z_verbose > 1) {
+        fprintf(stderr,"\\[%d,%d]", start-match, length);
+        do { putc(s->window[start++], stderr); } while (--length != 0);
+    }
+}
+#else
+#  define check_match(s, start, match, length)
+#endif
+
+/* ===========================================================================
+ * Fill the window when the lookahead becomes insufficient.
+ * Updates strstart and lookahead.
+ *
+ * IN assertion: lookahead < MIN_LOOKAHEAD
+ * OUT assertions: strstart <= window_size-MIN_LOOKAHEAD
+ *    At least one byte has been read, or avail_in == 0; reads are
+ *    performed for at least two bytes (required for the zip translate_eol
+ *    option -- not supported here).
+ */
+local void fill_window(s)
+    deflate_state *s;
+{
+    register unsigned n, m;
+    register Posf *p;
+    unsigned more;    /* Amount of free space at the end of the window. */
+    uInt wsize = s->w_size;
+
+    do {
+        more = (unsigned)(s->window_size -(ulg)s->lookahead -(ulg)s->strstart);
+
+        /* Deal with !@#$% 64K limit: */
+        if (more == 0 && s->strstart == 0 && s->lookahead == 0) {
+            more = wsize;
+
+        } else if (more == (unsigned)(-1)) {
+            /* Very unlikely, but possible on 16 bit machine if strstart == 0
+             * and lookahead == 1 (input done one byte at time)
+             */
+            more--;
+
+        /* If the window is almost full and there is insufficient lookahead,
+         * move the upper half to the lower one to make room in the upper half.
+         */
+        } else if (s->strstart >= wsize+MAX_DIST(s)) {
+
+            zmemcpy((charf *)s->window, (charf *)s->window+wsize,
+                   (unsigned)wsize);
+            s->match_start -= wsize;
+            s->strstart    -= wsize; /* we now have strstart >= MAX_DIST */
+            s->block_start -= (long) wsize;
+
+            /* Slide the hash table (could be avoided with 32 bit values
+               at the expense of memory usage). We slide even when level == 0
+               to keep the hash table consistent if we switch back to level > 0
+               later. (Using level 0 permanently is not an optimal usage of
+               zlib, so we don't care about this pathological case.)
+             */
+            n = s->hash_size;
+            p = &s->head[n];
+            do {
+                m = *--p;
+                *p = (Pos)(m >= wsize ? m-wsize : NIL);
+            } while (--n);
+
+            n = wsize;
+            p = &s->prev[n];
+            do {
+                m = *--p;
+                *p = (Pos)(m >= wsize ? m-wsize : NIL);
+                /* If n is not on any hash chain, prev[n] is garbage but
+                 * its value will never be used.
+                 */
+            } while (--n);
+            more += wsize;
+        }
+        if (s->strm->avail_in == 0) return;
+
+        /* If there was no sliding:
+         *    strstart <= WSIZE+MAX_DIST-1 && lookahead <= MIN_LOOKAHEAD - 1 &&
+         *    more == window_size - lookahead - strstart
+         * => more >= window_size - (MIN_LOOKAHEAD-1 + WSIZE + MAX_DIST-1)
+         * => more >= window_size - 2*WSIZE + 2
+         * In the BIG_MEM or MMAP case (not yet supported),
+         *   window_size == input_size + MIN_LOOKAHEAD  &&
+         *   strstart + s->lookahead <= input_size => more >= MIN_LOOKAHEAD.
+         * Otherwise, window_size == 2*WSIZE so more >= 2.
+         * If there was sliding, more >= WSIZE. So in all cases, more >= 2.
+         */
+        Assert(more >= 2, "more < 2");
+
+        n = read_buf(s->strm, (charf *)s->window + s->strstart + s->lookahead,
+                     more);
+        s->lookahead += n;
+
+        /* Initialize the hash value now that we have some input: */
+        if (s->lookahead >= MIN_MATCH) {
+            s->ins_h = s->window[s->strstart];
+            UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
+#if MIN_MATCH != 3
+            Call UPDATE_HASH() MIN_MATCH-3 more times
+#endif
+        }
+        /* If the whole input has less than MIN_MATCH bytes, ins_h is garbage,
+         * but this is not important since only literal bytes will be emitted.
+         */
+
+    } while (s->lookahead < MIN_LOOKAHEAD && s->strm->avail_in != 0);
+}
+
+/* ===========================================================================
+ * Flush the current block, with given end-of-file flag.
+ * IN assertion: strstart is set to the end of the current match.
+ */
+#define FLUSH_BLOCK_ONLY(s, eof) { \
+   _tr_flush_block(s, (s->block_start >= 0L ? \
+                   (charf *)&s->window[(unsigned)s->block_start] : \
+                   (charf *)Z_NULL), \
+               (ulg)((long)s->strstart - s->block_start), \
+               (eof)); \
+   s->block_start = s->strstart; \
+   flush_pending(s->strm); \
+   Tracev((stderr,"[FLUSH]")); \
+}
+
+/* Same but force premature exit if necessary. */
+#define FLUSH_BLOCK(s, eof) { \
+   FLUSH_BLOCK_ONLY(s, eof); \
+   if (s->strm->avail_out == 0) return (eof) ? finish_started : need_more; \
+}
+
+/* ===========================================================================
+ * Copy without compression as much as possible from the input stream, return
+ * the current block state.
+ * This function does not insert new strings in the dictionary since
+ * uncompressible data is probably not useful. This function is used
+ * only for the level=0 compression option.
+ * NOTE: this function should be optimized to avoid extra copying from
+ * window to pending_buf.
+ */
+local block_state deflate_stored(s, flush)
+    deflate_state *s;
+    int flush;
+{
+    /* Stored blocks are limited to 0xffff bytes, pending_buf is limited
+     * to pending_buf_size, and each stored block has a 5 byte header:
+     */
+    ulg max_block_size = 0xffff;
+    ulg max_start;
+
+    if (max_block_size > s->pending_buf_size - 5) {
+        max_block_size = s->pending_buf_size - 5;
+    }
+
+    /* Copy as much as possible from input to output: */
+    for (;;) {
+        /* Fill the window as much as possible: */
+        if (s->lookahead <= 1) {
+
+            Assert(s->strstart < s->w_size+MAX_DIST(s) ||
+                  s->block_start >= (long)s->w_size, "slide too late");
+
+            fill_window(s);
+            if (s->lookahead == 0 && flush == Z_NO_FLUSH) return need_more;
+
+            if (s->lookahead == 0) break; /* flush the current block */
+        }
+       Assert(s->block_start >= 0L, "block gone");
+
+       s->strstart += s->lookahead;
+       s->lookahead = 0;
+
+       /* Emit a stored block if pending_buf will be full: */
+       max_start = s->block_start + max_block_size;
+        if (s->strstart == 0 || (ulg)s->strstart >= max_start) {
+           /* strstart == 0 is possible when wraparound on 16-bit machine */
+           s->lookahead = (uInt)(s->strstart - max_start);
+           s->strstart = (uInt)max_start;
+            FLUSH_BLOCK(s, 0);
+       }
+       /* Flush if we may have to slide, otherwise block_start may become
+         * negative and the data will be gone:
+         */
+        if (s->strstart - (uInt)s->block_start >= MAX_DIST(s)) {
+            FLUSH_BLOCK(s, 0);
+       }
+    }
+    FLUSH_BLOCK(s, flush == Z_FINISH);
+    return flush == Z_FINISH ? finish_done : block_done;
+}
+
+/* ===========================================================================
+ * Compress as much as possible from the input stream, return the current
+ * block state.
+ * This function does not perform lazy evaluation of matches and inserts
+ * new strings in the dictionary only for unmatched strings or for short
+ * matches. It is used only for the fast compression options.
+ */
+local block_state deflate_fast(s, flush)
+    deflate_state *s;
+    int flush;
+{
+    IPos hash_head = NIL; /* head of the hash chain */
+    int bflush;           /* set if current block must be flushed */
+
+    for (;;) {
+        /* Make sure that we always have enough lookahead, except
+         * at the end of the input file. We need MAX_MATCH bytes
+         * for the next match, plus MIN_MATCH bytes to insert the
+         * string following the next match.
+         */
+        if (s->lookahead < MIN_LOOKAHEAD) {
+            fill_window(s);
+            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
+               return need_more;
+           }
+            if (s->lookahead == 0) break; /* flush the current block */
+        }
+
+        /* Insert the string window[strstart .. strstart+2] in the
+         * dictionary, and set hash_head to the head of the hash chain:
+         */
+        if (s->lookahead >= MIN_MATCH) {
+            INSERT_STRING(s, s->strstart, hash_head);
+        }
+
+        /* Find the longest match, discarding those <= prev_length.
+         * At this point we have always match_length < MIN_MATCH
+         */
+        if (hash_head != NIL && s->strstart - hash_head <= MAX_DIST(s)) {
+            /* To simplify the code, we prevent matches with the string
+             * of window index 0 (in particular we have to avoid a match
+             * of the string with itself at the start of the input file).
+             */
+            if (s->strategy != Z_HUFFMAN_ONLY) {
+                s->match_length = longest_match (s, hash_head);
+            }
+            /* longest_match() sets match_start */
+        }
+        if (s->match_length >= MIN_MATCH) {
+            check_match(s, s->strstart, s->match_start, s->match_length);
+
+            bflush = _tr_tally(s, s->strstart - s->match_start,
+                               s->match_length - MIN_MATCH);
+
+            s->lookahead -= s->match_length;
+
+            /* Insert new strings in the hash table only if the match length
+             * is not too large. This saves time but degrades compression.
+             */
+            if (s->match_length <= s->max_insert_length &&
+                s->lookahead >= MIN_MATCH) {
+                s->match_length--; /* string at strstart already in hash table */
+                do {
+                    s->strstart++;
+                    INSERT_STRING(s, s->strstart, hash_head);
+                    /* strstart never exceeds WSIZE-MAX_MATCH, so there are
+                     * always MIN_MATCH bytes ahead.
+                     */
+                } while (--s->match_length != 0);
+                s->strstart++; 
+            } else {
+                s->strstart += s->match_length;
+                s->match_length = 0;
+                s->ins_h = s->window[s->strstart];
+                UPDATE_HASH(s, s->ins_h, s->window[s->strstart+1]);
+#if MIN_MATCH != 3
+                Call UPDATE_HASH() MIN_MATCH-3 more times
+#endif
+                /* If lookahead < MIN_MATCH, ins_h is garbage, but it does not
+                 * matter since it will be recomputed at next deflate call.
+                 */
+            }
+        } else {
+            /* No match, output a literal byte */
+            Tracevv((stderr,"%c", s->window[s->strstart]));
+            bflush = _tr_tally (s, 0, s->window[s->strstart]);
+            s->lookahead--;
+            s->strstart++; 
+        }
+        if (bflush) FLUSH_BLOCK(s, 0);
+    }
+    FLUSH_BLOCK(s, flush == Z_FINISH);
+    return flush == Z_FINISH ? finish_done : block_done;
+}
+
+/* ===========================================================================
+ * Same as above, but achieves better compression. We use a lazy
+ * evaluation for matches: a match is finally adopted only if there is
+ * no better match at the next window position.
+ */
+local block_state deflate_slow(s, flush)
+    deflate_state *s;
+    int flush;
+{
+    IPos hash_head = NIL;    /* head of hash chain */
+    int bflush;              /* set if current block must be flushed */
+
+    /* Process the input block. */
+    for (;;) {
+        /* Make sure that we always have enough lookahead, except
+         * at the end of the input file. We need MAX_MATCH bytes
+         * for the next match, plus MIN_MATCH bytes to insert the
+         * string following the next match.
+         */
+        if (s->lookahead < MIN_LOOKAHEAD) {
+            fill_window(s);
+            if (s->lookahead < MIN_LOOKAHEAD && flush == Z_NO_FLUSH) {
+               return need_more;
+           }
+            if (s->lookahead == 0) break; /* flush the current block */
+        }
+
+        /* Insert the string window[strstart .. strstart+2] in the
+         * dictionary, and set hash_head to the head of the hash chain:
+         */
+        if (s->lookahead >= MIN_MATCH) {
+            INSERT_STRING(s, s->strstart, hash_head);
+        }
+
+        /* Find the longest match, discarding those <= prev_length.
+         */
+        s->prev_length = s->match_length, s->prev_match = s->match_start;
+        s->match_length = MIN_MATCH-1;
+
+        if (hash_head != NIL && s->prev_length < s->max_lazy_match &&
+            s->strstart - hash_head <= MAX_DIST(s)) {
+            /* To simplify the code, we prevent matches with the string
+             * of window index 0 (in particular we have to avoid a match
+             * of the string with itself at the start of the input file).
+             */
+            if (s->strategy != Z_HUFFMAN_ONLY) {
+                s->match_length = longest_match (s, hash_head);
+            }
+            /* longest_match() sets match_start */
+
+            if (s->match_length <= 5 && (s->strategy == Z_FILTERED ||
+                 (s->match_length == MIN_MATCH &&
+                  s->strstart - s->match_start > TOO_FAR))) {
+
+                /* If prev_match is also MIN_MATCH, match_start is garbage
+                 * but we will ignore the current match anyway.
+                 */
+                s->match_length = MIN_MATCH-1;
+            }
+        }
+        /* If there was a match at the previous step and the current
+         * match is not better, output the previous match:
+         */
+        if (s->prev_length >= MIN_MATCH && s->match_length <= s->prev_length) {
+            uInt max_insert = s->strstart + s->lookahead - MIN_MATCH;
+            /* Do not insert strings in hash table beyond this. */
+
+            check_match(s, s->strstart-1, s->prev_match, s->prev_length);
+
+            bflush = _tr_tally(s, s->strstart -1 - s->prev_match,
+                               s->prev_length - MIN_MATCH);
+
+            /* Insert in hash table all strings up to the end of the match.
+             * strstart-1 and strstart are already inserted. If there is not
+             * enough lookahead, the last two strings are not inserted in
+             * the hash table.
+             */
+            s->lookahead -= s->prev_length-1;
+            s->prev_length -= 2;
+            do {
+                if (++s->strstart <= max_insert) {
+                    INSERT_STRING(s, s->strstart, hash_head);
+                }
+            } while (--s->prev_length != 0);
+            s->match_available = 0;
+            s->match_length = MIN_MATCH-1;
+            s->strstart++;
+
+            if (bflush) FLUSH_BLOCK(s, 0);
+
+        } else if (s->match_available) {
+            /* If there was no match at the previous position, output a
+             * single literal. If there was a match but the current match
+             * is longer, truncate the previous match to a single literal.
+             */
+            Tracevv((stderr,"%c", s->window[s->strstart-1]));
+            if (_tr_tally (s, 0, s->window[s->strstart-1])) {
+                FLUSH_BLOCK_ONLY(s, 0);
+            }
+            s->strstart++;
+            s->lookahead--;
+            if (s->strm->avail_out == 0) return need_more;
+        } else {
+            /* There is no previous match to compare with, wait for
+             * the next step to decide.
+             */
+            s->match_available = 1;
+            s->strstart++;
+            s->lookahead--;
+        }
+    }
+    Assert (flush != Z_NO_FLUSH, "no flush?");
+    if (s->match_available) {
+        Tracevv((stderr,"%c", s->window[s->strstart-1]));
+        _tr_tally (s, 0, s->window[s->strstart-1]);
+        s->match_available = 0;
+    }
+    FLUSH_BLOCK(s, flush == Z_FINISH);
+    return flush == Z_FINISH ? finish_done : block_done;
+}
+/* --- deflate.c */
+
+/* +++ trees.c */
+/* trees.c -- output deflated data using Huffman coding
+ * Copyright (C) 1995-1996 Jean-loup Gailly
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/*
+ *  ALGORITHM
+ *
+ *      The "deflation" process uses several Huffman trees. The more
+ *      common source values are represented by shorter bit sequences.
+ *
+ *      Each code tree is stored in a compressed form which is itself
+ * a Huffman encoding of the lengths of all the code strings (in
+ * ascending order by source values).  The actual code strings are
+ * reconstructed from the lengths in the inflate process, as described
+ * in the deflate specification.
+ *
+ *  REFERENCES
+ *
+ *      Deutsch, L.P.,"'Deflate' Compressed Data Format Specification".
+ *      Available in ftp.uu.net:/pub/archiving/zip/doc/deflate-1.1.doc
+ *
+ *      Storer, James A.
+ *          Data Compression:  Methods and Theory, pp. 49-50.
+ *          Computer Science Press, 1988.  ISBN 0-7167-8156-5.
+ *
+ *      Sedgewick, R.
+ *          Algorithms, p290.
+ *          Addison-Wesley, 1983. ISBN 0-201-06672-6.
+ */
+
+/* From: trees.c,v 1.11 1996/07/24 13:41:06 me Exp $ */
+
+/* #include "deflate.h" */
+
+#ifdef DEBUG_ZLIB
+#  include <ctype.h>
+#endif
+
+/* ===========================================================================
+ * Constants
+ */
+
+#define MAX_BL_BITS 7
+/* Bit length codes must not exceed MAX_BL_BITS bits */
+
+#define END_BLOCK 256
+/* end of block literal code */
+
+#define REP_3_6      16
+/* repeat previous bit length 3-6 times (2 bits of repeat count) */
+
+#define REPZ_3_10    17
+/* repeat a zero length 3-10 times  (3 bits of repeat count) */
+
+#define REPZ_11_138  18
+/* repeat a zero length 11-138 times  (7 bits of repeat count) */
+
+local int extra_lbits[LENGTH_CODES] /* extra bits for each length code */
+   = {0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0};
+
+local int extra_dbits[D_CODES] /* extra bits for each distance code */
+   = {0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13};
+
+local int extra_blbits[BL_CODES]/* extra bits for each bit length code */
+   = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,3,7};
+
+local uch bl_order[BL_CODES]
+   = {16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15};
+/* The lengths of the bit length codes are sent in order of decreasing
+ * probability, to avoid transmitting the lengths for unused bit length codes.
+ */
+
+#define Buf_size (8 * 2*sizeof(char))
+/* Number of bits used within bi_buf. (bi_buf might be implemented on
+ * more than 16 bits on some systems.)
+ */
+
+/* ===========================================================================
+ * Local data. These are initialized only once.
+ */
+
+local ct_data static_ltree[L_CODES+2];
+/* The static literal tree. Since the bit lengths are imposed, there is no
+ * need for the L_CODES extra codes used during heap construction. However
+ * The codes 286 and 287 are needed to build a canonical tree (see _tr_init
+ * below).
+ */
+
+local ct_data static_dtree[D_CODES];
+/* The static distance tree. (Actually a trivial tree since all codes use
+ * 5 bits.)
+ */
+
+local uch dist_code[512];
+/* distance codes. The first 256 values correspond to the distances
+ * 3 .. 258, the last 256 values correspond to the top 8 bits of
+ * the 15 bit distances.
+ */
+
+local uch length_code[MAX_MATCH-MIN_MATCH+1];
+/* length code for each normalized match length (0 == MIN_MATCH) */
+
+local int base_length[LENGTH_CODES];
+/* First normalized length for each code (0 = MIN_MATCH) */
+
+local int base_dist[D_CODES];
+/* First normalized distance for each code (0 = distance of 1) */
+
+struct static_tree_desc_s {
+    ct_data *static_tree;        /* static tree or NULL */
+    intf    *extra_bits;         /* extra bits for each code or NULL */
+    int     extra_base;          /* base index for extra_bits */
+    int     elems;               /* max number of elements in the tree */
+    int     max_length;          /* max bit length for the codes */
+};
+
+local static_tree_desc  static_l_desc =
+{static_ltree, extra_lbits, LITERALS+1, L_CODES, MAX_BITS};
+
+local static_tree_desc  static_d_desc =
+{static_dtree, extra_dbits, 0,          D_CODES, MAX_BITS};
+
+local static_tree_desc  static_bl_desc =
+{(ct_data *)0, extra_blbits, 0,      BL_CODES, MAX_BL_BITS};
+
+/* ===========================================================================
+ * Local (static) routines in this file.
+ */
+
+local void tr_static_init OF((void));
+local void init_block     OF((deflate_state *s));
+local void pqdownheap     OF((deflate_state *s, ct_data *tree, int k));
+local void gen_bitlen     OF((deflate_state *s, tree_desc *desc));
+local void gen_codes      OF((ct_data *tree, int max_code, ushf *bl_count));
+local void build_tree     OF((deflate_state *s, tree_desc *desc));
+local void scan_tree      OF((deflate_state *s, ct_data *tree, int max_code));
+local void send_tree      OF((deflate_state *s, ct_data *tree, int max_code));
+local int  build_bl_tree  OF((deflate_state *s));
+local void send_all_trees OF((deflate_state *s, int lcodes, int dcodes,
+                              int blcodes));
+local void compress_block OF((deflate_state *s, ct_data *ltree,
+                              ct_data *dtree));
+local void set_data_type  OF((deflate_state *s));
+local unsigned bi_reverse OF((unsigned value, int length));
+local void bi_windup      OF((deflate_state *s));
+local void bi_flush       OF((deflate_state *s));
+local void copy_block     OF((deflate_state *s, charf *buf, unsigned len,
+                              int header));
+
+#ifndef DEBUG_ZLIB
+#  define send_code(s, c, tree) send_bits(s, tree[c].Code, tree[c].Len)
+   /* Send a code of the given tree. c and tree must not have side effects */
+
+#else /* DEBUG_ZLIB */
+#  define send_code(s, c, tree) \
+     { if (verbose>2) fprintf(stderr,"\ncd %3d ",(c)); \
+       send_bits(s, tree[c].Code, tree[c].Len); }
+#endif
+
+#define d_code(dist) \
+   ((dist) < 256 ? dist_code[dist] : dist_code[256+((dist)>>7)])
+/* Mapping from a distance to a distance code. dist is the distance - 1 and
+ * must not have side effects. dist_code[256] and dist_code[257] are never
+ * used.
+ */
+
+/* ===========================================================================
+ * Output a short LSB first on the stream.
+ * IN assertion: there is enough room in pendingBuf.
+ */
+#define put_short(s, w) { \
+    put_byte(s, (uch)((w) & 0xff)); \
+    put_byte(s, (uch)((ush)(w) >> 8)); \
+}
+
+/* ===========================================================================
+ * Send a value on a given number of bits.
+ * IN assertion: length <= 16 and value fits in length bits.
+ */
+#ifdef DEBUG_ZLIB
+local void send_bits      OF((deflate_state *s, int value, int length));
+
+local void send_bits(s, value, length)
+    deflate_state *s;
+    int value;  /* value to send */
+    int length; /* number of bits */
+{
+    Tracevv((stderr," l %2d v %4x ", length, value));
+    Assert(length > 0 && length <= 15, "invalid length");
+    s->bits_sent += (ulg)length;
+
+    /* If not enough room in bi_buf, use (valid) bits from bi_buf and
+     * (16 - bi_valid) bits from value, leaving (width - (16-bi_valid))
+     * unused bits in value.
+     */
+    if (s->bi_valid > (int)Buf_size - length) {
+        s->bi_buf |= (value << s->bi_valid);
+        put_short(s, s->bi_buf);
+        s->bi_buf = (ush)value >> (Buf_size - s->bi_valid);
+        s->bi_valid += length - Buf_size;
+    } else {
+        s->bi_buf |= value << s->bi_valid;
+        s->bi_valid += length;
+    }
+}
+#else /* !DEBUG_ZLIB */
+
+#define send_bits(s, value, length) \
+{ int len = length;\
+  if (s->bi_valid > (int)Buf_size - len) {\
+    int val = value;\
+    s->bi_buf |= (val << s->bi_valid);\
+    put_short(s, s->bi_buf);\
+    s->bi_buf = (ush)val >> (Buf_size - s->bi_valid);\
+    s->bi_valid += len - Buf_size;\
+  } else {\
+    s->bi_buf |= (value) << s->bi_valid;\
+    s->bi_valid += len;\
+  }\
+}
+#endif /* DEBUG_ZLIB */
+
+
+#define MAX(a,b) (a >= b ? a : b)
+/* the arguments must not have side effects */
+
+/* ===========================================================================
+ * Initialize the various 'constant' tables. In a multi-threaded environment,
+ * this function may be called by two threads concurrently, but this is
+ * harmless since both invocations do exactly the same thing.
+ */
+local void tr_static_init()
+{
+    static int static_init_done = 0;
+    int n;        /* iterates over tree elements */
+    int bits;     /* bit counter */
+    int length;   /* length value */
+    int code;     /* code value */
+    int dist;     /* distance index */
+    ush bl_count[MAX_BITS+1];
+    /* number of codes at each bit length for an optimal tree */
+
+    if (static_init_done) return;
+
+    /* Initialize the mapping length (0..255) -> length code (0..28) */
+    length = 0;
+    for (code = 0; code < LENGTH_CODES-1; code++) {
+        base_length[code] = length;
+        for (n = 0; n < (1<<extra_lbits[code]); n++) {
+            length_code[length++] = (uch)code;
+        }
+    }
+    Assert (length == 256, "tr_static_init: length != 256");
+    /* Note that the length 255 (match length 258) can be represented
+     * in two different ways: code 284 + 5 bits or code 285, so we
+     * overwrite length_code[255] to use the best encoding:
+     */
+    length_code[length-1] = (uch)code;
+
+    /* Initialize the mapping dist (0..32K) -> dist code (0..29) */
+    dist = 0;
+    for (code = 0 ; code < 16; code++) {
+        base_dist[code] = dist;
+        for (n = 0; n < (1<<extra_dbits[code]); n++) {
+            dist_code[dist++] = (uch)code;
+        }
+    }
+    Assert (dist == 256, "tr_static_init: dist != 256");
+    dist >>= 7; /* from now on, all distances are divided by 128 */
+    for ( ; code < D_CODES; code++) {
+        base_dist[code] = dist << 7;
+        for (n = 0; n < (1<<(extra_dbits[code]-7)); n++) {
+            dist_code[256 + dist++] = (uch)code;
+        }
+    }
+    Assert (dist == 256, "tr_static_init: 256+dist != 512");
+
+    /* Construct the codes of the static literal tree */
+    for (bits = 0; bits <= MAX_BITS; bits++) bl_count[bits] = 0;
+    n = 0;
+    while (n <= 143) static_ltree[n++].Len = 8, bl_count[8]++;
+    while (n <= 255) static_ltree[n++].Len = 9, bl_count[9]++;
+    while (n <= 279) static_ltree[n++].Len = 7, bl_count[7]++;
+    while (n <= 287) static_ltree[n++].Len = 8, bl_count[8]++;
+    /* Codes 286 and 287 do not exist, but we must include them in the
+     * tree construction to get a canonical Huffman tree (longest code
+     * all ones)
+     */
+    gen_codes((ct_data *)static_ltree, L_CODES+1, bl_count);
+
+    /* The static distance tree is trivial: */
+    for (n = 0; n < D_CODES; n++) {
+        static_dtree[n].Len = 5;
+        static_dtree[n].Code = bi_reverse((unsigned)n, 5);
+    }
+    static_init_done = 1;
+}
+
+/* ===========================================================================
+ * Initialize the tree data structures for a new zlib stream.
+ */
+void _tr_init(s)
+    deflate_state *s;
+{
+    tr_static_init();
+
+    s->compressed_len = 0L;
+
+    s->l_desc.dyn_tree = s->dyn_ltree;
+    s->l_desc.stat_desc = &static_l_desc;
+
+    s->d_desc.dyn_tree = s->dyn_dtree;
+    s->d_desc.stat_desc = &static_d_desc;
+
+    s->bl_desc.dyn_tree = s->bl_tree;
+    s->bl_desc.stat_desc = &static_bl_desc;
+
+    s->bi_buf = 0;
+    s->bi_valid = 0;
+    s->last_eob_len = 8; /* enough lookahead for inflate */
+#ifdef DEBUG_ZLIB
+    s->bits_sent = 0L;
+#endif
+
+    /* Initialize the first block of the first file: */
+    init_block(s);
+}
+
+/* ===========================================================================
+ * Initialize a new block.
+ */
+local void init_block(s)
+    deflate_state *s;
+{
+    int n; /* iterates over tree elements */
+
+    /* Initialize the trees. */
+    for (n = 0; n < L_CODES;  n++) s->dyn_ltree[n].Freq = 0;
+    for (n = 0; n < D_CODES;  n++) s->dyn_dtree[n].Freq = 0;
+    for (n = 0; n < BL_CODES; n++) s->bl_tree[n].Freq = 0;
+
+    s->dyn_ltree[END_BLOCK].Freq = 1;
+    s->opt_len = s->static_len = 0L;
+    s->last_lit = s->matches = 0;
+}
+
+#define SMALLEST 1
+/* Index within the heap array of least frequent node in the Huffman tree */
+
+
+/* ===========================================================================
+ * Remove the smallest element from the heap and recreate the heap with
+ * one less element. Updates heap and heap_len.
+ */
+#define pqremove(s, tree, top) \
+{\
+    top = s->heap[SMALLEST]; \
+    s->heap[SMALLEST] = s->heap[s->heap_len--]; \
+    pqdownheap(s, tree, SMALLEST); \
+}
+
+/* ===========================================================================
+ * Compares to subtrees, using the tree depth as tie breaker when
+ * the subtrees have equal frequency. This minimizes the worst case length.
+ */
+#define smaller(tree, n, m, depth) \
+   (tree[n].Freq < tree[m].Freq || \
+   (tree[n].Freq == tree[m].Freq && depth[n] <= depth[m]))
+
+/* ===========================================================================
+ * Restore the heap property by moving down the tree starting at node k,
+ * exchanging a node with the smallest of its two sons if necessary, stopping
+ * when the heap property is re-established (each father smaller than its
+ * two sons).
+ */
+local void pqdownheap(s, tree, k)
+    deflate_state *s;
+    ct_data *tree;  /* the tree to restore */
+    int k;               /* node to move down */
+{
+    int v = s->heap[k];
+    int j = k << 1;  /* left son of k */
+    while (j <= s->heap_len) {
+        /* Set j to the smallest of the two sons: */
+        if (j < s->heap_len &&
+            smaller(tree, s->heap[j+1], s->heap[j], s->depth)) {
+            j++;
+        }
+        /* Exit if v is smaller than both sons */
+        if (smaller(tree, v, s->heap[j], s->depth)) break;
+
+        /* Exchange v with the smallest son */
+        s->heap[k] = s->heap[j];  k = j;
+
+        /* And continue down the tree, setting j to the left son of k */
+        j <<= 1;
+    }
+    s->heap[k] = v;
+}
+
+/* ===========================================================================
+ * Compute the optimal bit lengths for a tree and update the total bit length
+ * for the current block.
+ * IN assertion: the fields freq and dad are set, heap[heap_max] and
+ *    above are the tree nodes sorted by increasing frequency.
+ * OUT assertions: the field len is set to the optimal bit length, the
+ *     array bl_count contains the frequencies for each bit length.
+ *     The length opt_len is updated; static_len is also updated if stree is
+ *     not null.
+ */
+local void gen_bitlen(s, desc)
+    deflate_state *s;
+    tree_desc *desc;    /* the tree descriptor */
+{
+    ct_data *tree  = desc->dyn_tree;
+    int max_code   = desc->max_code;
+    ct_data *stree = desc->stat_desc->static_tree;
+    intf *extra    = desc->stat_desc->extra_bits;
+    int base       = desc->stat_desc->extra_base;
+    int max_length = desc->stat_desc->max_length;
+    int h;              /* heap index */
+    int n, m;           /* iterate over the tree elements */
+    int bits;           /* bit length */
+    int xbits;          /* extra bits */
+    ush f;              /* frequency */
+    int overflow = 0;   /* number of elements with bit length too large */
+
+    for (bits = 0; bits <= MAX_BITS; bits++) s->bl_count[bits] = 0;
+
+    /* In a first pass, compute the optimal bit lengths (which may
+     * overflow in the case of the bit length tree).
+     */
+    tree[s->heap[s->heap_max]].Len = 0; /* root of the heap */
+
+    for (h = s->heap_max+1; h < HEAP_SIZE; h++) {
+        n = s->heap[h];
+        bits = tree[tree[n].Dad].Len + 1;
+        if (bits > max_length) bits = max_length, overflow++;
+        tree[n].Len = (ush)bits;
+        /* We overwrite tree[n].Dad which is no longer needed */
+
+        if (n > max_code) continue; /* not a leaf node */
+
+        s->bl_count[bits]++;
+        xbits = 0;
+        if (n >= base) xbits = extra[n-base];
+        f = tree[n].Freq;
+        s->opt_len += (ulg)f * (bits + xbits);
+        if (stree) s->static_len += (ulg)f * (stree[n].Len + xbits);
+    }
+    if (overflow == 0) return;
+
+    Trace((stderr,"\nbit length overflow\n"));
+    /* This happens for example on obj2 and pic of the Calgary corpus */
+
+    /* Find the first bit length which could increase: */
+    do {
+        bits = max_length-1;
+        while (s->bl_count[bits] == 0) bits--;
+        s->bl_count[bits]--;      /* move one leaf down the tree */
+        s->bl_count[bits+1] += 2; /* move one overflow item as its brother */
+        s->bl_count[max_length]--;
+        /* The brother of the overflow item also moves one step up,
+         * but this does not affect bl_count[max_length]
+         */
+        overflow -= 2;
+    } while (overflow > 0);
+
+    /* Now recompute all bit lengths, scanning in increasing frequency.
+     * h is still equal to HEAP_SIZE. (It is simpler to reconstruct all
+     * lengths instead of fixing only the wrong ones. This idea is taken
+     * from 'ar' written by Haruhiko Okumura.)
+     */
+    for (bits = max_length; bits != 0; bits--) {
+        n = s->bl_count[bits];
+        while (n != 0) {
+            m = s->heap[--h];
+            if (m > max_code) continue;
+            if (tree[m].Len != (unsigned) bits) {
+                Trace((stderr,"code %d bits %d->%d\n", m, tree[m].Len, bits));
+                s->opt_len += ((long)bits - (long)tree[m].Len)
+                              *(long)tree[m].Freq;
+                tree[m].Len = (ush)bits;
+            }
+            n--;
+        }
+    }
+}
+
+/* ===========================================================================
+ * Generate the codes for a given tree and bit counts (which need not be
+ * optimal).
+ * IN assertion: the array bl_count contains the bit length statistics for
+ * the given tree and the field len is set for all tree elements.
+ * OUT assertion: the field code is set for all tree elements of non
+ *     zero code length.
+ */
+local void gen_codes (tree, max_code, bl_count)
+    ct_data *tree;             /* the tree to decorate */
+    int max_code;              /* largest code with non zero frequency */
+    ushf *bl_count;            /* number of codes at each bit length */
+{
+    ush next_code[MAX_BITS+1]; /* next code value for each bit length */
+    ush code = 0;              /* running code value */
+    int bits;                  /* bit index */
+    int n;                     /* code index */
+
+    /* The distribution counts are first used to generate the code values
+     * without bit reversal.
+     */
+    for (bits = 1; bits <= MAX_BITS; bits++) {
+        next_code[bits] = code = (code + bl_count[bits-1]) << 1;
+    }
+    /* Check that the bit counts in bl_count are consistent. The last code
+     * must be all ones.
+     */
+    Assert (code + bl_count[MAX_BITS]-1 == (1<<MAX_BITS)-1,
+            "inconsistent bit counts");
+    Tracev((stderr,"\ngen_codes: max_code %d ", max_code));
+
+    for (n = 0;  n <= max_code; n++) {
+        int len = tree[n].Len;
+        if (len == 0) continue;
+        /* Now reverse the bits */
+        tree[n].Code = bi_reverse(next_code[len]++, len);
+
+        Tracecv(tree != static_ltree, (stderr,"\nn %3d %c l %2d c %4x (%x) ",
+             n, (isgraph(n) ? n : ' '), len, tree[n].Code, next_code[len]-1));
+    }
+}
+
+/* ===========================================================================
+ * Construct one Huffman tree and assigns the code bit strings and lengths.
+ * Update the total bit length for the current block.
+ * IN assertion: the field freq is set for all tree elements.
+ * OUT assertions: the fields len and code are set to the optimal bit length
+ *     and corresponding code. The length opt_len is updated; static_len is
+ *     also updated if stree is not null. The field max_code is set.
+ */
+local void build_tree(s, desc)
+    deflate_state *s;
+    tree_desc *desc; /* the tree descriptor */
+{
+    ct_data *tree   = desc->dyn_tree;
+    ct_data *stree  = desc->stat_desc->static_tree;
+    int elems       = desc->stat_desc->elems;
+    int n, m;          /* iterate over heap elements */
+    int max_code = -1; /* largest code with non zero frequency */
+    int node;          /* new node being created */
+
+    /* Construct the initial heap, with least frequent element in
+     * heap[SMALLEST]. The sons of heap[n] are heap[2*n] and heap[2*n+1].
+     * heap[0] is not used.
+     */
+    s->heap_len = 0, s->heap_max = HEAP_SIZE;
+
+    for (n = 0; n < elems; n++) {
+        if (tree[n].Freq != 0) {
+            s->heap[++(s->heap_len)] = max_code = n;
+            s->depth[n] = 0;
+        } else {
+            tree[n].Len = 0;
+        }
+    }
+
+    /* The pkzip format requires that at least one distance code exists,
+     * and that at least one bit should be sent even if there is only one
+     * possible code. So to avoid special checks later on we force at least
+     * two codes of non zero frequency.
+     */
+    while (s->heap_len < 2) {
+        node = s->heap[++(s->heap_len)] = (max_code < 2 ? ++max_code : 0);
+        tree[node].Freq = 1;
+        s->depth[node] = 0;
+        s->opt_len--; if (stree) s->static_len -= stree[node].Len;
+        /* node is 0 or 1 so it does not have extra bits */
+    }
+    desc->max_code = max_code;
+
+    /* The elements heap[heap_len/2+1 .. heap_len] are leaves of the tree,
+     * establish sub-heaps of increasing lengths:
+     */
+    for (n = s->heap_len/2; n >= 1; n--) pqdownheap(s, tree, n);
+
+    /* Construct the Huffman tree by repeatedly combining the least two
+     * frequent nodes.
+     */
+    node = elems;              /* next internal node of the tree */
+    do {
+        pqremove(s, tree, n);  /* n = node of least frequency */
+        m = s->heap[SMALLEST]; /* m = node of next least frequency */
+
+        s->heap[--(s->heap_max)] = n; /* keep the nodes sorted by frequency */
+        s->heap[--(s->heap_max)] = m;
+
+        /* Create a new node father of n and m */
+        tree[node].Freq = tree[n].Freq + tree[m].Freq;
+        s->depth[node] = (uch) (MAX(s->depth[n], s->depth[m]) + 1);
+        tree[n].Dad = tree[m].Dad = (ush)node;
+#ifdef DUMP_BL_TREE
+        if (tree == s->bl_tree) {
+            fprintf(stderr,"\nnode %d(%d), sons %d(%d) %d(%d)",
+                    node, tree[node].Freq, n, tree[n].Freq, m, tree[m].Freq);
+        }
+#endif
+        /* and insert the new node in the heap */
+        s->heap[SMALLEST] = node++;
+        pqdownheap(s, tree, SMALLEST);
+
+    } while (s->heap_len >= 2);
+
+    s->heap[--(s->heap_max)] = s->heap[SMALLEST];
+
+    /* At this point, the fields freq and dad are set. We can now
+     * generate the bit lengths.
+     */
+    gen_bitlen(s, (tree_desc *)desc);
+
+    /* The field len is now set, we can generate the bit codes */
+    gen_codes ((ct_data *)tree, max_code, s->bl_count);
+}
+
+/* ===========================================================================
+ * Scan a literal or distance tree to determine the frequencies of the codes
+ * in the bit length tree.
+ */
+local void scan_tree (s, tree, max_code)
+    deflate_state *s;
+    ct_data *tree;   /* the tree to be scanned */
+    int max_code;    /* and its largest code of non zero frequency */
+{
+    int n;                     /* iterates over all tree elements */
+    int prevlen = -1;          /* last emitted length */
+    int curlen;                /* length of current code */
+    int nextlen = tree[0].Len; /* length of next code */
+    int count = 0;             /* repeat count of the current code */
+    int max_count = 7;         /* max repeat count */
+    int min_count = 4;         /* min repeat count */
+
+    if (nextlen == 0) max_count = 138, min_count = 3;
+    tree[max_code+1].Len = (ush)0xffff; /* guard */
+
+    for (n = 0; n <= max_code; n++) {
+        curlen = nextlen; nextlen = tree[n+1].Len;
+        if (++count < max_count && curlen == nextlen) {
+            continue;
+        } else if (count < min_count) {
+            s->bl_tree[curlen].Freq += count;
+        } else if (curlen != 0) {
+            if (curlen != prevlen) s->bl_tree[curlen].Freq++;
+            s->bl_tree[REP_3_6].Freq++;
+        } else if (count <= 10) {
+            s->bl_tree[REPZ_3_10].Freq++;
+        } else {
+            s->bl_tree[REPZ_11_138].Freq++;
+        }
+        count = 0; prevlen = curlen;
+        if (nextlen == 0) {
+            max_count = 138, min_count = 3;
+        } else if (curlen == nextlen) {
+            max_count = 6, min_count = 3;
+        } else {
+            max_count = 7, min_count = 4;
+        }
+    }
+}
+
+/* ===========================================================================
+ * Send a literal or distance tree in compressed form, using the codes in
+ * bl_tree.
+ */
+local void send_tree (s, tree, max_code)
+    deflate_state *s;
+    ct_data *tree; /* the tree to be scanned */
+    int max_code;       /* and its largest code of non zero frequency */
+{
+    int n;                     /* iterates over all tree elements */
+    int prevlen = -1;          /* last emitted length */
+    int curlen;                /* length of current code */
+    int nextlen = tree[0].Len; /* length of next code */
+    int count = 0;             /* repeat count of the current code */
+    int max_count = 7;         /* max repeat count */
+    int min_count = 4;         /* min repeat count */
+
+    /* tree[max_code+1].Len = -1; */  /* guard already set */
+    if (nextlen == 0) max_count = 138, min_count = 3;
+
+    for (n = 0; n <= max_code; n++) {
+        curlen = nextlen; nextlen = tree[n+1].Len;
+        if (++count < max_count && curlen == nextlen) {
+            continue;
+        } else if (count < min_count) {
+            do { send_code(s, curlen, s->bl_tree); } while (--count != 0);
+
+        } else if (curlen != 0) {
+            if (curlen != prevlen) {
+                send_code(s, curlen, s->bl_tree); count--;
+            }
+            Assert(count >= 3 && count <= 6, " 3_6?");
+            send_code(s, REP_3_6, s->bl_tree); send_bits(s, count-3, 2);
+
+        } else if (count <= 10) {
+            send_code(s, REPZ_3_10, s->bl_tree); send_bits(s, count-3, 3);
+
+        } else {
+            send_code(s, REPZ_11_138, s->bl_tree); send_bits(s, count-11, 7);
+        }
+        count = 0; prevlen = curlen;
+        if (nextlen == 0) {
+            max_count = 138, min_count = 3;
+        } else if (curlen == nextlen) {
+            max_count = 6, min_count = 3;
+        } else {
+            max_count = 7, min_count = 4;
+        }
+    }
+}
+
+/* ===========================================================================
+ * Construct the Huffman tree for the bit lengths and return the index in
+ * bl_order of the last bit length code to send.
+ */
+local int build_bl_tree(s)
+    deflate_state *s;
+{
+    int max_blindex;  /* index of last bit length code of non zero freq */
+
+    /* Determine the bit length frequencies for literal and distance trees */
+    scan_tree(s, (ct_data *)s->dyn_ltree, s->l_desc.max_code);
+    scan_tree(s, (ct_data *)s->dyn_dtree, s->d_desc.max_code);
+
+    /* Build the bit length tree: */
+    build_tree(s, (tree_desc *)(&(s->bl_desc)));
+    /* opt_len now includes the length of the tree representations, except
+     * the lengths of the bit lengths codes and the 5+5+4 bits for the counts.
+     */
+
+    /* Determine the number of bit length codes to send. The pkzip format
+     * requires that at least 4 bit length codes be sent. (appnote.txt says
+     * 3 but the actual value used is 4.)
+     */
+    for (max_blindex = BL_CODES-1; max_blindex >= 3; max_blindex--) {
+        if (s->bl_tree[bl_order[max_blindex]].Len != 0) break;
+    }
+    /* Update opt_len to include the bit length tree and counts */
+    s->opt_len += 3*(max_blindex+1) + 5+5+4;
+    Tracev((stderr, "\ndyn trees: dyn %ld, stat %ld",
+            s->opt_len, s->static_len));
+
+    return max_blindex;
+}
+
+/* ===========================================================================
+ * Send the header for a block using dynamic Huffman trees: the counts, the
+ * lengths of the bit length codes, the literal tree and the distance tree.
+ * IN assertion: lcodes >= 257, dcodes >= 1, blcodes >= 4.
+ */
+local void send_all_trees(s, lcodes, dcodes, blcodes)
+    deflate_state *s;
+    int lcodes, dcodes, blcodes; /* number of codes for each tree */
+{
+    int rank;                    /* index in bl_order */
+
+    Assert (lcodes >= 257 && dcodes >= 1 && blcodes >= 4, "not enough codes");
+    Assert (lcodes <= L_CODES && dcodes <= D_CODES && blcodes <= BL_CODES,
+            "too many codes");
+    Tracev((stderr, "\nbl counts: "));
+    send_bits(s, lcodes-257, 5); /* not +255 as stated in appnote.txt */
+    send_bits(s, dcodes-1,   5);
+    send_bits(s, blcodes-4,  4); /* not -3 as stated in appnote.txt */
+    for (rank = 0; rank < blcodes; rank++) {
+        Tracev((stderr, "\nbl code %2d ", bl_order[rank]));
+        send_bits(s, s->bl_tree[bl_order[rank]].Len, 3);
+    }
+    Tracev((stderr, "\nbl tree: sent %ld", s->bits_sent));
+
+    send_tree(s, (ct_data *)s->dyn_ltree, lcodes-1); /* literal tree */
+    Tracev((stderr, "\nlit tree: sent %ld", s->bits_sent));
+
+    send_tree(s, (ct_data *)s->dyn_dtree, dcodes-1); /* distance tree */
+    Tracev((stderr, "\ndist tree: sent %ld", s->bits_sent));
+}
+
+/* ===========================================================================
+ * Send a stored block
+ */
+void _tr_stored_block(s, buf, stored_len, eof)
+    deflate_state *s;
+    charf *buf;       /* input block */
+    ulg stored_len;   /* length of input block */
+    int eof;          /* true if this is the last block for a file */
+{
+    send_bits(s, (STORED_BLOCK<<1)+eof, 3);  /* send block type */
+    s->compressed_len = (s->compressed_len + 3 + 7) & (ulg)~7L;
+    s->compressed_len += (stored_len + 4) << 3;
+
+    copy_block(s, buf, (unsigned)stored_len, 1); /* with header */
+}
+
+/* Send just the `stored block' type code without any length bytes or data.
+ */
+void _tr_stored_type_only(s)
+    deflate_state *s;
+{
+    send_bits(s, (STORED_BLOCK << 1), 3);
+    bi_windup(s);
+    s->compressed_len = (s->compressed_len + 3) & ~7L;
+}
+
+
+/* ===========================================================================
+ * Send one empty static block to give enough lookahead for inflate.
+ * This takes 10 bits, of which 7 may remain in the bit buffer.
+ * The current inflate code requires 9 bits of lookahead. If the
+ * last two codes for the previous block (real code plus EOB) were coded
+ * on 5 bits or less, inflate may have only 5+3 bits of lookahead to decode
+ * the last real code. In this case we send two empty static blocks instead
+ * of one. (There are no problems if the previous block is stored or fixed.)
+ * To simplify the code, we assume the worst case of last real code encoded
+ * on one bit only.
+ */
+void _tr_align(s)
+    deflate_state *s;
+{
+    send_bits(s, STATIC_TREES<<1, 3);
+    send_code(s, END_BLOCK, static_ltree);
+    s->compressed_len += 10L; /* 3 for block type, 7 for EOB */
+    bi_flush(s);
+    /* Of the 10 bits for the empty block, we have already sent
+     * (10 - bi_valid) bits. The lookahead for the last real code (before
+     * the EOB of the previous block) was thus at least one plus the length
+     * of the EOB plus what we have just sent of the empty static block.
+     */
+    if (1 + s->last_eob_len + 10 - s->bi_valid < 9) {
+        send_bits(s, STATIC_TREES<<1, 3);
+        send_code(s, END_BLOCK, static_ltree);
+        s->compressed_len += 10L;
+        bi_flush(s);
+    }
+    s->last_eob_len = 7;
+}
+
+/* ===========================================================================
+ * Determine the best encoding for the current block: dynamic trees, static
+ * trees or store, and output the encoded block to the zip file. This function
+ * returns the total compressed length for the file so far.
+ */
+ulg _tr_flush_block(s, buf, stored_len, eof)
+    deflate_state *s;
+    charf *buf;       /* input block, or NULL if too old */
+    ulg stored_len;   /* length of input block */
+    int eof;          /* true if this is the last block for a file */
+{
+    ulg opt_lenb, static_lenb; /* opt_len and static_len in bytes */
+    int max_blindex = 0;  /* index of last bit length code of non zero freq */
+
+    /* Build the Huffman trees unless a stored block is forced */
+    if (s->level > 0) {
+
+        /* Check if the file is ascii or binary */
+       if (s->data_type == Z_UNKNOWN) set_data_type(s);
+
+       /* Construct the literal and distance trees */
+       build_tree(s, (tree_desc *)(&(s->l_desc)));
+       Tracev((stderr, "\nlit data: dyn %ld, stat %ld", s->opt_len,
+               s->static_len));
+
+       build_tree(s, (tree_desc *)(&(s->d_desc)));
+       Tracev((stderr, "\ndist data: dyn %ld, stat %ld", s->opt_len,
+               s->static_len));
+       /* At this point, opt_len and static_len are the total bit lengths of
+        * the compressed block data, excluding the tree representations.
+        */
+
+       /* Build the bit length tree for the above two trees, and get the index
+        * in bl_order of the last bit length code to send.
+        */
+       max_blindex = build_bl_tree(s);
+
+       /* Determine the best encoding. Compute first the block length in bytes*/
+       opt_lenb = (s->opt_len+3+7)>>3;
+       static_lenb = (s->static_len+3+7)>>3;
+
+       Tracev((stderr, "\nopt %lu(%lu) stat %lu(%lu) stored %lu lit %u ",
+               opt_lenb, s->opt_len, static_lenb, s->static_len, stored_len,
+               s->last_lit));
+
+       if (static_lenb <= opt_lenb) opt_lenb = static_lenb;
+
+    } else {
+        Assert(buf != (char*)0, "lost buf");
+       opt_lenb = static_lenb = stored_len + 5; /* force a stored block */
+    }
+
+    /* If compression failed and this is the first and last block,
+     * and if the .zip file can be seeked (to rewrite the local header),
+     * the whole file is transformed into a stored file:
+     */
+#ifdef STORED_FILE_OK
+#  ifdef FORCE_STORED_FILE
+    if (eof && s->compressed_len == 0L) { /* force stored file */
+#  else
+    if (stored_len <= opt_lenb && eof && s->compressed_len==0L && seekable()) {
+#  endif
+        /* Since LIT_BUFSIZE <= 2*WSIZE, the input data must be there: */
+        if (buf == (charf*)0) error ("block vanished");
+
+        copy_block(s, buf, (unsigned)stored_len, 0); /* without header */
+        s->compressed_len = stored_len << 3;
+        s->method = STORED;
+    } else
+#endif /* STORED_FILE_OK */
+
+#ifdef FORCE_STORED
+    if (buf != (char*)0) { /* force stored block */
+#else
+    if (stored_len+4 <= opt_lenb && buf != (char*)0) {
+                       /* 4: two words for the lengths */
+#endif
+        /* The test buf != NULL is only necessary if LIT_BUFSIZE > WSIZE.
+         * Otherwise we can't have processed more than WSIZE input bytes since
+         * the last block flush, because compression would have been
+         * successful. If LIT_BUFSIZE <= WSIZE, it is never too late to
+         * transform a block into a stored block.
+         */
+        _tr_stored_block(s, buf, stored_len, eof);
+
+#ifdef FORCE_STATIC
+    } else if (static_lenb >= 0) { /* force static trees */
+#else
+    } else if (static_lenb == opt_lenb) {
+#endif
+        send_bits(s, (STATIC_TREES<<1)+eof, 3);
+        compress_block(s, (ct_data *)static_ltree, (ct_data *)static_dtree);
+        s->compressed_len += 3 + s->static_len;
+    } else {
+        send_bits(s, (DYN_TREES<<1)+eof, 3);
+        send_all_trees(s, s->l_desc.max_code+1, s->d_desc.max_code+1,
+                       max_blindex+1);
+        compress_block(s, (ct_data *)s->dyn_ltree, (ct_data *)s->dyn_dtree);
+        s->compressed_len += 3 + s->opt_len;
+    }
+    Assert (s->compressed_len == s->bits_sent, "bad compressed size");
+    init_block(s);
+
+    if (eof) {
+        bi_windup(s);
+        s->compressed_len += 7;  /* align on byte boundary */
+    }
+    Tracev((stderr,"\ncomprlen %lu(%lu) ", s->compressed_len>>3,
+           s->compressed_len-7*eof));
+
+    return s->compressed_len >> 3;
+}
+
+/* ===========================================================================
+ * Save the match info and tally the frequency counts. Return true if
+ * the current block must be flushed.
+ */
+int _tr_tally (s, dist, lc)
+    deflate_state *s;
+    unsigned dist;  /* distance of matched string */
+    unsigned lc;    /* match length-MIN_MATCH or unmatched char (if dist==0) */
+{
+    s->d_buf[s->last_lit] = (ush)dist;
+    s->l_buf[s->last_lit++] = (uch)lc;
+    if (dist == 0) {
+        /* lc is the unmatched char */
+        s->dyn_ltree[lc].Freq++;
+    } else {
+        s->matches++;
+        /* Here, lc is the match length - MIN_MATCH */
+        dist--;             /* dist = match distance - 1 */
+        Assert((ush)dist < (ush)MAX_DIST(s) &&
+               (ush)lc <= (ush)(MAX_MATCH-MIN_MATCH) &&
+               (ush)d_code(dist) < (ush)D_CODES,  "_tr_tally: bad match");
+
+        s->dyn_ltree[length_code[lc]+LITERALS+1].Freq++;
+        s->dyn_dtree[d_code(dist)].Freq++;
+    }
+
+    /* Try to guess if it is profitable to stop the current block here */
+    if (s->level > 2 && (s->last_lit & 0xfff) == 0) {
+        /* Compute an upper bound for the compressed length */
+        ulg out_length = (ulg)s->last_lit*8L;
+        ulg in_length = (ulg)((long)s->strstart - s->block_start);
+        int dcode;
+        for (dcode = 0; dcode < D_CODES; dcode++) {
+            out_length += (ulg)s->dyn_dtree[dcode].Freq *
+                (5L+extra_dbits[dcode]);
+        }
+        out_length >>= 3;
+        Tracev((stderr,"\nlast_lit %u, in %ld, out ~%ld(%ld%%) ",
+               s->last_lit, in_length, out_length,
+               100L - out_length*100L/in_length));
+        if (s->matches < s->last_lit/2 && out_length < in_length/2) return 1;
+    }
+    return (s->last_lit == s->lit_bufsize-1);
+    /* We avoid equality with lit_bufsize because of wraparound at 64K
+     * on 16 bit machines and because stored blocks are restricted to
+     * 64K-1 bytes.
+     */
+}
+
+/* ===========================================================================
+ * Send the block data compressed using the given Huffman trees
+ */
+local void compress_block(s, ltree, dtree)
+    deflate_state *s;
+    ct_data *ltree; /* literal tree */
+    ct_data *dtree; /* distance tree */
+{
+    unsigned dist;      /* distance of matched string */
+    int lc;             /* match length or unmatched char (if dist == 0) */
+    unsigned lx = 0;    /* running index in l_buf */
+    unsigned code;      /* the code to send */
+    int extra;          /* number of extra bits to send */
+
+    if (s->last_lit != 0) do {
+        dist = s->d_buf[lx];
+        lc = s->l_buf[lx++];
+        if (dist == 0) {
+            send_code(s, lc, ltree); /* send a literal byte */
+            Tracecv(isgraph(lc), (stderr," '%c' ", lc));
+        } else {
+            /* Here, lc is the match length - MIN_MATCH */
+            code = length_code[lc];
+            send_code(s, code+LITERALS+1, ltree); /* send the length code */
+            extra = extra_lbits[code];
+            if (extra != 0) {
+                lc -= base_length[code];
+                send_bits(s, lc, extra);       /* send the extra length bits */
+            }
+            dist--; /* dist is now the match distance - 1 */
+            code = d_code(dist);
+            Assert (code < D_CODES, "bad d_code");
+
+            send_code(s, code, dtree);       /* send the distance code */
+            extra = extra_dbits[code];
+            if (extra != 0) {
+                dist -= base_dist[code];
+                send_bits(s, dist, extra);   /* send the extra distance bits */
+            }
+        } /* literal or match pair ? */
+
+        /* Check that the overlay between pending_buf and d_buf+l_buf is ok: */
+        Assert(s->pending < s->lit_bufsize + 2*lx, "pendingBuf overflow");
+
+    } while (lx < s->last_lit);
+
+    send_code(s, END_BLOCK, ltree);
+    s->last_eob_len = ltree[END_BLOCK].Len;
+}
+
+/* ===========================================================================
+ * Set the data type to ASCII or BINARY, using a crude approximation:
+ * binary if more than 20% of the bytes are <= 6 or >= 128, ascii otherwise.
+ * IN assertion: the fields freq of dyn_ltree are set and the total of all
+ * frequencies does not exceed 64K (to fit in an int on 16 bit machines).
+ */
+local void set_data_type(s)
+    deflate_state *s;
+{
+    int n = 0;
+    unsigned ascii_freq = 0;
+    unsigned bin_freq = 0;
+    while (n < 7)        bin_freq += s->dyn_ltree[n++].Freq;
+    while (n < 128)    ascii_freq += s->dyn_ltree[n++].Freq;
+    while (n < LITERALS) bin_freq += s->dyn_ltree[n++].Freq;
+    s->data_type = (Byte)(bin_freq > (ascii_freq >> 2) ? Z_BINARY : Z_ASCII);
+}
+
+/* ===========================================================================
+ * Reverse the first len bits of a code, using straightforward code (a faster
+ * method would use a table)
+ * IN assertion: 1 <= len <= 15
+ */
+local unsigned bi_reverse(code, len)
+    unsigned code; /* the value to invert */
+    int len;       /* its bit length */
+{
+    register unsigned res = 0;
+    do {
+        res |= code & 1;
+        code >>= 1, res <<= 1;
+    } while (--len > 0);
+    return res >> 1;
+}
+
+/* ===========================================================================
+ * Flush the bit buffer, keeping at most 7 bits in it.
+ */
+local void bi_flush(s)
+    deflate_state *s;
+{
+    if (s->bi_valid == 16) {
+        put_short(s, s->bi_buf);
+        s->bi_buf = 0;
+        s->bi_valid = 0;
+    } else if (s->bi_valid >= 8) {
+        put_byte(s, (Byte)s->bi_buf);
+        s->bi_buf >>= 8;
+        s->bi_valid -= 8;
+    }
+}
+
+/* ===========================================================================
+ * Flush the bit buffer and align the output on a byte boundary
+ */
+local void bi_windup(s)
+    deflate_state *s;
+{
+    if (s->bi_valid > 8) {
+        put_short(s, s->bi_buf);
+    } else if (s->bi_valid > 0) {
+        put_byte(s, (Byte)s->bi_buf);
+    }
+    s->bi_buf = 0;
+    s->bi_valid = 0;
+#ifdef DEBUG_ZLIB
+    s->bits_sent = (s->bits_sent+7) & ~7;
+#endif
+}
+
+/* ===========================================================================
+ * Copy a stored block, storing first the length and its
+ * one's complement if requested.
+ */
+local void copy_block(s, buf, len, header)
+    deflate_state *s;
+    charf    *buf;    /* the input data */
+    unsigned len;     /* its length */
+    int      header;  /* true if block header must be written */
+{
+    bi_windup(s);        /* align on byte boundary */
+    s->last_eob_len = 8; /* enough lookahead for inflate */
+
+    if (header) {
+        put_short(s, (ush)len);   
+        put_short(s, (ush)~len);
+#ifdef DEBUG_ZLIB
+        s->bits_sent += 2*16;
+#endif
+    }
+#ifdef DEBUG_ZLIB
+    s->bits_sent += (ulg)len<<3;
+#endif
+    /* bundle up the put_byte(s, *buf++) calls */
+    zmemcpy(&s->pending_buf[s->pending], buf, len);
+    s->pending += len;
+}
+/* --- trees.c */
+
+/* +++ inflate.c */
+/* inflate.c -- zlib interface to inflate modules
+ * Copyright (C) 1995-1996 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* #include "zutil.h" */
+
+/* +++ infblock.h */
+/* infblock.h -- header to use infblock.c
+ * Copyright (C) 1995-1996 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+   part of the implementation of the compression library and is
+   subject to change. Applications should only use zlib.h.
+ */
+
+struct inflate_blocks_state;
+typedef struct inflate_blocks_state FAR inflate_blocks_statef;
+
+extern inflate_blocks_statef * inflate_blocks_new OF((
+    z_streamp z,
+    check_func c,               /* check function */
+    uInt w));                   /* window size */
+
+extern int inflate_blocks OF((
+    inflate_blocks_statef *,
+    z_streamp ,
+    int));                      /* initial return code */
+
+extern void inflate_blocks_reset OF((
+    inflate_blocks_statef *,
+    z_streamp ,
+    uLongf *));                  /* check value on output */
+
+extern int inflate_blocks_free OF((
+    inflate_blocks_statef *,
+    z_streamp ,
+    uLongf *));                  /* check value on output */
+
+extern void inflate_set_dictionary OF((
+    inflate_blocks_statef *s,
+    const Bytef *d,  /* dictionary */
+    uInt  n));       /* dictionary length */
+
+extern int inflate_addhistory OF((
+    inflate_blocks_statef *,
+    z_streamp));
+
+extern int inflate_packet_flush OF((
+    inflate_blocks_statef *));
+/* --- infblock.h */
+
+#ifndef NO_DUMMY_DECL
+struct inflate_blocks_state {int dummy;}; /* for buggy compilers */
+#endif
+
+/* inflate private state */
+struct internal_state {
+
+  /* mode */
+  enum {
+      METHOD,   /* waiting for method byte */
+      FLAG,     /* waiting for flag byte */
+      DICT4,    /* four dictionary check bytes to go */
+      DICT3,    /* three dictionary check bytes to go */
+      DICT2,    /* two dictionary check bytes to go */
+      DICT1,    /* one dictionary check byte to go */
+      DICT0,    /* waiting for inflateSetDictionary */
+      BLOCKS,   /* decompressing blocks */
+      CHECK4,   /* four check bytes to go */
+      CHECK3,   /* three check bytes to go */
+      CHECK2,   /* two check bytes to go */
+      CHECK1,   /* one check byte to go */
+      DONE,     /* finished check, done */
+      BAD}      /* got an error--stay here */
+    mode;               /* current inflate mode */
+
+  /* mode dependent information */
+  union {
+    uInt method;        /* if FLAGS, method byte */
+    struct {
+      uLong was;                /* computed check value */
+      uLong need;               /* stream check value */
+    } check;            /* if CHECK, check values to compare */
+    uInt marker;        /* if BAD, inflateSync's marker bytes count */
+  } sub;        /* submode */
+
+  /* mode independent information */
+  int  nowrap;          /* flag for no wrapper */
+  uInt wbits;           /* log2(window size)  (8..15, defaults to 15) */
+  inflate_blocks_statef 
+    *blocks;            /* current inflate_blocks state */
+
+};
+
+
+int inflateReset(z)
+z_streamp z;
+{
+  uLong c;
+
+  if (z == Z_NULL || z->state == Z_NULL)
+    return Z_STREAM_ERROR;
+  z->total_in = z->total_out = 0;
+  z->msg = Z_NULL;
+  z->state->mode = z->state->nowrap ? BLOCKS : METHOD;
+  inflate_blocks_reset(z->state->blocks, z, &c);
+  Trace((stderr, "inflate: reset\n"));
+  return Z_OK;
+}
+
+
+int inflateEnd(z)
+z_streamp z;
+{
+  uLong c;
+
+  if (z == Z_NULL || z->state == Z_NULL || z->zfree == Z_NULL)
+    return Z_STREAM_ERROR;
+  if (z->state->blocks != Z_NULL)
+    inflate_blocks_free(z->state->blocks, z, &c);
+  ZFREE(z, z->state);
+  z->state = Z_NULL;
+  Trace((stderr, "inflate: end\n"));
+  return Z_OK;
+}
+
+
+int inflateInit2_(z, w, version, stream_size)
+z_streamp z;
+int w;
+const char *version;
+int stream_size;
+{
+  if (version == Z_NULL || version[0] != ZLIB_VERSION[0] ||
+      stream_size != sizeof(z_stream))
+      return Z_VERSION_ERROR;
+
+  /* initialize state */
+  if (z == Z_NULL)
+    return Z_STREAM_ERROR;
+  z->msg = Z_NULL;
+#ifndef NO_ZCFUNCS
+  if (z->zalloc == Z_NULL)
+  {
+    z->zalloc = zcalloc;
+    z->opaque = (voidpf)0;
+  }
+  if (z->zfree == Z_NULL) z->zfree = zcfree;
+#endif
+  if ((z->state = (struct internal_state FAR *)
+       ZALLOC(z,1,sizeof(struct internal_state))) == Z_NULL)
+    return Z_MEM_ERROR;
+  z->state->blocks = Z_NULL;
+
+  /* handle undocumented nowrap option (no zlib header or check) */
+  z->state->nowrap = 0;
+  if (w < 0)
+  {
+    w = - w;
+    z->state->nowrap = 1;
+  }
+
+  /* set window size */
+  if (w < 8 || w > 15)
+  {
+    inflateEnd(z);
+    return Z_STREAM_ERROR;
+  }
+  z->state->wbits = (uInt)w;
+
+  /* create inflate_blocks state */
+  if ((z->state->blocks =
+      inflate_blocks_new(z, z->state->nowrap ? Z_NULL : adler32, (uInt)1 << w))
+      == Z_NULL)
+  {
+    inflateEnd(z);
+    return Z_MEM_ERROR;
+  }
+  Trace((stderr, "inflate: allocated\n"));
+
+  /* reset state */
+  inflateReset(z);
+  return Z_OK;
+}
+
+
+int inflateInit_(z, version, stream_size)
+z_streamp z;
+const char *version;
+int stream_size;
+{
+  return inflateInit2_(z, DEF_WBITS, version, stream_size);
+}
+
+
+#define NEEDBYTE {if(z->avail_in==0)goto empty;r=Z_OK;}
+#define NEXTBYTE (z->avail_in--,z->total_in++,*z->next_in++)
+
+int inflate(z, f)
+z_streamp z;
+int f;
+{
+  int r;
+  uInt b;
+
+  if (z == Z_NULL || z->state == Z_NULL || z->next_in == Z_NULL || f < 0)
+    return Z_STREAM_ERROR;
+  r = Z_BUF_ERROR;
+  while (1) switch (z->state->mode)
+  {
+    case METHOD:
+      NEEDBYTE
+      if (((z->state->sub.method = NEXTBYTE) & 0xf) != Z_DEFLATED)
+      {
+        z->state->mode = BAD;
+        z->msg = (char*)"unknown compression method";
+        z->state->sub.marker = 5;       /* can't try inflateSync */
+        break;
+      }
+      if ((z->state->sub.method >> 4) + 8 > z->state->wbits)
+      {
+        z->state->mode = BAD;
+        z->msg = (char*)"invalid window size";
+        z->state->sub.marker = 5;       /* can't try inflateSync */
+        break;
+      }
+      z->state->mode = FLAG;
+    case FLAG:
+      NEEDBYTE
+      b = NEXTBYTE;
+      if (((z->state->sub.method << 8) + b) % 31)
+      {
+        z->state->mode = BAD;
+        z->msg = (char*)"incorrect header check";
+        z->state->sub.marker = 5;       /* can't try inflateSync */
+        break;
+      }
+      Trace((stderr, "inflate: zlib header ok\n"));
+      if (!(b & PRESET_DICT))
+      {
+        z->state->mode = BLOCKS;
+       break;
+      }
+      z->state->mode = DICT4;
+    case DICT4:
+      NEEDBYTE
+      z->state->sub.check.need = (uLong)NEXTBYTE << 24;
+      z->state->mode = DICT3;
+    case DICT3:
+      NEEDBYTE
+      z->state->sub.check.need += (uLong)NEXTBYTE << 16;
+      z->state->mode = DICT2;
+    case DICT2:
+      NEEDBYTE
+      z->state->sub.check.need += (uLong)NEXTBYTE << 8;
+      z->state->mode = DICT1;
+    case DICT1:
+      NEEDBYTE
+      z->state->sub.check.need += (uLong)NEXTBYTE;
+      z->adler = z->state->sub.check.need;
+      z->state->mode = DICT0;
+      return Z_NEED_DICT;
+    case DICT0:
+      z->state->mode = BAD;
+      z->msg = (char*)"need dictionary";
+      z->state->sub.marker = 0;       /* can try inflateSync */
+      return Z_STREAM_ERROR;
+    case BLOCKS:
+      r = inflate_blocks(z->state->blocks, z, r);
+      if (f == Z_PACKET_FLUSH && z->avail_in == 0 && z->avail_out != 0)
+         r = inflate_packet_flush(z->state->blocks);
+      if (r == Z_DATA_ERROR)
+      {
+        z->state->mode = BAD;
+        z->state->sub.marker = 0;       /* can try inflateSync */
+        break;
+      }
+      if (r != Z_STREAM_END)
+        return r;
+      r = Z_OK;
+      inflate_blocks_reset(z->state->blocks, z, &z->state->sub.check.was);
+      if (z->state->nowrap)
+      {
+        z->state->mode = DONE;
+        break;
+      }
+      z->state->mode = CHECK4;
+    case CHECK4:
+      NEEDBYTE
+      z->state->sub.check.need = (uLong)NEXTBYTE << 24;
+      z->state->mode = CHECK3;
+    case CHECK3:
+      NEEDBYTE
+      z->state->sub.check.need += (uLong)NEXTBYTE << 16;
+      z->state->mode = CHECK2;
+    case CHECK2:
+      NEEDBYTE
+      z->state->sub.check.need += (uLong)NEXTBYTE << 8;
+      z->state->mode = CHECK1;
+    case CHECK1:
+      NEEDBYTE
+      z->state->sub.check.need += (uLong)NEXTBYTE;
+
+      if (z->state->sub.check.was != z->state->sub.check.need)
+      {
+        z->state->mode = BAD;
+        z->msg = (char*)"incorrect data check";
+        z->state->sub.marker = 5;       /* can't try inflateSync */
+        break;
+      }
+      Trace((stderr, "inflate: zlib check ok\n"));
+      z->state->mode = DONE;
+    case DONE:
+      return Z_STREAM_END;
+    case BAD:
+      return Z_DATA_ERROR;
+    default:
+      return Z_STREAM_ERROR;
+  }
+
+ empty:
+  if (f != Z_PACKET_FLUSH)
+    return r;
+  z->state->mode = BAD;
+  z->msg = (char *)"need more for packet flush";
+  z->state->sub.marker = 0;       /* can try inflateSync */
+  return Z_DATA_ERROR;
+}
+
+
+int inflateSetDictionary(z, dictionary, dictLength)
+z_streamp z;
+const Bytef *dictionary;
+uInt  dictLength;
+{
+  uInt length = dictLength;
+
+  if (z == Z_NULL || z->state == Z_NULL || z->state->mode != DICT0)
+    return Z_STREAM_ERROR;
+
+  if (adler32(1L, dictionary, dictLength) != z->adler) return Z_DATA_ERROR;
+  z->adler = 1L;
+
+  if (length >= ((uInt)1<<z->state->wbits))
+  {
+    length = (1<<z->state->wbits)-1;
+    dictionary += dictLength - length;
+  }
+  inflate_set_dictionary(z->state->blocks, dictionary, length);
+  z->state->mode = BLOCKS;
+  return Z_OK;
+}
+
+/*
+ * This subroutine adds the data at next_in/avail_in to the output history
+ * without performing any output.  The output buffer must be "caught up";
+ * i.e. no pending output (hence s->read equals s->write), and the state must
+ * be BLOCKS (i.e. we should be willing to see the start of a series of
+ * BLOCKS).  On exit, the output will also be caught up, and the checksum
+ * will have been updated if need be.
+ */
+
+int inflateIncomp(z)
+z_stream *z;
+{
+    if (z->state->mode != BLOCKS)
+       return Z_DATA_ERROR;
+    return inflate_addhistory(z->state->blocks, z);
+}
+
+
+int inflateSync(z)
+z_streamp z;
+{
+  uInt n;       /* number of bytes to look at */
+  Bytef *p;     /* pointer to bytes */
+  uInt m;       /* number of marker bytes found in a row */
+  uLong r, w;   /* temporaries to save total_in and total_out */
+
+  /* set up */
+  if (z == Z_NULL || z->state == Z_NULL)
+    return Z_STREAM_ERROR;
+  if (z->state->mode != BAD)
+  {
+    z->state->mode = BAD;
+    z->state->sub.marker = 0;
+  }
+  if ((n = z->avail_in) == 0)
+    return Z_BUF_ERROR;
+  p = z->next_in;
+  m = z->state->sub.marker;
+
+  /* search */
+  while (n && m < 4)
+  {
+    if (*p == (Byte)(m < 2 ? 0 : 0xff))
+      m++;
+    else if (*p)
+      m = 0;
+    else
+      m = 4 - m;
+    p++, n--;
+  }
+
+  /* restore */
+  z->total_in += p - z->next_in;
+  z->next_in = p;
+  z->avail_in = n;
+  z->state->sub.marker = m;
+
+  /* return no joy or set up to restart on a new block */
+  if (m != 4)
+    return Z_DATA_ERROR;
+  r = z->total_in;  w = z->total_out;
+  inflateReset(z);
+  z->total_in = r;  z->total_out = w;
+  z->state->mode = BLOCKS;
+  return Z_OK;
+}
+
+#undef NEEDBYTE
+#undef NEXTBYTE
+/* --- inflate.c */
+
+/* +++ infblock.c */
+/* infblock.c -- interpret and process block types to last block
+ * Copyright (C) 1995-1996 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* #include "zutil.h" */
+/* #include "infblock.h" */
+
+/* +++ inftrees.h */
+/* inftrees.h -- header to use inftrees.c
+ * Copyright (C) 1995-1996 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+   part of the implementation of the compression library and is
+   subject to change. Applications should only use zlib.h.
+ */
+
+/* Huffman code lookup table entry--this entry is four bytes for machines
+   that have 16-bit pointers (e.g. PC's in the small or medium model). */
+
+typedef struct inflate_huft_s FAR inflate_huft;
+
+struct inflate_huft_s {
+  union {
+    struct {
+      Byte Exop;        /* number of extra bits or operation */
+      Byte Bits;        /* number of bits in this code or subcode */
+    } what;
+    Bytef *pad;         /* pad structure to a power of 2 (4 bytes for */
+  } word;               /*  16-bit, 8 bytes for 32-bit machines) */
+  union {
+    uInt Base;          /* literal, length base, or distance base */
+    inflate_huft *Next; /* pointer to next level of table */
+  } more;
+};
+
+#ifdef DEBUG_ZLIB
+  extern uInt inflate_hufts;
+#endif
+
+extern int inflate_trees_bits OF((
+    uIntf *,                    /* 19 code lengths */
+    uIntf *,                    /* bits tree desired/actual depth */
+    inflate_huft * FAR *,       /* bits tree result */
+    z_streamp ));               /* for zalloc, zfree functions */
+
+extern int inflate_trees_dynamic OF((
+    uInt,                       /* number of literal/length codes */
+    uInt,                       /* number of distance codes */
+    uIntf *,                    /* that many (total) code lengths */
+    uIntf *,                    /* literal desired/actual bit depth */
+    uIntf *,                    /* distance desired/actual bit depth */
+    inflate_huft * FAR *,       /* literal/length tree result */
+    inflate_huft * FAR *,       /* distance tree result */
+    z_streamp ));               /* for zalloc, zfree functions */
+
+extern int inflate_trees_fixed OF((
+    uIntf *,                    /* literal desired/actual bit depth */
+    uIntf *,                    /* distance desired/actual bit depth */
+    inflate_huft * FAR *,       /* literal/length tree result */
+    inflate_huft * FAR *));     /* distance tree result */
+
+extern int inflate_trees_free OF((
+    inflate_huft *,             /* tables to free */
+    z_streamp ));               /* for zfree function */
+
+/* --- inftrees.h */
+
+/* +++ infcodes.h */
+/* infcodes.h -- header to use infcodes.c
+ * Copyright (C) 1995-1996 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+   part of the implementation of the compression library and is
+   subject to change. Applications should only use zlib.h.
+ */
+
+struct inflate_codes_state;
+typedef struct inflate_codes_state FAR inflate_codes_statef;
+
+extern inflate_codes_statef *inflate_codes_new OF((
+    uInt, uInt,
+    inflate_huft *, inflate_huft *,
+    z_streamp ));
+
+extern int inflate_codes OF((
+    inflate_blocks_statef *,
+    z_streamp ,
+    int));
+
+extern void inflate_codes_free OF((
+    inflate_codes_statef *,
+    z_streamp ));
+
+/* --- infcodes.h */
+
+/* +++ infutil.h */
+/* infutil.h -- types and macros common to blocks and codes
+ * Copyright (C) 1995-1996 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+   part of the implementation of the compression library and is
+   subject to change. Applications should only use zlib.h.
+ */
+
+#ifndef _INFUTIL_H
+#define _INFUTIL_H
+
+typedef enum {
+      TYPE,     /* get type bits (3, including end bit) */
+      LENS,     /* get lengths for stored */
+      STORED,   /* processing stored block */
+      TABLE,    /* get table lengths */
+      BTREE,    /* get bit lengths tree for a dynamic block */
+      DTREE,    /* get length, distance trees for a dynamic block */
+      CODES,    /* processing fixed or dynamic block */
+      DRY,      /* output remaining window bytes */
+      DONEB,    /* finished last block, done */
+      BADB}     /* got a data error--stuck here */
+inflate_block_mode;
+
+/* inflate blocks semi-private state */
+struct inflate_blocks_state {
+
+  /* mode */
+  inflate_block_mode  mode;     /* current inflate_block mode */
+
+  /* mode dependent information */
+  union {
+    uInt left;          /* if STORED, bytes left to copy */
+    struct {
+      uInt table;               /* table lengths (14 bits) */
+      uInt index;               /* index into blens (or border) */
+      uIntf *blens;             /* bit lengths of codes */
+      uInt bb;                  /* bit length tree depth */
+      inflate_huft *tb;         /* bit length decoding tree */
+    } trees;            /* if DTREE, decoding info for trees */
+    struct {
+      inflate_huft *tl;
+      inflate_huft *td;         /* trees to free */
+      inflate_codes_statef 
+         *codes;
+    } decode;           /* if CODES, current state */
+  } sub;                /* submode */
+  uInt last;            /* true if this block is the last block */
+
+  /* mode independent information */
+  uInt bitk;            /* bits in bit buffer */
+  uLong bitb;           /* bit buffer */
+  Bytef *window;        /* sliding window */
+  Bytef *end;           /* one byte after sliding window */
+  Bytef *read;          /* window read pointer */
+  Bytef *write;         /* window write pointer */
+  check_func checkfn;   /* check function */
+  uLong check;          /* check on output */
+
+};
+
+
+/* defines for inflate input/output */
+/*   update pointers and return */
+#define UPDBITS {s->bitb=b;s->bitk=k;}
+#define UPDIN {z->avail_in=n;z->total_in+=p-z->next_in;z->next_in=p;}
+#define UPDOUT {s->write=q;}
+#define UPDATE {UPDBITS UPDIN UPDOUT}
+#define LEAVE {UPDATE return inflate_flush(s,z,r);}
+/*   get bytes and bits */
+#define LOADIN {p=z->next_in;n=z->avail_in;b=s->bitb;k=s->bitk;}
+#define NEEDBYTE {if(n)r=Z_OK;else LEAVE}
+#define NEXTBYTE (n--,*p++)
+#define NEEDBITS(j) {while(k<(j)){NEEDBYTE;b|=((uLong)NEXTBYTE)<<k;k+=8;}}
+#define DUMPBITS(j) {b>>=(j);k-=(j);}
+/*   output bytes */
+#define WAVAIL (uInt)(q<s->read?s->read-q-1:s->end-q)
+#define LOADOUT {q=s->write;m=(uInt)WAVAIL;}
+#define WWRAP {if(q==s->end&&s->read!=s->window){q=s->window;m=(uInt)WAVAIL;}}
+#define FLUSH {UPDOUT r=inflate_flush(s,z,r); LOADOUT}
+#define NEEDOUT {if(m==0){WWRAP if(m==0){FLUSH WWRAP if(m==0) LEAVE}}r=Z_OK;}
+#define OUTBYTE(a) {*q++=(Byte)(a);m--;}
+/*   load local pointers */
+#define LOAD {LOADIN LOADOUT}
+
+/* masks for lower bits (size given to avoid silly warnings with Visual C++) */
+extern uInt inflate_mask[17];
+
+/* copy as much as possible from the sliding window to the output area */
+extern int inflate_flush OF((
+    inflate_blocks_statef *,
+    z_streamp ,
+    int));
+
+#ifndef NO_DUMMY_DECL
+struct internal_state      {int dummy;}; /* for buggy compilers */
+#endif
+
+#endif
+/* --- infutil.h */
+
+#ifndef NO_DUMMY_DECL
+struct inflate_codes_state {int dummy;}; /* for buggy compilers */
+#endif
+
+/* Table for deflate from PKZIP's appnote.txt. */
+local const uInt border[] = { /* Order of the bit length code lengths */
+        16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15};
+
+/*
+   Notes beyond the 1.93a appnote.txt:
+
+   1. Distance pointers never point before the beginning of the output
+      stream.
+   2. Distance pointers can point back across blocks, up to 32k away.
+   3. There is an implied maximum of 7 bits for the bit length table and
+      15 bits for the actual data.
+   4. If only one code exists, then it is encoded using one bit.  (Zero
+      would be more efficient, but perhaps a little confusing.)  If two
+      codes exist, they are coded using one bit each (0 and 1).
+   5. There is no way of sending zero distance codes--a dummy must be
+      sent if there are none.  (History: a pre 2.0 version of PKZIP would
+      store blocks with no distance codes, but this was discovered to be
+      too harsh a criterion.)  Valid only for 1.93a.  2.04c does allow
+      zero distance codes, which is sent as one code of zero bits in
+      length.
+   6. There are up to 286 literal/length codes.  Code 256 represents the
+      end-of-block.  Note however that the static length tree defines
+      288 codes just to fill out the Huffman codes.  Codes 286 and 287
+      cannot be used though, since there is no length base or extra bits
+      defined for them.  Similarily, there are up to 30 distance codes.
+      However, static trees define 32 codes (all 5 bits) to fill out the
+      Huffman codes, but the last two had better not show up in the data.
+   7. Unzip can check dynamic Huffman blocks for complete code sets.
+      The exception is that a single code would not be complete (see #4).
+   8. The five bits following the block type is really the number of
+      literal codes sent minus 257.
+   9. Length codes 8,16,16 are interpreted as 13 length codes of 8 bits
+      (1+6+6).  Therefore, to output three times the length, you output
+      three codes (1+1+1), whereas to output four times the same length,
+      you only need two codes (1+3).  Hmm.
+  10. In the tree reconstruction algorithm, Code = Code + Increment
+      only if BitLength(i) is not zero.  (Pretty obvious.)
+  11. Correction: 4 Bits: # of Bit Length codes - 4     (4 - 19)
+  12. Note: length code 284 can represent 227-258, but length code 285
+      really is 258.  The last length deserves its own, short code
+      since it gets used a lot in very redundant files.  The length
+      258 is special since 258 - 3 (the min match length) is 255.
+  13. The literal/length and distance code bit lengths are read as a
+      single stream of lengths.  It is possible (and advantageous) for
+      a repeat code (16, 17, or 18) to go across the boundary between
+      the two sets of lengths.
+ */
+
+
+void inflate_blocks_reset(s, z, c)
+inflate_blocks_statef *s;
+z_streamp z;
+uLongf *c;
+{
+  if (s->checkfn != Z_NULL)
+    *c = s->check;
+  if (s->mode == BTREE || s->mode == DTREE)
+    ZFREE(z, s->sub.trees.blens);
+  if (s->mode == CODES)
+  {
+    inflate_codes_free(s->sub.decode.codes, z);
+    inflate_trees_free(s->sub.decode.td, z);
+    inflate_trees_free(s->sub.decode.tl, z);
+  }
+  s->mode = TYPE;
+  s->bitk = 0;
+  s->bitb = 0;
+  s->read = s->write = s->window;
+  if (s->checkfn != Z_NULL)
+    z->adler = s->check = (*s->checkfn)(0L, Z_NULL, 0);
+  Trace((stderr, "inflate:   blocks reset\n"));
+}
+
+
+inflate_blocks_statef *inflate_blocks_new(z, c, w)
+z_streamp z;
+check_func c;
+uInt w;
+{
+  inflate_blocks_statef *s;
+
+  if ((s = (inflate_blocks_statef *)ZALLOC
+       (z,1,sizeof(struct inflate_blocks_state))) == Z_NULL)
+    return s;
+  if ((s->window = (Bytef *)ZALLOC(z, 1, w)) == Z_NULL)
+  {
+    ZFREE(z, s);
+    return Z_NULL;
+  }
+  s->end = s->window + w;
+  s->checkfn = c;
+  s->mode = TYPE;
+  Trace((stderr, "inflate:   blocks allocated\n"));
+  inflate_blocks_reset(s, z, &s->check);
+  return s;
+}
+
+
+#ifdef DEBUG_ZLIB
+  extern uInt inflate_hufts;
+#endif
+int inflate_blocks(s, z, r)
+inflate_blocks_statef *s;
+z_streamp z;
+int r;
+{
+  uInt t;               /* temporary storage */
+  uLong b;              /* bit buffer */
+  uInt k;               /* bits in bit buffer */
+  Bytef *p;             /* input data pointer */
+  uInt n;               /* bytes available there */
+  Bytef *q;             /* output window write pointer */
+  uInt m;               /* bytes to end of window or read pointer */
+
+  /* copy input/output information to locals (UPDATE macro restores) */
+  LOAD
+
+  /* process input based on current state */
+  while (1) switch (s->mode)
+  {
+    case TYPE:
+      NEEDBITS(3)
+      t = (uInt)b & 7;
+      s->last = t & 1;
+      switch (t >> 1)
+      {
+        case 0:                         /* stored */
+          Trace((stderr, "inflate:     stored block%s\n",
+                 s->last ? " (last)" : ""));
+          DUMPBITS(3)
+          t = k & 7;                    /* go to byte boundary */
+          DUMPBITS(t)
+          s->mode = LENS;               /* get length of stored block */
+          break;
+        case 1:                         /* fixed */
+          Trace((stderr, "inflate:     fixed codes block%s\n",
+                 s->last ? " (last)" : ""));
+          {
+            uInt bl, bd;
+            inflate_huft *tl, *td;
+
+            inflate_trees_fixed(&bl, &bd, &tl, &td);
+            s->sub.decode.codes = inflate_codes_new(bl, bd, tl, td, z);
+            if (s->sub.decode.codes == Z_NULL)
+            {
+              r = Z_MEM_ERROR;
+              LEAVE
+            }
+            s->sub.decode.tl = Z_NULL;  /* don't try to free these */
+            s->sub.decode.td = Z_NULL;
+          }
+          DUMPBITS(3)
+          s->mode = CODES;
+          break;
+        case 2:                         /* dynamic */
+          Trace((stderr, "inflate:     dynamic codes block%s\n",
+                 s->last ? " (last)" : ""));
+          DUMPBITS(3)
+          s->mode = TABLE;
+          break;
+        case 3:                         /* illegal */
+          DUMPBITS(3)
+          s->mode = BADB;
+          z->msg = (char*)"invalid block type";
+          r = Z_DATA_ERROR;
+          LEAVE
+      }
+      break;
+    case LENS:
+      NEEDBITS(32)
+      if ((((~b) >> 16) & 0xffff) != (b & 0xffff))
+      {
+        s->mode = BADB;
+        z->msg = (char*)"invalid stored block lengths";
+        r = Z_DATA_ERROR;
+        LEAVE
+      }
+      s->sub.left = (uInt)b & 0xffff;
+      b = k = 0;                      /* dump bits */
+      Tracev((stderr, "inflate:       stored length %u\n", s->sub.left));
+      s->mode = s->sub.left ? STORED : (s->last ? DRY : TYPE);
+      break;
+    case STORED:
+      if (n == 0)
+        LEAVE
+      NEEDOUT
+      t = s->sub.left;
+      if (t > n) t = n;
+      if (t > m) t = m;
+      zmemcpy(q, p, t);
+      p += t;  n -= t;
+      q += t;  m -= t;
+      if ((s->sub.left -= t) != 0)
+        break;
+      Tracev((stderr, "inflate:       stored end, %lu total out\n",
+              z->total_out + (q >= s->read ? q - s->read :
+              (s->end - s->read) + (q - s->window))));
+      s->mode = s->last ? DRY : TYPE;
+      break;
+    case TABLE:
+      NEEDBITS(14)
+      s->sub.trees.table = t = (uInt)b & 0x3fff;
+#ifndef PKZIP_BUG_WORKAROUND
+      if ((t & 0x1f) > 29 || ((t >> 5) & 0x1f) > 29)
+      {
+        s->mode = BADB;
+        z->msg = (char*)"too many length or distance symbols";
+        r = Z_DATA_ERROR;
+        LEAVE
+      }
+#endif
+      t = 258 + (t & 0x1f) + ((t >> 5) & 0x1f);
+      if (t < 19)
+        t = 19;
+      if ((s->sub.trees.blens = (uIntf*)ZALLOC(z, t, sizeof(uInt))) == Z_NULL)
+      {
+        r = Z_MEM_ERROR;
+        LEAVE
+      }
+      DUMPBITS(14)
+      s->sub.trees.index = 0;
+      Tracev((stderr, "inflate:       table sizes ok\n"));
+      s->mode = BTREE;
+    case BTREE:
+      while (s->sub.trees.index < 4 + (s->sub.trees.table >> 10))
+      {
+        NEEDBITS(3)
+        s->sub.trees.blens[border[s->sub.trees.index++]] = (uInt)b & 7;
+        DUMPBITS(3)
+      }
+      while (s->sub.trees.index < 19)
+        s->sub.trees.blens[border[s->sub.trees.index++]] = 0;
+      s->sub.trees.bb = 7;
+      t = inflate_trees_bits(s->sub.trees.blens, &s->sub.trees.bb,
+                             &s->sub.trees.tb, z);
+      if (t != Z_OK)
+      {
+        ZFREE(z, s->sub.trees.blens);
+        r = t;
+        if (r == Z_DATA_ERROR)
+          s->mode = BADB;
+        LEAVE
+      }
+      s->sub.trees.index = 0;
+      Tracev((stderr, "inflate:       bits tree ok\n"));
+      s->mode = DTREE;
+    case DTREE:
+      while (t = s->sub.trees.table,
+             s->sub.trees.index < 258 + (t & 0x1f) + ((t >> 5) & 0x1f))
+      {
+        inflate_huft *h;
+        uInt i, j, c;
+
+        t = s->sub.trees.bb;
+        NEEDBITS(t)
+        h = s->sub.trees.tb + ((uInt)b & inflate_mask[t]);
+        t = h->word.what.Bits;
+        c = h->more.Base;
+        if (c < 16)
+        {
+          DUMPBITS(t)
+          s->sub.trees.blens[s->sub.trees.index++] = c;
+        }
+        else /* c == 16..18 */
+        {
+          i = c == 18 ? 7 : c - 14;
+          j = c == 18 ? 11 : 3;
+          NEEDBITS(t + i)
+          DUMPBITS(t)
+          j += (uInt)b & inflate_mask[i];
+          DUMPBITS(i)
+          i = s->sub.trees.index;
+          t = s->sub.trees.table;
+          if (i + j > 258 + (t & 0x1f) + ((t >> 5) & 0x1f) ||
+              (c == 16 && i < 1))
+          {
+            inflate_trees_free(s->sub.trees.tb, z);
+            ZFREE(z, s->sub.trees.blens);
+            s->mode = BADB;
+            z->msg = (char*)"invalid bit length repeat";
+            r = Z_DATA_ERROR;
+            LEAVE
+          }
+          c = c == 16 ? s->sub.trees.blens[i - 1] : 0;
+          do {
+            s->sub.trees.blens[i++] = c;
+          } while (--j);
+          s->sub.trees.index = i;
+        }
+      }
+      inflate_trees_free(s->sub.trees.tb, z);
+      s->sub.trees.tb = Z_NULL;
+      {
+        uInt bl, bd;
+        inflate_huft *tl, *td;
+        inflate_codes_statef *c;
+
+        bl = 9;         /* must be <= 9 for lookahead assumptions */
+        bd = 6;         /* must be <= 9 for lookahead assumptions */
+        t = s->sub.trees.table;
+#ifdef DEBUG_ZLIB
+      inflate_hufts = 0;
+#endif
+        t = inflate_trees_dynamic(257 + (t & 0x1f), 1 + ((t >> 5) & 0x1f),
+                                  s->sub.trees.blens, &bl, &bd, &tl, &td, z);
+        ZFREE(z, s->sub.trees.blens);
+        if (t != Z_OK)
+        {
+          if (t == (uInt)Z_DATA_ERROR)
+            s->mode = BADB;
+          r = t;
+          LEAVE
+        }
+        Tracev((stderr, "inflate:       trees ok, %d * %d bytes used\n",
+              inflate_hufts, sizeof(inflate_huft)));
+        if ((c = inflate_codes_new(bl, bd, tl, td, z)) == Z_NULL)
+        {
+          inflate_trees_free(td, z);
+          inflate_trees_free(tl, z);
+          r = Z_MEM_ERROR;
+          LEAVE
+        }
+        s->sub.decode.codes = c;
+        s->sub.decode.tl = tl;
+        s->sub.decode.td = td;
+      }
+      s->mode = CODES;
+    case CODES:
+      UPDATE
+      if ((r = inflate_codes(s, z, r)) != Z_STREAM_END)
+        return inflate_flush(s, z, r);
+      r = Z_OK;
+      inflate_codes_free(s->sub.decode.codes, z);
+      inflate_trees_free(s->sub.decode.td, z);
+      inflate_trees_free(s->sub.decode.tl, z);
+      LOAD
+      Tracev((stderr, "inflate:       codes end, %lu total out\n",
+              z->total_out + (q >= s->read ? q - s->read :
+              (s->end - s->read) + (q - s->window))));
+      if (!s->last)
+      {
+        s->mode = TYPE;
+        break;
+      }
+      if (k > 7)              /* return unused byte, if any */
+      {
+        Assert(k < 16, "inflate_codes grabbed too many bytes")
+        k -= 8;
+        n++;
+        p--;                    /* can always return one */
+      }
+      s->mode = DRY;
+    case DRY:
+      FLUSH
+      if (s->read != s->write)
+        LEAVE
+      s->mode = DONEB;
+    case DONEB:
+      r = Z_STREAM_END;
+      LEAVE
+    case BADB:
+      r = Z_DATA_ERROR;
+      LEAVE
+    default:
+      r = Z_STREAM_ERROR;
+      LEAVE
+  }
+}
+
+
+int inflate_blocks_free(s, z, c)
+inflate_blocks_statef *s;
+z_streamp z;
+uLongf *c;
+{
+  inflate_blocks_reset(s, z, c);
+  ZFREE(z, s->window);
+  ZFREE(z, s);
+  Trace((stderr, "inflate:   blocks freed\n"));
+  return Z_OK;
+}
+
+
+void inflate_set_dictionary(s, d, n)
+inflate_blocks_statef *s;
+const Bytef *d;
+uInt  n;
+{
+  zmemcpy((charf *)s->window, d, n);
+  s->read = s->write = s->window + n;
+}
+
+/*
+ * This subroutine adds the data at next_in/avail_in to the output history
+ * without performing any output.  The output buffer must be "caught up";
+ * i.e. no pending output (hence s->read equals s->write), and the state must
+ * be BLOCKS (i.e. we should be willing to see the start of a series of
+ * BLOCKS).  On exit, the output will also be caught up, and the checksum
+ * will have been updated if need be.
+ */
+int inflate_addhistory(s, z)
+inflate_blocks_statef *s;
+z_stream *z;
+{
+    uLong b;              /* bit buffer */  /* NOT USED HERE */
+    uInt k;               /* bits in bit buffer */ /* NOT USED HERE */
+    uInt t;               /* temporary storage */
+    Bytef *p;             /* input data pointer */
+    uInt n;               /* bytes available there */
+    Bytef *q;             /* output window write pointer */
+    uInt m;               /* bytes to end of window or read pointer */
+
+    if (s->read != s->write)
+       return Z_STREAM_ERROR;
+    if (s->mode != TYPE)
+       return Z_DATA_ERROR;
+
+    /* we're ready to rock */
+    LOAD
+    /* while there is input ready, copy to output buffer, moving
+     * pointers as needed.
+     */
+    while (n) {
+       t = n;  /* how many to do */
+       /* is there room until end of buffer? */
+       if (t > m) t = m;
+       /* update check information */
+       if (s->checkfn != Z_NULL)
+           s->check = (*s->checkfn)(s->check, q, t);
+       zmemcpy(q, p, t);
+       q += t;
+       p += t;
+       n -= t;
+       z->total_out += t;
+       s->read = q;    /* drag read pointer forward */
+/*      WWRAP  */      /* expand WWRAP macro by hand to handle s->read */
+       if (q == s->end) {
+           s->read = q = s->window;
+           m = WAVAIL;
+       }
+    }
+    UPDATE
+    return Z_OK;
+}
+
+
+/*
+ * At the end of a Deflate-compressed PPP packet, we expect to have seen
+ * a `stored' block type value but not the (zero) length bytes.
+ */
+int inflate_packet_flush(s)
+    inflate_blocks_statef *s;
+{
+    if (s->mode != LENS)
+       return Z_DATA_ERROR;
+    s->mode = TYPE;
+    return Z_OK;
+}
+/* --- infblock.c */
+
+/* +++ inftrees.c */
+/* inftrees.c -- generate Huffman trees for efficient decoding
+ * Copyright (C) 1995-1996 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* #include "zutil.h" */
+/* #include "inftrees.h" */
+
+char inflate_copyright[] = " inflate 1.0.4 Copyright 1995-1996 Mark Adler ";
+/*
+  If you use the zlib library in a product, an acknowledgment is welcome
+  in the documentation of your product. If for some reason you cannot
+  include such an acknowledgment, I would appreciate that you keep this
+  copyright string in the executable of your product.
+ */
+
+#ifndef NO_DUMMY_DECL
+struct internal_state  {int dummy;}; /* for buggy compilers */
+#endif
+
+/* simplify the use of the inflate_huft type with some defines */
+#define base more.Base
+#define next more.Next
+#define exop word.what.Exop
+#define bits word.what.Bits
+
+
+local int huft_build OF((
+    uIntf *,            /* code lengths in bits */
+    uInt,               /* number of codes */
+    uInt,               /* number of "simple" codes */
+    const uIntf *,      /* list of base values for non-simple codes */
+    const uIntf *,      /* list of extra bits for non-simple codes */
+    inflate_huft * FAR*,/* result: starting table */
+    uIntf *,            /* maximum lookup bits (returns actual) */
+    z_streamp ));       /* for zalloc function */
+
+local voidpf falloc OF((
+    voidpf,             /* opaque pointer (not used) */
+    uInt,               /* number of items */
+    uInt));             /* size of item */
+
+/* Tables for deflate from PKZIP's appnote.txt. */
+local const uInt cplens[31] = { /* Copy lengths for literal codes 257..285 */
+        3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31,
+        35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0};
+        /* see note #13 above about 258 */
+local const uInt cplext[31] = { /* Extra bits for literal codes 257..285 */
+        0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2,
+        3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 112, 112}; /* 112==invalid */
+local const uInt cpdist[30] = { /* Copy offsets for distance codes 0..29 */
+        1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
+        257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145,
+        8193, 12289, 16385, 24577};
+local const uInt cpdext[30] = { /* Extra bits for distance codes */
+        0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6,
+        7, 7, 8, 8, 9, 9, 10, 10, 11, 11,
+        12, 12, 13, 13};
+
+/*
+   Huffman code decoding is performed using a multi-level table lookup.
+   The fastest way to decode is to simply build a lookup table whose
+   size is determined by the longest code.  However, the time it takes
+   to build this table can also be a factor if the data being decoded
+   is not very long.  The most common codes are necessarily the
+   shortest codes, so those codes dominate the decoding time, and hence
+   the speed.  The idea is you can have a shorter table that decodes the
+   shorter, more probable codes, and then point to subsidiary tables for
+   the longer codes.  The time it costs to decode the longer codes is
+   then traded against the time it takes to make longer tables.
+
+   This results of this trade are in the variables lbits and dbits
+   below.  lbits is the number of bits the first level table for literal/
+   length codes can decode in one step, and dbits is the same thing for
+   the distance codes.  Subsequent tables are also less than or equal to
+   those sizes.  These values may be adjusted either when all of the
+   codes are shorter than that, in which case the longest code length in
+   bits is used, or when the shortest code is *longer* than the requested
+   table size, in which case the length of the shortest code in bits is
+   used.
+
+   There are two different values for the two tables, since they code a
+   different number of possibilities each.  The literal/length table
+   codes 286 possible values, or in a flat code, a little over eight
+   bits.  The distance table codes 30 possible values, or a little less
+   than five bits, flat.  The optimum values for speed end up being
+   about one bit more than those, so lbits is 8+1 and dbits is 5+1.
+   The optimum values may differ though from machine to machine, and
+   possibly even between compilers.  Your mileage may vary.
+ */
+
+
+/* If BMAX needs to be larger than 16, then h and x[] should be uLong. */
+#define BMAX 15         /* maximum bit length of any code */
+#define N_MAX 288       /* maximum number of codes in any set */
+
+#ifdef DEBUG_ZLIB
+  uInt inflate_hufts;
+#endif
+
+local int huft_build(b, n, s, d, e, t, m, zs)
+uIntf *b;               /* code lengths in bits (all assumed <= BMAX) */
+uInt n;                 /* number of codes (assumed <= N_MAX) */
+uInt s;                 /* number of simple-valued codes (0..s-1) */
+const uIntf *d;         /* list of base values for non-simple codes */
+const uIntf *e;         /* list of extra bits for non-simple codes */
+inflate_huft * FAR *t;  /* result: starting table */
+uIntf *m;               /* maximum lookup bits, returns actual */
+z_streamp zs;           /* for zalloc function */
+/* Given a list of code lengths and a maximum table size, make a set of
+   tables to decode that set of codes.  Return Z_OK on success, Z_BUF_ERROR
+   if the given code set is incomplete (the tables are still built in this
+   case), Z_DATA_ERROR if the input is invalid (an over-subscribed set of
+   lengths), or Z_MEM_ERROR if not enough memory. */
+{
+
+  uInt a;                       /* counter for codes of length k */
+  uInt c[BMAX+1];               /* bit length count table */
+  uInt f;                       /* i repeats in table every f entries */
+  int g;                        /* maximum code length */
+  int h;                        /* table level */
+  register uInt i;              /* counter, current code */
+  register uInt j;              /* counter */
+  register int k;               /* number of bits in current code */
+  int l;                        /* bits per table (returned in m) */
+  register uIntf *p;            /* pointer into c[], b[], or v[] */
+  inflate_huft *q;              /* points to current table */
+  struct inflate_huft_s r;      /* table entry for structure assignment */
+  inflate_huft *u[BMAX];        /* table stack */
+  uInt v[N_MAX];                /* values in order of bit length */
+  register int w;               /* bits before this table == (l * h) */
+  uInt x[BMAX+1];               /* bit offsets, then code stack */
+  uIntf *xp;                    /* pointer into x */
+  int y;                        /* number of dummy codes added */
+  uInt z;                       /* number of entries in current table */
+
+
+  /* Generate counts for each bit length */
+  p = c;
+#define C0 *p++ = 0;
+#define C2 C0 C0 C0 C0
+#define C4 C2 C2 C2 C2
+  C4                            /* clear c[]--assume BMAX+1 is 16 */
+  p = b;  i = n;
+  do {
+    c[*p++]++;                  /* assume all entries <= BMAX */
+  } while (--i);
+  if (c[0] == n)                /* null input--all zero length codes */
+  {
+    *t = (inflate_huft *)Z_NULL;
+    *m = 0;
+    return Z_OK;
+  }
+
+
+  /* Find minimum and maximum length, bound *m by those */
+  l = *m;
+  for (j = 1; j <= BMAX; j++)
+    if (c[j])
+      break;
+  k = j;                        /* minimum code length */
+  if ((uInt)l < j)
+    l = j;
+  for (i = BMAX; i; i--)
+    if (c[i])
+      break;
+  g = i;                        /* maximum code length */
+  if ((uInt)l > i)
+    l = i;
+  *m = l;
+
+
+  /* Adjust last length count to fill out codes, if needed */
+  for (y = 1 << j; j < i; j++, y <<= 1)
+    if ((y -= c[j]) < 0)
+      return Z_DATA_ERROR;
+  if ((y -= c[i]) < 0)
+    return Z_DATA_ERROR;
+  c[i] += y;
+
+
+  /* Generate starting offsets into the value table for each length */
+  x[1] = j = 0;
+  p = c + 1;  xp = x + 2;
+  while (--i) {                 /* note that i == g from above */
+    *xp++ = (j += *p++);
+  }
+
+
+  /* Make a table of values in order of bit lengths */
+  p = b;  i = 0;
+  do {
+    if ((j = *p++) != 0)
+      v[x[j]++] = i;
+  } while (++i < n);
+  n = x[g];                   /* set n to length of v */
+
+
+  /* Generate the Huffman codes and for each, make the table entries */
+  x[0] = i = 0;                 /* first Huffman code is zero */
+  p = v;                        /* grab values in bit order */
+  h = -1;                       /* no tables yet--level -1 */
+  w = -l;                       /* bits decoded == (l * h) */
+  u[0] = (inflate_huft *)Z_NULL;        /* just to keep compilers happy */
+  q = (inflate_huft *)Z_NULL;   /* ditto */
+  z = 0;                        /* ditto */
+
+  /* go through the bit lengths (k already is bits in shortest code) */
+  for (; k <= g; k++)
+  {
+    a = c[k];
+    while (a--)
+    {
+      /* here i is the Huffman code of length k bits for value *p */
+      /* make tables up to required level */
+      while (k > w + l)
+      {
+        h++;
+        w += l;                 /* previous table always l bits */
+
+        /* compute minimum size table less than or equal to l bits */
+        z = g - w;
+        z = z > (uInt)l ? l : z;        /* table size upper limit */
+        if ((f = 1 << (j = k - w)) > a + 1)     /* try a k-w bit table */
+        {                       /* too few codes for k-w bit table */
+          f -= a + 1;           /* deduct codes from patterns left */
+          xp = c + k;
+          if (j < z)
+            while (++j < z)     /* try smaller tables up to z bits */
+            {
+              if ((f <<= 1) <= *++xp)
+                break;          /* enough codes to use up j bits */
+              f -= *xp;         /* else deduct codes from patterns */
+            }
+        }
+        z = 1 << j;             /* table entries for j-bit table */
+
+        /* allocate and link in new table */
+        if ((q = (inflate_huft *)ZALLOC
+             (zs,z + 1,sizeof(inflate_huft))) == Z_NULL)
+        {
+          if (h)
+            inflate_trees_free(u[0], zs);
+          return Z_MEM_ERROR;   /* not enough memory */
+        }
+#ifdef DEBUG_ZLIB
+        inflate_hufts += z + 1;
+#endif
+        *t = q + 1;             /* link to list for huft_free() */
+        *(t = &(q->next)) = Z_NULL;
+        u[h] = ++q;             /* table starts after link */
+
+        /* connect to last table, if there is one */
+        if (h)
+        {
+          x[h] = i;             /* save pattern for backing up */
+          r.bits = (Byte)l;     /* bits to dump before this table */
+          r.exop = (Byte)j;     /* bits in this table */
+          r.next = q;           /* pointer to this table */
+          j = i >> (w - l);     /* (get around Turbo C bug) */
+          u[h-1][j] = r;        /* connect to last table */
+        }
+      }
+
+      /* set up table entry in r */
+      r.bits = (Byte)(k - w);
+      if (p >= v + n)
+        r.exop = 128 + 64;      /* out of values--invalid code */
+      else if (*p < s)
+      {
+        r.exop = (Byte)(*p < 256 ? 0 : 32 + 64);     /* 256 is end-of-block */
+        r.base = *p++;          /* simple code is just the value */
+      }
+      else
+      {
+        r.exop = (Byte)(e[*p - s] + 16 + 64);/* non-simple--look up in lists */
+        r.base = d[*p++ - s];
+      }
+
+      /* fill code-like entries with r */
+      f = 1 << (k - w);
+      for (j = i >> w; j < z; j += f)
+        q[j] = r;
+
+      /* backwards increment the k-bit code i */
+      for (j = 1 << (k - 1); i & j; j >>= 1)
+        i ^= j;
+      i ^= j;
+
+      /* backup over finished tables */
+      while ((i & ((1 << w) - 1)) != x[h])
+      {
+        h--;                    /* don't need to update q */
+        w -= l;
+      }
+    }
+  }
+
+
+  /* Return Z_BUF_ERROR if we were given an incomplete table */
+  return y != 0 && g != 1 ? Z_BUF_ERROR : Z_OK;
+}
+
+
+int inflate_trees_bits(c, bb, tb, z)
+uIntf *c;               /* 19 code lengths */
+uIntf *bb;              /* bits tree desired/actual depth */
+inflate_huft * FAR *tb; /* bits tree result */
+z_streamp z;            /* for zfree function */
+{
+  int r;
+
+  r = huft_build(c, 19, 19, (uIntf*)Z_NULL, (uIntf*)Z_NULL, tb, bb, z);
+  if (r == Z_DATA_ERROR)
+    z->msg = (char*)"oversubscribed dynamic bit lengths tree";
+  else if (r == Z_BUF_ERROR || *bb == 0)
+  {
+    inflate_trees_free(*tb, z);
+    z->msg = (char*)"incomplete dynamic bit lengths tree";
+    r = Z_DATA_ERROR;
+  }
+  return r;
+}
+
+
+int inflate_trees_dynamic(nl, nd, c, bl, bd, tl, td, z)
+uInt nl;                /* number of literal/length codes */
+uInt nd;                /* number of distance codes */
+uIntf *c;               /* that many (total) code lengths */
+uIntf *bl;              /* literal desired/actual bit depth */
+uIntf *bd;              /* distance desired/actual bit depth */
+inflate_huft * FAR *tl; /* literal/length tree result */
+inflate_huft * FAR *td; /* distance tree result */
+z_streamp z;            /* for zfree function */
+{
+  int r;
+
+  /* build literal/length tree */
+  r = huft_build(c, nl, 257, cplens, cplext, tl, bl, z);
+  if (r != Z_OK || *bl == 0)
+  {
+    if (r == Z_DATA_ERROR)
+      z->msg = (char*)"oversubscribed literal/length tree";
+    else if (r != Z_MEM_ERROR)
+    {
+      inflate_trees_free(*tl, z);
+      z->msg = (char*)"incomplete literal/length tree";
+      r = Z_DATA_ERROR;
+    }
+    return r;
+  }
+
+  /* build distance tree */
+  r = huft_build(c + nl, nd, 0, cpdist, cpdext, td, bd, z);
+  if (r != Z_OK || (*bd == 0 && nl > 257))
+  {
+    if (r == Z_DATA_ERROR)
+      z->msg = (char*)"oversubscribed distance tree";
+    else if (r == Z_BUF_ERROR) {
+#ifdef PKZIP_BUG_WORKAROUND
+      r = Z_OK;
+    }
+#else
+      inflate_trees_free(*td, z);
+      z->msg = (char*)"incomplete distance tree";
+      r = Z_DATA_ERROR;
+    }
+    else if (r != Z_MEM_ERROR)
+    {
+      z->msg = (char*)"empty distance tree with lengths";
+      r = Z_DATA_ERROR;
+    }
+    inflate_trees_free(*tl, z);
+    return r;
+#endif
+  }
+
+  /* done */
+  return Z_OK;
+}
+
+
+/* build fixed tables only once--keep them here */
+local int fixed_built = 0;
+#define FIXEDH 530      /* number of hufts used by fixed tables */
+local inflate_huft fixed_mem[FIXEDH];
+local uInt fixed_bl;
+local uInt fixed_bd;
+local inflate_huft *fixed_tl;
+local inflate_huft *fixed_td;
+
+
+local voidpf falloc(q, n, s)
+voidpf q;       /* opaque pointer */
+uInt n;         /* number of items */
+uInt s;         /* size of item */
+{
+  Assert(s == sizeof(inflate_huft) && n <= *(intf *)q,
+         "inflate_trees falloc overflow");
+  *(intf *)q -= n+s-s; /* s-s to avoid warning */
+  return (voidpf)(fixed_mem + *(intf *)q);
+}
+
+
+int inflate_trees_fixed(bl, bd, tl, td)
+uIntf *bl;               /* literal desired/actual bit depth */
+uIntf *bd;               /* distance desired/actual bit depth */
+inflate_huft * FAR *tl;  /* literal/length tree result */
+inflate_huft * FAR *td;  /* distance tree result */
+{
+  /* build fixed tables if not already (multiple overlapped executions ok) */
+  if (!fixed_built)
+  {
+    int k;              /* temporary variable */
+    unsigned c[288];    /* length list for huft_build */
+    z_stream z;         /* for falloc function */
+    int f = FIXEDH;     /* number of hufts left in fixed_mem */
+
+    /* set up fake z_stream for memory routines */
+    z.zalloc = falloc;
+    z.zfree = Z_NULL;
+    z.opaque = (voidpf)&f;
+
+    /* literal table */
+    for (k = 0; k < 144; k++)
+      c[k] = 8;
+    for (; k < 256; k++)
+      c[k] = 9;
+    for (; k < 280; k++)
+      c[k] = 7;
+    for (; k < 288; k++)
+      c[k] = 8;
+    fixed_bl = 7;
+    huft_build(c, 288, 257, cplens, cplext, &fixed_tl, &fixed_bl, &z);
+
+    /* distance table */
+    for (k = 0; k < 30; k++)
+      c[k] = 5;
+    fixed_bd = 5;
+    huft_build(c, 30, 0, cpdist, cpdext, &fixed_td, &fixed_bd, &z);
+
+    /* done */
+    Assert(f == 0, "invalid build of fixed tables");
+    fixed_built = 1;
+  }
+  *bl = fixed_bl;
+  *bd = fixed_bd;
+  *tl = fixed_tl;
+  *td = fixed_td;
+  return Z_OK;
+}
+
+
+int inflate_trees_free(t, z)
+inflate_huft *t;        /* table to free */
+z_streamp z;            /* for zfree function */
+/* Free the malloc'ed tables built by huft_build(), which makes a linked
+   list of the tables it made, with the links in a dummy first entry of
+   each table. */
+{
+  register inflate_huft *p, *q, *r;
+
+  /* Reverse linked list */
+  p = Z_NULL;
+  q = t;
+  while (q != Z_NULL)
+  {
+    r = (q - 1)->next;
+    (q - 1)->next = p;
+    p = q;
+    q = r;
+  }
+  /* Go through linked list, freeing from the malloced (t[-1]) address. */
+  while (p != Z_NULL)
+  {
+    q = (--p)->next;
+    ZFREE(z,p);
+    p = q;
+  } 
+  return Z_OK;
+}
+/* --- inftrees.c */
+
+/* +++ infcodes.c */
+/* infcodes.c -- process literals and length/distance pairs
+ * Copyright (C) 1995-1996 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* #include "zutil.h" */
+/* #include "inftrees.h" */
+/* #include "infblock.h" */
+/* #include "infcodes.h" */
+/* #include "infutil.h" */
+
+/* +++ inffast.h */
+/* inffast.h -- header to use inffast.c
+ * Copyright (C) 1995-1996 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* WARNING: this file should *not* be used by applications. It is
+   part of the implementation of the compression library and is
+   subject to change. Applications should only use zlib.h.
+ */
+
+extern int inflate_fast OF((
+    uInt,
+    uInt,
+    inflate_huft *,
+    inflate_huft *,
+    inflate_blocks_statef *,
+    z_streamp ));
+/* --- inffast.h */
+
+/* simplify the use of the inflate_huft type with some defines */
+#define base more.Base
+#define next more.Next
+#define exop word.what.Exop
+#define bits word.what.Bits
+
+/* inflate codes private state */
+struct inflate_codes_state {
+
+  /* mode */
+  enum {        /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
+      START,    /* x: set up for LEN */
+      LEN,      /* i: get length/literal/eob next */
+      LENEXT,   /* i: getting length extra (have base) */
+      DIST,     /* i: get distance next */
+      DISTEXT,  /* i: getting distance extra */
+      COPY,     /* o: copying bytes in window, waiting for space */
+      LIT,      /* o: got literal, waiting for output space */
+      WASH,     /* o: got eob, possibly still output waiting */
+      END,      /* x: got eob and all data flushed */
+      BADCODE}  /* x: got error */
+    mode;               /* current inflate_codes mode */
+
+  /* mode dependent information */
+  uInt len;
+  union {
+    struct {
+      inflate_huft *tree;       /* pointer into tree */
+      uInt need;                /* bits needed */
+    } code;             /* if LEN or DIST, where in tree */
+    uInt lit;           /* if LIT, literal */
+    struct {
+      uInt get;                 /* bits to get for extra */
+      uInt dist;                /* distance back to copy from */
+    } copy;             /* if EXT or COPY, where and how much */
+  } sub;                /* submode */
+
+  /* mode independent information */
+  Byte lbits;           /* ltree bits decoded per branch */
+  Byte dbits;           /* dtree bits decoder per branch */
+  inflate_huft *ltree;          /* literal/length/eob tree */
+  inflate_huft *dtree;          /* distance tree */
+
+};
+
+
+inflate_codes_statef *inflate_codes_new(bl, bd, tl, td, z)
+uInt bl, bd;
+inflate_huft *tl;
+inflate_huft *td; /* need separate declaration for Borland C++ */
+z_streamp z;
+{
+  inflate_codes_statef *c;
+
+  if ((c = (inflate_codes_statef *)
+       ZALLOC(z,1,sizeof(struct inflate_codes_state))) != Z_NULL)
+  {
+    c->mode = START;
+    c->lbits = (Byte)bl;
+    c->dbits = (Byte)bd;
+    c->ltree = tl;
+    c->dtree = td;
+    Tracev((stderr, "inflate:       codes new\n"));
+  }
+  return c;
+}
+
+
+int inflate_codes(s, z, r)
+inflate_blocks_statef *s;
+z_streamp z;
+int r;
+{
+  uInt j;               /* temporary storage */
+  inflate_huft *t;      /* temporary pointer */
+  uInt e;               /* extra bits or operation */
+  uLong b;              /* bit buffer */
+  uInt k;               /* bits in bit buffer */
+  Bytef *p;             /* input data pointer */
+  uInt n;               /* bytes available there */
+  Bytef *q;             /* output window write pointer */
+  uInt m;               /* bytes to end of window or read pointer */
+  Bytef *f;             /* pointer to copy strings from */
+  inflate_codes_statef *c = s->sub.decode.codes;  /* codes state */
+
+  /* copy input/output information to locals (UPDATE macro restores) */
+  LOAD
+
+  /* process input and output based on current state */
+  while (1) switch (c->mode)
+  {             /* waiting for "i:"=input, "o:"=output, "x:"=nothing */
+    case START:         /* x: set up for LEN */
+#ifndef SLOW
+      if (m >= 258 && n >= 10)
+      {
+        UPDATE
+        r = inflate_fast(c->lbits, c->dbits, c->ltree, c->dtree, s, z);
+        LOAD
+        if (r != Z_OK)
+        {
+          c->mode = r == Z_STREAM_END ? WASH : BADCODE;
+          break;
+        }
+      }
+#endif /* !SLOW */
+      c->sub.code.need = c->lbits;
+      c->sub.code.tree = c->ltree;
+      c->mode = LEN;
+    case LEN:           /* i: get length/literal/eob next */
+      j = c->sub.code.need;
+      NEEDBITS(j)
+      t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
+      DUMPBITS(t->bits)
+      e = (uInt)(t->exop);
+      if (e == 0)               /* literal */
+      {
+        c->sub.lit = t->base;
+        Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
+                 "inflate:         literal '%c'\n" :
+                 "inflate:         literal 0x%02x\n", t->base));
+        c->mode = LIT;
+        break;
+      }
+      if (e & 16)               /* length */
+      {
+        c->sub.copy.get = e & 15;
+        c->len = t->base;
+        c->mode = LENEXT;
+        break;
+      }
+      if ((e & 64) == 0)        /* next table */
+      {
+        c->sub.code.need = e;
+        c->sub.code.tree = t->next;
+        break;
+      }
+      if (e & 32)               /* end of block */
+      {
+        Tracevv((stderr, "inflate:         end of block\n"));
+        c->mode = WASH;
+        break;
+      }
+      c->mode = BADCODE;        /* invalid code */
+      z->msg = (char*)"invalid literal/length code";
+      r = Z_DATA_ERROR;
+      LEAVE
+    case LENEXT:        /* i: getting length extra (have base) */
+      j = c->sub.copy.get;
+      NEEDBITS(j)
+      c->len += (uInt)b & inflate_mask[j];
+      DUMPBITS(j)
+      c->sub.code.need = c->dbits;
+      c->sub.code.tree = c->dtree;
+      Tracevv((stderr, "inflate:         length %u\n", c->len));
+      c->mode = DIST;
+    case DIST:          /* i: get distance next */
+      j = c->sub.code.need;
+      NEEDBITS(j)
+      t = c->sub.code.tree + ((uInt)b & inflate_mask[j]);
+      DUMPBITS(t->bits)
+      e = (uInt)(t->exop);
+      if (e & 16)               /* distance */
+      {
+        c->sub.copy.get = e & 15;
+        c->sub.copy.dist = t->base;
+        c->mode = DISTEXT;
+        break;
+      }
+      if ((e & 64) == 0)        /* next table */
+      {
+        c->sub.code.need = e;
+        c->sub.code.tree = t->next;
+        break;
+      }
+      c->mode = BADCODE;        /* invalid code */
+      z->msg = (char*)"invalid distance code";
+      r = Z_DATA_ERROR;
+      LEAVE
+    case DISTEXT:       /* i: getting distance extra */
+      j = c->sub.copy.get;
+      NEEDBITS(j)
+      c->sub.copy.dist += (uInt)b & inflate_mask[j];
+      DUMPBITS(j)
+      Tracevv((stderr, "inflate:         distance %u\n", c->sub.copy.dist));
+      c->mode = COPY;
+    case COPY:          /* o: copying bytes in window, waiting for space */
+#ifndef __TURBOC__ /* Turbo C bug for following expression */
+      f = (uInt)(q - s->window) < c->sub.copy.dist ?
+          s->end - (c->sub.copy.dist - (q - s->window)) :
+          q - c->sub.copy.dist;
+#else
+      f = q - c->sub.copy.dist;
+      if ((uInt)(q - s->window) < c->sub.copy.dist)
+        f = s->end - (c->sub.copy.dist - (uInt)(q - s->window));
+#endif
+      while (c->len)
+      {
+        NEEDOUT
+        OUTBYTE(*f++)
+        if (f == s->end)
+          f = s->window;
+        c->len--;
+      }
+      c->mode = START;
+      break;
+    case LIT:           /* o: got literal, waiting for output space */
+      NEEDOUT
+      OUTBYTE(c->sub.lit)
+      c->mode = START;
+      break;
+    case WASH:          /* o: got eob, possibly more output */
+      FLUSH
+      if (s->read != s->write)
+        LEAVE
+      c->mode = END;
+    case END:
+      r = Z_STREAM_END;
+      LEAVE
+    case BADCODE:       /* x: got error */
+      r = Z_DATA_ERROR;
+      LEAVE
+    default:
+      r = Z_STREAM_ERROR;
+      LEAVE
+  }
+}
+
+
+void inflate_codes_free(c, z)
+inflate_codes_statef *c;
+z_streamp z;
+{
+  ZFREE(z, c);
+  Tracev((stderr, "inflate:       codes free\n"));
+}
+/* --- infcodes.c */
+
+/* +++ infutil.c */
+/* inflate_util.c -- data and routines common to blocks and codes
+ * Copyright (C) 1995-1996 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* #include "zutil.h" */
+/* #include "infblock.h" */
+/* #include "inftrees.h" */
+/* #include "infcodes.h" */
+/* #include "infutil.h" */
+
+#ifndef NO_DUMMY_DECL
+struct inflate_codes_state {int dummy;}; /* for buggy compilers */
+#endif
+
+/* And'ing with mask[n] masks the lower n bits */
+uInt inflate_mask[17] = {
+    0x0000,
+    0x0001, 0x0003, 0x0007, 0x000f, 0x001f, 0x003f, 0x007f, 0x00ff,
+    0x01ff, 0x03ff, 0x07ff, 0x0fff, 0x1fff, 0x3fff, 0x7fff, 0xffff
+};
+
+
+/* copy as much as possible from the sliding window to the output area */
+int inflate_flush(s, z, r)
+inflate_blocks_statef *s;
+z_streamp z;
+int r;
+{
+  uInt n;
+  Bytef *p;
+  Bytef *q;
+
+  /* local copies of source and destination pointers */
+  p = z->next_out;
+  q = s->read;
+
+  /* compute number of bytes to copy as far as end of window */
+  n = (uInt)((q <= s->write ? s->write : s->end) - q);
+  if (n > z->avail_out) n = z->avail_out;
+  if (n && r == Z_BUF_ERROR) r = Z_OK;
+
+  /* update counters */
+  z->avail_out -= n;
+  z->total_out += n;
+
+  /* update check information */
+  if (s->checkfn != Z_NULL)
+    z->adler = s->check = (*s->checkfn)(s->check, q, n);
+
+  /* copy as far as end of window */
+  if (p != Z_NULL) {
+    zmemcpy(p, q, n);
+    p += n;
+  }
+  q += n;
+
+  /* see if more to copy at beginning of window */
+  if (q == s->end)
+  {
+    /* wrap pointers */
+    q = s->window;
+    if (s->write == s->end)
+      s->write = s->window;
+
+    /* compute bytes to copy */
+    n = (uInt)(s->write - q);
+    if (n > z->avail_out) n = z->avail_out;
+    if (n && r == Z_BUF_ERROR) r = Z_OK;
+
+    /* update counters */
+    z->avail_out -= n;
+    z->total_out += n;
+
+    /* update check information */
+    if (s->checkfn != Z_NULL)
+      z->adler = s->check = (*s->checkfn)(s->check, q, n);
+
+    /* copy */
+    if (p != Z_NULL) {
+      zmemcpy(p, q, n);
+      p += n;
+    }
+    q += n;
+  }
+
+  /* update pointers */
+  z->next_out = p;
+  s->read = q;
+
+  /* done */
+  return r;
+}
+/* --- infutil.c */
+
+/* +++ inffast.c */
+/* inffast.c -- process literals and length/distance pairs fast
+ * Copyright (C) 1995-1996 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* #include "zutil.h" */
+/* #include "inftrees.h" */
+/* #include "infblock.h" */
+/* #include "infcodes.h" */
+/* #include "infutil.h" */
+/* #include "inffast.h" */
+
+#ifndef NO_DUMMY_DECL
+struct inflate_codes_state {int dummy;}; /* for buggy compilers */
+#endif
+
+/* simplify the use of the inflate_huft type with some defines */
+#define base more.Base
+#define next more.Next
+#define exop word.what.Exop
+#define bits word.what.Bits
+
+/* macros for bit input with no checking and for returning unused bytes */
+#define GRABBITS(j) {while(k<(j)){b|=((uLong)NEXTBYTE)<<k;k+=8;}}
+#define UNGRAB {n+=(c=k>>3);p-=c;k&=7;}
+
+/* Called with number of bytes left to write in window at least 258
+   (the maximum string length) and number of input bytes available
+   at least ten.  The ten bytes are six bytes for the longest length/
+   distance pair plus four bytes for overloading the bit buffer. */
+
+int inflate_fast(bl, bd, tl, td, s, z)
+uInt bl, bd;
+inflate_huft *tl;
+inflate_huft *td; /* need separate declaration for Borland C++ */
+inflate_blocks_statef *s;
+z_streamp z;
+{
+  inflate_huft *t;      /* temporary pointer */
+  uInt e;               /* extra bits or operation */
+  uLong b;              /* bit buffer */
+  uInt k;               /* bits in bit buffer */
+  Bytef *p;             /* input data pointer */
+  uInt n;               /* bytes available there */
+  Bytef *q;             /* output window write pointer */
+  uInt m;               /* bytes to end of window or read pointer */
+  uInt ml;              /* mask for literal/length tree */
+  uInt md;              /* mask for distance tree */
+  uInt c;               /* bytes to copy */
+  uInt d;               /* distance back to copy from */
+  Bytef *r;             /* copy source pointer */
+
+  /* load input, output, bit values */
+  LOAD
+
+  /* initialize masks */
+  ml = inflate_mask[bl];
+  md = inflate_mask[bd];
+
+  /* do until not enough input or output space for fast loop */
+  do {                          /* assume called with m >= 258 && n >= 10 */
+    /* get literal/length code */
+    GRABBITS(20)                /* max bits for literal/length code */
+    if ((e = (t = tl + ((uInt)b & ml))->exop) == 0)
+    {
+      DUMPBITS(t->bits)
+      Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
+                "inflate:         * literal '%c'\n" :
+                "inflate:         * literal 0x%02x\n", t->base));
+      *q++ = (Byte)t->base;
+      m--;
+      continue;
+    }
+    do {
+      DUMPBITS(t->bits)
+      if (e & 16)
+      {
+        /* get extra bits for length */
+        e &= 15;
+        c = t->base + ((uInt)b & inflate_mask[e]);
+        DUMPBITS(e)
+        Tracevv((stderr, "inflate:         * length %u\n", c));
+
+        /* decode distance base of block to copy */
+        GRABBITS(15);           /* max bits for distance code */
+        e = (t = td + ((uInt)b & md))->exop;
+        do {
+          DUMPBITS(t->bits)
+          if (e & 16)
+          {
+            /* get extra bits to add to distance base */
+            e &= 15;
+            GRABBITS(e)         /* get extra bits (up to 13) */
+            d = t->base + ((uInt)b & inflate_mask[e]);
+            DUMPBITS(e)
+            Tracevv((stderr, "inflate:         * distance %u\n", d));
+
+            /* do the copy */
+            m -= c;
+            if ((uInt)(q - s->window) >= d)     /* offset before dest */
+            {                                   /*  just copy */
+              r = q - d;
+              *q++ = *r++;  c--;        /* minimum count is three, */
+              *q++ = *r++;  c--;        /*  so unroll loop a little */
+            }
+            else                        /* else offset after destination */
+            {
+              e = d - (uInt)(q - s->window); /* bytes from offset to end */
+              r = s->end - e;           /* pointer to offset */
+              if (c > e)                /* if source crosses, */
+              {
+                c -= e;                 /* copy to end of window */
+                do {
+                  *q++ = *r++;
+                } while (--e);
+                r = s->window;          /* copy rest from start of window */
+              }
+            }
+            do {                        /* copy all or what's left */
+              *q++ = *r++;
+            } while (--c);
+            break;
+          }
+          else if ((e & 64) == 0)
+            e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop;
+          else
+          {
+            z->msg = (char*)"invalid distance code";
+            UNGRAB
+            UPDATE
+            return Z_DATA_ERROR;
+          }
+        } while (1);
+        break;
+      }
+      if ((e & 64) == 0)
+      {
+        if ((e = (t = t->next + ((uInt)b & inflate_mask[e]))->exop) == 0)
+        {
+          DUMPBITS(t->bits)
+          Tracevv((stderr, t->base >= 0x20 && t->base < 0x7f ?
+                    "inflate:         * literal '%c'\n" :
+                    "inflate:         * literal 0x%02x\n", t->base));
+          *q++ = (Byte)t->base;
+          m--;
+          break;
+        }
+      }
+      else if (e & 32)
+      {
+        Tracevv((stderr, "inflate:         * end of block\n"));
+        UNGRAB
+        UPDATE
+        return Z_STREAM_END;
+      }
+      else
+      {
+        z->msg = (char*)"invalid literal/length code";
+        UNGRAB
+        UPDATE
+        return Z_DATA_ERROR;
+      }
+    } while (1);
+  } while (m >= 258 && n >= 10);
+
+  /* not enough input or output--restore pointers and return */
+  UNGRAB
+  UPDATE
+  return Z_OK;
+}
+/* --- inffast.c */
+
+/* +++ zutil.c */
+/* zutil.c -- target dependent utility functions for the compression library
+ * Copyright (C) 1995-1996 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* From: zutil.c,v 1.17 1996/07/24 13:41:12 me Exp $ */
+
+/* #include "zutil.h" */
+
+#ifndef NO_DUMMY_DECL
+struct internal_state      {int dummy;}; /* for buggy compilers */
+#endif
+
+#ifndef STDC
+extern void exit OF((int));
+#endif
+
+const char *z_errmsg[10] = {
+"need dictionary",     /* Z_NEED_DICT       2  */
+"stream end",          /* Z_STREAM_END      1  */
+"",                    /* Z_OK              0  */
+"file error",          /* Z_ERRNO         (-1) */
+"stream error",        /* Z_STREAM_ERROR  (-2) */
+"data error",          /* Z_DATA_ERROR    (-3) */
+"insufficient memory", /* Z_MEM_ERROR     (-4) */
+"buffer error",        /* Z_BUF_ERROR     (-5) */
+"incompatible version",/* Z_VERSION_ERROR (-6) */
+""};
+
+
+const char *zlibVersion()
+{
+    return ZLIB_VERSION;
+}
+
+#ifdef DEBUG_ZLIB
+void z_error (m)
+    char *m;
+{
+    fprintf(stderr, "%s\n", m);
+    exit(1);
+}
+#endif
+
+#ifndef HAVE_MEMCPY
+
+void zmemcpy(dest, source, len)
+    Bytef* dest;
+    Bytef* source;
+    uInt  len;
+{
+    if (len == 0) return;
+    do {
+        *dest++ = *source++; /* ??? to be unrolled */
+    } while (--len != 0);
+}
+
+int zmemcmp(s1, s2, len)
+    Bytef* s1;
+    Bytef* s2;
+    uInt  len;
+{
+    uInt j;
+
+    for (j = 0; j < len; j++) {
+        if (s1[j] != s2[j]) return 2*(s1[j] > s2[j])-1;
+    }
+    return 0;
+}
+
+void zmemzero(dest, len)
+    Bytef* dest;
+    uInt  len;
+{
+    if (len == 0) return;
+    do {
+        *dest++ = 0;  /* ??? to be unrolled */
+    } while (--len != 0);
+}
+#endif
+
+#ifdef __TURBOC__
+#if (defined( __BORLANDC__) || !defined(SMALL_MEDIUM)) && !defined(__32BIT__)
+/* Small and medium model in Turbo C are for now limited to near allocation
+ * with reduced MAX_WBITS and MAX_MEM_LEVEL
+ */
+#  define MY_ZCALLOC
+
+/* Turbo C malloc() does not allow dynamic allocation of 64K bytes
+ * and farmalloc(64K) returns a pointer with an offset of 8, so we
+ * must fix the pointer. Warning: the pointer must be put back to its
+ * original form in order to free it, use zcfree().
+ */
+
+#define MAX_PTR 10
+/* 10*64K = 640K */
+
+local int next_ptr = 0;
+
+typedef struct ptr_table_s {
+    voidpf org_ptr;
+    voidpf new_ptr;
+} ptr_table;
+
+local ptr_table table[MAX_PTR];
+/* This table is used to remember the original form of pointers
+ * to large buffers (64K). Such pointers are normalized with a zero offset.
+ * Since MSDOS is not a preemptive multitasking OS, this table is not
+ * protected from concurrent access. This hack doesn't work anyway on
+ * a protected system like OS/2. Use Microsoft C instead.
+ */
+
+voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
+{
+    voidpf buf = opaque; /* just to make some compilers happy */
+    ulg bsize = (ulg)items*size;
+
+    /* If we allocate less than 65520 bytes, we assume that farmalloc
+     * will return a usable pointer which doesn't have to be normalized.
+     */
+    if (bsize < 65520L) {
+        buf = farmalloc(bsize);
+        if (*(ush*)&buf != 0) return buf;
+    } else {
+        buf = farmalloc(bsize + 16L);
+    }
+    if (buf == NULL || next_ptr >= MAX_PTR) return NULL;
+    table[next_ptr].org_ptr = buf;
+
+    /* Normalize the pointer to seg:0 */
+    *((ush*)&buf+1) += ((ush)((uch*)buf-0) + 15) >> 4;
+    *(ush*)&buf = 0;
+    table[next_ptr++].new_ptr = buf;
+    return buf;
+}
+
+void  zcfree (voidpf opaque, voidpf ptr)
+{
+    int n;
+    if (*(ush*)&ptr != 0) { /* object < 64K */
+        farfree(ptr);
+        return;
+    }
+    /* Find the original pointer */
+    for (n = 0; n < next_ptr; n++) {
+        if (ptr != table[n].new_ptr) continue;
+
+        farfree(table[n].org_ptr);
+        while (++n < next_ptr) {
+            table[n-1] = table[n];
+        }
+        next_ptr--;
+        return;
+    }
+    ptr = opaque; /* just to make some compilers happy */
+    Assert(0, "zcfree: ptr not found");
+}
+#endif
+#endif /* __TURBOC__ */
+
+
+#if defined(M_I86) && !defined(__32BIT__)
+/* Microsoft C in 16-bit mode */
+
+#  define MY_ZCALLOC
+
+#if (!defined(_MSC_VER) || (_MSC_VER < 600))
+#  define _halloc  halloc
+#  define _hfree   hfree
+#endif
+
+voidpf zcalloc (voidpf opaque, unsigned items, unsigned size)
+{
+    if (opaque) opaque = 0; /* to make compiler happy */
+    return _halloc((long)items, size);
+}
+
+void  zcfree (voidpf opaque, voidpf ptr)
+{
+    if (opaque) opaque = 0; /* to make compiler happy */
+    _hfree(ptr);
+}
+
+#endif /* MSC */
+
+
+#ifndef MY_ZCALLOC /* Any system without a special alloc function */
+
+#ifndef STDC
+extern voidp  calloc OF((uInt items, uInt size));
+extern void   free   OF((voidpf ptr));
+#endif
+
+voidpf zcalloc (opaque, items, size)
+    voidpf opaque;
+    unsigned items;
+    unsigned size;
+{
+    if (opaque) items += size - size; /* make compiler happy */
+    return (voidpf)calloc(items, size);
+}
+
+void  zcfree (opaque, ptr)
+    voidpf opaque;
+    voidpf ptr;
+{
+    free(ptr);
+    if (opaque) return; /* make compiler happy */
+}
+
+#endif /* MY_ZCALLOC */
+/* --- zutil.c */
+
+/* +++ adler32.c */
+/* adler32.c -- compute the Adler-32 checksum of a data stream
+ * Copyright (C) 1995-1996 Mark Adler
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* From: adler32.c,v 1.10 1996/05/22 11:52:18 me Exp $ */
+
+/* #include "zlib.h" */
+
+#define BASE 65521L /* largest prime smaller than 65536 */
+#define NMAX 5552
+/* NMAX is the largest n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1 */
+
+#define DO1(buf,i)  {s1 += buf[i]; s2 += s1;}
+#define DO2(buf,i)  DO1(buf,i); DO1(buf,i+1);
+#define DO4(buf,i)  DO2(buf,i); DO2(buf,i+2);
+#define DO8(buf,i)  DO4(buf,i); DO4(buf,i+4);
+#define DO16(buf)   DO8(buf,0); DO8(buf,8);
+
+/* ========================================================================= */
+uLong adler32(adler, buf, len)
+    uLong adler;
+    const Bytef *buf;
+    uInt len;
+{
+    unsigned long s1 = adler & 0xffff;
+    unsigned long s2 = (adler >> 16) & 0xffff;
+    int k;
+
+    if (buf == Z_NULL) return 1L;
+
+    while (len > 0) {
+        k = len < NMAX ? len : NMAX;
+        len -= k;
+        while (k >= 16) {
+            DO16(buf);
+           buf += 16;
+            k -= 16;
+        }
+        if (k != 0) do {
+            s1 += *buf++;
+           s2 += s1;
+        } while (--k);
+        s1 %= BASE;
+        s2 %= BASE;
+    }
+    return (s2 << 16) | s1;
+}
+/* --- adler32.c */
diff --git a/fs/jffs2/zlib.h b/fs/jffs2/zlib.h
new file mode 100644 (file)
index 0000000..cf91535
--- /dev/null
@@ -0,0 +1,1010 @@
+/*     $Id: zlib.h,v 1.2 1997/12/23 10:47:44 paulus Exp $      */
+
+/*
+ * This file is derived from zlib.h and zconf.h from the zlib-1.0.4
+ * distribution by Jean-loup Gailly and Mark Adler, with some additions
+ * by Paul Mackerras to aid in implementing Deflate compression and
+ * decompression for PPP packets.
+ */
+
+/*
+ *  ==FILEVERSION 971127==
+ *
+ * This marker is used by the Linux installation script to determine
+ * whether an up-to-date version of this file is already installed.
+ */
+
+
+/* +++ zlib.h */
+/* zlib.h -- interface of the 'zlib' general purpose compression library
+  version 1.0.4, Jul 24th, 1996.
+
+  Copyright (C) 1995-1996 Jean-loup Gailly and Mark Adler
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+
+  Jean-loup Gailly        Mark Adler
+  gzip@prep.ai.mit.edu    madler@alumni.caltech.edu
+
+
+  The data format used by the zlib library is described by RFCs (Request for
+  Comments) 1950 to 1952 in the files ftp://ds.internic.net/rfc/rfc1950.txt
+  (zlib format), rfc1951.txt (deflate format) and rfc1952.txt (gzip format).
+*/
+
+#ifndef _ZLIB_H
+#define _ZLIB_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* +++ zconf.h */
+/* zconf.h -- configuration of the zlib compression library
+ * Copyright (C) 1995-1996 Jean-loup Gailly.
+ * For conditions of distribution and use, see copyright notice in zlib.h 
+ */
+
+/* From: zconf.h,v 1.20 1996/07/02 15:09:28 me Exp $ */
+
+#ifndef _ZCONF_H
+#define _ZCONF_H
+
+/*
+ * If you *really* need a unique prefix for all types and library functions,
+ * compile with -DZ_PREFIX. The "standard" zlib should be compiled without it.
+ */
+#ifdef Z_PREFIX
+#  define deflateInit_ z_deflateInit_
+#  define deflate      z_deflate
+#  define deflateEnd   z_deflateEnd
+#  define inflateInit_         z_inflateInit_
+#  define inflate      z_inflate
+#  define inflateEnd   z_inflateEnd
+#  define deflateInit2_        z_deflateInit2_
+#  define deflateSetDictionary z_deflateSetDictionary
+#  define deflateCopy  z_deflateCopy
+#  define deflateReset z_deflateReset
+#  define deflateParams        z_deflateParams
+#  define inflateInit2_        z_inflateInit2_
+#  define inflateSetDictionary z_inflateSetDictionary
+#  define inflateSync  z_inflateSync
+#  define inflateReset z_inflateReset
+#  define compress     z_compress
+#  define uncompress   z_uncompress
+#  define adler32      z_adler32
+#  define crc32                z_crc32
+#  define get_crc_table z_get_crc_table
+
+#  define Byte         z_Byte
+#  define uInt         z_uInt
+#  define uLong                z_uLong
+#  define Bytef                z_Bytef
+#  define charf                z_charf
+#  define intf         z_intf
+#  define uIntf                z_uIntf
+#  define uLongf       z_uLongf
+#  define voidpf       z_voidpf
+#  define voidp                z_voidp
+#endif
+
+#if (defined(_WIN32) || defined(__WIN32__)) && !defined(WIN32)
+#  define WIN32
+#endif
+#if defined(__GNUC__) || defined(WIN32) || defined(__386__) || defined(i386)
+#  ifndef __32BIT__
+#    define __32BIT__
+#  endif
+#endif
+#if defined(__MSDOS__) && !defined(MSDOS)
+#  define MSDOS
+#endif
+
+/*
+ * Compile with -DMAXSEG_64K if the alloc function cannot allocate more
+ * than 64k bytes at a time (needed on systems with 16-bit int).
+ */
+#if defined(MSDOS) && !defined(__32BIT__)
+#  define MAXSEG_64K
+#endif
+#ifdef MSDOS
+#  define UNALIGNED_OK
+#endif
+
+#if (defined(MSDOS) || defined(_WINDOWS) || defined(WIN32))  && !defined(STDC)
+#  define STDC
+#endif
+#if (defined(__STDC__) || defined(__cplusplus)) && !defined(STDC)
+#  define STDC
+#endif
+
+#ifndef STDC
+#  ifndef const /* cannot use !defined(STDC) && !defined(const) on Mac */
+#    define const
+#  endif
+#endif
+
+/* Some Mac compilers merge all .h files incorrectly: */
+#if defined(__MWERKS__) || defined(applec) ||defined(THINK_C) ||defined(__SC__)
+#  define NO_DUMMY_DECL
+#endif
+
+/* Maximum value for memLevel in deflateInit2 */
+#ifndef MAX_MEM_LEVEL
+#  ifdef MAXSEG_64K
+#    define MAX_MEM_LEVEL 8
+#  else
+#    define MAX_MEM_LEVEL 9
+#  endif
+#endif
+
+/* Maximum value for windowBits in deflateInit2 and inflateInit2 */
+#ifndef MAX_WBITS
+#  define MAX_WBITS   15 /* 32K LZ77 window */
+#endif
+
+/* The memory requirements for deflate are (in bytes):
+            1 << (windowBits+2)   +  1 << (memLevel+9)
+ that is: 128K for windowBits=15  +  128K for memLevel = 8  (default values)
+ plus a few kilobytes for small objects. For example, if you want to reduce
+ the default memory requirements from 256K to 128K, compile with
+     make CFLAGS="-O -DMAX_WBITS=14 -DMAX_MEM_LEVEL=7"
+ Of course this will generally degrade compression (there's no free lunch).
+
+   The memory requirements for inflate are (in bytes) 1 << windowBits
+ that is, 32K for windowBits=15 (default value) plus a few kilobytes
+ for small objects.
+*/
+
+                        /* Type declarations */
+
+#ifndef OF /* function prototypes */
+#  ifdef STDC
+#    define OF(args)  args
+#  else
+#    define OF(args)  ()
+#  endif
+#endif
+
+/* The following definitions for FAR are needed only for MSDOS mixed
+ * model programming (small or medium model with some far allocations).
+ * This was tested only with MSC; for other MSDOS compilers you may have
+ * to define NO_MEMCPY in zutil.h.  If you don't need the mixed model,
+ * just define FAR to be empty.
+ */
+#if (defined(M_I86SM) || defined(M_I86MM)) && !defined(__32BIT__)
+   /* MSC small or medium model */
+#  define SMALL_MEDIUM
+#  ifdef _MSC_VER
+#    define FAR __far
+#  else
+#    define FAR far
+#  endif
+#endif
+#if defined(__BORLANDC__) && (defined(__SMALL__) || defined(__MEDIUM__))
+#  ifndef __32BIT__
+#    define SMALL_MEDIUM
+#    define FAR __far
+#  endif
+#endif
+#ifndef FAR
+#   define FAR
+#endif
+
+typedef unsigned char  Byte;  /* 8 bits */
+typedef unsigned int   uInt;  /* 16 bits or more */
+typedef unsigned long  uLong; /* 32 bits or more */
+
+#if defined(__BORLANDC__) && defined(SMALL_MEDIUM)
+   /* Borland C/C++ ignores FAR inside typedef */
+#  define Bytef Byte FAR
+#else
+   typedef Byte  FAR Bytef;
+#endif
+typedef char  FAR charf;
+typedef int   FAR intf;
+typedef uInt  FAR uIntf;
+typedef uLong FAR uLongf;
+
+#ifdef STDC
+   typedef void FAR *voidpf;
+   typedef void     *voidp;
+#else
+   typedef Byte FAR *voidpf;
+   typedef Byte     *voidp;
+#endif
+
+
+/* Compile with -DZLIB_DLL for Windows DLL support */
+#if (defined(_WINDOWS) || defined(WINDOWS)) && defined(ZLIB_DLL)
+#  include <windows.h>
+#  define EXPORT  WINAPI
+#else
+#  define EXPORT
+#endif
+
+#endif /* _ZCONF_H */
+/* --- zconf.h */
+
+#define ZLIB_VERSION "1.0.4P"
+
+/* 
+     The 'zlib' compression library provides in-memory compression and
+  decompression functions, including integrity checks of the uncompressed
+  data.  This version of the library supports only one compression method
+  (deflation) but other algorithms may be added later and will have the same
+  stream interface.
+
+     For compression the application must provide the output buffer and
+  may optionally provide the input buffer for optimization. For decompression,
+  the application must provide the input buffer and may optionally provide
+  the output buffer for optimization.
+
+     Compression can be done in a single step if the buffers are large
+  enough (for example if an input file is mmap'ed), or can be done by
+  repeated calls of the compression function.  In the latter case, the
+  application must provide more input and/or consume the output
+  (providing more output space) before each call.
+
+     The library does not install any signal handler. It is recommended to
+  add at least a handler for SIGSEGV when decompressing; the library checks
+  the consistency of the input data whenever possible but may go nuts
+  for some forms of corrupted input.
+*/
+
+typedef voidpf (*alloc_func) OF((voidpf opaque, uInt items, uInt size));
+typedef void   (*free_func)  OF((voidpf opaque, voidpf address));
+
+struct internal_state;
+
+typedef struct z_stream_s {
+    Bytef    *next_in;  /* next input byte */
+    uInt     avail_in;  /* number of bytes available at next_in */
+    uLong    total_in;  /* total nb of input bytes read so far */
+
+    Bytef    *next_out; /* next output byte should be put there */
+    uInt     avail_out; /* remaining free space at next_out */
+    uLong    total_out; /* total nb of bytes output so far */
+
+    char     *msg;      /* last error message, NULL if no error */
+    struct internal_state FAR *state; /* not visible by applications */
+
+    alloc_func zalloc;  /* used to allocate the internal state */
+    free_func  zfree;   /* used to free the internal state */
+    voidpf     opaque;  /* private data object passed to zalloc and zfree */
+
+    int     data_type;  /* best guess about the data type: ascii or binary */
+    uLong   adler;      /* adler32 value of the uncompressed data */
+    uLong   reserved;   /* reserved for future use */
+} z_stream;
+
+typedef z_stream FAR *z_streamp;
+
+/*
+   The application must update next_in and avail_in when avail_in has
+   dropped to zero. It must update next_out and avail_out when avail_out
+   has dropped to zero. The application must initialize zalloc, zfree and
+   opaque before calling the init function. All other fields are set by the
+   compression library and must not be updated by the application.
+
+   The opaque value provided by the application will be passed as the first
+   parameter for calls of zalloc and zfree. This can be useful for custom
+   memory management. The compression library attaches no meaning to the
+   opaque value.
+
+   zalloc must return Z_NULL if there is not enough memory for the object.
+   On 16-bit systems, the functions zalloc and zfree must be able to allocate
+   exactly 65536 bytes, but will not be required to allocate more than this
+   if the symbol MAXSEG_64K is defined (see zconf.h). WARNING: On MSDOS,
+   pointers returned by zalloc for objects of exactly 65536 bytes *must*
+   have their offset normalized to zero. The default allocation function
+   provided by this library ensures this (see zutil.c). To reduce memory
+   requirements and avoid any allocation of 64K objects, at the expense of
+   compression ratio, compile the library with -DMAX_WBITS=14 (see zconf.h).
+
+   The fields total_in and total_out can be used for statistics or
+   progress reports. After compression, total_in holds the total size of
+   the uncompressed data and may be saved for use in the decompressor
+   (particularly if the decompressor wants to decompress everything in
+   a single step).
+*/
+
+                        /* constants */
+
+#define Z_NO_FLUSH      0
+#define Z_PARTIAL_FLUSH 1
+#define Z_PACKET_FLUSH 2
+#define Z_SYNC_FLUSH    3
+#define Z_FULL_FLUSH    4
+#define Z_FINISH        5
+/* Allowed flush values; see deflate() below for details */
+
+#define Z_OK            0
+#define Z_STREAM_END    1
+#define Z_NEED_DICT     2
+#define Z_ERRNO        (-1)
+#define Z_STREAM_ERROR (-2)
+#define Z_DATA_ERROR   (-3)
+#define Z_MEM_ERROR    (-4)
+#define Z_BUF_ERROR    (-5)
+#define Z_VERSION_ERROR (-6)
+/* Return codes for the compression/decompression functions. Negative
+ * values are errors, positive values are used for special but normal events.
+ */
+
+#define Z_NO_COMPRESSION         0
+#define Z_BEST_SPEED             1
+#define Z_BEST_COMPRESSION       9
+#define Z_DEFAULT_COMPRESSION  (-1)
+/* compression levels */
+
+#define Z_FILTERED            1
+#define Z_HUFFMAN_ONLY        2
+#define Z_DEFAULT_STRATEGY    0
+/* compression strategy; see deflateInit2() below for details */
+
+#define Z_BINARY   0
+#define Z_ASCII    1
+#define Z_UNKNOWN  2
+/* Possible values of the data_type field */
+
+#define Z_DEFLATED   8
+/* The deflate compression method (the only one supported in this version) */
+
+#define Z_NULL  0  /* for initializing zalloc, zfree, opaque */
+
+#define zlib_version zlibVersion()
+/* for compatibility with versions < 1.0.2 */
+
+                        /* basic functions */
+
+extern const char * EXPORT zlibVersion OF((void));
+/* The application can compare zlibVersion and ZLIB_VERSION for consistency.
+   If the first character differs, the library code actually used is
+   not compatible with the zlib.h header file used by the application.
+   This check is automatically made by deflateInit and inflateInit.
+ */
+
+/* 
+extern int EXPORT deflateInit OF((z_streamp strm, int level));
+
+     Initializes the internal stream state for compression. The fields
+   zalloc, zfree and opaque must be initialized before by the caller.
+   If zalloc and zfree are set to Z_NULL, deflateInit updates them to
+   use default allocation functions.
+
+     The compression level must be Z_DEFAULT_COMPRESSION, or between 0 and 9:
+   1 gives best speed, 9 gives best compression, 0 gives no compression at
+   all (the input data is simply copied a block at a time).
+   Z_DEFAULT_COMPRESSION requests a default compromise between speed and
+   compression (currently equivalent to level 6).
+
+     deflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
+   enough memory, Z_STREAM_ERROR if level is not a valid compression level,
+   Z_VERSION_ERROR if the zlib library version (zlib_version) is incompatible
+   with the version assumed by the caller (ZLIB_VERSION).
+   msg is set to null if there is no error message.  deflateInit does not
+   perform any compression: this will be done by deflate().
+*/
+
+
+extern int EXPORT deflate OF((z_streamp strm, int flush));
+/*
+  Performs one or both of the following actions:
+
+  - Compress more input starting at next_in and update next_in and avail_in
+    accordingly. If not all input can be processed (because there is not
+    enough room in the output buffer), next_in and avail_in are updated and
+    processing will resume at this point for the next call of deflate().
+
+  - Provide more output starting at next_out and update next_out and avail_out
+    accordingly. This action is forced if the parameter flush is non zero.
+    Forcing flush frequently degrades the compression ratio, so this parameter
+    should be set only when necessary (in interactive applications).
+    Some output may be provided even if flush is not set.
+
+  Before the call of deflate(), the application should ensure that at least
+  one of the actions is possible, by providing more input and/or consuming
+  more output, and updating avail_in or avail_out accordingly; avail_out
+  should never be zero before the call. The application can consume the
+  compressed output when it wants, for example when the output buffer is full
+  (avail_out == 0), or after each call of deflate(). If deflate returns Z_OK
+  and with zero avail_out, it must be called again after making room in the
+  output buffer because there might be more output pending.
+
+    If the parameter flush is set to Z_PARTIAL_FLUSH, the current compression
+  block is terminated and flushed to the output buffer so that the
+  decompressor can get all input data available so far. For method 9, a future
+  variant on method 8, the current block will be flushed but not terminated.
+  Z_SYNC_FLUSH has the same effect as partial flush except that the compressed
+  output is byte aligned (the compressor can clear its internal bit buffer)
+  and the current block is always terminated; this can be useful if the
+  compressor has to be restarted from scratch after an interruption (in which
+  case the internal state of the compressor may be lost).
+    If flush is set to Z_FULL_FLUSH, the compression block is terminated, a
+  special marker is output and the compression dictionary is discarded; this
+  is useful to allow the decompressor to synchronize if one compressed block
+  has been damaged (see inflateSync below).  Flushing degrades compression and
+  so should be used only when necessary.  Using Z_FULL_FLUSH too often can
+  seriously degrade the compression. If deflate returns with avail_out == 0,
+  this function must be called again with the same value of the flush
+  parameter and more output space (updated avail_out), until the flush is
+  complete (deflate returns with non-zero avail_out).
+
+    If the parameter flush is set to Z_PACKET_FLUSH, the compression
+  block is terminated, and a zero-length stored block is output,
+  omitting the length bytes (the effect of this is that the 3-bit type
+  code 000 for a stored block is output, and the output is then
+  byte-aligned).  This is designed for use at the end of a PPP packet.
+
+    If the parameter flush is set to Z_FINISH, pending input is processed,
+  pending output is flushed and deflate returns with Z_STREAM_END if there
+  was enough output space; if deflate returns with Z_OK, this function must be
+  called again with Z_FINISH and more output space (updated avail_out) but no
+  more input data, until it returns with Z_STREAM_END or an error. After
+  deflate has returned Z_STREAM_END, the only possible operations on the
+  stream are deflateReset or deflateEnd.
+  
+    Z_FINISH can be used immediately after deflateInit if all the compression
+  is to be done in a single step. In this case, avail_out must be at least
+  0.1% larger than avail_in plus 12 bytes.  If deflate does not return
+  Z_STREAM_END, then it must be called again as described above.
+
+    deflate() may update data_type if it can make a good guess about
+  the input data type (Z_ASCII or Z_BINARY). In doubt, the data is considered
+  binary. This field is only for information purposes and does not affect
+  the compression algorithm in any manner.
+
+    deflate() returns Z_OK if some progress has been made (more input
+  processed or more output produced), Z_STREAM_END if all input has been
+  consumed and all output has been produced (only when flush is set to
+  Z_FINISH), Z_STREAM_ERROR if the stream state was inconsistent (for example
+  if next_in or next_out was NULL), Z_BUF_ERROR if no progress is possible.
+*/
+
+
+extern int EXPORT deflateEnd OF((z_streamp strm));
+/*
+     All dynamically allocated data structures for this stream are freed.
+   This function discards any unprocessed input and does not flush any
+   pending output.
+
+     deflateEnd returns Z_OK if success, Z_STREAM_ERROR if the
+   stream state was inconsistent, Z_DATA_ERROR if the stream was freed
+   prematurely (some input or output was discarded). In the error case,
+   msg may be set but then points to a static string (which must not be
+   deallocated).
+*/
+
+
+/* 
+extern int EXPORT inflateInit OF((z_streamp strm));
+
+     Initializes the internal stream state for decompression. The fields
+   zalloc, zfree and opaque must be initialized before by the caller.  If
+   zalloc and zfree are set to Z_NULL, inflateInit updates them to use default
+   allocation functions.
+
+     inflateInit returns Z_OK if success, Z_MEM_ERROR if there was not
+   enough memory, Z_VERSION_ERROR if the zlib library version is incompatible
+   with the version assumed by the caller.  msg is set to null if there is no
+   error message. inflateInit does not perform any decompression: this will be
+   done by inflate().
+*/
+
+
+extern int EXPORT inflate OF((z_streamp strm, int flush));
+/*
+  Performs one or both of the following actions:
+
+  - Decompress more input starting at next_in and update next_in and avail_in
+    accordingly. If not all input can be processed (because there is not
+    enough room in the output buffer), next_in is updated and processing
+    will resume at this point for the next call of inflate().
+
+  - Provide more output starting at next_out and update next_out and avail_out
+    accordingly.  inflate() provides as much output as possible, until there
+    is no more input data or no more space in the output buffer (see below
+    about the flush parameter).
+
+  Before the call of inflate(), the application should ensure that at least
+  one of the actions is possible, by providing more input and/or consuming
+  more output, and updating the next_* and avail_* values accordingly.
+  The application can consume the uncompressed output when it wants, for
+  example when the output buffer is full (avail_out == 0), or after each
+  call of inflate(). If inflate returns Z_OK and with zero avail_out, it
+  must be called again after making room in the output buffer because there
+  might be more output pending.
+
+    If the parameter flush is set to Z_PARTIAL_FLUSH or Z_PACKET_FLUSH,
+  inflate flushes as much output as possible to the output buffer. The
+  flushing behavior of inflate is not specified for values of the flush
+  parameter other than Z_PARTIAL_FLUSH, Z_PACKET_FLUSH or Z_FINISH, but the
+  current implementation actually flushes as much output as possible
+  anyway.  For Z_PACKET_FLUSH, inflate checks that once all the input data
+  has been consumed, it is expecting to see the length field of a stored
+  block; if not, it returns Z_DATA_ERROR.
+
+    inflate() should normally be called until it returns Z_STREAM_END or an
+  error. However if all decompression is to be performed in a single step
+  (a single call of inflate), the parameter flush should be set to
+  Z_FINISH. In this case all pending input is processed and all pending
+  output is flushed; avail_out must be large enough to hold all the
+  uncompressed data. (The size of the uncompressed data may have been saved
+  by the compressor for this purpose.) The next operation on this stream must
+  be inflateEnd to deallocate the decompression state. The use of Z_FINISH
+  is never required, but can be used to inform inflate that a faster routine
+  may be used for the single inflate() call.
+
+    inflate() returns Z_OK if some progress has been made (more input
+  processed or more output produced), Z_STREAM_END if the end of the
+  compressed data has been reached and all uncompressed output has been
+  produced, Z_NEED_DICT if a preset dictionary is needed at this point (see
+  inflateSetDictionary below), Z_DATA_ERROR if the input data was corrupted,
+  Z_STREAM_ERROR if the stream structure was inconsistent (for example if
+  next_in or next_out was NULL), Z_MEM_ERROR if there was not enough memory,
+  Z_BUF_ERROR if no progress is possible or if there was not enough room in
+  the output buffer when Z_FINISH is used. In the Z_DATA_ERROR case, the
+  application may then call inflateSync to look for a good compression block.
+  In the Z_NEED_DICT case, strm->adler is set to the Adler32 value of the
+  dictionary chosen by the compressor.
+*/
+
+
+extern int EXPORT inflateEnd OF((z_streamp strm));
+/*
+     All dynamically allocated data structures for this stream are freed.
+   This function discards any unprocessed input and does not flush any
+   pending output.
+
+     inflateEnd returns Z_OK if success, Z_STREAM_ERROR if the stream state
+   was inconsistent. In the error case, msg may be set but then points to a
+   static string (which must not be deallocated).
+*/
+
+                        /* Advanced functions */
+
+/*
+    The following functions are needed only in some special applications.
+*/
+
+/*   
+extern int EXPORT deflateInit2 OF((z_streamp strm,
+                                   int  level,
+                                   int  method,
+                                   int  windowBits,
+                                   int  memLevel,
+                                   int  strategy));
+
+     This is another version of deflateInit with more compression options. The
+   fields next_in, zalloc, zfree and opaque must be initialized before by
+   the caller.
+
+     The method parameter is the compression method. It must be Z_DEFLATED in
+   this version of the library. (Method 9 will allow a 64K history buffer and
+   partial block flushes.)
+
+     The windowBits parameter is the base two logarithm of the window size
+   (the size of the history buffer).  It should be in the range 8..15 for this
+   version of the library (the value 16 will be allowed for method 9). Larger
+   values of this parameter result in better compression at the expense of
+   memory usage. The default value is 15 if deflateInit is used instead.
+
+     The memLevel parameter specifies how much memory should be allocated
+   for the internal compression state. memLevel=1 uses minimum memory but
+   is slow and reduces compression ratio; memLevel=9 uses maximum memory
+   for optimal speed. The default value is 8. See zconf.h for total memory
+   usage as a function of windowBits and memLevel.
+
+     The strategy parameter is used to tune the compression algorithm. Use the
+   value Z_DEFAULT_STRATEGY for normal data, Z_FILTERED for data produced by a
+   filter (or predictor), or Z_HUFFMAN_ONLY to force Huffman encoding only (no
+   string match).  Filtered data consists mostly of small values with a
+   somewhat random distribution. In this case, the compression algorithm is
+   tuned to compress them better. The effect of Z_FILTERED is to force more
+   Huffman coding and less string matching; it is somewhat intermediate
+   between Z_DEFAULT and Z_HUFFMAN_ONLY. The strategy parameter only affects
+   the compression ratio but not the correctness of the compressed output even
+   if it is not set appropriately.
+
+     If next_in is not null, the library will use this buffer to hold also
+   some history information; the buffer must either hold the entire input
+   data, or have at least 1<<(windowBits+1) bytes and be writable. If next_in
+   is null, the library will allocate its own history buffer (and leave next_in
+   null). next_out need not be provided here but must be provided by the
+   application for the next call of deflate().
+
+     If the history buffer is provided by the application, next_in must
+   must never be changed by the application since the compressor maintains
+   information inside this buffer from call to call; the application
+   must provide more input only by increasing avail_in. next_in is always
+   reset by the library in this case.
+
+      deflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was
+   not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as
+   an invalid method). msg is set to null if there is no error message.
+   deflateInit2 does not perform any compression: this will be done by
+   deflate(). 
+*/
+                            
+extern int EXPORT deflateSetDictionary OF((z_streamp strm,
+                                           const Bytef *dictionary,
+                                          uInt  dictLength));
+/*
+     Initializes the compression dictionary (history buffer) from the given
+   byte sequence without producing any compressed output. This function must
+   be called immediately after deflateInit or deflateInit2, before any call
+   of deflate. The compressor and decompressor must use exactly the same
+   dictionary (see inflateSetDictionary).
+     The dictionary should consist of strings (byte sequences) that are likely
+   to be encountered later in the data to be compressed, with the most commonly
+   used strings preferably put towards the end of the dictionary. Using a
+   dictionary is most useful when the data to be compressed is short and
+   can be predicted with good accuracy; the data can then be compressed better
+   than with the default empty dictionary. In this version of the library,
+   only the last 32K bytes of the dictionary are used.
+     Upon return of this function, strm->adler is set to the Adler32 value
+   of the dictionary; the decompressor may later use this value to determine
+   which dictionary has been used by the compressor. (The Adler32 value
+   applies to the whole dictionary even if only a subset of the dictionary is
+   actually used by the compressor.)
+
+     deflateSetDictionary returns Z_OK if success, or Z_STREAM_ERROR if a
+   parameter is invalid (such as NULL dictionary) or the stream state
+   is inconsistent (for example if deflate has already been called for this
+   stream). deflateSetDictionary does not perform any compression: this will
+   be done by deflate(). 
+*/
+
+extern int EXPORT deflateCopy OF((z_streamp dest,
+                                  z_streamp source));
+/*
+     Sets the destination stream as a complete copy of the source stream.  If
+   the source stream is using an application-supplied history buffer, a new
+   buffer is allocated for the destination stream.  The compressed output
+   buffer is always application-supplied. It's the responsibility of the
+   application to provide the correct values of next_out and avail_out for the
+   next call of deflate.
+
+     This function can be useful when several compression strategies will be
+   tried, for example when there are several ways of pre-processing the input
+   data with a filter. The streams that will be discarded should then be freed
+   by calling deflateEnd.  Note that deflateCopy duplicates the internal
+   compression state which can be quite large, so this strategy is slow and
+   can consume lots of memory.
+
+     deflateCopy returns Z_OK if success, Z_MEM_ERROR if there was not
+   enough memory, Z_STREAM_ERROR if the source stream state was inconsistent
+   (such as zalloc being NULL). msg is left unchanged in both source and
+   destination.
+*/
+
+extern int EXPORT deflateReset OF((z_streamp strm));
+/*
+     This function is equivalent to deflateEnd followed by deflateInit,
+   but does not free and reallocate all the internal compression state.
+   The stream will keep the same compression level and any other attributes
+   that may have been set by deflateInit2.
+
+      deflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent (such as zalloc or state being NULL).
+*/
+
+extern int EXPORT deflateParams OF((z_streamp strm, int level, int strategy));
+/*
+     Dynamically update the compression level and compression strategy.
+   This can be used to switch between compression and straight copy of
+   the input data, or to switch to a different kind of input data requiring
+   a different strategy. If the compression level is changed, the input
+   available so far is compressed with the old level (and may be flushed);
+   the new level will take effect only at the next call of deflate().
+
+     Before the call of deflateParams, the stream state must be set as for
+   a call of deflate(), since the currently available input may have to
+   be compressed and flushed. In particular, strm->avail_out must be non-zero.
+
+     deflateParams returns Z_OK if success, Z_STREAM_ERROR if the source
+   stream state was inconsistent or if a parameter was invalid, Z_BUF_ERROR
+   if strm->avail_out was zero.
+*/
+
+extern int EXPORT deflateOutputPending OF((z_streamp strm));
+/*
+     Returns the number of bytes of output which are immediately
+   available from the compressor (i.e. without any further input
+   or flush).
+*/
+
+/*   
+extern int EXPORT inflateInit2 OF((z_streamp strm,
+                                   int  windowBits));
+
+     This is another version of inflateInit with more compression options. The
+   fields next_out, zalloc, zfree and opaque must be initialized before by
+   the caller.
+
+     The windowBits parameter is the base two logarithm of the maximum window
+   size (the size of the history buffer).  It should be in the range 8..15 for
+   this version of the library (the value 16 will be allowed soon). The
+   default value is 15 if inflateInit is used instead. If a compressed stream
+   with a larger window size is given as input, inflate() will return with
+   the error code Z_DATA_ERROR instead of trying to allocate a larger window.
+
+     If next_out is not null, the library will use this buffer for the history
+   buffer; the buffer must either be large enough to hold the entire output
+   data, or have at least 1<<windowBits bytes.  If next_out is null, the
+   library will allocate its own buffer (and leave next_out null). next_in
+   need not be provided here but must be provided by the application for the
+   next call of inflate().
+
+     If the history buffer is provided by the application, next_out must
+   never be changed by the application since the decompressor maintains
+   history information inside this buffer from call to call; the application
+   can only reset next_out to the beginning of the history buffer when
+   avail_out is zero and all output has been consumed.
+
+      inflateInit2 returns Z_OK if success, Z_MEM_ERROR if there was
+   not enough memory, Z_STREAM_ERROR if a parameter is invalid (such as
+   windowBits < 8). msg is set to null if there is no error message.
+   inflateInit2 does not perform any decompression: this will be done by
+   inflate().
+*/
+
+extern int EXPORT inflateSetDictionary OF((z_streamp strm,
+                                          const Bytef *dictionary,
+                                          uInt  dictLength));
+/*
+     Initializes the decompression dictionary (history buffer) from the given
+   uncompressed byte sequence. This function must be called immediately after
+   a call of inflate if this call returned Z_NEED_DICT. The dictionary chosen
+   by the compressor can be determined from the Adler32 value returned by this
+   call of inflate. The compressor and decompressor must use exactly the same
+   dictionary (see deflateSetDictionary).
+
+     inflateSetDictionary returns Z_OK if success, Z_STREAM_ERROR if a
+   parameter is invalid (such as NULL dictionary) or the stream state is
+   inconsistent, Z_DATA_ERROR if the given dictionary doesn't match the
+   expected one (incorrect Adler32 value). inflateSetDictionary does not
+   perform any decompression: this will be done by subsequent calls of
+   inflate().
+*/
+
+extern int EXPORT inflateSync OF((z_streamp strm));
+/* 
+    Skips invalid compressed data until the special marker (see deflate()
+  above) can be found, or until all available input is skipped. No output
+  is provided.
+
+    inflateSync returns Z_OK if the special marker has been found, Z_BUF_ERROR
+  if no more input was provided, Z_DATA_ERROR if no marker has been found,
+  or Z_STREAM_ERROR if the stream structure was inconsistent. In the success
+  case, the application may save the current current value of total_in which
+  indicates where valid compressed data was found. In the error case, the
+  application may repeatedly call inflateSync, providing more input each time,
+  until success or end of the input data.
+*/
+
+extern int EXPORT inflateReset OF((z_streamp strm));
+/*
+     This function is equivalent to inflateEnd followed by inflateInit,
+   but does not free and reallocate all the internal decompression state.
+   The stream will keep attributes that may have been set by inflateInit2.
+
+      inflateReset returns Z_OK if success, or Z_STREAM_ERROR if the source
+   stream state was inconsistent (such as zalloc or state being NULL).
+*/
+
+extern int inflateIncomp OF((z_stream *strm));
+/*
+     This function adds the data at next_in (avail_in bytes) to the output
+   history without performing any output.  There must be no pending output,
+   and the decompressor must be expecting to see the start of a block.
+   Calling this function is equivalent to decompressing a stored block
+   containing the data at next_in (except that the data is not output).
+*/
+
+                        /* utility functions */
+
+/*
+     The following utility functions are implemented on top of the
+   basic stream-oriented functions. To simplify the interface, some
+   default options are assumed (compression level, window size,
+   standard memory allocation functions). The source code of these
+   utility functions can easily be modified if you need special options.
+*/
+
+extern int EXPORT compress OF((Bytef *dest,   uLongf *destLen,
+                              const Bytef *source, uLong sourceLen));
+/*
+     Compresses the source buffer into the destination buffer.  sourceLen is
+   the byte length of the source buffer. Upon entry, destLen is the total
+   size of the destination buffer, which must be at least 0.1% larger than
+   sourceLen plus 12 bytes. Upon exit, destLen is the actual size of the
+   compressed buffer.
+     This function can be used to compress a whole file at once if the
+   input file is mmap'ed.
+     compress returns Z_OK if success, Z_MEM_ERROR if there was not
+   enough memory, Z_BUF_ERROR if there was not enough room in the output
+   buffer.
+*/
+
+extern int EXPORT uncompress OF((Bytef *dest,   uLongf *destLen,
+                                const Bytef *source, uLong sourceLen));
+/*
+     Decompresses the source buffer into the destination buffer.  sourceLen is
+   the byte length of the source buffer. Upon entry, destLen is the total
+   size of the destination buffer, which must be large enough to hold the
+   entire uncompressed data. (The size of the uncompressed data must have
+   been saved previously by the compressor and transmitted to the decompressor
+   by some mechanism outside the scope of this compression library.)
+   Upon exit, destLen is the actual size of the compressed buffer.
+     This function can be used to decompress a whole file at once if the
+   input file is mmap'ed.
+
+     uncompress returns Z_OK if success, Z_MEM_ERROR if there was not
+   enough memory, Z_BUF_ERROR if there was not enough room in the output
+   buffer, or Z_DATA_ERROR if the input data was corrupted.
+*/
+
+
+typedef voidp gzFile;
+
+extern gzFile EXPORT gzopen  OF((const char *path, const char *mode));
+/*
+     Opens a gzip (.gz) file for reading or writing. The mode parameter
+   is as in fopen ("rb" or "wb") but can also include a compression level
+   ("wb9").  gzopen can be used to read a file which is not in gzip format;
+   in this case gzread will directly read from the file without decompression.
+     gzopen returns NULL if the file could not be opened or if there was
+   insufficient memory to allocate the (de)compression state; errno
+   can be checked to distinguish the two cases (if errno is zero, the
+   zlib error is Z_MEM_ERROR).
+*/
+
+extern gzFile EXPORT gzdopen  OF((int fd, const char *mode));
+/*
+     gzdopen() associates a gzFile with the file descriptor fd.  File
+   descriptors are obtained from calls like open, dup, creat, pipe or
+   fileno (in the file has been previously opened with fopen).
+   The mode parameter is as in gzopen.
+     The next call of gzclose on the returned gzFile will also close the
+   file descriptor fd, just like fclose(fdopen(fd), mode) closes the file
+   descriptor fd. If you want to keep fd open, use gzdopen(dup(fd), mode).
+     gzdopen returns NULL if there was insufficient memory to allocate
+   the (de)compression state.
+*/
+
+extern int EXPORT    gzread  OF((gzFile file, voidp buf, unsigned len));
+/*
+     Reads the given number of uncompressed bytes from the compressed file.
+   If the input file was not in gzip format, gzread copies the given number
+   of bytes into the buffer.
+     gzread returns the number of uncompressed bytes actually read (0 for
+   end of file, -1 for error). */
+
+extern int EXPORT    gzwrite OF((gzFile file, const voidp buf, unsigned len));
+/*
+     Writes the given number of uncompressed bytes into the compressed file.
+   gzwrite returns the number of uncompressed bytes actually written
+   (0 in case of error).
+*/
+
+extern int EXPORT    gzflush OF((gzFile file, int flush));
+/*
+     Flushes all pending output into the compressed file. The parameter
+   flush is as in the deflate() function. The return value is the zlib
+   error number (see function gzerror below). gzflush returns Z_OK if
+   the flush parameter is Z_FINISH and all output could be flushed.
+     gzflush should be called only when strictly necessary because it can
+   degrade compression.
+*/
+
+extern int EXPORT    gzclose OF((gzFile file));
+/*
+     Flushes all pending output if necessary, closes the compressed file
+   and deallocates all the (de)compression state. The return value is the zlib
+   error number (see function gzerror below).
+*/
+
+extern const char * EXPORT gzerror OF((gzFile file, int *errnum));
+/*
+     Returns the error message for the last error which occurred on the
+   given compressed file. errnum is set to zlib error number. If an
+   error occurred in the file system and not in the compression library,
+   errnum is set to Z_ERRNO and the application may consult errno
+   to get the exact error code.
+*/
+
+                        /* checksum functions */
+
+/*
+     These functions are not related to compression but are exported
+   anyway because they might be useful in applications using the
+   compression library.
+*/
+
+extern uLong EXPORT adler32 OF((uLong adler, const Bytef *buf, uInt len));
+
+/*
+     Update a running Adler-32 checksum with the bytes buf[0..len-1] and
+   return the updated checksum. If buf is NULL, this function returns
+   the required initial value for the checksum.
+   An Adler-32 checksum is almost as reliable as a CRC32 but can be computed
+   much faster. Usage example:
+
+     uLong adler = adler32(0L, Z_NULL, 0);
+
+     while (read_buffer(buffer, length) != EOF) {
+       adler = adler32(adler, buffer, length);
+     }
+     if (adler != original_adler) error();
+*/
+
+extern uLong EXPORT crc32   OF((uLong crc, const Bytef *buf, uInt len));
+/*
+     Update a running crc with the bytes buf[0..len-1] and return the updated
+   crc. If buf is NULL, this function returns the required initial value
+   for the crc. Pre- and post-conditioning (one's complement) is performed
+   within this function so it shouldn't be done by the application.
+   Usage example:
+
+     uLong crc = crc32(0L, Z_NULL, 0);
+
+     while (read_buffer(buffer, length) != EOF) {
+       crc = crc32(crc, buffer, length);
+     }
+     if (crc != original_crc) error();
+*/
+
+
+                        /* various hacks, don't look :) */
+
+/* deflateInit and inflateInit are macros to allow checking the zlib version
+ * and the compiler's view of z_stream:
+ */
+extern int EXPORT deflateInit_ OF((z_streamp strm, int level,
+                                  const char *version, int stream_size));
+extern int EXPORT inflateInit_ OF((z_streamp strm,
+                                  const char *version, int stream_size));
+extern int EXPORT deflateInit2_ OF((z_streamp strm, int  level, int  method,
+                                   int windowBits, int memLevel, int strategy,
+                                   const char *version, int stream_size));
+extern int EXPORT inflateInit2_ OF((z_streamp strm, int  windowBits,
+                                   const char *version, int stream_size));
+#define deflateInit(strm, level) \
+        deflateInit_((strm), (level),       ZLIB_VERSION, sizeof(z_stream))
+#define inflateInit(strm) \
+        inflateInit_((strm),                ZLIB_VERSION, sizeof(z_stream))
+#define deflateInit2(strm, level, method, windowBits, memLevel, strategy) \
+        deflateInit2_((strm),(level),(method),(windowBits),(memLevel),\
+                     (strategy),           ZLIB_VERSION, sizeof(z_stream))
+#define inflateInit2(strm, windowBits) \
+        inflateInit2_((strm), (windowBits), ZLIB_VERSION, sizeof(z_stream))
+
+#if !defined(_Z_UTIL_H) && !defined(NO_DUMMY_DECL)
+    struct internal_state {int dummy;}; /* hack for buggy compilers */
+#endif
+
+uLongf *get_crc_table OF((void)); /* can be used by asm versions of crc32() */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _ZLIB_H */
+/* --- zlib.h */
index 02e7115fe19b865653c3a57de99c5c1b7a67b209..4eb1bcf3f6f02f805ca6b6e513e215378e84be89 100644 (file)
@@ -76,9 +76,8 @@ lockd(struct svc_rqst *rqstp)
        nlmsvc_pid = current->pid;
        up(&lockd_start);
 
-       exit_mm(current);
-       current->session = 1;
-       current->pgrp = 1;
+       daemonize();
+       reparent_to_init();
        sprintf(current->comm, "lockd");
 
        /* Process request with signals blocked.  */
@@ -90,12 +89,6 @@ lockd(struct svc_rqst *rqstp)
        /* kick rpciod */
        rpciod_up();
 
-       /*
-        * N.B. current do_fork() doesn't like NULL task->files,
-        * so we defer closing files until forking rpciod.
-        */
-       exit_files(current);
-
        dprintk("NFS locking service started (ver " LOCKD_VERSION ").\n");
 
        if (!nlm_timeout)
@@ -127,6 +120,16 @@ lockd(struct svc_rqst *rqstp)
                        spin_lock_irq(&current->sigmask_lock);
                        flush_signals(current);
                        spin_unlock_irq(&current->sigmask_lock);
+                       if (nlmsvc_ops) {
+                               nlmsvc_ops->detach();
+#ifdef RPC_DEBUG
+                               nlmsvc_grace_period = 10 * HZ;
+#else
+                               nlmsvc_grace_period += 5 * nlm_timeout * HZ;
+
+#endif
+                               grace_period_expire = nlmsvc_grace_period + jiffies;
+                       }
                }
 
                /*
@@ -144,13 +147,14 @@ lockd(struct svc_rqst *rqstp)
                 * Find a socket with data available and call its
                 * recvfrom routine.
                 */
-               if ((err = svc_recv(serv, rqstp, timeout)) == -EAGAIN)
+               if ((err = svc_recv(serv, rqstp, timeout)) == -EAGAIN
+                       || err == -EINTR
+                       )
                        continue;
                if (err < 0) {
-                       if (err != -EINTR)
-                               printk(KERN_WARNING
-                                       "lockd: terminating on error %d\n",
-                                       -err);
+                       printk(KERN_WARNING
+                              "lockd: terminating on error %d\n",
+                              -err);
                        break;
                }
 
@@ -180,6 +184,8 @@ lockd(struct svc_rqst *rqstp)
         * shutting down the hosts and clearing the slot.
         */
        if (!nlmsvc_pid || current->pid == nlmsvc_pid) {
+               if (nlmsvc_ops)
+                       nlmsvc_ops->detach();
                nlm_shutdown_hosts();
                nlmsvc_pid = 0;
        } else
index e15483e4348eb568fca4523c8f8412b5548222f1..5bed6b8ae3208005ea5341e7450b16cd65b43d86 100644 (file)
@@ -32,8 +32,10 @@ nlm_fopen(struct svc_rqst *rqstp, struct nfs_fh *f, struct file *filp)
        fh.fh_export = NULL;
 
        nfserr = nfsd_open(rqstp, &fh, S_IFREG, MAY_LOCK, filp);
-       if (!nfserr)
+       if (!nfserr) {
                dget(filp->f_dentry);
+               mntget(filp->f_vfsmnt);
+       }
        fh_put(&fh);
        /* nlm and nfsd don't share error codes.
         * we invent: 0 = no error
@@ -55,6 +57,7 @@ nlm_fclose(struct file *filp)
 {
        nfsd_close(filp);
        dput(filp->f_dentry);
+       mntput(filp->f_vfsmnt);
 }
 
 struct nlmsvc_binding          nfsd_nlm_ops = {
index 2831b4d56486711dad7b63f396214a0fabe11d14..7ed6914635ce759da39f475aa137c75469d39ce7 100644 (file)
@@ -32,8 +32,6 @@
 #include <linux/smp.h>
 #include <linux/smp_lock.h>
 
-extern long sys_call_table[];
-
 static int     nfsctl_svc(struct nfsctl_svc *data);
 static int     nfsctl_addclient(struct nfsctl_client *data);
 static int     nfsctl_delclient(struct nfsctl_client *data);
index a3afc598e823e7fe35dfe28029f67535488d677f..1086f684ab4267efe900128ddc198740fa4659df 100644 (file)
@@ -584,6 +584,7 @@ nfserrno (int errno)
                { nfserr_dquot, -EDQUOT },
 #endif
                { nfserr_stale, -ESTALE },
+               { nfserr_dropit, -ENOMEM },
                { -1, -EIO }
        };
        int     i;
index 4f3e14d4a8b203058f6fc749c32e113e55778a44..814b1249424bcc949200b073858eca4802687df4 100644 (file)
@@ -268,6 +268,12 @@ nfsd_dispatch(struct svc_rqst *rqstp, u32 *statp)
 
        /* Now call the procedure handler, and encode NFS status. */
        nfserr = proc->pc_func(rqstp, rqstp->rq_argp, rqstp->rq_resp);
+       if (nfserr == nfserr_dropit) {
+               dprintk("nfsd: Dropping request due to malloc failure!\n");
+               nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
+               return 0;
+       }
+               
        if (rqstp->rq_proc != 0)
                svc_putlong(&rqstp->rq_resbuf, nfserr);
 
index fdc90401d7007bf27c5015846c1027e1555c8e7a..74c5d771d222d6032ba5e7c622cf130feed08028 100644 (file)
@@ -481,6 +481,7 @@ nfsd_open(struct svc_rqst *rqstp, struct svc_fh *fhp, int type,
        filp->f_op    = fops_get(inode->i_fop);
        atomic_set(&filp->f_count, 1);
        filp->f_dentry = dentry;
+       filp->f_vfsmnt = fhp->fh_export->ex_mnt;
        if (access & MAY_WRITE) {
                filp->f_flags = O_WRONLY|O_LARGEFILE;
                filp->f_mode  = FMODE_WRITE;
index 6fd8bd4f2de1497fa9a78eb894af4964adff6cec..ed5ff5abc53ee2c22093c7c4fc5df69a36a207d5 100644 (file)
@@ -1139,7 +1139,7 @@ struct inode * reiserfs_iget (struct super_block * s, struct cpu_key * key)
     args.objectid = key->on_disk_key.k_dir_id ;
     inode = iget4 (s, key->on_disk_key.k_objectid, 0, (void *)(&args));
     if (!inode) 
-      return inode ;
+       return ERR_PTR(-ENOMEM) ;
 
     if (comp_short_keys (INODE_PKEY (inode), key) || is_bad_inode (inode)) {
        /* either due to i/o error or a stale NFS handle */
@@ -1184,7 +1184,7 @@ struct dentry *reiserfs_fh_to_dentry(struct super_block *sb, __u32 *data,
            key.on_disk_key.k_objectid = data[0] ;
            key.on_disk_key.k_dir_id = data[1] ;
            inode = reiserfs_iget(sb, &key) ;
-           if (inode && (fhtype == 3 || fhtype >= 5) &&
+           if (inode && !IS_ERR(inode) && (fhtype == 3 || fhtype >= 5) &&
                data[2] != inode->i_generation) {
                    iput(inode) ;
                    inode = NULL ;
@@ -1193,13 +1193,15 @@ struct dentry *reiserfs_fh_to_dentry(struct super_block *sb, __u32 *data,
            key.on_disk_key.k_objectid = data[fhtype>=5?3:2] ;
            key.on_disk_key.k_dir_id = data[fhtype>=5?4:3] ;
            inode = reiserfs_iget(sb, &key) ;
-           if (inode && fhtype == 6 &&
+           if (inode && !IS_ERR(inode) && fhtype == 6 &&
                data[5] != inode->i_generation) {
                    iput(inode) ;
                    inode = NULL ;
            }
     }
 out:
+    if (IS_ERR(inode))
+       return ERR_PTR(PTR_ERR(inode));
     if (!inode)
         return ERR_PTR(-ESTALE) ;
 
index 504afed1ed7e0ff1150f0cf527bc65f4d36b0420..1e9703e9cbe6cf86b94b397d0f2a0abcfa4a8f35 100644 (file)
@@ -374,7 +374,7 @@ struct dentry * reiserfs_lookup (struct inode * dir, struct dentry * dentry)
     pathrelse (&path_to_entry);
     if (retval == NAME_FOUND) {
        inode = reiserfs_iget (dir->i_sb, (struct cpu_key *)&(de.de_dir_id));
-       if (!inode) {
+       if (!inode || IS_ERR(inode)) {
            return ERR_PTR(-EACCES);
         }
     }
index 49f228f3f1be87f6c492fa8c6278d67d978b6573..61e83a4bcf133efb745cf0f5b16bf5ede18b64b9 100644 (file)
@@ -7,6 +7,8 @@
 #ifndef __ASM_ALPHA_PROCESSOR_H
 #define __ASM_ALPHA_PROCESSOR_H
 
+#include <linux/personality.h> /* for ADDR_LIMIT_32BIT */
+
 /*
  * Returns current instruction pointer ("program counter").
  */
index a2f9b28d54defdf6338533f8ce26734c31fb2aa6..2949a9733e854ddf6ba90e7ea00844f171b6de65 100644 (file)
@@ -88,6 +88,7 @@ extern struct cpuinfo_x86 cpu_data[];
 #define cpu_has_fxsr   (test_bit(X86_FEATURE_FXSR, boot_cpu_data.x86_capability))
 #define cpu_has_xmm    (test_bit(X86_FEATURE_XMM,  boot_cpu_data.x86_capability))
 #define cpu_has_fpu    (test_bit(X86_FEATURE_FPU,  boot_cpu_data.x86_capability))
+#define cpu_has_apic   (test_bit(X86_FEATURE_APIC, boot_cpu_data.x86_capability))
 
 extern char ignore_irq13;
 
index 207d5a1043a4a14c2edf621fe5435e78479e5961..ae1ee677118d6513b302b67e84828727949633e2 100644 (file)
@@ -18,7 +18,7 @@
 #define EFL 14
 #define UESP 15
 #define SS   16
-
+#define FRAME_SIZE 17
 
 /* this struct defines the way the registers are stored on the 
    stack during a system call. */
index 6a2230b8fb3045d61577c7dd6361c9f6995aff8f..5a99f545530d378e39227620d2e39b3eba91acce 100644 (file)
@@ -555,19 +555,20 @@ static struct xor_block_template xor_block_p5_mmx = {
                : "memory")
 
 #define OFFS(x)                "16*("#x")"
-#define        PF0(x)          "       prefetcht0  "OFFS(x)"(%1)   ;\n"
-#define LD(x,y)                "       movaps   "OFFS(x)"(%1), %%xmm"#y"   ;\n"
-#define ST(x,y)                "       movaps %%xmm"#y",   "OFFS(x)"(%1)   ;\n"
-#define PF1(x)         "       prefetchnta "OFFS(x)"(%2)   ;\n"
-#define PF2(x)         "       prefetchnta "OFFS(x)"(%3)   ;\n"
-#define PF3(x)         "       prefetchnta "OFFS(x)"(%4)   ;\n"
-#define PF4(x)         "       prefetchnta "OFFS(x)"(%5)   ;\n"
-#define PF5(x)         "       prefetchnta "OFFS(x)"(%6)   ;\n"
-#define XO1(x,y)       "       xorps   "OFFS(x)"(%2), %%xmm"#y"   ;\n"
-#define XO2(x,y)       "       xorps   "OFFS(x)"(%3), %%xmm"#y"   ;\n"
-#define XO3(x,y)       "       xorps   "OFFS(x)"(%4), %%xmm"#y"   ;\n"
-#define XO4(x,y)       "       xorps   "OFFS(x)"(%5), %%xmm"#y"   ;\n"
-#define XO5(x,y)       "       xorps   "OFFS(x)"(%6), %%xmm"#y"   ;\n"
+#define PF_OFFS(x)     "256+16*("#x")"
+#define        PF0(x)          "       prefetchnta "PF_OFFS(x)"(%1)            ;\n"
+#define LD(x,y)                "       movaps   "OFFS(x)"(%1), %%xmm"#y"       ;\n"
+#define ST(x,y)                "       movaps %%xmm"#y",   "OFFS(x)"(%1)       ;\n"
+#define PF1(x)         "       prefetchnta "PF_OFFS(x)"(%2)            ;\n"
+#define PF2(x)         "       prefetchnta "PF_OFFS(x)"(%3)            ;\n"
+#define PF3(x)         "       prefetchnta "PF_OFFS(x)"(%4)            ;\n"
+#define PF4(x)         "       prefetchnta "PF_OFFS(x)"(%5)            ;\n"
+#define PF5(x)         "       prefetchnta "PF_OFFS(x)"(%6)            ;\n"
+#define XO1(x,y)       "       xorps   "OFFS(x)"(%2), %%xmm"#y"        ;\n"
+#define XO2(x,y)       "       xorps   "OFFS(x)"(%3), %%xmm"#y"        ;\n"
+#define XO3(x,y)       "       xorps   "OFFS(x)"(%4), %%xmm"#y"        ;\n"
+#define XO4(x,y)       "       xorps   "OFFS(x)"(%5), %%xmm"#y"        ;\n"
+#define XO5(x,y)       "       xorps   "OFFS(x)"(%6), %%xmm"#y"        ;\n"
 
 
 static void
index fef8b4776e95dfedfd2088eafe64965ea98430cc..e29bba62328936dbeca8932add20a41ba6c38cd8 100644 (file)
@@ -58,6 +58,7 @@ enum chipset_type {
        SIS_GENERIC,
        AMD_GENERIC,
        AMD_IRONGATE,
+       AMD_761,
        ALI_M1541,
        ALI_M1621,
        ALI_M1631,
index c805304f11e6c17983e49890b0955af200093717..208c4573381a617342c74ab07f27fca543454558 100644 (file)
@@ -45,7 +45,7 @@ enum brlock_indices {
 #include <linux/cache.h>
 #include <linux/spinlock.h>
 
-#if defined(__i386__) || defined(__ia64__)
+#if defined(__i386__) || defined(__ia64__) || defined(__x86_64__)
 #define __BRLOCK_USE_ATOMICS
 #else
 #undef __BRLOCK_USE_ATOMICS
index b73ddd8ade82907742c5f5d164c72d43ab443bd9..549459db59f9131a1062d02e9dd0f108415b19c7 100644 (file)
@@ -306,6 +306,8 @@ extern void set_bh_page(struct buffer_head *bh, struct page *page, unsigned long
 #include <linux/ncp_fs_i.h>
 #include <linux/proc_fs_i.h>
 #include <linux/usbdev_fs_i.h>
+#include <linux/jffs2_fs_i.h>
+#include <linux/cramfs_fs_sb.h>
 
 /*
  * Attribute flags.  These should be or-ed together to figure out what
@@ -481,6 +483,7 @@ struct inode {
                struct proc_inode_info          proc_i;
                struct socket                   socket_i;
                struct usbdev_inode_info        usbdev_i;
+               struct jffs2_inode_info         jffs2_i;
                void                            *generic_ip;
        } u;
 };
@@ -658,6 +661,7 @@ struct quota_mount_options
 #include <linux/ncp_fs_sb.h>
 #include <linux/usbdev_fs_sb.h>
 #include <linux/cramfs_fs_sb.h>
+#include <linux/jffs2_fs_sb.h>
 
 extern struct list_head super_blocks;
 extern spinlock_t sb_lock;
@@ -713,6 +717,7 @@ struct super_block {
                struct udf_sb_info      udf_sb;
                struct ncp_sb_info      ncpfs_sb;
                struct usbdev_sb_info   usbdevfs_sb;
+               struct jffs2_sb_info    jffs2_sb;
                struct cramfs_sb_info   cramfs_sb;
                void                    *generic_sbp;
        } u;
index 147e67fbdd6e335e722f89c4298e6603228ffe6a..7aea8bc88b32d15f339ddda055b5980c6c945f14 100644 (file)
@@ -91,22 +91,22 @@ extern struct gendisk *get_gendisk(kdev_t dev);
 
 struct solaris_x86_slice {
        ushort  s_tag;                  /* ID tag of partition */
-       ushort  s_flag;                 /* permision flags */
-       daddr_t s_start;                /* start sector no of partition */
-       long    s_size;                 /* # of blocks in partition */
+       ushort  s_flag;                 /* permission flags */
+       unsigned int s_start;           /* start sector no of partition */
+       unsigned int s_size;            /* # of blocks in partition */
 };
 
 struct solaris_x86_vtoc {
-               unsigned long v_bootinfo[3];    /* info needed by mboot (unsupported) */
-       unsigned long v_sanity;         /* to verify vtoc sanity */
-       unsigned long v_version;        /* layout version */
+       unsigned int v_bootinfo[3];     /* info needed by mboot (unsupported) */
+       unsigned int v_sanity;          /* to verify vtoc sanity */
+       unsigned int v_version;         /* layout version */
        char    v_volume[8];            /* volume name */
        ushort  v_sectorsz;             /* sector size in bytes */
        ushort  v_nparts;               /* number of partitions */
-       unsigned long v_reserved[10];   /* free space */
+       unsigned int v_reserved[10];    /* free space */
        struct solaris_x86_slice
                v_slice[SOLARIS_X86_NUMSLICE]; /* slice headers */
-       time_t  timestamp[SOLARIS_X86_NUMSLICE]; /* timestamp (unsupported) */
+       unsigned int timestamp[SOLARIS_X86_NUMSLICE]; /* timestamp (unsupported) */
        char    v_asciilabel[128];      /* for compatibility */
 };
 
diff --git a/include/linux/i2c-algo-ite.h b/include/linux/i2c-algo-ite.h
new file mode 100644 (file)
index 0000000..26a8b89
--- /dev/null
@@ -0,0 +1,69 @@
+/* ------------------------------------------------------------------------- */
+/* i2c-algo-ite.h i2c driver algorithms for ITE IIC adapters                 */
+/* ------------------------------------------------------------------------- */
+/*   Copyright (C) 1995-97 Simon G. Vogl
+                   1998-99 Hans Berglund
+
+    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., 675 Mass Ave, Cambridge, MA 02139, USA.                */
+/* ------------------------------------------------------------------------- */
+
+/* With some changes from Kyösti Mälkki <kmalkki@cc.hut.fi> and even
+   Frodo Looijaard <frodol@dds.nl> */
+
+/* Modifications by MontaVista Software, 2001
+   Changes made to support the ITE IIC peripheral */
+
+
+#ifndef I2C_ALGO_ITE_H
+#define I2C_ALGO_ITE_H 1
+
+#include <linux/i2c.h>
+
+/* Example of a sequential read request:
+       struct i2c_iic_msg s_msg; 
+
+       s_msg.addr=device_address;
+       s_msg.len=length;
+       s_msg.buf=buffer;
+       s_msg.waddr=word_address;
+       ioctl(file,I2C_SREAD, &s_msg);
+ */
+#define I2C_SREAD      0x780   /* SREAD ioctl command */
+
+struct i2c_iic_msg {
+       __u16 addr;     /* device address */
+       __u16 waddr;    /* word address */
+       short len;      /* msg length */
+       char *buf;      /* pointer to msg data */
+};
+
+struct i2c_algo_iic_data {
+       void *data;             /* private data for lolevel routines    */
+       void (*setiic) (void *data, int ctl, int val);
+       int  (*getiic) (void *data, int ctl);
+       int  (*getown) (void *data);
+       int  (*getclock) (void *data);
+       void (*waitforpin) (void);     
+
+       /* local settings */
+       int udelay;
+       int mdelay;
+       int timeout;
+};
+
+int i2c_iic_add_bus(struct i2c_adapter *);
+int i2c_iic_del_bus(struct i2c_adapter *);
+
+#endif /* I2C_ALGO_ITE_H */
diff --git a/include/linux/jffs2.h b/include/linux/jffs2.h
new file mode 100644 (file)
index 0000000..37d3d93
--- /dev/null
@@ -0,0 +1,164 @@
+/*
+ * JFFS2 -- Journalling Flash File System, Version 2.
+ *
+ * Copyright (C) 2001 Red Hat, Inc.
+ *
+ * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
+ *
+ * The original JFFS, from which the design for JFFS2 was derived,
+ * was designed and implemented by Axis Communications AB.
+ *
+ * The contents of this file are subject to the Red Hat eCos Public
+ * License Version 1.1 (the "Licence"); you may not use this file
+ * except in compliance with the Licence.  You may obtain a copy of
+ * the Licence at http://www.redhat.com/
+ *
+ * Software distributed under the Licence is distributed on an "AS IS"
+ * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
+ * See the Licence for the specific language governing rights and
+ * limitations under the Licence.
+ *
+ * The Original Code is JFFS2 - Journalling Flash File System, version 2
+ *
+ * Alternatively, the contents of this file may be used under the
+ * terms of the GNU General Public License version 2 (the "GPL"), in
+ * which case the provisions of the GPL are applicable instead of the
+ * above.  If you wish to allow the use of your version of this file
+ * only under the terms of the GPL and not to allow others to use your
+ * version of this file under the RHEPL, indicate your decision by
+ * deleting the provisions above and replace them with the notice and
+ * other provisions required by the GPL.  If you do not delete the
+ * provisions above, a recipient may use your version of this file
+ * under either the RHEPL or the GPL.
+ *
+ * $Id: jffs2.h,v 1.18 2001/03/25 22:36:12 dwmw2 Exp $
+ *
+ */
+
+#ifndef __LINUX_JFFS2_H__
+#define __LINUX_JFFS2_H__
+
+#include <asm/types.h>
+#define JFFS2_SUPER_MAGIC 0x72b6
+
+/* Values we may expect to find in the 'magic' field */
+#define JFFS2_OLD_MAGIC_BITMASK 0x1984
+#define JFFS2_MAGIC_BITMASK 0x1985
+#define KSAMTIB_CIGAM_2SFFJ 0x5981 /* For detecting wrong-endian fs */
+#define JFFS2_EMPTY_BITMASK 0xffff
+#define JFFS2_DIRTY_BITMASK 0x0000
+
+/* We only allow a single char for length, and 0xFF is empty flash so
+   we don't want it confused with a real length. Hence max 254.
+*/
+#define JFFS2_MAX_NAME_LEN 254
+
+/* How small can we sensibly write nodes? */
+#define JFFS2_MIN_DATA_LEN 128
+
+#define JFFS2_COMPR_NONE       0x00
+#define JFFS2_COMPR_ZERO       0x01
+#define JFFS2_COMPR_RTIME      0x02
+#define JFFS2_COMPR_RUBINMIPS  0x03
+#define JFFS2_COMPR_COPY       0x04
+#define JFFS2_COMPR_DYNRUBIN   0x05
+#define JFFS2_COMPR_ZLIB       0x06
+/* Compatibility flags. */
+#define JFFS2_COMPAT_MASK 0xc000      /* What do to if an unknown nodetype is found */
+#define JFFS2_NODE_ACCURATE 0x2000
+/* INCOMPAT: Fail to mount the filesystem */
+#define JFFS2_FEATURE_INCOMPAT 0xc000
+/* ROCOMPAT: Mount read-only */
+#define JFFS2_FEATURE_ROCOMPAT 0x8000
+/* RWCOMPAT_COPY: Mount read/write, and copy the node when it's GC'd */
+#define JFFS2_FEATURE_RWCOMPAT_COPY 0x4000
+/* RWCOMPAT_DELETE: Mount read/write, and delete the node when it's GC'd */
+#define JFFS2_FEATURE_RWCOMPAT_DELETE 0x0000
+
+#define JFFS2_NODETYPE_DIRENT (JFFS2_FEATURE_INCOMPAT | JFFS2_NODE_ACCURATE | 1)
+#define JFFS2_NODETYPE_INODE (JFFS2_FEATURE_INCOMPAT | JFFS2_NODE_ACCURATE | 2)
+#define JFFS2_NODETYPE_CLEANMARKER (JFFS2_FEATURE_RWCOMPAT_DELETE | JFFS2_NODE_ACCURATE | 3)
+
+// Maybe later...
+//#define JFFS2_NODETYPE_CHECKPOINT (JFFS2_FEATURE_RWCOMPAT_DELETE | JFFS2_NODE_ACCURATE | 3)
+//#define JFFS2_NODETYPE_OPTIONS (JFFS2_FEATURE_RWCOMPAT_COPY | JFFS2_NODE_ACCURATE | 4)
+
+/* Same as the non_ECC versions, but with extra space for real 
+ * ECC instead of just the checksum. For use on NAND flash 
+ */
+//#define JFFS2_NODETYPE_DIRENT_ECC (JFFS2_FEATURE_INCOMPAT | JFFS2_NODE_ACCURATE | 5)
+//#define JFFS2_NODETYPE_INODE_ECC (JFFS2_FEATURE_INCOMPAT | JFFS2_NODE_ACCURATE | 6)
+
+#define JFFS2_INO_FLAG_PREREAD   1     /* Do read_inode() for this one at 
+                                          mount time, don't wait for it to 
+                                          happen later */
+#define JFFS2_INO_FLAG_USERCOMPR  2    /* User has requested a specific 
+                                          compression type */
+
+
+struct jffs2_unknown_node
+{
+       /* All start like this */
+       __u16 magic;
+       __u16 nodetype;
+       __u32 totlen; /* So we can skip over nodes we don't grok */
+       __u32 hdr_crc;
+};
+
+struct jffs2_raw_dirent
+{
+       __u16 magic;
+       __u16 nodetype; /* == JFFS_NODETYPE_DIRENT */
+       __u32 totlen;
+       __u32 hdr_crc;
+       __u32 pino;
+       __u32 version;
+       __u32 ino; /* == zero for unlink */
+       __u32 mctime;
+       __u8 nsize;
+       __u8 type;
+       __u8 unused[2];
+       __u32 node_crc;
+       __u32 name_crc;
+       __u8 name[0];
+};
+
+/* The JFFS2 raw inode structure: Used for storage on physical media.  */
+/* The uid, gid, atime, mtime and ctime members could be longer, but 
+   are left like this for space efficiency. If and when people decide
+   they really need them extended, it's simple enough to add support for
+   a new type of raw node.
+*/
+struct jffs2_raw_inode
+{
+       __u16 magic;      /* A constant magic number.  */
+       __u16 nodetype;   /* == JFFS_NODETYPE_INODE */
+       __u32 totlen;     /* Total length of this node (inc data, etc.) */
+       __u32 hdr_crc;
+       __u32 ino;        /* Inode number.  */
+       __u32 version;    /* Version number.  */
+       __u32 mode;       /* The file's type or mode.  */
+       __u16 uid;        /* The file's owner.  */
+       __u16 gid;        /* The file's group.  */
+       __u32 isize;      /* Total resultant size of this inode (used for truncations)  */
+       __u32 atime;      /* Last access time.  */
+       __u32 mtime;      /* Last modification time.  */
+       __u32 ctime;      /* Change time.  */
+       __u32 offset;     /* Where to begin to write.  */
+       __u32 csize;      /* (Compressed) data size */
+       __u32 dsize;      /* Size of the node's data. (after decompression) */
+       __u8 compr;       /* Compression algorithm used */
+       __u8 usercompr;   /* Compression algorithm requested by the user */
+       __u16 flags;      /* See JFFS2_INO_FLAG_* */
+       __u32 data_crc;   /* CRC for the (compressed) data.  */
+       __u32 node_crc;   /* CRC for the raw inode (excluding data)  */
+//     __u8 data[dsize];
+};
+
+union jffs2_node_union {
+       struct jffs2_raw_inode i;
+       struct jffs2_raw_dirent d;
+       struct jffs2_unknown_node u;
+};
+
+#endif /* __LINUX_JFFS2_H__ */
diff --git a/include/linux/jffs2_fs_i.h b/include/linux/jffs2_fs_i.h
new file mode 100644 (file)
index 0000000..6121a88
--- /dev/null
@@ -0,0 +1,62 @@
+/* $Id: jffs2_fs_i.h,v 1.8 2001/04/18 13:05:28 dwmw2 Exp $ */
+
+#ifndef _JFFS2_FS_I
+#define _JFFS2_FS_I
+
+/* Include the pipe_inode_info at the beginning so that we can still
+   use the storage space in the inode when we have a pipe inode.
+   This sucks.
+*/
+
+#undef THISSUCKS /* Only for 2.2 */
+#ifdef THISSUCKS
+#include <linux/pipe_fs_i.h>
+#endif
+
+struct jffs2_inode_info {
+#ifdef THISSUCKS
+        struct pipe_inode_info pipecrap;
+#endif
+       /* We need an internal semaphore similar to inode->i_sem.
+          Unfortunately, we can't used the existing one, because
+          either the GC would deadlock, or we'd have to release it
+          before letting GC proceed. Or we'd have to put ugliness
+          into the GC code so it didn't attempt to obtain the i_sem
+          for the inode(s) which are already locked */
+       struct semaphore sem;
+
+       /* The highest (datanode) version number used for this ino */
+       __u32 highest_version;
+
+       /* List of data fragments which make up the file */
+       struct jffs2_node_frag *fraglist;
+
+       /* There may be one datanode which isn't referenced by any of the
+          above fragments, if it contains a metadata update but no actual
+          data - or if this is a directory inode */
+       /* This also holds the _only_ dnode for symlinks/device nodes, 
+          etc. */
+       struct jffs2_full_dnode *metadata;
+
+       /* Directory entries */
+       struct jffs2_full_dirent *dents;
+
+       /* Some stuff we just have to keep in-core at all times, for each inode. */
+       struct jffs2_inode_cache *inocache;
+
+       /* Keep a pointer to the last physical node in the list. We don't 
+          use the doubly-linked lists because we don't want to increase
+          the memory usage that much. This is simpler */
+       //      struct jffs2_raw_node_ref *lastnode;
+       __u16 flags;
+       __u8 usercompr;
+};
+
+#ifdef JFFS2_OUT_OF_KERNEL
+#define JFFS2_INODE_INFO(i) ((struct jffs2_inode_info *) &(i)->u)
+#else
+#define JFFS2_INODE_INFO(i) (&i->u.jffs2_i)
+#endif
+
+#endif /* _JFFS2_FS_I */
+
diff --git a/include/linux/jffs2_fs_sb.h b/include/linux/jffs2_fs_sb.h
new file mode 100644 (file)
index 0000000..9e9235f
--- /dev/null
@@ -0,0 +1,84 @@
+/* $Id: jffs2_fs_sb.h,v 1.15 2001/04/18 13:05:28 dwmw2 Exp $ */
+
+#ifndef _JFFS2_FS_SB
+#define _JFFS2_FS_SB
+
+#include <linux/types.h>
+#include <linux/spinlock.h>
+#include <linux/completion.h>
+#include <asm/semaphore.h>
+#include <linux/list.h>
+
+#define INOCACHE_HASHSIZE 1
+
+#define JFFS2_SB_FLAG_RO 1
+
+/* A struct for the overall file system control.  Pointers to
+   jffs2_sb_info structs are named `c' in the source code.  
+   Nee jffs_control
+*/
+struct jffs2_sb_info {
+       struct mtd_info *mtd;
+
+       __u32 highest_ino;
+       unsigned int flags;
+       spinlock_t nodelist_lock;
+
+       //      pid_t thread_pid;               /* GC thread's PID */
+       struct task_struct *gc_task;    /* GC task struct */
+       struct semaphore gc_thread_sem; /* GC thread startup mutex */
+       struct completion gc_thread_exit; /* GC thread exit completion */
+
+       //      __u32 gc_minfree_threshold;     /* GC trigger thresholds */
+       //      __u32 gc_maxdirty_threshold;
+
+       struct semaphore alloc_sem;     /* Used to protect all the following 
+                                          fields, and also to protect against
+                                          out-of-order writing of nodes.
+                                          And GC.
+                                       */
+       __u32 flash_size;
+       __u32 used_size;
+       __u32 dirty_size;
+       __u32 free_size;
+       __u32 erasing_size;
+       __u32 bad_size;
+       __u32 sector_size;
+       //      __u32 min_free_size;
+       //      __u32 max_chunk_size;
+
+       __u32 nr_free_blocks;
+       __u32 nr_erasing_blocks;
+
+       __u32 nr_blocks;
+       struct jffs2_eraseblock *blocks;        /* The whole array of blocks. Used for getting blocks 
+                                                * from the offset (blocks[ofs / sector_size]) */
+       struct jffs2_eraseblock *nextblock;     /* The block we're currently filling */
+
+       struct jffs2_eraseblock *gcblock;       /* The block we're currently garbage-collecting */
+
+       struct list_head clean_list;            /* Blocks 100% full of clean data */
+       struct list_head dirty_list;            /* Blocks with some dirty space */
+       struct list_head erasing_list;          /* Blocks which are currently erasing */
+       struct list_head erase_pending_list;    /* Blocks which need erasing */
+       struct list_head erase_complete_list;   /* Blocks which are erased and need the clean marker written to them */
+       struct list_head free_list;             /* Blocks which are free and ready to be used */
+       struct list_head bad_list;              /* Bad blocks. */
+       struct list_head bad_used_list;         /* Bad blocks with valid data in. */
+
+       spinlock_t erase_completion_lock;       /* Protect free_list and erasing_list 
+                                                  against erase completion handler */
+       wait_queue_head_t erase_wait;           /* For waiting for erases to complete */
+       struct jffs2_inode_cache *inocache_list[INOCACHE_HASHSIZE];
+       spinlock_t inocache_lock;
+};
+
+#ifdef JFFS2_OUT_OF_KERNEL
+#define JFFS2_SB_INFO(sb) ((struct jffs2_sb_info *) &(sb)->u)
+#else
+#define JFFS2_SB_INFO(sb) (&sb->u.jffs2_sb)
+#endif
+
+#define OFNI_BS_2SFFJ(c)  ((struct super_block *) ( ((char *)c) - ((char *)(&((struct super_block *)NULL)->u)) ) )
+
+#endif /* _JFFS2_FB_SB */
index 9bd982bb876b264b9e2596e1e32cce2b13996234..949bb22391f7f2527b91af4e1ab79df5549abee7 100644 (file)
@@ -187,7 +187,7 @@ typedef struct page {
  * Various page->flags bits:
  *
  * PG_reserved is set for special pages, which can never be swapped
- * out. Some of them might not even exist (eg. empty_bad_page)...
+ * out. Some of them might not even exist (eg empty_bad_page)...
  *
  * Multiple processes may "see" the same page. E.g. for untouched
  * mappings of /dev/null, all processes see the same page full of
@@ -210,8 +210,8 @@ typedef struct page {
  * The following discussion applies only to them.
  *
  * A page may belong to an inode's memory mapping. In this case,
- * page->mapping is the pointer to the inode, and page->offset is the
- * file offset of the page (not necessarily a multiple of PAGE_SIZE).
+ * page->mapping is the pointer to the inode, and page->index is the
+ * file offset of the page, in units of PAGE_CACHE_SIZE.
  *
  * A page may have buffers allocated to it. In this case,
  * page->buffers is a circular list of these buffer heads. Else,
@@ -226,7 +226,7 @@ typedef struct page {
  * using the page->list list_head. These fields are also used for
  * freelist managemet (when page->count==0).
  *
- * There is also a hash table mapping (inode,offset) to the page
+ * There is also a hash table mapping (mapping,index) to the page
  * in memory if present. The lists for this hash table use the fields
  * page->next_hash and page->pprev_hash.
  *
@@ -245,7 +245,7 @@ typedef struct page {
  *
  * For choosing which pages to swap out, inode pages carry a
  * PG_referenced bit, which is set any time the system accesses
- * that page through the (inode,offset) hash table. This referenced
+ * that page through the (mapping,index) hash table. This referenced
  * bit, together with the referenced bit in the page tables, is used
  * to manipulate page->age and move the page across the active,
  * inactive_dirty and inactive_clean lists.
@@ -260,7 +260,7 @@ typedef struct page {
  * PG_error is set to indicate that an I/O error occurred on this page.
  *
  * PG_arch_1 is an architecture specific page state bit.  The generic
- * code guarentees that this bit is cleared for a page when it first
+ * code guarantees that this bit is cleared for a page when it first
  * is entered into the page cache.
  *
  * PG_highmem pages are not permanently mapped into the kernel virtual
index c8e143a1f723b57be8ece615e012c94f3ed24afb..15bd7d44d13425b8b6859f47a615b505b9d14b6c 100644 (file)
@@ -58,7 +58,7 @@
 
 #endif
 
-#if defined(__i386__)
+#if defined(__i386__) || defined(__x86_64__)
 #define USE_MEMCPY
 #endif
 
index 571557de8762cb08e0d3e4aa348df58804be86ea..53d035c7479ab116f797181f78736bb0118ea3fc 100644 (file)
@@ -171,6 +171,11 @@ void               nfsd_lockd_unexport(struct svc_client *);
 #define        nfserr_badtype          __constant_htonl(NFSERR_BADTYPE)
 #define        nfserr_jukebox          __constant_htonl(NFSERR_JUKEBOX)
 
+/* error code for internal use - if a request fails due to
+ * kmalloc failure, it gets dropped.  Client should resend eventually
+ */
+#define        nfserr_dropit           __constant_htonl(30000)
+
 /* Check for dir entries '.' and '..' */
 #define isdotent(n, l) (l < 3 && n[0] == '.' && (l == 1 || n[1] == '.'))
 
index f693fcf58e021d15419aa3c43ffd57ae7279b073..74dadd4bb663d8af4818c1731109d3ef383a5c47 100644 (file)
 /* 000 */
 #define md__get_free_pages(x,y) __get_free_pages(x,y)
 
-#ifdef __i386__
+#if defined(__i386__) || defined(__x86_64__)
 /* 001 */
 static __inline__ int md_cpu_has_mmx(void)
 {
        return test_bit(X86_FEATURE_MMX,  &boot_cpu_data.x86_capability);
 }
+#else
+#define md_cpu_has_mmx(x)      (0)
 #endif
 
 /* 002 */
index 3b51e92fe5552fd09bdf445f875620b7d531634d..851df24f4a5a36cc2a82b5a9d5c2a5f2bebd1d3f 100644 (file)
 
 #define MD_RESERVED       0UL
 #define LINEAR            1UL
-#define STRIPED           2UL
-#define RAID0             STRIPED
+#define RAID0             2UL
 #define RAID1             3UL
 #define RAID5             4UL
 #define TRANSLUCENT       5UL
 #define HSM               6UL
-#define MAX_PERSONALITY   7UL
+#define MULTIPATH         7UL
+#define MAX_PERSONALITY   8UL
 
 static inline int pers_to_level (int pers)
 {
        switch (pers) {
+               case MULTIPATH:         return -4;
                case HSM:               return -3;
                case TRANSLUCENT:       return -2;
                case LINEAR:            return -1;
@@ -35,7 +36,7 @@ static inline int pers_to_level (int pers)
                case RAID1:             return 1;
                case RAID5:             return 5;
        }
-       panic("pers_to_level()");
+       BUG();
        return MD_RESERVED;
 }
 
@@ -171,6 +172,7 @@ struct mdk_rdev_s
        mdp_super_t *sb;
        unsigned long sb_offset;
 
+       int alias_device;               /* device alias to the same disk */
        int faulty;                     /* if faulty do not issue IO requests */
        int desc_nr;                    /* descriptor index in the superblock */
 };
@@ -258,6 +260,7 @@ static inline kdev_t mddev_to_kdev(mddev_t * mddev)
 
 extern mdk_rdev_t * find_rdev(mddev_t * mddev, kdev_t dev);
 extern mdk_rdev_t * find_rdev_nr(mddev_t *mddev, int nr);
+extern mdp_disk_t *get_spare(mddev_t *mddev);
 
 /*
  * iterates through some rdev ringlist. It's safe to remove the
index 22a1543808c9ad86379d83ee07bab365faab6be2..a2df5c2a42af93e373e3238d97aed33c0319c724 100644 (file)
@@ -35,6 +35,7 @@
 #define PROTECT_ARRAY          _IO (MD_MAJOR, 0x27)
 #define HOT_ADD_DISK           _IO (MD_MAJOR, 0x28)
 #define SET_DISK_FAULTY                _IO (MD_MAJOR, 0x29)
+#define HOT_GENERATE_ERROR     _IO (MD_MAJOR, 0x2a)
 
 /* usage */
 #define RUN_ARRAY              _IOW (MD_MAJOR, 0x30, mdu_param_t)
diff --git a/include/linux/raid/multipath.h b/include/linux/raid/multipath.h
new file mode 100644 (file)
index 0000000..db2f3ec
--- /dev/null
@@ -0,0 +1,87 @@
+#ifndef _MULTIPATH_H
+#define _MULTIPATH_H
+
+#include <linux/raid/md.h>
+
+struct multipath_info {
+       int             number;
+       int             raid_disk;
+       kdev_t          dev;
+       int             sect_limit;
+       int             head_position;
+
+       /*
+        * State bits:
+        */
+       int             operational;
+       int             write_only;
+       int             spare;
+
+       int             used_slot;
+};
+
+struct multipath_private_data {
+       mddev_t                 *mddev;
+       struct multipath_info   multipaths[MD_SB_DISKS];
+       int                     nr_disks;
+       int                     raid_disks;
+       int                     working_disks;
+       mdk_thread_t            *thread;
+       struct multipath_info   *spare;
+       md_spinlock_t           device_lock;
+
+       /* buffer pool */
+       /* buffer_heads that we have pre-allocated have b_pprev -> &freebh
+        * and are linked into a stack using b_next
+        * multipath_bh that are pre-allocated have MPBH_PreAlloc set.
+        * All these variable are protected by device_lock
+        */
+       struct buffer_head      *freebh;
+       int                     freebh_cnt;     /* how many are on the list */
+       struct multipath_bh     *freer1;
+       struct multipath_bh     *freebuf;       /* each bh_req has a page allocated */
+       md_wait_queue_head_t    wait_buffer;
+
+       /* for use when syncing multipaths: */
+       unsigned long   start_active, start_ready,
+               start_pending, start_future;
+       int     cnt_done, cnt_active, cnt_ready,
+               cnt_pending, cnt_future;
+       int     phase;
+       int     window;
+       md_wait_queue_head_t    wait_done;
+       md_wait_queue_head_t    wait_ready;
+       md_spinlock_t           segment_lock;
+};
+
+typedef struct multipath_private_data multipath_conf_t;
+
+/*
+ * this is the only point in the RAID code where we violate
+ * C type safety. mddev->private is an 'opaque' pointer.
+ */
+#define mddev_to_conf(mddev) ((multipath_conf_t *) mddev->private)
+
+/*
+ * this is our 'private' 'collective' MULTIPATH buffer head.
+ * it contains information about what kind of IO operations were started
+ * for this MULTIPATH operation, and about their status:
+ */
+
+struct multipath_bh {
+       atomic_t                remaining; /* 'have we finished' count,
+                                           * used from IRQ handlers
+                                           */
+       int                     cmd;
+       unsigned long           state;
+       mddev_t                 *mddev;
+       struct buffer_head      *master_bh;
+       struct buffer_head      *multipath_bh_list;
+       struct buffer_head      bh_req;
+       struct multipath_bh     *next_r1;       /* next for retry or in free list */
+};
+/* bits for multipath_bh.state */
+#define        MPBH_Uptodate   1
+#define        MPBH_SyncPhase  2
+#define        MPBH_PreAlloc   3       /* this was pre-allocated, add to free list */
+#endif
index 42d464b87ef87ce418a494825deea74d0d9a9f7c..d10b6277b2c38f7a97f3e043c898483717d8e571 100644 (file)
@@ -179,6 +179,8 @@ extern unsigned long swap_cache_find_success;
 
 extern spinlock_t pagemap_lru_lock;
 
+extern void FASTCALL(mark_page_accessed(struct page *));
+
 /*
  * Page aging defines.
  * Since we do exponential decay of the page age, we
@@ -202,6 +204,8 @@ extern spinlock_t pagemap_lru_lock;
 #define add_page_to_active_list(page) { \
        DEBUG_ADD_PAGE \
        ZERO_PAGE_BUG \
+       page->age = 0; \
+       ClearPageReferenced(page); \
        SetPageActive(page); \
        list_add(&(page)->lru, &active_list); \
        nr_active_pages++; \
index a2eadc979023559edf71a82802001a15c2f3636d..971bae8d90282dff0f3b1246d194be7da1777956 100644 (file)
--- a/ipc/msg.c
+++ b/ipc/msg.c
@@ -613,7 +613,7 @@ int inline pipelined_send(struct msg_queue* msq, struct msg_msg* msg)
                                wake_up_process(msr->r_tsk);
                        } else {
                                msr->r_msg = msg;
-                               msq->q_lspid = msr->r_tsk->pid;
+                               msq->q_lrpid = msr->r_tsk->pid;
                                msq->q_rtime = CURRENT_TIME;
                                wake_up_process(msr->r_tsk);
                                return 1;
@@ -683,6 +683,9 @@ retry:
                goto retry;
        }
 
+       msq->q_lspid = current->pid;
+       msq->q_stime = CURRENT_TIME;
+
        if(!pipelined_send(msq,msg)) {
                /* noone is waiting for this message, enqueue it */
                list_add_tail(&msg->m_list,&msq->q_messages);
@@ -694,8 +697,6 @@ retry:
        
        err = 0;
        msg = NULL;
-       msq->q_lspid = current->pid;
-       msq->q_stime = CURRENT_TIME;
 
 out_unlock_free:
        msg_unlock(msqid);
@@ -742,6 +743,10 @@ asmlinkage long sys_msgrcv (int msqid, struct msgbuf *msgp, size_t msgsz,
        if(msq==NULL)
                return -EINVAL;
 retry:
+       err = -EIDRM;
+       if (msg_checkid(msq,msqid))
+               goto out_unlock;
+
        err=-EACCES;
        if (ipcperms (&msq->q_perm, S_IRUGO))
                goto out_unlock;
index 76602a8649ff74ad273908ef8b251f7a7a1dc9c2..1c8c5c5cdd25ed79d72c07c00c0a20fa3f8319bf 100644 (file)
--- a/ipc/shm.c
+++ b/ipc/shm.c
@@ -606,6 +606,11 @@ asmlinkage long sys_shmat (int shmid, char *shmaddr, int shmflg, ulong *raddr)
        shp = shm_lock(shmid);
        if(shp == NULL)
                return -EINVAL;
+       err = shm_checkid(shp,shmid);
+       if (err) {
+               shm_unlock(shmid);
+               return err;
+       }
        if (ipcperms(&shp->shm_perm, acc_mode)) {
                shm_unlock(shmid);
                return -EACCES;
index 6e94c524b5f216c7e80bd01517e630cbcbef120f..ebfbf2b693b8a18f3b2b36e31853a9de2147dbc5 100644 (file)
@@ -102,6 +102,7 @@ inside:
                for_each_task(p) {
                        if(p->pid == last_pid   ||
                           p->pgrp == last_pid  ||
+                          p->tgid == last_pid  ||
                           p->session == last_pid) {
                                if(++last_pid >= next_safe) {
                                        if(last_pid & 0xffff8000)
index bca26e26a8d2e0844ea5cf70c56bb2dfe61c6f3f..3f8e0a763698ad4c601955cbc54e96a9202aee84 100644 (file)
@@ -308,6 +308,10 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
                if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' || *fmt =='Z') {
                        qualifier = *fmt;
                        ++fmt;
+                       if (qualifier == 'l' && *fmt == 'l') {
+                               qualifier = 'L';
+                               ++fmt;
+                       }
                }
 
                /* default base */
index a3cd1c4eaf4751808d96a6f3aa9563385775c890..60dfccdaea85aedb11c1dc4317e23bdc9c3d26cb 100644 (file)
@@ -307,9 +307,6 @@ inside:
                if (page->index == offset)
                        break;
        }
-       /* Mark the page referenced, kswapd will find it later. */
-       SetPageReferenced(page);
-
 not_found:
        return page;
 }
@@ -948,17 +945,24 @@ static void generic_file_readahead(int reada_ok,
        return;
 }
 
-
-static inline void check_used_once (struct page *page)
+/*
+ * Mark a page as having seen activity.
+ *
+ * If it was already so marked, move it
+ * to the active queue and drop the referenced
+ * bit. Otherwise, just mark it for future
+ * action..
+ */
+void mark_page_accessed(struct page *page)
 {
-       if (!PageActive(page)) {
-               if (page->age)
-                       activate_page(page);
-               else {
-                       page->age = PAGE_AGE_START;
-                       ClearPageReferenced(page);
-               }
+       if (!PageActive(page) && PageReferenced(page)) {
+               activate_page(page);
+               ClearPageReferenced(page);
+               return;
        }
+
+       /* Mark the page referenced, AFTER checking for previous usage.. */
+       SetPageReferenced(page);
 }
 
 /*
@@ -1077,7 +1081,7 @@ page_ok:
                index += offset >> PAGE_CACHE_SHIFT;
                offset &= ~PAGE_CACHE_MASK;
 
-               check_used_once (page);
+               mark_page_accessed(page);
                page_cache_release(page);
                if (ret == nr && desc->count)
                        continue;
@@ -2310,6 +2314,7 @@ repeat:
        }
        if (cached_page)
                page_cache_free(cached_page);
+       mark_page_accessed(page);
        return page;
 }
 
@@ -2569,7 +2574,6 @@ unlock:
                kunmap(page);
                /* Mark it unlocked again and drop the page.. */
                UnlockPage(page);
-               check_used_once(page);
                page_cache_release(page);
 
                if (status < 0)
index 0d7bc4538bf0668d2f389704815c8e1afd6c8755..0cd6915a45a10b07dbebddfb3bb201b4c87e4fce 100644 (file)
@@ -272,8 +272,12 @@ static inline int free_pte(pte_t pte)
                 * free_page() used to be able to clear swap cache
                 * entries.  We may now have to do it manually.  
                 */
-               if (pte_dirty(pte) && page->mapping)
-                       set_page_dirty(page);
+               if (page->mapping) {
+                       if (pte_dirty(pte))
+                               set_page_dirty(page);
+                       if (pte_young(pte))
+                               mark_page_accessed(page);
+               }
                free_page_and_swap_cache(page);
                return 1;
        }
@@ -1069,19 +1073,23 @@ void swapin_readahead(swp_entry_t entry)
        unsigned long offset;
 
        /*
-        * Get the number of handles we should do readahead io to.
+        * Get the number of handles we should do readahead io to. Also,
+        * grab temporary references on them, releasing them as io completes.
         */
        num = valid_swaphandles(entry, &offset);
        for (i = 0; i < num; offset++, i++) {
                /* Don't block on I/O for read-ahead */
-               if (atomic_read(&nr_async_pages) >=
-                   pager_daemon.swap_cluster << page_cluster)
+               if (atomic_read(&nr_async_pages) >= pager_daemon.swap_cluster
+                               * (1 << page_cluster)) {
+                       while (i++ < num)
+                               swap_free(SWP_ENTRY(SWP_TYPE(entry), offset++));
                        break;
+               }
                /* Ok, do the async read-ahead now */
                new_page = read_swap_cache_async(SWP_ENTRY(SWP_TYPE(entry), offset));
-               if (!new_page)
-                       break;
-               page_cache_release(new_page);
+               if (new_page != NULL)
+                       page_cache_release(new_page);
+               swap_free(SWP_ENTRY(SWP_TYPE(entry), offset));
        }
        return;
 }
@@ -1112,7 +1120,6 @@ static int do_swap_page(struct mm_struct * mm,
                         */
                        return pte_same(*page_table, orig_pte) ? -1 : 1;
                }
-               SetPageReferenced(page);
        }
 
        /*
index 6f539de6dce25cb1dae1c5aa554bda4c4ad8919d..e73a5f950b0f43abb38a597b1a2bf238e6cc84f0 100644 (file)
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -337,6 +337,9 @@ unsigned long do_mmap_pgoff(struct file * file, unsigned long addr, unsigned lon
        vma->vm_raend = 0;
 
        if (file) {
+               error = -EINVAL;
+               if (vm_flags & (VM_GROWSDOWN|VM_GROWSUP))
+                       goto free_vma;
                if (vm_flags & VM_DENYWRITE) {
                        error = deny_write_access(file);
                        if (error)
index 4798c544a019b18db66ce2808be8ce94aeb1454e..4ecae70a8ed8aeed92c857cac18d4c7c68829c5d 100644 (file)
@@ -1111,8 +1111,8 @@ void get_swaphandle_info(swp_entry_t entry, unsigned long *offset,
 }
 
 /*
- * Kernel_lock protects against swap device deletion. Don't grab an extra
- * reference on the swaphandle, it doesn't matter if it becomes unused.
+ * Kernel_lock protects against swap device deletion. Grab an extra
+ * reference on the swaphandle so that it dos not become unused.
  */
 int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
 {
@@ -1123,6 +1123,7 @@ int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
        *offset = SWP_OFFSET(entry);
        toff = *offset = (*offset >> page_cluster) << page_cluster;
 
+       swap_device_lock(swapdev);
        do {
                /* Don't read-ahead past the end of the swap area */
                if (toff >= swapdev->max)
@@ -1132,8 +1133,10 @@ int valid_swaphandles(swp_entry_t entry, unsigned long *offset)
                        break;
                if (swapdev->swap_map[toff] == SWAP_MAP_BAD)
                        break;
+               swapdev->swap_map[toff]++;
                toff++;
                ret++;
        } while (--i);
+       swap_device_unlock(swapdev);
        return ret;
 }
index 0c68f9ee8f3d137245e389b3d4b8e54aeddae75a..3025c2ca3620d15e27ada4b43db1b679889febcf 100644 (file)
@@ -97,7 +97,7 @@ static void try_to_swap_out(struct mm_struct * mm, struct vm_area_struct* vma, u
 
        /* Don't look at this pte if it's been accessed recently. */
        if (ptep_test_and_clear_young(page_table)) {
-               age_page_up(page);
+               mark_page_accessed(page);
                return;
        }
 
@@ -127,7 +127,7 @@ set_swap_pte:
                set_pte(page_table, swp_entry_to_pte(entry));
 drop_pte:
                mm->rss--;
-               if (!page->age)
+               if (!PageReferenced(page))
                        deactivate_page(page);
                UnlockPage(page);
                page_cache_release(page);
@@ -406,12 +406,10 @@ struct page * reclaim_page(zone_t * zone)
                        continue;
                }
 
-               /* Page is or was in use?  Move it to the active list. */
-               if (PageReferenced(page) || (!page->buffers && page_count(page) > 1)) {
-                       del_page_from_inactive_clean_list(page);
-                       add_page_to_active_list(page);
-                       page->age = PAGE_AGE_START;
-                       continue;
+               /* Page is referenced? Clear and move to the head of the list.. */
+               if (PageTestandClearReferenced(page)) {
+                       list_del(page_lru);
+                       list_add(page_lru, &zone->inactive_clean_list);
                }
 
                /* The page is dirty, or locked, move to inactive_dirty list. */
@@ -421,6 +419,14 @@ struct page * reclaim_page(zone_t * zone)
                        continue;
                }
 
+               /* Page is in use?  Move it to the active list. */
+               if (page_count(page) > 1) {
+                       UnlockPage(page);
+                       del_page_from_inactive_clean_list(page);
+                       add_page_to_active_list(page);
+                       continue;
+               }
+
                /* OK, remove the page from the caches. */
                if (PageSwapCache(page)) {
                        __delete_from_swap_cache(page);
@@ -503,12 +509,16 @@ int page_launder(int gfp_mask, int sync)
                        continue;
                }
 
-               /* Page is or was in use?  Move it to the active list. */
-               if (PageReferenced(page) || (!page->buffers && page_count(page) > 1) ||
-                               page_ramdisk(page)) {
+               /* Page is referenced? Clear and move to the head of the list.. */
+               if (PageTestandClearReferenced(page)) {
+                       list_del(page_lru);
+                       list_add(page_lru, &inactive_dirty_list);
+               }
+
+               /* Page is in use?  Move it to the active list. */
+               if ((!page->buffers && page_count(page) > 1) || page_ramdisk(page)) {
                        del_page_from_inactive_dirty_list(page);
                        add_page_to_active_list(page);
-                       page->age = PAGE_AGE_START;
                        continue;
                }
 
@@ -711,8 +721,7 @@ static int refill_inactive_scan(unsigned int priority)
                         *
                         * SUBTLE: we can have buffer pages with count 1.
                         */
-                       if (page->age == 0 && page_count(page) <=
-                                               (page->buffers ? 2 : 1)) {
+                       if (page_count(page) <= (page->buffers ? 2 : 1)) {
                                deactivate_page_nolock(page);
                                page_active = 0;
                        } else {
index ba94a4d6c030ea5042e11b4259ba42f85657b162..4a98c004927d19d6dba9ba2f29bc0f8b7edbd42e 100644 (file)
@@ -226,6 +226,7 @@ static struct nf_hook_ops ip_conntrack_local_in_ops
 
 static int init_or_cleanup(int init)
 {
+       struct proc_dir_entry *proc;
        int ret = 0;
 
        if (!init) goto cleanup;
@@ -234,11 +235,14 @@ static int init_or_cleanup(int init)
        if (ret < 0)
                goto cleanup_nothing;
 
-       proc_net_create("ip_conntrack",0,list_conntracks);
+       proc = proc_net_create("ip_conntrack",0,list_conntracks);
+       if (!proc) goto cleanup_init;
+       proc->owner = THIS_MODULE;
+
        ret = nf_register_hook(&ip_conntrack_in_ops);
        if (ret < 0) {
                printk("ip_conntrack: can't register in hook.\n");
-               goto cleanup_init;
+               goto cleanup_proc;
        }
        ret = nf_register_hook(&ip_conntrack_local_out_ops);
        if (ret < 0) {
@@ -266,8 +270,9 @@ static int init_or_cleanup(int init)
        nf_unregister_hook(&ip_conntrack_local_out_ops);
  cleanup_inops:
        nf_unregister_hook(&ip_conntrack_in_ops);
- cleanup_init:
+ cleanup_proc:
        proc_net_remove("ip_conntrack");
+ cleanup_init:
        ip_conntrack_cleanup();
  cleanup_nothing:
        return ret;
index e68804d65e0cb8e42b80d4319d8218328d520539..eb6215904403aca2b9f048de1c3bb367f140e286 100644 (file)
@@ -14,6 +14,7 @@
 #include <linux/inetdevice.h>
 #include <linux/proc_fs.h>
 #include <linux/version.h>
+#include <linux/module.h>
 #include <net/route.h>
 
 #define ASSERT_READ_LOCK(x) MUST_BE_READ_LOCKED(&ip_conntrack_lock)
@@ -302,13 +303,22 @@ masq_procinfo(char *buffer, char **start, off_t offset, int length)
 int __init masq_init(void)
 {
        int ret;
+       struct proc_dir_entry *proc;
 
        ret = ip_conntrack_init();
        if (ret == 0) {
                ret = ip_nat_init();
-               if (ret == 0)
-                       proc_net_create("ip_masquerade", 0, masq_procinfo);
-               else
+               if (ret == 0) {
+                       proc = proc_net_create("ip_masquerade",
+                                              0, masq_procinfo);
+                       if (proc)
+                               proc->owner = THIS_MODULE;
+                       else {
+                               ip_nat_cleanup();
+                               ip_conntrack_cleanup();
+                               ret = -ENOMEM;
+                       }
+               } else
                        ip_conntrack_cleanup();
        }
 
index 9fee820f6d0e5c03beed692614864ec72a7fa23f..6f6d5853a8ee93b8ab8f3174becc0b6d3b44d97c 100644 (file)
@@ -492,7 +492,7 @@ static int netlink_send_peer(ipq_queue_element_t *e)
 
 #define RCV_SKB_FAIL(err) do { netlink_ack(skb, nlh, (err)); return; } while (0);
 
-extern __inline__ void netlink_receive_user_skb(struct sk_buff *skb)
+static __inline__ void netlink_receive_user_skb(struct sk_buff *skb)
 {
        int status, type;
        struct nlmsghdr *nlh;
@@ -647,6 +647,7 @@ static int ipq_get_info(char *buffer, char **start, off_t offset, int length)
 static int __init init(void)
 {
        int status = 0;
+       struct proc_dir_entry *proc;
        
        nfnl = netlink_kernel_create(NETLINK_FIREWALL, netlink_receive_user_sk);
        if (nfnl == NULL) {
@@ -662,8 +663,14 @@ static int __init init(void)
                sock_release(nfnl->socket);
                return status;
        }
+       proc = proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
+       if (proc) proc->owner = THIS_MODULE;
+       else {
+               ipq_destroy_queue(nlq);
+               sock_release(nfnl->socket);
+               return -ENOMEM;
+       }
        register_netdevice_notifier(&ipq_dev_notifier);
-       proc_net_create(IPQ_PROC_FS_NAME, 0, ipq_get_info);
        ipq_sysctl_header = register_sysctl_table(ipq_root_table, 0);
        return status;
 }
index 4b0dd3dd2f030e66a61478619e6c877d57bdc993..3c430d0803bcb57f27a2a5bbd7379e11765c415c 100644 (file)
@@ -1730,10 +1730,16 @@ static int __init init(void)
        }
 
 #ifdef CONFIG_PROC_FS
-       if (!proc_net_create("ip_tables_names", 0, ipt_get_tables)) {
+       {
+       struct proc_dir_entry *proc;
+
+       proc = proc_net_create("ip_tables_names", 0, ipt_get_tables);
+       if (!proc) {
                nf_unregister_sockopt(&ipt_sockopts);
                return -ENOMEM;
        }
+       proc->owner = THIS_MODULE;
+       }
 #endif
 
        printk("ip_tables: (c)2000 Netfilter core team\n");
index be46649aaca92ed66b89785f3cd673919a5c74a0..e4812aafd1b17b4541c55ecfd45cd287b3977abc 100644 (file)
@@ -74,6 +74,7 @@
 #include <linux/sched.h>
 #include <linux/string.h>
 #include <linux/errno.h>
+#include <linux/module.h>
 
 #include <linux/socket.h>
 #include <linux/sockios.h>
@@ -1706,11 +1707,10 @@ struct firewall_ops ipfw_ops=
 
 int ipfw_init_or_cleanup(int init)
 {
+       struct proc_dir_entry *proc;
        int ret = 0;
        unsigned long flags;
 
-       FWC_WRITE_LOCK_IRQ(&ip_fw_lock, flags);
-
        if (!init) goto cleanup;
 
 #ifdef DEBUG_IP_FIREWALL_LOCKING
@@ -1727,17 +1727,24 @@ int ipfw_init_or_cleanup(int init)
        if (ret < 0)
                goto cleanup_netlink;
 
-       proc_net_create(IP_FW_PROC_CHAINS, S_IFREG | S_IRUSR | S_IWUSR, ip_chain_procinfo);
-       proc_net_create(IP_FW_PROC_CHAIN_NAMES, S_IFREG | S_IRUSR | S_IWUSR, ip_chain_name_procinfo);
+       proc = proc_net_create(IP_FW_PROC_CHAINS, S_IFREG | S_IRUSR | S_IWUSR,
+                              ip_chain_procinfo);
+       if (proc) proc->owner = THIS_MODULE;
+       proc = proc_net_create(IP_FW_PROC_CHAIN_NAMES,
+                              S_IFREG | S_IRUSR | S_IWUSR,
+                              ip_chain_name_procinfo);
+       if (proc) proc->owner = THIS_MODULE;
 
        IP_FW_INPUT_CHAIN = ip_init_chain(IP_FW_LABEL_INPUT, 1, FW_ACCEPT);
        IP_FW_FORWARD_CHAIN = ip_init_chain(IP_FW_LABEL_FORWARD, 1, FW_ACCEPT);
        IP_FW_OUTPUT_CHAIN = ip_init_chain(IP_FW_LABEL_OUTPUT, 1, FW_ACCEPT);
 
-       FWC_WRITE_UNLOCK_IRQ(&ip_fw_lock, flags);
        return ret;
 
  cleanup:
+       unregister_firewall(PF_INET, &ipfw_ops);
+
+       FWC_WRITE_LOCK_IRQ(&ip_fw_lock, flags);
        while (ip_fw_chains) {
                struct ip_chain *next = ip_fw_chains->next;
 
@@ -1745,18 +1752,16 @@ int ipfw_init_or_cleanup(int init)
                kfree(ip_fw_chains);
                ip_fw_chains = next;
        }
+       FWC_WRITE_UNLOCK_IRQ(&ip_fw_lock, flags);
 
        proc_net_remove(IP_FW_PROC_CHAINS);
        proc_net_remove(IP_FW_PROC_CHAIN_NAMES);
 
-       unregister_firewall(PF_INET, &ipfw_ops);
-
  cleanup_netlink:
 #if defined(CONFIG_NETLINK_DEV) || defined(CONFIG_NETLINK_DEV_MODULE)
        sock_release(ipfwsk->socket);
 
  cleanup_nothing:
 #endif
-       FWC_WRITE_UNLOCK_IRQ(&ip_fw_lock, flags);
        return ret;
 }
index 598c4bc6212cafc308e30dc5cad970e1df2254a1..f9fe554430c518cd5a89a38d8bf67debe075350c 100644 (file)
@@ -2107,7 +2107,12 @@ static int irda_getsockopt(struct socket *sock, int level, int optname,
                offset = sizeof(struct irda_device_list) - 
                        sizeof(struct irda_device_info);
 
-               /* Copy the list itself */
+               /* Copy the list itself - watch for overflow */
+               if(list.len > 2048)
+               {
+                       err = -EINVAL;
+                       goto bed;
+               }
                total = offset + (list.len * sizeof(struct irda_device_info));
                if (total > len)
                        total = len;
@@ -2117,7 +2122,7 @@ static int irda_getsockopt(struct socket *sock, int level, int optname,
                /* Write total number of bytes used back to client */
                if (put_user(total, optlen))
                        err = -EFAULT;
-
+bed:
                /* Free up our buffer */
                kfree(discoveries);
                if (err)
index 8cc206001f2732990b7689df9888ea483e994705..7e85e93a8dfe7a91350fcabb7e3dbb6285afa95b 100644 (file)
@@ -1059,17 +1059,13 @@ rpciod(void *ptr)
        rpciod_pid = current->pid;
        up(&rpciod_running);
 
-       exit_fs(current);
-       exit_files(current);
-       exit_mm(current);
+       daemonize();
 
        spin_lock_irq(&current->sigmask_lock);
        siginitsetinv(&current->blocked, sigmask(SIGKILL));
        recalc_sigpending(current);
        spin_unlock_irq(&current->sigmask_lock);
 
-       current->session = 1;
-       current->pgrp = 1;
        strcpy(current->comm, "rpciod");
 
        current->flags |= PF_MEMALLOC;
index cc6cf6ad8dafe22c6fddb8c5cb0e75b06e0a57ea..9c167af7c1e9777b863c0dc2715da0f8fd80d493 100644 (file)
@@ -1845,7 +1845,7 @@ static inline void unix_sysctl_register(void) {}
 static inline void unix_sysctl_unregister(void) {}
 #endif
 
-static const char banner[] __initdata = KERN_INFO "NET4: Unix domain sockets 1.0/SMP for Linux NET4.0.\n";
+static char banner[] __initdata = KERN_INFO "NET4: Unix domain sockets 1.0/SMP for Linux NET4.0.\n";
 
 static int __init af_unix_init(void)
 {
index 9593fe88b7b11d0f1f5dcf932b954b33163fa2f0..cf37e006dcd81290cc741b2643be36c3c471f67a 100644 (file)
@@ -291,7 +291,7 @@ void use_config(const char * name, int len)
  * Thus, there is one memory access per sizeof(unsigned long) characters.
  */
 
-#if defined(__alpha__) || defined(__i386__) || defined(__ia64__) || defined(__MIPSEL__)        \
+#if defined(__alpha__) || defined(__i386__) || defined(__ia64__)  || defined(__x86_64__) || defined(__MIPSEL__)        \
     || defined(__arm__)
 #define LE_MACHINE
 #endif