Back to home page

LXR

 
 

    


0001 /*
0002  * Many of the syscalls used in this file expect some of the arguments
0003  * to be __user pointers not __kernel pointers.  To limit the sparse
0004  * noise, turn off sparse checking for this file.
0005  */
0006 #ifdef __CHECKER__
0007 #undef __CHECKER__
0008 #warning "Sparse checking disabled for this file"
0009 #endif
0010 
0011 #include <linux/init.h>
0012 #include <linux/fs.h>
0013 #include <linux/slab.h>
0014 #include <linux/types.h>
0015 #include <linux/fcntl.h>
0016 #include <linux/delay.h>
0017 #include <linux/string.h>
0018 #include <linux/dirent.h>
0019 #include <linux/syscalls.h>
0020 #include <linux/utime.h>
0021 
0022 static ssize_t __init xwrite(int fd, const char *p, size_t count)
0023 {
0024     ssize_t out = 0;
0025 
0026     /* sys_write only can write MAX_RW_COUNT aka 2G-4K bytes at most */
0027     while (count) {
0028         ssize_t rv = sys_write(fd, p, count);
0029 
0030         if (rv < 0) {
0031             if (rv == -EINTR || rv == -EAGAIN)
0032                 continue;
0033             return out ? out : rv;
0034         } else if (rv == 0)
0035             break;
0036 
0037         p += rv;
0038         out += rv;
0039         count -= rv;
0040     }
0041 
0042     return out;
0043 }
0044 
0045 static __initdata char *message;
0046 static void __init error(char *x)
0047 {
0048     if (!message)
0049         message = x;
0050 }
0051 
0052 /* link hash */
0053 
0054 #define N_ALIGN(len) ((((len) + 1) & ~3) + 2)
0055 
0056 static __initdata struct hash {
0057     int ino, minor, major;
0058     umode_t mode;
0059     struct hash *next;
0060     char name[N_ALIGN(PATH_MAX)];
0061 } *head[32];
0062 
0063 static inline int hash(int major, int minor, int ino)
0064 {
0065     unsigned long tmp = ino + minor + (major << 3);
0066     tmp += tmp >> 5;
0067     return tmp & 31;
0068 }
0069 
0070 static char __init *find_link(int major, int minor, int ino,
0071                   umode_t mode, char *name)
0072 {
0073     struct hash **p, *q;
0074     for (p = head + hash(major, minor, ino); *p; p = &(*p)->next) {
0075         if ((*p)->ino != ino)
0076             continue;
0077         if ((*p)->minor != minor)
0078             continue;
0079         if ((*p)->major != major)
0080             continue;
0081         if (((*p)->mode ^ mode) & S_IFMT)
0082             continue;
0083         return (*p)->name;
0084     }
0085     q = kmalloc(sizeof(struct hash), GFP_KERNEL);
0086     if (!q)
0087         panic("can't allocate link hash entry");
0088     q->major = major;
0089     q->minor = minor;
0090     q->ino = ino;
0091     q->mode = mode;
0092     strcpy(q->name, name);
0093     q->next = NULL;
0094     *p = q;
0095     return NULL;
0096 }
0097 
0098 static void __init free_hash(void)
0099 {
0100     struct hash **p, *q;
0101     for (p = head; p < head + 32; p++) {
0102         while (*p) {
0103             q = *p;
0104             *p = q->next;
0105             kfree(q);
0106         }
0107     }
0108 }
0109 
0110 static long __init do_utime(char *filename, time_t mtime)
0111 {
0112     struct timespec t[2];
0113 
0114     t[0].tv_sec = mtime;
0115     t[0].tv_nsec = 0;
0116     t[1].tv_sec = mtime;
0117     t[1].tv_nsec = 0;
0118 
0119     return do_utimes(AT_FDCWD, filename, t, AT_SYMLINK_NOFOLLOW);
0120 }
0121 
0122 static __initdata LIST_HEAD(dir_list);
0123 struct dir_entry {
0124     struct list_head list;
0125     char *name;
0126     time_t mtime;
0127 };
0128 
0129 static void __init dir_add(const char *name, time_t mtime)
0130 {
0131     struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
0132     if (!de)
0133         panic("can't allocate dir_entry buffer");
0134     INIT_LIST_HEAD(&de->list);
0135     de->name = kstrdup(name, GFP_KERNEL);
0136     de->mtime = mtime;
0137     list_add(&de->list, &dir_list);
0138 }
0139 
0140 static void __init dir_utime(void)
0141 {
0142     struct dir_entry *de, *tmp;
0143     list_for_each_entry_safe(de, tmp, &dir_list, list) {
0144         list_del(&de->list);
0145         do_utime(de->name, de->mtime);
0146         kfree(de->name);
0147         kfree(de);
0148     }
0149 }
0150 
0151 static __initdata time_t mtime;
0152 
0153 /* cpio header parsing */
0154 
0155 static __initdata unsigned long ino, major, minor, nlink;
0156 static __initdata umode_t mode;
0157 static __initdata unsigned long body_len, name_len;
0158 static __initdata uid_t uid;
0159 static __initdata gid_t gid;
0160 static __initdata unsigned rdev;
0161 
0162 static void __init parse_header(char *s)
0163 {
0164     unsigned long parsed[12];
0165     char buf[9];
0166     int i;
0167 
0168     buf[8] = '\0';
0169     for (i = 0, s += 6; i < 12; i++, s += 8) {
0170         memcpy(buf, s, 8);
0171         parsed[i] = simple_strtoul(buf, NULL, 16);
0172     }
0173     ino = parsed[0];
0174     mode = parsed[1];
0175     uid = parsed[2];
0176     gid = parsed[3];
0177     nlink = parsed[4];
0178     mtime = parsed[5];
0179     body_len = parsed[6];
0180     major = parsed[7];
0181     minor = parsed[8];
0182     rdev = new_encode_dev(MKDEV(parsed[9], parsed[10]));
0183     name_len = parsed[11];
0184 }
0185 
0186 /* FSM */
0187 
0188 static __initdata enum state {
0189     Start,
0190     Collect,
0191     GotHeader,
0192     SkipIt,
0193     GotName,
0194     CopyFile,
0195     GotSymlink,
0196     Reset
0197 } state, next_state;
0198 
0199 static __initdata char *victim;
0200 static unsigned long byte_count __initdata;
0201 static __initdata loff_t this_header, next_header;
0202 
0203 static inline void __init eat(unsigned n)
0204 {
0205     victim += n;
0206     this_header += n;
0207     byte_count -= n;
0208 }
0209 
0210 static __initdata char *vcollected;
0211 static __initdata char *collected;
0212 static long remains __initdata;
0213 static __initdata char *collect;
0214 
0215 static void __init read_into(char *buf, unsigned size, enum state next)
0216 {
0217     if (byte_count >= size) {
0218         collected = victim;
0219         eat(size);
0220         state = next;
0221     } else {
0222         collect = collected = buf;
0223         remains = size;
0224         next_state = next;
0225         state = Collect;
0226     }
0227 }
0228 
0229 static __initdata char *header_buf, *symlink_buf, *name_buf;
0230 
0231 static int __init do_start(void)
0232 {
0233     read_into(header_buf, 110, GotHeader);
0234     return 0;
0235 }
0236 
0237 static int __init do_collect(void)
0238 {
0239     unsigned long n = remains;
0240     if (byte_count < n)
0241         n = byte_count;
0242     memcpy(collect, victim, n);
0243     eat(n);
0244     collect += n;
0245     if ((remains -= n) != 0)
0246         return 1;
0247     state = next_state;
0248     return 0;
0249 }
0250 
0251 static int __init do_header(void)
0252 {
0253     if (memcmp(collected, "070707", 6)==0) {
0254         error("incorrect cpio method used: use -H newc option");
0255         return 1;
0256     }
0257     if (memcmp(collected, "070701", 6)) {
0258         error("no cpio magic");
0259         return 1;
0260     }
0261     parse_header(collected);
0262     next_header = this_header + N_ALIGN(name_len) + body_len;
0263     next_header = (next_header + 3) & ~3;
0264     state = SkipIt;
0265     if (name_len <= 0 || name_len > PATH_MAX)
0266         return 0;
0267     if (S_ISLNK(mode)) {
0268         if (body_len > PATH_MAX)
0269             return 0;
0270         collect = collected = symlink_buf;
0271         remains = N_ALIGN(name_len) + body_len;
0272         next_state = GotSymlink;
0273         state = Collect;
0274         return 0;
0275     }
0276     if (S_ISREG(mode) || !body_len)
0277         read_into(name_buf, N_ALIGN(name_len), GotName);
0278     return 0;
0279 }
0280 
0281 static int __init do_skip(void)
0282 {
0283     if (this_header + byte_count < next_header) {
0284         eat(byte_count);
0285         return 1;
0286     } else {
0287         eat(next_header - this_header);
0288         state = next_state;
0289         return 0;
0290     }
0291 }
0292 
0293 static int __init do_reset(void)
0294 {
0295     while (byte_count && *victim == '\0')
0296         eat(1);
0297     if (byte_count && (this_header & 3))
0298         error("broken padding");
0299     return 1;
0300 }
0301 
0302 static int __init maybe_link(void)
0303 {
0304     if (nlink >= 2) {
0305         char *old = find_link(major, minor, ino, mode, collected);
0306         if (old)
0307             return (sys_link(old, collected) < 0) ? -1 : 1;
0308     }
0309     return 0;
0310 }
0311 
0312 static void __init clean_path(char *path, umode_t fmode)
0313 {
0314     struct stat st;
0315 
0316     if (!sys_newlstat(path, &st) && (st.st_mode ^ fmode) & S_IFMT) {
0317         if (S_ISDIR(st.st_mode))
0318             sys_rmdir(path);
0319         else
0320             sys_unlink(path);
0321     }
0322 }
0323 
0324 static __initdata int wfd;
0325 
0326 static int __init do_name(void)
0327 {
0328     state = SkipIt;
0329     next_state = Reset;
0330     if (strcmp(collected, "TRAILER!!!") == 0) {
0331         free_hash();
0332         return 0;
0333     }
0334     clean_path(collected, mode);
0335     if (S_ISREG(mode)) {
0336         int ml = maybe_link();
0337         if (ml >= 0) {
0338             int openflags = O_WRONLY|O_CREAT;
0339             if (ml != 1)
0340                 openflags |= O_TRUNC;
0341             wfd = sys_open(collected, openflags, mode);
0342 
0343             if (wfd >= 0) {
0344                 sys_fchown(wfd, uid, gid);
0345                 sys_fchmod(wfd, mode);
0346                 if (body_len)
0347                     sys_ftruncate(wfd, body_len);
0348                 vcollected = kstrdup(collected, GFP_KERNEL);
0349                 state = CopyFile;
0350             }
0351         }
0352     } else if (S_ISDIR(mode)) {
0353         sys_mkdir(collected, mode);
0354         sys_chown(collected, uid, gid);
0355         sys_chmod(collected, mode);
0356         dir_add(collected, mtime);
0357     } else if (S_ISBLK(mode) || S_ISCHR(mode) ||
0358            S_ISFIFO(mode) || S_ISSOCK(mode)) {
0359         if (maybe_link() == 0) {
0360             sys_mknod(collected, mode, rdev);
0361             sys_chown(collected, uid, gid);
0362             sys_chmod(collected, mode);
0363             do_utime(collected, mtime);
0364         }
0365     }
0366     return 0;
0367 }
0368 
0369 static int __init do_copy(void)
0370 {
0371     if (byte_count >= body_len) {
0372         if (xwrite(wfd, victim, body_len) != body_len)
0373             error("write error");
0374         sys_close(wfd);
0375         do_utime(vcollected, mtime);
0376         kfree(vcollected);
0377         eat(body_len);
0378         state = SkipIt;
0379         return 0;
0380     } else {
0381         if (xwrite(wfd, victim, byte_count) != byte_count)
0382             error("write error");
0383         body_len -= byte_count;
0384         eat(byte_count);
0385         return 1;
0386     }
0387 }
0388 
0389 static int __init do_symlink(void)
0390 {
0391     collected[N_ALIGN(name_len) + body_len] = '\0';
0392     clean_path(collected, 0);
0393     sys_symlink(collected + N_ALIGN(name_len), collected);
0394     sys_lchown(collected, uid, gid);
0395     do_utime(collected, mtime);
0396     state = SkipIt;
0397     next_state = Reset;
0398     return 0;
0399 }
0400 
0401 static __initdata int (*actions[])(void) = {
0402     [Start]     = do_start,
0403     [Collect]   = do_collect,
0404     [GotHeader] = do_header,
0405     [SkipIt]    = do_skip,
0406     [GotName]   = do_name,
0407     [CopyFile]  = do_copy,
0408     [GotSymlink]    = do_symlink,
0409     [Reset]     = do_reset,
0410 };
0411 
0412 static long __init write_buffer(char *buf, unsigned long len)
0413 {
0414     byte_count = len;
0415     victim = buf;
0416 
0417     while (!actions[state]())
0418         ;
0419     return len - byte_count;
0420 }
0421 
0422 static long __init flush_buffer(void *bufv, unsigned long len)
0423 {
0424     char *buf = (char *) bufv;
0425     long written;
0426     long origLen = len;
0427     if (message)
0428         return -1;
0429     while ((written = write_buffer(buf, len)) < len && !message) {
0430         char c = buf[written];
0431         if (c == '0') {
0432             buf += written;
0433             len -= written;
0434             state = Start;
0435         } else if (c == 0) {
0436             buf += written;
0437             len -= written;
0438             state = Reset;
0439         } else
0440             error("junk in compressed archive");
0441     }
0442     return origLen;
0443 }
0444 
0445 static unsigned long my_inptr; /* index of next byte to be processed in inbuf */
0446 
0447 #include <linux/decompress/generic.h>
0448 
0449 static char * __init unpack_to_rootfs(char *buf, unsigned long len)
0450 {
0451     long written;
0452     decompress_fn decompress;
0453     const char *compress_name;
0454     static __initdata char msg_buf[64];
0455 
0456     header_buf = kmalloc(110, GFP_KERNEL);
0457     symlink_buf = kmalloc(PATH_MAX + N_ALIGN(PATH_MAX) + 1, GFP_KERNEL);
0458     name_buf = kmalloc(N_ALIGN(PATH_MAX), GFP_KERNEL);
0459 
0460     if (!header_buf || !symlink_buf || !name_buf)
0461         panic("can't allocate buffers");
0462 
0463     state = Start;
0464     this_header = 0;
0465     message = NULL;
0466     while (!message && len) {
0467         loff_t saved_offset = this_header;
0468         if (*buf == '0' && !(this_header & 3)) {
0469             state = Start;
0470             written = write_buffer(buf, len);
0471             buf += written;
0472             len -= written;
0473             continue;
0474         }
0475         if (!*buf) {
0476             buf++;
0477             len--;
0478             this_header++;
0479             continue;
0480         }
0481         this_header = 0;
0482         decompress = decompress_method(buf, len, &compress_name);
0483         pr_debug("Detected %s compressed data\n", compress_name);
0484         if (decompress) {
0485             int res = decompress(buf, len, NULL, flush_buffer, NULL,
0486                    &my_inptr, error);
0487             if (res)
0488                 error("decompressor failed");
0489         } else if (compress_name) {
0490             if (!message) {
0491                 snprintf(msg_buf, sizeof msg_buf,
0492                      "compression method %s not configured",
0493                      compress_name);
0494                 message = msg_buf;
0495             }
0496         } else
0497             error("junk in compressed archive");
0498         if (state != Reset)
0499             error("junk in compressed archive");
0500         this_header = saved_offset + my_inptr;
0501         buf += my_inptr;
0502         len -= my_inptr;
0503     }
0504     dir_utime();
0505     kfree(name_buf);
0506     kfree(symlink_buf);
0507     kfree(header_buf);
0508     return message;
0509 }
0510 
0511 static int __initdata do_retain_initrd;
0512 
0513 static int __init retain_initrd_param(char *str)
0514 {
0515     if (*str)
0516         return 0;
0517     do_retain_initrd = 1;
0518     return 1;
0519 }
0520 __setup("retain_initrd", retain_initrd_param);
0521 
0522 extern char __initramfs_start[];
0523 extern unsigned long __initramfs_size;
0524 #include <linux/initrd.h>
0525 #include <linux/kexec.h>
0526 
0527 static void __init free_initrd(void)
0528 {
0529 #ifdef CONFIG_KEXEC_CORE
0530     unsigned long crashk_start = (unsigned long)__va(crashk_res.start);
0531     unsigned long crashk_end   = (unsigned long)__va(crashk_res.end);
0532 #endif
0533     if (do_retain_initrd)
0534         goto skip;
0535 
0536 #ifdef CONFIG_KEXEC_CORE
0537     /*
0538      * If the initrd region is overlapped with crashkernel reserved region,
0539      * free only memory that is not part of crashkernel region.
0540      */
0541     if (initrd_start < crashk_end && initrd_end > crashk_start) {
0542         /*
0543          * Initialize initrd memory region since the kexec boot does
0544          * not do.
0545          */
0546         memset((void *)initrd_start, 0, initrd_end - initrd_start);
0547         if (initrd_start < crashk_start)
0548             free_initrd_mem(initrd_start, crashk_start);
0549         if (initrd_end > crashk_end)
0550             free_initrd_mem(crashk_end, initrd_end);
0551     } else
0552 #endif
0553         free_initrd_mem(initrd_start, initrd_end);
0554 skip:
0555     initrd_start = 0;
0556     initrd_end = 0;
0557 }
0558 
0559 #ifdef CONFIG_BLK_DEV_RAM
0560 #define BUF_SIZE 1024
0561 static void __init clean_rootfs(void)
0562 {
0563     int fd;
0564     void *buf;
0565     struct linux_dirent64 *dirp;
0566     int num;
0567 
0568     fd = sys_open("/", O_RDONLY, 0);
0569     WARN_ON(fd < 0);
0570     if (fd < 0)
0571         return;
0572     buf = kzalloc(BUF_SIZE, GFP_KERNEL);
0573     WARN_ON(!buf);
0574     if (!buf) {
0575         sys_close(fd);
0576         return;
0577     }
0578 
0579     dirp = buf;
0580     num = sys_getdents64(fd, dirp, BUF_SIZE);
0581     while (num > 0) {
0582         while (num > 0) {
0583             struct stat st;
0584             int ret;
0585 
0586             ret = sys_newlstat(dirp->d_name, &st);
0587             WARN_ON_ONCE(ret);
0588             if (!ret) {
0589                 if (S_ISDIR(st.st_mode))
0590                     sys_rmdir(dirp->d_name);
0591                 else
0592                     sys_unlink(dirp->d_name);
0593             }
0594 
0595             num -= dirp->d_reclen;
0596             dirp = (void *)dirp + dirp->d_reclen;
0597         }
0598         dirp = buf;
0599         memset(buf, 0, BUF_SIZE);
0600         num = sys_getdents64(fd, dirp, BUF_SIZE);
0601     }
0602 
0603     sys_close(fd);
0604     kfree(buf);
0605 }
0606 #endif
0607 
0608 static int __init populate_rootfs(void)
0609 {
0610     char *err = unpack_to_rootfs(__initramfs_start, __initramfs_size);
0611     if (err)
0612         panic("%s", err); /* Failed to decompress INTERNAL initramfs */
0613     if (initrd_start) {
0614 #ifdef CONFIG_BLK_DEV_RAM
0615         int fd;
0616         printk(KERN_INFO "Trying to unpack rootfs image as initramfs...\n");
0617         err = unpack_to_rootfs((char *)initrd_start,
0618             initrd_end - initrd_start);
0619         if (!err) {
0620             free_initrd();
0621             goto done;
0622         } else {
0623             clean_rootfs();
0624             unpack_to_rootfs(__initramfs_start, __initramfs_size);
0625         }
0626         printk(KERN_INFO "rootfs image is not initramfs (%s)"
0627                 "; looks like an initrd\n", err);
0628         fd = sys_open("/initrd.image",
0629                   O_WRONLY|O_CREAT, 0700);
0630         if (fd >= 0) {
0631             ssize_t written = xwrite(fd, (char *)initrd_start,
0632                         initrd_end - initrd_start);
0633 
0634             if (written != initrd_end - initrd_start)
0635                 pr_err("/initrd.image: incomplete write (%zd != %ld)\n",
0636                        written, initrd_end - initrd_start);
0637 
0638             sys_close(fd);
0639             free_initrd();
0640         }
0641     done:
0642 #else
0643         printk(KERN_INFO "Unpacking initramfs...\n");
0644         err = unpack_to_rootfs((char *)initrd_start,
0645             initrd_end - initrd_start);
0646         if (err)
0647             printk(KERN_EMERG "Initramfs unpacking failed: %s\n", err);
0648         free_initrd();
0649 #endif
0650         /*
0651          * Try loading default modules from initramfs.  This gives
0652          * us a chance to load before device_initcalls.
0653          */
0654         load_default_modules();
0655     }
0656     return 0;
0657 }
0658 rootfs_initcall(populate_rootfs);