⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dnode.c

📁 嵌入式系统设计与实例开发实验教材二源码 多线程应用程序设计 串行端口程序设计 AD接口实验 CAN总线通信实验 GPS通信实验 Linux内核移植与编译实验 IC卡读写实验 SD驱动使
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  linux/fs/hpfs/dnode.c * *  Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999 * *  handling directory dnode tree - adding, deleteing & searching for dirents */#include "hpfs_fn.h"static loff_t get_pos(struct dnode *d, struct hpfs_dirent *fde){	struct hpfs_dirent *de;	struct hpfs_dirent *de_end = dnode_end_de(d);	int i = 1;	for (de = dnode_first_de(d); de < de_end; de = de_next_de(de)) {		if (de == fde) return ((loff_t) d->self << 4) | (loff_t)i;		i++;	}	printk("HPFS: get_pos: not_found\n");	return ((loff_t)d->self << 4) | (loff_t)1;}void hpfs_add_pos(struct inode *inode, loff_t *pos){	int i = 0;	loff_t **ppos;	if (inode->i_hpfs_rddir_off)		for (; inode->i_hpfs_rddir_off[i]; i++)			if (inode->i_hpfs_rddir_off[i] == pos) return;	if (!(i&0x0f)) {		if (!(ppos = kmalloc((i+0x11) * sizeof(loff_t*), GFP_KERNEL))) {			printk("HPFS: out of memory for position list\n");			return;		}		if (inode->i_hpfs_rddir_off) {			memcpy(ppos, inode->i_hpfs_rddir_off, i * sizeof(loff_t));			kfree(inode->i_hpfs_rddir_off);		}		inode->i_hpfs_rddir_off = ppos;	}	inode->i_hpfs_rddir_off[i] = pos;	inode->i_hpfs_rddir_off[i + 1] = NULL;}void hpfs_del_pos(struct inode *inode, loff_t *pos){	loff_t **i, **j;	if (!inode->i_hpfs_rddir_off) goto not_f;	for (i = inode->i_hpfs_rddir_off; *i; i++) if (*i == pos) goto fnd;	goto not_f;	fnd:	for (j = i + 1; *j; j++) ;	*i = *(j - 1);	*(j - 1) = NULL;	if (j - 1 == inode->i_hpfs_rddir_off) {		kfree(inode->i_hpfs_rddir_off);		inode->i_hpfs_rddir_off = NULL;	}	return;	not_f:	/*printk("HPFS: warning: position pointer %p->%08x not found\n", pos, (int)*pos);*/	return;}static void for_all_poss(struct inode *inode, void (*f)(loff_t *, loff_t, loff_t),			 loff_t p1, loff_t p2){	loff_t **i;	if (!inode->i_hpfs_rddir_off) return;	for (i = inode->i_hpfs_rddir_off; *i; i++) (*f)(*i, p1, p2);	return;}void hpfs_pos_subst(loff_t *p, loff_t f, loff_t t){	if (*p == f) *p = t;}/*void hpfs_hpfs_pos_substd(loff_t *p, loff_t f, loff_t t){	if ((*p & ~0x3f) == (f & ~0x3f)) *p = (t & ~0x3f) | (*p & 0x3f);}*/void hpfs_pos_ins(loff_t *p, loff_t d, loff_t c){	if ((*p & ~0x3f) == (d & ~0x3f) && (*p & 0x3f) >= (d & 0x3f)) {		int n = (*p & 0x3f) + c;		if (n > 0x3f) printk("HPFS: hpfs_pos_ins: %08x + %d\n", (int)*p, (int)c >> 8);		else *p = (*p & ~0x3f) | n;	}}void hpfs_pos_del(loff_t *p, loff_t d, loff_t c){	if ((*p & ~0x3f) == (d & ~0x3f) && (*p & 0x3f) >= (d & 0x3f)) {		int n = (*p & 0x3f) - c;		if (n < 1) printk("HPFS: hpfs_pos_ins: %08x - %d\n", (int)*p, (int)c >> 8);		else *p = (*p & ~0x3f) | n;	}}static struct hpfs_dirent *dnode_pre_last_de(struct dnode *d){	struct hpfs_dirent *de, *de_end, *dee = NULL, *deee = NULL;	de_end = dnode_end_de(d);	for (de = dnode_first_de(d); de < de_end; de = de_next_de(de)) {		deee = dee; dee = de;	}		return deee;}static struct hpfs_dirent *dnode_last_de(struct dnode *d){	struct hpfs_dirent *de, *de_end, *dee = NULL;	de_end = dnode_end_de(d);	for (de = dnode_first_de(d); de < de_end; de = de_next_de(de)) {		dee = de;	}		return dee;}static void set_last_pointer(struct super_block *s, struct dnode *d, dnode_secno ptr){	struct hpfs_dirent *de;	if (!(de = dnode_last_de(d))) {		hpfs_error(s, "set_last_pointer: empty dnode %08x", d->self);		return;	}	if (s->s_hpfs_chk) {		if (de->down) {			hpfs_error(s, "set_last_pointer: dnode %08x has already last pointer %08x",				d->self, de_down_pointer(de));			return;		}		if (de->length != 32) {			hpfs_error(s, "set_last_pointer: bad last dirent in dnode %08x", d->self);			return;		}	}	if (ptr) {		if ((d->first_free += 4) > 2048) {			hpfs_error(s,"set_last_pointer: too long dnode %08x", d->self);			d->first_free -= 4;			return;		}		de->length = 36;		de->down = 1;		*(dnode_secno *)((char *)de + 32) = ptr;	}}/* Add an entry to dnode and don't care if it grows over 2048 bytes */struct hpfs_dirent *hpfs_add_de(struct super_block *s, struct dnode *d, unsigned char *name,				unsigned namelen, secno down_ptr){	struct hpfs_dirent *de;	struct hpfs_dirent *de_end = dnode_end_de(d);	unsigned d_size = de_size(namelen, down_ptr);	for (de = dnode_first_de(d); de < de_end; de = de_next_de(de)) {		int c = hpfs_compare_names(s, name, namelen, de->name, de->namelen, de->last);		if (!c) {			hpfs_error(s, "name (%c,%d) already exists in dnode %08x", *name, namelen, d->self);			return NULL;		}		if (c < 0) break;	}	memmove((char *)de + d_size, de, (char *)de_end - (char *)de);	memset(de, 0, d_size);	if (down_ptr) {		*(int *)((char *)de + d_size - 4) = down_ptr;		de->down = 1;	}	de->length = d_size;	if (down_ptr) de->down = 1;	de->not_8x3 = hpfs_is_name_long(name, namelen);	de->namelen = namelen;	memcpy(de->name, name, namelen);	d->first_free += d_size;	return de;}/* Delete dirent and don't care about it's subtree */void hpfs_delete_de(struct super_block *s, struct dnode *d, struct hpfs_dirent *de){	if (de->last) {		hpfs_error(s, "attempt to delete last dirent in dnode %08x", d->self);		return;	}	d->first_free -= de->length;	memmove(de, de_next_de(de), d->first_free + (char *)d - (char *)de);}static void fix_up_ptrs(struct super_block *s, struct dnode *d){	struct hpfs_dirent *de;	struct hpfs_dirent *de_end = dnode_end_de(d);	dnode_secno dno = d->self;	for (de = dnode_first_de(d); de < de_end; de = de_next_de(de))		if (de->down) {			struct quad_buffer_head qbh;			struct dnode *dd;			if ((dd = hpfs_map_dnode(s, de_down_pointer(de), &qbh))) {				if (dd->up != dno || dd->root_dnode) {					dd->up = dno;					dd->root_dnode = 0;					hpfs_mark_4buffers_dirty(&qbh);				}				hpfs_brelse4(&qbh);			}		}}/* Add an entry to dnode and do dnode splitting if required */int hpfs_add_to_dnode(struct inode *i, dnode_secno dno, unsigned char *name, unsigned namelen,		      struct hpfs_dirent *new_de, dnode_secno down_ptr){	struct quad_buffer_head qbh, qbh1, qbh2;	struct dnode *d, *ad, *rd, *nd = NULL;	dnode_secno adno, rdno;	struct hpfs_dirent *de;	struct hpfs_dirent nde;	char *nname;	int h;	int pos;	struct buffer_head *bh;	struct fnode *fnode;	int c1, c2 = 0;	if (!(nname = kmalloc(256, GFP_KERNEL))) {		printk("HPFS: out of memory, can't add to dnode\n");		return 1;	}	go_up:	if (namelen >= 256) {		hpfs_error(i->i_sb, "hpfs_add_to_dnode: namelen == %d", namelen);		if (nd) kfree(nd);		kfree(nname);		return 1;	}	if (!(d = hpfs_map_dnode(i->i_sb, dno, &qbh))) {		if (nd) kfree(nd);		kfree(nname);		return 1;	}	go_up_a:	if (i->i_sb->s_hpfs_chk)		if (hpfs_stop_cycles(i->i_sb, dno, &c1, &c2, "hpfs_add_to_dnode")) {			hpfs_brelse4(&qbh);			if (nd) kfree(nd);			kfree(nname);			return 1;		}	if (d->first_free + de_size(namelen, down_ptr) <= 2048) {		loff_t t;		copy_de(de=hpfs_add_de(i->i_sb, d, name, namelen, down_ptr), new_de);		t = get_pos(d, de);		for_all_poss(i, hpfs_pos_ins, t, 1);		for_all_poss(i, hpfs_pos_subst, 4, t);		for_all_poss(i, hpfs_pos_subst, 5, t + 1);		hpfs_mark_4buffers_dirty(&qbh);		hpfs_brelse4(&qbh);		if (nd) kfree(nd);		kfree(nname);		return 0;	}	if (!nd) if (!(nd = kmalloc(0x924, GFP_KERNEL))) {		/* 0x924 is a max size of dnode after adding a dirent with		   max name length. We alloc this only once. There must		   not be any error while splitting dnodes, otherwise the		   whole directory, not only file we're adding, would		   be lost. */		printk("HPFS: out of memory for dnode splitting\n");		hpfs_brelse4(&qbh);		kfree(nname);		return 1;	}		memcpy(nd, d, d->first_free);	copy_de(de = hpfs_add_de(i->i_sb, nd, name, namelen, down_ptr), new_de);	for_all_poss(i, hpfs_pos_ins, get_pos(nd, de), 1);	h = ((char *)dnode_last_de(nd) - (char *)nd) / 2 + 10;	if (!(ad = hpfs_alloc_dnode(i->i_sb, d->up, &adno, &qbh1, 0))) {		hpfs_error(i->i_sb, "unable to alloc dnode - dnode tree will be corrupted");		hpfs_brelse4(&qbh);		kfree(nd);		kfree(nname);		return 1;	}	i->i_size += 2048;	i->i_blocks += 4;	pos = 1;	for (de = dnode_first_de(nd); (char *)de_next_de(de) - (char *)nd < h; de = de_next_de(de)) {		copy_de(hpfs_add_de(i->i_sb, ad, de->name, de->namelen, de->down ? de_down_pointer(de) : 0), de);		for_all_poss(i, hpfs_pos_subst, ((loff_t)dno << 4) | pos, ((loff_t)adno << 4) | pos);		pos++;	}	copy_de(new_de = &nde, de);	memcpy(name = nname, de->name, namelen = de->namelen);	for_all_poss(i, hpfs_pos_subst, ((loff_t)dno << 4) | pos, 4);	down_ptr = adno;	set_last_pointer(i->i_sb, ad, de->down ? de_down_pointer(de) : 0);	de = de_next_de(de);	memmove((char *)nd + 20, de, nd->first_free + (char *)nd - (char *)de);	nd->first_free -= (char *)de - (char *)nd - 20;	memcpy(d, nd, nd->first_free);	for_all_poss(i, hpfs_pos_del, (loff_t)dno << 4, pos);	fix_up_ptrs(i->i_sb, ad);	if (!d->root_dnode) {		dno = ad->up = d->up;		hpfs_mark_4buffers_dirty(&qbh);		hpfs_brelse4(&qbh);		hpfs_mark_4buffers_dirty(&qbh1);		hpfs_brelse4(&qbh1);		goto go_up;	}	if (!(rd = hpfs_alloc_dnode(i->i_sb, d->up, &rdno, &qbh2, 0))) {		hpfs_error(i->i_sb, "unable to alloc dnode - dnode tree will be corrupted");		hpfs_brelse4(&qbh);		hpfs_brelse4(&qbh1);		kfree(nd);		kfree(nname);		return 1;	}	i->i_size += 2048;	i->i_blocks += 4;	rd->root_dnode = 1;	rd->up = d->up;	if (!(fnode = hpfs_map_fnode(i->i_sb, d->up, &bh))) {		hpfs_free_dnode(i->i_sb, rdno);		hpfs_brelse4(&qbh);		hpfs_brelse4(&qbh1);		hpfs_brelse4(&qbh2);		kfree(nd);		kfree(nname);		return 1;	}	fnode->u.external[0].disk_secno = rdno;	mark_buffer_dirty(bh);	brelse(bh);	d->up = ad->up = i->i_hpfs_dno = rdno;	d->root_dnode = ad->root_dnode = 0;	hpfs_mark_4buffers_dirty(&qbh);	hpfs_brelse4(&qbh);	hpfs_mark_4buffers_dirty(&qbh1);	hpfs_brelse4(&qbh1);	qbh = qbh2;	set_last_pointer(i->i_sb, rd, dno);	dno = rdno;	d = rd;	goto go_up_a;}/* * Add an entry to directory btree. * I hate such crazy directory structure. * It's easy to read but terrible to write. * I wrote this directory code 4 times. * I hope, now it's finally bug-free. */int hpfs_add_dirent(struct inode *i, unsigned char *name, unsigned namelen,		    struct hpfs_dirent *new_de, int cdepth){	struct dnode *d;	struct hpfs_dirent *de, *de_end;	struct quad_buffer_head qbh;	dnode_secno dno;	int c;	int c1, c2 = 0;	dno = i->i_hpfs_dno;	down:	if (i->i_sb->s_hpfs_chk)		if (hpfs_stop_cycles(i->i_sb, dno, &c1, &c2, "hpfs_add_dirent")) return 1;	if (!(d = hpfs_map_dnode(i->i_sb, dno, &qbh))) return 1;	de_end = dnode_end_de(d);	for (de = dnode_first_de(d); de < de_end; de = de_next_de(de)) {		if (!(c = hpfs_compare_names(i->i_sb, name, namelen, de->name, de->namelen, de->last))) {			hpfs_brelse4(&qbh);			return -1;		}			if (c < 0) {			if (de->down) {				dno = de_down_pointer(de);				hpfs_brelse4(&qbh);				goto down;			}			break;		}	}	hpfs_brelse4(&qbh);	if (!cdepth) hpfs_lock_creation(i->i_sb);	if (hpfs_check_free_dnodes(i->i_sb, FREE_DNODES_ADD)) {		c = 1;		goto ret;	}		i->i_version = ++event;	c = hpfs_add_to_dnode(i, dno, name, namelen, new_de, 0);	ret:	if (!cdepth) hpfs_unlock_creation(i->i_sb);	return c;}/*  * Find dirent with higher name in 'from' subtree and move it to 'to' dnode. * Return the dnode we moved from (to be checked later if it's empty) */static secno move_to_top(struct inode *i, dnode_secno from, dnode_secno to){	dnode_secno dno, ddno;	dnode_secno chk_up = to;	struct dnode *dnode;	struct quad_buffer_head qbh;	struct hpfs_dirent *de, *nde;	int a;	loff_t t;	int c1, c2 = 0;	dno = from;	while (1) {		if (i->i_sb->s_hpfs_chk)			if (hpfs_stop_cycles(i->i_sb, dno, &c1, &c2, "move_to_top"))				return 0;		if (!(dnode = hpfs_map_dnode(i->i_sb, dno, &qbh))) return 0;		if (i->i_sb->s_hpfs_chk) {			if (dnode->up != chk_up) {				hpfs_error(i->i_sb, "move_to_top: up pointer from %08x should be %08x, is %08x",					dno, chk_up, dnode->up);				hpfs_brelse4(&qbh);				return 0;			}			chk_up = dno;		}		if (!(de = dnode_last_de(dnode))) {			hpfs_error(i->i_sb, "move_to_top: dnode %08x has no last de", dno);			hpfs_brelse4(&qbh);			return 0;		}		if (!de->down) break;		dno = de_down_pointer(de);		hpfs_brelse4(&qbh);	}	while (!(de = dnode_pre_last_de(dnode))) {		dnode_secno up = dnode->up;		hpfs_brelse4(&qbh);		hpfs_free_dnode(i->i_sb, dno);		i->i_size -= 2048;		i->i_blocks -= 4;		for_all_poss(i, hpfs_pos_subst, ((loff_t)dno << 4) | 1, 5);		if (up == to) return to;		if (!(dnode = hpfs_map_dnode(i->i_sb, up, &qbh))) return 0;		if (dnode->root_dnode) {			hpfs_error(i->i_sb, "move_to_top: got to root_dnode while moving from %08x to %08x", from, to);			hpfs_brelse4(&qbh);			return 0;		}		de = dnode_last_de(dnode);		if (!de || !de->down) {			hpfs_error(i->i_sb, "move_to_top: dnode %08x doesn't point down to %08x", up, dno);			hpfs_brelse4(&qbh);			return 0;		}		dnode->first_free -= 4;		de->length -= 4;		de->down = 0;		hpfs_mark_4buffers_dirty(&qbh);		dno = up;	}	t = get_pos(dnode, de);	for_all_poss(i, hpfs_pos_subst, t, 4);	for_all_poss(i, hpfs_pos_subst, t + 1, 5);	if (!(nde = kmalloc(de->length, GFP_KERNEL))) {		hpfs_error(i->i_sb, "out of memory for dirent - directory will be corrupted");		hpfs_brelse4(&qbh);		return 0;	}	memcpy(nde, de, de->length);	ddno = de->down ? de_down_pointer(de) : 0;	hpfs_delete_de(i->i_sb, dnode, de);	set_last_pointer(i->i_sb, dnode, ddno);	hpfs_mark_4buffers_dirty(&qbh);	hpfs_brelse4(&qbh);	a = hpfs_add_to_dnode(i, to, nde->name, nde->namelen, nde, from);	kfree(nde);	if (a) return 0;	return dno;}/*  * Check if a dnode is empty and delete it from the tree * (chkdsk doesn't like empty dnodes) */static void delete_empty_dnode(struct inode *i, dnode_secno dno){	struct quad_buffer_head qbh;	struct dnode *dnode;	dnode_secno down, up, ndown;	int p;	struct hpfs_dirent *de;	int c1, c2 = 0;	try_it_again:	if (hpfs_stop_cycles(i->i_sb, dno, &c1, &c2, "delete_empty_dnode")) return;	if (!(dnode = hpfs_map_dnode(i->i_sb, dno, &qbh))) return;	if (dnode->first_free > 56) goto end;	if (dnode->first_free == 52 || dnode->first_free == 56) {		struct hpfs_dirent *de_end;		int root = dnode->root_dnode;		up = dnode->up;		de = dnode_first_de(dnode);		down = de->down ? de_down_pointer(de) : 0;		if (i->i_sb->s_hpfs_chk) if (root && !down) {			hpfs_error(i->i_sb, "delete_empty_dnode: root dnode %08x is empty", dno);			goto end;		}		hpfs_brelse4(&qbh);		hpfs_free_dnode(i->i_sb, dno);		i->i_size -= 2048;		i->i_blocks -= 4;		if (root) {			struct fnode *fnode;			struct buffer_head *bh;			struct dnode *d1;			struct quad_buffer_head qbh1;			if (i->i_sb->s_hpfs_chk) if (up != i->i_ino) {				hpfs_error(i->i_sb, "bad pointer to fnode, dnode %08x, pointing to %08x, should be %08x", dno, up, i->i_ino);				return;			}			if ((d1 = hpfs_map_dnode(i->i_sb, down, &qbh1))) {				d1->up = up;				d1->root_dnode = 1;				hpfs_mark_4buffers_dirty(&qbh1);				hpfs_brelse4(&qbh1);			}			if ((fnode = hpfs_map_fnode(i->i_sb, up, &bh))) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -