dir.c 23 KB

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