embox

Форк
0
380 строк · 9.1 Кб
1
/*
2
 * JFFS2 -- Journalling Flash File System, Version 2.
3
 *
4
 * Copyright (C) 2001-2003 Free Software Foundation, Inc.
5
 *
6
 * Created by David Woodhouse <dwmw2@cambridge.redhat.com>
7
 *
8
 * For licensing information, see the file 'LICENCE' in this directory.
9
 *
10
 * $Id: dir-ecos.c,v 1.11 2005/02/08 19:36:27 lunn Exp $
11
 *
12
 */
13

14
#include <dirent.h>
15
#include <stddef.h>
16
#include <errno.h>
17
#include <string.h>
18

19
#include <linux/kernel.h>
20
#include <linux/crc32.h>
21
#include "nodelist.h"
22

23
/* Takes length argument because it can be either NUL-terminated or '/'-terminated */
24
struct _inode *jffs2_lookup(struct _inode *dir_i,
25
			const unsigned char *d_name, int namelen) {
26
	struct jffs2_inode_info *dir_f;
27
	struct jffs2_full_dirent *fd = NULL, *fd_list;
28
	uint32_t ino = 0;
29
	uint32_t hash = full_name_hash(d_name, namelen);
30
	struct _inode *inode = NULL;
31

32
	D1(printk("jffs2_lookup()\n"));
33

34
	dir_f = JFFS2_INODE_INFO(dir_i);
35

36
	down(&dir_f->sem);
37

38
	/* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
39
	for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= hash;
40
											fd_list = fd_list->next) {
41
		if (fd_list->nhash == hash &&
42
		    (!fd || fd_list->version > fd->version) &&
43
		    strlen((char *)fd_list->name) == namelen &&
44
		    !strncmp((char *)fd_list->name, (char *)d_name, namelen)) {
45
			fd = fd_list;
46
		}
47
	}
48
	if (fd) {
49
		ino = fd->ino;
50
	}
51
	up(&dir_f->sem);
52
	if (ino) {
53
		inode = jffs2_iget(dir_i->i_sb, ino);
54
		if (IS_ERR(inode)) {
55
			printk("jffs2_iget() failed for ino #%u\n", ino);
56
			return inode;
57
		}
58
	}
59

60
	return inode;
61
}
62

63
int jffs2_create(struct _inode *dir_i, const unsigned char *d_name,
64
									int mode, struct _inode **new_i) {
65
	struct jffs2_raw_inode *ri;
66
	struct jffs2_inode_info *f, *dir_f;
67
	struct jffs2_sb_info *c;
68
	struct _inode *inode;
69
	int ret;
70

71
	ri = jffs2_alloc_raw_inode();
72
	if (!ri) {
73
		return -ENOMEM;
74
	}
75

76
	c = &dir_i->i_sb->jffs2_sb;
77

78
	D1(printk( "jffs2_create()\n"));
79

80
	inode = jffs2_new_inode(dir_i, mode, ri);
81

82
	if (IS_ERR(inode)) {
83
		D1(printk( "jffs2_new_inode() failed\n"));
84
		jffs2_free_raw_inode(ri);
85
		return PTR_ERR(inode);
86
	}
87

88
	f = JFFS2_INODE_INFO(inode);
89
	dir_f = JFFS2_INODE_INFO(dir_i);
90

91
	ret = jffs2_do_create(c, dir_f, f, ri,
92
			      (const char *)d_name,
93
                              strlen((char *)d_name));
94

95
	if (ret) {
96
		inode->i_nlink = 0;
97
		jffs2_iput(inode);
98
		jffs2_free_raw_inode(ri);
99
		return ret;
100
	}
101

102
	jffs2_free_raw_inode(ri);
103

104
	D1(printk("jffs2_create: Created ino #%d with mode %o, nlink %d(%d)\n",
105
		  inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink));
106
    *new_i = inode;
107
	return 0;
108
}
109

110

111

112

113
int jffs2_unlink(struct _inode *dir_i, struct _inode *d_inode,
114
									const unsigned char *d_name) {
115
	struct jffs2_sb_info *c;
116
	struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
117
	struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(d_inode);
118
	int ret;
119

120
	c = &dir_i->i_sb->jffs2_sb;
121

122
	ret = jffs2_do_unlink(c, dir_f, (const char *)d_name,
123
			       strlen((char *)d_name), dead_f);
124
	if (dead_f->inocache) {
125
		d_inode->i_nlink = dead_f->inocache->nlink;
126
	}
127

128
	return ret;
129
}
130

131

132

133
int jffs2_link (struct _inode *old_d_inode,
134
		struct _inode *dir_i, const unsigned char *d_name) {
135
	struct jffs2_sb_info *c;
136
	struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_d_inode);
137
	struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
138
	int ret;
139
	uint8_t type;
140

141
	c = &old_d_inode->i_sb->jffs2_sb;
142

143
	/* XXX: This is ugly */
144
	type = (old_d_inode->i_mode & S_IFMT) >> 12;
145
	if (!type) {
146
		type = DT_REG;
147
	}
148

149
	ret = jffs2_do_link(c, dir_f, f->inocache->ino, type,
150
                            (const char * )d_name, strlen((char *)d_name));
151
	if (!ret) {
152
		down(&f->sem);
153
		old_d_inode->i_nlink = ++f->inocache->nlink;
154
		up(&f->sem);
155
	}
156
	return ret;
157
}
158

