dir.c 23 KB

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