1 /* fsys_xfs.c - an implementation for the SGI XFS file system */
2 /*
3  *  GRUB  --  GRand Unified Bootloader
4  *  Copyright (C) 2001,2002,2004  Free Software Foundation, Inc.
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <stddef.h>
21 #include <stdbool.h>
22 #include <xenfsimage_grub.h>
23 #include <xen-tools/common-macros.h>
24 #include "xfs.h"
25 
26 #define MAX_LINK_COUNT	8
27 
28 typedef struct xad {
29 	xfs_fileoff_t offset;
30 	xfs_fsblock_t start;
31 	xfs_filblks_t len;
32 } xad_t;
33 
34 struct xfs_info {
35 	int bsize;
36 	int dirbsize;
37 	int isize;
38 	unsigned int agblocks;
39 	int bdlog;
40 	int blklog;
41 	int inopblog;
42 	int agblklog;
43 	unsigned int nextents;
44 	xfs_daddr_t next;
45 	xfs_daddr_t daddr;
46 	xfs_dablk_t forw;
47 	xfs_dablk_t dablk;
48 	xfs_bmbt_rec_32_t *xt;
49 	xfs_bmbt_ptr_t ptr0;
50 	int btnode_ptr0_off;
51 	int i8param;
52 	int dirpos;
53 	int dirmax;
54 	int blkoff;
55 	int fpos;
56 	xfs_ino_t rootino;
57 };
58 
59 static struct xfs_info xfs;
60 
61 #define dirbuf		((char *)FSYS_BUF)
62 #define filebuf		((char *)FSYS_BUF + 4096)
63 #define inode		((xfs_dinode_t *)((char *)FSYS_BUF + 8192))
64 #define icore		(inode->di_core)
65 
66 #define	mask32lo(n)	((xfs_uint32_t)((1ull << (n)) - 1))
67 
68 #define	XFS_INO_MASK(k)		((xfs_uint32_t)((1ULL << (k)) - 1))
69 #define	XFS_INO_OFFSET_BITS	xfs.inopblog
70 #define	XFS_INO_AGINO_BITS	(xfs.agblklog + xfs.inopblog)
71 
72 static inline xfs_agblock_t
agino2agbno(xfs_agino_t agino)73 agino2agbno (xfs_agino_t agino)
74 {
75 	return agino >> XFS_INO_OFFSET_BITS;
76 }
77 
78 static inline xfs_agnumber_t
ino2agno(xfs_ino_t ino)79 ino2agno (xfs_ino_t ino)
80 {
81 	return ino >> XFS_INO_AGINO_BITS;
82 }
83 
84 static inline xfs_agino_t
ino2agino(xfs_ino_t ino)85 ino2agino (xfs_ino_t ino)
86 {
87 	return ino & XFS_INO_MASK(XFS_INO_AGINO_BITS);
88 }
89 
90 static inline int
ino2offset(xfs_ino_t ino)91 ino2offset (xfs_ino_t ino)
92 {
93 	return ino & XFS_INO_MASK(XFS_INO_OFFSET_BITS);
94 }
95 
96 static inline xfs_uint16_t
le16(xfs_uint16_t x)97 le16 (xfs_uint16_t x)
98 {
99 	__asm__("xchgb %b0,%h0"	\
100 		: "=Q" (x) \
101 		:  "0" (x)); \
102 		return x;
103 }
104 
105 static inline xfs_uint32_t
le32(xfs_uint32_t x)106 le32 (xfs_uint32_t x)
107 {
108 #if 0
109         /* 386 doesn't have bswap.  */
110 	__asm__("bswap %0" : "=r" (x) : "0" (x));
111 #else
112 	/* This is slower but this works on all x86 architectures.  */
113 	__asm__("xchgb %b0, %h0" \
114 		"\n\troll $16, %0" \
115 		"\n\txchgb %b0, %h0" \
116 		: "=Q" (x) : "0" (x));
117 #endif
118 	return x;
119 }
120 
121 static inline xfs_uint64_t
le64(xfs_uint64_t x)122 le64 (xfs_uint64_t x)
123 {
124 	xfs_uint32_t h = x >> 32;
125         xfs_uint32_t l = x & ((1ULL<<32)-1);
126         return (((xfs_uint64_t)le32(l)) << 32) | ((xfs_uint64_t)(le32(h)));
127 }
128 
129 
130 static xfs_fsblock_t
xt_start(xfs_bmbt_rec_32_t * r)131 xt_start (xfs_bmbt_rec_32_t *r)
132 {
133 	return (((xfs_fsblock_t)(le32 (r->l1) & mask32lo(9))) << 43) |
134 	       (((xfs_fsblock_t)le32 (r->l2)) << 11) |
135 	       (((xfs_fsblock_t)le32 (r->l3)) >> 21);
136 }
137 
138 static xfs_fileoff_t
xt_offset(xfs_bmbt_rec_32_t * r)139 xt_offset (xfs_bmbt_rec_32_t *r)
140 {
141 	return (((xfs_fileoff_t)le32 (r->l0) &
142 		mask32lo(31)) << 23) |
143 		(((xfs_fileoff_t)le32 (r->l1)) >> 9);
144 }
145 
146 static xfs_filblks_t
xt_len(xfs_bmbt_rec_32_t * r)147 xt_len (xfs_bmbt_rec_32_t *r)
148 {
149 	return le32(r->l3) & mask32lo(21);
150 }
151 
152 static int
isinxt(xfs_fileoff_t key,xfs_fileoff_t offset,xfs_filblks_t len)153 isinxt (xfs_fileoff_t key, xfs_fileoff_t offset, xfs_filblks_t len)
154 {
155 	return (key >= offset) ? (key < offset + len ? 1 : 0) : 0;
156 }
157 
158 static xfs_daddr_t
agb2daddr(xfs_agnumber_t agno,xfs_agblock_t agbno)159 agb2daddr (xfs_agnumber_t agno, xfs_agblock_t agbno)
160 {
161 	return ((xfs_fsblock_t)agno*xfs.agblocks + agbno) << xfs.bdlog;
162 }
163 
164 static xfs_daddr_t
fsb2daddr(xfs_fsblock_t fsbno)165 fsb2daddr (xfs_fsblock_t fsbno)
166 {
167 	return agb2daddr ((xfs_agnumber_t)(fsbno >> xfs.agblklog),
168 			 (xfs_agblock_t)(fsbno & mask32lo(xfs.agblklog)));
169 }
170 
171 static inline int
btroot_maxrecs(fsi_file_t * ffi)172 btroot_maxrecs (fsi_file_t *ffi)
173 {
174 	int tmp = icore.di_forkoff ? (icore.di_forkoff << 3) : xfs.isize;
175 
176 	return (tmp - sizeof(xfs_bmdr_block_t) - offsetof(xfs_dinode_t, di_u)) /
177 		(sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t));
178 }
179 
180 static int
di_read(fsi_file_t * ffi,xfs_ino_t ino)181 di_read (fsi_file_t *ffi, xfs_ino_t ino)
182 {
183 	xfs_agino_t agino;
184 	xfs_agnumber_t agno;
185 	xfs_agblock_t agbno;
186 	xfs_daddr_t daddr;
187 	int offset;
188 
189 	agno = ino2agno (ino);
190 	agino = ino2agino (ino);
191 	agbno = agino2agbno (agino);
192 	offset = ino2offset (ino);
193 	daddr = agb2daddr (agno, agbno);
194 
195 	devread (ffi, daddr, offset*xfs.isize, xfs.isize, (char *)inode);
196 
197 	xfs.ptr0 = *(xfs_bmbt_ptr_t *)
198 		    (inode->di_u.di_c + sizeof(xfs_bmdr_block_t)
199 		    + btroot_maxrecs (ffi)*sizeof(xfs_bmbt_key_t));
200 
201 	return 1;
202 }
203 
204 static void
init_extents(fsi_file_t * ffi)205 init_extents (fsi_file_t *ffi)
206 {
207 	xfs_bmbt_ptr_t ptr0;
208 	xfs_btree_lblock_t h;
209 
210 	switch (icore.di_format) {
211 	case XFS_DINODE_FMT_EXTENTS:
212 		xfs.xt = inode->di_u.di_bmx;
213 		xfs.nextents = le32 (icore.di_nextents);
214 		break;
215 	case XFS_DINODE_FMT_BTREE:
216 		ptr0 = xfs.ptr0;
217 		for (;;) {
218 			xfs.daddr = fsb2daddr (le64(ptr0));
219 			devread (ffi, xfs.daddr, 0,
220 				 sizeof(xfs_btree_lblock_t), (char *)&h);
221 			if (!h.bb_level) {
222 				xfs.nextents = le16(h.bb_numrecs);
223 				xfs.next = fsb2daddr (le64(h.bb_rightsib));
224 				xfs.fpos = sizeof(xfs_btree_block_t);
225 				return;
226 			}
227 			devread (ffi, xfs.daddr, xfs.btnode_ptr0_off,
228 				 sizeof(xfs_bmbt_ptr_t), (char *)&ptr0);
229 		}
230 	}
231 }
232 
233 static xad_t *
next_extent(fsi_file_t * ffi)234 next_extent (fsi_file_t *ffi)
235 {
236 	static xad_t xad;
237 
238 	switch (icore.di_format) {
239 	case XFS_DINODE_FMT_EXTENTS:
240 		if (xfs.nextents == 0)
241 			return NULL;
242 		break;
243 	case XFS_DINODE_FMT_BTREE:
244 		if (xfs.nextents == 0) {
245 			xfs_btree_lblock_t h;
246 			if (xfs.next == 0)
247 				return NULL;
248 			xfs.daddr = xfs.next;
249 			devread (ffi, xfs.daddr, 0, sizeof(xfs_btree_lblock_t), (char *)&h);
250 			xfs.nextents = le16(h.bb_numrecs);
251 			xfs.next = fsb2daddr (le64(h.bb_rightsib));
252 			xfs.fpos = sizeof(xfs_btree_block_t);
253 		}
254 		/* Yeah, I know that's slow, but I really don't care */
255 		devread (ffi, xfs.daddr, xfs.fpos, sizeof(xfs_bmbt_rec_t), filebuf);
256 		xfs.xt = (xfs_bmbt_rec_32_t *)filebuf;
257 		xfs.fpos += sizeof(xfs_bmbt_rec_32_t);
258 	}
259 	xad.offset = xt_offset (xfs.xt);
260 	xad.start = xt_start (xfs.xt);
261 	xad.len = xt_len (xfs.xt);
262 	++xfs.xt;
263 	--xfs.nextents;
264 
265 	return &xad;
266 }
267 
268 /*
269  * Name lies - the function reads only first 100 bytes
270  */
271 static void
xfs_dabread(fsi_file_t * ffi)272 xfs_dabread (fsi_file_t *ffi)
273 {
274 	xad_t *xad;
275 	xfs_fileoff_t offset;;
276 
277 	init_extents (ffi);
278 	while ((xad = next_extent (ffi))) {
279 		offset = xad->offset;
280 		if (isinxt (xfs.dablk, offset, xad->len)) {
281 			devread (ffi, fsb2daddr (xad->start + xfs.dablk - offset),
282 				 0, 100, dirbuf);
283 			break;
284 		}
285 	}
286 }
287 
288 static inline xfs_ino_t
sf_ino(char * sfe,int namelen)289 sf_ino (char *sfe, int namelen)
290 {
291 	void *p = sfe + namelen + 3;
292 
293 	return (xfs.i8param == 0)
294 		? le64(*(xfs_ino_t *)p) : le32(*(xfs_uint32_t *)p);
295 }
296 
297 static inline xfs_ino_t
sf_parent_ino(fsi_file_t * ffi)298 sf_parent_ino (fsi_file_t *ffi)
299 {
300 	return (xfs.i8param == 0)
301 		? le64(*(xfs_ino_t *)(&inode->di_u.di_dir2sf.hdr.parent))
302 		: le32(*(xfs_uint32_t *)(&inode->di_u.di_dir2sf.hdr.parent));
303 }
304 
305 static inline int
roundup8(int n)306 roundup8 (int n)
307 {
308 	return ((n+7)&~7);
309 }
310 
311 static int
312 xfs_read (fsi_file_t *ffi, char *buf, int len);
313 
314 static char *
next_dentry(fsi_file_t * ffi,xfs_ino_t * ino)315 next_dentry (fsi_file_t *ffi, xfs_ino_t *ino)
316 {
317 	int namelen = 1;
318 	int toread;
319 	static char usual[2][3] = {".", ".."};
320 	static xfs_dir2_sf_entry_t *sfe;
321 	char *name = usual[0];
322 
323 	if (xfs.dirpos >= xfs.dirmax) {
324 		if (xfs.forw == 0)
325 			return NULL;
326 		xfs.dablk = xfs.forw;
327 		xfs_dabread (ffi);
328 #define h	((xfs_dir2_leaf_hdr_t *)dirbuf)
329 		xfs.dirmax = le16 (h->count) - le16 (h->stale);
330 		xfs.forw = le32 (h->info.forw);
331 #undef h
332 		xfs.dirpos = 0;
333 	}
334 
335 	switch (icore.di_format) {
336 	case XFS_DINODE_FMT_LOCAL:
337 		switch (xfs.dirpos) {
338 		case -2:
339 			*ino = 0;
340 			break;
341 		case -1:
342 			*ino = sf_parent_ino (ffi);
343 			++name;
344 			++namelen;
345 			sfe = (xfs_dir2_sf_entry_t *)
346 				(inode->di_u.di_c
347 				 + sizeof(xfs_dir2_sf_hdr_t)
348 				 - xfs.i8param);
349 			break;
350 		default:
351 			namelen = sfe->namelen;
352 			*ino = sf_ino ((char *)sfe, namelen);
353 			name = (char *)sfe->name;
354 			sfe = (xfs_dir2_sf_entry_t *)
355 				  ((char *)sfe + namelen + 11 - xfs.i8param);
356 		}
357 		break;
358 	case XFS_DINODE_FMT_BTREE:
359 	case XFS_DINODE_FMT_EXTENTS:
360 #define dau	((xfs_dir2_data_union_t *)dirbuf)
361 		for (;;) {
362 			if (xfs.blkoff >= xfs.dirbsize) {
363 				xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
364 				filepos &= ~(xfs.dirbsize - 1);
365 				filepos |= xfs.blkoff;
366 			}
367 			xfs_read (ffi, dirbuf, 4);
368 			xfs.blkoff += 4;
369 			if (dau->unused.freetag == XFS_DIR2_DATA_FREE_TAG) {
370 				toread = roundup8 (le16(dau->unused.length)) - 4;
371 				xfs.blkoff += toread;
372 				filepos += toread;
373 				continue;
374 			}
375 			break;
376 		}
377 		xfs_read (ffi, (char *)dirbuf + 4, 5);
378 		*ino = le64 (dau->entry.inumber);
379 		namelen = dau->entry.namelen;
380 #undef dau
381 		toread = roundup8 (namelen + 11) - 9;
382 		xfs_read (ffi, dirbuf, toread);
383 		name = (char *)dirbuf;
384 		xfs.blkoff += toread + 5;
385 	}
386 	++xfs.dirpos;
387 	name[namelen] = 0;
388 
389 	return name;
390 }
391 
392 static char *
first_dentry(fsi_file_t * ffi,xfs_ino_t * ino)393 first_dentry (fsi_file_t *ffi, xfs_ino_t *ino)
394 {
395 	xfs.forw = 0;
396 	switch (icore.di_format) {
397 	case XFS_DINODE_FMT_LOCAL:
398 		xfs.dirmax = inode->di_u.di_dir2sf.hdr.count;
399 		xfs.i8param = inode->di_u.di_dir2sf.hdr.i8count ? 0 : 4;
400 		xfs.dirpos = -2;
401 		break;
402 	case XFS_DINODE_FMT_EXTENTS:
403 	case XFS_DINODE_FMT_BTREE:
404 		filepos = 0;
405 		xfs_read (ffi, dirbuf, sizeof(xfs_dir2_data_hdr_t));
406 		if (((xfs_dir2_data_hdr_t *)dirbuf)->magic == le32(XFS_DIR2_BLOCK_MAGIC)) {
407 #define tail		((xfs_dir2_block_tail_t *)dirbuf)
408 			filepos = xfs.dirbsize - sizeof(*tail);
409 			xfs_read (ffi, dirbuf, sizeof(*tail));
410 			xfs.dirmax = le32 (tail->count) - le32 (tail->stale);
411 #undef tail
412 		} else {
413 			xfs.dablk = (1ULL << 35) >> xfs.blklog;
414 #define h		((xfs_dir2_leaf_hdr_t *)dirbuf)
415 #define n		((xfs_da_intnode_t *)dirbuf)
416 			for (;;) {
417 				xfs_dabread (ffi);
418 				if ((n->hdr.info.magic == le16(XFS_DIR2_LEAFN_MAGIC))
419 				    || (n->hdr.info.magic == le16(XFS_DIR2_LEAF1_MAGIC))) {
420 					xfs.dirmax = le16 (h->count) - le16 (h->stale);
421 					xfs.forw = le32 (h->info.forw);
422 					break;
423 				}
424 				xfs.dablk = le32 (n->btree[0].before);
425 			}
426 #undef n
427 #undef h
428 		}
429 		xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
430 		filepos = xfs.blkoff;
431 		xfs.dirpos = 0;
432 	}
433 	return next_dentry (ffi, ino);
434 }
435 
436 static bool
xfs_sb_is_invalid(const xfs_sb_t * super)437 xfs_sb_is_invalid (const xfs_sb_t *super)
438 {
439 	return (le32(super->sb_magicnum) != XFS_SB_MAGIC)
440 	    || ((le16(super->sb_versionnum) & XFS_SB_VERSION_NUMBITS) !=
441 	        XFS_SB_VERSION_4)
442 	    || (super->sb_inodelog < XFS_SB_INODELOG_MIN)
443 	    || (super->sb_inodelog > XFS_SB_INODELOG_MAX)
444 	    || (super->sb_blocklog < XFS_SB_BLOCKLOG_MIN)
445 	    || (super->sb_blocklog > XFS_SB_BLOCKLOG_MAX)
446 	    || (super->sb_blocklog < super->sb_inodelog)
447 	    || (super->sb_agblklog > XFS_SB_AGBLKLOG_MAX)
448 	    || ((1ull << super->sb_agblklog) < le32(super->sb_agblocks))
449 	    || (((1ull << super->sb_agblklog) >> 1) >=
450 	        le32(super->sb_agblocks))
451 	    || ((super->sb_blocklog + super->sb_dirblklog) >=
452 	        XFS_SB_DIRBLK_NUMBITS);
453 }
454 
455 static int
xfs_mount(fsi_file_t * ffi,const char * options)456 xfs_mount (fsi_file_t *ffi, const char *options)
457 {
458 	xfs_sb_t super;
459 
460 	if (!devread (ffi, 0, 0, sizeof(super), (char *)&super)
461 	    || xfs_sb_is_invalid(&super)) {
462 		return 0;
463 	}
464 
465 	/*
466 	 * Not sanitized. It's exclusively used to generate disk addresses,
467 	 * so it's not important from a security standpoint.
468 	 */
469 	xfs.rootino = le64 (super.sb_rootino);
470 
471 	/*
472 	 * Sanitized to be consistent with each other, only used to
473 	 * generate disk addresses, so it's safe
474 	 */
475 	xfs.agblocks = le32 (super.sb_agblocks);
476 	xfs.agblklog = super.sb_agblklog;
477 
478 	/* Derived from sanitized parameters */
479 	BUILD_BUG_ON(XFS_SB_BLOCKLOG_MIN < SECTOR_BITS);
480 	xfs.bdlog = super.sb_blocklog - SECTOR_BITS;
481 	xfs.bsize = 1 << super.sb_blocklog;
482 	xfs.blklog = super.sb_blocklog;
483 	xfs.isize = 1 << super.sb_inodelog;
484 	xfs.dirbsize = 1 << (super.sb_blocklog + super.sb_dirblklog);
485 	xfs.inopblog = super.sb_blocklog - super.sb_inodelog;
486 
487 	xfs.btnode_ptr0_off =
488 		((xfs.bsize - sizeof(xfs_btree_block_t)) /
489 		(sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t)))
490 		 * sizeof(xfs_bmbt_key_t) + sizeof(xfs_btree_block_t);
491 
492 	return 1;
493 }
494 
495 static int
xfs_read(fsi_file_t * ffi,char * buf,int len)496 xfs_read (fsi_file_t *ffi, char *buf, int len)
497 {
498 	xad_t *xad;
499 	xfs_fileoff_t endofprev, endofcur, offset;
500 	xfs_filblks_t xadlen;
501 	int toread, startpos, endpos;
502 
503 	if (icore.di_format == XFS_DINODE_FMT_LOCAL) {
504 		grub_memmove (buf, inode->di_u.di_c + filepos, len);
505 		filepos += len;
506 		return len;
507 	}
508 
509 	startpos = filepos;
510 	endpos = filepos + len;
511 	endofprev = (xfs_fileoff_t)-1;
512 	init_extents (ffi);
513 	while (len > 0 && (xad = next_extent (ffi))) {
514 		offset = xad->offset;
515 		xadlen = xad->len;
516 		if (isinxt (filepos >> xfs.blklog, offset, xadlen)) {
517 			endofcur = (offset + xadlen) << xfs.blklog;
518 			toread = (endofcur >= endpos)
519 				  ? len : (endofcur - filepos);
520 
521 			disk_read_func = disk_read_hook;
522 			devread (ffi, fsb2daddr (xad->start),
523 				 filepos - (offset << xfs.blklog), toread, buf);
524 			disk_read_func = NULL;
525 
526 			buf += toread;
527 			len -= toread;
528 			filepos += toread;
529 		} else if (offset > endofprev) {
530 			toread = ((offset << xfs.blklog) >= endpos)
531 				  ? len : ((offset - endofprev) << xfs.blklog);
532 			len -= toread;
533 			filepos += toread;
534 			for (; toread; toread--) {
535 				*buf++ = 0;
536 			}
537 			continue;
538 		}
539 		endofprev = offset + xadlen;
540 	}
541 
542 	return filepos - startpos;
543 }
544 
545 static int
xfs_dir(fsi_file_t * ffi,char * dirname)546 xfs_dir (fsi_file_t *ffi, char *dirname)
547 {
548 	xfs_ino_t ino, parent_ino, new_ino;
549 	xfs_fsize_t di_size;
550 	int di_mode;
551 	int cmp, n, link_count;
552 	char linkbuf[xfs.bsize];
553 	char *rest, *name, ch;
554 
555 	parent_ino = ino = xfs.rootino;
556 	link_count = 0;
557 	for (;;) {
558 		di_read (ffi, ino);
559 		di_size = le64 (icore.di_size);
560 		di_mode = le16 (icore.di_mode);
561 
562 		if ((di_mode & IFMT) == IFLNK) {
563 			if (++link_count > MAX_LINK_COUNT) {
564 				errnum = ERR_SYMLINK_LOOP;
565 				return 0;
566 			}
567 			if (di_size < xfs.bsize - 1) {
568 				filepos = 0;
569 				filemax = di_size;
570 				n = xfs_read (ffi, linkbuf, filemax);
571 			} else {
572 				errnum = ERR_FILELENGTH;
573 				return 0;
574 			}
575 
576 			ino = (linkbuf[0] == '/') ? xfs.rootino : parent_ino;
577 			while (n < (xfs.bsize - 1) && (linkbuf[n++] = *dirname++));
578 			linkbuf[n] = 0;
579 			dirname = linkbuf;
580 			continue;
581 		}
582 
583 		if (!*dirname || isspace ((uint8_t)*dirname)) {
584 			if ((di_mode & IFMT) != IFREG) {
585 				errnum = ERR_BAD_FILETYPE;
586 				return 0;
587 			}
588 			filepos = 0;
589 			filemax = di_size;
590 			return 1;
591 		}
592 
593 		if ((di_mode & IFMT) != IFDIR) {
594 			errnum = ERR_BAD_FILETYPE;
595 			return 0;
596 		}
597 
598 		for (; *dirname == '/'; dirname++);
599 
600 		for (rest = dirname; (ch = *rest)
601                    && !isspace ((uint8_t)ch) && ch != '/'; rest++);
602 		*rest = 0;
603 
604 		name = first_dentry (ffi, &new_ino);
605 		for (;;) {
606 			cmp = (!*dirname) ? -1 : substring (dirname, name);
607 #ifndef STAGE1_5
608 			if (print_possibilities && ch != '/' && cmp <= 0) {
609 				if (print_possibilities > 0)
610 					print_possibilities = -print_possibilities;
611 				print_a_completion (name);
612 			} else
613 #endif
614 			if (cmp == 0) {
615 				parent_ino = ino;
616 				if (new_ino)
617 					ino = new_ino;
618 		        	*(dirname = rest) = ch;
619 				break;
620 			}
621 			name = next_dentry (ffi, &new_ino);
622 			if (name == NULL) {
623 				if (print_possibilities < 0)
624 					return 1;
625 
626 				errnum = ERR_FILE_NOT_FOUND;
627 				*rest = ch;
628 				return 0;
629 			}
630 		}
631 	}
632 }
633 
634 fsi_plugin_ops_t *
fsi_init_plugin(int version,fsi_plugin_t * fp,const char ** name)635 fsi_init_plugin(int version, fsi_plugin_t *fp, const char **name)
636 {
637 	static fsig_plugin_ops_t ops = {
638 		FSIMAGE_PLUGIN_VERSION,
639 		.fpo_mount = xfs_mount,
640 		.fpo_dir = xfs_dir,
641 		.fpo_read = xfs_read
642 	};
643 
644 	*name = "xfs";
645 	return (fsig_init(fp, &ops));
646 }
647