dir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  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. ihold(old_dentry->d_inode);
  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 = kmemdup(target, 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. D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->target));
  309. /* No data here. Only a metadata node, which will be
  310. obsoleted by the first data write
  311. */
  312. f->metadata = fn;
  313. mutex_unlock(&f->sem);
  314. jffs2_complete_reservation(c);
  315. ret = jffs2_init_security(inode, dir_i);
  316. if (ret)
  317. goto fail;
  318. ret = jffs2_init_acl_post(inode);
  319. if (ret)
  320. goto fail;
  321. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  322. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  323. if (ret)
  324. goto fail;
  325. rd = jffs2_alloc_raw_dirent();
  326. if (!rd) {
  327. /* Argh. Now we treat it like a normal delete */
  328. jffs2_complete_reservation(c);
  329. ret = -ENOMEM;
  330. goto fail;
  331. }
  332. dir_f = JFFS2_INODE_INFO(dir_i);
  333. mutex_lock(&dir_f->sem);
  334. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  335. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  336. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  337. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  338. rd->pino = cpu_to_je32(dir_i->i_ino);
  339. rd->version = cpu_to_je32(++dir_f->highest_version);
  340. rd->ino = cpu_to_je32(inode->i_ino);
  341. rd->mctime = cpu_to_je32(get_seconds());
  342. rd->nsize = namelen;
  343. rd->type = DT_LNK;
  344. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  345. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  346. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
  347. if (IS_ERR(fd)) {
  348. /* dirent failed to write. Delete the inode normally
  349. as if it were the final unlink() */
  350. jffs2_complete_reservation(c);
  351. jffs2_free_raw_dirent(rd);
  352. mutex_unlock(&dir_f->sem);
  353. ret = PTR_ERR(fd);
  354. goto fail;
  355. }
  356. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  357. jffs2_free_raw_dirent(rd);
  358. /* Link the fd into the inode's list, obsoleting an old
  359. one if necessary. */
  360. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  361. mutex_unlock(&dir_f->sem);
  362. jffs2_complete_reservation(c);
  363. d_instantiate(dentry, inode);
  364. unlock_new_inode(inode);
  365. return 0;
  366. fail:
  367. iget_failed(inode);
  368. return ret;
  369. }
  370. static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
  371. {
  372. struct jffs2_inode_info *f, *dir_f;
  373. struct jffs2_sb_info *c;
  374. struct inode *inode;
  375. struct jffs2_raw_inode *ri;
  376. struct jffs2_raw_dirent *rd;
  377. struct jffs2_full_dnode *fn;
  378. struct jffs2_full_dirent *fd;
  379. int namelen;
  380. uint32_t alloclen;
  381. int ret;
  382. mode |= S_IFDIR;
  383. ri = jffs2_alloc_raw_inode();
  384. if (!ri)
  385. return -ENOMEM;
  386. c = JFFS2_SB_INFO(dir_i->i_sb);
  387. /* Try to reserve enough space for both node and dirent.
  388. * Just the node will do for now, though
  389. */
  390. namelen = dentry->d_name.len;
  391. ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
  392. JFFS2_SUMMARY_INODE_SIZE);
  393. if (ret) {
  394. jffs2_free_raw_inode(ri);
  395. return ret;
  396. }
  397. inode = jffs2_new_inode(dir_i, mode, ri);
  398. if (IS_ERR(inode)) {
  399. jffs2_free_raw_inode(ri);
  400. jffs2_complete_reservation(c);
  401. return PTR_ERR(inode);
  402. }
  403. inode->i_op = &jffs2_dir_inode_operations;
  404. inode->i_fop = &jffs2_dir_operations;
  405. f = JFFS2_INODE_INFO(inode);
  406. /* Directories get nlink 2 at start */
  407. inode->i_nlink = 2;
  408. /* but ic->pino_nlink is the parent ino# */
  409. f->inocache->pino_nlink = dir_i->i_ino;
  410. ri->data_crc = cpu_to_je32(0);
  411. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  412. fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
  413. jffs2_free_raw_inode(ri);
  414. if (IS_ERR(fn)) {
  415. /* Eeek. Wave bye bye */
  416. mutex_unlock(&f->sem);
  417. jffs2_complete_reservation(c);
  418. ret = PTR_ERR(fn);
  419. goto fail;
  420. }
  421. /* No data here. Only a metadata node, which will be
  422. obsoleted by the first data write
  423. */
  424. f->metadata = fn;
  425. mutex_unlock(&f->sem);
  426. jffs2_complete_reservation(c);
  427. ret = jffs2_init_security(inode, dir_i);
  428. if (ret)
  429. goto fail;
  430. ret = jffs2_init_acl_post(inode);
  431. if (ret)
  432. goto fail;
  433. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  434. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  435. if (ret)
  436. goto fail;
  437. rd = jffs2_alloc_raw_dirent();
  438. if (!rd) {
  439. /* Argh. Now we treat it like a normal delete */
  440. jffs2_complete_reservation(c);
  441. ret = -ENOMEM;
  442. goto fail;
  443. }
  444. dir_f = JFFS2_INODE_INFO(dir_i);
  445. mutex_lock(&dir_f->sem);
  446. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  447. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  448. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  449. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  450. rd->pino = cpu_to_je32(dir_i->i_ino);
  451. rd->version = cpu_to_je32(++dir_f->highest_version);
  452. rd->ino = cpu_to_je32(inode->i_ino);
  453. rd->mctime = cpu_to_je32(get_seconds());
  454. rd->nsize = namelen;
  455. rd->type = DT_DIR;
  456. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  457. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  458. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
  459. if (IS_ERR(fd)) {
  460. /* dirent failed to write. Delete the inode normally
  461. as if it were the final unlink() */
  462. jffs2_complete_reservation(c);
  463. jffs2_free_raw_dirent(rd);
  464. mutex_unlock(&dir_f->sem);
  465. ret = PTR_ERR(fd);
  466. goto fail;
  467. }
  468. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  469. inc_nlink(dir_i);
  470. jffs2_free_raw_dirent(rd);
  471. /* Link the fd into the inode's list, obsoleting an old
  472. one if necessary. */
  473. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  474. mutex_unlock(&dir_f->sem);
  475. jffs2_complete_reservation(c);
  476. d_instantiate(dentry, inode);
  477. unlock_new_inode(inode);
  478. return 0;
  479. fail:
  480. iget_failed(inode);
  481. return ret;
  482. }
  483. static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
  484. {
  485. struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
  486. struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
  487. struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
  488. struct jffs2_full_dirent *fd;
  489. int ret;
  490. uint32_t now = get_seconds();
  491. for (fd = f->dents ; fd; fd = fd->next) {
  492. if (fd->ino)
  493. return -ENOTEMPTY;
  494. }
  495. ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
  496. dentry->d_name.len, f, now);
  497. if (!ret) {
  498. dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
  499. clear_nlink(dentry->d_inode);
  500. drop_nlink(dir_i);
  501. }
  502. return ret;
  503. }
  504. static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, int mode, dev_t rdev)
  505. {
  506. struct jffs2_inode_info *f, *dir_f;
  507. struct jffs2_sb_info *c;
  508. struct inode *inode;
  509. struct jffs2_raw_inode *ri;
  510. struct jffs2_raw_dirent *rd;
  511. struct jffs2_full_dnode *fn;
  512. struct jffs2_full_dirent *fd;
  513. int namelen;
  514. union jffs2_device_node dev;
  515. int devlen = 0;
  516. uint32_t alloclen;
  517. int ret;
  518. if (!new_valid_dev(rdev))
  519. return -EINVAL;
  520. ri = jffs2_alloc_raw_inode();
  521. if (!ri)
  522. return -ENOMEM;
  523. c = JFFS2_SB_INFO(dir_i->i_sb);
  524. if (S_ISBLK(mode) || S_ISCHR(mode))
  525. devlen = jffs2_encode_dev(&dev, rdev);
  526. /* Try to reserve enough space for both node and dirent.
  527. * Just the node will do for now, though
  528. */
  529. namelen = dentry->d_name.len;
  530. ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen,
  531. ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
  532. if (ret) {
  533. jffs2_free_raw_inode(ri);
  534. return ret;
  535. }
  536. inode = jffs2_new_inode(dir_i, mode, ri);
  537. if (IS_ERR(inode)) {
  538. jffs2_free_raw_inode(ri);
  539. jffs2_complete_reservation(c);
  540. return PTR_ERR(inode);
  541. }
  542. inode->i_op = &jffs2_file_inode_operations;
  543. init_special_inode(inode, inode->i_mode, rdev);
  544. f = JFFS2_INODE_INFO(inode);
  545. ri->dsize = ri->csize = cpu_to_je32(devlen);
  546. ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
  547. ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
  548. ri->compr = JFFS2_COMPR_NONE;
  549. ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
  550. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  551. fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL);
  552. jffs2_free_raw_inode(ri);
  553. if (IS_ERR(fn)) {
  554. /* Eeek. Wave bye bye */
  555. mutex_unlock(&f->sem);
  556. jffs2_complete_reservation(c);
  557. ret = PTR_ERR(fn);
  558. goto fail;
  559. }
  560. /* No data here. Only a metadata node, which will be
  561. obsoleted by the first data write
  562. */
  563. f->metadata = fn;
  564. mutex_unlock(&f->sem);
  565. jffs2_complete_reservation(c);
  566. ret = jffs2_init_security(inode, dir_i);
  567. if (ret)
  568. goto fail;
  569. ret = jffs2_init_acl_post(inode);
  570. if (ret)
  571. goto fail;
  572. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  573. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  574. if (ret)
  575. goto fail;
  576. rd = jffs2_alloc_raw_dirent();
  577. if (!rd) {
  578. /* Argh. Now we treat it like a normal delete */
  579. jffs2_complete_reservation(c);
  580. ret = -ENOMEM;
  581. goto fail;
  582. }
  583. dir_f = JFFS2_INODE_INFO(dir_i);
  584. mutex_lock(&dir_f->sem);
  585. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  586. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  587. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  588. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  589. rd->pino = cpu_to_je32(dir_i->i_ino);
  590. rd->version = cpu_to_je32(++dir_f->highest_version);
  591. rd->ino = cpu_to_je32(inode->i_ino);
  592. rd->mctime = cpu_to_je32(get_seconds());
  593. rd->nsize = namelen;
  594. /* XXX: This is ugly. */
  595. rd->type = (mode & S_IFMT) >> 12;
  596. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  597. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  598. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
  599. if (IS_ERR(fd)) {
  600. /* dirent failed to write. Delete the inode normally
  601. as if it were the final unlink() */
  602. jffs2_complete_reservation(c);
  603. jffs2_free_raw_dirent(rd);
  604. mutex_unlock(&dir_f->sem);
  605. ret = PTR_ERR(fd);
  606. goto fail;
  607. }
  608. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  609. jffs2_free_raw_dirent(rd);
  610. /* Link the fd into the inode's list, obsoleting an old
  611. one if necessary. */
  612. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  613. mutex_unlock(&dir_f->sem);
  614. jffs2_complete_reservation(c);
  615. d_instantiate(dentry, inode);
  616. unlock_new_inode(inode);
  617. return 0;
  618. fail:
  619. iget_failed(inode);
  620. return ret;
  621. }
  622. static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
  623. struct inode *new_dir_i, struct dentry *new_dentry)
  624. {
  625. int ret;
  626. struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
  627. struct jffs2_inode_info *victim_f = NULL;
  628. uint8_t type;
  629. uint32_t now;
  630. /* The VFS will check for us and prevent trying to rename a
  631. * file over a directory and vice versa, but if it's a directory,
  632. * the VFS can't check whether the victim is empty. The filesystem
  633. * needs to do that for itself.
  634. */
  635. if (new_dentry->d_inode) {
  636. victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
  637. if (S_ISDIR(new_dentry->d_inode->i_mode)) {
  638. struct jffs2_full_dirent *fd;
  639. mutex_lock(&victim_f->sem);
  640. for (fd = victim_f->dents; fd; fd = fd->next) {
  641. if (fd->ino) {
  642. mutex_unlock(&victim_f->sem);
  643. return -ENOTEMPTY;
  644. }
  645. }
  646. mutex_unlock(&victim_f->sem);
  647. }
  648. }
  649. /* XXX: We probably ought to alloc enough space for
  650. both nodes at the same time. Writing the new link,
  651. then getting -ENOSPC, is quite bad :)
  652. */
  653. /* Make a hard link */
  654. /* XXX: This is ugly */
  655. type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
  656. if (!type) type = DT_REG;
  657. now = get_seconds();
  658. ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
  659. old_dentry->d_inode->i_ino, type,
  660. new_dentry->d_name.name, new_dentry->d_name.len, now);
  661. if (ret)
  662. return ret;
  663. if (victim_f) {
  664. /* There was a victim. Kill it off nicely */
  665. drop_nlink(new_dentry->d_inode);
  666. /* Don't oops if the victim was a dirent pointing to an
  667. inode which didn't exist. */
  668. if (victim_f->inocache) {
  669. mutex_lock(&victim_f->sem);
  670. if (S_ISDIR(new_dentry->d_inode->i_mode))
  671. victim_f->inocache->pino_nlink = 0;
  672. else
  673. victim_f->inocache->pino_nlink--;
  674. mutex_unlock(&victim_f->sem);
  675. }
  676. }
  677. /* If it was a directory we moved, and there was no victim,
  678. increase i_nlink on its new parent */
  679. if (S_ISDIR(old_dentry->d_inode->i_mode) && !victim_f)
  680. inc_nlink(new_dir_i);
  681. /* Unlink the original */
  682. ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
  683. old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
  684. /* We don't touch inode->i_nlink */
  685. if (ret) {
  686. /* Oh shit. We really ought to make a single node which can do both atomically */
  687. struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
  688. mutex_lock(&f->sem);
  689. inc_nlink(old_dentry->d_inode);
  690. if (f->inocache && !S_ISDIR(old_dentry->d_inode->i_mode))
  691. f->inocache->pino_nlink++;
  692. mutex_unlock(&f->sem);
  693. printk(KERN_NOTICE "jffs2_rename(): Link succeeded, unlink failed (err %d). You now have a hard link\n", ret);
  694. /* Might as well let the VFS know */
  695. d_instantiate(new_dentry, old_dentry->d_inode);
  696. ihold(old_dentry->d_inode);
  697. new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
  698. return ret;
  699. }
  700. if (S_ISDIR(old_dentry->d_inode->i_mode))
  701. drop_nlink(old_dir_i);
  702. new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);
  703. return 0;
  704. }