dir.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  1. /*
  2. * fs/logfs/dir.c - directory-related code
  3. *
  4. * As should be obvious for Linux kernel code, license is GPLv2
  5. *
  6. * Copyright (c) 2005-2008 Joern Engel <joern@logfs.org>
  7. */
  8. #include "logfs.h"
  9. #include <linux/slab.h>
  10. /*
  11. * Atomic dir operations
  12. *
  13. * Directory operations are by default not atomic. Dentries and Inodes are
  14. * created/removed/altered in separate operations. Therefore we need to do
  15. * a small amount of journaling.
  16. *
  17. * Create, link, mkdir, mknod and symlink all share the same function to do
  18. * the work: __logfs_create. This function works in two atomic steps:
  19. * 1. allocate inode (remember in journal)
  20. * 2. allocate dentry (clear journal)
  21. *
  22. * As we can only get interrupted between the two, when the inode we just
  23. * created is simply stored in the anchor. On next mount, if we were
  24. * interrupted, we delete the inode. From a users point of view the
  25. * operation never happened.
  26. *
  27. * Unlink and rmdir also share the same function: unlink. Again, this
  28. * function works in two atomic steps
  29. * 1. remove dentry (remember inode in journal)
  30. * 2. unlink inode (clear journal)
  31. *
  32. * And again, on the next mount, if we were interrupted, we delete the inode.
  33. * From a users point of view the operation succeeded.
  34. *
  35. * Rename is the real pain to deal with, harder than all the other methods
  36. * combined. Depending on the circumstances we can run into three cases.
  37. * A "target rename" where the target dentry already existed, a "local
  38. * rename" where both parent directories are identical or a "cross-directory
  39. * rename" in the remaining case.
  40. *
  41. * Local rename is atomic, as the old dentry is simply rewritten with a new
  42. * name.
  43. *
  44. * Cross-directory rename works in two steps, similar to __logfs_create and
  45. * logfs_unlink:
  46. * 1. Write new dentry (remember old dentry in journal)
  47. * 2. Remove old dentry (clear journal)
  48. *
  49. * Here we remember a dentry instead of an inode. On next mount, if we were
  50. * interrupted, we delete the dentry. From a users point of view, the
  51. * operation succeeded.
  52. *
  53. * Target rename works in three atomic steps:
  54. * 1. Attach old inode to new dentry (remember old dentry and new inode)
  55. * 2. Remove old dentry (still remember the new inode)
  56. * 3. Remove victim inode
  57. *
  58. * Here we remember both an inode an a dentry. If we get interrupted
  59. * between steps 1 and 2, we delete both the dentry and the inode. If
  60. * we get interrupted between steps 2 and 3, we delete just the inode.
  61. * In either case, the remaining objects are deleted on next mount. From
  62. * a users point of view, the operation succeeded.
  63. */
  64. static int write_dir(struct inode *dir, struct logfs_disk_dentry *dd,
  65. loff_t pos)
  66. {
  67. return logfs_inode_write(dir, dd, sizeof(*dd), pos, WF_LOCK, NULL);
  68. }
  69. static int write_inode(struct inode *inode)
  70. {
  71. return __logfs_write_inode(inode, WF_LOCK);
  72. }
  73. static s64 dir_seek_data(struct inode *inode, s64 pos)
  74. {
  75. s64 new_pos = logfs_seek_data(inode, pos);
  76. return max(pos, new_pos - 1);
  77. }
  78. static int beyond_eof(struct inode *inode, loff_t bix)
  79. {
  80. loff_t pos = bix << inode->i_sb->s_blocksize_bits;
  81. return pos >= i_size_read(inode);
  82. }
  83. /*
  84. * Prime value was chosen to be roughly 256 + 26. r5 hash uses 11,
  85. * so short names (len <= 9) don't even occupy the complete 32bit name
  86. * space. A prime >256 ensures short names quickly spread the 32bit
  87. * name space. Add about 26 for the estimated amount of information
  88. * of each character and pick a prime nearby, preferably a bit-sparse
  89. * one.
  90. */
  91. static u32 hash_32(const char *s, int len, u32 seed)
  92. {
  93. u32 hash = seed;
  94. int i;
  95. for (i = 0; i < len; i++)
  96. hash = hash * 293 + s[i];
  97. return hash;
  98. }
  99. /*
  100. * We have to satisfy several conflicting requirements here. Small
  101. * directories should stay fairly compact and not require too many
  102. * indirect blocks. The number of possible locations for a given hash
  103. * should be small to make lookup() fast. And we should try hard not
  104. * to overflow the 32bit name space or nfs and 32bit host systems will
  105. * be unhappy.
  106. *
  107. * So we use the following scheme. First we reduce the hash to 0..15
  108. * and try a direct block. If that is occupied we reduce the hash to
  109. * 16..255 and try an indirect block. Same for 2x and 3x indirect
  110. * blocks. Lastly we reduce the hash to 0x800_0000 .. 0xffff_ffff,
  111. * but use buckets containing eight entries instead of a single one.
  112. *
  113. * Using 16 entries should allow for a reasonable amount of hash
  114. * collisions, so the 32bit name space can be packed fairly tight
  115. * before overflowing. Oh and currently we don't overflow but return
  116. * and error.
  117. *
  118. * How likely are collisions? Doing the appropriate math is beyond me
  119. * and the Bronstein textbook. But running a test program to brute
  120. * force collisions for a couple of days showed that on average the
  121. * first collision occurs after 598M entries, with 290M being the
  122. * smallest result. Obviously 21 entries could already cause a
  123. * collision if all entries are carefully chosen.
  124. */
  125. static pgoff_t hash_index(u32 hash, int round)
  126. {
  127. u32 i0_blocks = I0_BLOCKS;
  128. u32 i1_blocks = I1_BLOCKS;
  129. u32 i2_blocks = I2_BLOCKS;
  130. u32 i3_blocks = I3_BLOCKS;
  131. switch (round) {
  132. case 0:
  133. return hash % i0_blocks;
  134. case 1:
  135. return i0_blocks + hash % (i1_blocks - i0_blocks);
  136. case 2:
  137. return i1_blocks + hash % (i2_blocks - i1_blocks);
  138. case 3:
  139. return i2_blocks + hash % (i3_blocks - i2_blocks);
  140. case 4 ... 19:
  141. return i3_blocks + 16 * (hash % (((1<<31) - i3_blocks) / 16))
  142. + round - 4;
  143. }
  144. BUG();
  145. }
  146. static struct page *logfs_get_dd_page(struct inode *dir, struct dentry *dentry)
  147. {
  148. struct qstr *name = &dentry->d_name;
  149. struct page *page;
  150. struct logfs_disk_dentry *dd;
  151. u32 hash = hash_32(name->name, name->len, 0);
  152. pgoff_t index;
  153. int round;
  154. if (name->len > LOGFS_MAX_NAMELEN)
  155. return ERR_PTR(-ENAMETOOLONG);
  156. for (round = 0; round < 20; round++) {
  157. index = hash_index(hash, round);
  158. if (beyond_eof(dir, index))
  159. return NULL;
  160. if (!logfs_exist_block(dir, index))
  161. continue;
  162. page = read_cache_page(dir->i_mapping, index,
  163. (filler_t *)logfs_readpage, NULL);
  164. if (IS_ERR(page))
  165. return page;
  166. dd = kmap_atomic(page, KM_USER0);
  167. BUG_ON(dd->namelen == 0);
  168. if (name->len != be16_to_cpu(dd->namelen) ||
  169. memcmp(name->name, dd->name, name->len)) {
  170. kunmap_atomic(dd, KM_USER0);
  171. page_cache_release(page);
  172. continue;
  173. }
  174. kunmap_atomic(dd, KM_USER0);
  175. return page;
  176. }
  177. return NULL;
  178. }
  179. static int logfs_remove_inode(struct inode *inode)
  180. {
  181. int ret;
  182. inode->i_nlink--;
  183. ret = write_inode(inode);
  184. LOGFS_BUG_ON(ret, inode->i_sb);
  185. return ret;
  186. }
  187. static void abort_transaction(struct inode *inode, struct logfs_transaction *ta)
  188. {
  189. if (logfs_inode(inode)->li_block)
  190. logfs_inode(inode)->li_block->ta = NULL;
  191. kfree(ta);
  192. }
  193. static int logfs_unlink(struct inode *dir, struct dentry *dentry)
  194. {
  195. struct logfs_super *super = logfs_super(dir->i_sb);
  196. struct inode *inode = dentry->d_inode;
  197. struct logfs_transaction *ta;
  198. struct page *page;
  199. pgoff_t index;
  200. int ret;
  201. ta = kzalloc(sizeof(*ta), GFP_KERNEL);
  202. if (!ta)
  203. return -ENOMEM;
  204. ta->state = UNLINK_1;
  205. ta->ino = inode->i_ino;
  206. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  207. page = logfs_get_dd_page(dir, dentry);
  208. if (!page) {
  209. kfree(ta);
  210. return -ENOENT;
  211. }
  212. if (IS_ERR(page)) {
  213. kfree(ta);
  214. return PTR_ERR(page);
  215. }
  216. index = page->index;
  217. page_cache_release(page);
  218. mutex_lock(&super->s_dirop_mutex);
  219. logfs_add_transaction(dir, ta);
  220. ret = logfs_delete(dir, index, NULL);
  221. if (!ret)
  222. ret = write_inode(dir);
  223. if (ret) {
  224. abort_transaction(dir, ta);
  225. printk(KERN_ERR"LOGFS: unable to delete inode\n");
  226. goto out;
  227. }
  228. ta->state = UNLINK_2;
  229. logfs_add_transaction(inode, ta);
  230. ret = logfs_remove_inode(inode);
  231. out:
  232. mutex_unlock(&super->s_dirop_mutex);
  233. return ret;
  234. }
  235. static inline int logfs_empty_dir(struct inode *dir)
  236. {
  237. u64 data;
  238. data = logfs_seek_data(dir, 0) << dir->i_sb->s_blocksize_bits;
  239. return data >= i_size_read(dir);
  240. }
  241. static int logfs_rmdir(struct inode *dir, struct dentry *dentry)
  242. {
  243. struct inode *inode = dentry->d_inode;
  244. if (!logfs_empty_dir(inode))
  245. return -ENOTEMPTY;
  246. return logfs_unlink(dir, dentry);
  247. }
  248. /* FIXME: readdir currently has it's own dir_walk code. I don't see a good
  249. * way to combine the two copies */
  250. #define IMPLICIT_NODES 2
  251. static int __logfs_readdir(struct file *file, void *buf, filldir_t filldir)
  252. {
  253. struct inode *dir = file->f_dentry->d_inode;
  254. loff_t pos = file->f_pos - IMPLICIT_NODES;
  255. struct page *page;
  256. struct logfs_disk_dentry *dd;
  257. int full;
  258. BUG_ON(pos < 0);
  259. for (;; pos++) {
  260. if (beyond_eof(dir, pos))
  261. break;
  262. if (!logfs_exist_block(dir, pos)) {
  263. /* deleted dentry */
  264. pos = dir_seek_data(dir, pos);
  265. continue;
  266. }
  267. page = read_cache_page(dir->i_mapping, pos,
  268. (filler_t *)logfs_readpage, NULL);
  269. if (IS_ERR(page))
  270. return PTR_ERR(page);
  271. dd = kmap(page);
  272. BUG_ON(dd->namelen == 0);
  273. full = filldir(buf, (char *)dd->name, be16_to_cpu(dd->namelen),
  274. pos, be64_to_cpu(dd->ino), dd->type);
  275. kunmap(page);
  276. page_cache_release(page);
  277. if (full)
  278. break;
  279. }
  280. file->f_pos = pos + IMPLICIT_NODES;
  281. return 0;
  282. }
  283. static int logfs_readdir(struct file *file, void *buf, filldir_t filldir)
  284. {
  285. struct inode *inode = file->f_dentry->d_inode;
  286. ino_t pino = parent_ino(file->f_dentry);
  287. int err;
  288. if (file->f_pos < 0)
  289. return -EINVAL;
  290. if (file->f_pos == 0) {
  291. if (filldir(buf, ".", 1, 1, inode->i_ino, DT_DIR) < 0)
  292. return 0;
  293. file->f_pos++;
  294. }
  295. if (file->f_pos == 1) {
  296. if (filldir(buf, "..", 2, 2, pino, DT_DIR) < 0)
  297. return 0;
  298. file->f_pos++;
  299. }
  300. err = __logfs_readdir(file, buf, filldir);
  301. return err;
  302. }
  303. static void logfs_set_name(struct logfs_disk_dentry *dd, struct qstr *name)
  304. {
  305. dd->namelen = cpu_to_be16(name->len);
  306. memcpy(dd->name, name->name, name->len);
  307. }
  308. static struct dentry *logfs_lookup(struct inode *dir, struct dentry *dentry,
  309. struct nameidata *nd)
  310. {
  311. struct page *page;
  312. struct logfs_disk_dentry *dd;
  313. pgoff_t index;
  314. u64 ino = 0;
  315. struct inode *inode;
  316. page = logfs_get_dd_page(dir, dentry);
  317. if (IS_ERR(page))
  318. return ERR_CAST(page);
  319. if (!page) {
  320. d_add(dentry, NULL);
  321. return NULL;
  322. }
  323. index = page->index;
  324. dd = kmap_atomic(page, KM_USER0);
  325. ino = be64_to_cpu(dd->ino);
  326. kunmap_atomic(dd, KM_USER0);
  327. page_cache_release(page);
  328. inode = logfs_iget(dir->i_sb, ino);
  329. if (IS_ERR(inode)) {
  330. printk(KERN_ERR"LogFS: Cannot read inode #%llx for dentry (%lx, %lx)n",
  331. ino, dir->i_ino, index);
  332. return ERR_CAST(inode);
  333. }
  334. return d_splice_alias(inode, dentry);
  335. }
  336. static void grow_dir(struct inode *dir, loff_t index)
  337. {
  338. index = (index + 1) << dir->i_sb->s_blocksize_bits;
  339. if (i_size_read(dir) < index)
  340. i_size_write(dir, index);
  341. }
  342. static int logfs_write_dir(struct inode *dir, struct dentry *dentry,
  343. struct inode *inode)
  344. {
  345. struct page *page;
  346. struct logfs_disk_dentry *dd;
  347. u32 hash = hash_32(dentry->d_name.name, dentry->d_name.len, 0);
  348. pgoff_t index;
  349. int round, err;
  350. for (round = 0; round < 20; round++) {
  351. index = hash_index(hash, round);
  352. if (logfs_exist_block(dir, index))
  353. continue;
  354. page = find_or_create_page(dir->i_mapping, index, GFP_KERNEL);
  355. if (!page)
  356. return -ENOMEM;
  357. dd = kmap_atomic(page, KM_USER0);
  358. memset(dd, 0, sizeof(*dd));
  359. dd->ino = cpu_to_be64(inode->i_ino);
  360. dd->type = logfs_type(inode);
  361. logfs_set_name(dd, &dentry->d_name);
  362. kunmap_atomic(dd, KM_USER0);
  363. err = logfs_write_buf(dir, page, WF_LOCK);
  364. unlock_page(page);
  365. page_cache_release(page);
  366. if (!err)
  367. grow_dir(dir, index);
  368. return err;
  369. }
  370. /* FIXME: Is there a better return value? In most cases neither
  371. * the filesystem nor the directory are full. But we have had
  372. * too many collisions for this particular hash and no fallback.
  373. */
  374. return -ENOSPC;
  375. }
  376. static int __logfs_create(struct inode *dir, struct dentry *dentry,
  377. struct inode *inode, const char *dest, long destlen)
  378. {
  379. struct logfs_super *super = logfs_super(dir->i_sb);
  380. struct logfs_inode *li = logfs_inode(inode);
  381. struct logfs_transaction *ta;
  382. int ret;
  383. ta = kzalloc(sizeof(*ta), GFP_KERNEL);
  384. if (!ta) {
  385. inode->i_nlink--;
  386. iput(inode);
  387. return -ENOMEM;
  388. }
  389. ta->state = CREATE_1;
  390. ta->ino = inode->i_ino;
  391. mutex_lock(&super->s_dirop_mutex);
  392. logfs_add_transaction(inode, ta);
  393. if (dest) {
  394. /* symlink */
  395. ret = logfs_inode_write(inode, dest, destlen, 0, WF_LOCK, NULL);
  396. if (!ret)
  397. ret = write_inode(inode);
  398. } else {
  399. /* creat/mkdir/mknod */
  400. ret = write_inode(inode);
  401. }
  402. if (ret) {
  403. abort_transaction(inode, ta);
  404. li->li_flags |= LOGFS_IF_STILLBORN;
  405. /* FIXME: truncate symlink */
  406. inode->i_nlink--;
  407. iput(inode);
  408. goto out;
  409. }
  410. ta->state = CREATE_2;
  411. logfs_add_transaction(dir, ta);
  412. ret = logfs_write_dir(dir, dentry, inode);
  413. /* sync directory */
  414. if (!ret)
  415. ret = write_inode(dir);
  416. if (ret) {
  417. logfs_del_transaction(dir, ta);
  418. ta->state = CREATE_2;
  419. logfs_add_transaction(inode, ta);
  420. logfs_remove_inode(inode);
  421. iput(inode);
  422. goto out;
  423. }
  424. d_instantiate(dentry, inode);
  425. out:
  426. mutex_unlock(&super->s_dirop_mutex);
  427. return ret;
  428. }
  429. static int logfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
  430. {
  431. struct inode *inode;
  432. /*
  433. * FIXME: why do we have to fill in S_IFDIR, while the mode is
  434. * correct for mknod, creat, etc.? Smells like the vfs *should*
  435. * do it for us but for some reason fails to do so.
  436. */
  437. inode = logfs_new_inode(dir, S_IFDIR | mode);
  438. if (IS_ERR(inode))
  439. return PTR_ERR(inode);
  440. inode->i_op = &logfs_dir_iops;
  441. inode->i_fop = &logfs_dir_fops;
  442. return __logfs_create(dir, dentry, inode, NULL, 0);
  443. }
  444. static int logfs_create(struct inode *dir, struct dentry *dentry, int mode,
  445. struct nameidata *nd)
  446. {
  447. struct inode *inode;
  448. inode = logfs_new_inode(dir, mode);
  449. if (IS_ERR(inode))
  450. return PTR_ERR(inode);
  451. inode->i_op = &logfs_reg_iops;
  452. inode->i_fop = &logfs_reg_fops;
  453. inode->i_mapping->a_ops = &logfs_reg_aops;
  454. return __logfs_create(dir, dentry, inode, NULL, 0);
  455. }
  456. static int logfs_mknod(struct inode *dir, struct dentry *dentry, int mode,
  457. dev_t rdev)
  458. {
  459. struct inode *inode;
  460. if (dentry->d_name.len > LOGFS_MAX_NAMELEN)
  461. return -ENAMETOOLONG;
  462. inode = logfs_new_inode(dir, mode);
  463. if (IS_ERR(inode))
  464. return PTR_ERR(inode);
  465. init_special_inode(inode, mode, rdev);
  466. return __logfs_create(dir, dentry, inode, NULL, 0);
  467. }
  468. static int logfs_symlink(struct inode *dir, struct dentry *dentry,
  469. const char *target)
  470. {
  471. struct inode *inode;
  472. size_t destlen = strlen(target) + 1;
  473. if (destlen > dir->i_sb->s_blocksize)
  474. return -ENAMETOOLONG;
  475. inode = logfs_new_inode(dir, S_IFLNK | 0777);
  476. if (IS_ERR(inode))
  477. return PTR_ERR(inode);
  478. inode->i_op = &logfs_symlink_iops;
  479. inode->i_mapping->a_ops = &logfs_reg_aops;
  480. return __logfs_create(dir, dentry, inode, target, destlen);
  481. }
  482. static int logfs_link(struct dentry *old_dentry, struct inode *dir,
  483. struct dentry *dentry)
  484. {
  485. struct inode *inode = old_dentry->d_inode;
  486. if (inode->i_nlink >= LOGFS_LINK_MAX)
  487. return -EMLINK;
  488. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  489. ihold(inode);
  490. inode->i_nlink++;
  491. mark_inode_dirty_sync(inode);
  492. return __logfs_create(dir, dentry, inode, NULL, 0);
  493. }
  494. static int logfs_get_dd(struct inode *dir, struct dentry *dentry,
  495. struct logfs_disk_dentry *dd, loff_t *pos)
  496. {
  497. struct page *page;
  498. void *map;
  499. page = logfs_get_dd_page(dir, dentry);
  500. if (IS_ERR(page))
  501. return PTR_ERR(page);
  502. *pos = page->index;
  503. map = kmap_atomic(page, KM_USER0);
  504. memcpy(dd, map, sizeof(*dd));
  505. kunmap_atomic(map, KM_USER0);
  506. page_cache_release(page);
  507. return 0;
  508. }
  509. static int logfs_delete_dd(struct inode *dir, loff_t pos)
  510. {
  511. /*
  512. * Getting called with pos somewhere beyond eof is either a goofup
  513. * within this file or means someone maliciously edited the
  514. * (crc-protected) journal.
  515. */
  516. BUG_ON(beyond_eof(dir, pos));
  517. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  518. log_dir(" Delete dentry (%lx, %llx)\n", dir->i_ino, pos);
  519. return logfs_delete(dir, pos, NULL);
  520. }
  521. /*
  522. * Cross-directory rename, target does not exist. Just a little nasty.
  523. * Create a new dentry in the target dir, then remove the old dentry,
  524. * all the while taking care to remember our operation in the journal.
  525. */
  526. static int logfs_rename_cross(struct inode *old_dir, struct dentry *old_dentry,
  527. struct inode *new_dir, struct dentry *new_dentry)
  528. {
  529. struct logfs_super *super = logfs_super(old_dir->i_sb);
  530. struct logfs_disk_dentry dd;
  531. struct logfs_transaction *ta;
  532. loff_t pos;
  533. int err;
  534. /* 1. locate source dd */
  535. err = logfs_get_dd(old_dir, old_dentry, &dd, &pos);
  536. if (err)
  537. return err;
  538. ta = kzalloc(sizeof(*ta), GFP_KERNEL);
  539. if (!ta)
  540. return -ENOMEM;
  541. ta->state = CROSS_RENAME_1;
  542. ta->dir = old_dir->i_ino;
  543. ta->pos = pos;
  544. /* 2. write target dd */
  545. mutex_lock(&super->s_dirop_mutex);
  546. logfs_add_transaction(new_dir, ta);
  547. err = logfs_write_dir(new_dir, new_dentry, old_dentry->d_inode);
  548. if (!err)
  549. err = write_inode(new_dir);
  550. if (err) {
  551. super->s_rename_dir = 0;
  552. super->s_rename_pos = 0;
  553. abort_transaction(new_dir, ta);
  554. goto out;
  555. }
  556. /* 3. remove source dd */
  557. ta->state = CROSS_RENAME_2;
  558. logfs_add_transaction(old_dir, ta);
  559. err = logfs_delete_dd(old_dir, pos);
  560. if (!err)
  561. err = write_inode(old_dir);
  562. LOGFS_BUG_ON(err, old_dir->i_sb);
  563. out:
  564. mutex_unlock(&super->s_dirop_mutex);
  565. return err;
  566. }
  567. static int logfs_replace_inode(struct inode *dir, struct dentry *dentry,
  568. struct logfs_disk_dentry *dd, struct inode *inode)
  569. {
  570. loff_t pos;
  571. int err;
  572. err = logfs_get_dd(dir, dentry, dd, &pos);
  573. if (err)
  574. return err;
  575. dd->ino = cpu_to_be64(inode->i_ino);
  576. dd->type = logfs_type(inode);
  577. err = write_dir(dir, dd, pos);
  578. if (err)
  579. return err;
  580. log_dir("Replace dentry (%lx, %llx) %s -> %llx\n", dir->i_ino, pos,
  581. dd->name, be64_to_cpu(dd->ino));
  582. return write_inode(dir);
  583. }
  584. /* Target dentry exists - the worst case. We need to attach the source
  585. * inode to the target dentry, then remove the orphaned target inode and
  586. * source dentry.
  587. */
  588. static int logfs_rename_target(struct inode *old_dir, struct dentry *old_dentry,
  589. struct inode *new_dir, struct dentry *new_dentry)
  590. {
  591. struct logfs_super *super = logfs_super(old_dir->i_sb);
  592. struct inode *old_inode = old_dentry->d_inode;
  593. struct inode *new_inode = new_dentry->d_inode;
  594. int isdir = S_ISDIR(old_inode->i_mode);
  595. struct logfs_disk_dentry dd;
  596. struct logfs_transaction *ta;
  597. loff_t pos;
  598. int err;
  599. BUG_ON(isdir != S_ISDIR(new_inode->i_mode));
  600. if (isdir) {
  601. if (!logfs_empty_dir(new_inode))
  602. return -ENOTEMPTY;
  603. }
  604. /* 1. locate source dd */
  605. err = logfs_get_dd(old_dir, old_dentry, &dd, &pos);
  606. if (err)
  607. return err;
  608. ta = kzalloc(sizeof(*ta), GFP_KERNEL);
  609. if (!ta)
  610. return -ENOMEM;
  611. ta->state = TARGET_RENAME_1;
  612. ta->dir = old_dir->i_ino;
  613. ta->pos = pos;
  614. ta->ino = new_inode->i_ino;
  615. /* 2. attach source inode to target dd */
  616. mutex_lock(&super->s_dirop_mutex);
  617. logfs_add_transaction(new_dir, ta);
  618. err = logfs_replace_inode(new_dir, new_dentry, &dd, old_inode);
  619. if (err) {
  620. super->s_rename_dir = 0;
  621. super->s_rename_pos = 0;
  622. super->s_victim_ino = 0;
  623. abort_transaction(new_dir, ta);
  624. goto out;
  625. }
  626. /* 3. remove source dd */
  627. ta->state = TARGET_RENAME_2;
  628. logfs_add_transaction(old_dir, ta);
  629. err = logfs_delete_dd(old_dir, pos);
  630. if (!err)
  631. err = write_inode(old_dir);
  632. LOGFS_BUG_ON(err, old_dir->i_sb);
  633. /* 4. remove target inode */
  634. ta->state = TARGET_RENAME_3;
  635. logfs_add_transaction(new_inode, ta);
  636. err = logfs_remove_inode(new_inode);
  637. out:
  638. mutex_unlock(&super->s_dirop_mutex);
  639. return err;
  640. }
  641. static int logfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  642. struct inode *new_dir, struct dentry *new_dentry)
  643. {
  644. if (new_dentry->d_inode)
  645. return logfs_rename_target(old_dir, old_dentry,
  646. new_dir, new_dentry);
  647. return logfs_rename_cross(old_dir, old_dentry, new_dir, new_dentry);
  648. }
  649. /* No locking done here, as this is called before .get_sb() returns. */
  650. int logfs_replay_journal(struct super_block *sb)
  651. {
  652. struct logfs_super *super = logfs_super(sb);
  653. struct inode *inode;
  654. u64 ino, pos;
  655. int err;
  656. if (super->s_victim_ino) {
  657. /* delete victim inode */
  658. ino = super->s_victim_ino;
  659. printk(KERN_INFO"LogFS: delete unmapped inode #%llx\n", ino);
  660. inode = logfs_iget(sb, ino);
  661. if (IS_ERR(inode))
  662. goto fail;
  663. LOGFS_BUG_ON(i_size_read(inode) > 0, sb);
  664. super->s_victim_ino = 0;
  665. err = logfs_remove_inode(inode);
  666. iput(inode);
  667. if (err) {
  668. super->s_victim_ino = ino;
  669. goto fail;
  670. }
  671. }
  672. if (super->s_rename_dir) {
  673. /* delete old dd from rename */
  674. ino = super->s_rename_dir;
  675. pos = super->s_rename_pos;
  676. printk(KERN_INFO"LogFS: delete unbacked dentry (%llx, %llx)\n",
  677. ino, pos);
  678. inode = logfs_iget(sb, ino);
  679. if (IS_ERR(inode))
  680. goto fail;
  681. super->s_rename_dir = 0;
  682. super->s_rename_pos = 0;
  683. err = logfs_delete_dd(inode, pos);
  684. iput(inode);
  685. if (err) {
  686. super->s_rename_dir = ino;
  687. super->s_rename_pos = pos;
  688. goto fail;
  689. }
  690. }
  691. return 0;
  692. fail:
  693. LOGFS_BUG(sb);
  694. return -EIO;
  695. }
  696. const struct inode_operations logfs_symlink_iops = {
  697. .readlink = generic_readlink,
  698. .follow_link = page_follow_link_light,
  699. };
  700. const struct inode_operations logfs_dir_iops = {
  701. .create = logfs_create,
  702. .link = logfs_link,
  703. .lookup = logfs_lookup,
  704. .mkdir = logfs_mkdir,
  705. .mknod = logfs_mknod,
  706. .rename = logfs_rename,
  707. .rmdir = logfs_rmdir,
  708. .symlink = logfs_symlink,
  709. .unlink = logfs_unlink,
  710. };
  711. const struct file_operations logfs_dir_fops = {
  712. .fsync = logfs_fsync,
  713. .unlocked_ioctl = logfs_ioctl,
  714. .readdir = logfs_readdir,
  715. .read = generic_read_dir,
  716. .llseek = default_llseek,
  717. };