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