dir.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2001-2003 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. * $Id: dir.c,v 1.86 2005/07/06 12:13:09 dwmw2 Exp $
  11. *
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/sched.h>
  16. #include <linux/fs.h>
  17. #include <linux/crc32.h>
  18. #include <linux/jffs2.h>
  19. #include <linux/jffs2_fs_i.h>
  20. #include <linux/jffs2_fs_sb.h>
  21. #include <linux/time.h>
  22. #include "nodelist.h"
  23. static int jffs2_readdir (struct file *, void *, filldir_t);
  24. static int jffs2_create (struct inode *,struct dentry *,int,
  25. struct nameidata *);
  26. static struct dentry *jffs2_lookup (struct inode *,struct dentry *,
  27. struct nameidata *);
  28. static int jffs2_link (struct dentry *,struct inode *,struct dentry *);
  29. static int jffs2_unlink (struct inode *,struct dentry *);
  30. static int jffs2_symlink (struct inode *,struct dentry *,const char *);
  31. static int jffs2_mkdir (struct inode *,struct dentry *,int);
  32. static int jffs2_rmdir (struct inode *,struct dentry *);
  33. static int jffs2_mknod (struct inode *,struct dentry *,int,dev_t);
  34. static int jffs2_rename (struct inode *, struct dentry *,
  35. struct inode *, struct dentry *);
  36. struct file_operations jffs2_dir_operations =
  37. {
  38. .read = generic_read_dir,
  39. .readdir = jffs2_readdir,
  40. .ioctl = jffs2_ioctl,
  41. .fsync = jffs2_fsync
  42. };
  43. struct inode_operations jffs2_dir_inode_operations =
  44. {
  45. .create = jffs2_create,
  46. .lookup = jffs2_lookup,
  47. .link = jffs2_link,
  48. .unlink = jffs2_unlink,
  49. .symlink = jffs2_symlink,
  50. .mkdir = jffs2_mkdir,
  51. .rmdir = jffs2_rmdir,
  52. .mknod = jffs2_mknod,
  53. .rename = jffs2_rename,
  54. .setattr = jffs2_setattr,
  55. };
  56. /***********************************************************************/
  57. /* We keep the dirent list sorted in increasing order of name hash,
  58. and we use the same hash function as the dentries. Makes this
  59. nice and simple
  60. */
  61. static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
  62. struct nameidata *nd)
  63. {
  64. struct jffs2_inode_info *dir_f;
  65. struct jffs2_sb_info *c;
  66. struct jffs2_full_dirent *fd = NULL, *fd_list;
  67. uint32_t ino = 0;
  68. struct inode *inode = NULL;
  69. D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
  70. dir_f = JFFS2_INODE_INFO(dir_i);
  71. c = JFFS2_SB_INFO(dir_i->i_sb);
  72. down(&dir_f->sem);
  73. /* NB: The 2.2 backport will need to explicitly check for '.' and '..' here */
  74. for (fd_list = dir_f->dents; fd_list && fd_list->nhash <= target->d_name.hash; fd_list = fd_list->next) {
  75. if (fd_list->nhash == target->d_name.hash &&
  76. (!fd || fd_list->version > fd->version) &&
  77. strlen(fd_list->name) == target->d_name.len &&
  78. !strncmp(fd_list->name, target->d_name.name, target->d_name.len)) {
  79. fd = fd_list;
  80. }
  81. }
  82. if (fd)
  83. ino = fd->ino;
  84. up(&dir_f->sem);
  85. if (ino) {
  86. inode = iget(dir_i->i_sb, ino);
  87. if (!inode) {
  88. printk(KERN_WARNING "iget() failed for ino #%u\n", ino);
  89. return (ERR_PTR(-EIO));
  90. }
  91. }
  92. d_add(target, inode);
  93. return NULL;
  94. }
  95. /***********************************************************************/
  96. static int jffs2_readdir(struct file *filp, void *dirent, filldir_t filldir)
  97. {
  98. struct jffs2_inode_info *f;
  99. struct jffs2_sb_info *c;
  100. struct inode *inode = filp->f_dentry->d_inode;
  101. struct jffs2_full_dirent *fd;
  102. unsigned long offset, curofs;
  103. D1(printk(KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", filp->f_dentry->d_inode->i_ino));
  104. f = JFFS2_INODE_INFO(inode);
  105. c = JFFS2_SB_INFO(inode->i_sb);
  106. offset = filp->f_pos;
  107. if (offset == 0) {
  108. D1(printk(KERN_DEBUG "Dirent 0: \".\", ino #%lu\n", inode->i_ino));
  109. if (filldir(dirent, ".", 1, 0, inode->i_ino, DT_DIR) < 0)
  110. goto out;
  111. offset++;
  112. }
  113. if (offset == 1) {
  114. unsigned long pino = parent_ino(filp->f_dentry);
  115. D1(printk(KERN_DEBUG "Dirent 1: \"..\", ino #%lu\n", pino));
  116. if (filldir(dirent, "..", 2, 1, pino, DT_DIR) < 0)
  117. goto out;
  118. offset++;
  119. }
  120. curofs=1;
  121. down(&f->sem);
  122. for (fd = f->dents; fd; fd = fd->next) {
  123. curofs++;
  124. /* First loop: curofs = 2; offset = 2 */
  125. if (curofs < offset) {
  126. D2(printk(KERN_DEBUG "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
  127. fd->name, fd->ino, fd->type, curofs, offset));
  128. continue;
  129. }
  130. if (!fd->ino) {
  131. D2(printk(KERN_DEBUG "Skipping deletion dirent \"%s\"\n", fd->name));
  132. offset++;
  133. continue;
  134. }
  135. D2(printk(KERN_DEBUG "Dirent %ld: \"%s\", ino #%u, type %d\n", offset, fd->name, fd->ino, fd->type));
  136. if (filldir(dirent, fd->name, strlen(fd->name), offset, fd->ino, fd->type) < 0)
  137. break;
  138. offset++;
  139. }
  140. up(&f->sem);
  141. out:
  142. filp->f_pos = offset;
  143. return 0;
  144. }
  145. /***********************************************************************/
  146. static int jffs2_create(struct inode *dir_i, struct dentry *dentry, int mode,
  147. struct nameidata *nd)
  148. {
  149. struct jffs2_raw_inode *ri;
  150. struct jffs2_inode_info *f, *dir_f;
  151. struct jffs2_sb_info *c;
  152. struct inode *inode;
  153. int ret;
  154. ri = jffs2_alloc_raw_inode();
  155. if (!ri)
  156. return -ENOMEM;
  157. c = JFFS2_SB_INFO(dir_i->i_sb);
  158. D1(printk(KERN_DEBUG "jffs2_create()\n"));
  159. inode = jffs2_new_inode(dir_i, mode, ri);
  160. if (IS_ERR(inode)) {
  161. D1(printk(KERN_DEBUG "jffs2_new_inode() failed\n"));
  162. jffs2_free_raw_inode(ri);
  163. return PTR_ERR(inode);
  164. }
  165. inode->i_op = &jffs2_file_inode_operations;
  166. inode->i_fop = &jffs2_file_operations;
  167. inode->i_mapping->a_ops = &jffs2_file_address_operations;
  168. inode->i_mapping->nrpages = 0;
  169. f = JFFS2_INODE_INFO(inode);
  170. dir_f = JFFS2_INODE_INFO(dir_i);
  171. ret = jffs2_do_create(c, dir_f, f, ri,
  172. dentry->d_name.name, dentry->d_name.len);
  173. if (ret) {
  174. make_bad_inode(inode);
  175. iput(inode);
  176. jffs2_free_raw_inode(ri);
  177. return ret;
  178. }
  179. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(ri->ctime));
  180. jffs2_free_raw_inode(ri);
  181. d_instantiate(dentry, inode);
  182. D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
  183. inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages));
  184. return 0;
  185. }
  186. /***********************************************************************/
  187. static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
  188. {
  189. struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
  190. struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
  191. struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode);
  192. int ret;
  193. ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
  194. dentry->d_name.len, dead_f);
  195. if (dead_f->inocache)
  196. dentry->d_inode->i_nlink = dead_f->inocache->nlink;
  197. return ret;
  198. }
  199. /***********************************************************************/
  200. static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
  201. {
  202. struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb);
  203. struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
  204. struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
  205. int ret;
  206. uint8_t type;
  207. /* Don't let people make hard links to bad inodes. */
  208. if (!f->inocache)
  209. return -EIO;
  210. if (S_ISDIR(old_dentry->d_inode->i_mode))
  211. return -EPERM;
  212. /* XXX: This is ugly */
  213. type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
  214. if (!type) type = DT_REG;
  215. ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len);
  216. if (!ret) {
  217. down(&f->sem);
  218. old_dentry->d_inode->i_nlink = ++f->inocache->nlink;
  219. up(&f->sem);
  220. d_instantiate(dentry, old_dentry->d_inode);
  221. atomic_inc(&old_dentry->d_inode->i_count);
  222. }
  223. return ret;
  224. }
  225. /***********************************************************************/
  226. static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
  227. {
  228. struct jffs2_inode_info *f, *dir_f;
  229. struct jffs2_sb_info *c;
  230. struct inode *inode;
  231. struct jffs2_raw_inode *ri;
  232. struct jffs2_raw_dirent *rd;
  233. struct jffs2_full_dnode *fn;
  234. struct jffs2_full_dirent *fd;
  235. int namelen;
  236. uint32_t alloclen, phys_ofs;
  237. int ret, targetlen = strlen(target);
  238. /* FIXME: If you care. We'd need to use frags for the target
  239. if it grows much more than this */
  240. if (targetlen > 254)
  241. return -EINVAL;
  242. ri = jffs2_alloc_raw_inode();
  243. if (!ri)
  244. return -ENOMEM;
  245. c = JFFS2_SB_INFO(dir_i->i_sb);
  246. /* Try to reserve enough space for both node and dirent.
  247. * Just the node will do for now, though
  248. */
  249. namelen = dentry->d_name.len;
  250. ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &phys_ofs, &alloclen, ALLOC_NORMAL);
  251. if (ret) {
  252. jffs2_free_raw_inode(ri);
  253. return ret;
  254. }
  255. inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
  256. if (IS_ERR(inode)) {
  257. jffs2_free_raw_inode(ri);
  258. jffs2_complete_reservation(c);
  259. return PTR_ERR(inode);
  260. }
  261. inode->i_op = &jffs2_symlink_inode_operations;
  262. f = JFFS2_INODE_INFO(inode);
  263. inode->i_size = targetlen;
  264. ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
  265. ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
  266. ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
  267. ri->compr = JFFS2_COMPR_NONE;
  268. ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
  269. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  270. fn = jffs2_write_dnode(c, f, ri, target, targetlen, phys_ofs, ALLOC_NORMAL);
  271. jffs2_free_raw_inode(ri);
  272. if (IS_ERR(fn)) {
  273. /* Eeek. Wave bye bye */
  274. up(&f->sem);
  275. jffs2_complete_reservation(c);
  276. jffs2_clear_inode(inode);
  277. return PTR_ERR(fn);
  278. }
  279. /* We use f->dents field to store the target path. */
  280. f->dents = kmalloc(targetlen + 1, GFP_KERNEL);
  281. if (!f->dents) {
  282. printk(KERN_WARNING "Can't allocate %d bytes of memory\n", targetlen + 1);
  283. up(&f->sem);
  284. jffs2_complete_reservation(c);
  285. jffs2_clear_inode(inode);
  286. return -ENOMEM;
  287. }
  288. memcpy(f->dents, target, targetlen + 1);
  289. D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->dents));
  290. /* No data here. Only a metadata node, which will be
  291. obsoleted by the first data write
  292. */
  293. f->metadata = fn;
  294. up(&f->sem);
  295. jffs2_complete_reservation(c);
  296. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
  297. if (ret) {
  298. /* Eep. */
  299. jffs2_clear_inode(inode);
  300. return ret;
  301. }
  302. rd = jffs2_alloc_raw_dirent();
  303. if (!rd) {
  304. /* Argh. Now we treat it like a normal delete */
  305. jffs2_complete_reservation(c);
  306. jffs2_clear_inode(inode);
  307. return -ENOMEM;
  308. }
  309. dir_f = JFFS2_INODE_INFO(dir_i);
  310. down(&dir_f->sem);
  311. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  312. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  313. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  314. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  315. rd->pino = cpu_to_je32(dir_i->i_ino);
  316. rd->version = cpu_to_je32(++dir_f->highest_version);
  317. rd->ino = cpu_to_je32(inode->i_ino);
  318. rd->mctime = cpu_to_je32(get_seconds());
  319. rd->nsize = namelen;
  320. rd->type = DT_LNK;
  321. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  322. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  323. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
  324. if (IS_ERR(fd)) {
  325. /* dirent failed to write. Delete the inode normally
  326. as if it were the final unlink() */
  327. jffs2_complete_reservation(c);
  328. jffs2_free_raw_dirent(rd);
  329. up(&dir_f->sem);
  330. jffs2_clear_inode(inode);
  331. return PTR_ERR(fd);
  332. }
  333. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  334. jffs2_free_raw_dirent(rd);
  335. /* Link the fd into the inode's list, obsoleting an old
  336. one if necessary. */
  337. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  338. up(&dir_f->sem);
  339. jffs2_complete_reservation(c);
  340. d_instantiate(dentry, inode);
  341. return 0;
  342. }
  343. static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
  344. {
  345. struct jffs2_inode_info *f, *dir_f;
  346. struct jffs2_sb_info *c;
  347. struct inode *inode;
  348. struct jffs2_raw_inode *ri;
  349. struct jffs2_raw_dirent *rd;
  350. struct jffs2_full_dnode *fn;
  351. struct jffs2_full_dirent *fd;
  352. int namelen;
  353. uint32_t alloclen, phys_ofs;
  354. int ret;
  355. mode |= S_IFDIR;
  356. ri = jffs2_alloc_raw_inode();
  357. if (!ri)
  358. return -ENOMEM;
  359. c = JFFS2_SB_INFO(dir_i->i_sb);
  360. /* Try to reserve enough space for both node and dirent.
  361. * Just the node will do for now, though
  362. */
  363. namelen = dentry->d_name.len;
  364. ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
  365. if (ret) {
  366. jffs2_free_raw_inode(ri);
  367. return ret;
  368. }
  369. inode = jffs2_new_inode(dir_i, mode, ri);
  370. if (IS_ERR(inode)) {
  371. jffs2_free_raw_inode(ri);
  372. jffs2_complete_reservation(c);
  373. return PTR_ERR(inode);
  374. }
  375. inode->i_op = &jffs2_dir_inode_operations;
  376. inode->i_fop = &jffs2_dir_operations;
  377. /* Directories get nlink 2 at start */
  378. inode->i_nlink = 2;
  379. f = JFFS2_INODE_INFO(inode);
  380. ri->data_crc = cpu_to_je32(0);
  381. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  382. fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
  383. jffs2_free_raw_inode(ri);
  384. if (IS_ERR(fn)) {
  385. /* Eeek. Wave bye bye */
  386. up(&f->sem);
  387. jffs2_complete_reservation(c);
  388. jffs2_clear_inode(inode);
  389. return PTR_ERR(fn);
  390. }
  391. /* No data here. Only a metadata node, which will be
  392. obsoleted by the first data write
  393. */
  394. f->metadata = fn;
  395. up(&f->sem);
  396. jffs2_complete_reservation(c);
  397. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
  398. if (ret) {
  399. /* Eep. */
  400. jffs2_clear_inode(inode);
  401. return ret;
  402. }
  403. rd = jffs2_alloc_raw_dirent();
  404. if (!rd) {
  405. /* Argh. Now we treat it like a normal delete */
  406. jffs2_complete_reservation(c);
  407. jffs2_clear_inode(inode);
  408. return -ENOMEM;
  409. }
  410. dir_f = JFFS2_INODE_INFO(dir_i);
  411. down(&dir_f->sem);
  412. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  413. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  414. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  415. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  416. rd->pino = cpu_to_je32(dir_i->i_ino);
  417. rd->version = cpu_to_je32(++dir_f->highest_version);
  418. rd->ino = cpu_to_je32(inode->i_ino);
  419. rd->mctime = cpu_to_je32(get_seconds());
  420. rd->nsize = namelen;
  421. rd->type = DT_DIR;
  422. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  423. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  424. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
  425. if (IS_ERR(fd)) {
  426. /* dirent failed to write. Delete the inode normally
  427. as if it were the final unlink() */
  428. jffs2_complete_reservation(c);
  429. jffs2_free_raw_dirent(rd);
  430. up(&dir_f->sem);
  431. jffs2_clear_inode(inode);
  432. return PTR_ERR(fd);
  433. }
  434. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  435. dir_i->i_nlink++;
  436. jffs2_free_raw_dirent(rd);
  437. /* Link the fd into the inode's list, obsoleting an old
  438. one if necessary. */
  439. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  440. up(&dir_f->sem);
  441. jffs2_complete_reservation(c);
  442. d_instantiate(dentry, inode);
  443. return 0;
  444. }
  445. static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
  446. {
  447. struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
  448. struct jffs2_full_dirent *fd;
  449. int ret;
  450. for (fd = f->dents ; fd; fd = fd->next) {
  451. if (fd->ino)
  452. return -ENOTEMPTY;
  453. }
  454. ret = jffs2_unlink(dir_i, dentry);
  455. if (!ret)
  456. dir_i->i_nlink--;
  457. return ret;
  458. }
  459. static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, dev_t rdev)
  460. {
  461. struct jffs2_inode_info *f, *dir_f;
  462. struct jffs2_sb_info *c;
  463. struct inode *inode;
  464. struct jffs2_raw_inode *ri;
  465. struct jffs2_raw_dirent *rd;
  466. struct jffs2_full_dnode *fn;
  467. struct jffs2_full_dirent *fd;
  468. int namelen;
  469. jint16_t dev;
  470. int devlen = 0;
  471. uint32_t alloclen, phys_ofs;
  472. int ret;
  473. if (!old_valid_dev(rdev))
  474. return -EINVAL;
  475. ri = jffs2_alloc_raw_inode();
  476. if (!ri)
  477. return -ENOMEM;
  478. c = JFFS2_SB_INFO(dir_i->i_sb);
  479. if (S_ISBLK(mode) || S_ISCHR(mode)) {
  480. dev = cpu_to_je16(old_encode_dev(rdev));
  481. devlen = sizeof(dev);
  482. }
  483. /* Try to reserve enough space for both node and dirent.
  484. * Just the node will do for now, though
  485. */
  486. namelen = dentry->d_name.len;
  487. ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &phys_ofs, &alloclen, ALLOC_NORMAL);
  488. if (ret) {
  489. jffs2_free_raw_inode(ri);
  490. return ret;
  491. }
  492. inode = jffs2_new_inode(dir_i, mode, ri);
  493. if (IS_ERR(inode)) {
  494. jffs2_free_raw_inode(ri);
  495. jffs2_complete_reservation(c);
  496. return PTR_ERR(inode);
  497. }
  498. inode->i_op = &jffs2_file_inode_operations;
  499. init_special_inode(inode, inode->i_mode, rdev);
  500. f = JFFS2_INODE_INFO(inode);
  501. ri->dsize = ri->csize = cpu_to_je32(devlen);
  502. ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
  503. ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
  504. ri->compr = JFFS2_COMPR_NONE;
  505. ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
  506. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  507. fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, phys_ofs, ALLOC_NORMAL);
  508. jffs2_free_raw_inode(ri);
  509. if (IS_ERR(fn)) {
  510. /* Eeek. Wave bye bye */
  511. up(&f->sem);
  512. jffs2_complete_reservation(c);
  513. jffs2_clear_inode(inode);
  514. return PTR_ERR(fn);
  515. }
  516. /* No data here. Only a metadata node, which will be
  517. obsoleted by the first data write
  518. */
  519. f->metadata = fn;
  520. up(&f->sem);
  521. jffs2_complete_reservation(c);
  522. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen, ALLOC_NORMAL);
  523. if (ret) {
  524. /* Eep. */
  525. jffs2_clear_inode(inode);
  526. return ret;
  527. }
  528. rd = jffs2_alloc_raw_dirent();
  529. if (!rd) {
  530. /* Argh. Now we treat it like a normal delete */
  531. jffs2_complete_reservation(c);
  532. jffs2_clear_inode(inode);
  533. return -ENOMEM;
  534. }
  535. dir_f = JFFS2_INODE_INFO(dir_i);
  536. down(&dir_f->sem);
  537. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  538. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  539. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  540. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  541. rd->pino = cpu_to_je32(dir_i->i_ino);
  542. rd->version = cpu_to_je32(++dir_f->highest_version);
  543. rd->ino = cpu_to_je32(inode->i_ino);
  544. rd->mctime = cpu_to_je32(get_seconds());
  545. rd->nsize = namelen;
  546. /* XXX: This is ugly. */
  547. rd->type = (mode & S_IFMT) >> 12;
  548. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  549. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  550. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
  551. if (IS_ERR(fd)) {
  552. /* dirent failed to write. Delete the inode normally
  553. as if it were the final unlink() */
  554. jffs2_complete_reservation(c);
  555. jffs2_free_raw_dirent(rd);
  556. up(&dir_f->sem);
  557. jffs2_clear_inode(inode);
  558. return PTR_ERR(fd);
  559. }
  560. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  561. jffs2_free_raw_dirent(rd);
  562. /* Link the fd into the inode's list, obsoleting an old
  563. one if necessary. */
  564. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  565. up(&dir_f->sem);
  566. jffs2_complete_reservation(c);
  567. d_instantiate(dentry, inode);
  568. return 0;
  569. }
  570. static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
  571. struct inode *new_dir_i, struct dentry *new_dentry)
  572. {
  573. int ret;
  574. struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
  575. struct jffs2_inode_info *victim_f = NULL;
  576. uint8_t type;
  577. /* The VFS will check for us and prevent trying to rename a
  578. * file over a directory and vice versa, but if it's a directory,
  579. * the VFS can't check whether the victim is empty. The filesystem
  580. * needs to do that for itself.
  581. */
  582. if (new_dentry->d_inode) {
  583. victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
  584. if (S_ISDIR(new_dentry->d_inode->i_mode)) {
  585. struct jffs2_full_dirent *fd;
  586. down(&victim_f->sem);
  587. for (fd = victim_f->dents; fd; fd = fd->next) {
  588. if (fd->ino) {
  589. up(&victim_f->sem);
  590. return -ENOTEMPTY;
  591. }
  592. }
  593. up(&victim_f->sem);
  594. }
  595. }
  596. /* XXX: We probably ought to alloc enough space for
  597. both nodes at the same time. Writing the new link,
  598. then getting -ENOSPC, is quite bad :)
  599. */
  600. /* Make a hard link */
  601. /* XXX: This is ugly */
  602. type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
  603. if (!type) type = DT_REG;
  604. ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
  605. old_dentry->d_inode->i_ino, type,
  606. new_dentry->d_name.name, new_dentry->d_name.len);
  607. if (ret)
  608. return ret;
  609. if (victim_f) {
  610. /* There was a victim. Kill it off nicely */
  611. new_dentry->d_inode->i_nlink--;
  612. /* Don't oops if the victim was a dirent pointing to an
  613. inode which didn't exist. */
  614. if (victim_f->inocache) {
  615. down(&victim_f->sem);
  616. victim_f->inocache->nlink--;
  617. up(&victim_f->sem);
  618. }
  619. }
  620. /* If it was a directory we moved, and there was no victim,
  621. increase i_nlink on its new parent */
  622. if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
  623. new_dir_i->i_nlink++;
  624. /* Unlink the original */
  625. ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
  626. old_dentry->d_name.name, old_dentry->d_name.len, NULL);
  627. /* We don't touch inode->i_nlink */
  628. if (ret) {
  629. /* Oh shit. We really ought to make a single node which can do both atomically */
  630. struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
  631. down(&f->sem);
  632. old_dentry->d_inode->i_nlink++;
  633. if (f->inocache)
  634. f->inocache->nlink++;
  635. up(&f->sem);
  636. printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
  637. /* Might as well let the VFS know */
  638. d_instantiate(new_dentry, old_dentry->d_inode);
  639. atomic_inc(&old_dentry->d_inode->i_count);
  640. return ret;
  641. }
  642. if (S_ISDIR(old_dentry->d_inode->i_mode))
  643. old_dir_i->i_nlink--;
  644. return 0;
  645. }