dir.c 23 KB

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