dir.c 23 KB

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