dir.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  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. d_instantiate_new(dentry, inode);
  174. return 0;
  175. fail:
  176. iget_failed(inode);
  177. jffs2_free_raw_inode(ri);
  178. return ret;
  179. }
  180. /***********************************************************************/
  181. static int jffs2_unlink(struct inode *dir_i, struct dentry *dentry)
  182. {
  183. struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
  184. struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
  185. struct jffs2_inode_info *dead_f = JFFS2_INODE_INFO(d_inode(dentry));
  186. int ret;
  187. uint32_t now = get_seconds();
  188. ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
  189. dentry->d_name.len, dead_f, now);
  190. if (dead_f->inocache)
  191. set_nlink(d_inode(dentry), dead_f->inocache->pino_nlink);
  192. if (!ret)
  193. dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
  194. return ret;
  195. }
  196. /***********************************************************************/
  197. static int jffs2_link (struct dentry *old_dentry, struct inode *dir_i, struct dentry *dentry)
  198. {
  199. struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dentry->d_sb);
  200. struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode(old_dentry));
  201. struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
  202. int ret;
  203. uint8_t type;
  204. uint32_t now;
  205. /* Don't let people make hard links to bad inodes. */
  206. if (!f->inocache)
  207. return -EIO;
  208. if (d_is_dir(old_dentry))
  209. return -EPERM;
  210. /* XXX: This is ugly */
  211. type = (d_inode(old_dentry)->i_mode & S_IFMT) >> 12;
  212. if (!type) type = DT_REG;
  213. now = get_seconds();
  214. ret = jffs2_do_link(c, dir_f, f->inocache->ino, type, dentry->d_name.name, dentry->d_name.len, now);
  215. if (!ret) {
  216. mutex_lock(&f->sem);
  217. set_nlink(d_inode(old_dentry), ++f->inocache->pino_nlink);
  218. mutex_unlock(&f->sem);
  219. d_instantiate(dentry, d_inode(old_dentry));
  220. dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
  221. ihold(d_inode(old_dentry));
  222. }
  223. return ret;
  224. }
  225. /***********************************************************************/
  226. static int jffs2_symlink (struct inode *dir_i, struct dentry *dentry, const char *target)
  227. {
  228. struct jffs2_inode_info *f, *dir_f;
  229. struct jffs2_sb_info *c;
  230. struct inode *inode;
  231. struct jffs2_raw_inode *ri;
  232. struct jffs2_raw_dirent *rd;
  233. struct jffs2_full_dnode *fn;
  234. struct jffs2_full_dirent *fd;
  235. int namelen;
  236. uint32_t alloclen;
  237. int ret, targetlen = strlen(target);
  238. /* FIXME: If you care. We'd need to use frags for the target
  239. if it grows much more than this */
  240. if (targetlen > 254)
  241. return -ENAMETOOLONG;
  242. ri = jffs2_alloc_raw_inode();
  243. if (!ri)
  244. return -ENOMEM;
  245. c = JFFS2_SB_INFO(dir_i->i_sb);
  246. /* Try to reserve enough space for both node and dirent.
  247. * Just the node will do for now, though
  248. */
  249. namelen = dentry->d_name.len;
  250. ret = jffs2_reserve_space(c, sizeof(*ri) + targetlen, &alloclen,
  251. ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
  252. if (ret) {
  253. jffs2_free_raw_inode(ri);
  254. return ret;
  255. }
  256. inode = jffs2_new_inode(dir_i, S_IFLNK | S_IRWXUGO, ri);
  257. if (IS_ERR(inode)) {
  258. jffs2_free_raw_inode(ri);
  259. jffs2_complete_reservation(c);
  260. return PTR_ERR(inode);
  261. }
  262. inode->i_op = &jffs2_symlink_inode_operations;
  263. f = JFFS2_INODE_INFO(inode);
  264. inode->i_size = targetlen;
  265. ri->isize = ri->dsize = ri->csize = cpu_to_je32(inode->i_size);
  266. ri->totlen = cpu_to_je32(sizeof(*ri) + inode->i_size);
  267. ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
  268. ri->compr = JFFS2_COMPR_NONE;
  269. ri->data_crc = cpu_to_je32(crc32(0, target, targetlen));
  270. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  271. fn = jffs2_write_dnode(c, f, ri, target, targetlen, ALLOC_NORMAL);
  272. jffs2_free_raw_inode(ri);
  273. if (IS_ERR(fn)) {
  274. /* Eeek. Wave bye bye */
  275. mutex_unlock(&f->sem);
  276. jffs2_complete_reservation(c);
  277. ret = PTR_ERR(fn);
  278. goto fail;
  279. }
  280. /* We use f->target field to store the target path. */
  281. f->target = kmemdup(target, targetlen + 1, GFP_KERNEL);
  282. if (!f->target) {
  283. pr_warn("Can't allocate %d bytes of memory\n", targetlen + 1);
  284. mutex_unlock(&f->sem);
  285. jffs2_complete_reservation(c);
  286. ret = -ENOMEM;
  287. goto fail;
  288. }
  289. inode->i_link = f->target;
  290. jffs2_dbg(1, "%s(): symlink's target '%s' cached\n",
  291. __func__, (char *)f->target);
  292. /* No data here. Only a metadata node, which will be
  293. obsoleted by the first data write
  294. */
  295. f->metadata = fn;
  296. mutex_unlock(&f->sem);
  297. jffs2_complete_reservation(c);
  298. ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
  299. if (ret)
  300. goto fail;
  301. ret = jffs2_init_acl_post(inode);
  302. if (ret)
  303. goto fail;
  304. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  305. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  306. if (ret)
  307. goto fail;
  308. rd = jffs2_alloc_raw_dirent();
  309. if (!rd) {
  310. /* Argh. Now we treat it like a normal delete */
  311. jffs2_complete_reservation(c);
  312. ret = -ENOMEM;
  313. goto fail;
  314. }
  315. dir_f = JFFS2_INODE_INFO(dir_i);
  316. mutex_lock(&dir_f->sem);
  317. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  318. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  319. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  320. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  321. rd->pino = cpu_to_je32(dir_i->i_ino);
  322. rd->version = cpu_to_je32(++dir_f->highest_version);
  323. rd->ino = cpu_to_je32(inode->i_ino);
  324. rd->mctime = cpu_to_je32(get_seconds());
  325. rd->nsize = namelen;
  326. rd->type = DT_LNK;
  327. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  328. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  329. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
  330. if (IS_ERR(fd)) {
  331. /* dirent failed to write. Delete the inode normally
  332. as if it were the final unlink() */
  333. jffs2_complete_reservation(c);
  334. jffs2_free_raw_dirent(rd);
  335. mutex_unlock(&dir_f->sem);
  336. ret = PTR_ERR(fd);
  337. goto fail;
  338. }
  339. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  340. jffs2_free_raw_dirent(rd);
  341. /* Link the fd into the inode's list, obsoleting an old
  342. one if necessary. */
  343. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  344. mutex_unlock(&dir_f->sem);
  345. jffs2_complete_reservation(c);
  346. d_instantiate_new(dentry, inode);
  347. return 0;
  348. fail:
  349. iget_failed(inode);
  350. return ret;
  351. }
  352. static int jffs2_mkdir (struct inode *dir_i, struct dentry *dentry, umode_t mode)
  353. {
  354. struct jffs2_inode_info *f, *dir_f;
  355. struct jffs2_sb_info *c;
  356. struct inode *inode;
  357. struct jffs2_raw_inode *ri;
  358. struct jffs2_raw_dirent *rd;
  359. struct jffs2_full_dnode *fn;
  360. struct jffs2_full_dirent *fd;
  361. int namelen;
  362. uint32_t alloclen;
  363. int ret;
  364. mode |= S_IFDIR;
  365. ri = jffs2_alloc_raw_inode();
  366. if (!ri)
  367. return -ENOMEM;
  368. c = JFFS2_SB_INFO(dir_i->i_sb);
  369. /* Try to reserve enough space for both node and dirent.
  370. * Just the node will do for now, though
  371. */
  372. namelen = dentry->d_name.len;
  373. ret = jffs2_reserve_space(c, sizeof(*ri), &alloclen, ALLOC_NORMAL,
  374. JFFS2_SUMMARY_INODE_SIZE);
  375. if (ret) {
  376. jffs2_free_raw_inode(ri);
  377. return ret;
  378. }
  379. inode = jffs2_new_inode(dir_i, mode, ri);
  380. if (IS_ERR(inode)) {
  381. jffs2_free_raw_inode(ri);
  382. jffs2_complete_reservation(c);
  383. return PTR_ERR(inode);
  384. }
  385. inode->i_op = &jffs2_dir_inode_operations;
  386. inode->i_fop = &jffs2_dir_operations;
  387. f = JFFS2_INODE_INFO(inode);
  388. /* Directories get nlink 2 at start */
  389. set_nlink(inode, 2);
  390. /* but ic->pino_nlink is the parent ino# */
  391. f->inocache->pino_nlink = dir_i->i_ino;
  392. ri->data_crc = cpu_to_je32(0);
  393. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  394. fn = jffs2_write_dnode(c, f, ri, NULL, 0, ALLOC_NORMAL);
  395. jffs2_free_raw_inode(ri);
  396. if (IS_ERR(fn)) {
  397. /* Eeek. Wave bye bye */
  398. mutex_unlock(&f->sem);
  399. jffs2_complete_reservation(c);
  400. ret = PTR_ERR(fn);
  401. goto fail;
  402. }
  403. /* No data here. Only a metadata node, which will be
  404. obsoleted by the first data write
  405. */
  406. f->metadata = fn;
  407. mutex_unlock(&f->sem);
  408. jffs2_complete_reservation(c);
  409. ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
  410. if (ret)
  411. goto fail;
  412. ret = jffs2_init_acl_post(inode);
  413. if (ret)
  414. goto fail;
  415. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  416. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  417. if (ret)
  418. goto fail;
  419. rd = jffs2_alloc_raw_dirent();
  420. if (!rd) {
  421. /* Argh. Now we treat it like a normal delete */
  422. jffs2_complete_reservation(c);
  423. ret = -ENOMEM;
  424. goto fail;
  425. }
  426. dir_f = JFFS2_INODE_INFO(dir_i);
  427. mutex_lock(&dir_f->sem);
  428. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  429. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  430. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  431. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  432. rd->pino = cpu_to_je32(dir_i->i_ino);
  433. rd->version = cpu_to_je32(++dir_f->highest_version);
  434. rd->ino = cpu_to_je32(inode->i_ino);
  435. rd->mctime = cpu_to_je32(get_seconds());
  436. rd->nsize = namelen;
  437. rd->type = DT_DIR;
  438. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  439. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  440. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
  441. if (IS_ERR(fd)) {
  442. /* dirent failed to write. Delete the inode normally
  443. as if it were the final unlink() */
  444. jffs2_complete_reservation(c);
  445. jffs2_free_raw_dirent(rd);
  446. mutex_unlock(&dir_f->sem);
  447. ret = PTR_ERR(fd);
  448. goto fail;
  449. }
  450. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  451. inc_nlink(dir_i);
  452. jffs2_free_raw_dirent(rd);
  453. /* Link the fd into the inode's list, obsoleting an old
  454. one if necessary. */
  455. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  456. mutex_unlock(&dir_f->sem);
  457. jffs2_complete_reservation(c);
  458. d_instantiate_new(dentry, inode);
  459. return 0;
  460. fail:
  461. iget_failed(inode);
  462. return ret;
  463. }
  464. static int jffs2_rmdir (struct inode *dir_i, struct dentry *dentry)
  465. {
  466. struct jffs2_sb_info *c = JFFS2_SB_INFO(dir_i->i_sb);
  467. struct jffs2_inode_info *dir_f = JFFS2_INODE_INFO(dir_i);
  468. struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode(dentry));
  469. struct jffs2_full_dirent *fd;
  470. int ret;
  471. uint32_t now = get_seconds();
  472. for (fd = f->dents ; fd; fd = fd->next) {
  473. if (fd->ino)
  474. return -ENOTEMPTY;
  475. }
  476. ret = jffs2_do_unlink(c, dir_f, dentry->d_name.name,
  477. dentry->d_name.len, f, now);
  478. if (!ret) {
  479. dir_i->i_mtime = dir_i->i_ctime = ITIME(now);
  480. clear_nlink(d_inode(dentry));
  481. drop_nlink(dir_i);
  482. }
  483. return ret;
  484. }
  485. static int jffs2_mknod (struct inode *dir_i, struct dentry *dentry, umode_t mode, dev_t rdev)
  486. {
  487. struct jffs2_inode_info *f, *dir_f;
  488. struct jffs2_sb_info *c;
  489. struct inode *inode;
  490. struct jffs2_raw_inode *ri;
  491. struct jffs2_raw_dirent *rd;
  492. struct jffs2_full_dnode *fn;
  493. struct jffs2_full_dirent *fd;
  494. int namelen;
  495. union jffs2_device_node dev;
  496. int devlen = 0;
  497. uint32_t alloclen;
  498. int ret;
  499. ri = jffs2_alloc_raw_inode();
  500. if (!ri)
  501. return -ENOMEM;
  502. c = JFFS2_SB_INFO(dir_i->i_sb);
  503. if (S_ISBLK(mode) || S_ISCHR(mode))
  504. devlen = jffs2_encode_dev(&dev, rdev);
  505. /* Try to reserve enough space for both node and dirent.
  506. * Just the node will do for now, though
  507. */
  508. namelen = dentry->d_name.len;
  509. ret = jffs2_reserve_space(c, sizeof(*ri) + devlen, &alloclen,
  510. ALLOC_NORMAL, JFFS2_SUMMARY_INODE_SIZE);
  511. if (ret) {
  512. jffs2_free_raw_inode(ri);
  513. return ret;
  514. }
  515. inode = jffs2_new_inode(dir_i, mode, ri);
  516. if (IS_ERR(inode)) {
  517. jffs2_free_raw_inode(ri);
  518. jffs2_complete_reservation(c);
  519. return PTR_ERR(inode);
  520. }
  521. inode->i_op = &jffs2_file_inode_operations;
  522. init_special_inode(inode, inode->i_mode, rdev);
  523. f = JFFS2_INODE_INFO(inode);
  524. ri->dsize = ri->csize = cpu_to_je32(devlen);
  525. ri->totlen = cpu_to_je32(sizeof(*ri) + devlen);
  526. ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
  527. ri->compr = JFFS2_COMPR_NONE;
  528. ri->data_crc = cpu_to_je32(crc32(0, &dev, devlen));
  529. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  530. fn = jffs2_write_dnode(c, f, ri, (char *)&dev, devlen, ALLOC_NORMAL);
  531. jffs2_free_raw_inode(ri);
  532. if (IS_ERR(fn)) {
  533. /* Eeek. Wave bye bye */
  534. mutex_unlock(&f->sem);
  535. jffs2_complete_reservation(c);
  536. ret = PTR_ERR(fn);
  537. goto fail;
  538. }
  539. /* No data here. Only a metadata node, which will be
  540. obsoleted by the first data write
  541. */
  542. f->metadata = fn;
  543. mutex_unlock(&f->sem);
  544. jffs2_complete_reservation(c);
  545. ret = jffs2_init_security(inode, dir_i, &dentry->d_name);
  546. if (ret)
  547. goto fail;
  548. ret = jffs2_init_acl_post(inode);
  549. if (ret)
  550. goto fail;
  551. ret = jffs2_reserve_space(c, sizeof(*rd)+namelen, &alloclen,
  552. ALLOC_NORMAL, JFFS2_SUMMARY_DIRENT_SIZE(namelen));
  553. if (ret)
  554. goto fail;
  555. rd = jffs2_alloc_raw_dirent();
  556. if (!rd) {
  557. /* Argh. Now we treat it like a normal delete */
  558. jffs2_complete_reservation(c);
  559. ret = -ENOMEM;
  560. goto fail;
  561. }
  562. dir_f = JFFS2_INODE_INFO(dir_i);
  563. mutex_lock(&dir_f->sem);
  564. rd->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  565. rd->nodetype = cpu_to_je16(JFFS2_NODETYPE_DIRENT);
  566. rd->totlen = cpu_to_je32(sizeof(*rd) + namelen);
  567. rd->hdr_crc = cpu_to_je32(crc32(0, rd, sizeof(struct jffs2_unknown_node)-4));
  568. rd->pino = cpu_to_je32(dir_i->i_ino);
  569. rd->version = cpu_to_je32(++dir_f->highest_version);
  570. rd->ino = cpu_to_je32(inode->i_ino);
  571. rd->mctime = cpu_to_je32(get_seconds());
  572. rd->nsize = namelen;
  573. /* XXX: This is ugly. */
  574. rd->type = (mode & S_IFMT) >> 12;
  575. rd->node_crc = cpu_to_je32(crc32(0, rd, sizeof(*rd)-8));
  576. rd->name_crc = cpu_to_je32(crc32(0, dentry->d_name.name, namelen));
  577. fd = jffs2_write_dirent(c, dir_f, rd, dentry->d_name.name, namelen, ALLOC_NORMAL);
  578. if (IS_ERR(fd)) {
  579. /* dirent failed to write. Delete the inode normally
  580. as if it were the final unlink() */
  581. jffs2_complete_reservation(c);
  582. jffs2_free_raw_dirent(rd);
  583. mutex_unlock(&dir_f->sem);
  584. ret = PTR_ERR(fd);
  585. goto fail;
  586. }
  587. dir_i->i_mtime = dir_i->i_ctime = ITIME(je32_to_cpu(rd->mctime));
  588. jffs2_free_raw_dirent(rd);
  589. /* Link the fd into the inode's list, obsoleting an old
  590. one if necessary. */
  591. jffs2_add_fd_to_list(c, fd, &dir_f->dents);
  592. mutex_unlock(&dir_f->sem);
  593. jffs2_complete_reservation(c);
  594. d_instantiate_new(dentry, inode);
  595. return 0;
  596. fail:
  597. iget_failed(inode);
  598. return ret;
  599. }
  600. static int jffs2_rename (struct inode *old_dir_i, struct dentry *old_dentry,
  601. struct inode *new_dir_i, struct dentry *new_dentry,
  602. unsigned int flags)
  603. {
  604. int ret;
  605. struct jffs2_sb_info *c = JFFS2_SB_INFO(old_dir_i->i_sb);
  606. struct jffs2_inode_info *victim_f = NULL;
  607. uint8_t type;
  608. uint32_t now;
  609. if (flags & ~RENAME_NOREPLACE)
  610. return -EINVAL;
  611. /* The VFS will check for us and prevent trying to rename a
  612. * file over a directory and vice versa, but if it's a directory,
  613. * the VFS can't check whether the victim is empty. The filesystem
  614. * needs to do that for itself.
  615. */
  616. if (d_really_is_positive(new_dentry)) {
  617. victim_f = JFFS2_INODE_INFO(d_inode(new_dentry));
  618. if (d_is_dir(new_dentry)) {
  619. struct jffs2_full_dirent *fd;
  620. mutex_lock(&victim_f->sem);
  621. for (fd = victim_f->dents; fd; fd = fd->next) {
  622. if (fd->ino) {
  623. mutex_unlock(&victim_f->sem);
  624. return -ENOTEMPTY;
  625. }
  626. }
  627. mutex_unlock(&victim_f->sem);
  628. }
  629. }
  630. /* XXX: We probably ought to alloc enough space for
  631. both nodes at the same time. Writing the new link,
  632. then getting -ENOSPC, is quite bad :)
  633. */
  634. /* Make a hard link */
  635. /* XXX: This is ugly */
  636. type = (d_inode(old_dentry)->i_mode & S_IFMT) >> 12;
  637. if (!type) type = DT_REG;
  638. now = get_seconds();
  639. ret = jffs2_do_link(c, JFFS2_INODE_INFO(new_dir_i),
  640. d_inode(old_dentry)->i_ino, type,
  641. new_dentry->d_name.name, new_dentry->d_name.len, now);
  642. if (ret)
  643. return ret;
  644. if (victim_f) {
  645. /* There was a victim. Kill it off nicely */
  646. if (d_is_dir(new_dentry))
  647. clear_nlink(d_inode(new_dentry));
  648. else
  649. drop_nlink(d_inode(new_dentry));
  650. /* Don't oops if the victim was a dirent pointing to an
  651. inode which didn't exist. */
  652. if (victim_f->inocache) {
  653. mutex_lock(&victim_f->sem);
  654. if (d_is_dir(new_dentry))
  655. victim_f->inocache->pino_nlink = 0;
  656. else
  657. victim_f->inocache->pino_nlink--;
  658. mutex_unlock(&victim_f->sem);
  659. }
  660. }
  661. /* If it was a directory we moved, and there was no victim,
  662. increase i_nlink on its new parent */
  663. if (d_is_dir(old_dentry) && !victim_f)
  664. inc_nlink(new_dir_i);
  665. /* Unlink the original */
  666. ret = jffs2_do_unlink(c, JFFS2_INODE_INFO(old_dir_i),
  667. old_dentry->d_name.name, old_dentry->d_name.len, NULL, now);
  668. /* We don't touch inode->i_nlink */
  669. if (ret) {
  670. /* Oh shit. We really ought to make a single node which can do both atomically */
  671. struct jffs2_inode_info *f = JFFS2_INODE_INFO(d_inode(old_dentry));
  672. mutex_lock(&f->sem);
  673. inc_nlink(d_inode(old_dentry));
  674. if (f->inocache && !d_is_dir(old_dentry))
  675. f->inocache->pino_nlink++;
  676. mutex_unlock(&f->sem);
  677. pr_notice("%s(): Link succeeded, unlink failed (err %d). You now have a hard link\n",
  678. __func__, ret);
  679. /*
  680. * We can't keep the target in dcache after that.
  681. * For one thing, we can't afford dentry aliases for directories.
  682. * For another, if there was a victim, we _can't_ set new inode
  683. * for that sucker and we have to trigger mount eviction - the
  684. * caller won't do it on its own since we are returning an error.
  685. */
  686. d_invalidate(new_dentry);
  687. new_dir_i->i_mtime = new_dir_i->i_ctime = ITIME(now);
  688. return ret;
  689. }
  690. if (d_is_dir(old_dentry))
  691. drop_nlink(old_dir_i);
  692. new_dir_i->i_mtime = new_dir_i->i_ctime = old_dir_i->i_mtime = old_dir_i->i_ctime = ITIME(now);
  693. return 0;
  694. }