dir.c 23 KB

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