dir.c 23 KB

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