dir.c 23 KB

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