dir.c 22 KB

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