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