dir.c 21 KB

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