dir.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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, NULL, 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);
  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);
  171. page_cache_release(page);
  172. continue;
  173. }
  174. kunmap_atomic(dd);
  175. return page;
  176. }
  177. return NULL;
  178. }
  179. static int logfs_remove_inode(struct inode *inode)
  180. {
  181. int ret;
  182. drop_nlink(inode);
  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 = d_inode(dentry);
  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 = d_inode(dentry);
  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. static int logfs_readdir(struct file *file, struct dir_context *ctx)
  251. {
  252. struct inode *dir = file_inode(file);
  253. loff_t pos;
  254. struct page *page;
  255. struct logfs_disk_dentry *dd;
  256. if (ctx->pos < 0)
  257. return -EINVAL;
  258. if (!dir_emit_dots(file, ctx))
  259. return 0;
  260. pos = ctx->pos - 2;
  261. BUG_ON(pos < 0);
  262. for (;; pos++, ctx->pos++) {
  263. bool full;
  264. if (beyond_eof(dir, pos))
  265. break;
  266. if (!logfs_exist_block(dir, pos)) {
  267. /* deleted dentry */
  268. pos = dir_seek_data(dir, pos);
  269. continue;
  270. }
  271. page = read_cache_page(dir->i_mapping, pos,
  272. (filler_t *)logfs_readpage, NULL);
  273. if (IS_ERR(page))
  274. return PTR_ERR(page);
  275. dd = kmap(page);
  276. BUG_ON(dd->namelen == 0);
  277. full = !dir_emit(ctx, (char *)dd->name,
  278. be16_to_cpu(dd->namelen),
  279. be64_to_cpu(dd->ino), dd->type);
  280. kunmap(page);
  281. page_cache_release(page);
  282. if (full)
  283. break;
  284. }
  285. return 0;
  286. }
  287. static void logfs_set_name(struct logfs_disk_dentry *dd, struct qstr *name)
  288. {
  289. dd->namelen = cpu_to_be16(name->len);
  290. memcpy(dd->name, name->name, name->len);
  291. }
  292. static struct dentry *logfs_lookup(struct inode *dir, struct dentry *dentry,
  293. unsigned int flags)
  294. {
  295. struct page *page;
  296. struct logfs_disk_dentry *dd;
  297. pgoff_t index;
  298. u64 ino = 0;
  299. struct inode *inode;
  300. page = logfs_get_dd_page(dir, dentry);
  301. if (IS_ERR(page))
  302. return ERR_CAST(page);
  303. if (!page) {
  304. d_add(dentry, NULL);
  305. return NULL;
  306. }
  307. index = page->index;
  308. dd = kmap_atomic(page);
  309. ino = be64_to_cpu(dd->ino);
  310. kunmap_atomic(dd);
  311. page_cache_release(page);
  312. inode = logfs_iget(dir->i_sb, ino);
  313. if (IS_ERR(inode))
  314. printk(KERN_ERR"LogFS: Cannot read inode #%llx for dentry (%lx, %lx)n",
  315. ino, dir->i_ino, index);
  316. return d_splice_alias(inode, dentry);
  317. }
  318. static void grow_dir(struct inode *dir, loff_t index)
  319. {
  320. index = (index + 1) << dir->i_sb->s_blocksize_bits;
  321. if (i_size_read(dir) < index)
  322. i_size_write(dir, index);
  323. }
  324. static int logfs_write_dir(struct inode *dir, struct dentry *dentry,
  325. struct inode *inode)
  326. {
  327. struct page *page;
  328. struct logfs_disk_dentry *dd;
  329. u32 hash = hash_32(dentry->d_name.name, dentry->d_name.len, 0);
  330. pgoff_t index;
  331. int round, err;
  332. for (round = 0; round < 20; round++) {
  333. index = hash_index(hash, round);
  334. if (logfs_exist_block(dir, index))
  335. continue;
  336. page = find_or_create_page(dir->i_mapping, index, GFP_KERNEL);
  337. if (!page)
  338. return -ENOMEM;
  339. dd = kmap_atomic(page);
  340. memset(dd, 0, sizeof(*dd));
  341. dd->ino = cpu_to_be64(inode->i_ino);
  342. dd->type = logfs_type(inode);
  343. logfs_set_name(dd, &dentry->d_name);
  344. kunmap_atomic(dd);
  345. err = logfs_write_buf(dir, page, WF_LOCK);
  346. unlock_page(page);
  347. page_cache_release(page);
  348. if (!err)
  349. grow_dir(dir, index);
  350. return err;
  351. }
  352. /* FIXME: Is there a better return value? In most cases neither
  353. * the filesystem nor the directory are full. But we have had
  354. * too many collisions for this particular hash and no fallback.
  355. */
  356. return -ENOSPC;
  357. }
  358. static int __logfs_create(struct inode *dir, struct dentry *dentry,
  359. struct inode *inode, const char *dest, long destlen)
  360. {
  361. struct logfs_super *super = logfs_super(dir->i_sb);
  362. struct logfs_inode *li = logfs_inode(inode);
  363. struct logfs_transaction *ta;
  364. int ret;
  365. ta = kzalloc(sizeof(*ta), GFP_KERNEL);
  366. if (!ta) {
  367. drop_nlink(inode);
  368. iput(inode);
  369. return -ENOMEM;
  370. }
  371. ta->state = CREATE_1;
  372. ta->ino = inode->i_ino;
  373. mutex_lock(&super->s_dirop_mutex);
  374. logfs_add_transaction(inode, ta);
  375. if (dest) {
  376. /* symlink */
  377. ret = logfs_inode_write(inode, dest, destlen, 0, WF_LOCK, NULL);
  378. if (!ret)
  379. ret = write_inode(inode);
  380. } else {
  381. /* creat/mkdir/mknod */
  382. ret = write_inode(inode);
  383. }
  384. if (ret) {
  385. abort_transaction(inode, ta);
  386. li->li_flags |= LOGFS_IF_STILLBORN;
  387. /* FIXME: truncate symlink */
  388. drop_nlink(inode);
  389. iput(inode);
  390. goto out;
  391. }
  392. ta->state = CREATE_2;
  393. logfs_add_transaction(dir, ta);
  394. ret = logfs_write_dir(dir, dentry, inode);
  395. /* sync directory */
  396. if (!ret)
  397. ret = write_inode(dir);
  398. if (ret) {
  399. logfs_del_transaction(dir, ta);
  400. ta->state = CREATE_2;
  401. logfs_add_transaction(inode, ta);
  402. logfs_remove_inode(inode);
  403. iput(inode);
  404. goto out;
  405. }
  406. d_instantiate(dentry, inode);
  407. out:
  408. mutex_unlock(&super->s_dirop_mutex);
  409. return ret;
  410. }
  411. static int logfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
  412. {
  413. struct inode *inode;
  414. /*
  415. * FIXME: why do we have to fill in S_IFDIR, while the mode is
  416. * correct for mknod, creat, etc.? Smells like the vfs *should*
  417. * do it for us but for some reason fails to do so.
  418. */
  419. inode = logfs_new_inode(dir, S_IFDIR | mode);
  420. if (IS_ERR(inode))
  421. return PTR_ERR(inode);
  422. inode->i_op = &logfs_dir_iops;
  423. inode->i_fop = &logfs_dir_fops;
  424. return __logfs_create(dir, dentry, inode, NULL, 0);
  425. }
  426. static int logfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  427. bool excl)
  428. {
  429. struct inode *inode;
  430. inode = logfs_new_inode(dir, mode);
  431. if (IS_ERR(inode))
  432. return PTR_ERR(inode);
  433. inode->i_op = &logfs_reg_iops;
  434. inode->i_fop = &logfs_reg_fops;
  435. inode->i_mapping->a_ops = &logfs_reg_aops;
  436. return __logfs_create(dir, dentry, inode, NULL, 0);
  437. }
  438. static int logfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
  439. dev_t rdev)
  440. {
  441. struct inode *inode;
  442. if (dentry->d_name.len > LOGFS_MAX_NAMELEN)
  443. return -ENAMETOOLONG;
  444. inode = logfs_new_inode(dir, mode);
  445. if (IS_ERR(inode))
  446. return PTR_ERR(inode);
  447. init_special_inode(inode, mode, rdev);
  448. return __logfs_create(dir, dentry, inode, NULL, 0);
  449. }
  450. static int logfs_symlink(struct inode *dir, struct dentry *dentry,
  451. const char *target)
  452. {
  453. struct inode *inode;
  454. size_t destlen = strlen(target) + 1;
  455. if (destlen > dir->i_sb->s_blocksize)
  456. return -ENAMETOOLONG;
  457. inode = logfs_new_inode(dir, S_IFLNK | 0777);
  458. if (IS_ERR(inode))
  459. return PTR_ERR(inode);
  460. inode->i_op = &logfs_symlink_iops;
  461. inode->i_mapping->a_ops = &logfs_reg_aops;
  462. return __logfs_create(dir, dentry, inode, target, destlen);
  463. }
  464. static int logfs_link(struct dentry *old_dentry, struct inode *dir,
  465. struct dentry *dentry)
  466. {
  467. struct inode *inode = d_inode(old_dentry);
  468. inode->i_ctime = dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  469. ihold(inode);
  470. inc_nlink(inode);
  471. mark_inode_dirty_sync(inode);
  472. return __logfs_create(dir, dentry, inode, NULL, 0);
  473. }
  474. static int logfs_get_dd(struct inode *dir, struct dentry *dentry,
  475. struct logfs_disk_dentry *dd, loff_t *pos)
  476. {
  477. struct page *page;
  478. void *map;
  479. page = logfs_get_dd_page(dir, dentry);
  480. if (IS_ERR(page))
  481. return PTR_ERR(page);
  482. *pos = page->index;
  483. map = kmap_atomic(page);
  484. memcpy(dd, map, sizeof(*dd));
  485. kunmap_atomic(map);
  486. page_cache_release(page);
  487. return 0;
  488. }
  489. static int logfs_delete_dd(struct inode *dir, loff_t pos)
  490. {
  491. /*
  492. * Getting called with pos somewhere beyond eof is either a goofup
  493. * within this file or means someone maliciously edited the
  494. * (crc-protected) journal.
  495. */
  496. BUG_ON(beyond_eof(dir, pos));
  497. dir->i_ctime = dir->i_mtime = CURRENT_TIME;
  498. log_dir(" Delete dentry (%lx, %llx)\n", dir->i_ino, pos);
  499. return logfs_delete(dir, pos, NULL);
  500. }
  501. /*
  502. * Cross-directory rename, target does not exist. Just a little nasty.
  503. * Create a new dentry in the target dir, then remove the old dentry,
  504. * all the while taking care to remember our operation in the journal.
  505. */
  506. static int logfs_rename_cross(struct inode *old_dir, struct dentry *old_dentry,
  507. struct inode *new_dir, struct dentry *new_dentry)
  508. {
  509. struct logfs_super *super = logfs_super(old_dir->i_sb);
  510. struct logfs_disk_dentry dd;
  511. struct logfs_transaction *ta;
  512. loff_t pos;
  513. int err;
  514. /* 1. locate source dd */
  515. err = logfs_get_dd(old_dir, old_dentry, &dd, &pos);
  516. if (err)
  517. return err;
  518. ta = kzalloc(sizeof(*ta), GFP_KERNEL);
  519. if (!ta)
  520. return -ENOMEM;
  521. ta->state = CROSS_RENAME_1;
  522. ta->dir = old_dir->i_ino;
  523. ta->pos = pos;
  524. /* 2. write target dd */
  525. mutex_lock(&super->s_dirop_mutex);
  526. logfs_add_transaction(new_dir, ta);
  527. err = logfs_write_dir(new_dir, new_dentry, d_inode(old_dentry));
  528. if (!err)
  529. err = write_inode(new_dir);
  530. if (err) {
  531. super->s_rename_dir = 0;
  532. super->s_rename_pos = 0;
  533. abort_transaction(new_dir, ta);
  534. goto out;
  535. }
  536. /* 3. remove source dd */
  537. ta->state = CROSS_RENAME_2;
  538. logfs_add_transaction(old_dir, ta);
  539. err = logfs_delete_dd(old_dir, pos);
  540. if (!err)
  541. err = write_inode(old_dir);
  542. LOGFS_BUG_ON(err, old_dir->i_sb);
  543. out:
  544. mutex_unlock(&super->s_dirop_mutex);
  545. return err;
  546. }
  547. static int logfs_replace_inode(struct inode *dir, struct dentry *dentry,
  548. struct logfs_disk_dentry *dd, struct inode *inode)
  549. {
  550. loff_t pos;
  551. int err;
  552. err = logfs_get_dd(dir, dentry, dd, &pos);
  553. if (err)
  554. return err;
  555. dd->ino = cpu_to_be64(inode->i_ino);
  556. dd->type = logfs_type(inode);
  557. err = write_dir(dir, dd, pos);
  558. if (err)
  559. return err;
  560. log_dir("Replace dentry (%lx, %llx) %s -> %llx\n", dir->i_ino, pos,
  561. dd->name, be64_to_cpu(dd->ino));
  562. return write_inode(dir);
  563. }
  564. /* Target dentry exists - the worst case. We need to attach the source
  565. * inode to the target dentry, then remove the orphaned target inode and
  566. * source dentry.
  567. */
  568. static int logfs_rename_target(struct inode *old_dir, struct dentry *old_dentry,
  569. struct inode *new_dir, struct dentry *new_dentry)
  570. {
  571. struct logfs_super *super = logfs_super(old_dir->i_sb);
  572. struct inode *old_inode = d_inode(old_dentry);
  573. struct inode *new_inode = d_inode(new_dentry);
  574. int isdir = S_ISDIR(old_inode->i_mode);
  575. struct logfs_disk_dentry dd;
  576. struct logfs_transaction *ta;
  577. loff_t pos;
  578. int err;
  579. BUG_ON(isdir != S_ISDIR(new_inode->i_mode));
  580. if (isdir) {
  581. if (!logfs_empty_dir(new_inode))
  582. return -ENOTEMPTY;
  583. }
  584. /* 1. locate source dd */
  585. err = logfs_get_dd(old_dir, old_dentry, &dd, &pos);
  586. if (err)
  587. return err;
  588. ta = kzalloc(sizeof(*ta), GFP_KERNEL);
  589. if (!ta)
  590. return -ENOMEM;
  591. ta->state = TARGET_RENAME_1;
  592. ta->dir = old_dir->i_ino;
  593. ta->pos = pos;
  594. ta->ino = new_inode->i_ino;
  595. /* 2. attach source inode to target dd */
  596. mutex_lock(&super->s_dirop_mutex);
  597. logfs_add_transaction(new_dir, ta);
  598. err = logfs_replace_inode(new_dir, new_dentry, &dd, old_inode);
  599. if (err) {
  600. super->s_rename_dir = 0;
  601. super->s_rename_pos = 0;
  602. super->s_victim_ino = 0;
  603. abort_transaction(new_dir, ta);
  604. goto out;
  605. }
  606. /* 3. remove source dd */
  607. ta->state = TARGET_RENAME_2;
  608. logfs_add_transaction(old_dir, ta);
  609. err = logfs_delete_dd(old_dir, pos);
  610. if (!err)
  611. err = write_inode(old_dir);
  612. LOGFS_BUG_ON(err, old_dir->i_sb);
  613. /* 4. remove target inode */
  614. ta->state = TARGET_RENAME_3;
  615. logfs_add_transaction(new_inode, ta);
  616. err = logfs_remove_inode(new_inode);
  617. out:
  618. mutex_unlock(&super->s_dirop_mutex);
  619. return err;
  620. }
  621. static int logfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  622. struct inode *new_dir, struct dentry *new_dentry)
  623. {
  624. if (d_really_is_positive(new_dentry))
  625. return logfs_rename_target(old_dir, old_dentry,
  626. new_dir, new_dentry);
  627. return logfs_rename_cross(old_dir, old_dentry, new_dir, new_dentry);
  628. }
  629. /* No locking done here, as this is called before .get_sb() returns. */
  630. int logfs_replay_journal(struct super_block *sb)
  631. {
  632. struct logfs_super *super = logfs_super(sb);
  633. struct inode *inode;
  634. u64 ino, pos;
  635. int err;
  636. if (super->s_victim_ino) {
  637. /* delete victim inode */
  638. ino = super->s_victim_ino;
  639. printk(KERN_INFO"LogFS: delete unmapped inode #%llx\n", ino);
  640. inode = logfs_iget(sb, ino);
  641. if (IS_ERR(inode))
  642. goto fail;
  643. LOGFS_BUG_ON(i_size_read(inode) > 0, sb);
  644. super->s_victim_ino = 0;
  645. err = logfs_remove_inode(inode);
  646. iput(inode);
  647. if (err) {
  648. super->s_victim_ino = ino;
  649. goto fail;
  650. }
  651. }
  652. if (super->s_rename_dir) {
  653. /* delete old dd from rename */
  654. ino = super->s_rename_dir;
  655. pos = super->s_rename_pos;
  656. printk(KERN_INFO"LogFS: delete unbacked dentry (%llx, %llx)\n",
  657. ino, pos);
  658. inode = logfs_iget(sb, ino);
  659. if (IS_ERR(inode))
  660. goto fail;
  661. super->s_rename_dir = 0;
  662. super->s_rename_pos = 0;
  663. err = logfs_delete_dd(inode, pos);
  664. iput(inode);
  665. if (err) {
  666. super->s_rename_dir = ino;
  667. super->s_rename_pos = pos;
  668. goto fail;
  669. }
  670. }
  671. return 0;
  672. fail:
  673. LOGFS_BUG(sb);
  674. return -EIO;
  675. }
  676. const struct inode_operations logfs_symlink_iops = {
  677. .readlink = generic_readlink,
  678. .follow_link = page_follow_link_light,
  679. .put_link = page_put_link,
  680. };
  681. const struct inode_operations logfs_dir_iops = {
  682. .create = logfs_create,
  683. .link = logfs_link,
  684. .lookup = logfs_lookup,
  685. .mkdir = logfs_mkdir,
  686. .mknod = logfs_mknod,
  687. .rename = logfs_rename,
  688. .rmdir = logfs_rmdir,
  689. .symlink = logfs_symlink,
  690. .unlink = logfs_unlink,
  691. };
  692. const struct file_operations logfs_dir_fops = {
  693. .fsync = logfs_fsync,
  694. .unlocked_ioctl = logfs_ioctl,
  695. .iterate = logfs_readdir,
  696. .read = generic_read_dir,
  697. .llseek = default_llseek,
  698. };