dir.c 22 KB

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