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. .ioctl = jffs2_ioctl,
  38. .fsync = jffs2_fsync
  39. };
  40. const struct inode_operations jffs2_dir_inode_operations =
  41. {
  42. .create = jffs2_create,
  43. .lookup = jffs2_lookup,
  44. .link = jffs2_link,
  45. .unlink = jffs2_unlink,
  46. .symlink = jffs2_symlink,
  47. .mkdir = jffs2_mkdir,
  48. .rmdir = jffs2_rmdir,
  49. .mknod = jffs2_mknod,
  50. .rename = jffs2_rename,
  51. .permission = jffs2_permission,
  52. .setattr = jffs2_setattr,
  53. .setxattr = jffs2_setxattr,
  54. .getxattr = jffs2_getxattr,
  55. .listxattr = jffs2_listxattr,
  56. .removexattr = jffs2_removexattr
  57. };
  58. /***********************************************************************/
  59. /* We keep the dirent list sorted in increasing order of name hash,
  60. and we use the same hash function as the dentries. Makes this
  61. nice and simple
  62. */
  63. static struct dentry *jffs2_lookup(struct inode *dir_i, struct dentry *target,
  64. struct nameidata *nd)
  65. {
  66. struct jffs2_inode_info *dir_f;
  67. struct jffs2_sb_info *c;
  68. struct jffs2_full_dirent *fd = NULL, *fd_list;
  69. uint32_t ino = 0;
  70. struct inode *inode = NULL;
  71. D1(printk(KERN_DEBUG "jffs2_lookup()\n"));
  72. if (target->d_name.len > JFFS2_MAX_NAME_LEN)
  73. return ERR_PTR(-ENAMETOOLONG);
  74. dir_f = JFFS2_INODE_INFO(dir_i);
  75. c = JFFS2_SB_INFO(dir_i->i_sb);
  76. down(&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. up(&dir_f->sem);
  89. if (ino) {
  90. inode = iget(dir_i->i_sb, ino);
  91. if (!inode) {
  92. printk(KERN_WARNING "iget() failed for ino #%u\n", ino);
  93. return (ERR_PTR(-EIO));
  94. }
  95. }
  96. d_add(target, inode);
  97. return NULL;
  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. down(&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. up(&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. struct posix_acl *acl;
  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, &acl);
  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. ret = jffs2_do_create(c, dir_f, f, ri,
  177. dentry->d_name.name, dentry->d_name.len);
  178. if (ret)
  179. goto fail_acl;
  180. ret = jffs2_init_security(inode, dir_i);
  181. if (ret)
  182. goto fail_acl;
  183. ret = jffs2_init_acl(inode, acl);
  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. d_instantiate(dentry, inode);
  189. D1(printk(KERN_DEBUG "jffs2_create: Created ino #%lu with mode %o, nlink %d(%d). nrpages %ld\n",
  190. inode->i_ino, inode->i_mode, inode->i_nlink, f->inocache->nlink, inode->i_mapping->nrpages));
  191. return 0;
  192. fail_acl:
  193. posix_acl_release(acl);
  194. fail:
  195. make_bad_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->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. down(&f->sem);
  237. old_dentry->d_inode->i_nlink = ++f->inocache->nlink;
  238. up(&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. struct posix_acl *acl;
  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 -EINVAL;
  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, &acl);
  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. up(&f->sem);
  297. jffs2_complete_reservation(c);
  298. jffs2_clear_inode(inode);
  299. posix_acl_release(acl);
  300. return PTR_ERR(fn);
  301. }
  302. /* We use f->target field to store the target path. */
  303. f->target = kmalloc(targetlen + 1, GFP_KERNEL);
  304. if (!f->target) {
  305. printk(KERN_WARNING "Can't allocate %d bytes of memory\n", targetlen + 1);
  306. up(&f->sem);
  307. jffs2_complete_reservation(c);
  308. jffs2_clear_inode(inode);
  309. posix_acl_release(acl);
  310. return -ENOMEM;
  311. }
  312. memcpy(f->target, target, targetlen + 1);
  313. D1(printk(KERN_DEBUG "jffs2_symlink: symlink's target '%s' cached\n", (char *)f->target));
  314. /* No data here. Only a metadata node, which will be
  315. obsoleted by the first data write
  316. */
  317. f->metadata = fn;
  318. up(&f->sem);
  319. jffs2_complete_reservation(c);
  320. ret = jffs2_init_security(inode, dir_i);
  321. if (ret) {
  322. jffs2_clear_inode(inode);
  323. posix_acl_release(acl);
  324. return ret;
  325. }
  326. ret = jffs2_init_acl(inode, acl);
  327. if (ret) {
  328. jffs2_clear_inode(inode);
  329. return ret;
  330. }
  331. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  332. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  333. if (ret) {
  334. /* Eep. */
  335. jffs2_clear_inode(inode);
  336. return ret;
  337. }
  338. rd = jffs2_alloc_raw_dirent();
  339. if (!rd) {
  340. /* Argh. Now we treat it like a normal delete */
  341. jffs2_complete_reservation(c);
  342. jffs2_clear_inode(inode);
  343. return -ENOMEM;
  344. }
  345. dir_f = JFFS2_INODE_INFO(dir_i);
  346. down(&dir_f->sem);
  347. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  348. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  349. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  350. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  351. rd->pino = cpu_to_je32(dir_i->i_ino);
  352. rd->version = cpu_to_je32(++dir_f->highest_version);
  353. rd->ino = cpu_to_je32(inode->i_ino);
  354. rd->mctime = cpu_to_je32(get_seconds());
  355. rd->nsize = namelen;
  356. rd->type = DT_LNK;
  357. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  358. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  359. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
  360. if (IS_ERR(fd)) {
  361. /* dirent failed to write. Delete the inode normally
  362. as if it were the final unlink() */
  363. jffs2_complete_reservation(c);
  364. jffs2_free_raw_dirent(rd);
  365. up(&dir_f->sem);
  366. jffs2_clear_inode(inode);
  367. return PTR_ERR(fd);
  368. }
  369. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  370. jffs2_free_raw_dirent(rd);
  371. /* Link the fd into the inode's list, obsoleting an old
  372. one if necessary. */
  373. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  374. up(&dir_f->sem);
  375. jffs2_complete_reservation(c);
  376. d_instantiate(dentry, inode);
  377. return 0;
  378. }
  379. static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, int mode)
  380. {
  381. struct jffs2_inode_info *f, *dir_f;
  382. struct jffs2_sb_info *c;
  383. struct inode *inode;
  384. struct jffs2_raw_inode *ri;
  385. struct jffs2_raw_dirent *rd;
  386. struct jffs2_full_dnode *fn;
  387. struct jffs2_full_dirent *fd;
  388. int namelen;
  389. uint32_t alloclen;
  390. struct posix_acl *acl;
  391. int ret;
  392. mode |= S_IFDIR;
  393. ri = jffs2_alloc_raw_inode();
  394. if (!ri)
  395. return -ENOMEM;
  396. c = JFFS2_SB_INFO(dir_i->i_sb);
  397. /* Try to reserve enough space for both node and dirent.
  398. * Just the node will do for now, though
  399. */
  400. namelen = dentry->d_name.len;
  401. ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
  402. JFFS2_SUMMARY_INODE_SIZE);
  403. if (ret) {
  404. jffs2_free_raw_inode(ri);
  405. return ret;
  406. }
  407. inode = jffs2_new_inode(dir_i, mode, ri, &acl);
  408. if (IS_ERR(inode)) {
  409. jffs2_free_raw_inode(ri);
  410. jffs2_complete_reservation(c);
  411. return PTR_ERR(inode);
  412. }
  413. inode->i_op = &jffs2_dir_inode_operations;
  414. inode->i_fop = &jffs2_dir_operations;
  415. /* Directories get nlink 2 at start */
  416. inode->i_nlink = 2;
  417. f = JFFS2_INODE_INFO(inode);
  418. ri->data_crc = cpu_to_je32(0);
  419. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  420. fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
  421. jffs2_free_raw_inode(ri);
  422. if (IS_ERR(fn)) {
  423. /* Eeek. Wave bye bye */
  424. up(&f->sem);
  425. jffs2_complete_reservation(c);
  426. jffs2_clear_inode(inode);
  427. posix_acl_release(acl);
  428. return PTR_ERR(fn);
  429. }
  430. /* No data here. Only a metadata node, which will be
  431. obsoleted by the first data write
  432. */
  433. f->metadata = fn;
  434. up(&f->sem);
  435. jffs2_complete_reservation(c);
  436. ret = jffs2_init_security(inode, dir_i);
  437. if (ret) {
  438. jffs2_clear_inode(inode);
  439. posix_acl_release(acl);
  440. return ret;
  441. }
  442. ret = jffs2_init_acl(inode, acl);
  443. if (ret) {
  444. jffs2_clear_inode(inode);
  445. return ret;
  446. }
  447. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  448. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  449. if (ret) {
  450. /* Eep. */
  451. jffs2_clear_inode(inode);
  452. return ret;
  453. }
  454. rd = jffs2_alloc_raw_dirent();
  455. if (!rd) {
  456. /* Argh. Now we treat it like a normal delete */
  457. jffs2_complete_reservation(c);
  458. jffs2_clear_inode(inode);
  459. return -ENOMEM;
  460. }
  461. dir_f = JFFS2_INODE_INFO(dir_i);
  462. down(&dir_f->sem);
  463. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  464. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  465. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  466. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  467. rd->pino = cpu_to_je32(dir_i->i_ino);
  468. rd->version = cpu_to_je32(++dir_f->highest_version);
  469. rd->ino = cpu_to_je32(inode->i_ino);
  470. rd->mctime = cpu_to_je32(get_seconds());
  471. rd->nsize = namelen;
  472. rd->type = DT_DIR;
  473. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  474. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  475. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
  476. if (IS_ERR(fd)) {
  477. /* dirent failed to write. Delete the inode normally
  478. as if it were the final unlink() */
  479. jffs2_complete_reservation(c);
  480. jffs2_free_raw_dirent(rd);
  481. up(&dir_f->sem);
  482. jffs2_clear_inode(inode);
  483. return PTR_ERR(fd);
  484. }
  485. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  486. inc_nlink(dir_i);
  487. jffs2_free_raw_dirent(rd);
  488. /* Link the fd into the inode's list, obsoleting an old
  489. one if necessary. */
  490. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  491. up(&dir_f->sem);
  492. jffs2_complete_reservation(c);
  493. d_instantiate(dentry, inode);
  494. return 0;
  495. }
  496. static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
  497. {
  498. struct jffs2_inode_info *f = JFFS2_INODE_INFO(dentry->d_inode);
  499. struct jffs2_full_dirent *fd;
  500. int ret;
  501. for (fd = f->dents ; fd; fd = fd->next) {
  502. if (fd->ino)
  503. return -ENOTEMPTY;
  504. }
  505. ret = jffs2_unlink(dir_i, dentry);
  506. if (!ret)
  507. drop_nlink(dir_i);
  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. struct posix_acl *acl;
  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, &acl);
  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. up(&f->sem);
  563. jffs2_complete_reservation(c);
  564. jffs2_clear_inode(inode);
  565. posix_acl_release(acl);
  566. return PTR_ERR(fn);
  567. }
  568. /* No data here. Only a metadata node, which will be
  569. obsoleted by the first data write
  570. */
  571. f->metadata = fn;
  572. up(&f->sem);
  573. jffs2_complete_reservation(c);
  574. ret = jffs2_init_security(inode, dir_i);
  575. if (ret) {
  576. jffs2_clear_inode(inode);
  577. posix_acl_release(acl);
  578. return ret;
  579. }
  580. ret = jffs2_init_acl(inode, acl);
  581. if (ret) {
  582. jffs2_clear_inode(inode);
  583. return ret;
  584. }
  585. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  586. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  587. if (ret) {
  588. /* Eep. */
  589. jffs2_clear_inode(inode);
  590. return ret;
  591. }
  592. rd = jffs2_alloc_raw_dirent();
  593. if (!rd) {
  594. /* Argh. Now we treat it like a normal delete */
  595. jffs2_complete_reservation(c);
  596. jffs2_clear_inode(inode);
  597. return -ENOMEM;
  598. }
  599. dir_f = JFFS2_INODE_INFO(dir_i);
  600. down(&dir_f->sem);
  601. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  602. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  603. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  604. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  605. rd->pino = cpu_to_je32(dir_i->i_ino);
  606. rd->version = cpu_to_je32(++dir_f->highest_version);
  607. rd->ino = cpu_to_je32(inode->i_ino);
  608. rd->mctime = cpu_to_je32(get_seconds());
  609. rd->nsize = namelen;
  610. /* XXX: This is ugly. */
  611. rd->type = (mode & S_IFMT) >> 12;
  612. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  613. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  614. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
  615. if (IS_ERR(fd)) {
  616. /* dirent failed to write. Delete the inode normally
  617. as if it were the final unlink() */
  618. jffs2_complete_reservation(c);
  619. jffs2_free_raw_dirent(rd);
  620. up(&dir_f->sem);
  621. jffs2_clear_inode(inode);
  622. return PTR_ERR(fd);
  623. }
  624. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  625. jffs2_free_raw_dirent(rd);
  626. /* Link the fd into the inode's list, obsoleting an old
  627. one if necessary. */
  628. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  629. up(&dir_f->sem);
  630. jffs2_complete_reservation(c);
  631. d_instantiate(dentry, inode);
  632. return 0;
  633. }
  634. static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
  635. struct inode *new_dir_i, struct dentry *new_dentry)
  636. {
  637. int ret;
  638. struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
  639. struct jffs2_inode_info *victim_f = NULL;
  640. uint8_t type;
  641. uint32_t now;
  642. /* The VFS will check for us and prevent trying to rename a
  643. * file over a directory and vice versa, but if it's a directory,
  644. * the VFS can't check whether the victim is empty. The filesystem
  645. * needs to do that for itself.
  646. */
  647. if (new_dentry->d_inode) {
  648. victim_f = JFFS2_INODE_INFO(new_dentry->d_inode);
  649. if (S_ISDIR(new_dentry->d_inode->i_mode)) {
  650. struct jffs2_full_dirent *fd;
  651. down(&victim_f->sem);
  652. for (fd = victim_f->dents; fd; fd = fd->next) {
  653. if (fd->ino) {
  654. up(&victim_f->sem);
  655. return -ENOTEMPTY;
  656. }
  657. }
  658. up(&victim_f->sem);
  659. }
  660. }
  661. /* XXX: We probably ought to alloc enough space for
  662. both nodes at the same time. Writing the new link,
  663. then getting -ENOSPC, is quite bad :)
  664. */
  665. /* Make a hard link */
  666. /* XXX: This is ugly */
  667. type = (old_dentry->d_inode->i_mode & S_IFMT) >> 12;
  668. if (!type) type = DT_REG;
  669. now = get_seconds();
  670. ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
  671. old_dentry->d_inode->i_ino, type,
  672. new_dentry->d_name.name, new_dentry->d_name.len, now);
  673. if (ret)
  674. return ret;
  675. if (victim_f) {
  676. /* There was a victim. Kill it off nicely */
  677. drop_nlink(new_dentry->d_inode);
  678. /* Don't oops if the victim was a dirent pointing to an
  679. inode which didn't exist. */
  680. if (victim_f->inocache) {
  681. down(&victim_f->sem);
  682. victim_f->inocache->nlink--;
  683. up(&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. down(&f->sem);
  698. inc_nlink(old_dentry->d_inode);
  699. if (f->inocache)
  700. f->inocache->nlink++;
  701. up(&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. }