dir.c 23 KB

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