]> git.ozlabs.org Git - yaboot.git/blob - second/fs_xfs.c
Commit yaboot 1.3.4-pre1
[yaboot.git] / second / fs_xfs.c
1 /*
2  *  fsys_xfs.c - an implementation for the SGI XFS file system
3  *
4  *  Copyright (C) 2001  Ethan Benson
5  *
6  *  Adapted from Grub
7  *
8  *  Copyright (C) 2001  Serguei Tzukanov
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or
13  *  (at your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful,
16  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *  GNU General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License
21  *  along with this program; if not, write to the Free Software
22  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #include "types.h"
26 #include "ctype.h"
27 #include "string.h"
28 #include "stdlib.h"
29 #include "fs.h"
30 #include "xfs/xfs.h"
31 #include "errors.h"
32
33 #define SECTOR_BITS 9
34
35 int xfs_mount (void);
36 int xfs_read_data (char *buf, int len);
37 int xfs_dir (char *dirname);
38
39 /* Exported in struct fs_t */
40 static int xfs_open(struct boot_file_t *file, const char *dev_name,
41                     struct partition_t *part, const char *file_name);
42 static int xfs_read(struct boot_file_t *file, unsigned int size, void *buffer);
43 static int xfs_seek(struct boot_file_t *file, unsigned int newpos);
44 static int xfs_close(struct boot_file_t *file);
45
46 struct fs_t xfs_filesystem = {
47         name:"xfs",
48         open:xfs_open,
49         read:xfs_read,
50         seek:xfs_seek,
51         close:xfs_close
52 };
53
54 struct boot_file_t *xfs_file;
55 static char FSYS_BUF[32768];
56 __u64 partition_offset;
57 int errnum;
58
59 static int
60 xfs_open(struct boot_file_t *file, const char *dev_name,
61          struct partition_t *part, const char *file_name)
62 {
63         static char buffer[1024];
64
65         DEBUG_ENTER;
66         DEBUG_OPEN;
67
68         if (part)
69         {
70                 DEBUG_F("Determining offset for partition %d\n", part->part_number);
71                 partition_offset = ((__u64)(part->part_start)) * ((__u64)part->blocksize);
72                 DEBUG_F("%Lu = %lu * %hu\n", partition_offset,
73                         part->part_start,
74                         part->blocksize);
75         }
76         else
77                 partition_offset = 0;
78
79         sprintf(buffer, "%s:%d", dev_name, 0); /* 0 is full disk in OF */
80         DEBUG_F("Trying to open dev_name=%s; filename=%s; partition offset=%Lu\n",
81                 buffer, file_name, partition_offset);
82         file->of_device = prom_open(buffer);
83
84         if (file->of_device == PROM_INVALID_HANDLE || file->of_device == NULL)
85         {
86                 DEBUG_F("Can't open device %p\n", file->of_device);
87                 DEBUG_LEAVE(FILE_ERR_BADDEV);
88                 return FILE_ERR_BADDEV;
89         }
90
91         DEBUG_F("%p was successfully opened\n", file->of_device);
92
93         xfs_file = file;
94     
95         if (xfs_mount() != 1)
96         {
97                 DEBUG_F("Couldn't open XFS @ %s/%Lu\n", buffer, partition_offset);
98                 prom_close(file->of_device);
99                 DEBUG_LEAVE(FILE_ERR_BAD_FSYS);
100                 DEBUG_SLEEP;
101                 return FILE_ERR_BAD_FSYS;
102         }
103
104         DEBUG_F("Attempting to open %s\n", file_name);
105         strcpy(buffer, file_name); /* xfs_dir modifies argument */
106         if(!xfs_dir(buffer))
107         {
108                 DEBUG_F("xfs_dir() failed. errnum = %d\n", errnum);
109                 prom_close( file->of_device );
110                 DEBUG_LEAVE_F(errnum);
111                 DEBUG_SLEEP;
112                 return errnum;
113         }
114
115         DEBUG_F("Successfully opened %s\n", file_name);
116
117         DEBUG_LEAVE(FILE_ERR_OK);
118         return FILE_ERR_OK;
119 }
120
121 static int
122 xfs_read(struct boot_file_t *file, unsigned int size, void *buffer)
123 {
124         return xfs_read_data(buffer, size);
125 }
126
127 static int
128 xfs_seek(struct boot_file_t *file, unsigned int newpos)
129 {
130         file->pos = newpos;
131         return FILE_ERR_OK;
132 }
133
134 static int
135 xfs_close(struct boot_file_t *file)
136 {
137         if(file->of_device)
138         {
139                 prom_close(file->of_device);
140                 file->of_device = 0;
141                 DEBUG_F("xfs_close called\n");
142         }
143         return FILE_ERR_OK;
144 }
145
146 static int
147 read_disk_block(struct boot_file_t *file, __u32 block, __u32 start,
148                 __u32 length, void *buf)
149 {
150         unsigned long long pos = block * 512;
151         pos += partition_offset + start;
152         DEBUG_F("Reading %u bytes, starting at block %u, disk offset %Lu\n",
153                 length, block, pos);
154         if (!prom_lseek(file->of_device, pos)) {
155                 DEBUG_F("prom_lseek failed\n");
156                 return 0;
157         }
158         return prom_read(file->of_device, buf, length);
159 }
160
161 #define MAX_LINK_COUNT  8
162
163 typedef struct xad {
164         xfs_fileoff_t offset;
165         xfs_fsblock_t start;
166         xfs_filblks_t len;
167 } xad_t;
168
169 struct xfs_info {
170         int bsize;
171         int dirbsize;
172         int isize;
173         unsigned int agblocks;
174         int bdlog;
175         int blklog;
176         int inopblog;
177         int agblklog;
178         int agnolog;
179         int dirblklog;
180         unsigned int nextents;
181         xfs_daddr_t next;
182         xfs_daddr_t daddr;
183         xfs_dablk_t forw;
184         xfs_dablk_t dablk;
185         xfs_bmbt_rec_32_t *xt;
186         xfs_bmbt_ptr_t ptr0;
187         int btnode_ptr0_off;
188         int i8param;
189         int dirpos;
190         int dirmax;
191         int blkoff;
192         int fpos;
193         xfs_ino_t rootino;
194 };
195
196 static struct xfs_info xfs;
197
198 #define dirbuf          ((char *)FSYS_BUF)
199 #define filebuf         ((char *)FSYS_BUF + 4096)
200 #define inode           ((xfs_dinode_t *)((char *)FSYS_BUF + 8192))
201 #define icore           (inode->di_core)
202
203 #define mask32lo(n)     (((__uint32_t)1 << (n)) - 1)
204
205 #define XFS_INO_MASK(k)         ((__uint32_t)((1ULL << (k)) - 1))
206 #define XFS_INO_OFFSET_BITS     xfs.inopblog
207 #define XFS_INO_AGBNO_BITS      xfs.agblklog
208 #define XFS_INO_AGINO_BITS      (xfs.agblklog + xfs.inopblog)
209 #define XFS_INO_AGNO_BITS       xfs.agnolog
210
211 static inline xfs_agblock_t
212 agino2agbno (xfs_agino_t agino)
213 {
214         return agino >> XFS_INO_OFFSET_BITS;
215 }
216
217 static inline xfs_agnumber_t
218 ino2agno (xfs_ino_t ino)
219 {
220         return ino >> XFS_INO_AGINO_BITS;
221 }
222
223 static inline xfs_agino_t
224 ino2agino (xfs_ino_t ino)
225 {
226         return ino & XFS_INO_MASK(XFS_INO_AGINO_BITS);
227 }
228
229 static inline int
230 ino2offset (xfs_ino_t ino)
231 {
232         return ino & XFS_INO_MASK(XFS_INO_OFFSET_BITS);
233 }
234
235 /* XFS is big endian, powerpc is big endian */
236
237 static inline __const__ __uint16_t
238 le16 (__uint16_t x)
239 {
240         return x;
241 }
242
243 static inline __const__ __uint32_t
244 le32 (__uint32_t x)
245 {
246         return x;
247 }
248
249 static inline __const__ __uint64_t
250 le64 (__uint64_t x)
251 {
252         return x;
253 }
254
255 static xfs_fsblock_t
256 xt_start (xfs_bmbt_rec_32_t *r)
257 {
258         return (((xfs_fsblock_t)(le32 (r->l1) & mask32lo(9))) << 43) | 
259                (((xfs_fsblock_t)le32 (r->l2)) << 11) |
260                (((xfs_fsblock_t)le32 (r->l3)) >> 21);
261 }
262
263 static xfs_fileoff_t
264 xt_offset (xfs_bmbt_rec_32_t *r)
265 {
266         return (((xfs_fileoff_t)le32 (r->l0) &
267                 mask32lo(31)) << 23) |
268                 (((xfs_fileoff_t)le32 (r->l1)) >> 9);
269 }
270
271 static xfs_filblks_t
272 xt_len (xfs_bmbt_rec_32_t *r)
273 {
274         return le32(r->l3) & mask32lo(21);
275 }
276
277 static const char xfs_highbit[256] = {
278        -1, 0, 1, 1, 2, 2, 2, 2,                 /* 00 .. 07 */
279         3, 3, 3, 3, 3, 3, 3, 3,                 /* 08 .. 0f */
280         4, 4, 4, 4, 4, 4, 4, 4,                 /* 10 .. 17 */
281         4, 4, 4, 4, 4, 4, 4, 4,                 /* 18 .. 1f */
282         5, 5, 5, 5, 5, 5, 5, 5,                 /* 20 .. 27 */
283         5, 5, 5, 5, 5, 5, 5, 5,                 /* 28 .. 2f */
284         5, 5, 5, 5, 5, 5, 5, 5,                 /* 30 .. 37 */
285         5, 5, 5, 5, 5, 5, 5, 5,                 /* 38 .. 3f */
286         6, 6, 6, 6, 6, 6, 6, 6,                 /* 40 .. 47 */
287         6, 6, 6, 6, 6, 6, 6, 6,                 /* 48 .. 4f */
288         6, 6, 6, 6, 6, 6, 6, 6,                 /* 50 .. 57 */
289         6, 6, 6, 6, 6, 6, 6, 6,                 /* 58 .. 5f */
290         6, 6, 6, 6, 6, 6, 6, 6,                 /* 60 .. 67 */
291         6, 6, 6, 6, 6, 6, 6, 6,                 /* 68 .. 6f */
292         6, 6, 6, 6, 6, 6, 6, 6,                 /* 70 .. 77 */
293         6, 6, 6, 6, 6, 6, 6, 6,                 /* 78 .. 7f */
294         7, 7, 7, 7, 7, 7, 7, 7,                 /* 80 .. 87 */
295         7, 7, 7, 7, 7, 7, 7, 7,                 /* 88 .. 8f */
296         7, 7, 7, 7, 7, 7, 7, 7,                 /* 90 .. 97 */
297         7, 7, 7, 7, 7, 7, 7, 7,                 /* 98 .. 9f */
298         7, 7, 7, 7, 7, 7, 7, 7,                 /* a0 .. a7 */
299         7, 7, 7, 7, 7, 7, 7, 7,                 /* a8 .. af */
300         7, 7, 7, 7, 7, 7, 7, 7,                 /* b0 .. b7 */
301         7, 7, 7, 7, 7, 7, 7, 7,                 /* b8 .. bf */
302         7, 7, 7, 7, 7, 7, 7, 7,                 /* c0 .. c7 */
303         7, 7, 7, 7, 7, 7, 7, 7,                 /* c8 .. cf */
304         7, 7, 7, 7, 7, 7, 7, 7,                 /* d0 .. d7 */
305         7, 7, 7, 7, 7, 7, 7, 7,                 /* d8 .. df */
306         7, 7, 7, 7, 7, 7, 7, 7,                 /* e0 .. e7 */
307         7, 7, 7, 7, 7, 7, 7, 7,                 /* e8 .. ef */
308         7, 7, 7, 7, 7, 7, 7, 7,                 /* f0 .. f7 */
309         7, 7, 7, 7, 7, 7, 7, 7,                 /* f8 .. ff */
310 };
311
312 static int
313 xfs_highbit32(__uint32_t v)
314 {
315         int             i;
316
317         if (v & 0xffff0000)
318                 if (v & 0xff000000)
319                         i = 24;
320                 else
321                         i = 16;
322         else if (v & 0x0000ffff)
323                 if (v & 0x0000ff00)
324                         i = 8;
325                 else
326                         i = 0;
327         else
328                 return -1;
329         return i + xfs_highbit[(v >> i) & 0xff];
330 }
331
332 static int
333 isinxt (xfs_fileoff_t key, xfs_fileoff_t offset, xfs_filblks_t len)
334 {
335         return (key >= offset) ? (key < offset + len ? 1 : 0) : 0;
336 }
337
338 static xfs_daddr_t
339 agb2daddr (xfs_agnumber_t agno, xfs_agblock_t agbno)
340 {
341         return ((xfs_fsblock_t)agno*xfs.agblocks + agbno) << xfs.bdlog;
342 }
343
344 static xfs_daddr_t
345 fsb2daddr (xfs_fsblock_t fsbno)
346 {
347         return agb2daddr ((xfs_agnumber_t)(fsbno >> xfs.agblklog),
348                          (xfs_agblock_t)(fsbno & mask32lo(xfs.agblklog)));
349 }
350
351 static inline int
352 btroot_maxrecs (void)
353 {
354         int tmp = icore.di_forkoff ? (icore.di_forkoff << 3) : xfs.isize;
355
356         return (tmp - sizeof(xfs_bmdr_block_t) -
357                 (int)((char *)&inode->di_u - (char*)inode)) /
358                 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t));
359 }
360
361 static int
362 di_read (xfs_ino_t ino)
363 {
364         xfs_agino_t agino;
365         xfs_agnumber_t agno;
366         xfs_agblock_t agbno;
367         xfs_daddr_t daddr;
368         int offset;
369
370         agno = ino2agno (ino);
371         agino = ino2agino (ino);
372         agbno = agino2agbno (agino);
373         offset = ino2offset (ino);
374         daddr = agb2daddr (agno, agbno);
375
376         read_disk_block(xfs_file, daddr, offset*xfs.isize, xfs.isize, (char *)inode);
377
378         xfs.ptr0 = *(xfs_bmbt_ptr_t *)
379                     (inode->di_u.di_c + sizeof(xfs_bmdr_block_t)
380                     + btroot_maxrecs ()*sizeof(xfs_bmbt_key_t));
381
382         return 1;
383 }
384
385 static void
386 init_extents (void)
387 {
388         xfs_bmbt_ptr_t ptr0;
389         xfs_btree_lblock_t h;
390
391         switch (icore.di_format) {
392         case XFS_DINODE_FMT_EXTENTS:
393                 xfs.xt = inode->di_u.di_bmx;
394                 xfs.nextents = le32 (icore.di_nextents);
395                 break;
396         case XFS_DINODE_FMT_BTREE:
397                 ptr0 = xfs.ptr0;
398                 for (;;) {
399                         xfs.daddr = fsb2daddr (le64(ptr0));
400                         read_disk_block(xfs_file, xfs.daddr, 0,
401                                         sizeof(xfs_btree_lblock_t), (char *)&h);
402                         if (!h.bb_level) {
403                                 xfs.nextents = le16(h.bb_numrecs);
404                                 xfs.next = fsb2daddr (le64(h.bb_leftsib));
405                                 xfs.fpos = sizeof(xfs_btree_block_t);
406                                 return;
407                         }
408                         read_disk_block(xfs_file, xfs.daddr, xfs.btnode_ptr0_off,
409                                  sizeof(xfs_bmbt_ptr_t), (char *)&ptr0);
410                 }
411         }
412 }
413
414 static xad_t *
415 next_extent (void)
416 {
417         static xad_t xad;
418
419         switch (icore.di_format) {
420         case XFS_DINODE_FMT_EXTENTS:
421                 if (xfs.nextents == 0)
422                         return NULL;
423                 break;
424         case XFS_DINODE_FMT_BTREE:
425                 if (xfs.nextents == 0) {
426                         xfs_btree_lblock_t h;
427                         if (xfs.next == 0)
428                                 return NULL;
429                         xfs.daddr = xfs.next;
430                         read_disk_block(xfs_file, xfs.daddr, 0,
431                                         sizeof(xfs_btree_lblock_t), (char *)&h);
432                         xfs.nextents = le16(h.bb_numrecs);
433                         xfs.next = fsb2daddr (le64(h.bb_leftsib));
434                         xfs.fpos = sizeof(xfs_btree_block_t);
435                 }
436                 /* Yeah, I know that's slow, but I really don't care */
437                 read_disk_block(xfs_file, xfs.daddr, xfs.fpos,
438                                 sizeof(xfs_bmbt_rec_t), filebuf);
439                 xfs.xt = (xfs_bmbt_rec_32_t *)filebuf;
440                 xfs.fpos += sizeof(xfs_bmbt_rec_32_t);
441                 break;
442         default:
443                 return NULL;
444         }
445         xad.offset = xt_offset (xfs.xt);
446         xad.start = xt_start (xfs.xt);
447         xad.len = xt_len (xfs.xt);
448         ++xfs.xt;
449         --xfs.nextents;
450
451         return &xad;
452 }
453
454 /*
455  * Name lies - the function reads only first 100 bytes
456  */
457 static void
458 xfs_dabread (void)
459 {
460         xad_t *xad;
461         xfs_fileoff_t offset;;
462
463         init_extents ();
464         while ((xad = next_extent ())) {
465                 offset = xad->offset;
466                 if (isinxt (xfs.dablk, offset, xad->len)) {
467                         read_disk_block(xfs_file, fsb2daddr (xad->start + xfs.dablk - offset),
468                                         0, 100, dirbuf);
469                         break;
470                 }
471         }
472 }
473
474 static inline xfs_ino_t
475 sf_ino (char *sfe, int namelen)
476 {
477         void *p = sfe + namelen + 3;
478
479         return (xfs.i8param == 0)
480                 ? le64(*(xfs_ino_t *)p) : le32(*(__uint32_t *)p);
481 }
482
483 static inline xfs_ino_t
484 sf_parent_ino (void)
485 {
486         return (xfs.i8param == 0)
487                 ? le64(*(xfs_ino_t *)(&inode->di_u.di_dir2sf.hdr.parent))
488                 : le32(*(__uint32_t *)(&inode->di_u.di_dir2sf.hdr.parent));
489 }
490
491 static inline int
492 roundup8 (int n)
493 {
494         return ((n+7)&~7);
495 }
496
497 static char *
498 next_dentry (xfs_ino_t *ino)
499 {
500         int namelen = 1;
501         int toread;
502         static char *usual[2] = {".", ".."};
503         static xfs_dir2_sf_entry_t *sfe;
504         char *name = usual[0];
505
506         if (xfs.dirpos >= xfs.dirmax) {
507                 if (xfs.forw == 0)
508                         return NULL;
509                 xfs.dablk = xfs.forw;
510                 xfs_dabread ();
511 #define h       ((xfs_dir2_leaf_hdr_t *)dirbuf)
512                 xfs.dirmax = le16 (h->count) - le16 (h->stale);
513                 xfs.forw = le32 (h->info.forw);
514 #undef h
515                 xfs.dirpos = 0;
516         }
517
518         switch (icore.di_format) {
519         case XFS_DINODE_FMT_LOCAL:
520                 switch (xfs.dirpos) {
521                 case -2:
522                         *ino = 0;
523                         break;
524                 case -1:
525                         *ino = sf_parent_ino ();
526                         ++name;
527                         ++namelen;
528                         sfe = (xfs_dir2_sf_entry_t *)
529                                 (inode->di_u.di_c 
530                                  + sizeof(xfs_dir2_sf_hdr_t)
531                                  - xfs.i8param);
532                         break;
533                 default:
534                         namelen = sfe->namelen;
535                         *ino = sf_ino ((char *)sfe, namelen);
536                         name = sfe->name;
537                         sfe = (xfs_dir2_sf_entry_t *)
538                                   ((char *)sfe + namelen + 11 - xfs.i8param);
539                 }
540                 break;
541         case XFS_DINODE_FMT_BTREE:
542         case XFS_DINODE_FMT_EXTENTS:
543 #define dau     ((xfs_dir2_data_union_t *)dirbuf)
544                 for (;;) {
545                         if (xfs.blkoff >= xfs.dirbsize) {
546                                 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
547                                 xfs_file->pos &= ~(xfs.dirbsize - 1);
548                                 xfs_file->pos |= xfs.blkoff;
549                         }
550                         xfs_read_data (dirbuf, 4);
551                         xfs.blkoff += 4;
552                         if (dau->unused.freetag == XFS_DIR2_DATA_FREE_TAG) {
553                                 toread = roundup8 (le16(dau->unused.length)) - 4;
554                                 xfs.blkoff += toread;
555                                 xfs_file->pos += toread;
556                                 continue;
557                         }
558                         break;
559                 }
560                 xfs_read_data ((char *)dirbuf + 4, 5);
561                 *ino = le64 (dau->entry.inumber);
562                 namelen = dau->entry.namelen;
563 #undef dau
564                 toread = roundup8 (namelen + 11) - 9;
565                 xfs_read_data (dirbuf, toread);
566                 name = (char *)dirbuf;
567                 xfs.blkoff += toread + 5;
568                 break;
569         }
570         ++xfs.dirpos;
571         name[namelen] = 0;
572
573         return name;
574 }
575
576 static char *
577 first_dentry (xfs_ino_t *ino)
578 {
579         xfs.forw = 0;
580         switch (icore.di_format) {
581         case XFS_DINODE_FMT_LOCAL:
582                 xfs.dirmax = inode->di_u.di_dir2sf.hdr.count;
583                 xfs.i8param = inode->di_u.di_dir2sf.hdr.i8count ? 0 : 4;
584                 xfs.dirpos = -2;
585                 break;
586         case XFS_DINODE_FMT_EXTENTS:
587         case XFS_DINODE_FMT_BTREE:
588                 xfs_file->pos = 0;
589                 xfs_file->len = le64 (icore.di_size);
590                 xfs_read_data (dirbuf, sizeof(xfs_dir2_data_hdr_t));
591                 if (((xfs_dir2_data_hdr_t *)dirbuf)->magic == le32(XFS_DIR2_BLOCK_MAGIC)) {
592 #define tail            ((xfs_dir2_block_tail_t *)dirbuf)
593                         xfs_file->pos = xfs.dirbsize - sizeof(*tail);
594                         xfs_read_data (dirbuf, sizeof(*tail));
595                         xfs.dirmax = le32 (tail->count) - le32 (tail->stale);
596 #undef tail
597                 } else {
598                         xfs.dablk = (1ULL << 35) >> xfs.blklog;
599 #define h               ((xfs_dir2_leaf_hdr_t *)dirbuf)
600 #define n               ((xfs_da_intnode_t *)dirbuf)
601                         for (;;) {
602                                 xfs_dabread ();
603                                 if ((n->hdr.info.magic == le16(XFS_DIR2_LEAFN_MAGIC))
604                                     || (n->hdr.info.magic == le16(XFS_DIR2_LEAF1_MAGIC))) {
605                                         xfs.dirmax = le16 (h->count) - le16 (h->stale);
606                                         xfs.forw = le32 (h->info.forw);
607                                         break;
608                                 }
609                                 xfs.dablk = le32 (n->btree[0].before);
610                         }
611 #undef n
612 #undef h
613                 }
614                 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
615                 xfs_file->pos = xfs.blkoff;
616                 xfs.dirpos = 0;
617                 break;
618         }
619         return next_dentry (ino);
620 }
621
622 int
623 xfs_mount (void)
624 {
625         xfs_sb_t super;
626
627         if (read_disk_block(xfs_file, 0, 0, sizeof(super), &super) != sizeof(super)) {
628                 DEBUG_F("read_disk_block failed!\n");
629                 return 0;
630         } else if (super.sb_magicnum != XFS_SB_MAGIC) {
631                 DEBUG_F("xfs_mount: Bad magic: %x\n", super.sb_magicnum);
632                 return 0;
633         } else if ((super.sb_versionnum & XFS_SB_VERSION_NUMBITS) != XFS_SB_VERSION_4) {
634                 DEBUG_F("xfs_mount: Bad version: %x\n", super.sb_versionnum);
635                 return 0;
636         }
637
638         xfs.bsize = le32 (super.sb_blocksize);
639         xfs.blklog = super.sb_blocklog;
640         xfs.bdlog = xfs.blklog - SECTOR_BITS;
641         xfs.rootino = le64 (super.sb_rootino);
642         xfs.isize = le16 (super.sb_inodesize);
643         xfs.agblocks = le32 (super.sb_agblocks);
644         xfs.dirblklog = super.sb_dirblklog;
645         xfs.dirbsize = xfs.bsize << super.sb_dirblklog;
646
647         xfs.inopblog = super.sb_inopblog;
648         xfs.agblklog = super.sb_agblklog;
649         xfs.agnolog = xfs_highbit32 (le32 (super.sb_agcount) - 1) + 1;
650
651         xfs.btnode_ptr0_off =
652                 ((xfs.bsize - sizeof(xfs_btree_block_t)) /
653                 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t)))
654                  * sizeof(xfs_bmbt_key_t) + sizeof(xfs_btree_block_t);
655
656         return 1;
657 }
658
659 int
660 xfs_read_data (char *buf, int len)
661 {
662         xad_t *xad;
663         xfs_fileoff_t endofprev, endofcur, offset;
664         xfs_filblks_t xadlen;
665         int toread, startpos, endpos;
666
667         if (icore.di_format == XFS_DINODE_FMT_LOCAL) {
668                 memmove(buf, inode->di_u.di_c + xfs_file->pos, len);
669                 xfs_file->pos += len;
670                 return len;
671         }
672
673         startpos = xfs_file->pos;
674         endpos = xfs_file->pos + len;
675         if (endpos > xfs_file->len)
676                 endpos = xfs_file->len;
677         endofprev = (xfs_fileoff_t)-1;
678         init_extents ();
679         while (len > 0 && (xad = next_extent ())) {
680                 offset = xad->offset;
681                 xadlen = xad->len;
682                 if (isinxt (xfs_file->pos >> xfs.blklog, offset, xadlen)) {
683                         endofcur = (offset + xadlen) << xfs.blklog; 
684                         toread = (endofcur >= endpos)
685                                   ? len : (endofcur - xfs_file->pos);
686                         read_disk_block(xfs_file, fsb2daddr (xad->start),
687                                         xfs_file->pos - (offset << xfs.blklog), toread, buf);
688                         buf += toread;
689                         len -= toread;
690                         xfs_file->pos += toread;
691                 } else if (offset > endofprev) {
692                         toread = ((offset << xfs.blklog) >= endpos)
693                                   ? len : ((offset - endofprev) << xfs.blklog);
694                         len -= toread;
695                         xfs_file->pos += toread;
696                         for (; toread; toread--) {
697                                 *buf++ = 0;
698                         }
699                         continue;
700                 }
701                 endofprev = offset + xadlen; 
702         }
703
704         return xfs_file->pos - startpos;
705 }
706
707 int
708 xfs_dir (char *dirname)
709 {
710         xfs_ino_t ino, parent_ino, new_ino;
711         xfs_fsize_t di_size;
712         int di_mode;
713         int cmp, n, link_count;
714         char linkbuf[xfs.bsize];
715         char *rest, *name, ch;
716
717         DEBUG_ENTER;
718
719         parent_ino = ino = xfs.rootino;
720         link_count = 0;
721         for (;;) {
722                 di_read (ino);
723                 di_size = le64 (icore.di_size);
724                 di_mode = le16 (icore.di_mode);
725
726                 DEBUG_F("di_mode: %o\n", di_mode);
727                 if ((di_mode & IFMT) == IFLNK) {
728                         if (++link_count > MAX_LINK_COUNT) {
729                                 errnum = FILE_ERR_SYMLINK_LOOP;
730                                 DEBUG_LEAVE(FILE_ERR_SYMLINK_LOOP);
731                                 return 0;
732                         }
733                         if (di_size < xfs.bsize - 1) {
734                                 xfs_file->pos = 0;
735                                 xfs_file->len = di_size;
736                                 n = xfs_read_data (linkbuf, xfs_file->len);
737                         } else {
738                                 errnum = FILE_ERR_LENGTH;
739                                 DEBUG_LEAVE(FILE_ERR_LENGTH);
740                                 return 0;
741                         }
742
743                         ino = (linkbuf[0] == '/') ? xfs.rootino : parent_ino;
744                         while (n < (xfs.bsize - 1) && (linkbuf[n++] = *dirname++));
745                         linkbuf[n] = 0;
746                         dirname = linkbuf;
747                         continue;
748                 }
749
750                 DEBUG_F("*dirname: %s\n", dirname);
751                 if (!*dirname || isspace (*dirname)) {
752                         if ((di_mode & IFMT) != IFREG) {
753                                 errnum = FILE_ERR_BAD_TYPE;
754                                 DEBUG_LEAVE(FILE_ERR_BAD_TYPE);
755                                 return 0;
756                         }
757                         xfs_file->pos = 0;
758                         xfs_file->len = di_size;
759                         DEBUG_LEAVE(1);
760                         return 1;
761                 }
762
763                 if ((di_mode & IFMT) != IFDIR) {
764                         errnum = FILE_ERR_NOTDIR;
765                         DEBUG_LEAVE(FILE_ERR_NOTDIR);
766                         return 0;
767                 }
768
769                 for (; *dirname == '/'; dirname++);
770
771                 for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++);
772                 *rest = 0;
773
774                 name = first_dentry (&new_ino);
775                 for (;;) {
776                         cmp = (!*dirname) ? -1 : strcmp(dirname, name);
777                         if (cmp == 0) {
778                                 parent_ino = ino;
779                                 if (new_ino)
780                                         ino = new_ino;
781                                 *(dirname = rest) = ch;
782                                 break;
783                         }
784                         name = next_dentry (&new_ino);
785                         if (name == NULL) {
786                                 errnum = FILE_ERR_NOTFOUND;
787                                 DEBUG_LEAVE(FILE_ERR_NOTFOUND);
788                                 *rest = ch;
789                                 return 0;
790                         }
791                 }
792         }
793 }
794
795 /* 
796  * Local variables:
797  * c-file-style: "K&R"
798  * c-basic-offset: 8
799  * End:
800  */