dir.c 23 KB

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