dir.c 23 KB

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