diff options
Diffstat (limited to 'fs/hpfs')
-rw-r--r-- | fs/hpfs/Makefile | 8 | ||||
-rw-r--r-- | fs/hpfs/alloc.c | 456 | ||||
-rw-r--r-- | fs/hpfs/anode.c | 491 | ||||
-rw-r--r-- | fs/hpfs/buffer.c | 175 | ||||
-rw-r--r-- | fs/hpfs/dentry.c | 60 | ||||
-rw-r--r-- | fs/hpfs/dir.c | 320 | ||||
-rw-r--r-- | fs/hpfs/dnode.c | 1080 | ||||
-rw-r--r-- | fs/hpfs/ea.c | 363 | ||||
-rw-r--r-- | fs/hpfs/file.c | 140 | ||||
-rw-r--r-- | fs/hpfs/hpfs.h | 493 | ||||
-rw-r--r-- | fs/hpfs/hpfs_fn.h | 338 | ||||
-rw-r--r-- | fs/hpfs/inode.c | 291 | ||||
-rw-r--r-- | fs/hpfs/map.c | 275 | ||||
-rw-r--r-- | fs/hpfs/name.c | 144 | ||||
-rw-r--r-- | fs/hpfs/namei.c | 673 | ||||
-rw-r--r-- | fs/hpfs/super.c | 701 |
16 files changed, 6008 insertions, 0 deletions
diff --git a/fs/hpfs/Makefile b/fs/hpfs/Makefile new file mode 100644 index 00000000000..57b786fb982 --- /dev/null +++ b/fs/hpfs/Makefile @@ -0,0 +1,8 @@ +# +# Makefile for the Linux hpfs filesystem routines. +# + +obj-$(CONFIG_HPFS_FS) += hpfs.o + +hpfs-objs := alloc.o anode.o buffer.o dentry.o dir.o dnode.o ea.o file.o \ + inode.o map.o name.o namei.o super.o diff --git a/fs/hpfs/alloc.c b/fs/hpfs/alloc.c new file mode 100644 index 00000000000..5503e2c2891 --- /dev/null +++ b/fs/hpfs/alloc.c @@ -0,0 +1,456 @@ +/* + * linux/fs/hpfs/alloc.c + * + * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999 + * + * HPFS bitmap operations + */ + +#include "hpfs_fn.h" + +static int hpfs_alloc_if_possible_nolock(struct super_block *s, secno sec); + +/* + * Check if a sector is allocated in bitmap + * This is really slow. Turned on only if chk==2 + */ + +static int chk_if_allocated(struct super_block *s, secno sec, char *msg) +{ + struct quad_buffer_head qbh; + unsigned *bmp; + if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "chk"))) goto fail; + if ((bmp[(sec & 0x3fff) >> 5] >> (sec & 0x1f)) & 1) { + hpfs_error(s, "sector '%s' - %08x not allocated in bitmap", msg, sec); + goto fail1; + } + hpfs_brelse4(&qbh); + if (sec >= hpfs_sb(s)->sb_dirband_start && sec < hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) { + unsigned ssec = (sec - hpfs_sb(s)->sb_dirband_start) / 4; + if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto fail; + if ((bmp[ssec >> 5] >> (ssec & 0x1f)) & 1) { + hpfs_error(s, "sector '%s' - %08x not allocated in directory bitmap", msg, sec); + goto fail1; + } + hpfs_brelse4(&qbh); + } + return 0; + fail1: + hpfs_brelse4(&qbh); + fail: + return 1; +} + +/* + * Check if sector(s) have proper number and additionally check if they're + * allocated in bitmap. + */ + +int hpfs_chk_sectors(struct super_block *s, secno start, int len, char *msg) +{ + if (start + len < start || start < 0x12 || + start + len > hpfs_sb(s)->sb_fs_size) { + hpfs_error(s, "sector(s) '%s' badly placed at %08x", msg, start); + return 1; + } + if (hpfs_sb(s)->sb_chk>=2) { + int i; + for (i = 0; i < len; i++) + if (chk_if_allocated(s, start + i, msg)) return 1; + } + return 0; +} + +static secno alloc_in_bmp(struct super_block *s, secno near, unsigned n, unsigned forward) +{ + struct quad_buffer_head qbh; + unsigned *bmp; + unsigned bs = near & ~0x3fff; + unsigned nr = (near & 0x3fff) & ~(n - 1); + /*unsigned mnr;*/ + unsigned i, q; + int a, b; + secno ret = 0; + if (n != 1 && n != 4) { + hpfs_error(s, "Bad allocation size: %d", n); + return 0; + } + lock_super(s); + if (bs != ~0x3fff) { + if (!(bmp = hpfs_map_bitmap(s, near >> 14, &qbh, "aib"))) goto uls; + } else { + if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) goto uls; + } + if (!tstbits(bmp, nr, n + forward)) { + ret = bs + nr; + goto rt; + } + /*if (!tstbits(bmp, nr + n, n + forward)) { + ret = bs + nr + n; + goto rt; + }*/ + q = nr + n; b = 0; + while ((a = tstbits(bmp, q, n + forward)) != 0) { + q += a; + if (n != 1) q = ((q-1)&~(n-1))+n; + if (!b) { + if (q>>5 != nr>>5) { + b = 1; + q = nr & 0x1f; + } + } else if (q > nr) break; + } + if (!a) { + ret = bs + q; + goto rt; + } + nr >>= 5; + /*for (i = nr + 1; i != nr; i++, i &= 0x1ff) {*/ + i = nr; + do { + if (!bmp[i]) goto cont; + if (n + forward >= 0x3f && bmp[i] != -1) goto cont; + q = i<<5; + if (i > 0) { + unsigned k = bmp[i-1]; + while (k & 0x80000000) { + q--; k <<= 1; + } + } + if (n != 1) q = ((q-1)&~(n-1))+n; + while ((a = tstbits(bmp, q, n + forward)) != 0) { + q += a; + if (n != 1) q = ((q-1)&~(n-1))+n; + if (q>>5 > i) break; + } + if (!a) { + ret = bs + q; + goto rt; + } + cont: + i++, i &= 0x1ff; + } while (i != nr); + rt: + if (ret) { + if (hpfs_sb(s)->sb_chk && ((ret >> 14) != (bs >> 14) || (bmp[(ret & 0x3fff) >> 5] | ~(((1 << n) - 1) << (ret & 0x1f))) != 0xffffffff)) { + hpfs_error(s, "Allocation doesn't work! Wanted %d, allocated at %08x", n, ret); + ret = 0; + goto b; + } + bmp[(ret & 0x3fff) >> 5] &= ~(((1 << n) - 1) << (ret & 0x1f)); + hpfs_mark_4buffers_dirty(&qbh); + } + b: + hpfs_brelse4(&qbh); + uls: + unlock_super(s); + return ret; +} + +/* + * Allocation strategy: 1) search place near the sector specified + * 2) search bitmap where free sectors last found + * 3) search all bitmaps + * 4) search all bitmaps ignoring number of pre-allocated + * sectors + */ + +secno hpfs_alloc_sector(struct super_block *s, secno near, unsigned n, int forward, int lock) +{ + secno sec; + int i; + unsigned n_bmps; + struct hpfs_sb_info *sbi = hpfs_sb(s); + int f_p = 0; + int near_bmp; + if (forward < 0) { + forward = -forward; + f_p = 1; + } + if (lock) hpfs_lock_creation(s); + n_bmps = (sbi->sb_fs_size + 0x4000 - 1) >> 14; + if (near && near < sbi->sb_fs_size) { + if ((sec = alloc_in_bmp(s, near, n, f_p ? forward : forward/4))) goto ret; + near_bmp = near >> 14; + } else near_bmp = n_bmps / 2; + /* + if (b != -1) { + if ((sec = alloc_in_bmp(s, b<<14, n, f_p ? forward : forward/2))) { + b &= 0x0fffffff; + goto ret; + } + if (b > 0x10000000) if ((sec = alloc_in_bmp(s, (b&0xfffffff)<<14, n, f_p ? forward : 0))) goto ret; + */ + if (!f_p) if (forward > sbi->sb_max_fwd_alloc) forward = sbi->sb_max_fwd_alloc; + less_fwd: + for (i = 0; i < n_bmps; i++) { + if (near_bmp+i < n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i) << 14, n, forward)))) { + sbi->sb_c_bitmap = near_bmp+i; + goto ret; + } + if (!forward) { + if (near_bmp-i-1 >= 0 && ((sec = alloc_in_bmp(s, (near_bmp-i-1) << 14, n, forward)))) { + sbi->sb_c_bitmap = near_bmp-i-1; + goto ret; + } + } else { + if (near_bmp+i >= n_bmps && ((sec = alloc_in_bmp(s, (near_bmp+i-n_bmps) << 14, n, forward)))) { + sbi->sb_c_bitmap = near_bmp+i-n_bmps; + goto ret; + } + } + if (i == 1 && sbi->sb_c_bitmap != -1 && ((sec = alloc_in_bmp(s, (sbi->sb_c_bitmap) << 14, n, forward)))) { + goto ret; + } + } + if (!f_p) { + if (forward) { + sbi->sb_max_fwd_alloc = forward * 3 / 4; + forward /= 2; + goto less_fwd; + } + } + sec = 0; + ret: + if (sec && f_p) { + for (i = 0; i < forward; i++) { + if (!hpfs_alloc_if_possible_nolock(s, sec + i + 1)) { + hpfs_error(s, "Prealloc doesn't work! Wanted %d, allocated at %08x, can't allocate %d", forward, sec, i); + sec = 0; + break; + } + } + } + if (lock) hpfs_unlock_creation(s); + return sec; +} + +static secno alloc_in_dirband(struct super_block *s, secno near, int lock) +{ + unsigned nr = near; + secno sec; + struct hpfs_sb_info *sbi = hpfs_sb(s); + if (nr < sbi->sb_dirband_start) + nr = sbi->sb_dirband_start; + if (nr >= sbi->sb_dirband_start + sbi->sb_dirband_size) + nr = sbi->sb_dirband_start + sbi->sb_dirband_size - 4; + nr -= sbi->sb_dirband_start; + nr >>= 2; + if (lock) hpfs_lock_creation(s); + sec = alloc_in_bmp(s, (~0x3fff) | nr, 1, 0); + if (lock) hpfs_unlock_creation(s); + if (!sec) return 0; + return ((sec & 0x3fff) << 2) + sbi->sb_dirband_start; +} + +/* Alloc sector if it's free */ + +static int hpfs_alloc_if_possible_nolock(struct super_block *s, secno sec) +{ + struct quad_buffer_head qbh; + unsigned *bmp; + lock_super(s); + if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "aip"))) goto end; + if (bmp[(sec & 0x3fff) >> 5] & (1 << (sec & 0x1f))) { + bmp[(sec & 0x3fff) >> 5] &= ~(1 << (sec & 0x1f)); + hpfs_mark_4buffers_dirty(&qbh); + hpfs_brelse4(&qbh); + unlock_super(s); + return 1; + } + hpfs_brelse4(&qbh); + end: + unlock_super(s); + return 0; +} + +int hpfs_alloc_if_possible(struct super_block *s, secno sec) +{ + int r; + hpfs_lock_creation(s); + r = hpfs_alloc_if_possible_nolock(s, sec); + hpfs_unlock_creation(s); + return r; +} + +/* Free sectors in bitmaps */ + +void hpfs_free_sectors(struct super_block *s, secno sec, unsigned n) +{ + struct quad_buffer_head qbh; + unsigned *bmp; + struct hpfs_sb_info *sbi = hpfs_sb(s); + /*printk("2 - ");*/ + if (!n) return; + if (sec < 0x12) { + hpfs_error(s, "Trying to free reserved sector %08x", sec); + return; + } + lock_super(s); + sbi->sb_max_fwd_alloc += n > 0xffff ? 0xffff : n; + if (sbi->sb_max_fwd_alloc > 0xffffff) sbi->sb_max_fwd_alloc = 0xffffff; + new_map: + if (!(bmp = hpfs_map_bitmap(s, sec >> 14, &qbh, "free"))) { + unlock_super(s); + return; + } + new_tst: + if ((bmp[(sec & 0x3fff) >> 5] >> (sec & 0x1f) & 1)) { + hpfs_error(s, "sector %08x not allocated", sec); + hpfs_brelse4(&qbh); + unlock_super(s); + return; + } + bmp[(sec & 0x3fff) >> 5] |= 1 << (sec & 0x1f); + if (!--n) { + hpfs_mark_4buffers_dirty(&qbh); + hpfs_brelse4(&qbh); + unlock_super(s); + return; + } + if (!(++sec & 0x3fff)) { + hpfs_mark_4buffers_dirty(&qbh); + hpfs_brelse4(&qbh); + goto new_map; + } + goto new_tst; +} + +/* + * Check if there are at least n free dnodes on the filesystem. + * Called before adding to dnode. If we run out of space while + * splitting dnodes, it would corrupt dnode tree. + */ + +int hpfs_check_free_dnodes(struct super_block *s, int n) +{ + int n_bmps = (hpfs_sb(s)->sb_fs_size + 0x4000 - 1) >> 14; + int b = hpfs_sb(s)->sb_c_bitmap & 0x0fffffff; + int i, j; + unsigned *bmp; + struct quad_buffer_head qbh; + if ((bmp = hpfs_map_dnode_bitmap(s, &qbh))) { + for (j = 0; j < 512; j++) { + unsigned k; + if (!bmp[j]) continue; + for (k = bmp[j]; k; k >>= 1) if (k & 1) if (!--n) { + hpfs_brelse4(&qbh); + return 0; + } + } + } + hpfs_brelse4(&qbh); + i = 0; + if (hpfs_sb(s)->sb_c_bitmap != -1) { + bmp = hpfs_map_bitmap(s, b, &qbh, "chkdn1"); + goto chk_bmp; + } + chk_next: + if (i == b) i++; + if (i >= n_bmps) return 1; + bmp = hpfs_map_bitmap(s, i, &qbh, "chkdn2"); + chk_bmp: + if (bmp) { + for (j = 0; j < 512; j++) { + unsigned k; + if (!bmp[j]) continue; + for (k = 0xf; k; k <<= 4) + if ((bmp[j] & k) == k) { + if (!--n) { + hpfs_brelse4(&qbh); + return 0; + } + } + } + hpfs_brelse4(&qbh); + } + i++; + goto chk_next; +} + +void hpfs_free_dnode(struct super_block *s, dnode_secno dno) +{ + if (hpfs_sb(s)->sb_chk) if (dno & 3) { + hpfs_error(s, "hpfs_free_dnode: dnode %08x not aligned", dno); + return; + } + if (dno < hpfs_sb(s)->sb_dirband_start || + dno >= hpfs_sb(s)->sb_dirband_start + hpfs_sb(s)->sb_dirband_size) { + hpfs_free_sectors(s, dno, 4); + } else { + struct quad_buffer_head qbh; + unsigned *bmp; + unsigned ssec = (dno - hpfs_sb(s)->sb_dirband_start) / 4; + lock_super(s); + if (!(bmp = hpfs_map_dnode_bitmap(s, &qbh))) { + unlock_super(s); + return; + } + bmp[ssec >> 5] |= 1 << (ssec & 0x1f); + hpfs_mark_4buffers_dirty(&qbh); + hpfs_brelse4(&qbh); + unlock_super(s); + } +} + +struct dnode *hpfs_alloc_dnode(struct super_block *s, secno near, + dnode_secno *dno, struct quad_buffer_head *qbh, + int lock) +{ + struct dnode *d; + if (hpfs_count_one_bitmap(s, hpfs_sb(s)->sb_dmap) > FREE_DNODES_ADD) { + if (!(*dno = alloc_in_dirband(s, near, lock))) + if (!(*dno = hpfs_alloc_sector(s, near, 4, 0, lock))) return NULL; + } else { + if (!(*dno = hpfs_alloc_sector(s, near, 4, 0, lock))) + if (!(*dno = alloc_in_dirband(s, near, lock))) return NULL; + } + if (!(d = hpfs_get_4sectors(s, *dno, qbh))) { + hpfs_free_dnode(s, *dno); + return NULL; + } + memset(d, 0, 2048); + d->magic = DNODE_MAGIC; + d->first_free = 52; + d->dirent[0] = 32; + d->dirent[2] = 8; + d->dirent[30] = 1; + d->dirent[31] = 255; + d->self = *dno; + return d; +} + +struct fnode *hpfs_alloc_fnode(struct super_block *s, secno near, fnode_secno *fno, + struct buffer_head **bh) +{ + struct fnode *f; + if (!(*fno = hpfs_alloc_sector(s, near, 1, FNODE_ALLOC_FWD, 1))) return NULL; + if (!(f = hpfs_get_sector(s, *fno, bh))) { + hpfs_free_sectors(s, *fno, 1); + return NULL; + } + memset(f, 0, 512); + f->magic = FNODE_MAGIC; + f->ea_offs = 0xc4; + f->btree.n_free_nodes = 8; + f->btree.first_free = 8; + return f; +} + +struct anode *hpfs_alloc_anode(struct super_block *s, secno near, anode_secno *ano, + struct buffer_head **bh) +{ + struct anode *a; + if (!(*ano = hpfs_alloc_sector(s, near, 1, ANODE_ALLOC_FWD, 1))) return NULL; + if (!(a = hpfs_get_sector(s, *ano, bh))) { + hpfs_free_sectors(s, *ano, 1); + return NULL; + } + memset(a, 0, 512); + a->magic = ANODE_MAGIC; + a->self = *ano; + a->btree.n_free_nodes = 40; + a->btree.n_used_nodes = 0; + a->btree.first_free = 8; + return a; +} diff --git a/fs/hpfs/anode.c b/fs/hpfs/anode.c new file mode 100644 index 00000000000..1aa88c4e096 --- /dev/null +++ b/fs/hpfs/anode.c @@ -0,0 +1,491 @@ +/* + * linux/fs/hpfs/anode.c + * + * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999 + * + * handling HPFS anode tree that contains file allocation info + */ + +#include "hpfs_fn.h" + +/* Find a sector in allocation tree */ + +secno hpfs_bplus_lookup(struct super_block *s, struct inode *inode, + struct bplus_header *btree, unsigned sec, + struct buffer_head *bh) +{ + anode_secno a = -1; + struct anode *anode; + int i; + int c1, c2 = 0; + go_down: + if (hpfs_sb(s)->sb_chk) if (hpfs_stop_cycles(s, a, &c1, &c2, "hpfs_bplus_lookup")) return -1; + if (btree->internal) { + for (i = 0; i < btree->n_used_nodes; i++) + if (btree->u.internal[i].file_secno > sec) { + a = btree->u.internal[i].down; + brelse(bh); + if (!(anode = hpfs_map_anode(s, a, &bh))) return -1; + btree = &anode->btree; + goto go_down; + } + hpfs_error(s, "sector %08x not found in internal anode %08x", sec, a); + brelse(bh); + return -1; + } + for (i = 0; i < btree->n_used_nodes; i++) + if (btree->u.external[i].file_secno <= sec && + btree->u.external[i].file_secno + btree->u.external[i].length > sec) { + a = btree->u.external[i].disk_secno + sec - btree->u.external[i].file_secno; + if (hpfs_sb(s)->sb_chk) if (hpfs_chk_sectors(s, a, 1, "data")) { + brelse(bh); + return -1; + } + if (inode) { + struct hpfs_inode_info *hpfs_inode = hpfs_i(inode); + hpfs_inode->i_file_sec = btree->u.external[i].file_secno; + hpfs_inode->i_disk_sec = btree->u.external[i].disk_secno; + hpfs_inode->i_n_secs = btree->u.external[i].length; + } + brelse(bh); + return a; + } + hpfs_error(s, "sector %08x not found in external anode %08x", sec, a); + brelse(bh); + return -1; +} + +/* Add a sector to tree */ + +secno hpfs_add_sector_to_btree(struct super_block *s, secno node, int fnod, unsigned fsecno) +{ + struct bplus_header *btree; + struct anode *anode = NULL, *ranode = NULL; + struct fnode *fnode; + anode_secno a, na = -1, ra, up = -1; + secno se; + struct buffer_head *bh, *bh1, *bh2; + int n; + unsigned fs; + int c1, c2 = 0; + if (fnod) { + if (!(fnode = hpfs_map_fnode(s, node, &bh))) return -1; + btree = &fnode->btree; + } else { + if (!(anode = hpfs_map_anode(s, node, &bh))) return -1; + btree = &anode->btree; + } + a = node; + go_down: + if ((n = btree->n_used_nodes - 1) < -!!fnod) { + hpfs_error(s, "anode %08x has no entries", a); + brelse(bh); + return -1; + } + if (btree->internal) { + a = btree->u.internal[n].down; + btree->u.internal[n].file_secno = -1; + mark_buffer_dirty(bh); + brelse(bh); + if (hpfs_sb(s)->sb_chk) + if (hpfs_stop_cycles(s, a, &c1, &c2, "hpfs_add_sector_to_btree #1")) return -1; + if (!(anode = hpfs_map_anode(s, a, &bh))) return -1; + btree = &anode->btree; + goto go_down; + } + if (n >= 0) { + if (btree->u.external[n].file_secno + btree->u.external[n].length != fsecno) { + hpfs_error(s, "allocated size %08x, trying to add sector %08x, %cnode %08x", + btree->u.external[n].file_secno + btree->u.external[n].length, fsecno, + fnod?'f':'a', node); + brelse(bh); + return -1; + } + if (hpfs_alloc_if_possible(s, se = btree->u.external[n].disk_secno + btree->u.external[n].length)) { + btree->u.external[n].length++; + mark_buffer_dirty(bh); + brelse(bh); + return se; + } + } else { + if (fsecno) { + hpfs_error(s, "empty file %08x, trying to add sector %08x", node, fsecno); + brelse(bh); + return -1; + } + se = !fnod ? node : (node + 16384) & ~16383; + } + if (!(se = hpfs_alloc_sector(s, se, 1, fsecno*ALLOC_M>ALLOC_FWD_MAX ? ALLOC_FWD_MAX : fsecno*ALLOC_M<ALLOC_FWD_MIN ? ALLOC_FWD_MIN : fsecno*ALLOC_M, 1))) { + brelse(bh); + return -1; + } + fs = n < 0 ? 0 : btree->u.external[n].file_secno + btree->u.external[n].length; + if (!btree->n_free_nodes) { + up = a != node ? anode->up : -1; + if (!(anode = hpfs_alloc_anode(s, a, &na, &bh1))) { + brelse(bh); + hpfs_free_sectors(s, se, 1); + return -1; + } + if (a == node && fnod) { + anode->up = node; + anode->btree.fnode_parent = 1; + anode->btree.n_used_nodes = btree->n_used_nodes; + anode->btree.first_free = btree->first_free; + anode->btree.n_free_nodes = 40 - anode->btree.n_used_nodes; + memcpy(&anode->u, &btree->u, btree->n_used_nodes * 12); + btree->internal = 1; + btree->n_free_nodes = 11; + btree->n_used_nodes = 1; + btree->first_free = (char *)&(btree->u.internal[1]) - (char *)btree; + btree->u.internal[0].file_secno = -1; + btree->u.internal[0].down = na; + mark_buffer_dirty(bh); + } else if (!(ranode = hpfs_alloc_anode(s, /*a*/0, &ra, &bh2))) { + brelse(bh); + brelse(bh1); + hpfs_free_sectors(s, se, 1); + hpfs_free_sectors(s, na, 1); + return -1; + } + brelse(bh); + bh = bh1; + btree = &anode->btree; + } + btree->n_free_nodes--; n = btree->n_used_nodes++; + btree->first_free += 12; + btree->u.external[n].disk_secno = se; + btree->u.external[n].file_secno = fs; + btree->u.external[n].length = 1; + mark_buffer_dirty(bh); + brelse(bh); + if ((a == node && fnod) || na == -1) return se; + c2 = 0; + while (up != -1) { + struct anode *new_anode; + if (hpfs_sb(s)->sb_chk) + if (hpfs_stop_cycles(s, up, &c1, &c2, "hpfs_add_sector_to_btree #2")) return -1; + if (up != node || !fnod) { + if (!(anode = hpfs_map_anode(s, up, &bh))) return -1; + btree = &anode->btree; + } else { + if (!(fnode = hpfs_map_fnode(s, up, &bh))) return -1; + btree = &fnode->btree; + } + if (btree->n_free_nodes) { + btree->n_free_nodes--; n = btree->n_used_nodes++; + btree->first_free += 8; + btree->u.internal[n].file_secno = -1; + btree->u.internal[n].down = na; + btree->u.internal[n-1].file_secno = fs; + mark_buffer_dirty(bh); + brelse(bh); + brelse(bh2); + hpfs_free_sectors(s, ra, 1); + if ((anode = hpfs_map_anode(s, na, &bh))) { + anode->up = up; + anode->btree.fnode_parent = up == node && fnod; + mark_buffer_dirty(bh); + brelse(bh); + } + return se; + } + up = up != node ? anode->up : -1; + btree->u.internal[btree->n_used_nodes - 1].file_secno = /*fs*/-1; + mark_buffer_dirty(bh); + brelse(bh); + a = na; + if ((new_anode = hpfs_alloc_anode(s, a, &na, &bh))) { + anode = new_anode; + /*anode->up = up != -1 ? up : ra;*/ + anode->btree.internal = 1; + anode->btree.n_used_nodes = 1; + anode->btree.n_free_nodes = 59; + anode->btree.first_free = 16; + anode->btree.u.internal[0].down = a; + anode->btree.u.internal[0].file_secno = -1; + mark_buffer_dirty(bh); + brelse(bh); + if ((anode = hpfs_map_anode(s, a, &bh))) { + anode->up = na; + mark_buffer_dirty(bh); + brelse(bh); + } + } else na = a; + } + if ((anode = hpfs_map_anode(s, na, &bh))) { + anode->up = node; + if (fnod) anode->btree.fnode_parent = 1; + mark_buffer_dirty(bh); + brelse(bh); + } + if (!fnod) { + if (!(anode = hpfs_map_anode(s, node, &bh))) { + brelse(bh2); + return -1; + } + btree = &anode->btree; + } else { + if (!(fnode = hpfs_map_fnode(s, node, &bh))) { + brelse(bh2); + return -1; + } + btree = &fnode->btree; + } + ranode->up = node; + memcpy(&ranode->btree, btree, btree->first_free); + if (fnod) ranode->btree.fnode_parent = 1; + ranode->btree.n_free_nodes = (ranode->btree.internal ? 60 : 40) - ranode->btree.n_used_nodes; + if (ranode->btree.internal) for (n = 0; n < ranode->btree.n_used_nodes; n++) { + struct anode *unode; + if ((unode = hpfs_map_anode(s, ranode->u.internal[n].down, &bh1))) { + unode->up = ra; + unode->btree.fnode_parent = 0; + mark_buffer_dirty(bh1); + brelse(bh1); + } + } + btree->internal = 1; + btree->n_free_nodes = fnod ? 10 : 58; + btree->n_used_nodes = 2; + btree->first_free = (char *)&btree->u.internal[2] - (char *)btree; + btree->u.internal[0].file_secno = fs; + btree->u.internal[0].down = ra; + btree->u.internal[1].file_secno = -1; + btree->u.internal[1].down = na; + mark_buffer_dirty(bh); + brelse(bh); + mark_buffer_dirty(bh2); + brelse(bh2); + return se; +} + +/* + * Remove allocation tree. Recursion would look much nicer but + * I want to avoid it because it can cause stack overflow. + */ + +void hpfs_remove_btree(struct super_block *s, struct bplus_header *btree) +{ + struct bplus_header *btree1 = btree; + struct anode *anode = NULL; + anode_secno ano = 0, oano; + struct buffer_head *bh; + int level = 0; + int pos = 0; + int i; + int c1, c2 = 0; + int d1, d2; + go_down: + d2 = 0; + while (btree1->internal) { + ano = btree1->u.internal[pos].down; + if (level) brelse(bh); + if (hpfs_sb(s)->sb_chk) + if (hpfs_stop_cycles(s, ano, &d1, &d2, "hpfs_remove_btree #1")) + return; + if (!(anode = hpfs_map_anode(s, ano, &bh))) return; + btree1 = &anode->btree; + level++; + pos = 0; + } + for (i = 0; i < btree1->n_used_nodes; i++) + hpfs_free_sectors(s, btree1->u.external[i].disk_secno, btree1->u.external[i].length); + go_up: + if (!level) return; + brelse(bh); + if (hpfs_sb(s)->sb_chk) + if (hpfs_stop_cycles(s, ano, &c1, &c2, "hpfs_remove_btree #2")) return; + hpfs_free_sectors(s, ano, 1); + oano = ano; + ano = anode->up; + if (--level) { + if (!(anode = hpfs_map_anode(s, ano, &bh))) return; + btree1 = &anode->btree; + } else btree1 = btree; + for (i = 0; i < btree1->n_used_nodes; i++) { + if (btree1->u.internal[i].down == oano) { + if ((pos = i + 1) < btree1->n_used_nodes) + goto go_down; + else + goto go_up; + } + } + hpfs_error(s, + "reference to anode %08x not found in anode %08x " + "(probably bad up pointer)", + oano, level ? ano : -1); + if (level) + brelse(bh); +} + +/* Just a wrapper around hpfs_bplus_lookup .. used for reading eas */ + +static secno anode_lookup(struct super_block *s, anode_secno a, unsigned sec) +{ + struct anode *anode; + struct buffer_head *bh; + if (!(anode = hpfs_map_anode(s, a, &bh))) return -1; + return hpfs_bplus_lookup(s, NULL, &anode->btree, sec, bh); +} + +int hpfs_ea_read(struct super_block *s, secno a, int ano, unsigned pos, + unsigned len, char *buf) +{ + struct buffer_head *bh; + char *data; + secno sec; + unsigned l; + while (len) { + if (ano) { + if ((sec = anode_lookup(s, a, pos >> 9)) == -1) + return -1; + } else sec = a + (pos >> 9); + if (hpfs_sb(s)->sb_chk) if (hpfs_chk_sectors(s, sec, 1, "ea #1")) return -1; + if (!(data = hpfs_map_sector(s, sec, &bh, (len - 1) >> 9))) + return -1; + l = 0x200 - (pos & 0x1ff); if (l > len) l = len; + memcpy(buf, data + (pos & 0x1ff), l); + brelse(bh); + buf += l; pos += l; len -= l; + } + return 0; +} + +int hpfs_ea_write(struct super_block *s, secno a, int ano, unsigned pos, + unsigned len, char *buf) +{ + struct buffer_head *bh; + char *data; + secno sec; + unsigned l; + while (len) { + if (ano) { + if ((sec = anode_lookup(s, a, pos >> 9)) == -1) + return -1; + } else sec = a + (pos >> 9); + if (hpfs_sb(s)->sb_chk) if (hpfs_chk_sectors(s, sec, 1, "ea #2")) return -1; + if (!(data = hpfs_map_sector(s, sec, &bh, (len - 1) >> 9))) + return -1; + l = 0x200 - (pos & 0x1ff); if (l > len) l = len; + memcpy(data + (pos & 0x1ff), buf, l); + mark_buffer_dirty(bh); + brelse(bh); + buf += l; pos += l; len -= l; + } + return 0; +} + +void hpfs_ea_remove(struct super_block *s, secno a, int ano, unsigned len) +{ + struct anode *anode; + struct buffer_head *bh; + if (ano) { + if (!(anode = hpfs_map_anode(s, a, &bh))) return; + hpfs_remove_btree(s, &anode->btree); + brelse(bh); + hpfs_free_sectors(s, a, 1); + } else hpfs_free_sectors(s, a, (len + 511) >> 9); +} + +/* Truncate allocation tree. Doesn't join anodes - I hope it doesn't matter */ + +void hpfs_truncate_btree(struct super_block *s, secno f, int fno, unsigned secs) +{ + struct fnode *fnode; + struct anode *anode; + struct buffer_head *bh; + struct bplus_header *btree; + anode_secno node = f; + int i, j, nodes; + int c1, c2 = 0; + if (fno) { + if (!(fnode = hpfs_map_fnode(s, f, &bh))) return; + btree = &fnode->btree; + } else { + if (!(anode = hpfs_map_anode(s, f, &bh))) return; + btree = &anode->btree; + } + if (!secs) { + hpfs_remove_btree(s, btree); + if (fno) { + btree->n_free_nodes = 8; + btree->n_used_nodes = 0; + btree->first_free = 8; + btree->internal = 0; + mark_buffer_dirty(bh); + } else hpfs_free_sectors(s, f, 1); + brelse(bh); + return; + } + while (btree->internal) { + nodes = btree->n_used_nodes + btree->n_free_nodes; + for (i = 0; i < btree->n_used_nodes; i++) + if (btree->u.internal[i].file_secno >= secs) goto f; + brelse(bh); + hpfs_error(s, "internal btree %08x doesn't end with -1", node); + return; + f: + for (j = i + 1; j < btree->n_used_nodes; j++) + hpfs_ea_remove(s, btree->u.internal[j].down, 1, 0); + btree->n_used_nodes = i + 1; + btree->n_free_nodes = nodes - btree->n_used_nodes; + btree->first_free = 8 + 8 * btree->n_used_nodes; + mark_buffer_dirty(bh); + if (btree->u.internal[i].file_secno == secs) { + brelse(bh); + return; + } + node = btree->u.internal[i].down; + brelse(bh); + if (hpfs_sb(s)->sb_chk) + if (hpfs_stop_cycles(s, node, &c1, &c2, "hpfs_truncate_btree")) + return; + if (!(anode = hpfs_map_anode(s, node, &bh))) return; + btree = &anode->btree; + } + nodes = btree->n_used_nodes + btree->n_free_nodes; + for (i = 0; i < btree->n_used_nodes; i++) + if (btree->u.external[i].file_secno + btree->u.external[i].length >= secs) goto ff; + brelse(bh); + return; + ff: + if (secs <= btree->u.external[i].file_secno) { + hpfs_error(s, "there is an allocation error in file %08x, sector %08x", f, secs); + if (i) i--; + } + else if (btree->u.external[i].file_secno + btree->u.external[i].length > secs) { + hpfs_free_sectors(s, btree->u.external[i].disk_secno + secs - + btree->u.external[i].file_secno, btree->u.external[i].length + - secs + btree->u.external[i].file_secno); /* I hope gcc optimizes this :-) */ + btree->u.external[i].length = secs - btree->u.external[i].file_secno; + } + for (j = i + 1; j < btree->n_used_nodes; j++) + hpfs_free_sectors(s, btree->u.external[j].disk_secno, btree->u.external[j].length); + btree->n_used_nodes = i + 1; + btree->n_free_nodes = nodes - btree->n_used_nodes; + btree->first_free = 8 + 12 * btree->n_used_nodes; + mark_buffer_dirty(bh); + brelse(bh); +} + +/* Remove file or directory and it's eas - note that directory must + be empty when this is called. */ + +void hpfs_remove_fnode(struct super_block *s, fnode_secno fno) +{ + struct buffer_head *bh; + struct fnode *fnode; + struct extended_attribute *ea; + struct extended_attribute *ea_end; + if (!(fnode = hpfs_map_fnode(s, fno, &bh))) return; + if (!fnode->dirflag) hpfs_remove_btree(s, &fnode->btree); + else hpfs_remove_dtree(s, fnode->u.external[0].disk_secno); + ea_end = fnode_end_ea(fnode); + for (ea = fnode_ea(fnode); ea < ea_end; ea = next_ea(ea)) + if (ea->indirect) + hpfs_ea_remove(s, ea_sec(ea), ea->anode, ea_len(ea)); + hpfs_ea_ext_remove(s, fnode->ea_secno, fnode->ea_anode, fnode->ea_size_l); + brelse(bh); + hpfs_free_sectors(s, fno, 1); +} diff --git a/fs/hpfs/buffer.c b/fs/hpfs/buffer.c new file mode 100644 index 00000000000..2807aa833e6 --- /dev/null +++ b/fs/hpfs/buffer.c @@ -0,0 +1,175 @@ +/* + * linux/fs/hpfs/buffer.c + * + * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999 + * + * general buffer i/o + */ + +#include "hpfs_fn.h" + +void hpfs_lock_creation(struct super_block *s) +{ +#ifdef DEBUG_LOCKS + printk("lock creation\n"); +#endif + down(&hpfs_sb(s)->hpfs_creation_de); +} + +void hpfs_unlock_creation(struct super_block *s) +{ +#ifdef DEBUG_LOCKS + printk("unlock creation\n"); +#endif + up(&hpfs_sb(s)->hpfs_creation_de); +} + +/* Map a sector into a buffer and return pointers to it and to the buffer. */ + +void *hpfs_map_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp, + int ahead) +{ + struct buffer_head *bh; + + cond_resched(); + + *bhp = bh = sb_bread(s, secno); + if (bh != NULL) + return bh->b_data; + else { + printk("HPFS: hpfs_map_sector: read error\n"); + return NULL; + } +} + +/* Like hpfs_map_sector but don't read anything */ + +void *hpfs_get_sector(struct super_block *s, unsigned secno, struct buffer_head **bhp) +{ + struct buffer_head *bh; + /*return hpfs_map_sector(s, secno, bhp, 0);*/ + + cond_resched(); + + if ((*bhp = bh = sb_getblk(s, secno)) != NULL) { + if (!buffer_uptodate(bh)) wait_on_buffer(bh); + set_buffer_uptodate(bh); + return bh->b_data; + } else { + printk("HPFS: hpfs_get_sector: getblk failed\n"); + return NULL; + } +} + +/* Map 4 sectors into a 4buffer and return pointers to it and to the buffer. */ + +void *hpfs_map_4sectors(struct super_block *s, unsigned secno, struct quad_buffer_head *qbh, + int ahead) +{ + struct buffer_head *bh; + char *data; + + cond_resched(); + + if (secno & 3) { + printk("HPFS: hpfs_m |