dir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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. .permission = jffs2_permission,
  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. d_instantiate(dentry, inode);
  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. return 0;
  192. fail:
  193. make_bad_inode(inode);
  194. iput(inode);
  195. jffs2_free_raw_inode(ri);
  196. return ret;
  197. }
  198. /***********************************************************************/
  199. static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
  200. {
  201. struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
  202. struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
  203. struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(dentry->d_inode);
  204. int ret;
  205. uint32_t now = get_seconds();
  206. ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
  207. dentry->d_name.len, dead_f, now);
  208. if (dead_f->inocache)
  209. dentry->d_inode->i_nlink = dead_f->inocache->pino_nlink;
  210. if (!ret)
  211. dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
  212. return ret;
  213. }
  214. /***********************************************************************/
  215. static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
  216. {
  217. struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_inode->i_sb);
  218. struct jffs2_inode_info *f = JFFS2_INODE_INFO(old_dentry->d_inode);
  219. struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
  220. int ret;
  221. uint8_t type;
  222. uint32_t now;
  223. /* Don't let people make hard links to bad inodes. */
  224. if (!f->inocache)
  225. return -EIO;
  226. if (S_ISDIR(old_dentry->d_inode->i_mode))
  227. return -EPERM;
  228. /* XXX: This is ugly */
  229. type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
  230. if (!type) type = DT_REG;
  231. now = get_seconds();
  232. ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len, now);
  233. if (!ret) {
  234. mutex_lock(&f->sem);
  235. old_dentry->d_inode->i_nlink = ++f->inocache->pino_nlink;
  236. mutex_unlock(&f->sem);
  237. d_instantiate(dentry, old_dentry->d_inode);
  238. dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
  239. atomic_inc(&old_dentry->d_inode->i_count);
  240. }
  241. return ret;
  242. }
  243. /***********************************************************************/
  244. static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
  245. {
  246. struct jffs2_inode_info *f, *dir_f;
  247. struct jffs2_sb_info *c;
  248. struct inode *inode;
  249. struct jffs2_raw_inode *ri;
  250. struct jffs2_raw_dirent *rd;
  251. struct jffs2_full_dnode *fn;
  252. struct jffs2_full_dirent *fd;
  253. int namelen;
  254. uint32_t alloclen;
  255. int ret, targetlen = strlen(target);
  256. /* FIXME: If you care. We'd need to use frags for the target
  257. if it grows much more than this */
  258. if (targetlen > 254)
  259. return -ENAMETOOLONG;
  260. ri = jffs2_alloc_raw_inode();
  261. if (!ri)
  262. return -ENOMEM;
  263. c = JFFS2_SB_INFO(dir_i->i_sb);
  264. /* Try to reserve enough space for both node and dirent.
  265. * Just the node will do for now, though
  266. */
  267. namelen = dentry->d_name.len;
  268. ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen,
  269. ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
  270. if (ret) {
  271. jffs2_free_raw_inode(ri);
  272. return ret;
  273. }
  274. inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
  275. if (IS_ERR(inode)) {
  276. jffs2_free_raw_inode(ri);
  277. jffs2_complete_reservation(c);
  278. return PTR_ERR(inode);
  279. }
  280. inode->i_op = &jffs2_symlink_inode_operations;
  281. f = JFFS2_INODE_INFO(inode);
  282. inode->i_size = targetlen;
  283. ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
  284. ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
  285. ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
  286. ri->compr = JFFS2_COMPR_NONE;
  287. ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
  288. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  289. fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL);
  290. jffs2_free_raw_inode(ri);
  291. if (IS_ERR(fn)) {
  292. /* Eeek. Wave bye bye */
  293. mutex_unlock(&f->sem);
  294. jffs2_complete_reservation(c);
  295. jffs2_clear_inode(inode);
  296. return PTR_ERR(fn);
  297. }
  298. /* We use f->target field to store the target path. */
  299. f->target = kmalloc(targetlen + 1, GFP_KERNEL);
  300. if (!f->target) {
  301. printk(KERN_WARNING "Can't allocate %d bytes of memory\n", targetlen + 1);
  302. mutex_unlock(&f->sem);
  303. jffs2_complete_reservation(c);
  304. jffs2_clear_inode(inode);
  305. return -ENOMEM;
  306. }
  307. memcpy(f->target, target, targetlen + 1);
  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. jffs2_clear_inode(inode);
  318. return ret;
  319. }
  320. ret = jffs2_init_acl_post(inode);
  321. if (ret) {
  322. jffs2_clear_inode(inode);
  323. return ret;
  324. }
  325. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  326. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  327. if (ret) {
  328. /* Eep. */
  329. jffs2_clear_inode(inode);
  330. return ret;
  331. }
  332. rd = jffs2_alloc_raw_dirent();
  333. if (!rd) {
  334. /* Argh. Now we treat it like a normal delete */
  335. jffs2_complete_reservation(c);
  336. jffs2_clear_inode(inode);
  337. return -ENOMEM;
  338. }
  339. dir_f = JFFS2_INODE_INFO(dir_i);
  340. mutex_lock(&dir_f->sem);
  341. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  342. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  343. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  344. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  345. rd->pino = cpu_to_je32(dir_i->i_ino);
  346. rd->version = cpu_to_je32(++dir_f->highest_version);
  347. rd->ino = cpu_to_je32(inode->i_ino);
  348. rd->mctime = cpu_to_je32(get_seconds());
  349. rd->nsize = namelen;
  350. rd->type = DT_LNK;
  351. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  352. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  353. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
  354. if (IS_ERR(fd)) {
  355. /* dirent failed to write. Delete the inode normally
  356. as if it were the final unlink() */
  357. jffs2_complete_reservation(c);
  358. jffs2_free_raw_dirent(rd);
  359. mutex_unlock(&dir_f->sem);
  360. jffs2_clear_inode(inode);
  361. return PTR_ERR(fd);
  362. }
  363. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  364. jffs2_free_raw_dirent(rd);
  365. /* Link the fd into the inode's list, obsoleting an old
  366. one if necessary. */
  367. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  368. mutex_unlock(&dir_f->sem);
  369. jffs2_complete_reservation(c);
  370. d_instantiate(dentry, inode);
  371. return 0;
  372. }
  373. static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
  374. {
  375. struct jffs2_inode_info *f, *dir_f;
  376. struct jffs2_sb_info *c;
  377. struct inode *inode;
  378. struct jffs2_raw_inode *ri;
  379. struct jffs2_raw_dirent *rd;
  380. struct jffs2_full_dnode *fn;
  381. struct jffs2_full_dirent *fd;
  382. int namelen;
  383. uint32_t alloclen;
  384. int ret;
  385. mode |= S_IFDIR;
  386. ri = jffs2_alloc_raw_inode();
  387. if (!ri)
  388. return -ENOMEM;
  389. c = JFFS2_SB_INFO(dir_i->i_sb);
  390. /* Try to reserve enough space for both node and dirent.
  391. * Just the node will do for now, though
  392. */
  393. namelen = dentry->d_name.len;
  394. ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
  395. JFFS2_SUMMARY_INODE_SIZE);
  396. if (ret) {
  397. jffs2_free_raw_inode(ri);
  398. return ret;
  399. }
  400. inode = jffs2_new_inode(dir_i, mode, ri);
  401. if (IS_ERR(inode)) {
  402. jffs2_free_raw_inode(ri);
  403. jffs2_complete_reservation(c);
  404. return PTR_ERR(inode);
  405. }
  406. inode->i_op = &jffs2_dir_inode_operations;
  407. inode->i_fop = &jffs2_dir_operations;
  408. f = JFFS2_INODE_INFO(inode);
  409. /* Directories get nlink 2 at start */
  410. inode->i_nlink = 2;
  411. /* but ic->pino_nlink is the parent ino# */
  412. f->inocache->pino_nlink = dir_i->i_ino;
  413. ri->data_crc = cpu_to_je32(0);
  414. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  415. fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
  416. jffs2_free_raw_inode(ri);
  417. if (IS_ERR(fn)) {
  418. /* Eeek. Wave bye bye */
  419. mutex_unlock(&f->sem);
  420. jffs2_complete_reservation(c);
  421. jffs2_clear_inode(inode);
  422. return PTR_ERR(fn);
  423. }
  424. /* No data here. Only a metadata node, which will be
  425. obsoleted by the first data write
  426. */
  427. f->metadata = fn;
  428. mutex_unlock(&f->sem);
  429. jffs2_complete_reservation(c);
  430. ret = jffs2_init_security(inode, dir_i);
  431. if (ret) {
  432. jffs2_clear_inode(inode);
  433. return ret;
  434. }
  435. ret = jffs2_init_acl_post(inode);
  436. if (ret) {
  437. jffs2_clear_inode(inode);
  438. return ret;
  439. }
  440. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  441. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  442. if (ret) {
  443. /* Eep. */
  444. jffs2_clear_inode(inode);
  445. return ret;
  446. }
  447. rd = jffs2_alloc_raw_dirent();
  448. if (!rd) {
  449. /* Argh. Now we treat it like a normal delete */
  450. jffs2_complete_reservation(c);
  451. jffs2_clear_inode(inode);
  452. return -ENOMEM;
  453. }
  454. dir_f = JFFS2_INODE_INFO(dir_i);
  455. mutex_lock(&dir_f->sem);
  456. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  457. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  458. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  459. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  460. rd->pino = cpu_to_je32(dir_i->i_ino);
  461. rd->version = cpu_to_je32(++dir_f->highest_version);
  462. rd->ino = cpu_to_je32(inode->i_ino);
  463. rd->mctime = cpu_to_je32(get_seconds());
  464. rd->nsize = namelen;
  465. rd->type = DT_DIR;
  466. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  467. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  468. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
  469. if (IS_ERR(fd)) {
  470. /* dirent failed to write. Delete the inode normally
  471. as if it were the final unlink() */
  472. jffs2_complete_reservation(c);
  473. jffs2_free_raw_dirent(rd);
  474. mutex_unlock(&dir_f->sem);
  475. jffs2_clear_inode(inode);
  476. return PTR_ERR(fd);
  477. }
  478. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  479. inc_nlink(dir_i);
  480. jffs2_free_raw_dirent(rd);
  481. /* Link the fd into the inode's list, obsoleting an old
  482. one if necessary. */
  483. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  484. mutex_unlock(&dir_f->sem);
  485. jffs2_complete_reservation(c);
  486. d_instantiate(dentry, inode);
  487. return 0;
  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. jffs2_clear_inode(inode);
  564. return PTR_ERR(fn);
  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. jffs2_clear_inode(inode);
  575. return ret;
  576. }
  577. ret = jffs2_init_acl_post(inode);
  578. if (ret) {
  579. jffs2_clear_inode(inode);
  580. return ret;
  581. }
  582. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  583. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  584. if (ret) {
  585. /* Eep. */
  586. jffs2_clear_inode(inode);
  587. return ret;
  588. }
  589. rd = jffs2_alloc_raw_dirent();
  590. if (!rd) {
  591. /* Argh. Now we treat it like a normal delete */
  592. jffs2_complete_reservation(c);
  593. jffs2_clear_inode(inode);
  594. return -ENOMEM;
  595. }
  596. dir_f = JFFS2_INODE_INFO(dir_i);
  597. mutex_lock(&dir_f->sem);
  598. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  599. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  600. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  601. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  602. rd->pino = cpu_to_je32(dir_i->i_ino);
  603. rd->version = cpu_to_je32(++dir_f->highest_version);
  604. rd->ino = cpu_to_je32(inode->i_ino);
  605. rd->mctime = cpu_to_je32(get_seconds());
  606. rd->nsize = namelen;
  607. /* XXX: This is ugly. */
  608. rd->type = (mode & S_IFMT) >> 12;
  609. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  610. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  611. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
  612. if (IS_ERR(fd)) {
  613. /* dirent failed to write. Delete the inode normally
  614. as if it were the final unlink() */
  615. jffs2_complete_reservation(c);
  616. jffs2_free_raw_dirent(rd);
  617. mutex_unlock(&dir_f->sem);
  618. jffs2_clear_inode(inode);
  619. return PTR_ERR(fd);
  620. }
  621. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  622. jffs2_free_raw_dirent(rd);
  623. /* Link the fd into the inode's list, obsoleting an old
  624. one if necessary. */
  625. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  626. mutex_unlock(&dir_f->sem);
  627. jffs2_complete_reservation(c);
  628. d_instantiate(dentry, inode);
  629. return 0;
  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. }