dir.c 23 KB

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