UBIFS: Implement read-only UBIFS support in U-Boot
[kernel/u-boot.git] / fs / ubifs / ubifs.c
1 /*
2  * This file is part of UBIFS.
3  *
4  * Copyright (C) 2006-2008 Nokia Corporation.
5  *
6  * (C) Copyright 2008-2009
7  * Stefan Roese, DENX Software Engineering, sr@denx.de.
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU General Public License version 2 as published by
11  * the Free Software Foundation.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
16  * more details.
17  *
18  * You should have received a copy of the GNU General Public License along with
19  * this program; if not, write to the Free Software Foundation, Inc., 51
20  * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  *
22  * Authors: Artem Bityutskiy (Битюцкий Артём)
23  *          Adrian Hunter
24  */
25
26 #include "ubifs.h"
27
28 #if !defined(CONFIG_SYS_64BIT_VSPRINTF)
29 #warning Please define CONFIG_SYS_64BIT_VSPRINTF for correct output!
30 #endif
31
32 DECLARE_GLOBAL_DATA_PTR;
33
34 /* compress.c */
35
36 /*
37  * We need a wrapper for gunzip() because the parameters are
38  * incompatible with the lzo decompressor.
39  */
40 static int gzip_decompress(const unsigned char *in, size_t in_len,
41                            unsigned char *out, size_t *out_len)
42 {
43         unsigned long len = in_len;
44         return gunzip(out, *out_len, (unsigned char *)in, &len);
45 }
46
47 /* Fake description object for the "none" compressor */
48 static struct ubifs_compressor none_compr = {
49         .compr_type = UBIFS_COMPR_NONE,
50         .name = "no compression",
51         .capi_name = "",
52         .decompress = NULL,
53 };
54
55 static struct ubifs_compressor lzo_compr = {
56         .compr_type = UBIFS_COMPR_LZO,
57         .name = "LZO",
58         .capi_name = "lzo",
59         .decompress = lzo1x_decompress_safe,
60 };
61
62 static struct ubifs_compressor zlib_compr = {
63         .compr_type = UBIFS_COMPR_ZLIB,
64         .name = "zlib",
65         .capi_name = "deflate",
66         .decompress = gzip_decompress,
67 };
68
69 /* All UBIFS compressors */
70 struct ubifs_compressor *ubifs_compressors[UBIFS_COMPR_TYPES_CNT];
71
72 /**
73  * ubifs_decompress - decompress data.
74  * @in_buf: data to decompress
75  * @in_len: length of the data to decompress
76  * @out_buf: output buffer where decompressed data should
77  * @out_len: output length is returned here
78  * @compr_type: type of compression
79  *
80  * This function decompresses data from buffer @in_buf into buffer @out_buf.
81  * The length of the uncompressed data is returned in @out_len. This functions
82  * returns %0 on success or a negative error code on failure.
83  */
84 int ubifs_decompress(const void *in_buf, int in_len, void *out_buf,
85                      int *out_len, int compr_type)
86 {
87         int err;
88         struct ubifs_compressor *compr;
89
90         if (unlikely(compr_type < 0 || compr_type >= UBIFS_COMPR_TYPES_CNT)) {
91                 ubifs_err("invalid compression type %d", compr_type);
92                 return -EINVAL;
93         }
94
95         compr = ubifs_compressors[compr_type];
96
97         if (unlikely(!compr->capi_name)) {
98                 ubifs_err("%s compression is not compiled in", compr->name);
99                 return -EINVAL;
100         }
101
102         if (compr_type == UBIFS_COMPR_NONE) {
103                 memcpy(out_buf, in_buf, in_len);
104                 *out_len = in_len;
105                 return 0;
106         }
107
108         err = compr->decompress(in_buf, in_len, out_buf, (size_t *)out_len);
109         if (err)
110                 ubifs_err("cannot decompress %d bytes, compressor %s, "
111                           "error %d", in_len, compr->name, err);
112
113         return err;
114 }
115
116 /**
117  * compr_init - initialize a compressor.
118  * @compr: compressor description object
119  *
120  * This function initializes the requested compressor and returns zero in case
121  * of success or a negative error code in case of failure.
122  */
123 static int __init compr_init(struct ubifs_compressor *compr)
124 {
125         ubifs_compressors[compr->compr_type] = compr;
126         ubifs_compressors[compr->compr_type]->name += gd->reloc_off;
127         ubifs_compressors[compr->compr_type]->capi_name += gd->reloc_off;
128         ubifs_compressors[compr->compr_type]->decompress += gd->reloc_off;
129         return 0;
130 }
131
132 /**
133  * ubifs_compressors_init - initialize UBIFS compressors.
134  *
135  * This function initializes the compressor which were compiled in. Returns
136  * zero in case of success and a negative error code in case of failure.
137  */
138 int __init ubifs_compressors_init(void)
139 {
140         int err;
141
142         err = compr_init(&lzo_compr);
143         if (err)
144                 return err;
145
146         err = compr_init(&zlib_compr);
147         if (err)
148                 return err;
149
150         ubifs_compressors[UBIFS_COMPR_NONE] = &none_compr;
151         return 0;
152 }
153
154 /*
155  * ubifsls...
156  */
157
158 static int filldir(struct ubifs_info *c, const char *name, int namlen,
159                    u64 ino, unsigned int d_type)
160 {
161         struct inode *inode;
162         char filetime[32];
163
164         switch (d_type) {
165         case UBIFS_ITYPE_REG:
166                 printf("\t");
167                 break;
168         case UBIFS_ITYPE_DIR:
169                 printf("<DIR>\t");
170                 break;
171         case UBIFS_ITYPE_LNK:
172                 printf("<LNK>\t");
173                 break;
174         default:
175                 printf("other\t");
176                 break;
177         }
178
179         inode = ubifs_iget(c->vfs_sb, ino);
180         if (IS_ERR(inode)) {
181                 printf("%s: Error in ubifs_iget(), ino=%lld ret=%p!\n",
182                        __func__, ino, inode);
183                 return -1;
184         }
185         ctime_r((time_t *)&inode->i_mtime, filetime);
186         printf("%9lld  %24.24s  ", inode->i_size, filetime);
187         ubifs_iput(inode);
188
189         printf("%s\n", name);
190
191         return 0;
192 }
193
194 static int ubifs_printdir(struct file *file, void *dirent)
195 {
196         int err, over = 0;
197         struct qstr nm;
198         union ubifs_key key;
199         struct ubifs_dent_node *dent;
200         struct inode *dir = file->f_path.dentry->d_inode;
201         struct ubifs_info *c = dir->i_sb->s_fs_info;
202
203         dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
204
205         if (file->f_pos > UBIFS_S_KEY_HASH_MASK || file->f_pos == 2)
206                 /*
207                  * The directory was seek'ed to a senseless position or there
208                  * are no more entries.
209                  */
210                 return 0;
211
212         if (file->f_pos == 1) {
213                 /* Find the first entry in TNC and save it */
214                 lowest_dent_key(c, &key, dir->i_ino);
215                 nm.name = NULL;
216                 dent = ubifs_tnc_next_ent(c, &key, &nm);
217                 if (IS_ERR(dent)) {
218                         err = PTR_ERR(dent);
219                         goto out;
220                 }
221
222                 file->f_pos = key_hash_flash(c, &dent->key);
223                 file->private_data = dent;
224         }
225
226         dent = file->private_data;
227         if (!dent) {
228                 /*
229                  * The directory was seek'ed to and is now readdir'ed.
230                  * Find the entry corresponding to @file->f_pos or the
231                  * closest one.
232                  */
233                 dent_key_init_hash(c, &key, dir->i_ino, file->f_pos);
234                 nm.name = NULL;
235                 dent = ubifs_tnc_next_ent(c, &key, &nm);
236                 if (IS_ERR(dent)) {
237                         err = PTR_ERR(dent);
238                         goto out;
239                 }
240                 file->f_pos = key_hash_flash(c, &dent->key);
241                 file->private_data = dent;
242         }
243
244         while (1) {
245                 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
246                         dent->name, (unsigned long long)le64_to_cpu(dent->inum),
247                         key_hash_flash(c, &dent->key));
248                 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
249
250                 nm.len = le16_to_cpu(dent->nlen);
251                 over = filldir(c, (char *)dent->name, nm.len,
252                                le64_to_cpu(dent->inum), dent->type);
253                 if (over)
254                         return 0;
255
256                 /* Switch to the next entry */
257                 key_read(c, &dent->key, &key);
258                 nm.name = (char *)dent->name;
259                 dent = ubifs_tnc_next_ent(c, &key, &nm);
260                 if (IS_ERR(dent)) {
261                         err = PTR_ERR(dent);
262                         goto out;
263                 }
264
265                 kfree(file->private_data);
266                 file->f_pos = key_hash_flash(c, &dent->key);
267                 file->private_data = dent;
268                 cond_resched();
269         }
270
271 out:
272         if (err != -ENOENT) {
273                 ubifs_err("cannot find next direntry, error %d", err);
274                 return err;
275         }
276
277         kfree(file->private_data);
278         file->private_data = NULL;
279         file->f_pos = 2;
280         return 0;
281 }
282
283 static int ubifs_finddir(struct super_block *sb, char *dirname,
284                          unsigned long root_inum, unsigned long *inum)
285 {
286         int err;
287         struct qstr nm;
288         union ubifs_key key;
289         struct ubifs_dent_node *dent;
290         struct ubifs_info *c;
291         struct file *file;
292         struct dentry *dentry;
293         struct inode *dir;
294
295         file = kzalloc(sizeof(struct file), 0);
296         dentry = kzalloc(sizeof(struct dentry), 0);
297         dir = kzalloc(sizeof(struct inode), 0);
298         if (!file || !dentry || !dir) {
299                 printf("%s: Error, no memory for malloc!\n", __func__);
300                 err = -ENOMEM;
301                 goto out;
302         }
303
304         dir->i_sb = sb;
305         file->f_path.dentry = dentry;
306         file->f_path.dentry->d_parent = dentry;
307         file->f_path.dentry->d_inode = dir;
308         file->f_path.dentry->d_inode->i_ino = root_inum;
309         c = sb->s_fs_info;
310
311         dbg_gen("dir ino %lu, f_pos %#llx", dir->i_ino, file->f_pos);
312
313         /* Find the first entry in TNC and save it */
314         lowest_dent_key(c, &key, dir->i_ino);
315         nm.name = NULL;
316         dent = ubifs_tnc_next_ent(c, &key, &nm);
317         if (IS_ERR(dent)) {
318                 err = PTR_ERR(dent);
319                 goto out;
320         }
321
322         file->f_pos = key_hash_flash(c, &dent->key);
323         file->private_data = dent;
324
325         while (1) {
326                 dbg_gen("feed '%s', ino %llu, new f_pos %#x",
327                         dent->name, (unsigned long long)le64_to_cpu(dent->inum),
328                         key_hash_flash(c, &dent->key));
329                 ubifs_assert(le64_to_cpu(dent->ch.sqnum) > ubifs_inode(dir)->creat_sqnum);
330
331                 nm.len = le16_to_cpu(dent->nlen);
332                 if ((strncmp(dirname, (char *)dent->name, nm.len) == 0) &&
333                     (strlen(dirname) == nm.len)) {
334                         *inum = le64_to_cpu(dent->inum);
335                         return 1;
336                 }
337
338                 /* Switch to the next entry */
339                 key_read(c, &dent->key, &key);
340                 nm.name = (char *)dent->name;
341                 dent = ubifs_tnc_next_ent(c, &key, &nm);
342                 if (IS_ERR(dent)) {
343                         err = PTR_ERR(dent);
344                         goto out;
345                 }
346
347                 kfree(file->private_data);
348                 file->f_pos = key_hash_flash(c, &dent->key);
349                 file->private_data = dent;
350                 cond_resched();
351         }
352
353 out:
354         if (err != -ENOENT) {
355                 ubifs_err("cannot find next direntry, error %d", err);
356                 return err;
357         }
358
359         if (file)
360                 free(file);
361         if (dentry)
362                 free(dentry);
363         if (dir)
364                 free(dir);
365
366         if (file->private_data)
367                 kfree(file->private_data);
368         file->private_data = NULL;
369         file->f_pos = 2;
370         return 0;
371 }
372
373 static unsigned long ubifs_findfile(struct super_block *sb, char *filename)
374 {
375         int ret;
376         char *next;
377         char fpath[128];
378         char *name = fpath;
379         unsigned long root_inum = 1;
380         unsigned long inum;
381
382         strcpy(fpath, filename);
383
384         /* Remove all leading slashes */
385         while (*name == '/')
386                 name++;
387
388         /*
389          * Handle root-direcoty ('/')
390          */
391         inum = root_inum;
392         if (!name || *name == '\0')
393                 return inum;
394
395         for (;;) {
396                 /* Extract the actual part from the pathname.  */
397                 next = strchr(name, '/');
398                 if (next) {
399                         /* Remove all leading slashes.  */
400                         while (*next == '/')
401                                 *(next++) = '\0';
402                 }
403
404                 ret = ubifs_finddir(sb, name, root_inum, &inum);
405
406                 /*
407                  * Check if directory with this name exists
408                  */
409
410                 /* Found the node!  */
411                 if (!next || *next == '\0') {
412                         if (ret)
413                                 return inum;
414
415                         break;
416                 }
417
418                 root_inum = inum;
419                 name = next;
420         }
421
422         return 0;
423 }
424
425 int ubifs_ls(char *filename)
426 {
427         struct ubifs_info *c = ubifs_sb->s_fs_info;
428         struct file *file;
429         struct dentry *dentry;
430         struct inode *dir;
431         void *dirent = NULL;
432         unsigned long inum;
433         int ret = 0;
434
435         c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
436         inum = ubifs_findfile(ubifs_sb, filename);
437         if (!inum) {
438                 ret = -1;
439                 goto out;
440         }
441
442         file = kzalloc(sizeof(struct file), 0);
443         dentry = kzalloc(sizeof(struct dentry), 0);
444         dir = kzalloc(sizeof(struct inode), 0);
445         if (!file || !dentry || !dir) {
446                 printf("%s: Error, no memory for malloc!\n", __func__);
447                 ret = -ENOMEM;
448                 goto out_mem;
449         }
450
451         dir->i_sb = ubifs_sb;
452         file->f_path.dentry = dentry;
453         file->f_path.dentry->d_parent = dentry;
454         file->f_path.dentry->d_inode = dir;
455         file->f_path.dentry->d_inode->i_ino = inum;
456         file->f_pos = 1;
457         file->private_data = NULL;
458         ubifs_printdir(file, dirent);
459
460 out_mem:
461         if (file)
462                 free(file);
463         if (dentry)
464                 free(dentry);
465         if (dir)
466                 free(dir);
467
468 out:
469         ubi_close_volume(c->ubi);
470         return ret;
471 }
472
473 /*
474  * ubifsload...
475  */
476
477 /* file.c */
478
479 static inline void *kmap(struct page *page)
480 {
481         return page->addr;
482 }
483
484 static int read_block(struct inode *inode, void *addr, unsigned int block,
485                       struct ubifs_data_node *dn)
486 {
487         struct ubifs_info *c = inode->i_sb->s_fs_info;
488         int err, len, out_len;
489         union ubifs_key key;
490         unsigned int dlen;
491
492         data_key_init(c, &key, inode->i_ino, block);
493         err = ubifs_tnc_lookup(c, &key, dn);
494         if (err) {
495                 if (err == -ENOENT)
496                         /* Not found, so it must be a hole */
497                         memset(addr, 0, UBIFS_BLOCK_SIZE);
498                 return err;
499         }
500
501         ubifs_assert(le64_to_cpu(dn->ch.sqnum) > ubifs_inode(inode)->creat_sqnum);
502
503         len = le32_to_cpu(dn->size);
504         if (len <= 0 || len > UBIFS_BLOCK_SIZE)
505                 goto dump;
506
507         dlen = le32_to_cpu(dn->ch.len) - UBIFS_DATA_NODE_SZ;
508         out_len = UBIFS_BLOCK_SIZE;
509         err = ubifs_decompress(&dn->data, dlen, addr, &out_len,
510                                le16_to_cpu(dn->compr_type));
511         if (err || len != out_len)
512                 goto dump;
513
514         /*
515          * Data length can be less than a full block, even for blocks that are
516          * not the last in the file (e.g., as a result of making a hole and
517          * appending data). Ensure that the remainder is zeroed out.
518          */
519         if (len < UBIFS_BLOCK_SIZE)
520                 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
521
522         return 0;
523
524 dump:
525         ubifs_err("bad data node (block %u, inode %lu)",
526                   block, inode->i_ino);
527         dbg_dump_node(c, dn);
528         return -EINVAL;
529 }
530
531 static int do_readpage(struct ubifs_info *c, struct inode *inode, struct page *page)
532 {
533         void *addr;
534         int err = 0, i;
535         unsigned int block, beyond;
536         struct ubifs_data_node *dn;
537         loff_t i_size = inode->i_size;
538
539         dbg_gen("ino %lu, pg %lu, i_size %lld",
540                 inode->i_ino, page->index, i_size);
541
542         addr = kmap(page);
543
544         block = page->index << UBIFS_BLOCKS_PER_PAGE_SHIFT;
545         beyond = (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
546         if (block >= beyond) {
547                 /* Reading beyond inode */
548                 memset(addr, 0, PAGE_CACHE_SIZE);
549                 goto out;
550         }
551
552         dn = kmalloc(UBIFS_MAX_DATA_NODE_SZ, GFP_NOFS);
553         if (!dn) {
554                 err = -ENOMEM;
555                 goto error;
556         }
557
558         i = 0;
559         while (1) {
560                 int ret;
561
562                 if (block >= beyond) {
563                         /* Reading beyond inode */
564                         err = -ENOENT;
565                         memset(addr, 0, UBIFS_BLOCK_SIZE);
566                 } else {
567                         ret = read_block(inode, addr, block, dn);
568                         if (ret) {
569                                 err = ret;
570                                 if (err != -ENOENT)
571                                         break;
572                         } else if (block + 1 == beyond) {
573                                 int dlen = le32_to_cpu(dn->size);
574                                 int ilen = i_size & (UBIFS_BLOCK_SIZE - 1);
575
576                                 if (ilen && ilen < dlen)
577                                         memset(addr + ilen, 0, dlen - ilen);
578                         }
579                 }
580                 if (++i >= UBIFS_BLOCKS_PER_PAGE)
581                         break;
582                 block += 1;
583                 addr += UBIFS_BLOCK_SIZE;
584         }
585         if (err) {
586                 if (err == -ENOENT) {
587                         /* Not found, so it must be a hole */
588                         dbg_gen("hole");
589                         goto out_free;
590                 }
591                 ubifs_err("cannot read page %lu of inode %lu, error %d",
592                           page->index, inode->i_ino, err);
593                 goto error;
594         }
595
596 out_free:
597         kfree(dn);
598 out:
599         return 0;
600
601 error:
602         kfree(dn);
603         return err;
604 }
605
606 int ubifs_load(char *filename, u32 addr, u32 size)
607 {
608         struct ubifs_info *c = ubifs_sb->s_fs_info;
609         unsigned long inum;
610         struct inode *inode;
611         struct page page;
612         int err = 0;
613         int i;
614         int count;
615         char link_name[64];
616         struct ubifs_inode *ui;
617
618         c->ubi = ubi_open_volume(c->vi.ubi_num, c->vi.vol_id, UBI_READONLY);
619         inum = ubifs_findfile(ubifs_sb, filename);
620         if (!inum) {
621                 err = -1;
622                 goto out;
623         }
624
625         /*
626          * Read file inode
627          */
628         inode = ubifs_iget(ubifs_sb, inum);
629         if (IS_ERR(inode)) {
630                 printf("%s: Error reading inode %ld!\n", __func__, inum);
631                 err = PTR_ERR(inode);
632                 goto out;
633         }
634
635         /*
636          * Check for symbolic link
637          */
638         ui = ubifs_inode(inode);
639         if (((inode->i_mode & S_IFMT) == S_IFLNK) && ui->data_len) {
640                 memcpy(link_name, ui->data, ui->data_len);
641                 printf("%s is linked to %s!\n", filename, link_name);
642                 ubifs_iput(inode);
643
644                 /*
645                  * Now we have the "real" filename, call ubifs_load()
646                  * again (recursive call) to load this file instead
647                  */
648                 return ubifs_load(link_name, addr, size);
649         }
650
651         /*
652          * If no size was specified or if size bigger than filesize
653          * set size to filesize
654          */
655         if ((size == 0) || (size > inode->i_size))
656                 size = inode->i_size;
657
658         count = (size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT;
659         printf("Loading file '%s' to addr 0x%08x with size %d (0x%08x)...\n",
660                filename, addr, size, size);
661
662         page.addr = (void *)addr;
663         page.index = 0;
664         page.inode = inode;
665         for (i = 0; i < count; i++) {
666                 err = do_readpage(c, inode, &page);
667                 if (err)
668                         break;
669
670                 page.addr += PAGE_SIZE;
671                 page.index++;
672         }
673
674         if (err)
675                 printf("Error reading file '%s'\n", filename);
676         else
677                 printf("Done\n");
678
679         ubifs_iput(inode);
680
681 out:
682         ubi_close_volume(c->ubi);
683         return err;
684 }