1/*-
2 * Copyright 2000 Hans Reiser
3 * See README for licensing and copyright details
4 *
5 * Ported to FreeBSD by Jean-Sébastien Pédron <dumbbell@FreeBSD.org>
6 *
7 * $FreeBSD$
8 */
9
10#include <gnu/fs/reiserfs/reiserfs_fs.h>
11
12static b_strategy_t reiserfs_bufstrategy;
13
14/*
15 * Buffer operations for ReiserFS vnodes.
16 * We punt on VOP_BMAP, so we need to do strategy on the file's vnode
17 * rather than the underlying device's.
18 */
19static struct buf_ops reiserfs_vnbufops = {
20	.bop_name	= "ReiserFS",
21	.bop_strategy	= reiserfs_bufstrategy,
22};
23
24/* Default io size devuned in super.c */
25extern int reiserfs_default_io_size;
26void inode_set_bytes(struct reiserfs_node *ip, off_t bytes);
27
28/* Args for the create parameter of reiserfs_get_block */
29#define GET_BLOCK_NO_CREATE	0  /* Don't create new blocks or convert
30				      tails */
31#define GET_BLOCK_CREATE	1  /* Add anything you need to find block */
32#define GET_BLOCK_NO_HOLE	2  /* Return ENOENT for file holes */
33#define GET_BLOCK_READ_DIRECT	4  /* Read the tail if indirect item not
34				      found */
35#define GET_BLOCK_NO_ISEM	8  /* i_sem is not held, don't preallocate */
36#define GET_BLOCK_NO_DANGLE	16 /* Don't leave any transactions running */
37
38/* -------------------------------------------------------------------
39 * vnode operations
40 * -------------------------------------------------------------------*/
41
42int
43reiserfs_read(struct vop_read_args *ap)
44{
45	struct uio *uio;
46	struct vnode *vp;
47	struct reiserfs_node *ip;
48	struct reiserfs_sb_info *sbi;
49
50	int error;
51	long size;
52	daddr_t lbn;
53	off_t bytesinfile, offset;
54
55	uio = ap->a_uio;
56	vp  = ap->a_vp;
57	ip  = VTOI(vp);
58	sbi = ip->i_reiserfs;
59
60	size = sbi->s_blocksize;
61
62	for (error = 0; uio->uio_resid > 0;) {
63		if ((bytesinfile = ip->i_size - uio->uio_offset) <= 0)
64			break;
65
66		/* Compute the logical block number and its offset */
67		lbn    = uio->uio_offset / size;
68		offset = uio->uio_offset % size;
69		reiserfs_log(LOG_DEBUG, "logical block number: %ju\n",
70		    (intmax_t)lbn);
71		reiserfs_log(LOG_DEBUG, "block offset:         %ju\n",
72		    (intmax_t)offset);
73
74		/* Read file blocks */
75		reiserfs_log(LOG_DEBUG, "reiserfs_get_block(%ju)\n",
76		    (intmax_t)lbn);
77		if ((error = reiserfs_get_block(ip, lbn, offset, uio)) != 0) {
78			reiserfs_log(LOG_DEBUG,
79			    "reiserfs_get_block returned the error %d\n",
80			    error);
81			break;
82		}
83	}
84
85	return (error);
86}
87
88static void
89reiserfs_bufstrategy(struct bufobj *bo, struct buf *bp)
90{
91	struct vnode *vp;
92	int rc;
93
94	vp = bo->bo_private;
95	KASSERT(bo == &vp->v_bufobj, ("BO/VP mismatch: vp %p bo %p != %p",
96	    vp, &vp->v_bufobj, bo));
97	rc = VOP_STRATEGY(vp, bp);
98	KASSERT(rc == 0, ("ReiserFS VOP_STRATEGY failed: bp=%p, "
99	    "vp=%p, rc=%d", bp, vp, rc));
100}
101
102int
103reiserfs_inactive(struct vop_inactive_args *ap)
104{
105	int error;
106	struct vnode *vp;
107	struct reiserfs_node *ip;
108
109	error = 0;
110	vp = ap->a_vp;
111	ip = VTOI(vp);
112
113	reiserfs_log(LOG_DEBUG, "deactivating inode used %d times\n",
114	    vp->v_usecount);
115
116#if 0
117	/* Ignore inodes related to stale file handles. */
118	if (ip->i_mode == 0)
119		goto out;
120
121out:
122#endif
123
124	/*
125	 * If we are done with the inode, reclaim it so that it can be reused
126	 * immediately.
127	 */
128	if (ip->i_mode == 0) {
129		reiserfs_log(LOG_DEBUG, "recyling\n");
130		vrecycle(vp);
131	}
132
133	return (error);
134}
135
136int
137reiserfs_reclaim(struct vop_reclaim_args *ap)
138{
139	struct reiserfs_node *ip;
140	struct vnode *vp;
141
142	vp = ap->a_vp;
143
144	reiserfs_log(LOG_DEBUG, "reclaiming inode used %d times\n",
145	    vp->v_usecount);
146	ip = VTOI(vp);
147
148	/* XXX Update this node (write to the disk) */
149
150	/* Remove the inode from its hash chain. */
151	vfs_hash_remove(vp);
152
153	reiserfs_log(LOG_DEBUG, "free private data\n");
154	free(vp->v_data, M_REISERFSNODE);
155	vp->v_data = NULL;
156	vnode_destroy_vobject(vp);
157
158	return (0);
159}
160
161/* -------------------------------------------------------------------
162 * Functions from linux/fs/reiserfs/inode.c
163 * -------------------------------------------------------------------*/
164
165static void
166_make_cpu_key(struct cpu_key *key, int version,
167    uint32_t dirid, uint32_t objectid, off_t offset, int type, int length)
168{
169
170	key->version = version;
171
172	key->on_disk_key.k_dir_id   = dirid;
173	key->on_disk_key.k_objectid = objectid;
174	set_cpu_key_k_offset(key, offset);
175	set_cpu_key_k_type(key, type);
176	key->key_length = length;
177}
178
179/*
180 * Take base of inode_key (it comes from inode always) (dirid, objectid)
181 * and version from an inode, set offset and type of key
182 */
183void
184make_cpu_key(struct cpu_key *key, struct reiserfs_node *ip, off_t offset,
185    int type, int length)
186{
187
188	_make_cpu_key(key, get_inode_item_key_version(ip),
189	    le32toh(INODE_PKEY(ip)->k_dir_id),
190	    le32toh(INODE_PKEY(ip)->k_objectid),
191	    offset, type, length);
192}
193
194int
195reiserfs_get_block(struct reiserfs_node *ip, long block, off_t offset,
196    struct uio *uio)
197{
198	caddr_t blk = NULL, p;
199	struct cpu_key key;
200	/* unsigned long offset; */
201	INITIALIZE_PATH(path);
202	struct buf *bp, *blk_bp;
203	struct item_head *ih;
204	struct reiserfs_sb_info *sbi;
205	int blocknr, chars, done = 0, ret = 0, args = 0;
206
207	sbi = ip->i_reiserfs;
208
209	/* Prepare the key to look for the 'block'-th block of file */
210	reiserfs_log(LOG_DEBUG, "prepare cpu key\n");
211	make_cpu_key(&key, ip, (off_t)block * sbi->s_blocksize + 1, TYPE_ANY, 3);
212
213	/* research: */
214	reiserfs_log(LOG_DEBUG, "search for position\n");
215	if (search_for_position_by_key(sbi, &key, &path) != POSITION_FOUND) {
216		reiserfs_log(LOG_DEBUG, "position not found\n");
217		pathrelse(&path);
218#if 0
219		if (blk)
220			kunmap(bh_result->b_page);
221#endif
222		/*
223		 * We do not return ENOENT if there is a hole but page is
224		 * uptodate, because it means that there is some MMAPED data
225		 * associated with it that is yet to be written to disk.
226		 */
227		if ((args & GET_BLOCK_NO_HOLE)/* &&
228		    !PageUptodate(bh_result->b_page)*/)
229			return (ENOENT);
230		return (0);
231	}
232	reiserfs_log(LOG_DEBUG, "position found\n");
233
234	bp = get_last_bp(&path);
235	ih = get_ih(&path);
236
237	if (is_indirect_le_ih(ih)) {
238		off_t xfersize;
239		uint32_t *ind_item = (uint32_t *)B_I_PITEM(bp, ih);
240
241		reiserfs_log(LOG_DEBUG, "item is INDIRECT\n");
242
243		blocknr = get_block_num(ind_item, path.pos_in_item);
244		reiserfs_log(LOG_DEBUG, "block number: %d "
245		    "(ind_item=%p, pos_in_item=%u)\n",
246		    blocknr, ind_item, path.pos_in_item);
247
248		xfersize = MIN(sbi->s_blocksize - offset,
249		    ip->i_size - uio->uio_offset);
250		xfersize = MIN(xfersize, uio->uio_resid);
251
252		if (blocknr) {
253			ret = bread(sbi->s_devvp,
254			    blocknr * btodb(sbi->s_blocksize),
255			    sbi->s_blocksize, NOCRED, &blk_bp);
256			reiserfs_log(LOG_DEBUG, "xfersize: %ju\n",
257			    (intmax_t)xfersize);
258			ret = uiomove(blk_bp->b_data + offset, xfersize, uio);
259			brelse(blk_bp);
260		} else {
261			/*
262			 * We do not return ENOENT if there is a hole but
263			 * page is uptodate, because it means That there
264			 * is some MMAPED data associated with it that
265			 * is yet to be written to disk.
266			 */
267			if ((args & GET_BLOCK_NO_HOLE)/* &&
268			    !PageUptodate(bh_result->b_page)*/)
269				ret = (ENOENT);
270
271			/* Skip this hole */
272			uio->uio_resid  -= xfersize;
273			uio->uio_offset += xfersize;
274		}
275
276		pathrelse(&path);
277		return (ret);
278	}
279
280	reiserfs_log(LOG_DEBUG, "item should be DIRECT\n");
281
282#if 0
283	/* Requested data are in direct item(s) */
284	if (!(args & GET_BLOCK_READ_DIRECT)) {
285		/*
286		 * We are called by bmap. FIXME: we can not map block of
287		 * file when it is stored in direct item(s)
288		 */
289		pathrelse(&path);
290#if 0
291		if (blk)
292			kunmap(bh_result->b_page);
293#endif
294		return (ENOENT);
295	}
296#endif
297
298#if 0
299	/*
300	 * If we've got a direct item, and the buffer or page was uptodate, we
301	 * don't want to pull data off disk again. Skip to the end, where we
302	 * map the buffer and return
303	 */
304	if (buffer_uptodate(bh_result)) {
305		goto finished;
306	} else
307		/*
308		 * grab_tail_page can trigger calls to reiserfs_get_block
309		 * on up to date pages without any buffers. If the page
310		 * is up to date, we don't want read old data off disk.
311		 * Set the up to date bit on the buffer instead and jump
312		 * to the end
313		 */
314		if (!bh_result->b_page || PageUptodate(bh_result->b_page)) {
315			set_buffer_uptodate(bh_result);
316			goto finished;
317		}
318#endif
319
320#if 0
321	/* Read file tail into part of page */
322	offset = (cpu_key_k_offset(&key) - 1) & (PAGE_CACHE_SIZE - 1);
323	fs_gen = get_generation(ip->i_reiserfs);
324	copy_item_head(&tmp_ih, ih);
325#endif
326
327#if 0
328	/*
329	 * We only want to kmap if we are reading the tail into the page. this
330	 * is not the common case, so we don't kmap until we are sure we need
331	 * to. But, this means the item might move if kmap schedules
332	 */
333	if (!blk) {
334		blk = (char *)kmap(bh_result->b_page);
335		if (fs_changed (fs_gen, sbi) && item_moved(&tmp_ih, &path))
336			goto research;
337	}
338	blk += offset;
339	memset(blk, 0, sbi->s_blocksize);
340#endif
341	if (!blk) {
342		reiserfs_log(LOG_DEBUG, "allocating buffer\n");
343		blk = malloc(ip->i_size, M_REISERFSNODE, M_WAITOK | M_ZERO);
344		if (!blk)
345			return (ENOMEM);
346	}
347	/* p += offset; */
348
349	p = blk;
350	do {
351		if (!is_direct_le_ih(ih)) {
352			reiserfs_log(LOG_ERR, "BUG\n");
353			return (ENOENT); /* XXX Wrong error code */
354		}
355
356		/*
357		 * Make sure we don't read more bytes than actually exist
358		 * in the file. This can happen in odd cases where i_size
359		 * isn't correct, and when direct item padding results in
360		 * a few extra bytes at the end of the direct item
361		 */
362		if ((le_ih_k_offset(ih) + path.pos_in_item) > ip->i_size)
363			break;
364
365		if ((le_ih_k_offset(ih) - 1 + ih_item_len(ih)) > ip->i_size) {
366			chars = ip->i_size - (le_ih_k_offset(ih) - 1) -
367			    path.pos_in_item;
368			done  = 1;
369		} else {
370			chars = ih_item_len(ih) - path.pos_in_item;
371		}
372		reiserfs_log(LOG_DEBUG, "copying %d bytes\n", chars);
373		memcpy(p, B_I_PITEM(bp, ih) + path.pos_in_item, chars);
374		if (done) {
375			reiserfs_log(LOG_DEBUG, "copy done\n");
376			break;
377		}
378
379		p += chars;
380
381		if (PATH_LAST_POSITION(&path) != (B_NR_ITEMS(bp) - 1))
382			/*
383			 * We done, if read direct item is not the last
384			 * item of node
385			 * FIXME: we could try to check right delimiting
386			 * key to see whether direct item continues in
387			 * the right neighbor or rely on i_size
388			 */
389			break;
390
391		/* Update key to look for the next piece */
392		set_cpu_key_k_offset(&key, cpu_key_k_offset(&key) + chars);
393		if (search_for_position_by_key(sbi, &key, &path) !=
394		    POSITION_FOUND)
395			/*
396			 * We read something from tail, even if now we got
397			 * IO_ERROR
398			 */
399			break;
400
401		bp = get_last_bp(&path);
402		ih = get_ih(&path);
403	} while (1);
404
405	/* finished: */
406	pathrelse(&path);
407	/*
408	 * This buffer has valid data, but isn't valid for io. mapping it to
409	 * block #0 tells the rest of reiserfs it just has a tail in it
410	 */
411	ret = uiomove(blk, ip->i_size, uio);
412	free(blk, M_REISERFSNODE);
413	return (ret);
414}
415
416/*
417 * Compute real number of used bytes by file
418 * Following three functions can go away when we'll have enough space in
419 * stat item
420 */
421static int
422real_space_diff(struct reiserfs_node *ip, int sd_size)
423{
424	int bytes;
425	off_t blocksize = ip->i_reiserfs->s_blocksize;
426
427	if (S_ISLNK(ip->i_mode) || S_ISDIR(ip->i_mode))
428		return (sd_size);
429
430	/* End of file is also in full block with indirect reference, so round
431	 * up to the next block.
432	 *
433	 * There is just no way to know if the tail is actually packed on the
434	 * file, so we have to assume it isn't. When we pack the tail, we add
435	 * 4 bytes to pretend there really is an unformatted node pointer. */
436	bytes = ((ip->i_size + (blocksize - 1)) >>
437	    ip->i_reiserfs->s_blocksize_bits) * UNFM_P_SIZE + sd_size;
438
439	return (bytes);
440}
441
442static inline off_t
443to_real_used_space(struct reiserfs_node *ip, unsigned long blocks, int sd_size)
444{
445
446	if (S_ISLNK(ip->i_mode) || S_ISDIR(ip->i_mode)) {
447		return ip->i_size + (off_t)(real_space_diff(ip, sd_size));
448	}
449
450	return ((off_t)real_space_diff(ip, sd_size)) + (((off_t)blocks) << 9);
451}
452
453void
454inode_set_bytes(struct reiserfs_node *ip, off_t bytes)
455{
456
457	ip->i_blocks = bytes >> 9;
458	ip->i_bytes  = bytes & 511;
459}
460
461/* Called by read_locked_inode */
462static void
463init_inode(struct reiserfs_node *ip, struct path *path)
464{
465	struct buf *bp;
466	struct item_head *ih;
467	uint32_t rdev;
468
469	bp = PATH_PLAST_BUFFER(path);
470	ih = PATH_PITEM_HEAD(path);
471
472	reiserfs_log(LOG_DEBUG, "copy the key (objectid=%d, dirid=%d)\n",
473	    ih->ih_key.k_objectid, ih->ih_key.k_dir_id);
474	copy_key(INODE_PKEY(ip), &(ih->ih_key));
475	/* ip->i_blksize = reiserfs_default_io_size; */
476
477	reiserfs_log(LOG_DEBUG, "reset some inode structure members\n");
478	REISERFS_I(ip)->i_flags = 0;
479#if 0
480	REISERFS_I(ip)->i_prealloc_block = 0;
481	REISERFS_I(ip)->i_prealloc_count = 0;
482	REISERFS_I(ip)->i_trans_id = 0;
483	REISERFS_I(ip)->i_jl = NULL;
484	REISERFS_I(ip)->i_acl_access = NULL;
485	REISERFS_I(ip)->i_acl_default = NULL;
486#endif
487
488	if (stat_data_v1(ih)) {
489		reiserfs_log(LOG_DEBUG, "reiserfs/init_inode: stat data v1\n");
490		struct stat_data_v1 *sd;
491		unsigned long blocks;
492
493		sd = (struct stat_data_v1 *)B_I_PITEM(bp, ih);
494
495		reiserfs_log(LOG_DEBUG,
496		    "reiserfs/init_inode: filling more members\n");
497		set_inode_item_key_version(ip, KEY_FORMAT_3_5);
498		set_inode_sd_version(ip, STAT_DATA_V1);
499		ip->i_mode          = sd_v1_mode(sd);
500		ip->i_nlink         = sd_v1_nlink(sd);
501		ip->i_uid           = sd_v1_uid(sd);
502		ip->i_gid           = sd_v1_gid(sd);
503		ip->i_size          = sd_v1_size(sd);
504		ip->i_atime.tv_sec  = sd_v1_atime(sd);
505		ip->i_mtime.tv_sec  = sd_v1_mtime(sd);
506		ip->i_ctime.tv_sec  = sd_v1_ctime(sd);
507		ip->i_atime.tv_nsec = 0;
508		ip->i_ctime.tv_nsec = 0;
509		ip->i_mtime.tv_nsec = 0;
510
511		reiserfs_log(LOG_DEBUG, "  mode  = %08x\n", ip->i_mode);
512		reiserfs_log(LOG_DEBUG, "  nlink = %d\n", ip->i_nlink);
513		reiserfs_log(LOG_DEBUG, "  owner = %d:%d\n", ip->i_uid,
514		    ip->i_gid);
515		reiserfs_log(LOG_DEBUG, "  size  = %ju\n",
516		    (intmax_t)ip->i_size);
517		reiserfs_log(LOG_DEBUG, "  atime = %jd\n",
518		    (intmax_t)ip->i_atime.tv_sec);
519		reiserfs_log(LOG_DEBUG, "  mtime = %jd\n",
520		    (intmax_t)ip->i_mtime.tv_sec);
521		reiserfs_log(LOG_DEBUG, "  ctime = %jd\n",
522		    (intmax_t)ip->i_ctime.tv_sec);
523
524		ip->i_blocks     = sd_v1_blocks(sd);
525		ip->i_generation = le32toh(INODE_PKEY(ip)->k_dir_id);
526		blocks = (ip->i_size + 511) >> 9;
527		blocks = _ROUND_UP(blocks, ip->i_reiserfs->s_blocksize >> 9);
528		if (ip->i_blocks > blocks) {
529			/*
530			 * There was a bug in <= 3.5.23 when i_blocks could
531			 * take negative values. Starting from 3.5.17 this
532			 * value could even be stored in stat data. For such
533			 * files we set i_blocks based on file size. Just 2
534			 * notes: this can be wrong for sparce files. On-disk
535			 * value will be only updated if file's inode will
536			 * ever change.
537			 */
538			ip->i_blocks = blocks;
539		}
540
541		rdev = sd_v1_rdev(sd);
542		REISERFS_I(ip)->i_first_direct_byte =
543		    sd_v1_first_direct_byte(sd);
544
545		/*
546		 * An early bug in the quota code can give us an odd number
547		 * for the block count. This is incorrect, fix it here.
548		 */
549		if (ip->i_blocks & 1) {
550			ip->i_blocks++ ;
551		}
552		inode_set_bytes(ip, to_real_used_space(ip, ip->i_blocks,
553		    SD_V1_SIZE));
554
555		/*
556		 * nopack is initially zero for v1 objects. For v2 objects,
557		 * nopack is initialised from sd_attrs
558		 */
559		REISERFS_I(ip)->i_flags &= ~i_nopack_mask;
560		reiserfs_log(LOG_DEBUG, "...done\n");
561	} else {
562		reiserfs_log(LOG_DEBUG, "stat data v2\n");
563		/*
564		 * New stat data found, but object may have old items
565		 * (directories and symlinks)
566		 */
567		struct stat_data *sd = (struct stat_data *)B_I_PITEM(bp, ih);
568
569		reiserfs_log(LOG_DEBUG, "filling more members\n");
570		ip->i_mode          = sd_v2_mode(sd);
571		ip->i_nlink         = sd_v2_nlink(sd);
572		ip->i_uid           = sd_v2_uid(sd);
573		ip->i_size          = sd_v2_size(sd);
574		ip->i_gid           = sd_v2_gid(sd);
575		ip->i_mtime.tv_sec  = sd_v2_mtime(sd);
576		ip->i_atime.tv_sec  = sd_v2_atime(sd);
577		ip->i_ctime.tv_sec  = sd_v2_ctime(sd);
578		ip->i_ctime.tv_nsec = 0;
579		ip->i_mtime.tv_nsec = 0;
580		ip->i_atime.tv_nsec = 0;
581
582		reiserfs_log(LOG_DEBUG, "  mode  = %08x\n", ip->i_mode);
583		reiserfs_log(LOG_DEBUG, "  nlink = %d\n", ip->i_nlink);
584		reiserfs_log(LOG_DEBUG, "  owner = %d:%d\n", ip->i_uid,
585		    ip->i_gid);
586		reiserfs_log(LOG_DEBUG, "  size  = %ju\n",
587		    (intmax_t)ip->i_size);
588		reiserfs_log(LOG_DEBUG, "  atime = %jd\n",
589		    (intmax_t)ip->i_atime.tv_sec);
590		reiserfs_log(LOG_DEBUG, "  mtime = %jd\n",
591		    (intmax_t)ip->i_mtime.tv_sec);
592		reiserfs_log(LOG_DEBUG, "  ctime = %jd\n",
593		    (intmax_t)ip->i_ctime.tv_sec);
594
595		ip->i_blocks = sd_v2_blocks(sd);
596		rdev         = sd_v2_rdev(sd);
597		reiserfs_log(LOG_DEBUG, "  blocks = %u\n", ip->i_blocks);
598
599		if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
600			ip->i_generation = le32toh(INODE_PKEY(ip)->k_dir_id);
601		else
602			ip->i_generation = sd_v2_generation(sd);
603
604		if (S_ISDIR(ip->i_mode) || S_ISLNK(ip->i_mode))
605			set_inode_item_key_version(ip, KEY_FORMAT_3_5);
606		else
607			set_inode_item_key_version(ip, KEY_FORMAT_3_6);
608
609		REISERFS_I(ip)->i_first_direct_byte = 0;
610		set_inode_sd_version(ip, STAT_DATA_V2);
611		inode_set_bytes(ip, to_real_used_space(ip, ip->i_blocks,
612		    SD_V2_SIZE));
613
614		/*
615		 * Read persistent inode attributes from sd and initalise
616		 * generic inode flags from them
617		 */
618		REISERFS_I(ip)->i_attrs = sd_v2_attrs(sd);
619		sd_attrs_to_i_attrs(sd_v2_attrs(sd), ip);
620		reiserfs_log(LOG_DEBUG, "...done\n");
621	}
622
623	pathrelse(path);
624	if (S_ISREG(ip->i_mode)) {
625		reiserfs_log(LOG_DEBUG, "this inode is a regular file\n");
626		//ip->i_op = &reiserfs_file_ip_operations;
627		//ip->i_fop = &reiserfs_file_operations;
628		//ip->i_mapping->a_ops = &reiserfs_address_space_operations ;
629	} else if (S_ISDIR(ip->i_mode)) {
630		reiserfs_log(LOG_DEBUG, "this inode is a directory\n");
631		//ip->i_op = &reiserfs_dir_ip_operations;
632		//ip->i_fop = &reiserfs_dir_operations;
633	} else if (S_ISLNK(ip->i_mode)) {
634		reiserfs_log(LOG_DEBUG, "this inode is a symlink\n");
635		//ip->i_op = &reiserfs_symlink_ip_operations;
636		//ip->i_mapping->a_ops = &reiserfs_address_space_operations;
637	} else {
638		reiserfs_log(LOG_DEBUG, "this inode is something unknown in "
639		    "this universe\n");
640		ip->i_blocks = 0;
641		//ip->i_op = &reiserfs_special_ip_operations;
642		//init_special_ip(ip, ip->i_mode, new_decode_dev(rdev));
643	}
644}
645
646/*
647 * reiserfs_read_locked_inode is called to read the inode off disk, and
648 * it does a make_bad_inode when things go wrong. But, we need to make
649 * sure and clear the key in the private portion of the inode, otherwise
650 * a corresponding iput might try to delete whatever object the inode
651 * last represented.
652 */
653static void
654reiserfs_make_bad_inode(struct reiserfs_node *ip) {
655
656	memset(INODE_PKEY(ip), 0, KEY_SIZE);
657	//make_bad_inode(inode);
658}
659
660void
661reiserfs_read_locked_inode(struct reiserfs_node *ip,
662    struct reiserfs_iget_args *args)
663{
664	INITIALIZE_PATH(path_to_sd);
665	struct cpu_key key;
666	unsigned long dirino;
667	int retval;
668
669	dirino = args->dirid;
670
671	/*
672	 * Set version 1, version 2 could be used too, because stat data
673	 * key is the same in both versions
674	 */
675	key.version = KEY_FORMAT_3_5;
676	key.on_disk_key.k_dir_id = dirino;
677	key.on_disk_key.k_objectid = ip->i_number;
678	key.on_disk_key.u.k_offset_v1.k_offset = SD_OFFSET;
679	key.on_disk_key.u.k_offset_v1.k_uniqueness = SD_UNIQUENESS;
680
681	/* Look for the object's stat data */
682	retval = search_item(ip->i_reiserfs, &key, &path_to_sd);
683	if (retval == IO_ERROR) {
684		reiserfs_log(LOG_ERR,
685		    "I/O failure occured trying to find stat"
686		    "data %u/%u\n",
687		    key.on_disk_key.k_dir_id, key.on_disk_key.k_objectid);
688		reiserfs_make_bad_inode(ip);
689		return;
690	}
691	if (retval != ITEM_FOUND) {
692		/*
693		 * A stale NFS handle can trigger this without it being
694		 * an error
695		 */
696		reiserfs_log(LOG_ERR,
697		    "item not found (objectid=%u, dirid=%u)\n",
698		    key.on_disk_key.k_objectid, key.on_disk_key.k_dir_id);
699		pathrelse(&path_to_sd);
700		reiserfs_make_bad_inode(ip);
701		ip->i_nlink = 0;
702		return;
703	}
704
705	init_inode(ip, &path_to_sd);
706
707	/*
708	 * It is possible that knfsd is trying to access inode of a file
709	 * that is being removed from the disk by some other thread. As
710	 * we update sd on unlink all that is required is to check for
711	 * nlink here. This bug was first found by Sizif when debugging
712	 * SquidNG/Butterfly, forgotten, and found again after Philippe
713	 * Gramoulle <philippe.gramoulle@mmania.com> reproduced it.
714	 *
715	 * More logical fix would require changes in fs/inode.c:iput() to
716	 * remove inode from hash-table _after_ fs cleaned disk stuff up and
717	 * in iget() to return NULL if I_FREEING inode is found in hash-table.
718	 */
719	/*
720	 * Currently there is one place where it's ok to meet inode with
721	 * nlink == 0: processing of open-unlinked and half-truncated files
722	 * during mount (fs/reiserfs/super.c:finish_unfinished()).
723	 */
724	if((ip->i_nlink == 0) &&
725	    !REISERFS_SB(ip->i_reiserfs)->s_is_unlinked_ok ) {
726		reiserfs_log(LOG_WARNING, "dead inode read from disk. This is "
727		    "likely to be race with knfsd. Ignore");
728		reiserfs_make_bad_inode(ip);
729	}
730
731	/* Init inode should be relsing */
732	reiserfs_check_path(&path_to_sd);
733}
734
735int
736reiserfs_iget(
737    struct mount *mp, const struct cpu_key *key,
738    struct vnode **vpp, struct thread *td)
739{
740	int error, flags;
741	struct cdev *dev;
742	struct vnode *vp;
743	struct reiserfs_node *ip;
744	struct reiserfs_mount *rmp;
745
746	struct reiserfs_iget_args args;
747
748	//restart:
749	/* Check if the inode cache contains it */
750	// XXX LK_EXCLUSIVE ?
751	flags = LK_EXCLUSIVE;
752	error = vfs_hash_get(mp, key->on_disk_key.k_objectid, flags,
753	    td, vpp, NULL, NULL);
754	if (error || *vpp != NULL)
755		return (error);
756
757	rmp = VFSTOREISERFS(mp);
758	dev = rmp->rm_dev;
759
760	reiserfs_log(LOG_DEBUG, "malloc(struct reiserfs_node)\n");
761	ip = malloc(sizeof(struct reiserfs_node), M_REISERFSNODE,
762	    M_WAITOK | M_ZERO);
763
764	/* Allocate a new vnode/inode. */
765	reiserfs_log(LOG_DEBUG, "getnewvnode\n");
766	if ((error =
767	    getnewvnode("reiserfs", mp, &reiserfs_vnodeops, &vp)) != 0) {
768		*vpp = NULL;
769		free(ip, M_REISERFSNODE);
770		reiserfs_log(LOG_DEBUG, "getnewvnode FAILED\n");
771		return (error);
772	}
773
774	args.dirid = key->on_disk_key.k_dir_id;
775	args.objectid = key->on_disk_key.k_objectid;
776
777	reiserfs_log(LOG_DEBUG, "filling *ip\n");
778	vp->v_data     = ip;
779	ip->i_vnode    = vp;
780	ip->i_dev      = dev;
781	ip->i_number   = args.objectid;
782	ip->i_ino      = args.dirid;
783	ip->i_reiserfs = rmp->rm_reiserfs;
784
785	vp->v_bufobj.bo_ops = &reiserfs_vnbufops;
786	vp->v_bufobj.bo_private = vp;
787
788	/* If this is the root node, set the VV_ROOT flag */
789	if (ip->i_number == REISERFS_ROOT_OBJECTID &&
790	    ip->i_ino == REISERFS_ROOT_PARENT_OBJECTID)
791		vp->v_vflag |= VV_ROOT;
792
793#if 0
794	if (VOP_LOCK(vp, LK_EXCLUSIVE) != 0)
795		panic("reiserfs/iget: unexpected lock failure");
796
797	/*
798	 * Exclusively lock the vnode before adding to hash. Note, that we
799	 * must not release nor downgrade the lock (despite flags argument
800	 * says) till it is fully initialized.
801	 */
802	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, (struct mtx *)0);
803#endif
804
805	lockmgr(vp->v_vnlock, LK_EXCLUSIVE, NULL);
806	error = insmntque(vp, mp);
807	if (error != 0) {
808		free(ip, M_REISERFSNODE);
809		*vpp = NULL;
810		reiserfs_log(LOG_DEBUG, "insmntque FAILED\n");
811		return (error);
812	}
813	error = vfs_hash_insert(vp, key->on_disk_key.k_objectid, flags,
814	    td, vpp, NULL, NULL);
815	if (error || *vpp != NULL)
816		return (error);
817
818	/* Read the inode */
819	reiserfs_log(LOG_DEBUG, "call reiserfs_read_locked_inode ("
820	    "objectid=%d,dirid=%d)\n", args.objectid, args.dirid);
821	reiserfs_read_locked_inode(ip, &args);
822
823	ip->i_devvp = rmp->rm_devvp;
824
825	switch(vp->v_type = IFTOVT(ip->i_mode)) {
826	case VBLK:
827		reiserfs_log(LOG_DEBUG, "vnode type VBLK\n");
828		vp->v_op = &reiserfs_specops;
829		break;
830#if 0
831	case VCHR:
832		reiserfs_log(LOG_DEBUG, "vnode type VCHR\n");
833		vp->v_op = &reiserfs_specops;
834		vp = addaliasu(vp, ip->i_rdev);
835		ip->i_vnode = vp;
836		break;
837	case VFIFO:
838		reiserfs_log(LOG_DEBUG, "vnode type VFIFO\n");
839		vp->v_op = reiserfs_fifoop_p;
840		break;
841#endif
842	default:
843		break;
844	}
845
846	*vpp = vp;
847	return (0);
848}
849
850void
851sd_attrs_to_i_attrs(uint16_t sd_attrs, struct reiserfs_node *ip)
852{
853
854	if (reiserfs_attrs(ip->i_reiserfs)) {
855#if 0
856		if (sd_attrs & REISERFS_SYNC_FL)
857			ip->i_flags |= S_SYNC;
858		else
859			ip->i_flags &= ~S_SYNC;
860#endif
861		if (sd_attrs & REISERFS_IMMUTABLE_FL)
862			ip->i_flags |= IMMUTABLE;
863		else
864			ip->i_flags &= ~IMMUTABLE;
865		if (sd_attrs & REISERFS_APPEND_FL)
866			ip->i_flags |= APPEND;
867		else
868			ip->i_flags &= ~APPEND;
869#if 0
870		if (sd_attrs & REISERFS_NOATIME_FL)
871			ip->i_flags |= S_NOATIME;
872		else
873			ip->i_flags &= ~S_NOATIME;
874		if (sd_attrs & REISERFS_NOTAIL_FL)
875			REISERFS_I(ip)->i_flags |= i_nopack_mask;
876		else
877			REISERFS_I(ip)->i_flags &= ~i_nopack_mask;
878#endif
879	}
880}
881
882void
883i_attrs_to_sd_attrs(struct reiserfs_node *ip, uint16_t *sd_attrs)
884{
885
886	if (reiserfs_attrs(ip->i_reiserfs)) {
887#if 0
888		if (ip->i_flags & S_SYNC)
889			*sd_attrs |= REISERFS_SYNC_FL;
890		else
891			*sd_attrs &= ~REISERFS_SYNC_FL;
892#endif
893		if (ip->i_flags & IMMUTABLE)
894			*sd_attrs |= REISERFS_IMMUTABLE_FL;
895		else
896			*sd_attrs &= ~REISERFS_IMMUTABLE_FL;
897		if (ip->i_flags & APPEND)
898			*sd_attrs |= REISERFS_APPEND_FL;
899		else
900			*sd_attrs &= ~REISERFS_APPEND_FL;
901#if 0
902		if (ip->i_flags & S_NOATIME)
903			*sd_attrs |= REISERFS_NOATIME_FL;
904		else
905			*sd_attrs &= ~REISERFS_NOATIME_FL;
906		if (REISERFS_I(ip)->i_flags & i_nopack_mask)
907			*sd_attrs |= REISERFS_NOTAIL_FL;
908		else
909			*sd_attrs &= ~REISERFS_NOTAIL_FL;
910#endif
911	}
912}
913