Evgeniy Polyakov | b3f08ca | 2009-02-09 17:02:38 +0300 | [diff] [blame] | 1 | /* |
| 2 | * 2007+ Copyright (c) Evgeniy Polyakov <zbr@ioremap.net> |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/backing-dev.h> |
| 18 | #include <linux/crypto.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/jhash.h> |
| 21 | #include <linux/hash.h> |
| 22 | #include <linux/ktime.h> |
| 23 | #include <linux/mm.h> |
| 24 | #include <linux/mount.h> |
| 25 | #include <linux/pagemap.h> |
| 26 | #include <linux/pagevec.h> |
| 27 | #include <linux/parser.h> |
| 28 | #include <linux/swap.h> |
| 29 | #include <linux/slab.h> |
| 30 | #include <linux/statfs.h> |
| 31 | #include <linux/writeback.h> |
| 32 | #include <linux/quotaops.h> |
| 33 | |
| 34 | #include "netfs.h" |
| 35 | |
| 36 | #define POHMELFS_MAGIC_NUM 0x504f482e |
| 37 | |
| 38 | static struct kmem_cache *pohmelfs_inode_cache; |
| 39 | |
| 40 | /* |
| 41 | * Removes inode from all trees, drops local name cache and removes all queued |
| 42 | * requests for object removal. |
| 43 | */ |
| 44 | void pohmelfs_inode_del_inode(struct pohmelfs_sb *psb, struct pohmelfs_inode *pi) |
| 45 | { |
| 46 | mutex_lock(&pi->offset_lock); |
| 47 | pohmelfs_free_names(pi); |
| 48 | mutex_unlock(&pi->offset_lock); |
| 49 | |
| 50 | dprintk("%s: deleted stuff in ino: %llu.\n", __func__, pi->ino); |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * Sync inode to server. |
| 55 | * Returns zero in success and negative error value otherwise. |
| 56 | * It will gather path to root directory into structures containing |
| 57 | * creation mode, permissions and names, so that the whole path |
| 58 | * to given inode could be created using only single network command. |
| 59 | */ |
| 60 | int pohmelfs_write_inode_create(struct inode *inode, struct netfs_trans *trans) |
| 61 | { |
| 62 | struct pohmelfs_inode *pi = POHMELFS_I(inode); |
| 63 | int err = -ENOMEM, size; |
| 64 | struct netfs_cmd *cmd; |
| 65 | void *data; |
| 66 | int cur_len = netfs_trans_cur_len(trans); |
| 67 | |
| 68 | if (unlikely(cur_len < 0)) |
| 69 | return -ETOOSMALL; |
| 70 | |
| 71 | cmd = netfs_trans_current(trans); |
| 72 | cur_len -= sizeof(struct netfs_cmd); |
| 73 | |
| 74 | data = (void *)(cmd + 1); |
| 75 | |
| 76 | err = pohmelfs_construct_path_string(pi, data, cur_len); |
| 77 | if (err < 0) |
| 78 | goto err_out_exit; |
| 79 | |
| 80 | size = err; |
| 81 | |
| 82 | cmd->start = i_size_read(inode); |
| 83 | cmd->cmd = NETFS_CREATE; |
| 84 | cmd->size = size; |
| 85 | cmd->id = pi->ino; |
| 86 | cmd->ext = inode->i_mode; |
| 87 | |
| 88 | netfs_convert_cmd(cmd); |
| 89 | |
| 90 | netfs_trans_update(cmd, trans, size); |
| 91 | |
| 92 | return 0; |
| 93 | |
| 94 | err_out_exit: |
| 95 | printk("%s: completed ino: %llu, err: %d.\n", __func__, pi->ino, err); |
| 96 | return err; |
| 97 | } |
| 98 | |
| 99 | static int pohmelfs_write_trans_complete(struct page **pages, unsigned int page_num, |
| 100 | void *private, int err) |
| 101 | { |
| 102 | unsigned i; |
| 103 | |
| 104 | dprintk("%s: pages: %lu-%lu, page_num: %u, err: %d.\n", |
| 105 | __func__, pages[0]->index, pages[page_num-1]->index, |
| 106 | page_num, err); |
| 107 | |
| 108 | for (i = 0; i < page_num; i++) { |
| 109 | struct page *page = pages[i]; |
| 110 | |
| 111 | if (!page) |
| 112 | continue; |
| 113 | |
| 114 | end_page_writeback(page); |
| 115 | |
| 116 | if (err < 0) { |
| 117 | SetPageError(page); |
| 118 | set_page_dirty(page); |
| 119 | } |
| 120 | |
| 121 | unlock_page(page); |
| 122 | page_cache_release(page); |
| 123 | |
| 124 | /* dprintk("%s: %3u/%u: page: %p.\n", __func__, i, page_num, page); */ |
| 125 | } |
| 126 | return err; |
| 127 | } |
| 128 | |
| 129 | static int pohmelfs_inode_has_dirty_pages(struct address_space *mapping, pgoff_t index) |
| 130 | { |
| 131 | int ret; |
| 132 | struct page *page; |
| 133 | |
| 134 | rcu_read_lock(); |
| 135 | ret = radix_tree_gang_lookup_tag(&mapping->page_tree, |
| 136 | (void **)&page, index, 1, PAGECACHE_TAG_DIRTY); |
| 137 | rcu_read_unlock(); |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | static int pohmelfs_writepages(struct address_space *mapping, struct writeback_control *wbc) |
| 142 | { |
| 143 | struct inode *inode = mapping->host; |
| 144 | struct pohmelfs_inode *pi = POHMELFS_I(inode); |
| 145 | struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb); |
| 146 | struct backing_dev_info *bdi = mapping->backing_dev_info; |
| 147 | int err = 0; |
| 148 | int done = 0; |
| 149 | int nr_pages; |
| 150 | pgoff_t index; |
| 151 | pgoff_t end; /* Inclusive */ |
| 152 | int scanned = 0; |
| 153 | int range_whole = 0; |
| 154 | |
| 155 | if (wbc->nonblocking && bdi_write_congested(bdi)) { |
| 156 | wbc->encountered_congestion = 1; |
| 157 | return 0; |
| 158 | } |
| 159 | |
| 160 | if (wbc->range_cyclic) { |
| 161 | index = mapping->writeback_index; /* Start from prev offset */ |
| 162 | end = -1; |
| 163 | } else { |
| 164 | index = wbc->range_start >> PAGE_CACHE_SHIFT; |
| 165 | end = wbc->range_end >> PAGE_CACHE_SHIFT; |
| 166 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) |
| 167 | range_whole = 1; |
| 168 | scanned = 1; |
| 169 | } |
| 170 | retry: |
| 171 | while (!done && (index <= end)) { |
| 172 | unsigned int i = min(end - index, (pgoff_t)psb->trans_max_pages); |
| 173 | int path_len; |
| 174 | struct netfs_trans *trans; |
| 175 | |
| 176 | err = pohmelfs_inode_has_dirty_pages(mapping, index); |
| 177 | if (!err) |
| 178 | break; |
| 179 | |
| 180 | err = pohmelfs_path_length(pi); |
| 181 | if (err < 0) |
| 182 | break; |
| 183 | |
| 184 | path_len = err; |
| 185 | |
| 186 | if (path_len <= 2) { |
| 187 | err = -ENOENT; |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | trans = netfs_trans_alloc(psb, path_len, 0, i); |
| 192 | if (!trans) { |
| 193 | err = -ENOMEM; |
| 194 | break; |
| 195 | } |
| 196 | trans->complete = &pohmelfs_write_trans_complete; |
| 197 | |
| 198 | trans->page_num = nr_pages = find_get_pages_tag(mapping, &index, |
| 199 | PAGECACHE_TAG_DIRTY, trans->page_num, |
| 200 | trans->pages); |
| 201 | |
| 202 | dprintk("%s: t: %p, nr_pages: %u, end: %lu, index: %lu, max: %u.\n", |
| 203 | __func__, trans, nr_pages, end, index, trans->page_num); |
| 204 | |
| 205 | if (!nr_pages) |
| 206 | goto err_out_reset; |
| 207 | |
| 208 | err = pohmelfs_write_inode_create(inode, trans); |
| 209 | if (err) |
| 210 | goto err_out_reset; |
| 211 | |
| 212 | err = 0; |
| 213 | scanned = 1; |
| 214 | |
| 215 | for (i = 0; i < trans->page_num; i++) { |
| 216 | struct page *page = trans->pages[i]; |
| 217 | |
| 218 | lock_page(page); |
| 219 | |
| 220 | if (unlikely(page->mapping != mapping)) |
| 221 | goto out_continue; |
| 222 | |
| 223 | if (!wbc->range_cyclic && page->index > end) { |
| 224 | done = 1; |
| 225 | goto out_continue; |
| 226 | } |
| 227 | |
| 228 | if (wbc->sync_mode != WB_SYNC_NONE) |
| 229 | wait_on_page_writeback(page); |
| 230 | |
| 231 | if (PageWriteback(page) || |
| 232 | !clear_page_dirty_for_io(page)) { |
| 233 | dprintk("%s: not clear for io page: %p, writeback: %d.\n", |
| 234 | __func__, page, PageWriteback(page)); |
| 235 | goto out_continue; |
| 236 | } |
| 237 | |
| 238 | set_page_writeback(page); |
| 239 | |
| 240 | trans->attached_size += page_private(page); |
| 241 | trans->attached_pages++; |
| 242 | #if 0 |
| 243 | dprintk("%s: %u/%u added trans: %p, gen: %u, page: %p, [High: %d], size: %lu, idx: %lu.\n", |
| 244 | __func__, i, trans->page_num, trans, trans->gen, page, |
| 245 | !!PageHighMem(page), page_private(page), page->index); |
| 246 | #endif |
| 247 | wbc->nr_to_write--; |
| 248 | |
| 249 | if (wbc->nr_to_write <= 0) |
| 250 | done = 1; |
| 251 | if (wbc->nonblocking && bdi_write_congested(bdi)) { |
| 252 | wbc->encountered_congestion = 1; |
| 253 | done = 1; |
| 254 | } |
| 255 | |
| 256 | continue; |
| 257 | out_continue: |
| 258 | unlock_page(page); |
| 259 | trans->pages[i] = NULL; |
| 260 | } |
| 261 | |
| 262 | err = netfs_trans_finish(trans, psb); |
| 263 | if (err) |
| 264 | break; |
| 265 | |
| 266 | continue; |
| 267 | |
| 268 | err_out_reset: |
| 269 | trans->result = err; |
| 270 | netfs_trans_reset(trans); |
| 271 | netfs_trans_put(trans); |
| 272 | break; |
| 273 | } |
| 274 | |
| 275 | if (!scanned && !done) { |
| 276 | /* |
| 277 | * We hit the last page and there is more work to be done: wrap |
| 278 | * back to the start of the file |
| 279 | */ |
| 280 | scanned = 1; |
| 281 | index = 0; |
| 282 | goto retry; |
| 283 | } |
| 284 | |
| 285 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) |
| 286 | mapping->writeback_index = index; |
| 287 | |
| 288 | return err; |
| 289 | } |
| 290 | |
| 291 | /* |
| 292 | * Inode writeback creation completion callback. |
| 293 | * Only invoked for just created inodes, which do not have pages attached, |
| 294 | * like dirs and empty files. |
| 295 | */ |
| 296 | static int pohmelfs_write_inode_complete(struct page **pages, unsigned int page_num, |
| 297 | void *private, int err) |
| 298 | { |
| 299 | struct inode *inode = private; |
| 300 | struct pohmelfs_inode *pi = POHMELFS_I(inode); |
| 301 | |
| 302 | if (inode) { |
| 303 | if (err) { |
| 304 | mark_inode_dirty(inode); |
| 305 | clear_bit(NETFS_INODE_REMOTE_SYNCED, &pi->state); |
| 306 | } else { |
| 307 | set_bit(NETFS_INODE_REMOTE_SYNCED, &pi->state); |
| 308 | } |
| 309 | |
| 310 | pohmelfs_put_inode(pi); |
| 311 | } |
| 312 | |
| 313 | return err; |
| 314 | } |
| 315 | |
| 316 | int pohmelfs_write_create_inode(struct pohmelfs_inode *pi) |
| 317 | { |
| 318 | struct netfs_trans *t; |
| 319 | struct inode *inode = &pi->vfs_inode; |
| 320 | struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb); |
| 321 | int err; |
| 322 | |
| 323 | if (test_bit(NETFS_INODE_REMOTE_SYNCED, &pi->state)) |
| 324 | return 0; |
| 325 | |
| 326 | dprintk("%s: started ino: %llu.\n", __func__, pi->ino); |
| 327 | |
| 328 | err = pohmelfs_path_length(pi); |
| 329 | if (err < 0) |
| 330 | goto err_out_exit; |
| 331 | |
| 332 | t = netfs_trans_alloc(psb, err + 1, 0, 0); |
| 333 | if (!t) { |
| 334 | err = -ENOMEM; |
| 335 | goto err_out_put; |
| 336 | } |
| 337 | t->complete = pohmelfs_write_inode_complete; |
| 338 | t->private = igrab(inode); |
| 339 | if (!t->private) { |
| 340 | err = -ENOENT; |
| 341 | goto err_out_put; |
| 342 | } |
| 343 | |
| 344 | err = pohmelfs_write_inode_create(inode, t); |
| 345 | if (err) |
| 346 | goto err_out_put; |
| 347 | |
| 348 | netfs_trans_finish(t, POHMELFS_SB(inode->i_sb)); |
| 349 | |
| 350 | return 0; |
| 351 | |
| 352 | err_out_put: |
| 353 | t->result = err; |
| 354 | netfs_trans_put(t); |
| 355 | err_out_exit: |
| 356 | return err; |
| 357 | } |
| 358 | |
| 359 | /* |
| 360 | * Sync all not-yet-created children in given directory to the server. |
| 361 | */ |
| 362 | static int pohmelfs_write_inode_create_children(struct inode *inode) |
| 363 | { |
| 364 | struct pohmelfs_inode *parent = POHMELFS_I(inode); |
| 365 | struct super_block *sb = inode->i_sb; |
| 366 | struct pohmelfs_name *n; |
| 367 | |
| 368 | while (!list_empty(&parent->sync_create_list)) { |
| 369 | n = NULL; |
| 370 | mutex_lock(&parent->offset_lock); |
| 371 | if (!list_empty(&parent->sync_create_list)) { |
| 372 | n = list_first_entry(&parent->sync_create_list, |
| 373 | struct pohmelfs_name, sync_create_entry); |
| 374 | list_del_init(&n->sync_create_entry); |
| 375 | } |
| 376 | mutex_unlock(&parent->offset_lock); |
| 377 | |
| 378 | if (!n) |
| 379 | break; |
| 380 | |
| 381 | inode = ilookup(sb, n->ino); |
| 382 | |
| 383 | dprintk("%s: parent: %llu, ino: %llu, inode: %p.\n", |
| 384 | __func__, parent->ino, n->ino, inode); |
| 385 | |
| 386 | if (inode && (inode->i_state & I_DIRTY)) { |
| 387 | struct pohmelfs_inode *pi = POHMELFS_I(inode); |
| 388 | pohmelfs_write_create_inode(pi); |
| 389 | //pohmelfs_meta_command(pi, NETFS_INODE_INFO, 0, NULL, NULL, 0); |
| 390 | iput(inode); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | /* |
| 398 | * Removes given child from given inode on server. |
| 399 | */ |
| 400 | int pohmelfs_remove_child(struct pohmelfs_inode *pi, struct pohmelfs_name *n) |
| 401 | { |
| 402 | return pohmelfs_meta_command_data(pi, pi->ino, NETFS_REMOVE, NULL, 0, NULL, NULL, 0); |
| 403 | } |
| 404 | |
| 405 | /* |
| 406 | * Writeback for given inode. |
| 407 | */ |
| 408 | static int pohmelfs_write_inode(struct inode *inode, int sync) |
| 409 | { |
| 410 | struct pohmelfs_inode *pi = POHMELFS_I(inode); |
| 411 | |
| 412 | pohmelfs_write_create_inode(pi); |
| 413 | pohmelfs_write_inode_create_children(inode); |
| 414 | |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | /* |
| 419 | * It is not exported, sorry... |
| 420 | */ |
| 421 | static inline wait_queue_head_t *page_waitqueue(struct page *page) |
| 422 | { |
| 423 | const struct zone *zone = page_zone(page); |
| 424 | |
| 425 | return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)]; |
| 426 | } |
| 427 | |
| 428 | static int pohmelfs_wait_on_page_locked(struct page *page) |
| 429 | { |
| 430 | struct pohmelfs_sb *psb = POHMELFS_SB(page->mapping->host->i_sb); |
| 431 | long ret = psb->wait_on_page_timeout; |
| 432 | DEFINE_WAIT_BIT(wait, &page->flags, PG_locked); |
| 433 | int err = 0; |
| 434 | |
| 435 | if (!PageLocked(page)) |
| 436 | return 0; |
| 437 | |
| 438 | for (;;) { |
| 439 | prepare_to_wait(page_waitqueue(page), |
| 440 | &wait.wait, TASK_INTERRUPTIBLE); |
| 441 | |
| 442 | dprintk("%s: page: %p, locked: %d, uptodate: %d, error: %d, flags: %lx.\n", |
| 443 | __func__, page, PageLocked(page), PageUptodate(page), |
| 444 | PageError(page), page->flags); |
| 445 | |
| 446 | if (!PageLocked(page)) |
| 447 | break; |
| 448 | |
| 449 | if (!signal_pending(current)) { |
| 450 | ret = schedule_timeout(ret); |
| 451 | if (!ret) |
| 452 | break; |
| 453 | continue; |
| 454 | } |
| 455 | ret = -ERESTARTSYS; |
| 456 | break; |
| 457 | } |
| 458 | finish_wait(page_waitqueue(page), &wait.wait); |
| 459 | |
| 460 | if (!ret) |
| 461 | err = -ETIMEDOUT; |
| 462 | |
| 463 | |
| 464 | if (!err) |
| 465 | SetPageUptodate(page); |
| 466 | |
| 467 | if (err) |
| 468 | printk("%s: page: %p, uptodate: %d, locked: %d, err: %d.\n", |
| 469 | __func__, page, PageUptodate(page), PageLocked(page), err); |
| 470 | |
| 471 | return err; |
| 472 | } |
| 473 | |
| 474 | static int pohmelfs_read_page_complete(struct page **pages, unsigned int page_num, |
| 475 | void *private, int err) |
| 476 | { |
| 477 | struct page *page = private; |
| 478 | |
| 479 | if (PageChecked(page)) |
| 480 | return err; |
| 481 | |
| 482 | if (err < 0) { |
| 483 | dprintk("%s: page: %p, err: %d.\n", __func__, page, err); |
| 484 | SetPageError(page); |
| 485 | } |
| 486 | |
| 487 | unlock_page(page); |
| 488 | |
| 489 | return err; |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Read a page from remote server. |
| 494 | * Function will wait until page is unlocked. |
| 495 | */ |
| 496 | static int pohmelfs_readpage(struct file *file, struct page *page) |
| 497 | { |
| 498 | struct inode *inode = page->mapping->host; |
| 499 | struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb); |
| 500 | struct pohmelfs_inode *pi = POHMELFS_I(inode); |
| 501 | struct netfs_trans *t; |
| 502 | struct netfs_cmd *cmd; |
| 503 | int err, path_len; |
| 504 | void *data; |
| 505 | u64 isize; |
| 506 | |
| 507 | err = pohmelfs_data_lock(pi, page->index << PAGE_CACHE_SHIFT, |
| 508 | PAGE_SIZE, POHMELFS_READ_LOCK); |
| 509 | if (err) |
| 510 | goto err_out_exit; |
| 511 | |
| 512 | isize = i_size_read(inode); |
| 513 | if (isize <= page->index << PAGE_CACHE_SHIFT) { |
| 514 | SetPageUptodate(page); |
| 515 | unlock_page(page); |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | path_len = pohmelfs_path_length(pi); |
| 520 | if (path_len < 0) { |
| 521 | err = path_len; |
| 522 | goto err_out_exit; |
| 523 | } |
| 524 | |
| 525 | t = netfs_trans_alloc(psb, path_len, NETFS_TRANS_SINGLE_DST, 0); |
| 526 | if (!t) { |
| 527 | err = -ENOMEM; |
| 528 | goto err_out_exit; |
| 529 | } |
| 530 | |
| 531 | t->complete = pohmelfs_read_page_complete; |
| 532 | t->private = page; |
| 533 | |
| 534 | cmd = netfs_trans_current(t); |
| 535 | data = (void *)(cmd + 1); |
| 536 | |
| 537 | err = pohmelfs_construct_path_string(pi, data, path_len); |
| 538 | if (err < 0) |
| 539 | goto err_out_free; |
| 540 | |
| 541 | path_len = err; |
| 542 | |
| 543 | cmd->id = pi->ino; |
| 544 | cmd->start = page->index; |
| 545 | cmd->start <<= PAGE_CACHE_SHIFT; |
| 546 | cmd->size = PAGE_CACHE_SIZE + path_len; |
| 547 | cmd->cmd = NETFS_READ_PAGE; |
| 548 | cmd->ext = path_len; |
| 549 | |
| 550 | dprintk("%s: path: '%s', page: %p, ino: %llu, start: %llu, size: %lu.\n", |
| 551 | __func__, (char *)data, page, pi->ino, cmd->start, PAGE_CACHE_SIZE); |
| 552 | |
| 553 | netfs_convert_cmd(cmd); |
| 554 | netfs_trans_update(cmd, t, path_len); |
| 555 | |
| 556 | err = netfs_trans_finish(t, psb); |
| 557 | if (err) |
| 558 | goto err_out_return; |
| 559 | |
| 560 | return pohmelfs_wait_on_page_locked(page); |
| 561 | |
| 562 | err_out_free: |
| 563 | t->result = err; |
| 564 | netfs_trans_put(t); |
| 565 | err_out_exit: |
| 566 | SetPageError(page); |
| 567 | if (PageLocked(page)) |
| 568 | unlock_page(page); |
| 569 | err_out_return: |
| 570 | printk("%s: page: %p, start: %lu, size: %lu, err: %d.\n", |
| 571 | __func__, page, page->index << PAGE_CACHE_SHIFT, PAGE_CACHE_SIZE, err); |
| 572 | |
| 573 | return err; |
| 574 | } |
| 575 | |
| 576 | /* |
| 577 | * Write begin/end magic. |
| 578 | * Allocates a page and writes inode if it was not synced to server before. |
| 579 | */ |
| 580 | static int pohmelfs_write_begin(struct file *file, struct address_space *mapping, |
| 581 | loff_t pos, unsigned len, unsigned flags, |
| 582 | struct page **pagep, void **fsdata) |
| 583 | { |
| 584 | struct inode *inode = mapping->host; |
| 585 | struct page *page; |
| 586 | pgoff_t index; |
| 587 | unsigned start, end; |
| 588 | int err; |
| 589 | |
| 590 | *pagep = NULL; |
| 591 | |
| 592 | index = pos >> PAGE_CACHE_SHIFT; |
| 593 | start = pos & (PAGE_CACHE_SIZE - 1); |
| 594 | end = start + len; |
| 595 | |
| 596 | page = grab_cache_page(mapping, index); |
| 597 | #if 0 |
| 598 | dprintk("%s: page: %p pos: %llu, len: %u, index: %lu, start: %u, end: %u, uptodate: %d.\n", |
| 599 | __func__, page, pos, len, index, start, end, PageUptodate(page)); |
| 600 | #endif |
| 601 | if (!page) { |
| 602 | err = -ENOMEM; |
| 603 | goto err_out_exit; |
| 604 | } |
| 605 | |
| 606 | while (!PageUptodate(page)) { |
| 607 | if (start && test_bit(NETFS_INODE_REMOTE_SYNCED, &POHMELFS_I(inode)->state)) { |
| 608 | err = pohmelfs_readpage(file, page); |
| 609 | if (err) |
| 610 | goto err_out_exit; |
| 611 | |
| 612 | lock_page(page); |
| 613 | continue; |
| 614 | } |
| 615 | |
| 616 | if (len != PAGE_CACHE_SIZE) { |
| 617 | void *kaddr = kmap_atomic(page, KM_USER0); |
| 618 | |
| 619 | memset(kaddr + start, 0, PAGE_CACHE_SIZE - start); |
| 620 | flush_dcache_page(page); |
| 621 | kunmap_atomic(kaddr, KM_USER0); |
| 622 | } |
| 623 | SetPageUptodate(page); |
| 624 | } |
| 625 | |
| 626 | set_page_private(page, end); |
| 627 | |
| 628 | *pagep = page; |
| 629 | |
| 630 | return 0; |
| 631 | |
| 632 | err_out_exit: |
| 633 | page_cache_release(page); |
| 634 | *pagep = NULL; |
| 635 | |
| 636 | return err; |
| 637 | } |
| 638 | |
| 639 | static int pohmelfs_write_end(struct file *file, struct address_space *mapping, |
| 640 | loff_t pos, unsigned len, unsigned copied, |
| 641 | struct page *page, void *fsdata) |
| 642 | { |
| 643 | struct inode *inode = mapping->host; |
| 644 | |
| 645 | if (copied != len) { |
| 646 | unsigned from = pos & (PAGE_CACHE_SIZE - 1); |
| 647 | void *kaddr = kmap_atomic(page, KM_USER0); |
| 648 | |
| 649 | memset(kaddr + from + copied, 0, len - copied); |
| 650 | flush_dcache_page(page); |
| 651 | kunmap_atomic(kaddr, KM_USER0); |
| 652 | } |
| 653 | |
| 654 | SetPageUptodate(page); |
| 655 | set_page_dirty(page); |
| 656 | #if 0 |
| 657 | dprintk("%s: page: %p [U: %d, D: %d, L: %d], pos: %llu, len: %u, copied: %u.\n", |
| 658 | __func__, page, |
| 659 | PageUptodate(page), PageDirty(page), PageLocked(page), |
| 660 | pos, len, copied); |
| 661 | #endif |
| 662 | flush_dcache_page(page); |
| 663 | |
| 664 | unlock_page(page); |
| 665 | page_cache_release(page); |
| 666 | |
| 667 | if (pos + copied > inode->i_size) { |
| 668 | struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb); |
| 669 | |
| 670 | psb->avail_size -= pos + copied - inode->i_size; |
| 671 | |
| 672 | i_size_write(inode, pos + copied); |
| 673 | } |
| 674 | |
| 675 | return copied; |
| 676 | } |
| 677 | |
| 678 | static int pohmelfs_readpages_trans_complete(struct page **__pages, unsigned int page_num, |
| 679 | void *private, int err) |
| 680 | { |
| 681 | struct pohmelfs_inode *pi = private; |
| 682 | unsigned int i, num; |
| 683 | struct page **pages, *page = (struct page *)__pages; |
| 684 | loff_t index = page->index; |
| 685 | |
| 686 | pages = kzalloc(sizeof(void *) * page_num, GFP_NOIO); |
| 687 | if (!pages) |
| 688 | return -ENOMEM; |
| 689 | |
| 690 | num = find_get_pages_contig(pi->vfs_inode.i_mapping, index, page_num, pages); |
| 691 | if (num <= 0) { |
| 692 | err = num; |
| 693 | goto err_out_free; |
| 694 | } |
| 695 | |
| 696 | for (i=0; i<num; ++i) { |
| 697 | page = pages[i]; |
| 698 | |
| 699 | if (err) |
| 700 | printk("%s: %u/%u: page: %p, index: %lu, uptodate: %d, locked: %d, err: %d.\n", |
| 701 | __func__, i, num, page, page->index, |
| 702 | PageUptodate(page), PageLocked(page), err); |
| 703 | |
| 704 | if (!PageChecked(page)) { |
| 705 | if (err < 0) |
| 706 | SetPageError(page); |
| 707 | unlock_page(page); |
| 708 | } |
| 709 | page_cache_release(page); |
| 710 | page_cache_release(page); |
| 711 | } |
| 712 | |
| 713 | err_out_free: |
| 714 | kfree(pages); |
| 715 | return err; |
| 716 | } |
| 717 | |
| 718 | static int pohmelfs_send_readpages(struct pohmelfs_inode *pi, struct page *first, unsigned int num) |
| 719 | { |
| 720 | struct netfs_trans *t; |
| 721 | struct netfs_cmd *cmd; |
| 722 | struct pohmelfs_sb *psb = POHMELFS_SB(pi->vfs_inode.i_sb); |
| 723 | int err, path_len; |
| 724 | void *data; |
| 725 | |
| 726 | err = pohmelfs_data_lock(pi, first->index << PAGE_CACHE_SHIFT, |
| 727 | num * PAGE_SIZE, POHMELFS_READ_LOCK); |
| 728 | if (err) |
| 729 | goto err_out_exit; |
| 730 | |
| 731 | path_len = pohmelfs_path_length(pi); |
| 732 | if (path_len < 0) { |
| 733 | err = path_len; |
| 734 | goto err_out_exit; |
| 735 | } |
| 736 | |
| 737 | t = netfs_trans_alloc(psb, path_len, NETFS_TRANS_SINGLE_DST, 0); |
| 738 | if (!t) { |
| 739 | err = -ENOMEM; |
| 740 | goto err_out_exit; |
| 741 | } |
| 742 | |
| 743 | cmd = netfs_trans_current(t); |
| 744 | data = (void *)(cmd + 1); |
| 745 | |
| 746 | t->complete = pohmelfs_readpages_trans_complete; |
| 747 | t->private = pi; |
| 748 | t->page_num = num; |
| 749 | t->pages = (struct page **)first; |
| 750 | |
| 751 | err = pohmelfs_construct_path_string(pi, data, path_len); |
| 752 | if (err < 0) |
| 753 | goto err_out_put; |
| 754 | |
| 755 | path_len = err; |
| 756 | |
| 757 | cmd->cmd = NETFS_READ_PAGES; |
| 758 | cmd->start = first->index; |
| 759 | cmd->start <<= PAGE_CACHE_SHIFT; |
| 760 | cmd->size = (num << 8 | PAGE_CACHE_SHIFT); |
| 761 | cmd->id = pi->ino; |
| 762 | cmd->ext = path_len; |
| 763 | |
| 764 | dprintk("%s: t: %p, gen: %u, path: '%s', path_len: %u, " |
| 765 | "start: %lu, num: %u.\n", |
| 766 | __func__, t, t->gen, (char *)data, path_len, |
| 767 | first->index, num); |
| 768 | |
| 769 | netfs_convert_cmd(cmd); |
| 770 | netfs_trans_update(cmd, t, path_len); |
| 771 | |
| 772 | return netfs_trans_finish(t, psb); |
| 773 | |
| 774 | err_out_put: |
| 775 | netfs_trans_free(t); |
| 776 | err_out_exit: |
| 777 | pohmelfs_readpages_trans_complete((struct page **)first, num, pi, err); |
| 778 | return err; |
| 779 | } |
| 780 | |
| 781 | #define list_to_page(head) (list_entry((head)->prev, struct page, lru)) |
| 782 | |
| 783 | static int pohmelfs_readpages(struct file *file, struct address_space *mapping, |
| 784 | struct list_head *pages, unsigned nr_pages) |
| 785 | { |
| 786 | unsigned int page_idx, num = 0; |
| 787 | struct page *page = NULL, *first = NULL; |
| 788 | |
| 789 | for (page_idx = 0; page_idx < nr_pages; page_idx++) { |
| 790 | page = list_to_page(pages); |
| 791 | |
| 792 | prefetchw(&page->flags); |
| 793 | list_del(&page->lru); |
| 794 | |
| 795 | if (!add_to_page_cache_lru(page, mapping, |
| 796 | page->index, GFP_KERNEL)) { |
| 797 | |
| 798 | if (!num) { |
| 799 | num = 1; |
| 800 | first = page; |
| 801 | continue; |
| 802 | } |
| 803 | |
| 804 | dprintk("%s: added to lru page: %p, page_index: %lu, first_index: %lu.\n", |
| 805 | __func__, page, page->index, first->index); |
| 806 | |
| 807 | if (unlikely(first->index + num != page->index) || (num > 500)) { |
| 808 | pohmelfs_send_readpages(POHMELFS_I(mapping->host), |
| 809 | first, num); |
| 810 | first = page; |
| 811 | num = 0; |
| 812 | } |
| 813 | |
| 814 | num++; |
| 815 | } |
| 816 | } |
| 817 | pohmelfs_send_readpages(POHMELFS_I(mapping->host), first, num); |
| 818 | |
| 819 | /* |
| 820 | * This will be sync read, so when last page is processed, |
| 821 | * all previous are alerady unlocked and ready to be used. |
| 822 | */ |
| 823 | return 0; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * Small addres space operations for POHMELFS. |
| 828 | */ |
| 829 | const struct address_space_operations pohmelfs_aops = { |
| 830 | .readpage = pohmelfs_readpage, |
| 831 | .readpages = pohmelfs_readpages, |
| 832 | .writepages = pohmelfs_writepages, |
| 833 | .write_begin = pohmelfs_write_begin, |
| 834 | .write_end = pohmelfs_write_end, |
| 835 | .set_page_dirty = __set_page_dirty_nobuffers, |
| 836 | }; |
| 837 | |
| 838 | /* |
| 839 | * ->detroy_inode() callback. Deletes inode from the caches |
| 840 | * and frees private data. |
| 841 | */ |
| 842 | static void pohmelfs_destroy_inode(struct inode *inode) |
| 843 | { |
| 844 | struct super_block *sb = inode->i_sb; |
| 845 | struct pohmelfs_sb *psb = POHMELFS_SB(sb); |
| 846 | struct pohmelfs_inode *pi = POHMELFS_I(inode); |
| 847 | |
| 848 | //pohmelfs_data_unlock(pi, 0, inode->i_size, POHMELFS_READ_LOCK); |
| 849 | |
| 850 | pohmelfs_inode_del_inode(psb, pi); |
| 851 | |
| 852 | dprintk("%s: pi: %p, inode: %p, ino: %llu.\n", |
| 853 | __func__, pi, &pi->vfs_inode, pi->ino); |
| 854 | kmem_cache_free(pohmelfs_inode_cache, pi); |
| 855 | atomic_long_dec(&psb->total_inodes); |
| 856 | } |
| 857 | |
| 858 | /* |
| 859 | * ->alloc_inode() callback. Allocates inode and initilizes private data. |
| 860 | */ |
| 861 | static struct inode *pohmelfs_alloc_inode(struct super_block *sb) |
| 862 | { |
| 863 | struct pohmelfs_inode *pi; |
| 864 | |
| 865 | pi = kmem_cache_alloc(pohmelfs_inode_cache, GFP_NOIO); |
| 866 | if (!pi) |
| 867 | return NULL; |
| 868 | |
| 869 | pi->hash_root = RB_ROOT; |
| 870 | mutex_init(&pi->offset_lock); |
| 871 | |
| 872 | INIT_LIST_HEAD(&pi->sync_create_list); |
| 873 | |
| 874 | INIT_LIST_HEAD(&pi->inode_entry); |
| 875 | |
| 876 | pi->lock_type = 0; |
| 877 | pi->state = 0; |
| 878 | pi->total_len = 0; |
| 879 | pi->drop_count = 0; |
| 880 | |
| 881 | dprintk("%s: pi: %p, inode: %p.\n", __func__, pi, &pi->vfs_inode); |
| 882 | |
| 883 | atomic_long_inc(&POHMELFS_SB(sb)->total_inodes); |
| 884 | |
| 885 | return &pi->vfs_inode; |
| 886 | } |
| 887 | |
| 888 | /* |
| 889 | * We want fsync() to work on POHMELFS. |
| 890 | */ |
| 891 | static int pohmelfs_fsync(struct file *file, struct dentry *dentry, int datasync) |
| 892 | { |
| 893 | struct inode *inode = file->f_mapping->host; |
| 894 | struct writeback_control wbc = { |
| 895 | .sync_mode = WB_SYNC_ALL, |
| 896 | .nr_to_write = 0, /* sys_fsync did this */ |
| 897 | }; |
| 898 | |
| 899 | return sync_inode(inode, &wbc); |
| 900 | } |
| 901 | |
| 902 | ssize_t pohmelfs_write(struct file *file, const char __user *buf, |
| 903 | size_t len, loff_t *ppos) |
| 904 | { |
| 905 | struct address_space *mapping = file->f_mapping; |
| 906 | struct inode *inode = mapping->host; |
| 907 | struct pohmelfs_inode *pi = POHMELFS_I(inode); |
| 908 | struct iovec iov = { .iov_base = (void __user *)buf, .iov_len = len }; |
| 909 | struct kiocb kiocb; |
| 910 | ssize_t ret; |
| 911 | loff_t pos = *ppos; |
| 912 | |
| 913 | init_sync_kiocb(&kiocb, file); |
| 914 | kiocb.ki_pos = pos; |
| 915 | kiocb.ki_left = len; |
| 916 | |
Alexander Beregalov | 02d84ca | 2009-02-13 17:53:30 +0300 | [diff] [blame] | 917 | dprintk("%s: len: %zu, pos: %llu.\n", __func__, len, pos); |
Evgeniy Polyakov | b3f08ca | 2009-02-09 17:02:38 +0300 | [diff] [blame] | 918 | |
| 919 | mutex_lock(&inode->i_mutex); |
| 920 | ret = pohmelfs_data_lock(pi, pos, len, POHMELFS_WRITE_LOCK); |
| 921 | if (ret) |
| 922 | goto err_out_unlock; |
| 923 | |
| 924 | ret = generic_file_aio_write_nolock(&kiocb, &iov, 1, pos); |
| 925 | *ppos = kiocb.ki_pos; |
| 926 | |
| 927 | mutex_unlock(&inode->i_mutex); |
| 928 | WARN_ON(ret < 0); |
| 929 | |
| 930 | if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) { |
| 931 | ssize_t err; |
| 932 | |
| 933 | err = sync_page_range(inode, mapping, pos, ret); |
| 934 | if (err < 0) |
| 935 | ret = err; |
| 936 | WARN_ON(ret < 0); |
| 937 | } |
| 938 | |
| 939 | return ret; |
| 940 | |
| 941 | err_out_unlock: |
| 942 | mutex_unlock(&inode->i_mutex); |
| 943 | return ret; |
| 944 | } |
| 945 | |
| 946 | const static struct file_operations pohmelfs_file_ops = { |
| 947 | .open = generic_file_open, |
| 948 | .fsync = pohmelfs_fsync, |
| 949 | |
| 950 | .llseek = generic_file_llseek, |
| 951 | |
| 952 | .read = do_sync_read, |
| 953 | .aio_read = generic_file_aio_read, |
| 954 | |
| 955 | .mmap = generic_file_mmap, |
| 956 | |
| 957 | .splice_read = generic_file_splice_read, |
| 958 | .splice_write = generic_file_splice_write, |
| 959 | |
| 960 | .write = pohmelfs_write, |
| 961 | .aio_write = generic_file_aio_write, |
| 962 | }; |
| 963 | |
| 964 | const struct inode_operations pohmelfs_symlink_inode_operations = { |
| 965 | .readlink = generic_readlink, |
| 966 | .follow_link = page_follow_link_light, |
| 967 | .put_link = page_put_link, |
| 968 | }; |
| 969 | |
| 970 | int pohmelfs_setattr_raw(struct inode *inode, struct iattr *attr) |
| 971 | { |
| 972 | int err; |
| 973 | |
| 974 | err = inode_change_ok(inode, attr); |
| 975 | if (err) { |
| 976 | dprintk("%s: ino: %llu, inode changes are not allowed.\n", __func__, POHMELFS_I(inode)->ino); |
| 977 | goto err_out_exit; |
| 978 | } |
| 979 | |
| 980 | if ((attr->ia_valid & ATTR_UID && attr->ia_uid != inode->i_uid) || |
| 981 | (attr->ia_valid & ATTR_GID && attr->ia_gid != inode->i_gid)) { |
Alexander Beregalov | de459f2 | 2009-02-13 15:53:57 +0300 | [diff] [blame] | 982 | err = vfs_dq_transfer(inode, attr) ? -EDQUOT : 0; |
Evgeniy Polyakov | b3f08ca | 2009-02-09 17:02:38 +0300 | [diff] [blame] | 983 | if (err) |
| 984 | goto err_out_exit; |
| 985 | } |
| 986 | |
| 987 | err = inode_setattr(inode, attr); |
| 988 | if (err) { |
| 989 | dprintk("%s: ino: %llu, failed to set the attributes.\n", __func__, POHMELFS_I(inode)->ino); |
| 990 | goto err_out_exit; |
| 991 | } |
| 992 | |
| 993 | dprintk("%s: ino: %llu, mode: %o -> %o, uid: %u -> %u, gid: %u -> %u, size: %llu -> %llu.\n", |
| 994 | __func__, POHMELFS_I(inode)->ino, inode->i_mode, attr->ia_mode, |
| 995 | inode->i_uid, attr->ia_uid, inode->i_gid, attr->ia_gid, inode->i_size, attr->ia_size); |
| 996 | |
| 997 | return 0; |
| 998 | |
| 999 | err_out_exit: |
| 1000 | return err; |
| 1001 | } |
| 1002 | |
| 1003 | int pohmelfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 1004 | { |
| 1005 | struct inode *inode = dentry->d_inode; |
| 1006 | struct pohmelfs_inode *pi = POHMELFS_I(inode); |
| 1007 | int err; |
| 1008 | |
| 1009 | err = pohmelfs_data_lock(pi, 0, ~0, POHMELFS_WRITE_LOCK); |
| 1010 | if (err) |
| 1011 | goto err_out_exit; |
| 1012 | |
| 1013 | err = security_inode_setattr(dentry, attr); |
| 1014 | if (err) |
| 1015 | goto err_out_exit; |
| 1016 | |
| 1017 | err = pohmelfs_setattr_raw(inode, attr); |
| 1018 | if (err) |
| 1019 | goto err_out_exit; |
| 1020 | |
| 1021 | return 0; |
| 1022 | |
| 1023 | err_out_exit: |
| 1024 | return err; |
| 1025 | } |
| 1026 | |
| 1027 | static int pohmelfs_send_xattr_req(struct pohmelfs_inode *pi, u64 id, u64 start, |
| 1028 | const char *name, const void *value, size_t attrsize, int command) |
| 1029 | { |
| 1030 | struct pohmelfs_sb *psb = POHMELFS_SB(pi->vfs_inode.i_sb); |
| 1031 | int err, path_len, namelen = strlen(name) + 1; /* 0-byte */ |
| 1032 | struct netfs_trans *t; |
| 1033 | struct netfs_cmd *cmd; |
| 1034 | void *data; |
| 1035 | |
Alexander Beregalov | 02d84ca | 2009-02-13 17:53:30 +0300 | [diff] [blame] | 1036 | dprintk("%s: id: %llu, start: %llu, name: '%s', attrsize: %zu, cmd: %d.\n", |
Evgeniy Polyakov | b3f08ca | 2009-02-09 17:02:38 +0300 | [diff] [blame] | 1037 | __func__, id, start, name, attrsize, command); |
| 1038 | |
| 1039 | path_len = pohmelfs_path_length(pi); |
| 1040 | if (path_len < 0) { |
| 1041 | err = path_len; |
| 1042 | goto err_out_exit; |
| 1043 | } |
| 1044 | |
| 1045 | t = netfs_trans_alloc(psb, namelen + path_len + attrsize, 0, 0); |
| 1046 | if (!t) { |
| 1047 | err = -ENOMEM; |
| 1048 | goto err_out_exit; |
| 1049 | } |
| 1050 | |
| 1051 | cmd = netfs_trans_current(t); |
| 1052 | data = cmd + 1; |
| 1053 | |
| 1054 | path_len = pohmelfs_construct_path_string(pi, data, path_len); |
| 1055 | if (path_len < 0) { |
| 1056 | err = path_len; |
| 1057 | goto err_out_put; |
| 1058 | } |
| 1059 | data += path_len; |
| 1060 | |
| 1061 | /* |
| 1062 | * 'name' is a NUL-terminated string already and |
| 1063 | * 'namelen' includes 0-byte. |
| 1064 | */ |
| 1065 | memcpy(data, name, namelen); |
| 1066 | data += namelen; |
| 1067 | |
| 1068 | memcpy(data, value, attrsize); |
| 1069 | |
| 1070 | cmd->cmd = command; |
| 1071 | cmd->id = id; |
| 1072 | cmd->start = start; |
| 1073 | cmd->size = attrsize + namelen + path_len; |
| 1074 | cmd->ext = path_len; |
| 1075 | cmd->csize = 0; |
| 1076 | cmd->cpad = 0; |
| 1077 | |
| 1078 | netfs_convert_cmd(cmd); |
| 1079 | netfs_trans_update(cmd, t, namelen + path_len + attrsize); |
| 1080 | |
| 1081 | return netfs_trans_finish(t, psb); |
| 1082 | |
| 1083 | err_out_put: |
| 1084 | t->result = err; |
| 1085 | netfs_trans_put(t); |
| 1086 | err_out_exit: |
| 1087 | return err; |
| 1088 | } |
| 1089 | |
| 1090 | static int pohmelfs_setxattr(struct dentry *dentry, const char *name, |
| 1091 | const void *value, size_t attrsize, int flags) |
| 1092 | { |
| 1093 | struct inode *inode = dentry->d_inode; |
| 1094 | struct pohmelfs_inode *pi = POHMELFS_I(inode); |
| 1095 | struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb); |
| 1096 | |
| 1097 | if (!(psb->state_flags & POHMELFS_FLAGS_XATTR)) |
| 1098 | return -EOPNOTSUPP; |
| 1099 | |
| 1100 | return pohmelfs_send_xattr_req(pi, flags, attrsize, name, |
| 1101 | value, attrsize, NETFS_XATTR_SET); |
| 1102 | } |
| 1103 | |
| 1104 | static ssize_t pohmelfs_getxattr(struct dentry *dentry, const char *name, |
| 1105 | void *value, size_t attrsize) |
| 1106 | { |
| 1107 | struct inode *inode = dentry->d_inode; |
| 1108 | struct pohmelfs_inode *pi = POHMELFS_I(inode); |
| 1109 | struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb); |
| 1110 | struct pohmelfs_mcache *m; |
| 1111 | int err; |
| 1112 | long timeout = psb->mcache_timeout; |
| 1113 | |
| 1114 | if (!(psb->state_flags & POHMELFS_FLAGS_XATTR)) |
| 1115 | return -EOPNOTSUPP; |
| 1116 | |
| 1117 | m = pohmelfs_mcache_alloc(psb, 0, attrsize, value); |
| 1118 | if (IS_ERR(m)) |
| 1119 | return PTR_ERR(m); |
| 1120 | |
| 1121 | dprintk("%s: ino: %llu, name: '%s', size: %zu.\n", |
| 1122 | __func__, pi->ino, name, attrsize); |
| 1123 | |
| 1124 | err = pohmelfs_send_xattr_req(pi, m->gen, attrsize, name, value, 0, NETFS_XATTR_GET); |
| 1125 | if (err) |
| 1126 | goto err_out_put; |
| 1127 | |
| 1128 | do { |
| 1129 | err = wait_for_completion_timeout(&m->complete, timeout); |
| 1130 | if (err) { |
| 1131 | err = m->err; |
| 1132 | break; |
| 1133 | } |
| 1134 | |
| 1135 | /* |
| 1136 | * This loop is a bit ugly, since it waits until reference counter |
| 1137 | * hits 1 and then put object here. Main goal is to prevent race with |
| 1138 | * network thread, when it can start processing given request, i.e. |
| 1139 | * increase its reference counter but yet not complete it, while |
| 1140 | * we will exit from ->getxattr() with timeout, and although request |
| 1141 | * will not be freed (its reference counter was increased by network |
| 1142 | * thread), data pointer provided by user may be released, so we will |
| 1143 | * overwrite already freed area in network thread. |
| 1144 | * |
| 1145 | * Now after timeout we remove request from the cache, so it can not be |
| 1146 | * found by network thread, and wait for its reference counter to hit 1, |
| 1147 | * i.e. if network thread already started to process this request, we wait |
| 1148 | * it to finish, and then free object locally. If reference counter is |
| 1149 | * already 1, i.e. request is not used by anyone else, we can free it without |
| 1150 | * problem. |
| 1151 | */ |
| 1152 | err = -ETIMEDOUT; |
| 1153 | timeout = HZ; |
| 1154 | |
| 1155 | pohmelfs_mcache_remove_locked(psb, m); |
| 1156 | } while (atomic_read(&m->refcnt) != 1); |
| 1157 | |
| 1158 | pohmelfs_mcache_put(psb, m); |
| 1159 | |
| 1160 | dprintk("%s: ino: %llu, err: %d.\n", __func__, pi->ino, err); |
| 1161 | |
| 1162 | return err; |
| 1163 | |
| 1164 | err_out_put: |
| 1165 | pohmelfs_mcache_put(psb, m); |
| 1166 | return err; |
| 1167 | } |
| 1168 | |
| 1169 | static int pohmelfs_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) |
| 1170 | { |
| 1171 | struct inode *inode = dentry->d_inode; |
| 1172 | struct pohmelfs_inode *pi = POHMELFS_I(inode); |
| 1173 | int err; |
Evgeniy Polyakov | 86ac725 | 2009-03-27 15:04:20 +0300 | [diff] [blame] | 1174 | #if 0 |
Evgeniy Polyakov | b3f08ca | 2009-02-09 17:02:38 +0300 | [diff] [blame] | 1175 | err = pohmelfs_data_lock(pi, 0, ~0, POHMELFS_READ_LOCK); |
| 1176 | if (err) |
| 1177 | return err; |
Evgeniy Polyakov | 86ac725 | 2009-03-27 15:04:20 +0300 | [diff] [blame] | 1178 | #endif |
Evgeniy Polyakov | b3f08ca | 2009-02-09 17:02:38 +0300 | [diff] [blame] | 1179 | dprintk("%s: ino: %llu, mode: %o, uid: %u, gid: %u, size: %llu.\n", |
| 1180 | __func__, pi->ino, inode->i_mode, inode->i_uid, |
| 1181 | inode->i_gid, inode->i_size); |
| 1182 | |
| 1183 | generic_fillattr(inode, stat); |
| 1184 | return 0; |
| 1185 | } |
| 1186 | |
| 1187 | const struct inode_operations pohmelfs_file_inode_operations = { |
| 1188 | .setattr = pohmelfs_setattr, |
| 1189 | .getattr = pohmelfs_getattr, |
| 1190 | .setxattr = pohmelfs_setxattr, |
| 1191 | .getxattr = pohmelfs_getxattr, |
| 1192 | }; |
| 1193 | |
| 1194 | /* |
| 1195 | * Fill inode data: mode, size, operation callbacks and so on... |
| 1196 | */ |
| 1197 | void pohmelfs_fill_inode(struct inode *inode, struct netfs_inode_info *info) |
| 1198 | { |
| 1199 | inode->i_mode = info->mode; |
| 1200 | inode->i_nlink = info->nlink; |
| 1201 | inode->i_uid = info->uid; |
| 1202 | inode->i_gid = info->gid; |
| 1203 | inode->i_blocks = info->blocks; |
| 1204 | inode->i_rdev = info->rdev; |
| 1205 | inode->i_size = info->size; |
| 1206 | inode->i_version = info->version; |
| 1207 | inode->i_blkbits = ffs(info->blocksize); |
| 1208 | |
| 1209 | dprintk("%s: inode: %p, num: %lu/%llu inode is regular: %d, dir: %d, link: %d, mode: %o, size: %llu.\n", |
| 1210 | __func__, inode, inode->i_ino, info->ino, |
| 1211 | S_ISREG(inode->i_mode), S_ISDIR(inode->i_mode), |
| 1212 | S_ISLNK(inode->i_mode), inode->i_mode, inode->i_size); |
| 1213 | |
| 1214 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME_SEC; |
| 1215 | |
| 1216 | /* |
| 1217 | * i_mapping is a pointer to i_data during inode initialization. |
| 1218 | */ |
| 1219 | inode->i_data.a_ops = &pohmelfs_aops; |
| 1220 | |
| 1221 | if (S_ISREG(inode->i_mode)) { |
| 1222 | inode->i_fop = &pohmelfs_file_ops; |
| 1223 | inode->i_op = &pohmelfs_file_inode_operations; |
| 1224 | } else if (S_ISDIR(inode->i_mode)) { |
| 1225 | inode->i_fop = &pohmelfs_dir_fops; |
| 1226 | inode->i_op = &pohmelfs_dir_inode_ops; |
| 1227 | } else if (S_ISLNK(inode->i_mode)) { |
| 1228 | inode->i_op = &pohmelfs_symlink_inode_operations; |
| 1229 | inode->i_fop = &pohmelfs_file_ops; |
| 1230 | } else { |
| 1231 | inode->i_fop = &generic_ro_fops; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | static void pohmelfs_drop_inode(struct inode *inode) |
| 1236 | { |
| 1237 | struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb); |
| 1238 | struct pohmelfs_inode *pi = POHMELFS_I(inode); |
| 1239 | |
| 1240 | spin_lock(&psb->ino_lock); |
| 1241 | list_del_init(&pi->inode_entry); |
| 1242 | spin_unlock(&psb->ino_lock); |
| 1243 | |
| 1244 | generic_drop_inode(inode); |
| 1245 | } |
| 1246 | |
| 1247 | static struct pohmelfs_inode *pohmelfs_get_inode_from_list(struct pohmelfs_sb *psb, |
| 1248 | struct list_head *head, unsigned int *count) |
| 1249 | { |
| 1250 | struct pohmelfs_inode *pi = NULL; |
| 1251 | |
| 1252 | spin_lock(&psb->ino_lock); |
| 1253 | if (!list_empty(head)) { |
| 1254 | pi = list_entry(head->next, struct pohmelfs_inode, |
| 1255 | inode_entry); |
| 1256 | list_del_init(&pi->inode_entry); |
| 1257 | *count = pi->drop_count; |
| 1258 | pi->drop_count = 0; |
| 1259 | } |
| 1260 | spin_unlock(&psb->ino_lock); |
| 1261 | |
| 1262 | return pi; |
| 1263 | } |
| 1264 | |
| 1265 | static void pohmelfs_flush_transactions(struct pohmelfs_sb *psb) |
| 1266 | { |
| 1267 | struct pohmelfs_config *c; |
| 1268 | |
| 1269 | mutex_lock(&psb->state_lock); |
| 1270 | list_for_each_entry(c, &psb->state_list, config_entry) { |
| 1271 | pohmelfs_state_flush_transactions(&c->state); |
| 1272 | } |
| 1273 | mutex_unlock(&psb->state_lock); |
| 1274 | } |
| 1275 | |
| 1276 | /* |
| 1277 | * ->put_super() callback. Invoked before superblock is destroyed, |
| 1278 | * so it has to clean all private data. |
| 1279 | */ |
| 1280 | static void pohmelfs_put_super(struct super_block *sb) |
| 1281 | { |
| 1282 | struct pohmelfs_sb *psb = POHMELFS_SB(sb); |
| 1283 | struct pohmelfs_inode *pi; |
| 1284 | unsigned int count; |
| 1285 | unsigned int in_drop_list = 0; |
| 1286 | struct inode *inode, *tmp; |
| 1287 | |
| 1288 | dprintk("%s.\n", __func__); |
| 1289 | |
| 1290 | /* |
| 1291 | * Kill pending transactions, which could affect inodes in-flight. |
| 1292 | */ |
| 1293 | pohmelfs_flush_transactions(psb); |
| 1294 | |
| 1295 | while ((pi = pohmelfs_get_inode_from_list(psb, &psb->drop_list, &count))) { |
| 1296 | inode = &pi->vfs_inode; |
| 1297 | |
| 1298 | dprintk("%s: ino: %llu, pi: %p, inode: %p, count: %u.\n", |
| 1299 | __func__, pi->ino, pi, inode, count); |
| 1300 | |
| 1301 | if (atomic_read(&inode->i_count) != count) { |
| 1302 | printk("%s: ino: %llu, pi: %p, inode: %p, count: %u, i_count: %d.\n", |
| 1303 | __func__, pi->ino, pi, inode, count, |
| 1304 | atomic_read(&inode->i_count)); |
| 1305 | count = atomic_read(&inode->i_count); |
| 1306 | in_drop_list++; |
| 1307 | } |
| 1308 | |
| 1309 | while (count--) |
| 1310 | iput(&pi->vfs_inode); |
| 1311 | } |
| 1312 | |
| 1313 | list_for_each_entry_safe(inode, tmp, &sb->s_inodes, i_sb_list) { |
| 1314 | pi = POHMELFS_I(inode); |
| 1315 | |
| 1316 | dprintk("%s: ino: %llu, pi: %p, inode: %p, i_count: %u.\n", |
| 1317 | __func__, pi->ino, pi, inode, atomic_read(&inode->i_count)); |
| 1318 | |
| 1319 | /* |
| 1320 | * These are special inodes, they were created during |
| 1321 | * directory reading or lookup, and were not bound to dentry, |
| 1322 | * so they live here with reference counter being 1 and prevent |
| 1323 | * umount from succeed since it believes that they are busy. |
| 1324 | */ |
| 1325 | count = atomic_read(&inode->i_count); |
| 1326 | if (count) { |
| 1327 | list_del_init(&inode->i_sb_list); |
| 1328 | while (count--) |
| 1329 | iput(&pi->vfs_inode); |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | psb->trans_scan_timeout = psb->drop_scan_timeout = 0; |
| 1334 | cancel_rearming_delayed_work(&psb->dwork); |
| 1335 | cancel_rearming_delayed_work(&psb->drop_dwork); |
| 1336 | flush_scheduled_work(); |
| 1337 | |
| 1338 | dprintk("%s: stopped workqueues.\n", __func__); |
| 1339 | |
| 1340 | pohmelfs_crypto_exit(psb); |
| 1341 | pohmelfs_state_exit(psb); |
| 1342 | |
| 1343 | kfree(psb); |
| 1344 | sb->s_fs_info = NULL; |
| 1345 | |
| 1346 | pohmelfs_ftrans_exit(); |
| 1347 | } |
| 1348 | |
| 1349 | static int pohmelfs_remount(struct super_block *sb, int *flags, char *data) |
| 1350 | { |
| 1351 | *flags |= MS_RDONLY; |
| 1352 | return 0; |
| 1353 | } |
| 1354 | |
| 1355 | static int pohmelfs_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 1356 | { |
| 1357 | struct super_block *sb = dentry->d_sb; |
| 1358 | struct pohmelfs_sb *psb = POHMELFS_SB(sb); |
| 1359 | |
| 1360 | /* |
| 1361 | * There are no filesystem size limits yet. |
| 1362 | */ |
| 1363 | memset(buf, 0, sizeof(struct kstatfs)); |
| 1364 | |
| 1365 | buf->f_type = POHMELFS_MAGIC_NUM; /* 'POH.' */ |
| 1366 | buf->f_bsize = sb->s_blocksize; |
| 1367 | buf->f_files = psb->ino; |
| 1368 | buf->f_namelen = 255; |
| 1369 | buf->f_files = atomic_long_read(&psb->total_inodes); |
| 1370 | buf->f_bfree = buf->f_bavail = psb->avail_size >> PAGE_SHIFT; |
| 1371 | buf->f_blocks = psb->total_size >> PAGE_SHIFT; |
| 1372 | |
| 1373 | dprintk("%s: total: %llu, avail: %llu, inodes: %llu, bsize: %lu.\n", |
| 1374 | __func__, psb->total_size, psb->avail_size, buf->f_files, sb->s_blocksize); |
| 1375 | |
| 1376 | return 0; |
| 1377 | } |
| 1378 | |
| 1379 | static int pohmelfs_show_options(struct seq_file *seq, struct vfsmount *vfs) |
| 1380 | { |
| 1381 | struct pohmelfs_sb *psb = POHMELFS_SB(vfs->mnt_sb); |
| 1382 | |
| 1383 | seq_printf(seq, ",idx=%u", psb->idx); |
| 1384 | seq_printf(seq, ",trans_scan_timeout=%u", jiffies_to_msecs(psb->trans_scan_timeout)); |
| 1385 | seq_printf(seq, ",drop_scan_timeout=%u", jiffies_to_msecs(psb->drop_scan_timeout)); |
| 1386 | seq_printf(seq, ",wait_on_page_timeout=%u", jiffies_to_msecs(psb->wait_on_page_timeout)); |
| 1387 | seq_printf(seq, ",trans_retries=%u", psb->trans_retries); |
| 1388 | seq_printf(seq, ",crypto_thread_num=%u", psb->crypto_thread_num); |
| 1389 | seq_printf(seq, ",trans_max_pages=%u", psb->trans_max_pages); |
| 1390 | seq_printf(seq, ",mcache_timeout=%u", jiffies_to_msecs(psb->mcache_timeout)); |
| 1391 | if (psb->crypto_fail_unsupported) |
| 1392 | seq_printf(seq, ",crypto_fail_unsupported"); |
| 1393 | |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
| 1397 | static const struct super_operations pohmelfs_sb_ops = { |
| 1398 | .alloc_inode = pohmelfs_alloc_inode, |
| 1399 | .destroy_inode = pohmelfs_destroy_inode, |
| 1400 | .drop_inode = pohmelfs_drop_inode, |
| 1401 | .write_inode = pohmelfs_write_inode, |
| 1402 | .put_super = pohmelfs_put_super, |
| 1403 | .remount_fs = pohmelfs_remount, |
| 1404 | .statfs = pohmelfs_statfs, |
| 1405 | .show_options = pohmelfs_show_options, |
| 1406 | }; |
| 1407 | |
| 1408 | enum { |
| 1409 | pohmelfs_opt_idx, |
| 1410 | pohmelfs_opt_trans_scan_timeout, |
| 1411 | pohmelfs_opt_drop_scan_timeout, |
| 1412 | pohmelfs_opt_wait_on_page_timeout, |
| 1413 | pohmelfs_opt_trans_retries, |
| 1414 | pohmelfs_opt_crypto_thread_num, |
| 1415 | pohmelfs_opt_trans_max_pages, |
| 1416 | pohmelfs_opt_crypto_fail_unsupported, |
| 1417 | pohmelfs_opt_mcache_timeout, |
| 1418 | }; |
| 1419 | |
| 1420 | static struct match_token pohmelfs_tokens[] = { |
| 1421 | {pohmelfs_opt_idx, "idx=%u"}, |
| 1422 | {pohmelfs_opt_trans_scan_timeout, "trans_scan_timeout=%u"}, |
| 1423 | {pohmelfs_opt_drop_scan_timeout, "drop_scan_timeout=%u"}, |
| 1424 | {pohmelfs_opt_wait_on_page_timeout, "wait_on_page_timeout=%u"}, |
| 1425 | {pohmelfs_opt_trans_retries, "trans_retries=%u"}, |
| 1426 | {pohmelfs_opt_crypto_thread_num, "crypto_thread_num=%u"}, |
| 1427 | {pohmelfs_opt_trans_max_pages, "trans_max_pages=%u"}, |
| 1428 | {pohmelfs_opt_crypto_fail_unsupported, "crypto_fail_unsupported"}, |
| 1429 | {pohmelfs_opt_mcache_timeout, "mcache_timeout=%u"}, |
| 1430 | }; |
| 1431 | |
| 1432 | static int pohmelfs_parse_options(char *options, struct pohmelfs_sb *psb) |
| 1433 | { |
| 1434 | char *p; |
| 1435 | substring_t args[MAX_OPT_ARGS]; |
| 1436 | int option, err; |
| 1437 | |
| 1438 | if (!options) |
| 1439 | return 0; |
| 1440 | |
| 1441 | while ((p = strsep(&options, ",")) != NULL) { |
| 1442 | int token; |
| 1443 | if (!*p) |
| 1444 | continue; |
| 1445 | |
| 1446 | token = match_token(p, pohmelfs_tokens, args); |
| 1447 | |
| 1448 | err = match_int(&args[0], &option); |
| 1449 | if (err) |
| 1450 | return err; |
| 1451 | |
| 1452 | switch (token) { |
| 1453 | case pohmelfs_opt_idx: |
| 1454 | psb->idx = option; |
| 1455 | break; |
| 1456 | case pohmelfs_opt_trans_scan_timeout: |
| 1457 | psb->trans_scan_timeout = msecs_to_jiffies(option); |
| 1458 | break; |
| 1459 | case pohmelfs_opt_drop_scan_timeout: |
| 1460 | psb->drop_scan_timeout = msecs_to_jiffies(option); |
| 1461 | break; |
| 1462 | case pohmelfs_opt_wait_on_page_timeout: |
| 1463 | psb->wait_on_page_timeout = msecs_to_jiffies(option); |
| 1464 | break; |
| 1465 | case pohmelfs_opt_mcache_timeout: |
| 1466 | psb->mcache_timeout = msecs_to_jiffies(option); |
| 1467 | break; |
| 1468 | case pohmelfs_opt_trans_retries: |
| 1469 | psb->trans_retries = option; |
| 1470 | break; |
| 1471 | case pohmelfs_opt_crypto_thread_num: |
| 1472 | psb->crypto_thread_num = option; |
| 1473 | break; |
| 1474 | case pohmelfs_opt_trans_max_pages: |
| 1475 | psb->trans_max_pages = option; |
| 1476 | break; |
| 1477 | case pohmelfs_opt_crypto_fail_unsupported: |
| 1478 | psb->crypto_fail_unsupported = 1; |
| 1479 | break; |
| 1480 | default: |
| 1481 | return -EINVAL; |
| 1482 | } |
| 1483 | } |
| 1484 | |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | static void pohmelfs_flush_inode(struct pohmelfs_inode *pi, unsigned int count) |
| 1489 | { |
| 1490 | struct inode *inode = &pi->vfs_inode; |
| 1491 | |
| 1492 | dprintk("%s: %p: ino: %llu, owned: %d.\n", |
| 1493 | __func__, inode, pi->ino, test_bit(NETFS_INODE_OWNED, &pi->state)); |
| 1494 | |
| 1495 | mutex_lock(&inode->i_mutex); |
| 1496 | if (test_and_clear_bit(NETFS_INODE_OWNED, &pi->state)) { |
| 1497 | filemap_fdatawrite(inode->i_mapping); |
| 1498 | inode->i_sb->s_op->write_inode(inode, 0); |
| 1499 | } |
| 1500 | |
| 1501 | truncate_inode_pages(inode->i_mapping, 0); |
| 1502 | |
| 1503 | pohmelfs_data_unlock(pi, 0, ~0, POHMELFS_WRITE_LOCK); |
| 1504 | mutex_unlock(&inode->i_mutex); |
| 1505 | } |
| 1506 | |
| 1507 | static void pohmelfs_put_inode_count(struct pohmelfs_inode *pi, unsigned int count) |
| 1508 | { |
| 1509 | dprintk("%s: ino: %llu, pi: %p, inode: %p, count: %u.\n", |
| 1510 | __func__, pi->ino, pi, &pi->vfs_inode, count); |
| 1511 | |
| 1512 | if (test_and_clear_bit(NETFS_INODE_NEED_FLUSH, &pi->state)) |
| 1513 | pohmelfs_flush_inode(pi, count); |
| 1514 | |
| 1515 | while (count--) |
| 1516 | iput(&pi->vfs_inode); |
| 1517 | } |
| 1518 | |
| 1519 | static void pohmelfs_drop_scan(struct work_struct *work) |
| 1520 | { |
| 1521 | struct pohmelfs_sb *psb = |
| 1522 | container_of(work, struct pohmelfs_sb, drop_dwork.work); |
| 1523 | struct pohmelfs_inode *pi; |
| 1524 | unsigned int count = 0; |
| 1525 | |
| 1526 | while ((pi = pohmelfs_get_inode_from_list(psb, &psb->drop_list, &count))) { |
| 1527 | pohmelfs_put_inode_count(pi, count); |
| 1528 | } |
| 1529 | pohmelfs_check_states(psb); |
| 1530 | |
| 1531 | if (psb->drop_scan_timeout) |
| 1532 | schedule_delayed_work(&psb->drop_dwork, psb->drop_scan_timeout); |
| 1533 | } |
| 1534 | |
| 1535 | /* |
| 1536 | * Run through all transactions starting from the oldest, |
| 1537 | * drop transaction from current state and try to send it |
| 1538 | * to all remote nodes, which are currently installed. |
| 1539 | */ |
| 1540 | static void pohmelfs_trans_scan_state(struct netfs_state *st) |
| 1541 | { |
| 1542 | struct rb_node *rb_node; |
| 1543 | struct netfs_trans_dst *dst; |
| 1544 | struct pohmelfs_sb *psb = st->psb; |
| 1545 | unsigned int timeout = psb->trans_scan_timeout; |
| 1546 | struct netfs_trans *t; |
| 1547 | int err; |
| 1548 | |
| 1549 | mutex_lock(&st->trans_lock); |
| 1550 | for (rb_node = rb_first(&st->trans_root); rb_node; ) { |
| 1551 | dst = rb_entry(rb_node, struct netfs_trans_dst, state_entry); |
| 1552 | t = dst->trans; |
| 1553 | |
| 1554 | if (timeout && time_after(dst->send_time + timeout, jiffies) |
| 1555 | && dst->retries == 0) |
| 1556 | break; |
| 1557 | |
| 1558 | dprintk("%s: t: %p, gen: %u, st: %p, retries: %u, max: %u.\n", |
| 1559 | __func__, t, t->gen, st, dst->retries, psb->trans_retries); |
| 1560 | netfs_trans_get(t); |
| 1561 | |
| 1562 | rb_node = rb_next(rb_node); |
| 1563 | |
| 1564 | err = -ETIMEDOUT; |
| 1565 | if (timeout && (++dst->retries < psb->trans_retries)) { |
| 1566 | err = netfs_trans_resend(t, psb); |
| 1567 | } |
| 1568 | |
| 1569 | if (err || (t->flags & NETFS_TRANS_SINGLE_DST)) { |
| 1570 | if (netfs_trans_remove_nolock(dst, st)) |
| 1571 | netfs_trans_drop_dst_nostate(dst); |
| 1572 | } |
| 1573 | |
| 1574 | t->result = err; |
| 1575 | netfs_trans_put(t); |
| 1576 | } |
| 1577 | mutex_unlock(&st->trans_lock); |
| 1578 | } |
| 1579 | |
| 1580 | /* |
| 1581 | * Walk through all installed network states and resend all |
| 1582 | * transactions, which are old enough. |
| 1583 | */ |
| 1584 | static void pohmelfs_trans_scan(struct work_struct *work) |
| 1585 | { |
| 1586 | struct pohmelfs_sb *psb = |
| 1587 | container_of(work, struct pohmelfs_sb, dwork.work); |
| 1588 | struct netfs_state *st; |
| 1589 | struct pohmelfs_config *c; |
| 1590 | |
| 1591 | mutex_lock(&psb->state_lock); |
| 1592 | list_for_each_entry(c, &psb->state_list, config_entry) { |
| 1593 | st = &c->state; |
| 1594 | |
| 1595 | pohmelfs_trans_scan_state(st); |
| 1596 | } |
| 1597 | mutex_unlock(&psb->state_lock); |
| 1598 | |
| 1599 | /* |
| 1600 | * If no timeout specified then system is in the middle of umount process, |
| 1601 | * so no need to reschedule scanning process again. |
| 1602 | */ |
| 1603 | if (psb->trans_scan_timeout) |
| 1604 | schedule_delayed_work(&psb->dwork, psb->trans_scan_timeout); |
| 1605 | } |
| 1606 | |
| 1607 | int pohmelfs_meta_command_data(struct pohmelfs_inode *pi, u64 id, unsigned int cmd_op, char *addon, |
| 1608 | unsigned int flags, netfs_trans_complete_t complete, void *priv, u64 start) |
| 1609 | { |
| 1610 | struct inode *inode = &pi->vfs_inode; |
| 1611 | struct pohmelfs_sb *psb = POHMELFS_SB(inode->i_sb); |
| 1612 | int err = 0, sz; |
| 1613 | struct netfs_trans *t; |
| 1614 | int path_len, addon_len = 0; |
| 1615 | void *data; |
| 1616 | struct netfs_inode_info *info; |
| 1617 | struct netfs_cmd *cmd; |
| 1618 | |
| 1619 | dprintk("%s: ino: %llu, cmd: %u, addon: %p.\n", __func__, pi->ino, cmd_op, addon); |
| 1620 | |
| 1621 | path_len = pohmelfs_path_length(pi); |
| 1622 | if (path_len < 0) { |
| 1623 | err = path_len; |
| 1624 | goto err_out_exit; |
| 1625 | } |
| 1626 | |
| 1627 | if (addon) |
| 1628 | addon_len = strlen(addon) + 1; /* 0-byte */ |
| 1629 | sz = addon_len; |
| 1630 | |
| 1631 | if (cmd_op == NETFS_INODE_INFO) |
| 1632 | sz += sizeof(struct netfs_inode_info); |
| 1633 | |
| 1634 | t = netfs_trans_alloc(psb, sz + path_len, flags, 0); |
| 1635 | if (!t) { |
| 1636 | err = -ENOMEM; |
| 1637 | goto err_out_exit; |
| 1638 | } |
| 1639 | t->complete = complete; |
| 1640 | t->private = priv; |
| 1641 | |
| 1642 | cmd = netfs_trans_current(t); |
| 1643 | data = (void *)(cmd + 1); |
| 1644 | |
| 1645 | if (cmd_op == NETFS_INODE_INFO) { |
| 1646 | info = (struct netfs_inode_info *)(cmd + 1); |
| 1647 | data = (void *)(info + 1); |
| 1648 | |
| 1649 | /* |
| 1650 | * We are under i_mutex, can read and change whatever we want... |
| 1651 | */ |
| 1652 | info->mode = inode->i_mode; |
| 1653 | info->nlink = inode->i_nlink; |
| 1654 | info->uid = inode->i_uid; |
| 1655 | info->gid = inode->i_gid; |
| 1656 | info->blocks = inode->i_blocks; |
| 1657 | info->rdev = inode->i_rdev; |
| 1658 | info->size = inode->i_size; |
| 1659 | info->version = inode->i_version; |
| 1660 | |
| 1661 | netfs_convert_inode_info(info); |
| 1662 | } |
| 1663 | |
| 1664 | path_len = pohmelfs_construct_path_string(pi, data, path_len); |
| 1665 | if (path_len < 0) |
| 1666 | goto err_out_free; |
| 1667 | |
| 1668 | dprintk("%s: path_len: %d.\n", __func__, path_len); |
| 1669 | |
| 1670 | if (addon) { |
| 1671 | path_len--; /* Do not place null-byte before the addon */ |
| 1672 | path_len += sprintf(data + path_len, "/%s", addon) + 1; /* 0 - byte */ |
| 1673 | } |
| 1674 | |
| 1675 | sz += path_len; |
| 1676 | |
| 1677 | cmd->cmd = cmd_op; |
| 1678 | cmd->ext = path_len; |
| 1679 | cmd->size = sz; |
| 1680 | cmd->id = id; |
| 1681 | cmd->start = start; |
| 1682 | |
| 1683 | netfs_convert_cmd(cmd); |
| 1684 | netfs_trans_update(cmd, t, sz); |
| 1685 | |
| 1686 | /* |
| 1687 | * Note, that it is possible to leak error here: transaction callback will not |
| 1688 | * be invoked for allocation path failure. |
| 1689 | */ |
| 1690 | return netfs_trans_finish(t, psb); |
| 1691 | |
| 1692 | err_out_free: |
| 1693 | netfs_trans_free(t); |
| 1694 | err_out_exit: |
| 1695 | if (complete) |
| 1696 | complete(NULL, 0, priv, err); |
| 1697 | return err; |
| 1698 | } |
| 1699 | |
| 1700 | int pohmelfs_meta_command(struct pohmelfs_inode *pi, unsigned int cmd_op, unsigned int flags, |
| 1701 | netfs_trans_complete_t complete, void *priv, u64 start) |
| 1702 | { |
| 1703 | return pohmelfs_meta_command_data(pi, pi->ino, cmd_op, NULL, flags, complete, priv, start); |
| 1704 | } |
| 1705 | |
| 1706 | /* |
| 1707 | * Send request and wait for POHMELFS root capabilities response, |
| 1708 | * which will update server's informaion about size of the export, |
| 1709 | * permissions, number of objects, available size and so on. |
| 1710 | */ |
| 1711 | static int pohmelfs_root_handshake(struct pohmelfs_sb *psb) |
| 1712 | { |
| 1713 | struct netfs_trans *t; |
| 1714 | struct netfs_cmd *cmd; |
| 1715 | int err = -ENOMEM; |
| 1716 | |
| 1717 | t = netfs_trans_alloc(psb, 0, 0, 0); |
| 1718 | if (!t) |
| 1719 | goto err_out_exit; |
| 1720 | |
| 1721 | cmd = netfs_trans_current(t); |
| 1722 | |
| 1723 | cmd->cmd = NETFS_CAPABILITIES; |
| 1724 | cmd->id = POHMELFS_ROOT_CAPABILITIES; |
| 1725 | cmd->size = 0; |
| 1726 | cmd->start = 0; |
| 1727 | cmd->ext = 0; |
| 1728 | cmd->csize = 0; |
| 1729 | |
| 1730 | netfs_convert_cmd(cmd); |
| 1731 | netfs_trans_update(cmd, t, 0); |
| 1732 | |
| 1733 | err = netfs_trans_finish(t, psb); |
| 1734 | if (err) |
| 1735 | goto err_out_exit; |
| 1736 | |
| 1737 | psb->flags = ~0; |
| 1738 | err = wait_event_interruptible_timeout(psb->wait, |
| 1739 | (psb->flags != ~0), |
| 1740 | psb->wait_on_page_timeout); |
| 1741 | if (!err) { |
| 1742 | err = -ETIMEDOUT; |
| 1743 | } else { |
| 1744 | err = -psb->flags; |
| 1745 | } |
| 1746 | |
| 1747 | if (err) |
| 1748 | goto err_out_exit; |
| 1749 | |
| 1750 | return 0; |
| 1751 | |
| 1752 | err_out_exit: |
| 1753 | return err; |
| 1754 | } |
| 1755 | |
| 1756 | /* |
| 1757 | * Allocate private superblock and create root dir. |
| 1758 | */ |
| 1759 | static int pohmelfs_fill_super(struct super_block *sb, void *data, int silent) |
| 1760 | { |
| 1761 | struct pohmelfs_sb *psb; |
| 1762 | int err = -ENOMEM; |
| 1763 | struct inode *root; |
| 1764 | struct pohmelfs_inode *npi; |
| 1765 | struct qstr str; |
| 1766 | |
| 1767 | pohmelfs_ftrans_init(); |
| 1768 | |
| 1769 | psb = kzalloc(sizeof(struct pohmelfs_sb), GFP_KERNEL); |
| 1770 | if (!psb) |
| 1771 | goto err_out_exit; |
| 1772 | |
| 1773 | sb->s_fs_info = psb; |
| 1774 | sb->s_op = &pohmelfs_sb_ops; |
| 1775 | sb->s_magic = POHMELFS_MAGIC_NUM; |
| 1776 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
| 1777 | sb->s_blocksize = PAGE_SIZE; |
| 1778 | |
| 1779 | psb->sb = sb; |
| 1780 | |
| 1781 | psb->ino = 2; |
| 1782 | psb->idx = 0; |
| 1783 | psb->active_state = NULL; |
| 1784 | psb->trans_retries = 5; |
| 1785 | psb->trans_data_size = PAGE_SIZE; |
| 1786 | psb->drop_scan_timeout = msecs_to_jiffies(1000); |
| 1787 | psb->trans_scan_timeout = msecs_to_jiffies(5000); |
| 1788 | psb->wait_on_page_timeout = msecs_to_jiffies(5000); |
| 1789 | init_waitqueue_head(&psb->wait); |
| 1790 | |
| 1791 | spin_lock_init(&psb->ino_lock); |
| 1792 | |
| 1793 | INIT_LIST_HEAD(&psb->drop_list); |
| 1794 | |
| 1795 | mutex_init(&psb->mcache_lock); |
| 1796 | psb->mcache_root = RB_ROOT; |
| 1797 | psb->mcache_timeout = msecs_to_jiffies(5000); |
| 1798 | atomic_long_set(&psb->mcache_gen, 0); |
| 1799 | |
| 1800 | psb->trans_max_pages = 100; |
| 1801 | |
| 1802 | psb->crypto_align_size = 16; |
| 1803 | psb->crypto_attached_size = 0; |
| 1804 | psb->hash_strlen = 0; |
| 1805 | psb->cipher_strlen = 0; |
| 1806 | psb->perform_crypto = 0; |
| 1807 | psb->crypto_thread_num = 2; |
| 1808 | psb->crypto_fail_unsupported = 0; |
| 1809 | mutex_init(&psb->crypto_thread_lock); |
| 1810 | INIT_LIST_HEAD(&psb->crypto_ready_list); |
| 1811 | INIT_LIST_HEAD(&psb->crypto_active_list); |
| 1812 | |
| 1813 | atomic_set(&psb->trans_gen, 1); |
| 1814 | atomic_set(&psb->total_inodes, 0); |
| 1815 | |
| 1816 | mutex_init(&psb->state_lock); |
| 1817 | INIT_LIST_HEAD(&psb->state_list); |
| 1818 | |
| 1819 | err = pohmelfs_parse_options((char *) data, psb); |
| 1820 | if (err) |
| 1821 | goto err_out_free_sb; |
| 1822 | |
| 1823 | err = pohmelfs_copy_crypto(psb); |
| 1824 | if (err) |
| 1825 | goto err_out_free_sb; |
| 1826 | |
| 1827 | err = pohmelfs_state_init(psb); |
| 1828 | if (err) |
| 1829 | goto err_out_free_strings; |
| 1830 | |
| 1831 | err = pohmelfs_crypto_init(psb); |
| 1832 | if (err) |
| 1833 | goto err_out_state_exit; |
| 1834 | |
| 1835 | err = pohmelfs_root_handshake(psb); |
| 1836 | if (err) |
| 1837 | goto err_out_crypto_exit; |
| 1838 | |
| 1839 | str.name = "/"; |
| 1840 | str.hash = jhash("/", 1, 0); |
| 1841 | str.len = 1; |
| 1842 | |
| 1843 | npi = pohmelfs_create_entry_local(psb, NULL, &str, 0, 0755|S_IFDIR); |
| 1844 | if (IS_ERR(npi)) { |
| 1845 | err = PTR_ERR(npi); |
| 1846 | goto err_out_crypto_exit; |
| 1847 | } |
Evgeniy Polyakov | 872dc5e | 2009-03-27 15:04:22 +0300 | [diff] [blame^] | 1848 | set_bit(NETFS_INODE_REMOTE_SYNCED, &npi->state); |
| 1849 | clear_bit(NETFS_INODE_OWNED, &npi->state); |
Evgeniy Polyakov | b3f08ca | 2009-02-09 17:02:38 +0300 | [diff] [blame] | 1850 | |
| 1851 | root = &npi->vfs_inode; |
| 1852 | |
| 1853 | sb->s_root = d_alloc_root(root); |
| 1854 | if (!sb->s_root) |
| 1855 | goto err_out_put_root; |
| 1856 | |
| 1857 | INIT_DELAYED_WORK(&psb->drop_dwork, pohmelfs_drop_scan); |
| 1858 | schedule_delayed_work(&psb->drop_dwork, psb->drop_scan_timeout); |
| 1859 | |
| 1860 | INIT_DELAYED_WORK(&psb->dwork, pohmelfs_trans_scan); |
| 1861 | schedule_delayed_work(&psb->dwork, psb->trans_scan_timeout); |
| 1862 | |
| 1863 | return 0; |
| 1864 | |
| 1865 | err_out_put_root: |
| 1866 | iput(root); |
| 1867 | err_out_crypto_exit: |
| 1868 | pohmelfs_crypto_exit(psb); |
| 1869 | err_out_state_exit: |
| 1870 | pohmelfs_state_exit(psb); |
| 1871 | err_out_free_strings: |
| 1872 | kfree(psb->cipher_string); |
| 1873 | kfree(psb->hash_string); |
| 1874 | err_out_free_sb: |
| 1875 | kfree(psb); |
| 1876 | err_out_exit: |
| 1877 | |
| 1878 | dprintk("%s: err: %d.\n", __func__, err); |
| 1879 | return err; |
| 1880 | } |
| 1881 | |
| 1882 | /* |
| 1883 | * Some VFS magic here... |
| 1884 | */ |
| 1885 | static int pohmelfs_get_sb(struct file_system_type *fs_type, |
| 1886 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
| 1887 | { |
| 1888 | return get_sb_nodev(fs_type, flags, data, pohmelfs_fill_super, |
| 1889 | mnt); |
| 1890 | } |
| 1891 | |
| 1892 | static struct file_system_type pohmel_fs_type = { |
| 1893 | .owner = THIS_MODULE, |
| 1894 | .name = "pohmel", |
| 1895 | .get_sb = pohmelfs_get_sb, |
| 1896 | .kill_sb = kill_anon_super, |
| 1897 | }; |
| 1898 | |
| 1899 | /* |
| 1900 | * Cache and module initializations and freeing routings. |
| 1901 | */ |
| 1902 | static void pohmelfs_init_once(void *data) |
| 1903 | { |
| 1904 | struct pohmelfs_inode *pi = data; |
| 1905 | |
| 1906 | inode_init_once(&pi->vfs_inode); |
| 1907 | } |
| 1908 | |
| 1909 | static int __init pohmelfs_init_inodecache(void) |
| 1910 | { |
| 1911 | pohmelfs_inode_cache = kmem_cache_create("pohmelfs_inode_cache", |
| 1912 | sizeof(struct pohmelfs_inode), |
| 1913 | 0, (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD), |
| 1914 | pohmelfs_init_once); |
| 1915 | if (!pohmelfs_inode_cache) |
| 1916 | return -ENOMEM; |
| 1917 | |
| 1918 | return 0; |
| 1919 | } |
| 1920 | |
| 1921 | static void pohmelfs_destroy_inodecache(void) |
| 1922 | { |
| 1923 | kmem_cache_destroy(pohmelfs_inode_cache); |
| 1924 | } |
| 1925 | |
| 1926 | static int __init init_pohmel_fs(void) |
| 1927 | { |
| 1928 | int err; |
| 1929 | |
| 1930 | err = pohmelfs_config_init(); |
| 1931 | if (err) |
| 1932 | goto err_out_exit; |
| 1933 | |
| 1934 | err = pohmelfs_init_inodecache(); |
| 1935 | if (err) |
| 1936 | goto err_out_config_exit; |
| 1937 | |
| 1938 | err = pohmelfs_mcache_init(); |
| 1939 | if (err) |
| 1940 | goto err_out_destroy; |
| 1941 | |
| 1942 | err = netfs_trans_init(); |
| 1943 | if (err) |
| 1944 | goto err_out_mcache_exit; |
| 1945 | |
| 1946 | err = register_filesystem(&pohmel_fs_type); |
| 1947 | if (err) |
| 1948 | goto err_out_trans; |
| 1949 | |
| 1950 | return 0; |
| 1951 | |
| 1952 | err_out_trans: |
| 1953 | netfs_trans_exit(); |
| 1954 | err_out_mcache_exit: |
| 1955 | pohmelfs_mcache_exit(); |
| 1956 | err_out_destroy: |
| 1957 | pohmelfs_destroy_inodecache(); |
| 1958 | err_out_config_exit: |
| 1959 | pohmelfs_config_exit(); |
| 1960 | err_out_exit: |
| 1961 | return err; |
| 1962 | } |
| 1963 | |
| 1964 | static void __exit exit_pohmel_fs(void) |
| 1965 | { |
| 1966 | unregister_filesystem(&pohmel_fs_type); |
| 1967 | pohmelfs_destroy_inodecache(); |
| 1968 | pohmelfs_mcache_exit(); |
| 1969 | pohmelfs_config_exit(); |
| 1970 | netfs_trans_exit(); |
| 1971 | } |
| 1972 | |
| 1973 | module_init(init_pohmel_fs); |
| 1974 | module_exit(exit_pohmel_fs); |
| 1975 | |
| 1976 | MODULE_LICENSE("GPL"); |
| 1977 | MODULE_AUTHOR("Evgeniy Polyakov <zbr@ioremap.net>"); |
| 1978 | MODULE_DESCRIPTION("Pohmel filesystem"); |