dir.c 23 KB

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