159
int jffs2_mkdir (struct _inode *dir_i,
160
		const unsigned char *d_name, int mode) {
161
	struct jffs2_inode_info *f, *dir_f;
162
	struct jffs2_sb_info *c;
163
	struct _inode *inode;
164
	struct jffs2_raw_inode *ri;
165
	struct jffs2_raw_dirent *rd;
166
	struct jffs2_full_dnode *fn;
167
	struct jffs2_full_dirent *fd;
168
	int namelen;
169
	uint32_t alloclen, phys_ofs;
170
	int ret;
171

172
	mode |= S_IFDIR;
173

174
	ri = jffs2_alloc_raw_inode();
175
	if (!ri) {
176
		return -ENOMEM;
177
	}
178
	c = &dir_i->i_sb->jffs2_sb;
179

180
	/* Try to reserve enough space for both node and dirent.
181
	 * Just the node will do for now, though
182
	 */
183
	namelen = strlen((char *)d_name);
184
	ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
185

186
	if (ret) {
187
		jffs2_free_raw_inode(ri);
188
		return ret;
189
	}
190

191
	inode = jffs2_new_inode(dir_i, mode, ri);
192

193
	if (IS_ERR(inode)) {
194
		jffs2_free_raw_inode(ri);
195
		jffs2_complete_reservation(c);
196
		return PTR_ERR(inode);
197
	}
198

199
	f = JFFS2_INODE_INFO(inode);
200

201
	ri->data_crc = cpu_to_je32(0);
202
	ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
203

204
	fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
205

206
	jffs2_free_raw_inode(ri);
207

208
	if (IS_ERR(fn)) {
209
		/* Eeek. Wave bye bye */
210
		up(&f->sem);
211
		jffs2_complete_reservation(c);
212
		inode->i_nlink = 0;
213
		jffs2_iput(inode);
214
		return PTR_ERR(fn);
215
	}
216
	/* No data here. Only a metadata node, which will be
217
	   obsoleted by the first data write
218
	*/
219
	f->metadata = fn;
220
	up(&f->sem);
221

222
	jffs2_complete_reservation(c);
223
	ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
224
	if (ret) {
225
		/* Eep. */
226
		inode->i_nlink = 0;
227
		jffs2_iput(inode);
228
		return ret;
229
	}
230

231
	rd = jffs2_alloc_raw_dirent();
232
	if (!rd) {
233
		/* Argh. Now we treat it like a normal delete */
234
		jffs2_complete_reservation(c);
235
		inode->i_nlink = 0;
236
		jffs2_iput(inode);
237
		return -ENOMEM;
238
	}
239

240
	dir_f = JFFS2_INODE_INFO(dir_i);
241
	down(&dir_f->sem);
242

243
	rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
244
	rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
245
	rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
246
	rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
247

248
	rd->pino = cpu_to_je32(dir_i->i_ino);
249
	rd->version = cpu_to_je32(++dir_f->highest_version);
250
	rd->ino = cpu_to_je32(inode->i_ino);
251
	rd->mctime = cpu_to_je32(timestamp());
252
	rd->nsize = namelen;
253
	rd->type = DT_DIR;
254
	rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
255
	rd->name_crc = cpu_to_je32(crc32(0, d_name, namelen));
256

257
	fd = jffs2_write_dirent(c, dir_f, rd, d_name, namelen, phys_ofs, ALLOC_NORMAL);
258

259
	jffs2_complete_reservation(c);
260
	jffs2_free_raw_dirent(rd);
261

262
	if (IS_ERR(fd)) {
263
		/* dirent failed to write. Delete the inode normally
264
		   as if it were the final unlink() */
265
		up(&dir_f->sem);
266
		inode->i_nlink = 0;
267
		jffs2_iput(inode);
268
		return PTR_ERR(fd);
269
	}
270

271
	/* Link the fd into the inode's list, obsoleting an old
272
	   one if necessary. */
273
	jffs2_add_fd_to_list(c, fd, &dir_f->dents);
274
	up(&dir_f->sem);
275

276
	jffs2_iput(inode);
277
	return 0;
278
}
279

