dir.c 23 KB

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