diff --git a/stand/libsa/tftp.c b/stand/libsa/tftp.c --- a/stand/libsa/tftp.c +++ b/stand/libsa/tftp.c @@ -73,6 +73,7 @@ static off_t tftp_seek(struct open_file *, off_t, int); static int tftp_set_blksize(struct tftp_handle *, const char *); static int tftp_stat(struct open_file *, struct stat *); +static int tftp_preload(struct open_file *); struct fs_ops tftp_fsops = { .fs_name = "tftp", @@ -82,6 +83,7 @@ .fo_write = null_write, .fo_seek = tftp_seek, .fo_stat = tftp_stat, + .fo_preload = tftp_preload, .fo_readdir = null_readdir }; @@ -521,6 +523,16 @@ size = tftpfile->tftp_tsize - tftpfile->off; } + if (tftpfile->tftp_cache != NULL) { + bcopy(tftpfile->tftp_cache + tftpfile->off, + addr, size); + + addr = (char *)addr + size; + tftpfile->off += size; + res -= size; + goto out; + } + while (size > 0) { int needblock, count; @@ -586,6 +598,7 @@ } +out: if (resid != NULL) *resid = res; return (rc); @@ -603,6 +616,7 @@ if (tftpfile) { free(tftpfile->path); free(tftpfile->pkt); + free(tftpfile->tftp_cache); free(tftpfile); } is_open = 0; @@ -643,6 +657,52 @@ return (tftpfile->off); } +static int +tftp_preload(struct open_file *f) +{ + struct tftp_handle *tftpfile; + char *cache; + int rc; +#ifdef TFTP_DEBUG + time_t start, end; +#endif + + tftpfile = f->f_fsdata; + cache = malloc(sizeof(char) * tftpfile->tftp_tsize); + if (cache == NULL) { + printf("Couldn't allocate %ju bytes for preload caching" + ", disabling caching\n", + (uintmax_t)sizeof(char) * tftpfile->tftp_tsize); + return (-1); + } + +#ifdef TFTP_DEBUG + start = getsecs(); + printf("Preloading %s ", tftpfile->path); +#endif + while (tftpfile->islastblock == 0) { + twiddle(32); + rc = tftp_getnextblock(tftpfile); + if (rc) { + free(cache); + printf("Got TFTP error %d, disabling caching\n", rc); + return (rc); + } + bcopy(tftpfile->tftp_hdr->th_data, + cache + (tftpfile->tftp_blksize * (tftpfile->currblock - 1)), + tftpfile->validsize); + } +#ifdef TFTP_DEBUG + end = getsecs(); + printf("\nPreloaded %s (%ju bytes) during %jd seconds\n", + tftpfile->path, (intmax_t)tftpfile->tftp_tsize, + (intmax_t)end - start); +#endif + + tftpfile->tftp_cache = cache; + return (0); +} + static int tftp_set_blksize(struct tftp_handle *h, const char *str) {