280
int jffs2_rmdir (struct _inode *dir_i,
281
		struct _inode *d_inode, const unsigned char *d_name) {
282
	struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode);
283
	struct jffs2_full_dirent *fd;
284

285
	for (fd = f->dents ; fd; fd = fd->next) {
286
		if (fd->ino) {
287
			return EPERM;
288
		}
289
	}
290
	return jffs2_unlink(dir_i, d_inode, d_name);
291
}
292

293
int jffs2_rename (struct _inode *old_dir_i,
294
		struct _inode *d_inode, const unsigned char *old_d_name,
295
		  struct _inode *new_dir_i, const unsigned char *new_d_name) {
296
	int ret;
297
	struct jffs2_sb_info *c;
298
	struct jffs2_inode_info *victim_f = NULL;
299
	uint8_t type;
300

301
	c = &old_dir_i->i_sb->jffs2_sb;
302

303
#if 0 /* FIXME -- this really doesn't belong in individual file systems.
304
	 The fileio code ought to do this for us, or at least part of it */
305
	if (new_dentry->d_inode) {
306
		if (S_ISDIR(d_inode->i_mode) &&
307
		    !S_ISDIR(new_dentry->d_inode->i_mode)) {
308
			/* Cannot rename directory over non-directory */
309
			return -EINVAL;
310
		}
311

312
		victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
313

314
		if (S_ISDIR(new_dentry->d_inode->i_mode)) {
315
			struct jffs2_full_dirent *fd;
316

317
			if (!S_ISDIR(d_inode->i_mode)) {
318
				/* Cannot rename non-directory over directory */
319
				return -EINVAL;
320
			}
321
			down(&victim_f->sem);
322
			for (fd = victim_f->dents; fd; fd = fd->next) {
323
				if (fd->ino) {
324
					up(&victim_f->sem);
325
					return -ENOTEMPTY;
326
				}
327
			}
328
			up(&victim_f->sem);
329
		}
330
	}
331
#endif
332

333
	/* XXX: We probably ought to alloc enough space for
334
	 * both nodes at the same time. Writing the new link,
335
	 * then getting -ENOSPC, is quite bad :)
336
	 */
337

338
	/* Make a hard link */
339

340
	/* XXX: This is ugly */
341
	type = (d_inode->i_mode & S_IFMT) >> 12;
342
	if (!type) {
343
		type = DT_REG;
344
	}
345

346
	ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i), d_inode->i_ino, type,
347
			    		(const char *)new_d_name, strlen((char *)new_d_name));
348
	if (ret) {
349
		return ret;
350
	}
351

352
	if (victim_f) {
353
		/* There was a victim. Kill it off nicely
354
		 * Don't oops if the victim was a dirent pointing to an
355
		 * inode which didn't exist.
356
		 */
357
		if (victim_f->inocache) {
358
			down(&victim_f->sem);
359
			victim_f->inocache->nlink--;
360
			up(&victim_f->sem);
361
		}
362
	}
363

364
	/* Unlink the original */
365
	ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
366
             (const char *)old_d_name, strlen((char *)old_d_name), NULL);
367

368
	if (ret) {
369
		/* Oh shit. We really ought to make a single node which can do both atomically */
370
		struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode);
371
		down(&f->sem);
372
		if (f->inocache) {
373
			d_inode->i_nlink = f->inocache->nlink++;
374
		}
375
		up(&f->sem);
376

377
		printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
378
	}
379
	return ret;
380
}
381

382

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.