]> git.ozlabs.org Git - yaboot.git/blob - second/fs_xfs.c
The following patch fix reverts removal of some lines for the netboot patch.
[yaboot.git] / second / fs_xfs.c
1 /*
2  *  fs_xfs.c - an implementation for the SGI XFS file system
3  *
4  *  Copyright (C) 2001, 2002 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 uint64_t 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 = ((uint64_t) part->part_start) * 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, uint64_t block, int start,
149                 int length, void *buf)
150 {
151         uint64_t pos = block * 512;
152         pos += partition_offset + start;
153         DEBUG_F("Reading %d bytes, starting at block %Lu, 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 #define le16(x) (x)
238 #define le32(x) (x)
239 #define le64(x) (x)
240
241 static xfs_fsblock_t
242 xt_start (xfs_bmbt_rec_32_t *r)
243 {
244         return (((xfs_fsblock_t)(le32 (r->l1) & mask32lo(9))) << 43) |
245                (((xfs_fsblock_t)le32 (r->l2)) << 11) |
246                (((xfs_fsblock_t)le32 (r->l3)) >> 21);
247 }
248
249 static xfs_fileoff_t
250 xt_offset (xfs_bmbt_rec_32_t *r)
251 {
252         return (((xfs_fileoff_t)le32 (r->l0) &
253                 mask32lo(31)) << 23) |
254                 (((xfs_fileoff_t)le32 (r->l1)) >> 9);
255 }
256
257 static xfs_filblks_t
258 xt_len (xfs_bmbt_rec_32_t *r)
259 {
260         return le32(r->l3) & mask32lo(21);
261 }
262
263 static const char xfs_highbit[256] = {
264        -1, 0, 1, 1, 2, 2, 2, 2,                 /* 00 .. 07 */
265         3, 3, 3, 3, 3, 3, 3, 3,                 /* 08 .. 0f */
266         4, 4, 4, 4, 4, 4, 4, 4,                 /* 10 .. 17 */
267         4, 4, 4, 4, 4, 4, 4, 4,                 /* 18 .. 1f */
268         5, 5, 5, 5, 5, 5, 5, 5,                 /* 20 .. 27 */
269         5, 5, 5, 5, 5, 5, 5, 5,                 /* 28 .. 2f */
270         5, 5, 5, 5, 5, 5, 5, 5,                 /* 30 .. 37 */
271         5, 5, 5, 5, 5, 5, 5, 5,                 /* 38 .. 3f */
272         6, 6, 6, 6, 6, 6, 6, 6,                 /* 40 .. 47 */
273         6, 6, 6, 6, 6, 6, 6, 6,                 /* 48 .. 4f */
274         6, 6, 6, 6, 6, 6, 6, 6,                 /* 50 .. 57 */
275         6, 6, 6, 6, 6, 6, 6, 6,                 /* 58 .. 5f */
276         6, 6, 6, 6, 6, 6, 6, 6,                 /* 60 .. 67 */
277         6, 6, 6, 6, 6, 6, 6, 6,                 /* 68 .. 6f */
278         6, 6, 6, 6, 6, 6, 6, 6,                 /* 70 .. 77 */
279         6, 6, 6, 6, 6, 6, 6, 6,                 /* 78 .. 7f */
280         7, 7, 7, 7, 7, 7, 7, 7,                 /* 80 .. 87 */
281         7, 7, 7, 7, 7, 7, 7, 7,                 /* 88 .. 8f */
282         7, 7, 7, 7, 7, 7, 7, 7,                 /* 90 .. 97 */
283         7, 7, 7, 7, 7, 7, 7, 7,                 /* 98 .. 9f */
284         7, 7, 7, 7, 7, 7, 7, 7,                 /* a0 .. a7 */
285         7, 7, 7, 7, 7, 7, 7, 7,                 /* a8 .. af */
286         7, 7, 7, 7, 7, 7, 7, 7,                 /* b0 .. b7 */
287         7, 7, 7, 7, 7, 7, 7, 7,                 /* b8 .. bf */
288         7, 7, 7, 7, 7, 7, 7, 7,                 /* c0 .. c7 */
289         7, 7, 7, 7, 7, 7, 7, 7,                 /* c8 .. cf */
290         7, 7, 7, 7, 7, 7, 7, 7,                 /* d0 .. d7 */
291         7, 7, 7, 7, 7, 7, 7, 7,                 /* d8 .. df */
292         7, 7, 7, 7, 7, 7, 7, 7,                 /* e0 .. e7 */
293         7, 7, 7, 7, 7, 7, 7, 7,                 /* e8 .. ef */
294         7, 7, 7, 7, 7, 7, 7, 7,                 /* f0 .. f7 */
295         7, 7, 7, 7, 7, 7, 7, 7,                 /* f8 .. ff */
296 };
297
298 static int
299 xfs_highbit32(__uint32_t v)
300 {
301         int             i;
302
303         if (v & 0xffff0000)
304                 if (v & 0xff000000)
305                         i = 24;
306                 else
307                         i = 16;
308         else if (v & 0x0000ffff)
309                 if (v & 0x0000ff00)
310                         i = 8;
311                 else
312                         i = 0;
313         else
314                 return -1;
315         return i + xfs_highbit[(v >> i) & 0xff];
316 }
317
318 static int
319 isinxt (xfs_fileoff_t key, xfs_fileoff_t offset, xfs_filblks_t len)
320 {
321         return (key >= offset) ? (key < offset + len ? 1 : 0) : 0;
322 }
323
324 static xfs_daddr_t
325 agb2daddr (xfs_agnumber_t agno, xfs_agblock_t agbno)
326 {
327         return ((xfs_fsblock_t)agno*xfs.agblocks + agbno) << xfs.bdlog;
328 }
329
330 static xfs_daddr_t
331 fsb2daddr (xfs_fsblock_t fsbno)
332 {
333         return agb2daddr ((xfs_agnumber_t)(fsbno >> xfs.agblklog),
334                          (xfs_agblock_t)(fsbno & mask32lo(xfs.agblklog)));
335 }
336
337 static inline int
338 btroot_maxrecs (void)
339 {
340         int tmp = icore.di_forkoff ? (icore.di_forkoff << 3) : xfs.isize;
341
342         return (tmp - sizeof(xfs_bmdr_block_t) -
343                 (int)((char *)&inode->di_u - (char*)inode)) /
344                 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t));
345 }
346
347 static int
348 di_read (xfs_ino_t ino)
349 {
350         xfs_agino_t agino;
351         xfs_agnumber_t agno;
352         xfs_agblock_t agbno;
353         xfs_daddr_t daddr;
354         int offset;
355
356         agno = ino2agno (ino);
357         agino = ino2agino (ino);
358         agbno = agino2agbno (agino);
359         offset = ino2offset (ino);
360         daddr = agb2daddr (agno, agbno);
361
362         read_disk_block(xfs_file, daddr, offset*xfs.isize, xfs.isize, (char *)inode);
363
364         xfs.ptr0 = *(xfs_bmbt_ptr_t *)
365                     (inode->di_u.di_c + sizeof(xfs_bmdr_block_t)
366                     + btroot_maxrecs ()*sizeof(xfs_bmbt_key_t));
367
368         return 1;
369 }
370
371 static void
372 init_extents (void)
373 {
374         xfs_bmbt_ptr_t ptr0;
375         xfs_btree_lblock_t h;
376
377         switch (icore.di_format) {
378         case XFS_DINODE_FMT_EXTENTS:
379                 xfs.xt = inode->di_u.di_bmx;
380                 xfs.nextents = le32 (icore.di_nextents);
381                 break;
382         case XFS_DINODE_FMT_BTREE:
383                 ptr0 = xfs.ptr0;
384                 for (;;) {
385                         xfs.daddr = fsb2daddr (le64(ptr0));
386                         read_disk_block(xfs_file, xfs.daddr, 0,
387                                         sizeof(xfs_btree_lblock_t), (char *)&h);
388                         if (!h.bb_level) {
389                                 xfs.nextents = le16(h.bb_numrecs);
390                                 xfs.next = fsb2daddr (le64(h.bb_leftsib));
391                                 xfs.fpos = sizeof(xfs_btree_block_t);
392                                 return;
393                         }
394                         read_disk_block(xfs_file, xfs.daddr, xfs.btnode_ptr0_off,
395                                  sizeof(xfs_bmbt_ptr_t), (char *)&ptr0);
396                 }
397         }
398 }
399
400 static xad_t *
401 next_extent (void)
402 {
403         static xad_t xad;
404
405         switch (icore.di_format) {
406         case XFS_DINODE_FMT_EXTENTS:
407                 if (xfs.nextents == 0)
408                         return NULL;
409                 break;
410         case XFS_DINODE_FMT_BTREE:
411                 if (xfs.nextents == 0) {
412                         xfs_btree_lblock_t h;
413                         if (xfs.next == 0)
414                                 return NULL;
415                         xfs.daddr = xfs.next;
416                         read_disk_block(xfs_file, xfs.daddr, 0,
417                                         sizeof(xfs_btree_lblock_t), (char *)&h);
418                         xfs.nextents = le16(h.bb_numrecs);
419                         xfs.next = fsb2daddr (le64(h.bb_leftsib));
420                         xfs.fpos = sizeof(xfs_btree_block_t);
421                 }
422                 /* Yeah, I know that's slow, but I really don't care */
423                 read_disk_block(xfs_file, xfs.daddr, xfs.fpos,
424                                 sizeof(xfs_bmbt_rec_t), filebuf);
425                 xfs.xt = (xfs_bmbt_rec_32_t *)filebuf;
426                 xfs.fpos += sizeof(xfs_bmbt_rec_32_t);
427                 break;
428         default:
429                 return NULL;
430         }
431         xad.offset = xt_offset (xfs.xt);
432         xad.start = xt_start (xfs.xt);
433         xad.len = xt_len (xfs.xt);
434         ++xfs.xt;
435         --xfs.nextents;
436
437         return &xad;
438 }
439
440 /*
441  * Name lies - the function reads only first 100 bytes
442  */
443 static void
444 xfs_dabread (void)
445 {
446         xad_t *xad;
447         xfs_fileoff_t offset;;
448
449         init_extents ();
450         while ((xad = next_extent ())) {
451                 offset = xad->offset;
452                 if (isinxt (xfs.dablk, offset, xad->len)) {
453                         read_disk_block(xfs_file, fsb2daddr (xad->start + xfs.dablk - offset),
454                                         0, 100, dirbuf);
455                         break;
456                 }
457         }
458 }
459
460 static inline xfs_ino_t
461 sf_ino (char *sfe, int namelen)
462 {
463         void *p = sfe + namelen + 3;
464
465         return (xfs.i8param == 0)
466                 ? le64(*(xfs_ino_t *)p) : le32(*(__uint32_t *)p);
467 }
468
469 static inline xfs_ino_t
470 sf_parent_ino (void)
471 {
472         return (xfs.i8param == 0)
473                 ? le64(*(xfs_ino_t *)(&inode->di_u.di_dir2sf.hdr.parent))
474                 : le32(*(__uint32_t *)(&inode->di_u.di_dir2sf.hdr.parent));
475 }
476
477 static inline int
478 roundup8 (int n)
479 {
480         return ((n+7)&~7);
481 }
482
483 static char *
484 next_dentry (xfs_ino_t *ino)
485 {
486         int namelen = 1;
487         int toread;
488         static char *usual[2] = {".", ".."};
489         static xfs_dir2_sf_entry_t *sfe;
490         char *name = usual[0];
491
492         if (xfs.dirpos >= xfs.dirmax) {
493                 if (xfs.forw == 0)
494                         return NULL;
495                 xfs.dablk = xfs.forw;
496                 xfs_dabread ();
497 #define h       ((xfs_dir2_leaf_hdr_t *)dirbuf)
498                 xfs.dirmax = le16 (h->count) - le16 (h->stale);
499                 xfs.forw = le32 (h->info.forw);
500 #undef h
501                 xfs.dirpos = 0;
502         }
503
504         switch (icore.di_format) {
505         case XFS_DINODE_FMT_LOCAL:
506                 switch (xfs.dirpos) {
507                 case -2:
508                         *ino = 0;
509                         break;
510                 case -1:
511                         *ino = sf_parent_ino ();
512                         ++name;
513                         ++namelen;
514                         sfe = (xfs_dir2_sf_entry_t *)
515                                 (inode->di_u.di_c
516                                  + sizeof(xfs_dir2_sf_hdr_t)
517                                  - xfs.i8param);
518                         break;
519                 default:
520                         namelen = sfe->namelen;
521                         *ino = sf_ino ((char *)sfe, namelen);
522                         name = sfe->name;
523                         sfe = (xfs_dir2_sf_entry_t *)
524                                   ((char *)sfe + namelen + 11 - xfs.i8param);
525                 }
526                 break;
527         case XFS_DINODE_FMT_BTREE:
528         case XFS_DINODE_FMT_EXTENTS:
529 #define dau     ((xfs_dir2_data_union_t *)dirbuf)
530                 for (;;) {
531                         if (xfs.blkoff >= xfs.dirbsize) {
532                                 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
533                                 xfs_file->pos &= ~(xfs.dirbsize - 1);
534                                 xfs_file->pos |= xfs.blkoff;
535                         }
536                         xfs_read_data (dirbuf, 4);
537                         xfs.blkoff += 4;
538                         if (dau->unused.freetag == XFS_DIR2_DATA_FREE_TAG) {
539                                 toread = roundup8 (le16(dau->unused.length)) - 4;
540                                 xfs.blkoff += toread;
541                                 xfs_file->pos += toread;
542                                 continue;
543                         }
544                         break;
545                 }
546                 xfs_read_data ((char *)dirbuf + 4, 5);
547                 *ino = le64 (dau->entry.inumber);
548                 namelen = dau->entry.namelen;
549 #undef dau
550                 toread = roundup8 (namelen + 11) - 9;
551                 xfs_read_data (dirbuf, toread);
552                 name = (char *)dirbuf;
553                 xfs.blkoff += toread + 5;
554                 break;
555         }
556         ++xfs.dirpos;
557         name[namelen] = 0;
558
559         return name;
560 }
561
562 static char *
563 first_dentry (xfs_ino_t *ino)
564 {
565         xfs.forw = 0;
566         switch (icore.di_format) {
567         case XFS_DINODE_FMT_LOCAL:
568                 xfs.dirmax = inode->di_u.di_dir2sf.hdr.count;
569                 xfs.i8param = inode->di_u.di_dir2sf.hdr.i8count ? 0 : 4;
570                 xfs.dirpos = -2;
571                 break;
572         case XFS_DINODE_FMT_EXTENTS:
573         case XFS_DINODE_FMT_BTREE:
574                 xfs_file->pos = 0;
575                 xfs_file->len = le64 (icore.di_size);
576                 xfs_read_data (dirbuf, sizeof(xfs_dir2_data_hdr_t));
577                 if (((xfs_dir2_data_hdr_t *)dirbuf)->magic == le32(XFS_DIR2_BLOCK_MAGIC)) {
578 #define tail            ((xfs_dir2_block_tail_t *)dirbuf)
579                         xfs_file->pos = xfs.dirbsize - sizeof(*tail);
580                         xfs_read_data (dirbuf, sizeof(*tail));
581                         xfs.dirmax = le32 (tail->count) - le32 (tail->stale);
582 #undef tail
583                 } else {
584                         xfs.dablk = (1ULL << 35) >> xfs.blklog;
585 #define h               ((xfs_dir2_leaf_hdr_t *)dirbuf)
586 #define n               ((xfs_da_intnode_t *)dirbuf)
587                         for (;;) {
588                                 xfs_dabread ();
589                                 if ((n->hdr.info.magic == le16(XFS_DIR2_LEAFN_MAGIC))
590                                     || (n->hdr.info.magic == le16(XFS_DIR2_LEAF1_MAGIC))) {
591                                         xfs.dirmax = le16 (h->count) - le16 (h->stale);
592                                         xfs.forw = le32 (h->info.forw);
593                                         break;
594                                 }
595                                 xfs.dablk = le32 (n->btree[0].before);
596                         }
597 #undef n
598 #undef h
599                 }
600                 xfs.blkoff = sizeof(xfs_dir2_data_hdr_t);
601                 xfs_file->pos = xfs.blkoff;
602                 xfs.dirpos = 0;
603                 break;
604         }
605         return next_dentry (ino);
606 }
607
608 int
609 xfs_mount (void)
610 {
611         xfs_sb_t super;
612
613         if (read_disk_block(xfs_file, 0, 0, sizeof(super), &super) != sizeof(super)) {
614                 DEBUG_F("read_disk_block failed!\n");
615                 return 0;
616         } else if (super.sb_magicnum != XFS_SB_MAGIC) {
617                 DEBUG_F("xfs_mount: Bad magic: %x\n", super.sb_magicnum);
618                 return 0;
619         } else if ((super.sb_versionnum & XFS_SB_VERSION_NUMBITS) != XFS_SB_VERSION_4) {
620                 DEBUG_F("xfs_mount: Bad version: %x\n", super.sb_versionnum);
621                 return 0;
622         }
623
624         xfs.bsize = le32 (super.sb_blocksize);
625         xfs.blklog = super.sb_blocklog;
626         xfs.bdlog = xfs.blklog - SECTOR_BITS;
627         xfs.rootino = le64 (super.sb_rootino);
628         xfs.isize = le16 (super.sb_inodesize);
629         xfs.agblocks = le32 (super.sb_agblocks);
630         xfs.dirblklog = super.sb_dirblklog;
631         xfs.dirbsize = xfs.bsize << super.sb_dirblklog;
632
633         xfs.inopblog = super.sb_inopblog;
634         xfs.agblklog = super.sb_agblklog;
635         xfs.agnolog = xfs_highbit32 (le32 (super.sb_agcount) - 1) + 1;
636
637         xfs.btnode_ptr0_off =
638                 ((xfs.bsize - sizeof(xfs_btree_block_t)) /
639                 (sizeof (xfs_bmbt_key_t) + sizeof (xfs_bmbt_ptr_t)))
640                  * sizeof(xfs_bmbt_key_t) + sizeof(xfs_btree_block_t);
641
642         return 1;
643 }
644
645 int
646 xfs_read_data (char *buf, int len)
647 {
648         xad_t *xad;
649         xfs_fileoff_t endofprev, endofcur, offset;
650         xfs_filblks_t xadlen;
651         int toread, startpos, endpos;
652
653         if (icore.di_format == XFS_DINODE_FMT_LOCAL) {
654                 memmove(buf, inode->di_u.di_c + xfs_file->pos, len);
655                 xfs_file->pos += len;
656                 return len;
657         }
658
659         startpos = xfs_file->pos;
660         endpos = xfs_file->pos + len;
661         if (endpos > xfs_file->len)
662                 endpos = xfs_file->len;
663         endofprev = (xfs_fileoff_t)-1;
664         init_extents ();
665         while (len > 0 && (xad = next_extent ())) {
666                 offset = xad->offset;
667                 xadlen = xad->len;
668                 if (isinxt (xfs_file->pos >> xfs.blklog, offset, xadlen)) {
669                         endofcur = (offset + xadlen) << xfs.blklog;
670                         toread = (endofcur >= endpos)
671                                   ? len : (endofcur - xfs_file->pos);
672                         read_disk_block(xfs_file, fsb2daddr (xad->start),
673                                         xfs_file->pos - (offset << xfs.blklog), toread, buf);
674                         buf += toread;
675                         len -= toread;
676                         xfs_file->pos += toread;
677                 } else if (offset > endofprev) {
678                         toread = ((offset << xfs.blklog) >= endpos)
679                                   ? len : ((offset - endofprev) << xfs.blklog);
680                         len -= toread;
681                         xfs_file->pos += toread;
682                         for (; toread; toread--) {
683                                 *buf++ = 0;
684                         }
685                         continue;
686                 }
687                 endofprev = offset + xadlen;
688         }
689
690         return xfs_file->pos - startpos;
691 }
692
693 int
694 xfs_dir (char *dirname)
695 {
696         xfs_ino_t ino, parent_ino, new_ino;
697         xfs_fsize_t di_size;
698         int di_mode;
699         int cmp, n, link_count;
700         char linkbuf[xfs.bsize];
701         char *rest, *name, ch;
702
703         DEBUG_ENTER;
704
705         parent_ino = ino = xfs.rootino;
706         link_count = 0;
707         for (;;) {
708                 di_read (ino);
709                 di_size = le64 (icore.di_size);
710                 di_mode = le16 (icore.di_mode);
711
712                 DEBUG_F("di_mode: %o\n", di_mode);
713                 if ((di_mode & IFMT) == IFLNK) {
714                         if (++link_count > MAX_LINK_COUNT) {
715                                 errnum = FILE_ERR_SYMLINK_LOOP;
716                                 DEBUG_LEAVE(FILE_ERR_SYMLINK_LOOP);
717                                 return 0;
718                         }
719                         if (di_size < xfs.bsize - 1) {
720                                 xfs_file->pos = 0;
721                                 xfs_file->len = di_size;
722                                 n = xfs_read_data (linkbuf, xfs_file->len);
723                         } else {
724                                 errnum = FILE_ERR_LENGTH;
725                                 DEBUG_LEAVE(FILE_ERR_LENGTH);
726                                 return 0;
727                         }
728
729                         ino = (linkbuf[0] == '/') ? xfs.rootino : parent_ino;
730                         while (n < (xfs.bsize - 1) && (linkbuf[n++] = *dirname++));
731                         linkbuf[n] = 0;
732                         dirname = linkbuf;
733                         continue;
734                 }
735
736                 DEBUG_F("*dirname: %s\n", dirname);
737                 if (!*dirname || isspace (*dirname)) {
738                         if ((di_mode & IFMT) != IFREG) {
739                                 errnum = FILE_ERR_BAD_TYPE;
740                                 DEBUG_LEAVE(FILE_ERR_BAD_TYPE);
741                                 return 0;
742                         }
743                         xfs_file->pos = 0;
744                         xfs_file->len = di_size;
745                         DEBUG_LEAVE(1);
746                         return 1;
747                 }
748
749                 if ((di_mode & IFMT) != IFDIR) {
750                         errnum = FILE_ERR_NOTDIR;
751                         DEBUG_LEAVE(FILE_ERR_NOTDIR);
752                         return 0;
753                 }
754
755                 for (; *dirname == '/'; dirname++);
756
757                 for (rest = dirname; (ch = *rest) && !isspace (ch) && ch != '/'; rest++);
758                 *rest = 0;
759
760                 name = first_dentry (&new_ino);
761                 for (;;) {
762                         cmp = (!*dirname) ? -1 : strcmp(dirname, name);
763                         if (cmp == 0) {
764                                 parent_ino = ino;
765                                 if (new_ino)
766                                         ino = new_ino;
767                                 *(dirname = rest) = ch;
768                                 break;
769                         }
770                         name = next_dentry (&new_ino);
771                         if (name == NULL) {
772                                 errnum = FILE_ERR_NOTFOUND;
773                                 DEBUG_LEAVE(FILE_ERR_NOTFOUND);
774                                 *rest = ch;
775                                 return 0;
776                         }
777                 }
778         }
779 }
780
781 /*
782  * Local variables:
783  * c-file-style: "k&r"
784  * c-basic-offset: 8
785  * End:
786  */