• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * JFFS2 -- Journalling Flash File System, Version 2.
3  *
4  * Copyright © 2001-2007 Red Hat, Inc.
5  *
6  * Created by David Woodhouse <dwmw2@infradead.org>
7  *
8  * For licensing information, see the file 'LICENCE' in this directory.
9  *
10  */
11 
12 #include <linux/kernel.h>
13 #include "mtd_dev.h"
14 #include "nodelist.h"
15 
jffs2_flash_direct_writev(struct jffs2_sb_info * c,const struct kvec * vecs,unsigned long count,loff_t to,size_t * retlen)16 int jffs2_flash_direct_writev(struct jffs2_sb_info *c, const struct kvec *vecs,
17 			      unsigned long count, loff_t to, size_t *retlen)
18 {
19 	unsigned long i;
20 	size_t totlen = 0, thislen;
21 	int ret = 0;
22 
23 	for (i = 0; i < count; i++) {
24 		// writes need to be aligned but the data we're passed may not be
25 		// Observation suggests most unaligned writes are small, so we
26 		// optimize for that case.
27 
28 		if (((vecs[i].iov_len & (sizeof(int) - 1))) ||
29 		(((unsigned long) vecs[i].iov_base & (sizeof(unsigned long) - 1)))) {
30 			// are there iov's after this one? Or is it so much we'd need
31 			// to do multiple writes anyway?
32 			if ((i + 1) < count || vecs[i].iov_len > 256) {
33 				// cop out and malloc
34 				unsigned long j;
35 				size_t sizetomalloc = 0, totvecsize = 0;
36 				char *cbuf, *cbufptr;
37 
38 				for (j = i; j < count; j++)
39 					totvecsize += vecs[j].iov_len;
40 
41 				// pad up in case unaligned
42 				sizetomalloc = totvecsize + sizeof(int) - 1;
43 				sizetomalloc &= ~(sizeof(int) - 1);
44 				cbuf = (char *) malloc(sizetomalloc);
45 				// malloc returns aligned memory
46 				if (!cbuf) {
47 					ret = -ENOMEM;
48 					goto writev_out;
49 				}
50 				cbufptr = cbuf;
51 				for (j = i; j < count; j++) {
52 					(void)memcpy_s(cbufptr, vecs[j].iov_len, vecs[j].iov_base, vecs[j].iov_len);
53 					cbufptr += vecs[j].iov_len;
54 				}
55 				ret = jffs2_flash_write(c, to, sizetomalloc, &thislen,
56 					(unsigned char *) cbuf);
57 				if (thislen > totvecsize) // in case it was aligned up
58 					thislen = totvecsize;
59 				totlen += thislen;
60 				free(cbuf);
61 				goto writev_out;
62 			} else {
63 				// otherwise optimize for the common case
64 				int buf[256/sizeof(int)]; // int, so int aligned
65 				size_t lentowrite;
66 
67 				lentowrite = vecs[i].iov_len;
68 				// pad up in case its unaligned
69 				lentowrite += sizeof(int) - 1;
70 				lentowrite &= ~(sizeof(int) - 1);
71 				ret = memcpy_s(buf, sizeof(buf), vecs[i].iov_base, vecs[i].iov_len);
72 				if (ret != EOK)
73 					goto writev_out;
74 
75 				ret = jffs2_flash_write(c, to, lentowrite, &thislen,
76 					(unsigned char *) &buf[0]);
77 				if (thislen > vecs[i].iov_len)
78 					thislen = vecs[i].iov_len;
79 			}
80 		} else {
81 				ret = jffs2_flash_write(c, to, vecs[i].iov_len, &thislen,
82 					vecs[i].iov_base);
83 		}
84 		totlen += thislen;
85 		if (ret || thislen != vecs[i].iov_len) break;
86 		to += vecs[i].iov_len;
87 	}
88 
89 writev_out:
90 	if (retlen) *retlen = totlen;
91 
92 	return ret;
93 }
94 
jffs2_flash_direct_write(struct jffs2_sb_info * c,loff_t ofs,size_t len,size_t * retlen,const u_char * buf)95 int jffs2_flash_direct_write(struct jffs2_sb_info *c, loff_t ofs, size_t len,
96 			size_t *retlen, const u_char *buf)
97 {
98 	int ret;
99 	ret = c->mtd->write(c->mtd, ofs, len, (char *)buf);
100 	if (ret >= 0) {
101 		*retlen = ret;
102 		return 0;
103 	}
104 	*retlen = 0;
105 	return ret;
106 }
107