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 logfs_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. const struct qstr *name = &dentry->d_name;
  149. struct page *page;
  150. struct logfs_disk_dentry *dd;
  151. u32 hash = logfs_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. put_page(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(inode);
  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. put_page(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. put_page(page);
  282. if (full)
  283. break;
  284. }
  285. return 0;
  286. }
  287. static void logfs_set_name(struct logfs_disk_dentry *dd, const 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. put_page(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 = logfs_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. put_page(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 = &page_symlink_inode_operations;
  461. inode_nohighmem(inode);
  462. inode->i_mapping->a_ops = &logfs_reg_aops;
  463. return __logfs_create(dir, dentry, inode, target, destlen);
  464. }
  465. static int logfs_link(struct dentry *old_dentry, struct inode *dir,
  466. struct dentry *dentry)
  467. {
  468. struct inode *inode = d_inode(old_dentry);
  469. inode->i_ctime = dir->i_ctime = dir->i_mtime = current_time(inode);
  470. ihold(inode);
  471. inc_nlink(inode);
  472. mark_inode_dirty_sync(inode);
  473. return __logfs_create(dir, dentry, inode, NULL, 0);
  474. }
  475. static int logfs_get_dd(struct inode *dir, struct dentry *dentry,
  476. struct logfs_disk_dentry *dd, loff_t *pos)
  477. {
  478. struct page *page;
  479. void *map;
  480. page = logfs_get_dd_page(dir, dentry);
  481. if (IS_ERR(page))
  482. return PTR_ERR(page);
  483. *pos = page->index;
  484. map = kmap_atomic(page);
  485. memcpy(dd, map, sizeof(*dd));
  486. kunmap_atomic(map);
  487. put_page(page);
  488. return 0;
  489. }
  490. static int logfs_delete_dd(struct inode *dir, loff_t pos)
  491. {
  492. /*
  493. * Getting called with pos somewhere beyond eof is either a goofup
  494. * within this file or means someone maliciously edited the
  495. * (crc-protected) journal.
  496. */
  497. BUG_ON(beyond_eof(dir, pos));
  498. dir->i_ctime = dir->i_mtime = current_time(dir);
  499. log_dir(" Delete dentry (%lx, %llx)\n", dir->i_ino, pos);
  500. return logfs_delete(dir, pos, NULL);
  501. }
  502. /*
  503. * Cross-directory rename, target does not exist. Just a little nasty.
  504. * Create a new dentry in the target dir, then remove the old dentry,
  505. * all the while taking care to remember our operation in the journal.
  506. */
  507. static int logfs_rename_cross(struct inode *old_dir, struct dentry *old_dentry,
  508. struct inode *new_dir, struct dentry *new_dentry)
  509. {
  510. struct logfs_super *super = logfs_super(old_dir->i_sb);
  511. struct logfs_disk_dentry dd;
  512. struct logfs_transaction *ta;
  513. loff_t pos;
  514. int err;
  515. /* 1. locate source dd */
  516. err = logfs_get_dd(old_dir, old_dentry, &dd, &pos);
  517. if (err)
  518. return err;
  519. ta = kzalloc(sizeof(*ta), GFP_KERNEL);
  520. if (!ta)
  521. return -ENOMEM;
  522. ta->state = CROSS_RENAME_1;
  523. ta->dir = old_dir->i_ino;
  524. ta->pos = pos;
  525. /* 2. write target dd */
  526. mutex_lock(&super->s_dirop_mutex);
  527. logfs_add_transaction(new_dir, ta);
  528. err = logfs_write_dir(new_dir, new_dentry, d_inode(old_dentry));
  529. if (!err)
  530. err = write_inode(new_dir);
  531. if (err) {
  532. super->s_rename_dir = 0;
  533. super->s_rename_pos = 0;
  534. abort_transaction(new_dir, ta);
  535. goto out;
  536. }
  537. /* 3. remove source dd */
  538. ta->state = CROSS_RENAME_2;
  539. logfs_add_transaction(old_dir, ta);
  540. err = logfs_delete_dd(old_dir, pos);
  541. if (!err)
  542. err = write_inode(old_dir);
  543. LOGFS_BUG_ON(err, old_dir->i_sb);
  544. out:
  545. mutex_unlock(&super->s_dirop_mutex);
  546. return err;
  547. }
  548. static int logfs_replace_inode(struct inode *dir, struct dentry *dentry,
  549. struct logfs_disk_dentry *dd, struct inode *inode)
  550. {
  551. loff_t pos;
  552. int err;
  553. err = logfs_get_dd(dir, dentry, dd, &pos);
  554. if (err)
  555. return err;
  556. dd->ino = cpu_to_be64(inode->i_ino);
  557. dd->type = logfs_type(inode);
  558. err = write_dir(dir, dd, pos);
  559. if (err)
  560. return err;
  561. log_dir("Replace dentry (%lx, %llx) %s -> %llx\n", dir->i_ino, pos,
  562. dd->name, be64_to_cpu(dd->ino));
  563. return write_inode(dir);
  564. }
  565. /* Target dentry exists - the worst case. We need to attach the source
  566. * inode to the target dentry, then remove the orphaned target inode and
  567. * source dentry.
  568. */
  569. static int logfs_rename_target(struct inode *old_dir, struct dentry *old_dentry,
  570. struct inode *new_dir, struct dentry *new_dentry)
  571. {
  572. struct logfs_super *super = logfs_super(old_dir->i_sb);
  573. struct inode *old_inode = d_inode(old_dentry);
  574. struct inode *new_inode = d_inode(new_dentry);
  575. int isdir = S_ISDIR(old_inode->i_mode);
  576. struct logfs_disk_dentry dd;
  577. struct logfs_transaction *ta;
  578. loff_t pos;
  579. int err;
  580. BUG_ON(isdir != S_ISDIR(new_inode->i_mode));
  581. if (isdir) {
  582. if (!logfs_empty_dir(new_inode))
  583. return -ENOTEMPTY;
  584. }
  585. /* 1. locate source dd */
  586. err = logfs_get_dd(old_dir, old_dentry, &dd, &pos);
  587. if (err)
  588. return err;
  589. ta = kzalloc(sizeof(*ta), GFP_KERNEL);
  590. if (!ta)
  591. return -ENOMEM;
  592. ta->state = TARGET_RENAME_1;
  593. ta->dir = old_dir->i_ino;
  594. ta->pos = pos;
  595. ta->ino = new_inode->i_ino;
  596. /* 2. attach source inode to target dd */
  597. mutex_lock(&super->s_dirop_mutex);
  598. logfs_add_transaction(new_dir, ta);
  599. err = logfs_replace_inode(new_dir, new_dentry, &dd, old_inode);
  600. if (err) {
  601. super->s_rename_dir = 0;
  602. super->s_rename_pos = 0;
  603. super->s_victim_ino = 0;
  604. abort_transaction(new_dir, ta);
  605. goto out;
  606. }
  607. /* 3. remove source dd */
  608. ta->state = TARGET_RENAME_2;
  609. logfs_add_transaction(old_dir, ta);
  610. err = logfs_delete_dd(old_dir, pos);
  611. if (!err)
  612. err = write_inode(old_dir);
  613. LOGFS_BUG_ON(err, old_dir->i_sb);
  614. /* 4. remove target inode */
  615. ta->state = TARGET_RENAME_3;
  616. logfs_add_transaction(new_inode, ta);
  617. err = logfs_remove_inode(new_inode);
  618. out:
  619. mutex_unlock(&super->s_dirop_mutex);
  620. return err;
  621. }
  622. static int logfs_rename(struct inode *old_dir, struct dentry *old_dentry,
  623. struct inode *new_dir, struct dentry *new_dentry,
  624. unsigned int flags)
  625. {
  626. if (flags & ~RENAME_NOREPLACE)
  627. return -EINVAL;
  628. if (d_really_is_positive(new_dentry))
  629. return logfs_rename_target(old_dir, old_dentry,
  630. new_dir, new_dentry);
  631. return logfs_rename_cross(old_dir, old_dentry, new_dir, new_dentry);
  632. }
  633. /* No locking done here, as this is called before .get_sb() returns. */
  634. int logfs_replay_journal(struct super_block *sb)
  635. {
  636. struct logfs_super *super = logfs_super(sb);
  637. struct inode *inode;
  638. u64 ino, pos;
  639. int err;
  640. if (super->s_victim_ino) {
  641. /* delete victim inode */
  642. ino = super->s_victim_ino;
  643. printk(KERN_INFO"LogFS: delete unmapped inode #%llx\n", ino);
  644. inode = logfs_iget(sb, ino);
  645. if (IS_ERR(inode))
  646. goto fail;
  647. LOGFS_BUG_ON(i_size_read(inode) > 0, sb);
  648. super->s_victim_ino = 0;
  649. err = logfs_remove_inode(inode);
  650. iput(inode);
  651. if (err) {
  652. super->s_victim_ino = ino;
  653. goto fail;
  654. }
  655. }
  656. if (super->s_rename_dir) {
  657. /* delete old dd from rename */
  658. ino = super->s_rename_dir;
  659. pos = super->s_rename_pos;
  660. printk(KERN_INFO"LogFS: delete unbacked dentry (%llx, %llx)\n",
  661. ino, pos);
  662. inode = logfs_iget(sb, ino);
  663. if (IS_ERR(inode))
  664. goto fail;
  665. super->s_rename_dir = 0;
  666. super->s_rename_pos = 0;
  667. err = logfs_delete_dd(inode, pos);
  668. iput(inode);
  669. if (err) {
  670. super->s_rename_dir = ino;
  671. super->s_rename_pos = pos;
  672. goto fail;
  673. }
  674. }
  675. return 0;
  676. fail:
  677. LOGFS_BUG(sb);
  678. return -EIO;
  679. }
  680. const struct inode_operations logfs_dir_iops = {
  681. .create = logfs_create,
  682. .link = logfs_link,
  683. .lookup = logfs_lookup,
  684. .mkdir = logfs_mkdir,
  685. .mknod = logfs_mknod,
  686. .rename = logfs_rename,
  687. .rmdir = logfs_rmdir,
  688. .symlink = logfs_symlink,
  689. .unlink = logfs_unlink,
  690. };
  691. const struct file_operations logfs_dir_fops = {
  692. .fsync = logfs_fsync,
  693. .unlocked_ioctl = logfs_ioctl,
  694. .iterate_shared = logfs_readdir,
  695. .read = generic_read_dir,
  696. .llseek = generic_file_llseek,
  697. };