dir.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  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.90 2005/11/07 11:14:39 gleixner 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. uint32_t now = get_seconds();
  194. ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
  195. dentry->d_name.len, dead_f, now);
  196. if (dead_f->inocache)
  197. dentry->d_inode->i_nlink = dead_f->inocache->nlink;
  198. if (!ret)
  199. dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
  200. return ret;
  201. }
  202. /***********************************************************************/
  203. static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
  204. {
  205. struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb);
  206. struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
  207. struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
  208. int ret;
  209. uint8_t type;
  210. uint32_t now;
  211. /* Don't let people make hard links to bad inodes. */
  212. if (!f->inocache)
  213. return -EIO;
  214. if (S_ISDIR(old_dentry->d_inode->i_mode))
  215. return -EPERM;
  216. /* XXX: This is ugly */
  217. type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
  218. if (!type) type = DT_REG;
  219. now = get_seconds();
  220. ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len, now);
  221. if (!ret) {
  222. down(&f->sem);
  223. old_dentry->d_inode->i_nlink = ++f->inocache->nlink;
  224. up(&f->sem);
  225. d_instantiate(dentry, old_dentry->d_inode);
  226. dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
  227. atomic_inc(&old_dentry->d_inode->i_count);
  228. }
  229. return ret;
  230. }
  231. /***********************************************************************/
  232. static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
  233. {
  234. struct jffs2_inode_info *f, *dir_f;
  235. struct jffs2_sb_info *c;
  236. struct inode *inode;
  237. struct jffs2_raw_inode *ri;
  238. struct jffs2_raw_dirent *rd;
  239. struct jffs2_full_dnode *fn;
  240. struct jffs2_full_dirent *fd;
  241. int namelen;
  242. uint32_t alloclen, phys_ofs;
  243. int ret, targetlen = strlen(target);
  244. /* FIXME: If you care. We'd need to use frags for the target
  245. if it grows much more than this */
  246. if (targetlen > 254)
  247. return -EINVAL;
  248. ri = jffs2_alloc_raw_inode();
  249. if (!ri)
  250. return -ENOMEM;
  251. c = JFFS2_SB_INFO(dir_i->i_sb);
  252. /* Try to reserve enough space for both node and dirent.
  253. * Just the node will do for now, though
  254. */
  255. namelen = dentry->d_name.len;
  256. ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &phys_ofs, &alloclen,
  257. ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
  258. if (ret) {
  259. jffs2_free_raw_inode(ri);
  260. return ret;
  261. }
  262. inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
  263. if (IS_ERR(inode)) {
  264. jffs2_free_raw_inode(ri);
  265. jffs2_complete_reservation(c);
  266. return PTR_ERR(inode);
  267. }
  268. inode->i_op = &jffs2_symlink_inode_operations;
  269. f = JFFS2_INODE_INFO(inode);
  270. inode->i_size = targetlen;
  271. ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
  272. ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
  273. ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
  274. ri->compr = JFFS2_COMPR_NONE;
  275. ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
  276. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  277. fn = jffs2_write_dnode(c, f, ri, target, targetlen, phys_ofs, ALLOC_NORMAL);
  278. jffs2_free_raw_inode(ri);
  279. if (IS_ERR(fn)) {
  280. /* Eeek. Wave bye bye */
  281. up(&f->sem);
  282. jffs2_complete_reservation(c);
  283. jffs2_clear_inode(inode);
  284. return PTR_ERR(fn);
  285. }
  286. /* We use f->target field to store the target path. */
  287. f->target = kmalloc(targetlen + 1, GFP_KERNEL);
  288. if (!f->target) {
  289. printk(KERN_WARNING "Can't allocate %d bytes of memory\n", targetlen + 1);
  290. up(&f->sem);
  291. jffs2_complete_reservation(c);
  292. jffs2_clear_inode(inode);
  293. return -ENOMEM;
  294. }
  295. memcpy(f->target, target, targetlen + 1);
  296. D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->target));
  297. /* No data here. Only a metadata node, which will be
  298. obsoleted by the first data write
  299. */
  300. f->metadata = fn;
  301. up(&f->sem);
  302. jffs2_complete_reservation(c);
  303. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
  304. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  305. if (ret) {
  306. /* Eep. */
  307. jffs2_clear_inode(inode);
  308. return ret;
  309. }
  310. rd = jffs2_alloc_raw_dirent();
  311. if (!rd) {
  312. /* Argh. Now we treat it like a normal delete */
  313. jffs2_complete_reservation(c);
  314. jffs2_clear_inode(inode);
  315. return -ENOMEM;
  316. }
  317. dir_f = JFFS2_INODE_INFO(dir_i);
  318. down(&dir_f->sem);
  319. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  320. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  321. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  322. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  323. rd->pino = cpu_to_je32(dir_i->i_ino);
  324. rd->version = cpu_to_je32(++dir_f->highest_version);
  325. rd->ino = cpu_to_je32(inode->i_ino);
  326. rd->mctime = cpu_to_je32(get_seconds());
  327. rd->nsize = namelen;
  328. rd->type = DT_LNK;
  329. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  330. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  331. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
  332. if (IS_ERR(fd)) {
  333. /* dirent failed to write. Delete the inode normally
  334. as if it were the final unlink() */
  335. jffs2_complete_reservation(c);
  336. jffs2_free_raw_dirent(rd);
  337. up(&dir_f->sem);
  338. jffs2_clear_inode(inode);
  339. return PTR_ERR(fd);
  340. }
  341. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  342. jffs2_free_raw_dirent(rd);
  343. /* Link the fd into the inode's list, obsoleting an old
  344. one if necessary. */
  345. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  346. up(&dir_f->sem);
  347. jffs2_complete_reservation(c);
  348. d_instantiate(dentry, inode);
  349. return 0;
  350. }
  351. static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
  352. {
  353. struct jffs2_inode_info *f, *dir_f;
  354. struct jffs2_sb_info *c;
  355. struct inode *inode;
  356. struct jffs2_raw_inode *ri;
  357. struct jffs2_raw_dirent *rd;
  358. struct jffs2_full_dnode *fn;
  359. struct jffs2_full_dirent *fd;
  360. int namelen;
  361. uint32_t alloclen, phys_ofs;
  362. int ret;
  363. mode |= S_IFDIR;
  364. ri = jffs2_alloc_raw_inode();
  365. if (!ri)
  366. return -ENOMEM;
  367. c = JFFS2_SB_INFO(dir_i->i_sb);
  368. /* Try to reserve enough space for both node and dirent.
  369. * Just the node will do for now, though
  370. */
  371. namelen = dentry->d_name.len;
  372. ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL,
  373. JFFS2_SUMMARY_INODE_SIZE);
  374. if (ret) {
  375. jffs2_free_raw_inode(ri);
  376. return ret;
  377. }
  378. inode = jffs2_new_inode(dir_i, mode, ri);
  379. if (IS_ERR(inode)) {
  380. jffs2_free_raw_inode(ri);
  381. jffs2_complete_reservation(c);
  382. return PTR_ERR(inode);
  383. }
  384. inode->i_op = &jffs2_dir_inode_operations;
  385. inode->i_fop = &jffs2_dir_operations;
  386. /* Directories get nlink 2 at start */
  387. inode->i_nlink = 2;
  388. f = JFFS2_INODE_INFO(inode);
  389. ri->data_crc = cpu_to_je32(0);
  390. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  391. fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
  392. jffs2_free_raw_inode(ri);
  393. if (IS_ERR(fn)) {
  394. /* Eeek. Wave bye bye */
  395. up(&f->sem);
  396. jffs2_complete_reservation(c);
  397. jffs2_clear_inode(inode);
  398. return PTR_ERR(fn);
  399. }
  400. /* No data here. Only a metadata node, which will be
  401. obsoleted by the first data write
  402. */
  403. f->metadata = fn;
  404. up(&f->sem);
  405. jffs2_complete_reservation(c);
  406. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
  407. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  408. if (ret) {
  409. /* Eep. */
  410. jffs2_clear_inode(inode);
  411. return ret;
  412. }
  413. rd = jffs2_alloc_raw_dirent();
  414. if (!rd) {
  415. /* Argh. Now we treat it like a normal delete */
  416. jffs2_complete_reservation(c);
  417. jffs2_clear_inode(inode);
  418. return -ENOMEM;
  419. }
  420. dir_f = JFFS2_INODE_INFO(dir_i);
  421. down(&dir_f->sem);
  422. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  423. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  424. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  425. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  426. rd->pino = cpu_to_je32(dir_i->i_ino);
  427. rd->version = cpu_to_je32(++dir_f->highest_version);
  428. rd->ino = cpu_to_je32(inode->i_ino);
  429. rd->mctime = cpu_to_je32(get_seconds());
  430. rd->nsize = namelen;
  431. rd->type = DT_DIR;
  432. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  433. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  434. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
  435. if (IS_ERR(fd)) {
  436. /* dirent failed to write. Delete the inode normally
  437. as if it were the final unlink() */
  438. jffs2_complete_reservation(c);
  439. jffs2_free_raw_dirent(rd);
  440. up(&dir_f->sem);
  441. jffs2_clear_inode(inode);
  442. return PTR_ERR(fd);
  443. }
  444. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  445. dir_i->i_nlink++;
  446. jffs2_free_raw_dirent(rd);
  447. /* Link the fd into the inode's list, obsoleting an old
  448. one if necessary. */
  449. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  450. up(&dir_f->sem);
  451. jffs2_complete_reservation(c);
  452. d_instantiate(dentry, inode);
  453. return 0;
  454. }
  455. static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
  456. {
  457. struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
  458. struct jffs2_full_dirent *fd;
  459. int ret;
  460. for (fd = f->dents ; fd; fd = fd->next) {
  461. if (fd->ino)
  462. return -ENOTEMPTY;
  463. }
  464. ret = jffs2_unlink(dir_i, dentry);
  465. if (!ret)
  466. dir_i->i_nlink--;
  467. return ret;
  468. }
  469. static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, dev_t rdev)
  470. {
  471. struct jffs2_inode_info *f, *dir_f;
  472. struct jffs2_sb_info *c;
  473. struct inode *inode;
  474. struct jffs2_raw_inode *ri;
  475. struct jffs2_raw_dirent *rd;
  476. struct jffs2_full_dnode *fn;
  477. struct jffs2_full_dirent *fd;
  478. int namelen;
  479. jint16_t dev;
  480. int devlen = 0;
  481. uint32_t alloclen, phys_ofs;
  482. int ret;
  483. if (!old_valid_dev(rdev))
  484. return -EINVAL;
  485. ri = jffs2_alloc_raw_inode();
  486. if (!ri)
  487. return -ENOMEM;
  488. c = JFFS2_SB_INFO(dir_i->i_sb);
  489. if (S_ISBLK(mode) || S_ISCHR(mode)) {
  490. dev = cpu_to_je16(old_encode_dev(rdev));
  491. devlen = sizeof(dev);
  492. }
  493. /* Try to reserve enough space for both node and dirent.
  494. * Just the node will do for now, though
  495. */
  496. namelen = dentry->d_name.len;
  497. ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &phys_ofs, &alloclen,
  498. ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
  499. if (ret) {
  500. jffs2_free_raw_inode(ri);
  501. return ret;
  502. }
  503. inode = jffs2_new_inode(dir_i, mode, ri);
  504. if (IS_ERR(inode)) {
  505. jffs2_free_raw_inode(ri);
  506. jffs2_complete_reservation(c);
  507. return PTR_ERR(inode);
  508. }
  509. inode->i_op = &jffs2_file_inode_operations;
  510. init_special_inode(inode, inode->i_mode, rdev);
  511. f = JFFS2_INODE_INFO(inode);
  512. ri->dsize = ri->csize = cpu_to_je32(devlen);
  513. ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
  514. ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
  515. ri->compr = JFFS2_COMPR_NONE;
  516. ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
  517. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  518. fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, phys_ofs, ALLOC_NORMAL);
  519. jffs2_free_raw_inode(ri);
  520. if (IS_ERR(fn)) {
  521. /* Eeek. Wave bye bye */
  522. up(&f->sem);
  523. jffs2_complete_reservation(c);
  524. jffs2_clear_inode(inode);
  525. return PTR_ERR(fn);
  526. }
  527. /* No data here. Only a metadata node, which will be
  528. obsoleted by the first data write
  529. */
  530. f->metadata = fn;
  531. up(&f->sem);
  532. jffs2_complete_reservation(c);
  533. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &phys_ofs, &alloclen,
  534. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  535. if (ret) {
  536. /* Eep. */
  537. jffs2_clear_inode(inode);
  538. return ret;
  539. }
  540. rd = jffs2_alloc_raw_dirent();
  541. if (!rd) {
  542. /* Argh. Now we treat it like a normal delete */
  543. jffs2_complete_reservation(c);
  544. jffs2_clear_inode(inode);
  545. return -ENOMEM;
  546. }
  547. dir_f = JFFS2_INODE_INFO(dir_i);
  548. down(&dir_f->sem);
  549. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  550. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  551. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  552. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  553. rd->pino = cpu_to_je32(dir_i->i_ino);
  554. rd->version = cpu_to_je32(++dir_f->highest_version);
  555. rd->ino = cpu_to_je32(inode->i_ino);
  556. rd->mctime = cpu_to_je32(get_seconds());
  557. rd->nsize = namelen;
  558. /* XXX: This is ugly. */
  559. rd->type = (mode & S_IFMT) >> 12;
  560. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  561. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  562. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, phys_ofs, ALLOC_NORMAL);
  563. if (IS_ERR(fd)) {
  564. /* dirent failed to write. Delete the inode normally
  565. as if it were the final unlink() */
  566. jffs2_complete_reservation(c);
  567. jffs2_free_raw_dirent(rd);
  568. up(&dir_f->sem);
  569. jffs2_clear_inode(inode);
  570. return PTR_ERR(fd);
  571. }
  572. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  573. jffs2_free_raw_dirent(rd);
  574. /* Link the fd into the inode's list, obsoleting an old
  575. one if necessary. */
  576. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  577. up(&dir_f->sem);
  578. jffs2_complete_reservation(c);
  579. d_instantiate(dentry, inode);
  580. return 0;
  581. }
  582. static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
  583. struct inode *new_dir_i, struct dentry *new_dentry)
  584. {
  585. int ret;
  586. struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
  587. struct jffs2_inode_info *victim_f = NULL;
  588. uint8_t type;
  589. uint32_t now;
  590. /* The VFS will check for us and prevent trying to rename a
  591. * file over a directory and vice versa, but if it's a directory,
  592. * the VFS can't check whether the victim is empty. The filesystem
  593. * needs to do that for itself.
  594. */
  595. if (new_dentry->d_inode) {
  596. victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
  597. if (S_ISDIR(new_dentry->d_inode->i_mode)) {
  598. struct jffs2_full_dirent *fd;
  599. down(&victim_f->sem);
  600. for (fd = victim_f->dents; fd; fd = fd->next) {
  601. if (fd->ino) {
  602. up(&victim_f->sem);
  603. return -ENOTEMPTY;
  604. }
  605. }
  606. up(&victim_f->sem);
  607. }
  608. }
  609. /* XXX: We probably ought to alloc enough space for
  610. both nodes at the same time. Writing the new link,
  611. then getting -ENOSPC, is quite bad :)
  612. */
  613. /* Make a hard link */
  614. /* XXX: This is ugly */
  615. type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
  616. if (!type) type = DT_REG;
  617. now = get_seconds();
  618. ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
  619. old_dentry->d_inode->i_ino, type,
  620. new_dentry->d_name.name, new_dentry->d_name.len, now);
  621. if (ret)
  622. return ret;
  623. if (victim_f) {
  624. /* There was a victim. Kill it off nicely */
  625. new_dentry->d_inode->i_nlink--;
  626. /* Don't oops if the victim was a dirent pointing to an
  627. inode which didn't exist. */
  628. if (victim_f->inocache) {
  629. down(&victim_f->sem);
  630. victim_f->inocache->nlink--;
  631. up(&victim_f->sem);
  632. }
  633. }
  634. /* If it was a directory we moved, and there was no victim,
  635. increase i_nlink on its new parent */
  636. if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
  637. new_dir_i->i_nlink++;
  638. /* Unlink the original */
  639. ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
  640. old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
  641. /* We don't touch inode->i_nlink */
  642. if (ret) {
  643. /* Oh shit. We really ought to make a single node which can do both atomically */
  644. struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
  645. down(&f->sem);
  646. old_dentry->d_inode->i_nlink++;
  647. if (f->inocache)
  648. f->inocache->nlink++;
  649. up(&f->sem);
  650. printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
  651. /* Might as well let the VFS know */
  652. d_instantiate(new_dentry, old_dentry->d_inode);
  653. atomic_inc(&old_dentry->d_inode->i_count);
  654. new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
  655. return ret;
  656. }
  657. if (S_ISDIR(old_dentry->d_inode->i_mode))
  658. old_dir_i->i_nlink--;
  659. new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);
  660. return 0;
  661. }