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