diff options
Diffstat (limited to 'fs/ceph')
62 files changed, 28006 insertions, 0 deletions
diff --git a/fs/ceph/Kconfig b/fs/ceph/Kconfig new file mode 100644 index 000000000000..04b8280582a9 --- /dev/null +++ b/fs/ceph/Kconfig | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | config CEPH_FS | ||
| 2 | tristate "Ceph distributed file system (EXPERIMENTAL)" | ||
| 3 | depends on INET && EXPERIMENTAL | ||
| 4 | select LIBCRC32C | ||
| 5 | select CONFIG_CRYPTO_AES | ||
| 6 | help | ||
| 7 | Choose Y or M here to include support for mounting the | ||
| 8 | experimental Ceph distributed file system. Ceph is an extremely | ||
| 9 | scalable file system designed to provide high performance, | ||
| 10 | reliable access to petabytes of storage. | ||
| 11 | |||
| 12 | More information at http://ceph.newdream.net/. | ||
| 13 | |||
| 14 | If unsure, say N. | ||
| 15 | |||
| 16 | config CEPH_FS_PRETTYDEBUG | ||
| 17 | bool "Include file:line in ceph debug output" | ||
| 18 | depends on CEPH_FS | ||
| 19 | default n | ||
| 20 | help | ||
| 21 | If you say Y here, debug output will include a filename and | ||
| 22 | line to aid debugging. This icnreases kernel size and slows | ||
| 23 | execution slightly when debug call sites are enabled (e.g., | ||
| 24 | via CONFIG_DYNAMIC_DEBUG). | ||
| 25 | |||
| 26 | If unsure, say N. | ||
| 27 | |||
diff --git a/fs/ceph/Makefile b/fs/ceph/Makefile new file mode 100644 index 000000000000..6a660e610be8 --- /dev/null +++ b/fs/ceph/Makefile | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | # | ||
| 2 | # Makefile for CEPH filesystem. | ||
| 3 | # | ||
| 4 | |||
| 5 | ifneq ($(KERNELRELEASE),) | ||
| 6 | |||
| 7 | obj-$(CONFIG_CEPH_FS) += ceph.o | ||
| 8 | |||
| 9 | ceph-objs := super.o inode.o dir.o file.o addr.o ioctl.o \ | ||
| 10 | export.o caps.o snap.o xattr.o \ | ||
| 11 | messenger.o msgpool.o buffer.o pagelist.o \ | ||
| 12 | mds_client.o mdsmap.o \ | ||
| 13 | mon_client.o \ | ||
| 14 | osd_client.o osdmap.o crush/crush.o crush/mapper.o crush/hash.o \ | ||
| 15 | debugfs.o \ | ||
| 16 | auth.o auth_none.o \ | ||
| 17 | crypto.o armor.o \ | ||
| 18 | auth_x.o \ | ||
| 19 | ceph_fs.o ceph_strings.o ceph_hash.o ceph_frag.o | ||
| 20 | |||
| 21 | else | ||
| 22 | #Otherwise we were called directly from the command | ||
| 23 | # line; invoke the kernel build system. | ||
| 24 | |||
| 25 | KERNELDIR ?= /lib/modules/$(shell uname -r)/build | ||
| 26 | PWD := $(shell pwd) | ||
| 27 | |||
| 28 | default: all | ||
| 29 | |||
| 30 | all: | ||
| 31 | $(MAKE) -C $(KERNELDIR) M=$(PWD) CONFIG_CEPH_FS=m modules | ||
| 32 | |||
| 33 | modules_install: | ||
| 34 | $(MAKE) -C $(KERNELDIR) M=$(PWD) CONFIG_CEPH_FS=m modules_install | ||
| 35 | |||
| 36 | clean: | ||
| 37 | $(MAKE) -C $(KERNELDIR) M=$(PWD) clean | ||
| 38 | |||
| 39 | endif | ||
diff --git a/fs/ceph/README b/fs/ceph/README new file mode 100644 index 000000000000..18352fab37c0 --- /dev/null +++ b/fs/ceph/README | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | # | ||
| 2 | # The following files are shared by (and manually synchronized | ||
| 3 | # between) the Ceph userland and kernel client. | ||
| 4 | # | ||
| 5 | # userland kernel | ||
| 6 | src/include/ceph_fs.h fs/ceph/ceph_fs.h | ||
| 7 | src/include/ceph_fs.cc fs/ceph/ceph_fs.c | ||
| 8 | src/include/msgr.h fs/ceph/msgr.h | ||
| 9 | src/include/rados.h fs/ceph/rados.h | ||
| 10 | src/include/ceph_strings.cc fs/ceph/ceph_strings.c | ||
| 11 | src/include/ceph_frag.h fs/ceph/ceph_frag.h | ||
| 12 | src/include/ceph_frag.cc fs/ceph/ceph_frag.c | ||
| 13 | src/include/ceph_hash.h fs/ceph/ceph_hash.h | ||
| 14 | src/include/ceph_hash.cc fs/ceph/ceph_hash.c | ||
| 15 | src/crush/crush.c fs/ceph/crush/crush.c | ||
| 16 | src/crush/crush.h fs/ceph/crush/crush.h | ||
| 17 | src/crush/mapper.c fs/ceph/crush/mapper.c | ||
| 18 | src/crush/mapper.h fs/ceph/crush/mapper.h | ||
| 19 | src/crush/hash.h fs/ceph/crush/hash.h | ||
| 20 | src/crush/hash.c fs/ceph/crush/hash.c | ||
diff --git a/fs/ceph/addr.c b/fs/ceph/addr.c new file mode 100644 index 000000000000..ce8ef6107727 --- /dev/null +++ b/fs/ceph/addr.c | |||
| @@ -0,0 +1,1194 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/backing-dev.h> | ||
| 4 | #include <linux/fs.h> | ||
| 5 | #include <linux/mm.h> | ||
| 6 | #include <linux/pagemap.h> | ||
| 7 | #include <linux/writeback.h> /* generic_writepages */ | ||
| 8 | #include <linux/pagevec.h> | ||
| 9 | #include <linux/task_io_accounting_ops.h> | ||
| 10 | |||
| 11 | #include "super.h" | ||
| 12 | #include "osd_client.h" | ||
| 13 | |||
| 14 | /* | ||
| 15 | * Ceph address space ops. | ||
| 16 | * | ||
| 17 | * There are a few funny things going on here. | ||
| 18 | * | ||
| 19 | * The page->private field is used to reference a struct | ||
| 20 | * ceph_snap_context for _every_ dirty page. This indicates which | ||
| 21 | * snapshot the page was logically dirtied in, and thus which snap | ||
| 22 | * context needs to be associated with the osd write during writeback. | ||
| 23 | * | ||
| 24 | * Similarly, struct ceph_inode_info maintains a set of counters to | ||
| 25 | * count dirty pages on the inode. In the absense of snapshots, | ||
| 26 | * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count. | ||
| 27 | * | ||
| 28 | * When a snapshot is taken (that is, when the client receives | ||
| 29 | * notification that a snapshot was taken), each inode with caps and | ||
| 30 | * with dirty pages (dirty pages implies there is a cap) gets a new | ||
| 31 | * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending | ||
| 32 | * order, new snaps go to the tail). The i_wrbuffer_ref_head count is | ||
| 33 | * moved to capsnap->dirty. (Unless a sync write is currently in | ||
| 34 | * progress. In that case, the capsnap is said to be "pending", new | ||
| 35 | * writes cannot start, and the capsnap isn't "finalized" until the | ||
| 36 | * write completes (or fails) and a final size/mtime for the inode for | ||
| 37 | * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0. | ||
| 38 | * | ||
| 39 | * On writeback, we must submit writes to the osd IN SNAP ORDER. So, | ||
| 40 | * we look for the first capsnap in i_cap_snaps and write out pages in | ||
| 41 | * that snap context _only_. Then we move on to the next capsnap, | ||
| 42 | * eventually reaching the "live" or "head" context (i.e., pages that | ||
| 43 | * are not yet snapped) and are writing the most recently dirtied | ||
| 44 | * pages. | ||
| 45 | * | ||
| 46 | * Invalidate and so forth must take care to ensure the dirty page | ||
| 47 | * accounting is preserved. | ||
| 48 | */ | ||
| 49 | |||
| 50 | #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10)) | ||
| 51 | #define CONGESTION_OFF_THRESH(congestion_kb) \ | ||
| 52 | (CONGESTION_ON_THRESH(congestion_kb) - \ | ||
| 53 | (CONGESTION_ON_THRESH(congestion_kb) >> 2)) | ||
| 54 | |||
| 55 | |||
| 56 | |||
| 57 | /* | ||
| 58 | * Dirty a page. Optimistically adjust accounting, on the assumption | ||
| 59 | * that we won't race with invalidate. If we do, readjust. | ||
| 60 | */ | ||
| 61 | static int ceph_set_page_dirty(struct page *page) | ||
| 62 | { | ||
| 63 | struct address_space *mapping = page->mapping; | ||
| 64 | struct inode *inode; | ||
| 65 | struct ceph_inode_info *ci; | ||
| 66 | int undo = 0; | ||
| 67 | struct ceph_snap_context *snapc; | ||
| 68 | |||
| 69 | if (unlikely(!mapping)) | ||
| 70 | return !TestSetPageDirty(page); | ||
| 71 | |||
| 72 | if (TestSetPageDirty(page)) { | ||
| 73 | dout("%p set_page_dirty %p idx %lu -- already dirty\n", | ||
| 74 | mapping->host, page, page->index); | ||
| 75 | return 0; | ||
| 76 | } | ||
| 77 | |||
| 78 | inode = mapping->host; | ||
| 79 | ci = ceph_inode(inode); | ||
| 80 | |||
| 81 | /* | ||
| 82 | * Note that we're grabbing a snapc ref here without holding | ||
| 83 | * any locks! | ||
| 84 | */ | ||
| 85 | snapc = ceph_get_snap_context(ci->i_snap_realm->cached_context); | ||
| 86 | |||
| 87 | /* dirty the head */ | ||
| 88 | spin_lock(&inode->i_lock); | ||
| 89 | if (ci->i_wrbuffer_ref_head == 0) | ||
| 90 | ci->i_head_snapc = ceph_get_snap_context(snapc); | ||
| 91 | ++ci->i_wrbuffer_ref_head; | ||
| 92 | if (ci->i_wrbuffer_ref == 0) | ||
| 93 | igrab(inode); | ||
| 94 | ++ci->i_wrbuffer_ref; | ||
| 95 | dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d " | ||
| 96 | "snapc %p seq %lld (%d snaps)\n", | ||
| 97 | mapping->host, page, page->index, | ||
| 98 | ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1, | ||
| 99 | ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, | ||
| 100 | snapc, snapc->seq, snapc->num_snaps); | ||
| 101 | spin_unlock(&inode->i_lock); | ||
| 102 | |||
| 103 | /* now adjust page */ | ||
| 104 | spin_lock_irq(&mapping->tree_lock); | ||
| 105 | if (page->mapping) { /* Race with truncate? */ | ||
| 106 | WARN_ON_ONCE(!PageUptodate(page)); | ||
| 107 | |||
| 108 | if (mapping_cap_account_dirty(mapping)) { | ||
| 109 | __inc_zone_page_state(page, NR_FILE_DIRTY); | ||
| 110 | __inc_bdi_stat(mapping->backing_dev_info, | ||
| 111 | BDI_RECLAIMABLE); | ||
| 112 | task_io_account_write(PAGE_CACHE_SIZE); | ||
| 113 | } | ||
| 114 | radix_tree_tag_set(&mapping->page_tree, | ||
| 115 | page_index(page), PAGECACHE_TAG_DIRTY); | ||
| 116 | |||
| 117 | /* | ||
| 118 | * Reference snap context in page->private. Also set | ||
| 119 | * PagePrivate so that we get invalidatepage callback. | ||
| 120 | */ | ||
| 121 | page->private = (unsigned long)snapc; | ||
| 122 | SetPagePrivate(page); | ||
| 123 | } else { | ||
| 124 | dout("ANON set_page_dirty %p (raced truncate?)\n", page); | ||
| 125 | undo = 1; | ||
| 126 | } | ||
| 127 | |||
| 128 | spin_unlock_irq(&mapping->tree_lock); | ||
| 129 | |||
| 130 | if (undo) | ||
| 131 | /* whoops, we failed to dirty the page */ | ||
| 132 | ceph_put_wrbuffer_cap_refs(ci, 1, snapc); | ||
| 133 | |||
| 134 | __mark_inode_dirty(mapping->host, I_DIRTY_PAGES); | ||
| 135 | |||
| 136 | BUG_ON(!PageDirty(page)); | ||
| 137 | return 1; | ||
| 138 | } | ||
| 139 | |||
| 140 | /* | ||
| 141 | * If we are truncating the full page (i.e. offset == 0), adjust the | ||
| 142 | * dirty page counters appropriately. Only called if there is private | ||
| 143 | * data on the page. | ||
| 144 | */ | ||
| 145 | static void ceph_invalidatepage(struct page *page, unsigned long offset) | ||
| 146 | { | ||
| 147 | struct inode *inode; | ||
| 148 | struct ceph_inode_info *ci; | ||
| 149 | struct ceph_snap_context *snapc = (void *)page->private; | ||
| 150 | |||
| 151 | BUG_ON(!PageLocked(page)); | ||
| 152 | BUG_ON(!page->private); | ||
| 153 | BUG_ON(!PagePrivate(page)); | ||
| 154 | BUG_ON(!page->mapping); | ||
| 155 | |||
| 156 | inode = page->mapping->host; | ||
| 157 | |||
| 158 | /* | ||
| 159 | * We can get non-dirty pages here due to races between | ||
| 160 | * set_page_dirty and truncate_complete_page; just spit out a | ||
| 161 | * warning, in case we end up with accounting problems later. | ||
| 162 | */ | ||
| 163 | if (!PageDirty(page)) | ||
| 164 | pr_err("%p invalidatepage %p page not dirty\n", inode, page); | ||
| 165 | |||
| 166 | if (offset == 0) | ||
| 167 | ClearPageChecked(page); | ||
| 168 | |||
| 169 | ci = ceph_inode(inode); | ||
| 170 | if (offset == 0) { | ||
| 171 | dout("%p invalidatepage %p idx %lu full dirty page %lu\n", | ||
| 172 | inode, page, page->index, offset); | ||
| 173 | ceph_put_wrbuffer_cap_refs(ci, 1, snapc); | ||
| 174 | ceph_put_snap_context(snapc); | ||
| 175 | page->private = 0; | ||
| 176 | ClearPagePrivate(page); | ||
| 177 | } else { | ||
| 178 | dout("%p invalidatepage %p idx %lu partial dirty page\n", | ||
| 179 | inode, page, page->index); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | |||
| 183 | /* just a sanity check */ | ||
| 184 | static int ceph_releasepage(struct page *page, gfp_t g) | ||
| 185 | { | ||
| 186 | struct inode *inode = page->mapping ? page->mapping->host : NULL; | ||
| 187 | dout("%p releasepage %p idx %lu\n", inode, page, page->index); | ||
| 188 | WARN_ON(PageDirty(page)); | ||
| 189 | WARN_ON(page->private); | ||
| 190 | WARN_ON(PagePrivate(page)); | ||
| 191 | return 0; | ||
| 192 | } | ||
| 193 | |||
| 194 | /* | ||
| 195 | * read a single page, without unlocking it. | ||
| 196 | */ | ||
| 197 | static int readpage_nounlock(struct file *filp, struct page *page) | ||
| 198 | { | ||
| 199 | struct inode *inode = filp->f_dentry->d_inode; | ||
| 200 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 201 | struct ceph_osd_client *osdc = &ceph_inode_to_client(inode)->osdc; | ||
| 202 | int err = 0; | ||
| 203 | u64 len = PAGE_CACHE_SIZE; | ||
| 204 | |||
| 205 | dout("readpage inode %p file %p page %p index %lu\n", | ||
| 206 | inode, filp, page, page->index); | ||
| 207 | err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout, | ||
| 208 | page->index << PAGE_CACHE_SHIFT, &len, | ||
| 209 | ci->i_truncate_seq, ci->i_truncate_size, | ||
| 210 | &page, 1); | ||
| 211 | if (err == -ENOENT) | ||
| 212 | err = 0; | ||
| 213 | if (err < 0) { | ||
| 214 | SetPageError(page); | ||
| 215 | goto out; | ||
| 216 | } else if (err < PAGE_CACHE_SIZE) { | ||
| 217 | /* zero fill remainder of page */ | ||
| 218 | zero_user_segment(page, err, PAGE_CACHE_SIZE); | ||
| 219 | } | ||
| 220 | SetPageUptodate(page); | ||
| 221 | |||
| 222 | out: | ||
| 223 | return err < 0 ? err : 0; | ||
| 224 | } | ||
| 225 | |||
| 226 | static int ceph_readpage(struct file *filp, struct page *page) | ||
| 227 | { | ||
| 228 | int r = readpage_nounlock(filp, page); | ||
| 229 | unlock_page(page); | ||
| 230 | return r; | ||
| 231 | } | ||
| 232 | |||
| 233 | /* | ||
| 234 | * Build a vector of contiguous pages from the provided page list. | ||
| 235 | */ | ||
| 236 | static struct page **page_vector_from_list(struct list_head *page_list, | ||
| 237 | unsigned *nr_pages) | ||
| 238 | { | ||
| 239 | struct page **pages; | ||
| 240 | struct page *page; | ||
| 241 | int next_index, contig_pages = 0; | ||
| 242 | |||
| 243 | /* build page vector */ | ||
| 244 | pages = kmalloc(sizeof(*pages) * *nr_pages, GFP_NOFS); | ||
| 245 | if (!pages) | ||
| 246 | return ERR_PTR(-ENOMEM); | ||
| 247 | |||
| 248 | BUG_ON(list_empty(page_list)); | ||
| 249 | next_index = list_entry(page_list->prev, struct page, lru)->index; | ||
| 250 | list_for_each_entry_reverse(page, page_list, lru) { | ||
| 251 | if (page->index == next_index) { | ||
| 252 | dout("readpages page %d %p\n", contig_pages, page); | ||
| 253 | pages[contig_pages] = page; | ||
| 254 | contig_pages++; | ||
| 255 | next_index++; | ||
| 256 | } else { | ||
| 257 | break; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | *nr_pages = contig_pages; | ||
| 261 | return pages; | ||
| 262 | } | ||
| 263 | |||
| 264 | /* | ||
| 265 | * Read multiple pages. Leave pages we don't read + unlock in page_list; | ||
| 266 | * the caller (VM) cleans them up. | ||
| 267 | */ | ||
| 268 | static int ceph_readpages(struct file *file, struct address_space *mapping, | ||
| 269 | struct list_head *page_list, unsigned nr_pages) | ||
| 270 | { | ||
| 271 | struct inode *inode = file->f_dentry->d_inode; | ||
| 272 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 273 | struct ceph_osd_client *osdc = &ceph_inode_to_client(inode)->osdc; | ||
| 274 | int rc = 0; | ||
| 275 | struct page **pages; | ||
| 276 | struct pagevec pvec; | ||
| 277 | loff_t offset; | ||
| 278 | u64 len; | ||
| 279 | |||
| 280 | dout("readpages %p file %p nr_pages %d\n", | ||
| 281 | inode, file, nr_pages); | ||
| 282 | |||
| 283 | pages = page_vector_from_list(page_list, &nr_pages); | ||
| 284 | if (IS_ERR(pages)) | ||
| 285 | return PTR_ERR(pages); | ||
| 286 | |||
| 287 | /* guess read extent */ | ||
| 288 | offset = pages[0]->index << PAGE_CACHE_SHIFT; | ||
| 289 | len = nr_pages << PAGE_CACHE_SHIFT; | ||
| 290 | rc = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout, | ||
| 291 | offset, &len, | ||
| 292 | ci->i_truncate_seq, ci->i_truncate_size, | ||
| 293 | pages, nr_pages); | ||
| 294 | if (rc == -ENOENT) | ||
| 295 | rc = 0; | ||
| 296 | if (rc < 0) | ||
| 297 | goto out; | ||
| 298 | |||
| 299 | /* set uptodate and add to lru in pagevec-sized chunks */ | ||
| 300 | pagevec_init(&pvec, 0); | ||
| 301 | for (; !list_empty(page_list) && len > 0; | ||
| 302 | rc -= PAGE_CACHE_SIZE, len -= PAGE_CACHE_SIZE) { | ||
| 303 | struct page *page = | ||
| 304 | list_entry(page_list->prev, struct page, lru); | ||
| 305 | |||
| 306 | list_del(&page->lru); | ||
| 307 | |||
| 308 | if (rc < (int)PAGE_CACHE_SIZE) { | ||
| 309 | /* zero (remainder of) page */ | ||
| 310 | int s = rc < 0 ? 0 : rc; | ||
| 311 | zero_user_segment(page, s, PAGE_CACHE_SIZE); | ||
| 312 | } | ||
| 313 | |||
| 314 | if (add_to_page_cache(page, mapping, page->index, GFP_NOFS)) { | ||
| 315 | page_cache_release(page); | ||
| 316 | dout("readpages %p add_to_page_cache failed %p\n", | ||
| 317 | inode, page); | ||
| 318 | continue; | ||
| 319 | } | ||
| 320 | dout("readpages %p adding %p idx %lu\n", inode, page, | ||
| 321 | page->index); | ||
| 322 | flush_dcache_page(page); | ||
| 323 | SetPageUptodate(page); | ||
| 324 | unlock_page(page); | ||
| 325 | if (pagevec_add(&pvec, page) == 0) | ||
| 326 | pagevec_lru_add_file(&pvec); /* add to lru */ | ||
| 327 | } | ||
| 328 | pagevec_lru_add_file(&pvec); | ||
| 329 | rc = 0; | ||
| 330 | |||
| 331 | out: | ||
| 332 | kfree(pages); | ||
| 333 | return rc; | ||
| 334 | } | ||
| 335 | |||
| 336 | /* | ||
| 337 | * Get ref for the oldest snapc for an inode with dirty data... that is, the | ||
| 338 | * only snap context we are allowed to write back. | ||
| 339 | * | ||
| 340 | * Caller holds i_lock. | ||
| 341 | */ | ||
| 342 | static struct ceph_snap_context *__get_oldest_context(struct inode *inode, | ||
| 343 | u64 *snap_size) | ||
| 344 | { | ||
| 345 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 346 | struct ceph_snap_context *snapc = NULL; | ||
| 347 | struct ceph_cap_snap *capsnap = NULL; | ||
| 348 | |||
| 349 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { | ||
| 350 | dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap, | ||
| 351 | capsnap->context, capsnap->dirty_pages); | ||
| 352 | if (capsnap->dirty_pages) { | ||
| 353 | snapc = ceph_get_snap_context(capsnap->context); | ||
| 354 | if (snap_size) | ||
| 355 | *snap_size = capsnap->size; | ||
| 356 | break; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | if (!snapc && ci->i_snap_realm) { | ||
| 360 | snapc = ceph_get_snap_context(ci->i_snap_realm->cached_context); | ||
| 361 | dout(" head snapc %p has %d dirty pages\n", | ||
| 362 | snapc, ci->i_wrbuffer_ref_head); | ||
| 363 | } | ||
| 364 | return snapc; | ||
| 365 | } | ||
| 366 | |||
| 367 | static struct ceph_snap_context *get_oldest_context(struct inode *inode, | ||
| 368 | u64 *snap_size) | ||
| 369 | { | ||
| 370 | struct ceph_snap_context *snapc = NULL; | ||
| 371 | |||
| 372 | spin_lock(&inode->i_lock); | ||
| 373 | snapc = __get_oldest_context(inode, snap_size); | ||
| 374 | spin_unlock(&inode->i_lock); | ||
| 375 | return snapc; | ||
| 376 | } | ||
| 377 | |||
| 378 | /* | ||
| 379 | * Write a single page, but leave the page locked. | ||
| 380 | * | ||
| 381 | * If we get a write error, set the page error bit, but still adjust the | ||
| 382 | * dirty page accounting (i.e., page is no longer dirty). | ||
| 383 | */ | ||
| 384 | static int writepage_nounlock(struct page *page, struct writeback_control *wbc) | ||
| 385 | { | ||
| 386 | struct inode *inode; | ||
| 387 | struct ceph_inode_info *ci; | ||
| 388 | struct ceph_client *client; | ||
| 389 | struct ceph_osd_client *osdc; | ||
| 390 | loff_t page_off = page->index << PAGE_CACHE_SHIFT; | ||
| 391 | int len = PAGE_CACHE_SIZE; | ||
| 392 | loff_t i_size; | ||
| 393 | int err = 0; | ||
| 394 | struct ceph_snap_context *snapc; | ||
| 395 | u64 snap_size = 0; | ||
| 396 | long writeback_stat; | ||
| 397 | |||
| 398 | dout("writepage %p idx %lu\n", page, page->index); | ||
| 399 | |||
| 400 | if (!page->mapping || !page->mapping->host) { | ||
| 401 | dout("writepage %p - no mapping\n", page); | ||
| 402 | return -EFAULT; | ||
| 403 | } | ||
| 404 | inode = page->mapping->host; | ||
| 405 | ci = ceph_inode(inode); | ||
| 406 | client = ceph_inode_to_client(inode); | ||
| 407 | osdc = &client->osdc; | ||
| 408 | |||
| 409 | /* verify this is a writeable snap context */ | ||
| 410 | snapc = (void *)page->private; | ||
| 411 | if (snapc == NULL) { | ||
| 412 | dout("writepage %p page %p not dirty?\n", inode, page); | ||
| 413 | goto out; | ||
| 414 | } | ||
| 415 | if (snapc != get_oldest_context(inode, &snap_size)) { | ||
| 416 | dout("writepage %p page %p snapc %p not writeable - noop\n", | ||
| 417 | inode, page, (void *)page->private); | ||
| 418 | /* we should only noop if called by kswapd */ | ||
| 419 | WARN_ON((current->flags & PF_MEMALLOC) == 0); | ||
| 420 | goto out; | ||
| 421 | } | ||
| 422 | |||
| 423 | /* is this a partial page at end of file? */ | ||
| 424 | if (snap_size) | ||
| 425 | i_size = snap_size; | ||
| 426 | else | ||
| 427 | i_size = i_size_read(inode); | ||
| 428 | if (i_size < page_off + len) | ||
| 429 | len = i_size - page_off; | ||
| 430 | |||
| 431 | dout("writepage %p page %p index %lu on %llu~%u\n", | ||
| 432 | inode, page, page->index, page_off, len); | ||
| 433 | |||
| 434 | writeback_stat = atomic_long_inc_return(&client->writeback_count); | ||
| 435 | if (writeback_stat > | ||
| 436 | CONGESTION_ON_THRESH(client->mount_args->congestion_kb)) | ||
| 437 | set_bdi_congested(&client->backing_dev_info, BLK_RW_ASYNC); | ||
| 438 | |||
| 439 | set_page_writeback(page); | ||
| 440 | err = ceph_osdc_writepages(osdc, ceph_vino(inode), | ||
| 441 | &ci->i_layout, snapc, | ||
| 442 | page_off, len, | ||
| 443 | ci->i_truncate_seq, ci->i_truncate_size, | ||
| 444 | &inode->i_mtime, | ||
| 445 | &page, 1, 0, 0, true); | ||
| 446 | if (err < 0) { | ||
| 447 | dout("writepage setting page/mapping error %d %p\n", err, page); | ||
| 448 | SetPageError(page); | ||
| 449 | mapping_set_error(&inode->i_data, err); | ||
| 450 | if (wbc) | ||
| 451 | wbc->pages_skipped++; | ||
| 452 | } else { | ||
| 453 | dout("writepage cleaned page %p\n", page); | ||
| 454 | err = 0; /* vfs expects us to return 0 */ | ||
| 455 | } | ||
| 456 | page->private = 0; | ||
| 457 | ClearPagePrivate(page); | ||
| 458 | end_page_writeback(page); | ||
| 459 | ceph_put_wrbuffer_cap_refs(ci, 1, snapc); | ||
| 460 | ceph_put_snap_context(snapc); | ||
| 461 | out: | ||
| 462 | return err; | ||
| 463 | } | ||
| 464 | |||
| 465 | static int ceph_writepage(struct page *page, struct writeback_control *wbc) | ||
| 466 | { | ||
| 467 | int err; | ||
| 468 | struct inode *inode = page->mapping->host; | ||
| 469 | BUG_ON(!inode); | ||
| 470 | igrab(inode); | ||
| 471 | err = writepage_nounlock(page, wbc); | ||
| 472 | unlock_page(page); | ||
| 473 | iput(inode); | ||
| 474 | return err; | ||
| 475 | } | ||
| 476 | |||
| 477 | |||
| 478 | /* | ||
| 479 | * lame release_pages helper. release_pages() isn't exported to | ||
| 480 | * modules. | ||
| 481 | */ | ||
| 482 | static void ceph_release_pages(struct page **pages, int num) | ||
| 483 | { | ||
| 484 | struct pagevec pvec; | ||
| 485 | int i; | ||
| 486 | |||
| 487 | pagevec_init(&pvec, 0); | ||
| 488 | for (i = 0; i < num; i++) { | ||
| 489 | if (pagevec_add(&pvec, pages[i]) == 0) | ||
| 490 | pagevec_release(&pvec); | ||
| 491 | } | ||
| 492 | pagevec_release(&pvec); | ||
| 493 | } | ||
| 494 | |||
| 495 | |||
| 496 | /* | ||
| 497 | * async writeback completion handler. | ||
| 498 | * | ||
| 499 | * If we get an error, set the mapping error bit, but not the individual | ||
| 500 | * page error bits. | ||
| 501 | */ | ||
| 502 | static void writepages_finish(struct ceph_osd_request *req, | ||
| 503 | struct ceph_msg *msg) | ||
| 504 | { | ||
| 505 | struct inode *inode = req->r_inode; | ||
| 506 | struct ceph_osd_reply_head *replyhead; | ||
| 507 | struct ceph_osd_op *op; | ||
| 508 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 509 | unsigned wrote; | ||
| 510 | struct page *page; | ||
| 511 | int i; | ||
| 512 | struct ceph_snap_context *snapc = req->r_snapc; | ||
| 513 | struct address_space *mapping = inode->i_mapping; | ||
| 514 | struct writeback_control *wbc = req->r_wbc; | ||
| 515 | __s32 rc = -EIO; | ||
| 516 | u64 bytes = 0; | ||
| 517 | struct ceph_client *client = ceph_inode_to_client(inode); | ||
| 518 | long writeback_stat; | ||
| 519 | unsigned issued = __ceph_caps_issued(ci, NULL); | ||
| 520 | |||
| 521 | /* parse reply */ | ||
| 522 | replyhead = msg->front.iov_base; | ||
| 523 | WARN_ON(le32_to_cpu(replyhead->num_ops) == 0); | ||
| 524 | op = (void *)(replyhead + 1); | ||
| 525 | rc = le32_to_cpu(replyhead->result); | ||
| 526 | bytes = le64_to_cpu(op->extent.length); | ||
| 527 | |||
| 528 | if (rc >= 0) { | ||
| 529 | /* | ||
| 530 | * Assume we wrote the pages we originally sent. The | ||
| 531 | * osd might reply with fewer pages if our writeback | ||
| 532 | * raced with a truncation and was adjusted at the osd, | ||
| 533 | * so don't believe the reply. | ||
| 534 | */ | ||
| 535 | wrote = req->r_num_pages; | ||
| 536 | } else { | ||
| 537 | wrote = 0; | ||
| 538 | mapping_set_error(mapping, rc); | ||
| 539 | } | ||
| 540 | dout("writepages_finish %p rc %d bytes %llu wrote %d (pages)\n", | ||
| 541 | inode, rc, bytes, wrote); | ||
| 542 | |||
| 543 | /* clean all pages */ | ||
| 544 | for (i = 0; i < req->r_num_pages; i++) { | ||
| 545 | page = req->r_pages[i]; | ||
| 546 | BUG_ON(!page); | ||
| 547 | WARN_ON(!PageUptodate(page)); | ||
| 548 | |||
| 549 | writeback_stat = | ||
| 550 | atomic_long_dec_return(&client->writeback_count); | ||
| 551 | if (writeback_stat < | ||
| 552 | CONGESTION_OFF_THRESH(client->mount_args->congestion_kb)) | ||
| 553 | clear_bdi_congested(&client->backing_dev_info, | ||
| 554 | BLK_RW_ASYNC); | ||
| 555 | |||
| 556 | if (i >= wrote) { | ||
| 557 | dout("inode %p skipping page %p\n", inode, page); | ||
| 558 | wbc->pages_skipped++; | ||
| 559 | } | ||
| 560 | page->private = 0; | ||
| 561 | ClearPagePrivate(page); | ||
| 562 | ceph_put_snap_context(snapc); | ||
| 563 | dout("unlocking %d %p\n", i, page); | ||
| 564 | end_page_writeback(page); | ||
| 565 | |||
| 566 | /* | ||
| 567 | * We lost the cache cap, need to truncate the page before | ||
| 568 | * it is unlocked, otherwise we'd truncate it later in the | ||
| 569 | * page truncation thread, possibly losing some data that | ||
| 570 | * raced its way in | ||
| 571 | */ | ||
| 572 | if ((issued & CEPH_CAP_FILE_CACHE) == 0) | ||
| 573 | generic_error_remove_page(inode->i_mapping, page); | ||
| 574 | |||
| 575 | unlock_page(page); | ||
| 576 | } | ||
| 577 | dout("%p wrote+cleaned %d pages\n", inode, wrote); | ||
| 578 | ceph_put_wrbuffer_cap_refs(ci, req->r_num_pages, snapc); | ||
| 579 | |||
| 580 | ceph_release_pages(req->r_pages, req->r_num_pages); | ||
| 581 | if (req->r_pages_from_pool) | ||
| 582 | mempool_free(req->r_pages, | ||
| 583 | ceph_client(inode->i_sb)->wb_pagevec_pool); | ||
| 584 | else | ||
| 585 | kfree(req->r_pages); | ||
| 586 | ceph_osdc_put_request(req); | ||
| 587 | } | ||
| 588 | |||
| 589 | /* | ||
| 590 | * allocate a page vec, either directly, or if necessary, via a the | ||
| 591 | * mempool. we avoid the mempool if we can because req->r_num_pages | ||
| 592 | * may be less than the maximum write size. | ||
| 593 | */ | ||
| 594 | static void alloc_page_vec(struct ceph_client *client, | ||
| 595 | struct ceph_osd_request *req) | ||
| 596 | { | ||
| 597 | req->r_pages = kmalloc(sizeof(struct page *) * req->r_num_pages, | ||
| 598 | GFP_NOFS); | ||
| 599 | if (!req->r_pages) { | ||
| 600 | req->r_pages = mempool_alloc(client->wb_pagevec_pool, GFP_NOFS); | ||
| 601 | req->r_pages_from_pool = 1; | ||
| 602 | WARN_ON(!req->r_pages); | ||
| 603 | } | ||
| 604 | } | ||
| 605 | |||
| 606 | /* | ||
| 607 | * initiate async writeback | ||
| 608 | */ | ||
| 609 | static int ceph_writepages_start(struct address_space *mapping, | ||
| 610 | struct writeback_control *wbc) | ||
| 611 | { | ||
| 612 | struct inode *inode = mapping->host; | ||
| 613 | struct backing_dev_info *bdi = mapping->backing_dev_info; | ||
| 614 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 615 | struct ceph_client *client; | ||
| 616 | pgoff_t index, start, end; | ||
| 617 | int range_whole = 0; | ||
| 618 | int should_loop = 1; | ||
| 619 | pgoff_t max_pages = 0, max_pages_ever = 0; | ||
| 620 | struct ceph_snap_context *snapc = NULL, *last_snapc = NULL; | ||
| 621 | struct pagevec pvec; | ||
| 622 | int done = 0; | ||
| 623 | int rc = 0; | ||
| 624 | unsigned wsize = 1 << inode->i_blkbits; | ||
| 625 | struct ceph_osd_request *req = NULL; | ||
| 626 | int do_sync; | ||
| 627 | u64 snap_size = 0; | ||
| 628 | |||
| 629 | /* | ||
| 630 | * Include a 'sync' in the OSD request if this is a data | ||
| 631 | * integrity write (e.g., O_SYNC write or fsync()), or if our | ||
| 632 | * cap is being revoked. | ||
| 633 | */ | ||
| 634 | do_sync = wbc->sync_mode == WB_SYNC_ALL; | ||
| 635 | if (ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER)) | ||
| 636 | do_sync = 1; | ||
| 637 | dout("writepages_start %p dosync=%d (mode=%s)\n", | ||
| 638 | inode, do_sync, | ||
| 639 | wbc->sync_mode == WB_SYNC_NONE ? "NONE" : | ||
| 640 | (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD")); | ||
| 641 | |||
| 642 | client = ceph_inode_to_client(inode); | ||
| 643 | if (client->mount_state == CEPH_MOUNT_SHUTDOWN) { | ||
| 644 | pr_warning("writepage_start %p on forced umount\n", inode); | ||
| 645 | return -EIO; /* we're in a forced umount, don't write! */ | ||
| 646 | } | ||
| 647 | if (client->mount_args->wsize && client->mount_args->wsize < wsize) | ||
| 648 | wsize = client->mount_args->wsize; | ||
| 649 | if (wsize < PAGE_CACHE_SIZE) | ||
| 650 | wsize = PAGE_CACHE_SIZE; | ||
| 651 | max_pages_ever = wsize >> PAGE_CACHE_SHIFT; | ||
| 652 | |||
| 653 | pagevec_init(&pvec, 0); | ||
| 654 | |||
| 655 | /* ?? */ | ||
| 656 | if (wbc->nonblocking && bdi_write_congested(bdi)) { | ||
| 657 | dout(" writepages congested\n"); | ||
| 658 | wbc->encountered_congestion = 1; | ||
| 659 | goto out_final; | ||
| 660 | } | ||
| 661 | |||
| 662 | /* where to start/end? */ | ||
| 663 | if (wbc->range_cyclic) { | ||
| 664 | start = mapping->writeback_index; /* Start from prev offset */ | ||
| 665 | end = -1; | ||
| 666 | dout(" cyclic, start at %lu\n", start); | ||
| 667 | } else { | ||
| 668 | start = wbc->range_start >> PAGE_CACHE_SHIFT; | ||
| 669 | end = wbc->range_end >> PAGE_CACHE_SHIFT; | ||
| 670 | if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX) | ||
| 671 | range_whole = 1; | ||
| 672 | should_loop = 0; | ||
| 673 | dout(" not cyclic, %lu to %lu\n", start, end); | ||
| 674 | } | ||
| 675 | index = start; | ||
| 676 | |||
| 677 | retry: | ||
| 678 | /* find oldest snap context with dirty data */ | ||
| 679 | ceph_put_snap_context(snapc); | ||
| 680 | snapc = get_oldest_context(inode, &snap_size); | ||
| 681 | if (!snapc) { | ||
| 682 | /* hmm, why does writepages get called when there | ||
| 683 | is no dirty data? */ | ||
| 684 | dout(" no snap context with dirty data?\n"); | ||
| 685 | goto out; | ||
| 686 | } | ||
| 687 | dout(" oldest snapc is %p seq %lld (%d snaps)\n", | ||
| 688 | snapc, snapc->seq, snapc->num_snaps); | ||
| 689 | if (last_snapc && snapc != last_snapc) { | ||
| 690 | /* if we switched to a newer snapc, restart our scan at the | ||
| 691 | * start of the original file range. */ | ||
| 692 | dout(" snapc differs from last pass, restarting at %lu\n", | ||
| 693 | index); | ||
| 694 | index = start; | ||
| 695 | } | ||
| 696 | last_snapc = snapc; | ||
| 697 | |||
| 698 | while (!done && index <= end) { | ||
| 699 | unsigned i; | ||
| 700 | int first; | ||
| 701 | pgoff_t next; | ||
| 702 | int pvec_pages, locked_pages; | ||
| 703 | struct page *page; | ||
| 704 | int want; | ||
| 705 | u64 offset, len; | ||
| 706 | struct ceph_osd_request_head *reqhead; | ||
| 707 | struct ceph_osd_op *op; | ||
| 708 | long writeback_stat; | ||
| 709 | |||
| 710 | next = 0; | ||
| 711 | locked_pages = 0; | ||
| 712 | max_pages = max_pages_ever; | ||
| 713 | |||
| 714 | get_more_pages: | ||
| 715 | first = -1; | ||
| 716 | want = min(end - index, | ||
| 717 | min((pgoff_t)PAGEVEC_SIZE, | ||
| 718 | max_pages - (pgoff_t)locked_pages) - 1) | ||
| 719 | + 1; | ||
| 720 | pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index, | ||
| 721 | PAGECACHE_TAG_DIRTY, | ||
| 722 | want); | ||
| 723 | dout("pagevec_lookup_tag got %d\n", pvec_pages); | ||
| 724 | if (!pvec_pages && !locked_pages) | ||
| 725 | break; | ||
| 726 | for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) { | ||
| 727 | page = pvec.pages[i]; | ||
| 728 | dout("? %p idx %lu\n", page, page->index); | ||
| 729 | if (locked_pages == 0) | ||
| 730 | lock_page(page); /* first page */ | ||
| 731 | else if (!trylock_page(page)) | ||
| 732 | break; | ||
| 733 | |||
| 734 | /* only dirty pages, or our accounting breaks */ | ||
| 735 | if (unlikely(!PageDirty(page)) || | ||
| 736 | unlikely(page->mapping != mapping)) { | ||
| 737 | dout("!dirty or !mapping %p\n", page); | ||
| 738 | unlock_page(page); | ||
| 739 | break; | ||
| 740 | } | ||
| 741 | if (!wbc->range_cyclic && page->index > end) { | ||
| 742 | dout("end of range %p\n", page); | ||
| 743 | done = 1; | ||
| 744 | unlock_page(page); | ||
| 745 | break; | ||
| 746 | } | ||
| 747 | if (next && (page->index != next)) { | ||
| 748 | dout("not consecutive %p\n", page); | ||
| 749 | unlock_page(page); | ||
| 750 | break; | ||
| 751 | } | ||
| 752 | if (wbc->sync_mode != WB_SYNC_NONE) { | ||
| 753 | dout("waiting on writeback %p\n", page); | ||
| 754 | wait_on_page_writeback(page); | ||
| 755 | } | ||
| 756 | if ((snap_size && page_offset(page) > snap_size) || | ||
| 757 | (!snap_size && | ||
| 758 | page_offset(page) > i_size_read(inode))) { | ||
| 759 | dout("%p page eof %llu\n", page, snap_size ? | ||
| 760 | snap_size : i_size_read(inode)); | ||
| 761 | done = 1; | ||
| 762 | unlock_page(page); | ||
| 763 | break; | ||
| 764 | } | ||
| 765 | if (PageWriteback(page)) { | ||
| 766 | dout("%p under writeback\n", page); | ||
| 767 | unlock_page(page); | ||
| 768 | break; | ||
| 769 | } | ||
| 770 | |||
| 771 | /* only if matching snap context */ | ||
| 772 | if (snapc != (void *)page->private) { | ||
| 773 | dout("page snapc %p != oldest %p\n", | ||
| 774 | (void *)page->private, snapc); | ||
| 775 | unlock_page(page); | ||
| 776 | if (!locked_pages) | ||
| 777 | continue; /* keep looking for snap */ | ||
| 778 | break; | ||
| 779 | } | ||
| 780 | |||
| 781 | if (!clear_page_dirty_for_io(page)) { | ||
| 782 | dout("%p !clear_page_dirty_for_io\n", page); | ||
| 783 | unlock_page(page); | ||
| 784 | break; | ||
| 785 | } | ||
| 786 | |||
| 787 | /* ok */ | ||
| 788 | if (locked_pages == 0) { | ||
| 789 | /* prepare async write request */ | ||
| 790 | offset = page->index << PAGE_CACHE_SHIFT; | ||
| 791 | len = wsize; | ||
| 792 | req = ceph_osdc_new_request(&client->osdc, | ||
| 793 | &ci->i_layout, | ||
| 794 | ceph_vino(inode), | ||
| 795 | offset, &len, | ||
| 796 | CEPH_OSD_OP_WRITE, | ||
| 797 | CEPH_OSD_FLAG_WRITE | | ||
| 798 | CEPH_OSD_FLAG_ONDISK, | ||
| 799 | snapc, do_sync, | ||
| 800 | ci->i_truncate_seq, | ||
| 801 | ci->i_truncate_size, | ||
| 802 | &inode->i_mtime, true, 1); | ||
| 803 | max_pages = req->r_num_pages; | ||
| 804 | |||
| 805 | alloc_page_vec(client, req); | ||
| 806 | req->r_callback = writepages_finish; | ||
| 807 | req->r_inode = inode; | ||
| 808 | req->r_wbc = wbc; | ||
| 809 | } | ||
| 810 | |||
| 811 | /* note position of first page in pvec */ | ||
| 812 | if (first < 0) | ||
| 813 | first = i; | ||
| 814 | dout("%p will write page %p idx %lu\n", | ||
| 815 | inode, page, page->index); | ||
| 816 | |||
| 817 | writeback_stat = atomic_long_inc_return(&client->writeback_count); | ||
| 818 | if (writeback_stat > CONGESTION_ON_THRESH(client->mount_args->congestion_kb)) { | ||
| 819 | set_bdi_congested(&client->backing_dev_info, BLK_RW_ASYNC); | ||
| 820 | } | ||
| 821 | |||
| 822 | set_page_writeback(page); | ||
| 823 | req->r_pages[locked_pages] = page; | ||
| 824 | locked_pages++; | ||
| 825 | next = page->index + 1; | ||
| 826 | } | ||
| 827 | |||
| 828 | /* did we get anything? */ | ||
| 829 | if (!locked_pages) | ||
| 830 | goto release_pvec_pages; | ||
| 831 | if (i) { | ||
| 832 | int j; | ||
| 833 | BUG_ON(!locked_pages || first < 0); | ||
| 834 | |||
| 835 | if (pvec_pages && i == pvec_pages && | ||
| 836 | locked_pages < max_pages) { | ||
| 837 | dout("reached end pvec, trying for more\n"); | ||
| 838 | pagevec_reinit(&pvec); | ||
| 839 | goto get_more_pages; | ||
| 840 | } | ||
| 841 | |||
| 842 | /* shift unused pages over in the pvec... we | ||
| 843 | * will need to release them below. */ | ||
| 844 | for (j = i; j < pvec_pages; j++) { | ||
| 845 | dout(" pvec leftover page %p\n", | ||
| 846 | pvec.pages[j]); | ||
| 847 | pvec.pages[j-i+first] = pvec.pages[j]; | ||
| 848 | } | ||
| 849 | pvec.nr -= i-first; | ||
| 850 | } | ||
| 851 | |||
| 852 | /* submit the write */ | ||
| 853 | offset = req->r_pages[0]->index << PAGE_CACHE_SHIFT; | ||
| 854 | len = min((snap_size ? snap_size : i_size_read(inode)) - offset, | ||
| 855 | (u64)locked_pages << PAGE_CACHE_SHIFT); | ||
| 856 | dout("writepages got %d pages at %llu~%llu\n", | ||
| 857 | locked_pages, offset, len); | ||
| 858 | |||
| 859 | /* revise final length, page count */ | ||
| 860 | req->r_num_pages = locked_pages; | ||
| 861 | reqhead = req->r_request->front.iov_base; | ||
| 862 | op = (void *)(reqhead + 1); | ||
| 863 | op->extent.length = cpu_to_le64(len); | ||
| 864 | op->payload_len = cpu_to_le32(len); | ||
| 865 | req->r_request->hdr.data_len = cpu_to_le32(len); | ||
| 866 | |||
| 867 | ceph_osdc_start_request(&client->osdc, req, true); | ||
| 868 | req = NULL; | ||
| 869 | |||
| 870 | /* continue? */ | ||
| 871 | index = next; | ||
| 872 | wbc->nr_to_write -= locked_pages; | ||
| 873 | if (wbc->nr_to_write <= 0) | ||
| 874 | done = 1; | ||
| 875 | |||
| 876 | release_pvec_pages: | ||
| 877 | dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr, | ||
| 878 | pvec.nr ? pvec.pages[0] : NULL); | ||
| 879 | pagevec_release(&pvec); | ||
| 880 | |||
| 881 | if (locked_pages && !done) | ||
| 882 | goto retry; | ||
| 883 | } | ||
| 884 | |||
| 885 | if (should_loop && !done) { | ||
| 886 | /* more to do; loop back to beginning of file */ | ||
| 887 | dout("writepages looping back to beginning of file\n"); | ||
| 888 | should_loop = 0; | ||
| 889 | index = 0; | ||
| 890 | goto retry; | ||
| 891 | } | ||
| 892 | |||
| 893 | if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) | ||
| 894 | mapping->writeback_index = index; | ||
| 895 | |||
| 896 | out: | ||
| 897 | if (req) | ||
| 898 | ceph_osdc_put_request(req); | ||
| 899 | if (rc > 0) | ||
| 900 | rc = 0; /* vfs expects us to return 0 */ | ||
| 901 | ceph_put_snap_context(snapc); | ||
| 902 | dout("writepages done, rc = %d\n", rc); | ||
| 903 | out_final: | ||
| 904 | return rc; | ||
| 905 | } | ||
| 906 | |||
| 907 | |||
| 908 | |||
| 909 | /* | ||
| 910 | * See if a given @snapc is either writeable, or already written. | ||
| 911 | */ | ||
| 912 | static int context_is_writeable_or_written(struct inode *inode, | ||
| 913 | struct ceph_snap_context *snapc) | ||
| 914 | { | ||
| 915 | struct ceph_snap_context *oldest = get_oldest_context(inode, NULL); | ||
| 916 | return !oldest || snapc->seq <= oldest->seq; | ||
| 917 | } | ||
| 918 | |||
| 919 | /* | ||
| 920 | * We are only allowed to write into/dirty the page if the page is | ||
| 921 | * clean, or already dirty within the same snap context. | ||
| 922 | * | ||
| 923 | * called with page locked. | ||
| 924 | * return success with page locked, | ||
| 925 | * or any failure (incl -EAGAIN) with page unlocked. | ||
| 926 | */ | ||
| 927 | static int ceph_update_writeable_page(struct file *file, | ||
| 928 | loff_t pos, unsigned len, | ||
| 929 | struct page *page) | ||
| 930 | { | ||
| 931 | struct inode *inode = file->f_dentry->d_inode; | ||
| 932 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 933 | struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; | ||
| 934 | loff_t page_off = pos & PAGE_CACHE_MASK; | ||
| 935 | int pos_in_page = pos & ~PAGE_CACHE_MASK; | ||
| 936 | int end_in_page = pos_in_page + len; | ||
| 937 | loff_t i_size; | ||
| 938 | struct ceph_snap_context *snapc; | ||
| 939 | int r; | ||
| 940 | |||
| 941 | retry_locked: | ||
| 942 | /* writepages currently holds page lock, but if we change that later, */ | ||
| 943 | wait_on_page_writeback(page); | ||
| 944 | |||
| 945 | /* check snap context */ | ||
| 946 | BUG_ON(!ci->i_snap_realm); | ||
| 947 | down_read(&mdsc->snap_rwsem); | ||
| 948 | BUG_ON(!ci->i_snap_realm->cached_context); | ||
| 949 | if (page->private && | ||
| 950 | (void *)page->private != ci->i_snap_realm->cached_context) { | ||
| 951 | /* | ||
| 952 | * this page is already dirty in another (older) snap | ||
| 953 | * context! is it writeable now? | ||
| 954 | */ | ||
| 955 | snapc = get_oldest_context(inode, NULL); | ||
| 956 | up_read(&mdsc->snap_rwsem); | ||
| 957 | |||
| 958 | if (snapc != (void *)page->private) { | ||
| 959 | dout(" page %p snapc %p not current or oldest\n", | ||
| 960 | page, (void *)page->private); | ||
| 961 | /* | ||
| 962 | * queue for writeback, and wait for snapc to | ||
| 963 | * be writeable or written | ||
| 964 | */ | ||
| 965 | snapc = ceph_get_snap_context((void *)page->private); | ||
| 966 | unlock_page(page); | ||
| 967 | ceph_queue_writeback(inode); | ||
| 968 | r = wait_event_interruptible(ci->i_cap_wq, | ||
| 969 | context_is_writeable_or_written(inode, snapc)); | ||
| 970 | ceph_put_snap_context(snapc); | ||
| 971 | if (r == -ERESTARTSYS) | ||
| 972 | return r; | ||
| 973 | return -EAGAIN; | ||
| 974 | } | ||
| 975 | |||
| 976 | /* yay, writeable, do it now (without dropping page lock) */ | ||
| 977 | dout(" page %p snapc %p not current, but oldest\n", | ||
| 978 | page, snapc); | ||
| 979 | if (!clear_page_dirty_for_io(page)) | ||
| 980 | goto retry_locked; | ||
| 981 | r = writepage_nounlock(page, NULL); | ||
| 982 | if (r < 0) | ||
| 983 | goto fail_nosnap; | ||
| 984 | goto retry_locked; | ||
| 985 | } | ||
| 986 | |||
| 987 | if (PageUptodate(page)) { | ||
| 988 | dout(" page %p already uptodate\n", page); | ||
| 989 | return 0; | ||
| 990 | } | ||
| 991 | |||
| 992 | /* full page? */ | ||
| 993 | if (pos_in_page == 0 && len == PAGE_CACHE_SIZE) | ||
| 994 | return 0; | ||
| 995 | |||
| 996 | /* past end of file? */ | ||
| 997 | i_size = inode->i_size; /* caller holds i_mutex */ | ||
| 998 | |||
| 999 | if (i_size + len > inode->i_sb->s_maxbytes) { | ||
| 1000 | /* file is too big */ | ||
| 1001 | r = -EINVAL; | ||
| 1002 | goto fail; | ||
| 1003 | } | ||
| 1004 | |||
| 1005 | if (page_off >= i_size || | ||
| 1006 | (pos_in_page == 0 && (pos+len) >= i_size && | ||
| 1007 | end_in_page - pos_in_page != PAGE_CACHE_SIZE)) { | ||
| 1008 | dout(" zeroing %p 0 - %d and %d - %d\n", | ||
| 1009 | page, pos_in_page, end_in_page, (int)PAGE_CACHE_SIZE); | ||
| 1010 | zero_user_segments(page, | ||
| 1011 | 0, pos_in_page, | ||
| 1012 | end_in_page, PAGE_CACHE_SIZE); | ||
| 1013 | return 0; | ||
| 1014 | } | ||
| 1015 | |||
| 1016 | /* we need to read it. */ | ||
| 1017 | up_read(&mdsc->snap_rwsem); | ||
| 1018 | r = readpage_nounlock(file, page); | ||
| 1019 | if (r < 0) | ||
| 1020 | goto fail_nosnap; | ||
| 1021 | goto retry_locked; | ||
| 1022 | |||
| 1023 | fail: | ||
| 1024 | up_read(&mdsc->snap_rwsem); | ||
| 1025 | fail_nosnap: | ||
| 1026 | unlock_page(page); | ||
| 1027 | return r; | ||
| 1028 | } | ||
| 1029 | |||
| 1030 | /* | ||
| 1031 | * We are only allowed to write into/dirty the page if the page is | ||
| 1032 | * clean, or already dirty within the same snap context. | ||
| 1033 | */ | ||
| 1034 | static int ceph_write_begin(struct file *file, struct address_space *mapping, | ||
| 1035 | loff_t pos, unsigned len, unsigned flags, | ||
| 1036 | struct page **pagep, void **fsdata) | ||
| 1037 | { | ||
| 1038 | struct inode *inode = file->f_dentry->d_inode; | ||
| 1039 | struct page *page; | ||
| 1040 | pgoff_t index = pos >> PAGE_CACHE_SHIFT; | ||
| 1041 | int r; | ||
| 1042 | |||
| 1043 | do { | ||
| 1044 | /* get a page */ | ||
| 1045 | page = grab_cache_page_write_begin(mapping, index, 0); | ||
| 1046 | if (!page) | ||
| 1047 | return -ENOMEM; | ||
| 1048 | *pagep = page; | ||
| 1049 | |||
| 1050 | dout("write_begin file %p inode %p page %p %d~%d\n", file, | ||
| 1051 | inode, page, (int)pos, (int)len); | ||
| 1052 | |||
| 1053 | r = ceph_update_writeable_page(file, pos, len, page); | ||
| 1054 | } while (r == -EAGAIN); | ||
| 1055 | |||
| 1056 | return r; | ||
| 1057 | } | ||
| 1058 | |||
| 1059 | /* | ||
| 1060 | * we don't do anything in here that simple_write_end doesn't do | ||
| 1061 | * except adjust dirty page accounting and drop read lock on | ||
| 1062 | * mdsc->snap_rwsem. | ||
| 1063 | */ | ||
| 1064 | static int ceph_write_end(struct file *file, struct address_space *mapping, | ||
| 1065 | loff_t pos, unsigned len, unsigned copied, | ||
| 1066 | struct page *page, void *fsdata) | ||
| 1067 | { | ||
| 1068 | struct inode *inode = file->f_dentry->d_inode; | ||
| 1069 | struct ceph_client *client = ceph_inode_to_client(inode); | ||
| 1070 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 1071 | unsigned from = pos & (PAGE_CACHE_SIZE - 1); | ||
| 1072 | int check_cap = 0; | ||
| 1073 | |||
| 1074 | dout("write_end file %p inode %p page %p %d~%d (%d)\n", file, | ||
| 1075 | inode, page, (int)pos, (int)copied, (int)len); | ||
| 1076 | |||
| 1077 | /* zero the stale part of the page if we did a short copy */ | ||
| 1078 | if (copied < len) | ||
| 1079 | zero_user_segment(page, from+copied, len); | ||
| 1080 | |||
| 1081 | /* did file size increase? */ | ||
| 1082 | /* (no need for i_size_read(); we caller holds i_mutex */ | ||
| 1083 | if (pos+copied > inode->i_size) | ||
| 1084 | check_cap = ceph_inode_set_size(inode, pos+copied); | ||
| 1085 | |||
| 1086 | if (!PageUptodate(page)) | ||
| 1087 | SetPageUptodate(page); | ||
| 1088 | |||
| 1089 | set_page_dirty(page); | ||
| 1090 | |||
| 1091 | unlock_page(page); | ||
| 1092 | up_read(&mdsc->snap_rwsem); | ||
| 1093 | page_cache_release(page); | ||
| 1094 | |||
| 1095 | if (check_cap) | ||
| 1096 | ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL); | ||
| 1097 | |||
| 1098 | return copied; | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | /* | ||
| 1102 | * we set .direct_IO to indicate direct io is supported, but since we | ||
| 1103 | * intercept O_DIRECT reads and writes early, this function should | ||
| 1104 | * never get called. | ||
| 1105 | */ | ||
| 1106 | static ssize_t ceph_direct_io(int rw, struct kiocb *iocb, | ||
| 1107 | const struct iovec *iov, | ||
| 1108 | loff_t pos, unsigned long nr_segs) | ||
| 1109 | { | ||
| 1110 | WARN_ON(1); | ||
| 1111 | return -EINVAL; | ||
| 1112 | } | ||
| 1113 | |||
| 1114 | const struct address_space_operations ceph_aops = { | ||
| 1115 | .readpage = ceph_readpage, | ||
| 1116 | .readpages = ceph_readpages, | ||
| 1117 | .writepage = ceph_writepage, | ||
| 1118 | .writepages = ceph_writepages_start, | ||
| 1119 | .write_begin = ceph_write_begin, | ||
| 1120 | .write_end = ceph_write_end, | ||
| 1121 | .set_page_dirty = ceph_set_page_dirty, | ||
| 1122 | .invalidatepage = ceph_invalidatepage, | ||
| 1123 | .releasepage = ceph_releasepage, | ||
| 1124 | .direct_IO = ceph_direct_io, | ||
| 1125 | }; | ||
| 1126 | |||
| 1127 | |||
| 1128 | /* | ||
| 1129 | * vm ops | ||
| 1130 | */ | ||
| 1131 | |||
| 1132 | /* | ||
| 1133 | * Reuse write_begin here for simplicity. | ||
| 1134 | */ | ||
| 1135 | static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf) | ||
| 1136 | { | ||
| 1137 | struct inode *inode = vma->vm_file->f_dentry->d_inode; | ||
| 1138 | struct page *page = vmf->page; | ||
| 1139 | struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; | ||
| 1140 | loff_t off = page->index << PAGE_CACHE_SHIFT; | ||
| 1141 | loff_t size, len; | ||
| 1142 | int ret; | ||
| 1143 | |||
| 1144 | size = i_size_read(inode); | ||
| 1145 | if (off + PAGE_CACHE_SIZE <= size) | ||
| 1146 | len = PAGE_CACHE_SIZE; | ||
| 1147 | else | ||
| 1148 | len = size & ~PAGE_CACHE_MASK; | ||
| 1149 | |||
| 1150 | dout("page_mkwrite %p %llu~%llu page %p idx %lu\n", inode, | ||
| 1151 | off, len, page, page->index); | ||
| 1152 | |||
| 1153 | lock_page(page); | ||
| 1154 | |||
| 1155 | ret = VM_FAULT_NOPAGE; | ||
| 1156 | if ((off > size) || | ||
| 1157 | (page->mapping != inode->i_mapping)) | ||
| 1158 | goto out; | ||
| 1159 | |||
| 1160 | ret = ceph_update_writeable_page(vma->vm_file, off, len, page); | ||
| 1161 | if (ret == 0) { | ||
| 1162 | /* success. we'll keep the page locked. */ | ||
| 1163 | set_page_dirty(page); | ||
| 1164 | up_read(&mdsc->snap_rwsem); | ||
| 1165 | ret = VM_FAULT_LOCKED; | ||
| 1166 | } else { | ||
| 1167 | if (ret == -ENOMEM) | ||
| 1168 | ret = VM_FAULT_OOM; | ||
| 1169 | else | ||
| 1170 | ret = VM_FAULT_SIGBUS; | ||
| 1171 | } | ||
| 1172 | out: | ||
| 1173 | dout("page_mkwrite %p %llu~%llu = %d\n", inode, off, len, ret); | ||
| 1174 | if (ret != VM_FAULT_LOCKED) | ||
| 1175 | unlock_page(page); | ||
| 1176 | return ret; | ||
| 1177 | } | ||
| 1178 | |||
| 1179 | static struct vm_operations_struct ceph_vmops = { | ||
| 1180 | .fault = filemap_fault, | ||
| 1181 | .page_mkwrite = ceph_page_mkwrite, | ||
| 1182 | }; | ||
| 1183 | |||
| 1184 | int ceph_mmap(struct file *file, struct vm_area_struct *vma) | ||
| 1185 | { | ||
| 1186 | struct address_space *mapping = file->f_mapping; | ||
| 1187 | |||
| 1188 | if (!mapping->a_ops->readpage) | ||
| 1189 | return -ENOEXEC; | ||
| 1190 | file_accessed(file); | ||
| 1191 | vma->vm_ops = &ceph_vmops; | ||
| 1192 | vma->vm_flags |= VM_CAN_NONLINEAR; | ||
| 1193 | return 0; | ||
| 1194 | } | ||
diff --git a/fs/ceph/armor.c b/fs/ceph/armor.c new file mode 100644 index 000000000000..67b2c030924b --- /dev/null +++ b/fs/ceph/armor.c | |||
| @@ -0,0 +1,99 @@ | |||
| 1 | |||
| 2 | #include <linux/errno.h> | ||
| 3 | |||
| 4 | /* | ||
| 5 | * base64 encode/decode. | ||
| 6 | */ | ||
| 7 | |||
| 8 | const char *pem_key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; | ||
| 9 | |||
| 10 | static int encode_bits(int c) | ||
| 11 | { | ||
| 12 | return pem_key[c]; | ||
| 13 | } | ||
| 14 | |||
| 15 | static int decode_bits(char c) | ||
| 16 | { | ||
| 17 | if (c >= 'A' && c <= 'Z') | ||
| 18 | return c - 'A'; | ||
| 19 | if (c >= 'a' && c <= 'z') | ||
| 20 | return c - 'a' + 26; | ||
| 21 | if (c >= '0' && c <= '9') | ||
| 22 | return c - '0' + 52; | ||
| 23 | if (c == '+') | ||
| 24 | return 62; | ||
| 25 | if (c == '/') | ||
| 26 | return 63; | ||
| 27 | if (c == '=') | ||
| 28 | return 0; /* just non-negative, please */ | ||
| 29 | return -EINVAL; | ||
| 30 | } | ||
| 31 | |||
| 32 | int ceph_armor(char *dst, const char *src, const char *end) | ||
| 33 | { | ||
| 34 | int olen = 0; | ||
| 35 | int line = 0; | ||
| 36 | |||
| 37 | while (src < end) { | ||
| 38 | unsigned char a, b, c; | ||
| 39 | |||
| 40 | a = *src++; | ||
| 41 | *dst++ = encode_bits(a >> 2); | ||
| 42 | if (src < end) { | ||
| 43 | b = *src++; | ||
| 44 | *dst++ = encode_bits(((a & 3) << 4) | (b >> 4)); | ||
| 45 | if (src < end) { | ||
| 46 | c = *src++; | ||
| 47 | *dst++ = encode_bits(((b & 15) << 2) | | ||
| 48 | (c >> 6)); | ||
| 49 | *dst++ = encode_bits(c & 63); | ||
| 50 | } else { | ||
| 51 | *dst++ = encode_bits((b & 15) << 2); | ||
| 52 | *dst++ = '='; | ||
| 53 | } | ||
| 54 | } else { | ||
| 55 | *dst++ = encode_bits(((a & 3) << 4)); | ||
| 56 | *dst++ = '='; | ||
| 57 | *dst++ = '='; | ||
| 58 | } | ||
| 59 | olen += 4; | ||
| 60 | line += 4; | ||
| 61 | if (line == 64) { | ||
| 62 | line = 0; | ||
| 63 | *(dst++) = '\n'; | ||
| 64 | olen++; | ||
| 65 | } | ||
| 66 | } | ||
| 67 | return olen; | ||
| 68 | } | ||
| 69 | |||
| 70 | int ceph_unarmor(char *dst, const char *src, const char *end) | ||
| 71 | { | ||
| 72 | int olen = 0; | ||
| 73 | |||
| 74 | while (src < end) { | ||
| 75 | int a, b, c, d; | ||
| 76 | |||
| 77 | if (src < end && src[0] == '\n') | ||
| 78 | src++; | ||
| 79 | if (src + 4 > end) | ||
| 80 | return -EINVAL; | ||
| 81 | a = decode_bits(src[0]); | ||
| 82 | b = decode_bits(src[1]); | ||
| 83 | c = decode_bits(src[2]); | ||
| 84 | d = decode_bits(src[3]); | ||
| 85 | if (a < 0 || b < 0 || c < 0 || d < 0) | ||
| 86 | return -EINVAL; | ||
| 87 | |||
| 88 | *dst++ = (a << 2) | (b >> 4); | ||
| 89 | if (src[2] == '=') | ||
| 90 | return olen + 1; | ||
| 91 | *dst++ = ((b & 15) << 4) | (c >> 2); | ||
| 92 | if (src[3] == '=') | ||
| 93 | return olen + 2; | ||
| 94 | *dst++ = ((c & 3) << 6) | d; | ||
| 95 | olen += 3; | ||
| 96 | src += 4; | ||
| 97 | } | ||
| 98 | return olen; | ||
| 99 | } | ||
diff --git a/fs/ceph/auth.c b/fs/ceph/auth.c new file mode 100644 index 000000000000..abb204fea6c7 --- /dev/null +++ b/fs/ceph/auth.c | |||
| @@ -0,0 +1,257 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/module.h> | ||
| 4 | #include <linux/err.h> | ||
| 5 | |||
| 6 | #include "types.h" | ||
| 7 | #include "auth_none.h" | ||
| 8 | #include "auth_x.h" | ||
| 9 | #include "decode.h" | ||
| 10 | #include "super.h" | ||
| 11 | |||
| 12 | #include "messenger.h" | ||
| 13 | |||
| 14 | /* | ||
| 15 | * get protocol handler | ||
| 16 | */ | ||
| 17 | static u32 supported_protocols[] = { | ||
| 18 | CEPH_AUTH_NONE, | ||
| 19 | CEPH_AUTH_CEPHX | ||
| 20 | }; | ||
| 21 | |||
| 22 | int ceph_auth_init_protocol(struct ceph_auth_client *ac, int protocol) | ||
| 23 | { | ||
| 24 | switch (protocol) { | ||
| 25 | case CEPH_AUTH_NONE: | ||
| 26 | return ceph_auth_none_init(ac); | ||
| 27 | case CEPH_AUTH_CEPHX: | ||
| 28 | return ceph_x_init(ac); | ||
| 29 | default: | ||
| 30 | return -ENOENT; | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | /* | ||
| 35 | * setup, teardown. | ||
| 36 | */ | ||
| 37 | struct ceph_auth_client *ceph_auth_init(const char *name, const char *secret) | ||
| 38 | { | ||
| 39 | struct ceph_auth_client *ac; | ||
| 40 | int ret; | ||
| 41 | |||
| 42 | dout("auth_init name '%s' secret '%s'\n", name, secret); | ||
| 43 | |||
| 44 | ret = -ENOMEM; | ||
| 45 | ac = kzalloc(sizeof(*ac), GFP_NOFS); | ||
| 46 | if (!ac) | ||
| 47 | goto out; | ||
| 48 | |||
| 49 | ac->negotiating = true; | ||
| 50 | if (name) | ||
| 51 | ac->name = name; | ||
| 52 | else | ||
| 53 | ac->name = CEPH_AUTH_NAME_DEFAULT; | ||
| 54 | dout("auth_init name %s secret %s\n", ac->name, secret); | ||
| 55 | ac->secret = secret; | ||
| 56 | return ac; | ||
| 57 | |||
| 58 | out: | ||
| 59 | return ERR_PTR(ret); | ||
| 60 | } | ||
| 61 | |||
| 62 | void ceph_auth_destroy(struct ceph_auth_client *ac) | ||
| 63 | { | ||
| 64 | dout("auth_destroy %p\n", ac); | ||
| 65 | if (ac->ops) | ||
| 66 | ac->ops->destroy(ac); | ||
| 67 | kfree(ac); | ||
| 68 | } | ||
| 69 | |||
| 70 | /* | ||
| 71 | * Reset occurs when reconnecting to the monitor. | ||
| 72 | */ | ||
| 73 | void ceph_auth_reset(struct ceph_auth_client *ac) | ||
| 74 | { | ||
| 75 | dout("auth_reset %p\n", ac); | ||
| 76 | if (ac->ops && !ac->negotiating) | ||
| 77 | ac->ops->reset(ac); | ||
| 78 | ac->negotiating = true; | ||
| 79 | } | ||
| 80 | |||
| 81 | int ceph_entity_name_encode(const char *name, void **p, void *end) | ||
| 82 | { | ||
| 83 | int len = strlen(name); | ||
| 84 | |||
| 85 | if (*p + 2*sizeof(u32) + len > end) | ||
| 86 | return -ERANGE; | ||
| 87 | ceph_encode_32(p, CEPH_ENTITY_TYPE_CLIENT); | ||
| 88 | ceph_encode_32(p, len); | ||
| 89 | ceph_encode_copy(p, name, len); | ||
| 90 | return 0; | ||
| 91 | } | ||
| 92 | |||
| 93 | /* | ||
| 94 | * Initiate protocol negotiation with monitor. Include entity name | ||
| 95 | * and list supported protocols. | ||
| 96 | */ | ||
| 97 | int ceph_auth_build_hello(struct ceph_auth_client *ac, void *buf, size_t len) | ||
| 98 | { | ||
| 99 | struct ceph_mon_request_header *monhdr = buf; | ||
| 100 | void *p = monhdr + 1, *end = buf + len, *lenp; | ||
| 101 | int i, num; | ||
| 102 | int ret; | ||
| 103 | |||
| 104 | dout("auth_build_hello\n"); | ||
| 105 | monhdr->have_version = 0; | ||
| 106 | monhdr->session_mon = cpu_to_le16(-1); | ||
| 107 | monhdr->session_mon_tid = 0; | ||
| 108 | |||
| 109 | ceph_encode_32(&p, 0); /* no protocol, yet */ | ||
| 110 | |||
| 111 | lenp = p; | ||
| 112 | p += sizeof(u32); | ||
| 113 | |||
| 114 | ceph_decode_need(&p, end, 1 + sizeof(u32), bad); | ||
| 115 | ceph_encode_8(&p, 1); | ||
| 116 | num = ARRAY_SIZE(supported_protocols); | ||
| 117 | ceph_encode_32(&p, num); | ||
| 118 | ceph_decode_need(&p, end, num * sizeof(u32), bad); | ||
| 119 | for (i = 0; i < num; i++) | ||
| 120 | ceph_encode_32(&p, supported_protocols[i]); | ||
| 121 | |||
| 122 | ret = ceph_entity_name_encode(ac->name, &p, end); | ||
| 123 | if (ret < 0) | ||
| 124 | return ret; | ||
| 125 | ceph_decode_need(&p, end, sizeof(u64), bad); | ||
| 126 | ceph_encode_64(&p, ac->global_id); | ||
| 127 | |||
| 128 | ceph_encode_32(&lenp, p - lenp - sizeof(u32)); | ||
| 129 | return p - buf; | ||
| 130 | |||
| 131 | bad: | ||
| 132 | return -ERANGE; | ||
| 133 | } | ||
| 134 | |||
| 135 | int ceph_build_auth_request(struct ceph_auth_client *ac, | ||
| 136 | void *msg_buf, size_t msg_len) | ||
| 137 | { | ||
| 138 | struct ceph_mon_request_header *monhdr = msg_buf; | ||
| 139 | void *p = monhdr + 1; | ||
| 140 | void *end = msg_buf + msg_len; | ||
| 141 | int ret; | ||
| 142 | |||
| 143 | monhdr->have_version = 0; | ||
| 144 | monhdr->session_mon = cpu_to_le16(-1); | ||
| 145 | monhdr->session_mon_tid = 0; | ||
| 146 | |||
| 147 | ceph_encode_32(&p, ac->protocol); | ||
| 148 | |||
| 149 | ret = ac->ops->build_request(ac, p + sizeof(u32), end); | ||
| 150 | if (ret < 0) { | ||
| 151 | pr_err("error %d building request\n", ret); | ||
| 152 | return ret; | ||
| 153 | } | ||
| 154 | dout(" built request %d bytes\n", ret); | ||
| 155 | ceph_encode_32(&p, ret); | ||
| 156 | return p + ret - msg_buf; | ||
| 157 | } | ||
| 158 | |||
| 159 | /* | ||
| 160 | * Handle auth message from monitor. | ||
| 161 | */ | ||
| 162 | int ceph_handle_auth_reply(struct ceph_auth_client *ac, | ||
| 163 | void *buf, size_t len, | ||
| 164 | void *reply_buf, size_t reply_len) | ||
| 165 | { | ||
| 166 | void *p = buf; | ||
| 167 | void *end = buf + len; | ||
| 168 | int protocol; | ||
| 169 | s32 result; | ||
| 170 | u64 global_id; | ||
| 171 | void *payload, *payload_end; | ||
| 172 | int payload_len; | ||
| 173 | char *result_msg; | ||
| 174 | int result_msg_len; | ||
| 175 | int ret = -EINVAL; | ||
| 176 | |||
| 177 | dout("handle_auth_reply %p %p\n", p, end); | ||
| 178 | ceph_decode_need(&p, end, sizeof(u32) * 3 + sizeof(u64), bad); | ||
| 179 | protocol = ceph_decode_32(&p); | ||
| 180 | result = ceph_decode_32(&p); | ||
| 181 | global_id = ceph_decode_64(&p); | ||
| 182 | payload_len = ceph_decode_32(&p); | ||
| 183 | payload = p; | ||
| 184 | p += payload_len; | ||
| 185 | ceph_decode_need(&p, end, sizeof(u32), bad); | ||
| 186 | result_msg_len = ceph_decode_32(&p); | ||
| 187 | result_msg = p; | ||
| 188 | p += result_msg_len; | ||
| 189 | if (p != end) | ||
| 190 | goto bad; | ||
| 191 | |||
| 192 | dout(" result %d '%.*s' gid %llu len %d\n", result, result_msg_len, | ||
| 193 | result_msg, global_id, payload_len); | ||
| 194 | |||
| 195 | payload_end = payload + payload_len; | ||
| 196 | |||
| 197 | if (global_id && ac->global_id != global_id) { | ||
| 198 | dout(" set global_id %lld -> %lld\n", ac->global_id, global_id); | ||
| 199 | ac->global_id = global_id; | ||
| 200 | } | ||
| 201 | |||
| 202 | if (ac->negotiating) { | ||
| 203 | /* server does not support our protocols? */ | ||
| 204 | if (!protocol && result < 0) { | ||
| 205 | ret = result; | ||
| 206 | goto out; | ||
| 207 | } | ||
| 208 | /* set up (new) protocol handler? */ | ||
| 209 | if (ac->protocol && ac->protocol != protocol) { | ||
| 210 | ac->ops->destroy(ac); | ||
| 211 | ac->protocol = 0; | ||
| 212 | ac->ops = NULL; | ||
| 213 | } | ||
| 214 | if (ac->protocol != protocol) { | ||
| 215 | ret = ceph_auth_init_protocol(ac, protocol); | ||
| 216 | if (ret) { | ||
| 217 | pr_err("error %d on auth protocol %d init\n", | ||
| 218 | ret, protocol); | ||
| 219 | goto out; | ||
| 220 | } | ||
| 221 | } | ||
| 222 | |||
| 223 | ac->negotiating = false; | ||
| 224 | } | ||
| 225 | |||
| 226 | ret = ac->ops->handle_reply(ac, result, payload, payload_end); | ||
| 227 | if (ret == -EAGAIN) { | ||
| 228 | return ceph_build_auth_request(ac, reply_buf, reply_len); | ||
| 229 | } else if (ret) { | ||
| 230 | pr_err("authentication error %d\n", ret); | ||
| 231 | return ret; | ||
| 232 | } | ||
| 233 | return 0; | ||
| 234 | |||
| 235 | bad: | ||
| 236 | pr_err("failed to decode auth msg\n"); | ||
| 237 | out: | ||
| 238 | return ret; | ||
| 239 | } | ||
| 240 | |||
| 241 | int ceph_build_auth(struct ceph_auth_client *ac, | ||
| 242 | void *msg_buf, size_t msg_len) | ||
| 243 | { | ||
| 244 | if (!ac->protocol) | ||
| 245 | return ceph_auth_build_hello(ac, msg_buf, msg_len); | ||
| 246 | BUG_ON(!ac->ops); | ||
| 247 | if (!ac->ops->is_authenticated(ac)) | ||
| 248 | return ceph_build_auth_request(ac, msg_buf, msg_len); | ||
| 249 | return 0; | ||
| 250 | } | ||
| 251 | |||
| 252 | int ceph_auth_is_authenticated(struct ceph_auth_client *ac) | ||
| 253 | { | ||
| 254 | if (!ac->ops) | ||
| 255 | return 0; | ||
| 256 | return ac->ops->is_authenticated(ac); | ||
| 257 | } | ||
diff --git a/fs/ceph/auth.h b/fs/ceph/auth.h new file mode 100644 index 000000000000..ca4f57cfb267 --- /dev/null +++ b/fs/ceph/auth.h | |||
| @@ -0,0 +1,84 @@ | |||
| 1 | #ifndef _FS_CEPH_AUTH_H | ||
| 2 | #define _FS_CEPH_AUTH_H | ||
| 3 | |||
| 4 | #include "types.h" | ||
| 5 | #include "buffer.h" | ||
| 6 | |||
| 7 | /* | ||
| 8 | * Abstract interface for communicating with the authenticate module. | ||
| 9 | * There is some handshake that takes place between us and the monitor | ||
| 10 | * to acquire the necessary keys. These are used to generate an | ||
| 11 | * 'authorizer' that we use when connecting to a service (mds, osd). | ||
| 12 | */ | ||
| 13 | |||
| 14 | struct ceph_auth_client; | ||
| 15 | struct ceph_authorizer; | ||
| 16 | |||
| 17 | struct ceph_auth_client_ops { | ||
| 18 | /* | ||
| 19 | * true if we are authenticated and can connect to | ||
| 20 | * services. | ||
| 21 | */ | ||
| 22 | int (*is_authenticated)(struct ceph_auth_client *ac); | ||
| 23 | |||
| 24 | /* | ||
| 25 | * build requests and process replies during monitor | ||
| 26 | * handshake. if handle_reply returns -EAGAIN, we build | ||
| 27 | * another request. | ||
| 28 | */ | ||
| 29 | int (*build_request)(struct ceph_auth_client *ac, void *buf, void *end); | ||
| 30 | int (*handle_reply)(struct ceph_auth_client *ac, int result, | ||
| 31 | void *buf, void *end); | ||
| 32 | |||
| 33 | /* | ||
| 34 | * Create authorizer for connecting to a service, and verify | ||
| 35 | * the response to authenticate the service. | ||
| 36 | */ | ||
| 37 | int (*create_authorizer)(struct ceph_auth_client *ac, int peer_type, | ||
| 38 | struct ceph_authorizer **a, | ||
| 39 | void **buf, size_t *len, | ||
| 40 | void **reply_buf, size_t *reply_len); | ||
| 41 | int (*verify_authorizer_reply)(struct ceph_auth_client *ac, | ||
| 42 | struct ceph_authorizer *a, size_t len); | ||
| 43 | void (*destroy_authorizer)(struct ceph_auth_client *ac, | ||
| 44 | struct ceph_authorizer *a); | ||
| 45 | void (*invalidate_authorizer)(struct ceph_auth_client *ac, | ||
| 46 | int peer_type); | ||
| 47 | |||
| 48 | /* reset when we (re)connect to a monitor */ | ||
| 49 | void (*reset)(struct ceph_auth_client *ac); | ||
| 50 | |||
| 51 | void (*destroy)(struct ceph_auth_client *ac); | ||
| 52 | }; | ||
| 53 | |||
| 54 | struct ceph_auth_client { | ||
| 55 | u32 protocol; /* CEPH_AUTH_* */ | ||
| 56 | void *private; /* for use by protocol implementation */ | ||
| 57 | const struct ceph_auth_client_ops *ops; /* null iff protocol==0 */ | ||
| 58 | |||
| 59 | bool negotiating; /* true if negotiating protocol */ | ||
| 60 | const char *name; /* entity name */ | ||
| 61 | u64 global_id; /* our unique id in system */ | ||
| 62 | const char *secret; /* our secret key */ | ||
| 63 | unsigned want_keys; /* which services we want */ | ||
| 64 | }; | ||
| 65 | |||
| 66 | extern struct ceph_auth_client *ceph_auth_init(const char *name, | ||
| 67 | const char *secret); | ||
| 68 | extern void ceph_auth_destroy(struct ceph_auth_client *ac); | ||
| 69 | |||
| 70 | extern void ceph_auth_reset(struct ceph_auth_client *ac); | ||
| 71 | |||
| 72 | extern int ceph_auth_build_hello(struct ceph_auth_client *ac, | ||
| 73 | void *buf, size_t len); | ||
| 74 | extern int ceph_handle_auth_reply(struct ceph_auth_client *ac, | ||
| 75 | void *buf, size_t len, | ||
| 76 | void *reply_buf, size_t reply_len); | ||
| 77 | extern int ceph_entity_name_encode(const char *name, void **p, void *end); | ||
| 78 | |||
| 79 | extern int ceph_build_auth(struct ceph_auth_client *ac, | ||
| 80 | void *msg_buf, size_t msg_len); | ||
| 81 | |||
| 82 | extern int ceph_auth_is_authenticated(struct ceph_auth_client *ac); | ||
| 83 | |||
| 84 | #endif | ||
diff --git a/fs/ceph/auth_none.c b/fs/ceph/auth_none.c new file mode 100644 index 000000000000..b4ef6f0a6c85 --- /dev/null +++ b/fs/ceph/auth_none.c | |||
| @@ -0,0 +1,121 @@ | |||
| 1 | |||
| 2 | #include "ceph_debug.h" | ||
| 3 | |||
| 4 | #include <linux/err.h> | ||
| 5 | #include <linux/module.h> | ||
| 6 | #include <linux/random.h> | ||
| 7 | |||
| 8 | #include "auth_none.h" | ||
| 9 | #include "auth.h" | ||
| 10 | #include "decode.h" | ||
| 11 | |||
| 12 | static void reset(struct ceph_auth_client *ac) | ||
| 13 | { | ||
| 14 | struct ceph_auth_none_info *xi = ac->private; | ||
| 15 | |||
| 16 | xi->starting = true; | ||
| 17 | xi->built_authorizer = false; | ||
| 18 | } | ||
| 19 | |||
| 20 | static void destroy(struct ceph_auth_client *ac) | ||
| 21 | { | ||
| 22 | kfree(ac->private); | ||
| 23 | ac->private = NULL; | ||
| 24 | } | ||
| 25 | |||
| 26 | static int is_authenticated(struct ceph_auth_client *ac) | ||
| 27 | { | ||
| 28 | struct ceph_auth_none_info *xi = ac->private; | ||
| 29 | |||
| 30 | return !xi->starting; | ||
| 31 | } | ||
| 32 | |||
| 33 | /* | ||
| 34 | * the generic auth code decode the global_id, and we carry no actual | ||
| 35 | * authenticate state, so nothing happens here. | ||
| 36 | */ | ||
| 37 | static int handle_reply(struct ceph_auth_client *ac, int result, | ||
| 38 | void *buf, void *end) | ||
| 39 | { | ||
| 40 | struct ceph_auth_none_info *xi = ac->private; | ||
| 41 | |||
| 42 | xi->starting = false; | ||
| 43 | return result; | ||
| 44 | } | ||
| 45 | |||
| 46 | /* | ||
| 47 | * build an 'authorizer' with our entity_name and global_id. we can | ||
| 48 | * reuse a single static copy since it is identical for all services | ||
| 49 | * we connect to. | ||
| 50 | */ | ||
| 51 | static int ceph_auth_none_create_authorizer( | ||
| 52 | struct ceph_auth_client *ac, int peer_type, | ||
| 53 | struct ceph_authorizer **a, | ||
| 54 | void **buf, size_t *len, | ||
| 55 | void **reply_buf, size_t *reply_len) | ||
| 56 | { | ||
| 57 | struct ceph_auth_none_info *ai = ac->private; | ||
| 58 | struct ceph_none_authorizer *au = &ai->au; | ||
| 59 | void *p, *end; | ||
| 60 | int ret; | ||
| 61 | |||
| 62 | if (!ai->built_authorizer) { | ||
| 63 | p = au->buf; | ||
| 64 | end = p + sizeof(au->buf); | ||
| 65 | ceph_encode_8(&p, 1); | ||
| 66 | ret = ceph_entity_name_encode(ac->name, &p, end - 8); | ||
| 67 | if (ret < 0) | ||
| 68 | goto bad; | ||
| 69 | ceph_decode_need(&p, end, sizeof(u64), bad2); | ||
| 70 | ceph_encode_64(&p, ac->global_id); | ||
| 71 | au->buf_len = p - (void *)au->buf; | ||
| 72 | ai->built_authorizer = true; | ||
| 73 | dout("built authorizer len %d\n", au->buf_len); | ||
| 74 | } | ||
| 75 | |||
| 76 | *a = (struct ceph_authorizer *)au; | ||
| 77 | *buf = au->buf; | ||
| 78 | *len = au->buf_len; | ||
| 79 | *reply_buf = au->reply_buf; | ||
| 80 | *reply_len = sizeof(au->reply_buf); | ||
| 81 | return 0; | ||
| 82 | |||
| 83 | bad2: | ||
| 84 | ret = -ERANGE; | ||
| 85 | bad: | ||
| 86 | return ret; | ||
| 87 | } | ||
| 88 | |||
| 89 | static void ceph_auth_none_destroy_authorizer(struct ceph_auth_client *ac, | ||
| 90 | struct ceph_authorizer *a) | ||
| 91 | { | ||
| 92 | /* nothing to do */ | ||
| 93 | } | ||
| 94 | |||
| 95 | static const struct ceph_auth_client_ops ceph_auth_none_ops = { | ||
| 96 | .reset = reset, | ||
| 97 | .destroy = destroy, | ||
| 98 | .is_authenticated = is_authenticated, | ||
| 99 | .handle_reply = handle_reply, | ||
| 100 | .create_authorizer = ceph_auth_none_create_authorizer, | ||
| 101 | .destroy_authorizer = ceph_auth_none_destroy_authorizer, | ||
| 102 | }; | ||
| 103 | |||
| 104 | int ceph_auth_none_init(struct ceph_auth_client *ac) | ||
| 105 | { | ||
| 106 | struct ceph_auth_none_info *xi; | ||
| 107 | |||
| 108 | dout("ceph_auth_none_init %p\n", ac); | ||
| 109 | xi = kzalloc(sizeof(*xi), GFP_NOFS); | ||
| 110 | if (!xi) | ||
| 111 | return -ENOMEM; | ||
| 112 | |||
| 113 | xi->starting = true; | ||
| 114 | xi->built_authorizer = false; | ||
| 115 | |||
| 116 | ac->protocol = CEPH_AUTH_NONE; | ||
| 117 | ac->private = xi; | ||
| 118 | ac->ops = &ceph_auth_none_ops; | ||
| 119 | return 0; | ||
| 120 | } | ||
| 121 | |||
diff --git a/fs/ceph/auth_none.h b/fs/ceph/auth_none.h new file mode 100644 index 000000000000..56c05533a31c --- /dev/null +++ b/fs/ceph/auth_none.h | |||
| @@ -0,0 +1,28 @@ | |||
| 1 | #ifndef _FS_CEPH_AUTH_NONE_H | ||
| 2 | #define _FS_CEPH_AUTH_NONE_H | ||
| 3 | |||
| 4 | #include "auth.h" | ||
| 5 | |||
| 6 | /* | ||
| 7 | * null security mode. | ||
| 8 | * | ||
| 9 | * we use a single static authorizer that simply encodes our entity name | ||
| 10 | * and global id. | ||
| 11 | */ | ||
| 12 | |||
| 13 | struct ceph_none_authorizer { | ||
| 14 | char buf[128]; | ||
| 15 | int buf_len; | ||
| 16 | char reply_buf[0]; | ||
| 17 | }; | ||
| 18 | |||
| 19 | struct ceph_auth_none_info { | ||
| 20 | bool starting; | ||
| 21 | bool built_authorizer; | ||
| 22 | struct ceph_none_authorizer au; /* we only need one; it's static */ | ||
| 23 | }; | ||
| 24 | |||
| 25 | extern int ceph_auth_none_init(struct ceph_auth_client *ac); | ||
| 26 | |||
| 27 | #endif | ||
| 28 | |||
diff --git a/fs/ceph/auth_x.c b/fs/ceph/auth_x.c new file mode 100644 index 000000000000..8d8a84964763 --- /dev/null +++ b/fs/ceph/auth_x.c | |||
| @@ -0,0 +1,679 @@ | |||
| 1 | |||
| 2 | #include "ceph_debug.h" | ||
| 3 | |||
| 4 | #include <linux/err.h> | ||
| 5 | #include <linux/module.h> | ||
| 6 | #include <linux/random.h> | ||
| 7 | |||
| 8 | #include "auth_x.h" | ||
| 9 | #include "auth_x_protocol.h" | ||
| 10 | #include "crypto.h" | ||
| 11 | #include "auth.h" | ||
| 12 | #include "decode.h" | ||
| 13 | |||
| 14 | struct kmem_cache *ceph_x_ticketbuf_cachep; | ||
| 15 | |||
| 16 | #define TEMP_TICKET_BUF_LEN 256 | ||
| 17 | |||
| 18 | static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed); | ||
| 19 | |||
| 20 | static int ceph_x_is_authenticated(struct ceph_auth_client *ac) | ||
| 21 | { | ||
| 22 | struct ceph_x_info *xi = ac->private; | ||
| 23 | int need; | ||
| 24 | |||
| 25 | ceph_x_validate_tickets(ac, &need); | ||
| 26 | dout("ceph_x_is_authenticated want=%d need=%d have=%d\n", | ||
| 27 | ac->want_keys, need, xi->have_keys); | ||
| 28 | return (ac->want_keys & xi->have_keys) == ac->want_keys; | ||
| 29 | } | ||
| 30 | |||
| 31 | static int ceph_x_encrypt_buflen(int ilen) | ||
| 32 | { | ||
| 33 | return sizeof(struct ceph_x_encrypt_header) + ilen + 16 + | ||
| 34 | sizeof(u32); | ||
| 35 | } | ||
| 36 | |||
| 37 | static int ceph_x_encrypt(struct ceph_crypto_key *secret, | ||
| 38 | void *ibuf, int ilen, void *obuf, size_t olen) | ||
| 39 | { | ||
| 40 | struct ceph_x_encrypt_header head = { | ||
| 41 | .struct_v = 1, | ||
| 42 | .magic = cpu_to_le64(CEPHX_ENC_MAGIC) | ||
| 43 | }; | ||
| 44 | size_t len = olen - sizeof(u32); | ||
| 45 | int ret; | ||
| 46 | |||
| 47 | ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len, | ||
| 48 | &head, sizeof(head), ibuf, ilen); | ||
| 49 | if (ret) | ||
| 50 | return ret; | ||
| 51 | ceph_encode_32(&obuf, len); | ||
| 52 | return len + sizeof(u32); | ||
| 53 | } | ||
| 54 | |||
| 55 | static int ceph_x_decrypt(struct ceph_crypto_key *secret, | ||
| 56 | void **p, void *end, void *obuf, size_t olen) | ||
| 57 | { | ||
| 58 | struct ceph_x_encrypt_header head; | ||
| 59 | size_t head_len = sizeof(head); | ||
| 60 | int len, ret; | ||
| 61 | |||
| 62 | len = ceph_decode_32(p); | ||
| 63 | if (*p + len > end) | ||
| 64 | return -EINVAL; | ||
| 65 | |||
| 66 | dout("ceph_x_decrypt len %d\n", len); | ||
| 67 | ret = ceph_decrypt2(secret, &head, &head_len, obuf, &olen, | ||
| 68 | *p, len); | ||
| 69 | if (ret) | ||
| 70 | return ret; | ||
| 71 | if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC) | ||
| 72 | return -EPERM; | ||
| 73 | *p += len; | ||
| 74 | return olen; | ||
| 75 | } | ||
| 76 | |||
| 77 | /* | ||
| 78 | * get existing (or insert new) ticket handler | ||
| 79 | */ | ||
| 80 | struct ceph_x_ticket_handler *get_ticket_handler(struct ceph_auth_client *ac, | ||
| 81 | int service) | ||
| 82 | { | ||
| 83 | struct ceph_x_ticket_handler *th; | ||
| 84 | struct ceph_x_info *xi = ac->private; | ||
| 85 | struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node; | ||
| 86 | |||
| 87 | while (*p) { | ||
| 88 | parent = *p; | ||
| 89 | th = rb_entry(parent, struct ceph_x_ticket_handler, node); | ||
| 90 | if (service < th->service) | ||
| 91 | p = &(*p)->rb_left; | ||
| 92 | else if (service > th->service) | ||
| 93 | p = &(*p)->rb_right; | ||
| 94 | else | ||
| 95 | return th; | ||
| 96 | } | ||
| 97 | |||
| 98 | /* add it */ | ||
| 99 | th = kzalloc(sizeof(*th), GFP_NOFS); | ||
| 100 | if (!th) | ||
| 101 | return ERR_PTR(-ENOMEM); | ||
| 102 | th->service = service; | ||
| 103 | rb_link_node(&th->node, parent, p); | ||
| 104 | rb_insert_color(&th->node, &xi->ticket_handlers); | ||
| 105 | return th; | ||
| 106 | } | ||
| 107 | |||
| 108 | static void remove_ticket_handler(struct ceph_auth_client *ac, | ||
| 109 | struct ceph_x_ticket_handler *th) | ||
| 110 | { | ||
| 111 | struct ceph_x_info *xi = ac->private; | ||
| 112 | |||
| 113 | dout("remove_ticket_handler %p %d\n", th, th->service); | ||
| 114 | rb_erase(&th->node, &xi->ticket_handlers); | ||
| 115 | ceph_crypto_key_destroy(&th->session_key); | ||
| 116 | if (th->ticket_blob) | ||
| 117 | ceph_buffer_put(th->ticket_blob); | ||
| 118 | kfree(th); | ||
| 119 | } | ||
| 120 | |||
| 121 | static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac, | ||
| 122 | struct ceph_crypto_key *secret, | ||
| 123 | void *buf, void *end) | ||
| 124 | { | ||
| 125 | struct ceph_x_info *xi = ac->private; | ||
| 126 | int num; | ||
| 127 | void *p = buf; | ||
| 128 | int ret; | ||
| 129 | char *dbuf; | ||
| 130 | char *ticket_buf; | ||
| 131 | u8 struct_v; | ||
| 132 | |||
| 133 | dbuf = kmem_cache_alloc(ceph_x_ticketbuf_cachep, GFP_NOFS | GFP_ATOMIC); | ||
| 134 | if (!dbuf) | ||
| 135 | return -ENOMEM; | ||
| 136 | |||
| 137 | ret = -ENOMEM; | ||
| 138 | ticket_buf = kmem_cache_alloc(ceph_x_ticketbuf_cachep, | ||
| 139 | GFP_NOFS | GFP_ATOMIC); | ||
| 140 | if (!ticket_buf) | ||
| 141 | goto out_dbuf; | ||
| 142 | |||
| 143 | ceph_decode_need(&p, end, 1 + sizeof(u32), bad); | ||
| 144 | struct_v = ceph_decode_8(&p); | ||
| 145 | if (struct_v != 1) | ||
| 146 | goto bad; | ||
| 147 | num = ceph_decode_32(&p); | ||
| 148 | dout("%d tickets\n", num); | ||
| 149 | while (num--) { | ||
| 150 | int type; | ||
| 151 | u8 struct_v; | ||
| 152 | struct ceph_x_ticket_handler *th; | ||
| 153 | void *dp, *dend; | ||
| 154 | int dlen; | ||
| 155 | char is_enc; | ||
| 156 | struct timespec validity; | ||
| 157 | struct ceph_crypto_key old_key; | ||
| 158 | void *tp, *tpend; | ||
| 159 | struct ceph_timespec new_validity; | ||
| 160 | struct ceph_crypto_key new_session_key; | ||
| 161 | struct ceph_buffer *new_ticket_blob; | ||
| 162 | unsigned long new_expires, new_renew_after; | ||
| 163 | u64 new_secret_id; | ||
| 164 | |||
| 165 | ceph_decode_need(&p, end, sizeof(u32) + 1, bad); | ||
| 166 | |||
| 167 | type = ceph_decode_32(&p); | ||
| 168 | dout(" ticket type %d %s\n", type, ceph_entity_type_name(type)); | ||
| 169 | |||
| 170 | struct_v = ceph_decode_8(&p); | ||
| 171 | if (struct_v != 1) | ||
| 172 | goto bad; | ||
| 173 | |||
| 174 | th = get_ticket_handler(ac, type); | ||
| 175 | if (IS_ERR(th)) { | ||
| 176 | ret = PTR_ERR(th); | ||
| 177 | goto out; | ||
| 178 | } | ||
| 179 | |||
| 180 | /* blob for me */ | ||
| 181 | dlen = ceph_x_decrypt(secret, &p, end, dbuf, | ||
| 182 | TEMP_TICKET_BUF_LEN); | ||
| 183 | if (dlen <= 0) { | ||
| 184 | ret = dlen; | ||
| 185 | goto out; | ||
| 186 | } | ||
| 187 | dout(" decrypted %d bytes\n", dlen); | ||
| 188 | dend = dbuf + dlen; | ||
| 189 | dp = dbuf; | ||
| 190 | |||
| 191 | struct_v = ceph_decode_8(&dp); | ||
| 192 | if (struct_v != 1) | ||
| 193 | goto bad; | ||
| 194 | |||
| 195 | memcpy(&old_key, &th->session_key, sizeof(old_key)); | ||
| 196 | ret = ceph_crypto_key_decode(&new_session_key, &dp, dend); | ||
| 197 | if (ret) | ||
| 198 | goto out; | ||
| 199 | |||
| 200 | ceph_decode_copy(&dp, &new_validity, sizeof(new_validity)); | ||
| 201 | ceph_decode_timespec(&validity, &new_validity); | ||
| 202 | new_expires = get_seconds() + validity.tv_sec; | ||
| 203 | new_renew_after = new_expires - (validity.tv_sec / 4); | ||
| 204 | dout(" expires=%lu renew_after=%lu\n", new_expires, | ||
| 205 | new_renew_after); | ||
| 206 | |||
| 207 | /* ticket blob for service */ | ||
| 208 | ceph_decode_8_safe(&p, end, is_enc, bad); | ||
| 209 | tp = ticket_buf; | ||
| 210 | if (is_enc) { | ||
| 211 | /* encrypted */ | ||
| 212 | dout(" encrypted ticket\n"); | ||
| 213 | dlen = ceph_x_decrypt(&old_key, &p, end, ticket_buf, | ||
| 214 | TEMP_TICKET_BUF_LEN); | ||
| 215 | if (dlen < 0) { | ||
| 216 | ret = dlen; | ||
| 217 | goto out; | ||
| 218 | } | ||
| 219 | dlen = ceph_decode_32(&tp); | ||
| 220 | } else { | ||
| 221 | /* unencrypted */ | ||
| 222 | ceph_decode_32_safe(&p, end, dlen, bad); | ||
| 223 | ceph_decode_need(&p, end, dlen, bad); | ||
| 224 | ceph_decode_copy(&p, ticket_buf, dlen); | ||
| 225 | } | ||
| 226 | tpend = tp + dlen; | ||
| 227 | dout(" ticket blob is %d bytes\n", dlen); | ||
| 228 | ceph_decode_need(&tp, tpend, 1 + sizeof(u64), bad); | ||
| 229 | struct_v = ceph_decode_8(&tp); | ||
| 230 | new_secret_id = ceph_decode_64(&tp); | ||
| 231 | ret = ceph_decode_buffer(&new_ticket_blob, &tp, tpend); | ||
| 232 | if (ret) | ||
| 233 | goto out; | ||
| 234 | |||
| 235 | /* all is well, update our ticket */ | ||
| 236 | ceph_crypto_key_destroy(&th->session_key); | ||
| 237 | if (th->ticket_blob) | ||
| 238 | ceph_buffer_put(th->ticket_blob); | ||
| 239 | th->session_key = new_session_key; | ||
| 240 | th->ticket_blob = new_ticket_blob; | ||
| 241 | th->validity = new_validity; | ||
| 242 | th->secret_id = new_secret_id; | ||
| 243 | th->expires = new_expires; | ||
| 244 | th->renew_after = new_renew_after; | ||
| 245 | dout(" got ticket service %d (%s) secret_id %lld len %d\n", | ||
| 246 | type, ceph_entity_type_name(type), th->secret_id, | ||
| 247 | (int)th->ticket_blob->vec.iov_len); | ||
| 248 | xi->have_keys |= th->service; | ||
| 249 | } | ||
| 250 | |||
| 251 | ret = 0; | ||
| 252 | out: | ||
| 253 | kmem_cache_free(ceph_x_ticketbuf_cachep, ticket_buf); | ||
| 254 | out_dbuf: | ||
| 255 | kmem_cache_free(ceph_x_ticketbuf_cachep, dbuf); | ||
| 256 | return ret; | ||
| 257 | |||
| 258 | bad: | ||
| 259 | ret = -EINVAL; | ||
| 260 | goto out; | ||
| 261 | } | ||
| 262 | |||
| 263 | static int ceph_x_build_authorizer(struct ceph_auth_client *ac, | ||
| 264 | struct ceph_x_ticket_handler *th, | ||
| 265 | struct ceph_x_authorizer *au) | ||
| 266 | { | ||
| 267 | int maxlen; | ||
| 268 | struct ceph_x_authorize_a *msg_a; | ||
| 269 | struct ceph_x_authorize_b msg_b; | ||
| 270 | void *p, *end; | ||
| 271 | int ret; | ||
| 272 | int ticket_blob_len = | ||
| 273 | (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0); | ||
| 274 | |||
| 275 | dout("build_authorizer for %s %p\n", | ||
| 276 | ceph_entity_type_name(th->service), au); | ||
| 277 | |||
| 278 | maxlen = sizeof(*msg_a) + sizeof(msg_b) + | ||
| 279 | ceph_x_encrypt_buflen(ticket_blob_len); | ||
| 280 | dout(" need len %d\n", maxlen); | ||
| 281 | if (au->buf && au->buf->alloc_len < maxlen) { | ||
| 282 | ceph_buffer_put(au->buf); | ||
| 283 | au->buf = NULL; | ||
| 284 | } | ||
| 285 | if (!au->buf) { | ||
| 286 | au->buf = ceph_buffer_new(maxlen, GFP_NOFS); | ||
| 287 | if (!au->buf) | ||
| 288 | return -ENOMEM; | ||
| 289 | } | ||
| 290 | au->service = th->service; | ||
| 291 | |||
| 292 | msg_a = au->buf->vec.iov_base; | ||
| 293 | msg_a->struct_v = 1; | ||
| 294 | msg_a->global_id = cpu_to_le64(ac->global_id); | ||
| 295 | msg_a->service_id = cpu_to_le32(th->service); | ||
| 296 | msg_a->ticket_blob.struct_v = 1; | ||
| 297 | msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id); | ||
| 298 | msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len); | ||
| 299 | if (ticket_blob_len) { | ||
| 300 | memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base, | ||
| 301 | th->ticket_blob->vec.iov_len); | ||
| 302 | } | ||
| 303 | dout(" th %p secret_id %lld %lld\n", th, th->secret_id, | ||
| 304 | le64_to_cpu(msg_a->ticket_blob.secret_id)); | ||
| 305 | |||
| 306 | p = msg_a + 1; | ||
| 307 | p += ticket_blob_len; | ||
| 308 | end = au->buf->vec.iov_base + au->buf->vec.iov_len; | ||
| 309 | |||
| 310 | get_random_bytes(&au->nonce, sizeof(au->nonce)); | ||
| 311 | msg_b.struct_v = 1; | ||
| 312 | msg_b.nonce = cpu_to_le64(au->nonce); | ||
| 313 | ret = ceph_x_encrypt(&th->session_key, &msg_b, sizeof(msg_b), | ||
| 314 | p, end - p); | ||
| 315 | if (ret < 0) | ||
| 316 | goto out_buf; | ||
| 317 | p += ret; | ||
| 318 | au->buf->vec.iov_len = p - au->buf->vec.iov_base; | ||
| 319 | dout(" built authorizer nonce %llx len %d\n", au->nonce, | ||
| 320 | (int)au->buf->vec.iov_len); | ||
| 321 | BUG_ON(au->buf->vec.iov_len > maxlen); | ||
| 322 | return 0; | ||
| 323 | |||
| 324 | out_buf: | ||
| 325 | ceph_buffer_put(au->buf); | ||
| 326 | au->buf = NULL; | ||
| 327 | return ret; | ||
| 328 | } | ||
| 329 | |||
| 330 | static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th, | ||
| 331 | void **p, void *end) | ||
| 332 | { | ||
| 333 | ceph_decode_need(p, end, 1 + sizeof(u64), bad); | ||
| 334 | ceph_encode_8(p, 1); | ||
| 335 | ceph_encode_64(p, th->secret_id); | ||
| 336 | if (th->ticket_blob) { | ||
| 337 | const char *buf = th->ticket_blob->vec.iov_base; | ||
| 338 | u32 len = th->ticket_blob->vec.iov_len; | ||
| 339 | |||
| 340 | ceph_encode_32_safe(p, end, len, bad); | ||
| 341 | ceph_encode_copy_safe(p, end, buf, len, bad); | ||
| 342 | } else { | ||
| 343 | ceph_encode_32_safe(p, end, 0, bad); | ||
| 344 | } | ||
| 345 | |||
| 346 | return 0; | ||
| 347 | bad: | ||
| 348 | return -ERANGE; | ||
| 349 | } | ||
| 350 | |||
| 351 | static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed) | ||
| 352 | { | ||
| 353 | int want = ac->want_keys; | ||
| 354 | struct ceph_x_info *xi = ac->private; | ||
| 355 | int service; | ||
| 356 | |||
| 357 | *pneed = ac->want_keys & ~(xi->have_keys); | ||
| 358 | |||
| 359 | for (service = 1; service <= want; service <<= 1) { | ||
| 360 | struct ceph_x_ticket_handler *th; | ||
| 361 | |||
| 362 | if (!(ac->want_keys & service)) | ||
| 363 | continue; | ||
| 364 | |||
| 365 | if (*pneed & service) | ||
| 366 | continue; | ||
| 367 | |||
| 368 | th = get_ticket_handler(ac, service); | ||
| 369 | |||
| 370 | if (!th) { | ||
| 371 | *pneed |= service; | ||
| 372 | continue; | ||
| 373 | } | ||
| 374 | |||
| 375 | if (get_seconds() >= th->renew_after) | ||
| 376 | *pneed |= service; | ||
| 377 | if (get_seconds() >= th->expires) | ||
| 378 | xi->have_keys &= ~service; | ||
| 379 | } | ||
| 380 | } | ||
| 381 | |||
| 382 | |||
| 383 | static int ceph_x_build_request(struct ceph_auth_client *ac, | ||
| 384 | void *buf, void *end) | ||
| 385 | { | ||
| 386 | struct ceph_x_info *xi = ac->private; | ||
| 387 | int need; | ||
| 388 | struct ceph_x_request_header *head = buf; | ||
| 389 | int ret; | ||
| 390 | struct ceph_x_ticket_handler *th = | ||
| 391 | get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); | ||
| 392 | |||
| 393 | ceph_x_validate_tickets(ac, &need); | ||
| 394 | |||
| 395 | dout("build_request want %x have %x need %x\n", | ||
| 396 | ac->want_keys, xi->have_keys, need); | ||
| 397 | |||
| 398 | if (need & CEPH_ENTITY_TYPE_AUTH) { | ||
| 399 | struct ceph_x_authenticate *auth = (void *)(head + 1); | ||
| 400 | void *p = auth + 1; | ||
| 401 | struct ceph_x_challenge_blob tmp; | ||
| 402 | char tmp_enc[40]; | ||
| 403 | u64 *u; | ||
| 404 | |||
| 405 | if (p > end) | ||
| 406 | return -ERANGE; | ||
| 407 | |||
| 408 | dout(" get_auth_session_key\n"); | ||
| 409 | head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY); | ||
| 410 | |||
| 411 | /* encrypt and hash */ | ||
| 412 | get_random_bytes(&auth->client_challenge, sizeof(u64)); | ||
| 413 | tmp.client_challenge = auth->client_challenge; | ||
| 414 | tmp.server_challenge = cpu_to_le64(xi->server_challenge); | ||
| 415 | ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp), | ||
| 416 | tmp_enc, sizeof(tmp_enc)); | ||
| 417 | if (ret < 0) | ||
| 418 | return ret; | ||
| 419 | |||
| 420 | auth->struct_v = 1; | ||
| 421 | auth->key = 0; | ||
| 422 | for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++) | ||
| 423 | auth->key ^= *u; | ||
| 424 | dout(" server_challenge %llx client_challenge %llx key %llx\n", | ||
| 425 | xi->server_challenge, le64_to_cpu(auth->client_challenge), | ||
| 426 | le64_to_cpu(auth->key)); | ||
| 427 | |||
| 428 | /* now encode the old ticket if exists */ | ||
| 429 | ret = ceph_x_encode_ticket(th, &p, end); | ||
| 430 | if (ret < 0) | ||
| 431 | return ret; | ||
| 432 | |||
| 433 | return p - buf; | ||
| 434 | } | ||
| 435 | |||
| 436 | if (need) { | ||
| 437 | void *p = head + 1; | ||
| 438 | struct ceph_x_service_ticket_request *req; | ||
| 439 | |||
| 440 | if (p > end) | ||
| 441 | return -ERANGE; | ||
| 442 | head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY); | ||
| 443 | |||
| 444 | BUG_ON(!th); | ||
| 445 | ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer); | ||
| 446 | if (ret) | ||
| 447 | return ret; | ||
| 448 | ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base, | ||
| 449 | xi->auth_authorizer.buf->vec.iov_len); | ||
| 450 | |||
| 451 | req = p; | ||
| 452 | req->keys = cpu_to_le32(need); | ||
| 453 | p += sizeof(*req); | ||
| 454 | return p - buf; | ||
| 455 | } | ||
| 456 | |||
| 457 | return 0; | ||
| 458 | } | ||
| 459 | |||
| 460 | static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result, | ||
| 461 | void *buf, void *end) | ||
| 462 | { | ||
| 463 | struct ceph_x_info *xi = ac->private; | ||
| 464 | struct ceph_x_reply_header *head = buf; | ||
| 465 | struct ceph_x_ticket_handler *th; | ||
| 466 | int len = end - buf; | ||
| 467 | int op; | ||
| 468 | int ret; | ||
| 469 | |||
| 470 | if (result) | ||
| 471 | return result; /* XXX hmm? */ | ||
| 472 | |||
| 473 | if (xi->starting) { | ||
| 474 | /* it's a hello */ | ||
| 475 | struct ceph_x_server_challenge *sc = buf; | ||
| 476 | |||
| 477 | if (len != sizeof(*sc)) | ||
| 478 | return -EINVAL; | ||
| 479 | xi->server_challenge = le64_to_cpu(sc->server_challenge); | ||
| 480 | dout("handle_reply got server challenge %llx\n", | ||
| 481 | xi->server_challenge); | ||
| 482 | xi->starting = false; | ||
| 483 | xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH; | ||
| 484 | return -EAGAIN; | ||
| 485 | } | ||
| 486 | |||
| 487 | op = le32_to_cpu(head->op); | ||
| 488 | result = le32_to_cpu(head->result); | ||
| 489 | dout("handle_reply op %d result %d\n", op, result); | ||
| 490 | switch (op) { | ||
| 491 | case CEPHX_GET_AUTH_SESSION_KEY: | ||
| 492 | /* verify auth key */ | ||
| 493 | ret = ceph_x_proc_ticket_reply(ac, &xi->secret, | ||
| 494 | buf + sizeof(*head), end); | ||
| 495 | break; | ||
| 496 | |||
| 497 | case CEPHX_GET_PRINCIPAL_SESSION_KEY: | ||
| 498 | th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH); | ||
| 499 | BUG_ON(!th); | ||
| 500 | ret = ceph_x_proc_ticket_reply(ac, &th->session_key, | ||
| 501 | buf + sizeof(*head), end); | ||
| 502 | break; | ||
| 503 | |||
| 504 | default: | ||
| 505 | return -EINVAL; | ||
| 506 | } | ||
| 507 | if (ret) | ||
| 508 | return ret; | ||
| 509 | if (ac->want_keys == xi->have_keys) | ||
| 510 | return 0; | ||
| 511 | return -EAGAIN; | ||
| 512 | } | ||
| 513 | |||
| 514 | static int ceph_x_create_authorizer( | ||
| 515 | struct ceph_auth_client *ac, int peer_type, | ||
| 516 | struct ceph_authorizer **a, | ||
| 517 | void **buf, size_t *len, | ||
| 518 | void **reply_buf, size_t *reply_len) | ||
| 519 | { | ||
| 520 | struct ceph_x_authorizer *au; | ||
| 521 | struct ceph_x_ticket_handler *th; | ||
| 522 | int ret; | ||
| 523 | |||
| 524 | th = get_ticket_handler(ac, peer_type); | ||
| 525 | if (IS_ERR(th)) | ||
| 526 | return PTR_ERR(th); | ||
| 527 | |||
| 528 | au = kzalloc(sizeof(*au), GFP_NOFS); | ||
| 529 | if (!au) | ||
| 530 | return -ENOMEM; | ||
| 531 | |||
| 532 | ret = ceph_x_build_authorizer(ac, th, au); | ||
| 533 | if (ret) { | ||
| 534 | kfree(au); | ||
| 535 | return ret; | ||
| 536 | } | ||
| 537 | |||
| 538 | *a = (struct ceph_authorizer *)au; | ||
| 539 | *buf = au->buf->vec.iov_base; | ||
| 540 | *len = au->buf->vec.iov_len; | ||
| 541 | *reply_buf = au->reply_buf; | ||
| 542 | *reply_len = sizeof(au->reply_buf); | ||
| 543 | return 0; | ||
| 544 | } | ||
| 545 | |||
| 546 | static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac, | ||
| 547 | struct ceph_authorizer *a, size_t len) | ||
| 548 | { | ||
| 549 | struct ceph_x_authorizer *au = (void *)a; | ||
| 550 | struct ceph_x_ticket_handler *th; | ||
| 551 | int ret = 0; | ||
| 552 | struct ceph_x_authorize_reply reply; | ||
| 553 | void *p = au->reply_buf; | ||
| 554 | void *end = p + sizeof(au->reply_buf); | ||
| 555 | |||
| 556 | th = get_ticket_handler(ac, au->service); | ||
| 557 | if (!th) | ||
| 558 | return -EIO; /* hrm! */ | ||
| 559 | ret = ceph_x_decrypt(&th->session_key, &p, end, &reply, sizeof(reply)); | ||
| 560 | if (ret < 0) | ||
| 561 | return ret; | ||
| 562 | if (ret != sizeof(reply)) | ||
| 563 | return -EPERM; | ||
| 564 | |||
| 565 | if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one)) | ||
| 566 | ret = -EPERM; | ||
| 567 | else | ||
| 568 | ret = 0; | ||
| 569 | dout("verify_authorizer_reply nonce %llx got %llx ret %d\n", | ||
| 570 | au->nonce, le64_to_cpu(reply.nonce_plus_one), ret); | ||
| 571 | return ret; | ||
| 572 | } | ||
| 573 | |||
| 574 | static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac, | ||
| 575 | struct ceph_authorizer *a) | ||
| 576 | { | ||
| 577 | struct ceph_x_authorizer *au = (void *)a; | ||
| 578 | |||
| 579 | ceph_buffer_put(au->buf); | ||
| 580 | kfree(au); | ||
| 581 | } | ||
| 582 | |||
| 583 | |||
| 584 | static void ceph_x_reset(struct ceph_auth_client *ac) | ||
| 585 | { | ||
| 586 | struct ceph_x_info *xi = ac->private; | ||
| 587 | |||
| 588 | dout("reset\n"); | ||
| 589 | xi->starting = true; | ||
| 590 | xi->server_challenge = 0; | ||
| 591 | } | ||
| 592 | |||
| 593 | static void ceph_x_destroy(struct ceph_auth_client *ac) | ||
| 594 | { | ||
| 595 | struct ceph_x_info *xi = ac->private; | ||
| 596 | struct rb_node *p; | ||
| 597 | |||
| 598 | dout("ceph_x_destroy %p\n", ac); | ||
| 599 | ceph_crypto_key_destroy(&xi->secret); | ||
| 600 | |||
| 601 | while ((p = rb_first(&xi->ticket_handlers)) != NULL) { | ||
| 602 | struct ceph_x_ticket_handler *th = | ||
| 603 | rb_entry(p, struct ceph_x_ticket_handler, node); | ||
| 604 | remove_ticket_handler(ac, th); | ||
| 605 | } | ||
| 606 | |||
| 607 | kmem_cache_destroy(ceph_x_ticketbuf_cachep); | ||
| 608 | |||
| 609 | kfree(ac->private); | ||
| 610 | ac->private = NULL; | ||
| 611 | } | ||
| 612 | |||
| 613 | static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac, | ||
| 614 | int peer_type) | ||
| 615 | { | ||
| 616 | struct ceph_x_ticket_handler *th; | ||
| 617 | |||
| 618 | th = get_ticket_handler(ac, peer_type); | ||
| 619 | if (th && !IS_ERR(th)) | ||
| 620 | remove_ticket_handler(ac, th); | ||
| 621 | } | ||
| 622 | |||
| 623 | |||
| 624 | static const struct ceph_auth_client_ops ceph_x_ops = { | ||
| 625 | .is_authenticated = ceph_x_is_authenticated, | ||
| 626 | .build_request = ceph_x_build_request, | ||
| 627 | .handle_reply = ceph_x_handle_reply, | ||
| 628 | .create_authorizer = ceph_x_create_authorizer, | ||
| 629 | .verify_authorizer_reply = ceph_x_verify_authorizer_reply, | ||
| 630 | .destroy_authorizer = ceph_x_destroy_authorizer, | ||
| 631 | .invalidate_authorizer = ceph_x_invalidate_authorizer, | ||
| 632 | .reset = ceph_x_reset, | ||
| 633 | .destroy = ceph_x_destroy, | ||
| 634 | }; | ||
| 635 | |||
| 636 | |||
| 637 | int ceph_x_init(struct ceph_auth_client *ac) | ||
| 638 | { | ||
| 639 | struct ceph_x_info *xi; | ||
| 640 | int ret; | ||
| 641 | |||
| 642 | dout("ceph_x_init %p\n", ac); | ||
| 643 | xi = kzalloc(sizeof(*xi), GFP_NOFS); | ||
| 644 | if (!xi) | ||
| 645 | return -ENOMEM; | ||
| 646 | |||
| 647 | ret = -ENOMEM; | ||
| 648 | ceph_x_ticketbuf_cachep = kmem_cache_create("ceph_x_ticketbuf", | ||
| 649 | TEMP_TICKET_BUF_LEN, 8, | ||
| 650 | (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD), | ||
| 651 | NULL); | ||
| 652 | if (!ceph_x_ticketbuf_cachep) | ||
| 653 | goto done_nomem; | ||
| 654 | ret = -EINVAL; | ||
| 655 | if (!ac->secret) { | ||
| 656 | pr_err("no secret set (for auth_x protocol)\n"); | ||
| 657 | goto done_nomem; | ||
| 658 | } | ||
| 659 | |||
| 660 | ret = ceph_crypto_key_unarmor(&xi->secret, ac->secret); | ||
| 661 | if (ret) | ||
| 662 | goto done_nomem; | ||
| 663 | |||
| 664 | xi->starting = true; | ||
| 665 | xi->ticket_handlers = RB_ROOT; | ||
| 666 | |||
| 667 | ac->protocol = CEPH_AUTH_CEPHX; | ||
| 668 | ac->private = xi; | ||
| 669 | ac->ops = &ceph_x_ops; | ||
| 670 | return 0; | ||
| 671 | |||
| 672 | done_nomem: | ||
| 673 | kfree(xi); | ||
| 674 | if (ceph_x_ticketbuf_cachep) | ||
| 675 | kmem_cache_destroy(ceph_x_ticketbuf_cachep); | ||
| 676 | return ret; | ||
| 677 | } | ||
| 678 | |||
| 679 | |||
diff --git a/fs/ceph/auth_x.h b/fs/ceph/auth_x.h new file mode 100644 index 000000000000..ff6f8180e681 --- /dev/null +++ b/fs/ceph/auth_x.h | |||
| @@ -0,0 +1,49 @@ | |||
| 1 | #ifndef _FS_CEPH_AUTH_X_H | ||
| 2 | #define _FS_CEPH_AUTH_X_H | ||
| 3 | |||
| 4 | #include <linux/rbtree.h> | ||
| 5 | |||
| 6 | #include "crypto.h" | ||
| 7 | #include "auth.h" | ||
| 8 | #include "auth_x_protocol.h" | ||
| 9 | |||
| 10 | /* | ||
| 11 | * Handle ticket for a single service. | ||
| 12 | */ | ||
| 13 | struct ceph_x_ticket_handler { | ||
| 14 | struct rb_node node; | ||
| 15 | unsigned service; | ||
| 16 | |||
| 17 | struct ceph_crypto_key session_key; | ||
| 18 | struct ceph_timespec validity; | ||
| 19 | |||
| 20 | u64 secret_id; | ||
| 21 | struct ceph_buffer *ticket_blob; | ||
| 22 | |||
| 23 | unsigned long renew_after, expires; | ||
| 24 | }; | ||
| 25 | |||
| 26 | |||
| 27 | struct ceph_x_authorizer { | ||
| 28 | struct ceph_buffer *buf; | ||
| 29 | unsigned service; | ||
| 30 | u64 nonce; | ||
| 31 | char reply_buf[128]; /* big enough for encrypted blob */ | ||
| 32 | }; | ||
| 33 | |||
| 34 | struct ceph_x_info { | ||
| 35 | struct ceph_crypto_key secret; | ||
| 36 | |||
| 37 | bool starting; | ||
| 38 | u64 server_challenge; | ||
| 39 | |||
| 40 | unsigned have_keys; | ||
| 41 | struct rb_root ticket_handlers; | ||
| 42 | |||
| 43 | struct ceph_x_authorizer auth_authorizer; | ||
| 44 | }; | ||
| 45 | |||
| 46 | extern int ceph_x_init(struct ceph_auth_client *ac); | ||
| 47 | |||
| 48 | #endif | ||
| 49 | |||
diff --git a/fs/ceph/auth_x_protocol.h b/fs/ceph/auth_x_protocol.h new file mode 100644 index 000000000000..671d30576c4f --- /dev/null +++ b/fs/ceph/auth_x_protocol.h | |||
| @@ -0,0 +1,90 @@ | |||
| 1 | #ifndef __FS_CEPH_AUTH_X_PROTOCOL | ||
| 2 | #define __FS_CEPH_AUTH_X_PROTOCOL | ||
| 3 | |||
| 4 | #define CEPHX_GET_AUTH_SESSION_KEY 0x0100 | ||
| 5 | #define CEPHX_GET_PRINCIPAL_SESSION_KEY 0x0200 | ||
| 6 | #define CEPHX_GET_ROTATING_KEY 0x0400 | ||
| 7 | |||
| 8 | /* common bits */ | ||
| 9 | struct ceph_x_ticket_blob { | ||
| 10 | __u8 struct_v; | ||
| 11 | __le64 secret_id; | ||
| 12 | __le32 blob_len; | ||
| 13 | char blob[]; | ||
| 14 | } __attribute__ ((packed)); | ||
| 15 | |||
| 16 | |||
| 17 | /* common request/reply headers */ | ||
| 18 | struct ceph_x_request_header { | ||
| 19 | __le16 op; | ||
| 20 | } __attribute__ ((packed)); | ||
| 21 | |||
| 22 | struct ceph_x_reply_header { | ||
| 23 | __le16 op; | ||
| 24 | __le32 result; | ||
| 25 | } __attribute__ ((packed)); | ||
| 26 | |||
| 27 | |||
| 28 | /* authenticate handshake */ | ||
| 29 | |||
| 30 | /* initial hello (no reply header) */ | ||
| 31 | struct ceph_x_server_challenge { | ||
| 32 | __u8 struct_v; | ||
| 33 | __le64 server_challenge; | ||
| 34 | } __attribute__ ((packed)); | ||
| 35 | |||
| 36 | struct ceph_x_authenticate { | ||
| 37 | __u8 struct_v; | ||
| 38 | __le64 client_challenge; | ||
| 39 | __le64 key; | ||
| 40 | /* ticket blob */ | ||
| 41 | } __attribute__ ((packed)); | ||
| 42 | |||
| 43 | struct ceph_x_service_ticket_request { | ||
| 44 | __u8 struct_v; | ||
| 45 | __le32 keys; | ||
| 46 | } __attribute__ ((packed)); | ||
| 47 | |||
| 48 | struct ceph_x_challenge_blob { | ||
| 49 | __le64 server_challenge; | ||
| 50 | __le64 client_challenge; | ||
| 51 | } __attribute__ ((packed)); | ||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | /* authorize handshake */ | ||
| 56 | |||
| 57 | /* | ||
| 58 | * The authorizer consists of two pieces: | ||
| 59 | * a - service id, ticket blob | ||
| 60 | * b - encrypted with session key | ||
| 61 | */ | ||
| 62 | struct ceph_x_authorize_a { | ||
| 63 | __u8 struct_v; | ||
| 64 | __le64 global_id; | ||
| 65 | __le32 service_id; | ||
| 66 | struct ceph_x_ticket_blob ticket_blob; | ||
| 67 | } __attribute__ ((packed)); | ||
| 68 | |||
| 69 | struct ceph_x_authorize_b { | ||
| 70 | __u8 struct_v; | ||
| 71 | __le64 nonce; | ||
| 72 | } __attribute__ ((packed)); | ||
| 73 | |||
| 74 | struct ceph_x_authorize_reply { | ||
| 75 | __u8 struct_v; | ||
| 76 | __le64 nonce_plus_one; | ||
| 77 | } __attribute__ ((packed)); | ||
| 78 | |||
| 79 | |||
| 80 | /* | ||
| 81 | * encyption bundle | ||
| 82 | */ | ||
| 83 | #define CEPHX_ENC_MAGIC 0xff009cad8826aa55ull | ||
| 84 | |||
| 85 | struct ceph_x_encrypt_header { | ||
| 86 | __u8 struct_v; | ||
| 87 | __le64 magic; | ||
| 88 | } __attribute__ ((packed)); | ||
| 89 | |||
| 90 | #endif | ||
diff --git a/fs/ceph/buffer.c b/fs/ceph/buffer.c new file mode 100644 index 000000000000..b98086c7aeba --- /dev/null +++ b/fs/ceph/buffer.c | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | |||
| 2 | #include "ceph_debug.h" | ||
| 3 | #include "buffer.h" | ||
| 4 | #include "decode.h" | ||
| 5 | |||
| 6 | struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp) | ||
| 7 | { | ||
| 8 | struct ceph_buffer *b; | ||
| 9 | |||
| 10 | b = kmalloc(sizeof(*b), gfp); | ||
| 11 | if (!b) | ||
| 12 | return NULL; | ||
| 13 | |||
| 14 | b->vec.iov_base = kmalloc(len, gfp | __GFP_NOWARN); | ||
| 15 | if (b->vec.iov_base) { | ||
| 16 | b->is_vmalloc = false; | ||
| 17 | } else { | ||
| 18 | b->vec.iov_base = __vmalloc(len, gfp, PAGE_KERNEL); | ||
| 19 | if (!b->vec.iov_base) { | ||
| 20 | kfree(b); | ||
| 21 | return NULL; | ||
| 22 | } | ||
| 23 | b->is_vmalloc = true; | ||
| 24 | } | ||
| 25 | |||
| 26 | kref_init(&b->kref); | ||
| 27 | b->alloc_len = len; | ||
| 28 | b->vec.iov_len = len; | ||
| 29 | dout("buffer_new %p\n", b); | ||
| 30 | return b; | ||
| 31 | } | ||
| 32 | |||
| 33 | void ceph_buffer_release(struct kref *kref) | ||
| 34 | { | ||
| 35 | struct ceph_buffer *b = container_of(kref, struct ceph_buffer, kref); | ||
| 36 | |||
| 37 | dout("buffer_release %p\n", b); | ||
| 38 | if (b->vec.iov_base) { | ||
| 39 | if (b->is_vmalloc) | ||
| 40 | vfree(b->vec.iov_base); | ||
| 41 | else | ||
| 42 | kfree(b->vec.iov_base); | ||
| 43 | } | ||
| 44 | kfree(b); | ||
| 45 | } | ||
| 46 | |||
| 47 | int ceph_buffer_alloc(struct ceph_buffer *b, int len, gfp_t gfp) | ||
| 48 | { | ||
| 49 | b->vec.iov_base = kmalloc(len, gfp | __GFP_NOWARN); | ||
| 50 | if (b->vec.iov_base) { | ||
| 51 | b->is_vmalloc = false; | ||
| 52 | } else { | ||
| 53 | b->vec.iov_base = __vmalloc(len, gfp, PAGE_KERNEL); | ||
| 54 | b->is_vmalloc = true; | ||
| 55 | } | ||
| 56 | if (!b->vec.iov_base) | ||
| 57 | return -ENOMEM; | ||
| 58 | b->alloc_len = len; | ||
| 59 | b->vec.iov_len = len; | ||
| 60 | return 0; | ||
| 61 | } | ||
| 62 | |||
| 63 | int ceph_decode_buffer(struct ceph_buffer **b, void **p, void *end) | ||
| 64 | { | ||
| 65 | size_t len; | ||
| 66 | |||
| 67 | ceph_decode_need(p, end, sizeof(u32), bad); | ||
| 68 | len = ceph_decode_32(p); | ||
| 69 | dout("decode_buffer len %d\n", (int)len); | ||
| 70 | ceph_decode_need(p, end, len, bad); | ||
| 71 | *b = ceph_buffer_new(len, GFP_NOFS); | ||
| 72 | if (!*b) | ||
| 73 | return -ENOMEM; | ||
| 74 | ceph_decode_copy(p, (*b)->vec.iov_base, len); | ||
| 75 | return 0; | ||
| 76 | bad: | ||
| 77 | return -EINVAL; | ||
| 78 | } | ||
diff --git a/fs/ceph/buffer.h b/fs/ceph/buffer.h new file mode 100644 index 000000000000..58d19014068f --- /dev/null +++ b/fs/ceph/buffer.h | |||
| @@ -0,0 +1,39 @@ | |||
| 1 | #ifndef __FS_CEPH_BUFFER_H | ||
| 2 | #define __FS_CEPH_BUFFER_H | ||
| 3 | |||
| 4 | #include <linux/kref.h> | ||
| 5 | #include <linux/mm.h> | ||
| 6 | #include <linux/vmalloc.h> | ||
| 7 | #include <linux/types.h> | ||
| 8 | #include <linux/uio.h> | ||
| 9 | |||
| 10 | /* | ||
| 11 | * a simple reference counted buffer. | ||
| 12 | * | ||
| 13 | * use kmalloc for small sizes (<= one page), vmalloc for larger | ||
| 14 | * sizes. | ||
| 15 | */ | ||
| 16 | struct ceph_buffer { | ||
| 17 | struct kref kref; | ||
| 18 | struct kvec vec; | ||
| 19 | size_t alloc_len; | ||
| 20 | bool is_vmalloc; | ||
| 21 | }; | ||
| 22 | |||
| 23 | extern struct ceph_buffer *ceph_buffer_new(size_t len, gfp_t gfp); | ||
| 24 | extern void ceph_buffer_release(struct kref *kref); | ||
| 25 | |||
| 26 | static inline struct ceph_buffer *ceph_buffer_get(struct ceph_buffer *b) | ||
| 27 | { | ||
| 28 | kref_get(&b->kref); | ||
| 29 | return b; | ||
| 30 | } | ||
| 31 | |||
| 32 | static inline void ceph_buffer_put(struct ceph_buffer *b) | ||
| 33 | { | ||
| 34 | kref_put(&b->kref, ceph_buffer_release); | ||
| 35 | } | ||
| 36 | |||
| 37 | extern int ceph_decode_buffer(struct ceph_buffer **b, void **p, void *end); | ||
| 38 | |||
| 39 | #endif | ||
diff --git a/fs/ceph/caps.c b/fs/ceph/caps.c new file mode 100644 index 000000000000..7d0a0d0adc18 --- /dev/null +++ b/fs/ceph/caps.c | |||
| @@ -0,0 +1,2932 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/fs.h> | ||
| 4 | #include <linux/kernel.h> | ||
| 5 | #include <linux/sched.h> | ||
| 6 | #include <linux/vmalloc.h> | ||
| 7 | #include <linux/wait.h> | ||
| 8 | #include <linux/writeback.h> | ||
| 9 | |||
| 10 | #include "super.h" | ||
| 11 | #include "decode.h" | ||
| 12 | #include "messenger.h" | ||
| 13 | |||
| 14 | /* | ||
| 15 | * Capability management | ||
| 16 | * | ||
| 17 | * The Ceph metadata servers control client access to inode metadata | ||
| 18 | * and file data by issuing capabilities, granting clients permission | ||
| 19 | * to read and/or write both inode field and file data to OSDs | ||
| 20 | * (storage nodes). Each capability consists of a set of bits | ||
| 21 | * indicating which operations are allowed. | ||
| 22 | * | ||
| 23 | * If the client holds a *_SHARED cap, the client has a coherent value | ||
| 24 | * that can be safely read from the cached inode. | ||
| 25 | * | ||
| 26 | * In the case of a *_EXCL (exclusive) or FILE_WR capabilities, the | ||
| 27 | * client is allowed to change inode attributes (e.g., file size, | ||
| 28 | * mtime), note its dirty state in the ceph_cap, and asynchronously | ||
| 29 | * flush that metadata change to the MDS. | ||
| 30 | * | ||
| 31 | * In the event of a conflicting operation (perhaps by another | ||
| 32 | * client), the MDS will revoke the conflicting client capabilities. | ||
| 33 | * | ||
| 34 | * In order for a client to cache an inode, it must hold a capability | ||
| 35 | * with at least one MDS server. When inodes are released, release | ||
| 36 | * notifications are batched and periodically sent en masse to the MDS | ||
| 37 | * cluster to release server state. | ||
| 38 | */ | ||
| 39 | |||
| 40 | |||
| 41 | /* | ||
| 42 | * Generate readable cap strings for debugging output. | ||
| 43 | */ | ||
| 44 | #define MAX_CAP_STR 20 | ||
| 45 | static char cap_str[MAX_CAP_STR][40]; | ||
| 46 | static DEFINE_SPINLOCK(cap_str_lock); | ||
| 47 | static int last_cap_str; | ||
| 48 | |||
| 49 | static char *gcap_string(char *s, int c) | ||
| 50 | { | ||
| 51 | if (c & CEPH_CAP_GSHARED) | ||
| 52 | *s++ = 's'; | ||
| 53 | if (c & CEPH_CAP_GEXCL) | ||
| 54 | *s++ = 'x'; | ||
| 55 | if (c & CEPH_CAP_GCACHE) | ||
| 56 | *s++ = 'c'; | ||
| 57 | if (c & CEPH_CAP_GRD) | ||
| 58 | *s++ = 'r'; | ||
| 59 | if (c & CEPH_CAP_GWR) | ||
| 60 | *s++ = 'w'; | ||
| 61 | if (c & CEPH_CAP_GBUFFER) | ||
| 62 | *s++ = 'b'; | ||
| 63 | if (c & CEPH_CAP_GLAZYIO) | ||
| 64 | *s++ = 'l'; | ||
| 65 | return s; | ||
| 66 | } | ||
| 67 | |||
| 68 | const char *ceph_cap_string(int caps) | ||
| 69 | { | ||
| 70 | int i; | ||
| 71 | char *s; | ||
| 72 | int c; | ||
| 73 | |||
| 74 | spin_lock(&cap_str_lock); | ||
| 75 | i = last_cap_str++; | ||
| 76 | if (last_cap_str == MAX_CAP_STR) | ||
| 77 | last_cap_str = 0; | ||
| 78 | spin_unlock(&cap_str_lock); | ||
| 79 | |||
| 80 | s = cap_str[i]; | ||
| 81 | |||
| 82 | if (caps & CEPH_CAP_PIN) | ||
| 83 | *s++ = 'p'; | ||
| 84 | |||
| 85 | c = (caps >> CEPH_CAP_SAUTH) & 3; | ||
| 86 | if (c) { | ||
| 87 | *s++ = 'A'; | ||
| 88 | s = gcap_string(s, c); | ||
| 89 | } | ||
| 90 | |||
| 91 | c = (caps >> CEPH_CAP_SLINK) & 3; | ||
| 92 | if (c) { | ||
| 93 | *s++ = 'L'; | ||
| 94 | s = gcap_string(s, c); | ||
| 95 | } | ||
| 96 | |||
| 97 | c = (caps >> CEPH_CAP_SXATTR) & 3; | ||
| 98 | if (c) { | ||
| 99 | *s++ = 'X'; | ||
| 100 | s = gcap_string(s, c); | ||
| 101 | } | ||
| 102 | |||
| 103 | c = caps >> CEPH_CAP_SFILE; | ||
| 104 | if (c) { | ||
| 105 | *s++ = 'F'; | ||
| 106 | s = gcap_string(s, c); | ||
| 107 | } | ||
| 108 | |||
| 109 | if (s == cap_str[i]) | ||
| 110 | *s++ = '-'; | ||
| 111 | *s = 0; | ||
| 112 | return cap_str[i]; | ||
| 113 | } | ||
| 114 | |||
| 115 | /* | ||
| 116 | * Cap reservations | ||
| 117 | * | ||
| 118 | * Maintain a global pool of preallocated struct ceph_caps, referenced | ||
| 119 | * by struct ceph_caps_reservations. This ensures that we preallocate | ||
| 120 | * memory needed to successfully process an MDS response. (If an MDS | ||
| 121 | * sends us cap information and we fail to process it, we will have | ||
| 122 | * problems due to the client and MDS being out of sync.) | ||
| 123 | * | ||
| 124 | * Reservations are 'owned' by a ceph_cap_reservation context. | ||
| 125 | */ | ||
| 126 | static spinlock_t caps_list_lock; | ||
| 127 | static struct list_head caps_list; /* unused (reserved or unreserved) */ | ||
| 128 | static int caps_total_count; /* total caps allocated */ | ||
| 129 | static int caps_use_count; /* in use */ | ||
| 130 | static int caps_reserve_count; /* unused, reserved */ | ||
| 131 | static int caps_avail_count; /* unused, unreserved */ | ||
| 132 | static int caps_min_count; /* keep at least this many (unreserved) */ | ||
| 133 | |||
| 134 | void __init ceph_caps_init(void) | ||
| 135 | { | ||
| 136 | INIT_LIST_HEAD(&caps_list); | ||
| 137 | spin_lock_init(&caps_list_lock); | ||
| 138 | } | ||
| 139 | |||
| 140 | void ceph_caps_finalize(void) | ||
| 141 | { | ||
| 142 | struct ceph_cap *cap; | ||
| 143 | |||
| 144 | spin_lock(&caps_list_lock); | ||
| 145 | while (!list_empty(&caps_list)) { | ||
| 146 | cap = list_first_entry(&caps_list, struct ceph_cap, caps_item); | ||
| 147 | list_del(&cap->caps_item); | ||
| 148 | kmem_cache_free(ceph_cap_cachep, cap); | ||
| 149 | } | ||
| 150 | caps_total_count = 0; | ||
| 151 | caps_avail_count = 0; | ||
| 152 | caps_use_count = 0; | ||
| 153 | caps_reserve_count = 0; | ||
| 154 | caps_min_count = 0; | ||
| 155 | spin_unlock(&caps_list_lock); | ||
| 156 | } | ||
| 157 | |||
| 158 | void ceph_adjust_min_caps(int delta) | ||
| 159 | { | ||
| 160 | spin_lock(&caps_list_lock); | ||
| 161 | caps_min_count += delta; | ||
| 162 | BUG_ON(caps_min_count < 0); | ||
| 163 | spin_unlock(&caps_list_lock); | ||
| 164 | } | ||
| 165 | |||
| 166 | int ceph_reserve_caps(struct ceph_cap_reservation *ctx, int need) | ||
| 167 | { | ||
| 168 | int i; | ||
| 169 | struct ceph_cap *cap; | ||
| 170 | int have; | ||
| 171 | int alloc = 0; | ||
| 172 | LIST_HEAD(newcaps); | ||
| 173 | int ret = 0; | ||
| 174 | |||
| 175 | dout("reserve caps ctx=%p need=%d\n", ctx, need); | ||
| 176 | |||
| 177 | /* first reserve any caps that are already allocated */ | ||
| 178 | spin_lock(&caps_list_lock); | ||
| 179 | if (caps_avail_count >= need) | ||
| 180 | have = need; | ||
| 181 | else | ||
| 182 | have = caps_avail_count; | ||
| 183 | caps_avail_count -= have; | ||
| 184 | caps_reserve_count += have; | ||
| 185 | BUG_ON(caps_total_count != caps_use_count + caps_reserve_count + | ||
| 186 | caps_avail_count); | ||
| 187 | spin_unlock(&caps_list_lock); | ||
| 188 | |||
| 189 | for (i = have; i < need; i++) { | ||
| 190 | cap = kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); | ||
| 191 | if (!cap) { | ||
| 192 | ret = -ENOMEM; | ||
| 193 | goto out_alloc_count; | ||
| 194 | } | ||
| 195 | list_add(&cap->caps_item, &newcaps); | ||
| 196 | alloc++; | ||
| 197 | } | ||
| 198 | BUG_ON(have + alloc != need); | ||
| 199 | |||
| 200 | spin_lock(&caps_list_lock); | ||
| 201 | caps_total_count += alloc; | ||
| 202 | caps_reserve_count += alloc; | ||
| 203 | list_splice(&newcaps, &caps_list); | ||
| 204 | |||
| 205 | BUG_ON(caps_total_count != caps_use_count + caps_reserve_count + | ||
| 206 | caps_avail_count); | ||
| 207 | spin_unlock(&caps_list_lock); | ||
| 208 | |||
| 209 | ctx->count = need; | ||
| 210 | dout("reserve caps ctx=%p %d = %d used + %d resv + %d avail\n", | ||
| 211 | ctx, caps_total_count, caps_use_count, caps_reserve_count, | ||
| 212 | caps_avail_count); | ||
| 213 | return 0; | ||
| 214 | |||
| 215 | out_alloc_count: | ||
| 216 | /* we didn't manage to reserve as much as we needed */ | ||
| 217 | pr_warning("reserve caps ctx=%p ENOMEM need=%d got=%d\n", | ||
| 218 | ctx, need, have); | ||
| 219 | return ret; | ||
| 220 | } | ||
| 221 | |||
| 222 | int ceph_unreserve_caps(struct ceph_cap_reservation *ctx) | ||
| 223 | { | ||
| 224 | dout("unreserve caps ctx=%p count=%d\n", ctx, ctx->count); | ||
| 225 | if (ctx->count) { | ||
| 226 | spin_lock(&caps_list_lock); | ||
| 227 | BUG_ON(caps_reserve_count < ctx->count); | ||
| 228 | caps_reserve_count -= ctx->count; | ||
| 229 | caps_avail_count += ctx->count; | ||
| 230 | ctx->count = 0; | ||
| 231 | dout("unreserve caps %d = %d used + %d resv + %d avail\n", | ||
| 232 | caps_total_count, caps_use_count, caps_reserve_count, | ||
| 233 | caps_avail_count); | ||
| 234 | BUG_ON(caps_total_count != caps_use_count + caps_reserve_count + | ||
| 235 | caps_avail_count); | ||
| 236 | spin_unlock(&caps_list_lock); | ||
| 237 | } | ||
| 238 | return 0; | ||
| 239 | } | ||
| 240 | |||
| 241 | static struct ceph_cap *get_cap(struct ceph_cap_reservation *ctx) | ||
| 242 | { | ||
| 243 | struct ceph_cap *cap = NULL; | ||
| 244 | |||
| 245 | /* temporary, until we do something about cap import/export */ | ||
| 246 | if (!ctx) | ||
| 247 | return kmem_cache_alloc(ceph_cap_cachep, GFP_NOFS); | ||
| 248 | |||
| 249 | spin_lock(&caps_list_lock); | ||
| 250 | dout("get_cap ctx=%p (%d) %d = %d used + %d resv + %d avail\n", | ||
| 251 | ctx, ctx->count, caps_total_count, caps_use_count, | ||
| 252 | caps_reserve_count, caps_avail_count); | ||
| 253 | BUG_ON(!ctx->count); | ||
| 254 | BUG_ON(ctx->count > caps_reserve_count); | ||
| 255 | BUG_ON(list_empty(&caps_list)); | ||
| 256 | |||
| 257 | ctx->count--; | ||
| 258 | caps_reserve_count--; | ||
| 259 | caps_use_count++; | ||
| 260 | |||
| 261 | cap = list_first_entry(&caps_list, struct ceph_cap, caps_item); | ||
| 262 | list_del(&cap->caps_item); | ||
| 263 | |||
| 264 | BUG_ON(caps_total_count != caps_use_count + caps_reserve_count + | ||
| 265 | caps_avail_count); | ||
| 266 | spin_unlock(&caps_list_lock); | ||
| 267 | return cap; | ||
| 268 | } | ||
| 269 | |||
| 270 | void ceph_put_cap(struct ceph_cap *cap) | ||
| 271 | { | ||
| 272 | spin_lock(&caps_list_lock); | ||
| 273 | dout("put_cap %p %d = %d used + %d resv + %d avail\n", | ||
| 274 | cap, caps_total_count, caps_use_count, | ||
| 275 | caps_reserve_count, caps_avail_count); | ||
| 276 | caps_use_count--; | ||
| 277 | /* | ||
| 278 | * Keep some preallocated caps around (ceph_min_count), to | ||
| 279 | * avoid lots of free/alloc churn. | ||
| 280 | */ | ||
| 281 | if (caps_avail_count >= caps_reserve_count + caps_min_count) { | ||
| 282 | caps_total_count--; | ||
| 283 | kmem_cache_free(ceph_cap_cachep, cap); | ||
| 284 | } else { | ||
| 285 | caps_avail_count++; | ||
| 286 | list_add(&cap->caps_item, &caps_list); | ||
| 287 | } | ||
| 288 | |||
| 289 | BUG_ON(caps_total_count != caps_use_count + caps_reserve_count + | ||
| 290 | caps_avail_count); | ||
| 291 | spin_unlock(&caps_list_lock); | ||
| 292 | } | ||
| 293 | |||
| 294 | void ceph_reservation_status(struct ceph_client *client, | ||
| 295 | int *total, int *avail, int *used, int *reserved, | ||
| 296 | int *min) | ||
| 297 | { | ||
| 298 | if (total) | ||
| 299 | *total = caps_total_count; | ||
| 300 | if (avail) | ||
| 301 | *avail = caps_avail_count; | ||
| 302 | if (used) | ||
| 303 | *used = caps_use_count; | ||
| 304 | if (reserved) | ||
| 305 | *reserved = caps_reserve_count; | ||
| 306 | if (min) | ||
| 307 | *min = caps_min_count; | ||
| 308 | } | ||
| 309 | |||
| 310 | /* | ||
| 311 | * Find ceph_cap for given mds, if any. | ||
| 312 | * | ||
| 313 | * Called with i_lock held. | ||
| 314 | */ | ||
| 315 | static struct ceph_cap *__get_cap_for_mds(struct ceph_inode_info *ci, int mds) | ||
| 316 | { | ||
| 317 | struct ceph_cap *cap; | ||
| 318 | struct rb_node *n = ci->i_caps.rb_node; | ||
| 319 | |||
| 320 | while (n) { | ||
| 321 | cap = rb_entry(n, struct ceph_cap, ci_node); | ||
| 322 | if (mds < cap->mds) | ||
| 323 | n = n->rb_left; | ||
| 324 | else if (mds > cap->mds) | ||
| 325 | n = n->rb_right; | ||
| 326 | else | ||
| 327 | return cap; | ||
| 328 | } | ||
| 329 | return NULL; | ||
| 330 | } | ||
| 331 | |||
| 332 | /* | ||
| 333 | * Return id of any MDS with a cap, preferably FILE_WR|WRBUFFER|EXCL, else | ||
| 334 | * -1. | ||
| 335 | */ | ||
| 336 | static int __ceph_get_cap_mds(struct ceph_inode_info *ci, u32 *mseq) | ||
| 337 | { | ||
| 338 | struct ceph_cap *cap; | ||
| 339 | int mds = -1; | ||
| 340 | struct rb_node *p; | ||
| 341 | |||
| 342 | /* prefer mds with WR|WRBUFFER|EXCL caps */ | ||
| 343 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | ||
| 344 | cap = rb_entry(p, struct ceph_cap, ci_node); | ||
| 345 | mds = cap->mds; | ||
| 346 | if (mseq) | ||
| 347 | *mseq = cap->mseq; | ||
| 348 | if (cap->issued & (CEPH_CAP_FILE_WR | | ||
| 349 | CEPH_CAP_FILE_BUFFER | | ||
| 350 | CEPH_CAP_FILE_EXCL)) | ||
| 351 | break; | ||
| 352 | } | ||
| 353 | return mds; | ||
| 354 | } | ||
| 355 | |||
| 356 | int ceph_get_cap_mds(struct inode *inode) | ||
| 357 | { | ||
| 358 | int mds; | ||
| 359 | spin_lock(&inode->i_lock); | ||
| 360 | mds = __ceph_get_cap_mds(ceph_inode(inode), NULL); | ||
| 361 | spin_unlock(&inode->i_lock); | ||
| 362 | return mds; | ||
| 363 | } | ||
| 364 | |||
| 365 | /* | ||
| 366 | * Called under i_lock. | ||
| 367 | */ | ||
| 368 | static void __insert_cap_node(struct ceph_inode_info *ci, | ||
| 369 | struct ceph_cap *new) | ||
| 370 | { | ||
| 371 | struct rb_node **p = &ci->i_caps.rb_node; | ||
| 372 | struct rb_node *parent = NULL; | ||
| 373 | struct ceph_cap *cap = NULL; | ||
| 374 | |||
| 375 | while (*p) { | ||
| 376 | parent = *p; | ||
| 377 | cap = rb_entry(parent, struct ceph_cap, ci_node); | ||
| 378 | if (new->mds < cap->mds) | ||
| 379 | p = &(*p)->rb_left; | ||
| 380 | else if (new->mds > cap->mds) | ||
| 381 | p = &(*p)->rb_right; | ||
| 382 | else | ||
| 383 | BUG(); | ||
| 384 | } | ||
| 385 | |||
| 386 | rb_link_node(&new->ci_node, parent, p); | ||
| 387 | rb_insert_color(&new->ci_node, &ci->i_caps); | ||
| 388 | } | ||
| 389 | |||
| 390 | /* | ||
| 391 | * (re)set cap hold timeouts, which control the delayed release | ||
| 392 | * of unused caps back to the MDS. Should be called on cap use. | ||
| 393 | */ | ||
| 394 | static void __cap_set_timeouts(struct ceph_mds_client *mdsc, | ||
| 395 | struct ceph_inode_info *ci) | ||
| 396 | { | ||
| 397 | struct ceph_mount_args *ma = mdsc->client->mount_args; | ||
| 398 | |||
| 399 | ci->i_hold_caps_min = round_jiffies(jiffies + | ||
| 400 | ma->caps_wanted_delay_min * HZ); | ||
| 401 | ci->i_hold_caps_max = round_jiffies(jiffies + | ||
| 402 | ma->caps_wanted_delay_max * HZ); | ||
| 403 | dout("__cap_set_timeouts %p min %lu max %lu\n", &ci->vfs_inode, | ||
| 404 | ci->i_hold_caps_min - jiffies, ci->i_hold_caps_max - jiffies); | ||
| 405 | } | ||
| 406 | |||
| 407 | /* | ||
| 408 | * (Re)queue cap at the end of the delayed cap release list. | ||
| 409 | * | ||
| 410 | * If I_FLUSH is set, leave the inode at the front of the list. | ||
| 411 | * | ||
| 412 | * Caller holds i_lock | ||
| 413 | * -> we take mdsc->cap_delay_lock | ||
| 414 | */ | ||
| 415 | static void __cap_delay_requeue(struct ceph_mds_client *mdsc, | ||
| 416 | struct ceph_inode_info *ci) | ||
| 417 | { | ||
| 418 | __cap_set_timeouts(mdsc, ci); | ||
| 419 | dout("__cap_delay_requeue %p flags %d at %lu\n", &ci->vfs_inode, | ||
| 420 | ci->i_ceph_flags, ci->i_hold_caps_max); | ||
| 421 | if (!mdsc->stopping) { | ||
| 422 | spin_lock(&mdsc->cap_delay_lock); | ||
| 423 | if (!list_empty(&ci->i_cap_delay_list)) { | ||
| 424 | if (ci->i_ceph_flags & CEPH_I_FLUSH) | ||
| 425 | goto no_change; | ||
| 426 | list_del_init(&ci->i_cap_delay_list); | ||
| 427 | } | ||
| 428 | list_add_tail(&ci->i_cap_delay_list, &mdsc->cap_delay_list); | ||
| 429 | no_change: | ||
| 430 | spin_unlock(&mdsc->cap_delay_lock); | ||
| 431 | } | ||
| 432 | } | ||
| 433 | |||
| 434 | /* | ||
| 435 | * Queue an inode for immediate writeback. Mark inode with I_FLUSH, | ||
| 436 | * indicating we should send a cap message to flush dirty metadata | ||
| 437 | * asap, and move to the front of the delayed cap list. | ||
| 438 | */ | ||
| 439 | static void __cap_delay_requeue_front(struct ceph_mds_client *mdsc, | ||
| 440 | struct ceph_inode_info *ci) | ||
| 441 | { | ||
| 442 | dout("__cap_delay_requeue_front %p\n", &ci->vfs_inode); | ||
| 443 | spin_lock(&mdsc->cap_delay_lock); | ||
| 444 | ci->i_ceph_flags |= CEPH_I_FLUSH; | ||
| 445 | if (!list_empty(&ci->i_cap_delay_list)) | ||
| 446 | list_del_init(&ci->i_cap_delay_list); | ||
| 447 | list_add(&ci->i_cap_delay_list, &mdsc->cap_delay_list); | ||
| 448 | spin_unlock(&mdsc->cap_delay_lock); | ||
| 449 | } | ||
| 450 | |||
| 451 | /* | ||
| 452 | * Cancel delayed work on cap. | ||
| 453 | * | ||
| 454 | * Caller must hold i_lock. | ||
| 455 | */ | ||
| 456 | static void __cap_delay_cancel(struct ceph_mds_client *mdsc, | ||
| 457 | struct ceph_inode_info *ci) | ||
| 458 | { | ||
| 459 | dout("__cap_delay_cancel %p\n", &ci->vfs_inode); | ||
| 460 | if (list_empty(&ci->i_cap_delay_list)) | ||
| 461 | return; | ||
| 462 | spin_lock(&mdsc->cap_delay_lock); | ||
| 463 | list_del_init(&ci->i_cap_delay_list); | ||
| 464 | spin_unlock(&mdsc->cap_delay_lock); | ||
| 465 | } | ||
| 466 | |||
| 467 | /* | ||
| 468 | * Common issue checks for add_cap, handle_cap_grant. | ||
| 469 | */ | ||
| 470 | static void __check_cap_issue(struct ceph_inode_info *ci, struct ceph_cap *cap, | ||
| 471 | unsigned issued) | ||
| 472 | { | ||
| 473 | unsigned had = __ceph_caps_issued(ci, NULL); | ||
| 474 | |||
| 475 | /* | ||
| 476 | * Each time we receive FILE_CACHE anew, we increment | ||
| 477 | * i_rdcache_gen. | ||
| 478 | */ | ||
| 479 | if ((issued & CEPH_CAP_FILE_CACHE) && | ||
| 480 | (had & CEPH_CAP_FILE_CACHE) == 0) | ||
| 481 | ci->i_rdcache_gen++; | ||
| 482 | |||
| 483 | /* | ||
| 484 | * if we are newly issued FILE_SHARED, clear I_COMPLETE; we | ||
| 485 | * don't know what happened to this directory while we didn't | ||
| 486 | * have the cap. | ||
| 487 | */ | ||
| 488 | if ((issued & CEPH_CAP_FILE_SHARED) && | ||
| 489 | (had & CEPH_CAP_FILE_SHARED) == 0) { | ||
| 490 | ci->i_shared_gen++; | ||
| 491 | if (S_ISDIR(ci->vfs_inode.i_mode)) { | ||
| 492 | dout(" marking %p NOT complete\n", &ci->vfs_inode); | ||
| 493 | ci->i_ceph_flags &= ~CEPH_I_COMPLETE; | ||
| 494 | } | ||
| 495 | } | ||
| 496 | } | ||
| 497 | |||
| 498 | /* | ||
| 499 | * Add a capability under the given MDS session. | ||
| 500 | * | ||
| 501 | * Caller should hold session snap_rwsem (read) and s_mutex. | ||
| 502 | * | ||
| 503 | * @fmode is the open file mode, if we are opening a file, otherwise | ||
| 504 | * it is < 0. (This is so we can atomically add the cap and add an | ||
| 505 | * open file reference to it.) | ||
| 506 | */ | ||
| 507 | int ceph_add_cap(struct inode *inode, | ||
| 508 | struct ceph_mds_session *session, u64 cap_id, | ||
| 509 | int fmode, unsigned issued, unsigned wanted, | ||
| 510 | unsigned seq, unsigned mseq, u64 realmino, int flags, | ||
| 511 | struct ceph_cap_reservation *caps_reservation) | ||
| 512 | { | ||
| 513 | struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; | ||
| 514 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 515 | struct ceph_cap *new_cap = NULL; | ||
| 516 | struct ceph_cap *cap; | ||
| 517 | int mds = session->s_mds; | ||
| 518 | int actual_wanted; | ||
| 519 | |||
| 520 | dout("add_cap %p mds%d cap %llx %s seq %d\n", inode, | ||
| 521 | session->s_mds, cap_id, ceph_cap_string(issued), seq); | ||
| 522 | |||
| 523 | /* | ||
| 524 | * If we are opening the file, include file mode wanted bits | ||
| 525 | * in wanted. | ||
| 526 | */ | ||
| 527 | if (fmode >= 0) | ||
| 528 | wanted |= ceph_caps_for_mode(fmode); | ||
| 529 | |||
| 530 | retry: | ||
| 531 | spin_lock(&inode->i_lock); | ||
| 532 | cap = __get_cap_for_mds(ci, mds); | ||
| 533 | if (!cap) { | ||
| 534 | if (new_cap) { | ||
| 535 | cap = new_cap; | ||
| 536 | new_cap = NULL; | ||
| 537 | } else { | ||
| 538 | spin_unlock(&inode->i_lock); | ||
| 539 | new_cap = get_cap(caps_reservation); | ||
| 540 | if (new_cap == NULL) | ||
| 541 | return -ENOMEM; | ||
| 542 | goto retry; | ||
| 543 | } | ||
| 544 | |||
| 545 | cap->issued = 0; | ||
| 546 | cap->implemented = 0; | ||
| 547 | cap->mds = mds; | ||
| 548 | cap->mds_wanted = 0; | ||
| 549 | |||
| 550 | cap->ci = ci; | ||
| 551 | __insert_cap_node(ci, cap); | ||
| 552 | |||
| 553 | /* clear out old exporting info? (i.e. on cap import) */ | ||
| 554 | if (ci->i_cap_exporting_mds == mds) { | ||
| 555 | ci->i_cap_exporting_issued = 0; | ||
| 556 | ci->i_cap_exporting_mseq = 0; | ||
| 557 | ci->i_cap_exporting_mds = -1; | ||
| 558 | } | ||
| 559 | |||
| 560 | /* add to session cap list */ | ||
| 561 | cap->session = session; | ||
| 562 | spin_lock(&session->s_cap_lock); | ||
| 563 | list_add_tail(&cap->session_caps, &session->s_caps); | ||
| 564 | session->s_nr_caps++; | ||
| 565 | spin_unlock(&session->s_cap_lock); | ||
| 566 | } | ||
| 567 | |||
| 568 | if (!ci->i_snap_realm) { | ||
| 569 | /* | ||
| 570 | * add this inode to the appropriate snap realm | ||
| 571 | */ | ||
| 572 | struct ceph_snap_realm *realm = ceph_lookup_snap_realm(mdsc, | ||
| 573 | realmino); | ||
| 574 | if (realm) { | ||
| 575 | ceph_get_snap_realm(mdsc, realm); | ||
| 576 | spin_lock(&realm->inodes_with_caps_lock); | ||
| 577 | ci->i_snap_realm = realm; | ||
| 578 | list_add(&ci->i_snap_realm_item, | ||
| 579 | &realm->inodes_with_caps); | ||
| 580 | spin_unlock(&realm->inodes_with_caps_lock); | ||
| 581 | } else { | ||
| 582 | pr_err("ceph_add_cap: couldn't find snap realm %llx\n", | ||
| 583 | realmino); | ||
| 584 | } | ||
| 585 | } | ||
| 586 | |||
| 587 | __check_cap_issue(ci, cap, issued); | ||
| 588 | |||
| 589 | /* | ||
| 590 | * If we are issued caps we don't want, or the mds' wanted | ||
| 591 | * value appears to be off, queue a check so we'll release | ||
| 592 | * later and/or update the mds wanted value. | ||
| 593 | */ | ||
| 594 | actual_wanted = __ceph_caps_wanted(ci); | ||
| 595 | if ((wanted & ~actual_wanted) || | ||
| 596 | (issued & ~actual_wanted & CEPH_CAP_ANY_WR)) { | ||
| 597 | dout(" issued %s, mds wanted %s, actual %s, queueing\n", | ||
| 598 | ceph_cap_string(issued), ceph_cap_string(wanted), | ||
| 599 | ceph_cap_string(actual_wanted)); | ||
| 600 | __cap_delay_requeue(mdsc, ci); | ||
| 601 | } | ||
| 602 | |||
| 603 | if (flags & CEPH_CAP_FLAG_AUTH) | ||
| 604 | ci->i_auth_cap = cap; | ||
| 605 | else if (ci->i_auth_cap == cap) | ||
| 606 | ci->i_auth_cap = NULL; | ||
| 607 | |||
| 608 | dout("add_cap inode %p (%llx.%llx) cap %p %s now %s seq %d mds%d\n", | ||
| 609 | inode, ceph_vinop(inode), cap, ceph_cap_string(issued), | ||
| 610 | ceph_cap_string(issued|cap->issued), seq, mds); | ||
| 611 | cap->cap_id = cap_id; | ||
| 612 | cap->issued = issued; | ||
| 613 | cap->implemented |= issued; | ||
| 614 | cap->mds_wanted |= wanted; | ||
| 615 | cap->seq = seq; | ||
| 616 | cap->issue_seq = seq; | ||
| 617 | cap->mseq = mseq; | ||
| 618 | cap->cap_gen = session->s_cap_gen; | ||
| 619 | |||
| 620 | if (fmode >= 0) | ||
| 621 | __ceph_get_fmode(ci, fmode); | ||
| 622 | spin_unlock(&inode->i_lock); | ||
| 623 | wake_up(&ci->i_cap_wq); | ||
| 624 | return 0; | ||
| 625 | } | ||
| 626 | |||
| 627 | /* | ||
| 628 | * Return true if cap has not timed out and belongs to the current | ||
| 629 | * generation of the MDS session (i.e. has not gone 'stale' due to | ||
| 630 | * us losing touch with the mds). | ||
| 631 | */ | ||
| 632 | static int __cap_is_valid(struct ceph_cap *cap) | ||
| 633 | { | ||
| 634 | unsigned long ttl; | ||
| 635 | u32 gen; | ||
| 636 | |||
| 637 | spin_lock(&cap->session->s_cap_lock); | ||
| 638 | gen = cap->session->s_cap_gen; | ||
| 639 | ttl = cap->session->s_cap_ttl; | ||
| 640 | spin_unlock(&cap->session->s_cap_lock); | ||
| 641 | |||
| 642 | if (cap->cap_gen < gen || time_after_eq(jiffies, ttl)) { | ||
| 643 | dout("__cap_is_valid %p cap %p issued %s " | ||
| 644 | "but STALE (gen %u vs %u)\n", &cap->ci->vfs_inode, | ||
| 645 | cap, ceph_cap_string(cap->issued), cap->cap_gen, gen); | ||
| 646 | return 0; | ||
| 647 | } | ||
| 648 | |||
| 649 | return 1; | ||
| 650 | } | ||
| 651 | |||
| 652 | /* | ||
| 653 | * Return set of valid cap bits issued to us. Note that caps time | ||
| 654 | * out, and may be invalidated in bulk if the client session times out | ||
| 655 | * and session->s_cap_gen is bumped. | ||
| 656 | */ | ||
| 657 | int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented) | ||
| 658 | { | ||
| 659 | int have = ci->i_snap_caps | ci->i_cap_exporting_issued; | ||
| 660 | struct ceph_cap *cap; | ||
| 661 | struct rb_node *p; | ||
| 662 | |||
| 663 | if (implemented) | ||
| 664 | *implemented = 0; | ||
| 665 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | ||
| 666 | cap = rb_entry(p, struct ceph_cap, ci_node); | ||
| 667 | if (!__cap_is_valid(cap)) | ||
| 668 | continue; | ||
| 669 | dout("__ceph_caps_issued %p cap %p issued %s\n", | ||
| 670 | &ci->vfs_inode, cap, ceph_cap_string(cap->issued)); | ||
| 671 | have |= cap->issued; | ||
| 672 | if (implemented) | ||
| 673 | *implemented |= cap->implemented; | ||
| 674 | } | ||
| 675 | return have; | ||
| 676 | } | ||
| 677 | |||
| 678 | /* | ||
| 679 | * Get cap bits issued by caps other than @ocap | ||
| 680 | */ | ||
| 681 | int __ceph_caps_issued_other(struct ceph_inode_info *ci, struct ceph_cap *ocap) | ||
| 682 | { | ||
| 683 | int have = ci->i_snap_caps; | ||
| 684 | struct ceph_cap *cap; | ||
| 685 | struct rb_node *p; | ||
| 686 | |||
| 687 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | ||
| 688 | cap = rb_entry(p, struct ceph_cap, ci_node); | ||
| 689 | if (cap == ocap) | ||
| 690 | continue; | ||
| 691 | if (!__cap_is_valid(cap)) | ||
| 692 | continue; | ||
| 693 | have |= cap->issued; | ||
| 694 | } | ||
| 695 | return have; | ||
| 696 | } | ||
| 697 | |||
| 698 | /* | ||
| 699 | * Move a cap to the end of the LRU (oldest caps at list head, newest | ||
| 700 | * at list tail). | ||
| 701 | */ | ||
| 702 | static void __touch_cap(struct ceph_cap *cap) | ||
| 703 | { | ||
| 704 | struct ceph_mds_session *s = cap->session; | ||
| 705 | |||
| 706 | spin_lock(&s->s_cap_lock); | ||
| 707 | if (s->s_cap_iterator == NULL) { | ||
| 708 | dout("__touch_cap %p cap %p mds%d\n", &cap->ci->vfs_inode, cap, | ||
| 709 | s->s_mds); | ||
| 710 | list_move_tail(&cap->session_caps, &s->s_caps); | ||
| 711 | } else { | ||
| 712 | dout("__touch_cap %p cap %p mds%d NOP, iterating over caps\n", | ||
| 713 | &cap->ci->vfs_inode, cap, s->s_mds); | ||
| 714 | } | ||
| 715 | spin_unlock(&s->s_cap_lock); | ||
| 716 | } | ||
| 717 | |||
| 718 | /* | ||
| 719 | * Check if we hold the given mask. If so, move the cap(s) to the | ||
| 720 | * front of their respective LRUs. (This is the preferred way for | ||
| 721 | * callers to check for caps they want.) | ||
| 722 | */ | ||
| 723 | int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int touch) | ||
| 724 | { | ||
| 725 | struct ceph_cap *cap; | ||
| 726 | struct rb_node *p; | ||
| 727 | int have = ci->i_snap_caps; | ||
| 728 | |||
| 729 | if ((have & mask) == mask) { | ||
| 730 | dout("__ceph_caps_issued_mask %p snap issued %s" | ||
| 731 | " (mask %s)\n", &ci->vfs_inode, | ||
| 732 | ceph_cap_string(have), | ||
| 733 | ceph_cap_string(mask)); | ||
| 734 | return 1; | ||
| 735 | } | ||
| 736 | |||
| 737 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | ||
| 738 | cap = rb_entry(p, struct ceph_cap, ci_node); | ||
| 739 | if (!__cap_is_valid(cap)) | ||
| 740 | continue; | ||
| 741 | if ((cap->issued & mask) == mask) { | ||
| 742 | dout("__ceph_caps_issued_mask %p cap %p issued %s" | ||
| 743 | " (mask %s)\n", &ci->vfs_inode, cap, | ||
| 744 | ceph_cap_string(cap->issued), | ||
| 745 | ceph_cap_string(mask)); | ||
| 746 | if (touch) | ||
| 747 | __touch_cap(cap); | ||
| 748 | return 1; | ||
| 749 | } | ||
| 750 | |||
| 751 | /* does a combination of caps satisfy mask? */ | ||
| 752 | have |= cap->issued; | ||
| 753 | if ((have & mask) == mask) { | ||
| 754 | dout("__ceph_caps_issued_mask %p combo issued %s" | ||
| 755 | " (mask %s)\n", &ci->vfs_inode, | ||
| 756 | ceph_cap_string(cap->issued), | ||
| 757 | ceph_cap_string(mask)); | ||
| 758 | if (touch) { | ||
| 759 | struct rb_node *q; | ||
| 760 | |||
| 761 | /* touch this + preceeding caps */ | ||
| 762 | __touch_cap(cap); | ||
| 763 | for (q = rb_first(&ci->i_caps); q != p; | ||
| 764 | q = rb_next(q)) { | ||
| 765 | cap = rb_entry(q, struct ceph_cap, | ||
| 766 | ci_node); | ||
| 767 | if (!__cap_is_valid(cap)) | ||
| 768 | continue; | ||
| 769 | __touch_cap(cap); | ||
| 770 | } | ||
| 771 | } | ||
| 772 | return 1; | ||
| 773 | } | ||
| 774 | } | ||
| 775 | |||
| 776 | return 0; | ||
| 777 | } | ||
| 778 | |||
| 779 | /* | ||
| 780 | * Return true if mask caps are currently being revoked by an MDS. | ||
| 781 | */ | ||
| 782 | int ceph_caps_revoking(struct ceph_inode_info *ci, int mask) | ||
| 783 | { | ||
| 784 | struct inode *inode = &ci->vfs_inode; | ||
| 785 | struct ceph_cap *cap; | ||
| 786 | struct rb_node *p; | ||
| 787 | int ret = 0; | ||
| 788 | |||
| 789 | spin_lock(&inode->i_lock); | ||
| 790 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | ||
| 791 | cap = rb_entry(p, struct ceph_cap, ci_node); | ||
| 792 | if (__cap_is_valid(cap) && | ||
| 793 | (cap->implemented & ~cap->issued & mask)) { | ||
| 794 | ret = 1; | ||
| 795 | break; | ||
| 796 | } | ||
| 797 | } | ||
| 798 | spin_unlock(&inode->i_lock); | ||
| 799 | dout("ceph_caps_revoking %p %s = %d\n", inode, | ||
| 800 | ceph_cap_string(mask), ret); | ||
| 801 | return ret; | ||
| 802 | } | ||
| 803 | |||
| 804 | int __ceph_caps_used(struct ceph_inode_info *ci) | ||
| 805 | { | ||
| 806 | int used = 0; | ||
| 807 | if (ci->i_pin_ref) | ||
| 808 | used |= CEPH_CAP_PIN; | ||
| 809 | if (ci->i_rd_ref) | ||
| 810 | used |= CEPH_CAP_FILE_RD; | ||
| 811 | if (ci->i_rdcache_ref || ci->i_rdcache_gen) | ||
| 812 | used |= CEPH_CAP_FILE_CACHE; | ||
| 813 | if (ci->i_wr_ref) | ||
| 814 | used |= CEPH_CAP_FILE_WR; | ||
| 815 | if (ci->i_wrbuffer_ref) | ||
| 816 | used |= CEPH_CAP_FILE_BUFFER; | ||
| 817 | return used; | ||
| 818 | } | ||
| 819 | |||
| 820 | /* | ||
| 821 | * wanted, by virtue of open file modes | ||
| 822 | */ | ||
| 823 | int __ceph_caps_file_wanted(struct ceph_inode_info *ci) | ||
| 824 | { | ||
| 825 | int want = 0; | ||
| 826 | int mode; | ||
| 827 | for (mode = 0; mode < 4; mode++) | ||
| 828 | if (ci->i_nr_by_mode[mode]) | ||
| 829 | want |= ceph_caps_for_mode(mode); | ||
| 830 | return want; | ||
| 831 | } | ||
| 832 | |||
| 833 | /* | ||
| 834 | * Return caps we have registered with the MDS(s) as 'wanted'. | ||
| 835 | */ | ||
| 836 | int __ceph_caps_mds_wanted(struct ceph_inode_info *ci) | ||
| 837 | { | ||
| 838 | struct ceph_cap *cap; | ||
| 839 | struct rb_node *p; | ||
| 840 | int mds_wanted = 0; | ||
| 841 | |||
| 842 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | ||
| 843 | cap = rb_entry(p, struct ceph_cap, ci_node); | ||
| 844 | if (!__cap_is_valid(cap)) | ||
| 845 | continue; | ||
| 846 | mds_wanted |= cap->mds_wanted; | ||
| 847 | } | ||
| 848 | return mds_wanted; | ||
| 849 | } | ||
| 850 | |||
| 851 | /* | ||
| 852 | * called under i_lock | ||
| 853 | */ | ||
| 854 | static int __ceph_is_any_caps(struct ceph_inode_info *ci) | ||
| 855 | { | ||
| 856 | return !RB_EMPTY_ROOT(&ci->i_caps) || ci->i_cap_exporting_mds >= 0; | ||
| 857 | } | ||
| 858 | |||
| 859 | /* | ||
| 860 | * caller should hold i_lock. | ||
| 861 | * caller will not hold session s_mutex if called from destroy_inode. | ||
| 862 | */ | ||
| 863 | void __ceph_remove_cap(struct ceph_cap *cap) | ||
| 864 | { | ||
| 865 | struct ceph_mds_session *session = cap->session; | ||
| 866 | struct ceph_inode_info *ci = cap->ci; | ||
| 867 | struct ceph_mds_client *mdsc = &ceph_client(ci->vfs_inode.i_sb)->mdsc; | ||
| 868 | |||
| 869 | dout("__ceph_remove_cap %p from %p\n", cap, &ci->vfs_inode); | ||
| 870 | |||
| 871 | /* remove from inode list */ | ||
| 872 | rb_erase(&cap->ci_node, &ci->i_caps); | ||
| 873 | cap->ci = NULL; | ||
| 874 | if (ci->i_auth_cap == cap) | ||
| 875 | ci->i_auth_cap = NULL; | ||
| 876 | |||
| 877 | /* remove from session list */ | ||
| 878 | spin_lock(&session->s_cap_lock); | ||
| 879 | if (session->s_cap_iterator == cap) { | ||
| 880 | /* not yet, we are iterating over this very cap */ | ||
| 881 | dout("__ceph_remove_cap delaying %p removal from session %p\n", | ||
| 882 | cap, cap->session); | ||
| 883 | } else { | ||
| 884 | list_del_init(&cap->session_caps); | ||
| 885 | session->s_nr_caps--; | ||
| 886 | cap->session = NULL; | ||
| 887 | } | ||
| 888 | spin_unlock(&session->s_cap_lock); | ||
| 889 | |||
| 890 | if (cap->session == NULL) | ||
| 891 | ceph_put_cap(cap); | ||
| 892 | |||
| 893 | if (!__ceph_is_any_caps(ci) && ci->i_snap_realm) { | ||
| 894 | struct ceph_snap_realm *realm = ci->i_snap_realm; | ||
| 895 | spin_lock(&realm->inodes_with_caps_lock); | ||
| 896 | list_del_init(&ci->i_snap_realm_item); | ||
| 897 | ci->i_snap_realm_counter++; | ||
| 898 | ci->i_snap_realm = NULL; | ||
| 899 | spin_unlock(&realm->inodes_with_caps_lock); | ||
| 900 | ceph_put_snap_realm(mdsc, realm); | ||
| 901 | } | ||
| 902 | if (!__ceph_is_any_real_caps(ci)) | ||
| 903 | __cap_delay_cancel(mdsc, ci); | ||
| 904 | } | ||
| 905 | |||
| 906 | /* | ||
| 907 | * Build and send a cap message to the given MDS. | ||
| 908 | * | ||
| 909 | * Caller should be holding s_mutex. | ||
| 910 | */ | ||
| 911 | static int send_cap_msg(struct ceph_mds_session *session, | ||
| 912 | u64 ino, u64 cid, int op, | ||
| 913 | int caps, int wanted, int dirty, | ||
| 914 | u32 seq, u64 flush_tid, u32 issue_seq, u32 mseq, | ||
| 915 | u64 size, u64 max_size, | ||
| 916 | struct timespec *mtime, struct timespec *atime, | ||
| 917 | u64 time_warp_seq, | ||
| 918 | uid_t uid, gid_t gid, mode_t mode, | ||
| 919 | u64 xattr_version, | ||
| 920 | struct ceph_buffer *xattrs_buf, | ||
| 921 | u64 follows) | ||
| 922 | { | ||
| 923 | struct ceph_mds_caps *fc; | ||
| 924 | struct ceph_msg *msg; | ||
| 925 | |||
| 926 | dout("send_cap_msg %s %llx %llx caps %s wanted %s dirty %s" | ||
| 927 | " seq %u/%u mseq %u follows %lld size %llu/%llu" | ||
| 928 | " xattr_ver %llu xattr_len %d\n", ceph_cap_op_name(op), | ||
| 929 | cid, ino, ceph_cap_string(caps), ceph_cap_string(wanted), | ||
| 930 | ceph_cap_string(dirty), | ||
| 931 | seq, issue_seq, mseq, follows, size, max_size, | ||
| 932 | xattr_version, xattrs_buf ? (int)xattrs_buf->vec.iov_len : 0); | ||
| 933 | |||
| 934 | msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPS, sizeof(*fc), 0, 0, NULL); | ||
| 935 | if (IS_ERR(msg)) | ||
| 936 | return PTR_ERR(msg); | ||
| 937 | |||
| 938 | msg->hdr.tid = cpu_to_le64(flush_tid); | ||
| 939 | |||
| 940 | fc = msg->front.iov_base; | ||
| 941 | memset(fc, 0, sizeof(*fc)); | ||
| 942 | |||
| 943 | fc->cap_id = cpu_to_le64(cid); | ||
| 944 | fc->op = cpu_to_le32(op); | ||
| 945 | fc->seq = cpu_to_le32(seq); | ||
| 946 | fc->issue_seq = cpu_to_le32(issue_seq); | ||
| 947 | fc->migrate_seq = cpu_to_le32(mseq); | ||
| 948 | fc->caps = cpu_to_le32(caps); | ||
| 949 | fc->wanted = cpu_to_le32(wanted); | ||
| 950 | fc->dirty = cpu_to_le32(dirty); | ||
| 951 | fc->ino = cpu_to_le64(ino); | ||
| 952 | fc->snap_follows = cpu_to_le64(follows); | ||
| 953 | |||
| 954 | fc->size = cpu_to_le64(size); | ||
| 955 | fc->max_size = cpu_to_le64(max_size); | ||
| 956 | if (mtime) | ||
| 957 | ceph_encode_timespec(&fc->mtime, mtime); | ||
| 958 | if (atime) | ||
| 959 | ceph_encode_timespec(&fc->atime, atime); | ||
| 960 | fc->time_warp_seq = cpu_to_le32(time_warp_seq); | ||
| 961 | |||
| 962 | fc->uid = cpu_to_le32(uid); | ||
| 963 | fc->gid = cpu_to_le32(gid); | ||
| 964 | fc->mode = cpu_to_le32(mode); | ||
| 965 | |||
| 966 | fc->xattr_version = cpu_to_le64(xattr_version); | ||
| 967 | if (xattrs_buf) { | ||
| 968 | msg->middle = ceph_buffer_get(xattrs_buf); | ||
| 969 | fc->xattr_len = cpu_to_le32(xattrs_buf->vec.iov_len); | ||
| 970 | msg->hdr.middle_len = cpu_to_le32(xattrs_buf->vec.iov_len); | ||
| 971 | } | ||
| 972 | |||
| 973 | ceph_con_send(&session->s_con, msg); | ||
| 974 | return 0; | ||
| 975 | } | ||
| 976 | |||
| 977 | /* | ||
| 978 | * Queue cap releases when an inode is dropped from our cache. Since | ||
| 979 | * inode is about to be destroyed, there is no need for i_lock. | ||
| 980 | */ | ||
| 981 | void ceph_queue_caps_release(struct inode *inode) | ||
| 982 | { | ||
| 983 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 984 | struct rb_node *p; | ||
| 985 | |||
| 986 | p = rb_first(&ci->i_caps); | ||
| 987 | while (p) { | ||
| 988 | struct ceph_cap *cap = rb_entry(p, struct ceph_cap, ci_node); | ||
| 989 | struct ceph_mds_session *session = cap->session; | ||
| 990 | struct ceph_msg *msg; | ||
| 991 | struct ceph_mds_cap_release *head; | ||
| 992 | struct ceph_mds_cap_item *item; | ||
| 993 | |||
| 994 | spin_lock(&session->s_cap_lock); | ||
| 995 | BUG_ON(!session->s_num_cap_releases); | ||
| 996 | msg = list_first_entry(&session->s_cap_releases, | ||
| 997 | struct ceph_msg, list_head); | ||
| 998 | |||
| 999 | dout(" adding %p release to mds%d msg %p (%d left)\n", | ||
| 1000 | inode, session->s_mds, msg, session->s_num_cap_releases); | ||
| 1001 | |||
| 1002 | BUG_ON(msg->front.iov_len + sizeof(*item) > PAGE_CACHE_SIZE); | ||
| 1003 | head = msg->front.iov_base; | ||
| 1004 | head->num = cpu_to_le32(le32_to_cpu(head->num) + 1); | ||
| 1005 | item = msg->front.iov_base + msg->front.iov_len; | ||
| 1006 | item->ino = cpu_to_le64(ceph_ino(inode)); | ||
| 1007 | item->cap_id = cpu_to_le64(cap->cap_id); | ||
| 1008 | item->migrate_seq = cpu_to_le32(cap->mseq); | ||
| 1009 | item->seq = cpu_to_le32(cap->issue_seq); | ||
| 1010 | |||
| 1011 | session->s_num_cap_releases--; | ||
| 1012 | |||
| 1013 | msg->front.iov_len += sizeof(*item); | ||
| 1014 | if (le32_to_cpu(head->num) == CEPH_CAPS_PER_RELEASE) { | ||
| 1015 | dout(" release msg %p full\n", msg); | ||
| 1016 | list_move_tail(&msg->list_head, | ||
| 1017 | &session->s_cap_releases_done); | ||
| 1018 | } else { | ||
| 1019 | dout(" release msg %p at %d/%d (%d)\n", msg, | ||
| 1020 | (int)le32_to_cpu(head->num), | ||
| 1021 | (int)CEPH_CAPS_PER_RELEASE, | ||
| 1022 | (int)msg->front.iov_len); | ||
| 1023 | } | ||
| 1024 | spin_unlock(&session->s_cap_lock); | ||
| 1025 | p = rb_next(p); | ||
| 1026 | __ceph_remove_cap(cap); | ||
| 1027 | } | ||
| 1028 | } | ||
| 1029 | |||
| 1030 | /* | ||
| 1031 | * Send a cap msg on the given inode. Update our caps state, then | ||
| 1032 | * drop i_lock and send the message. | ||
| 1033 | * | ||
| 1034 | * Make note of max_size reported/requested from mds, revoked caps | ||
| 1035 | * that have now been implemented. | ||
| 1036 | * | ||
| 1037 | * Make half-hearted attempt ot to invalidate page cache if we are | ||
| 1038 | * dropping RDCACHE. Note that this will leave behind locked pages | ||
| 1039 | * that we'll then need to deal with elsewhere. | ||
| 1040 | * | ||
| 1041 | * Return non-zero if delayed release, or we experienced an error | ||
| 1042 | * such that the caller should requeue + retry later. | ||
| 1043 | * | ||
| 1044 | * called with i_lock, then drops it. | ||
| 1045 | * caller should hold snap_rwsem (read), s_mutex. | ||
| 1046 | */ | ||
| 1047 | static int __send_cap(struct ceph_mds_client *mdsc, struct ceph_cap *cap, | ||
| 1048 | int op, int used, int want, int retain, int flushing, | ||
| 1049 | unsigned *pflush_tid) | ||
| 1050 | __releases(cap->ci->vfs_inode->i_lock) | ||
| 1051 | { | ||
| 1052 | struct ceph_inode_info *ci = cap->ci; | ||
| 1053 | struct inode *inode = &ci->vfs_inode; | ||
| 1054 | u64 cap_id = cap->cap_id; | ||
| 1055 | int held, revoking, dropping, keep; | ||
| 1056 | u64 seq, issue_seq, mseq, time_warp_seq, follows; | ||
| 1057 | u64 size, max_size; | ||
| 1058 | struct timespec mtime, atime; | ||
| 1059 | int wake = 0; | ||
| 1060 | mode_t mode; | ||
| 1061 | uid_t uid; | ||
| 1062 | gid_t gid; | ||
| 1063 | struct ceph_mds_session *session; | ||
| 1064 | u64 xattr_version = 0; | ||
| 1065 | int delayed = 0; | ||
| 1066 | u64 flush_tid = 0; | ||
| 1067 | int i; | ||
| 1068 | int ret; | ||
| 1069 | |||
| 1070 | held = cap->issued | cap->implemented; | ||
| 1071 | revoking = cap->implemented & ~cap->issued; | ||
| 1072 | retain &= ~revoking; | ||
| 1073 | dropping = cap->issued & ~retain; | ||
| 1074 | |||
| 1075 | dout("__send_cap %p cap %p session %p %s -> %s (revoking %s)\n", | ||
| 1076 | inode, cap, cap->session, | ||
| 1077 | ceph_cap_string(held), ceph_cap_string(held & retain), | ||
| 1078 | ceph_cap_string(revoking)); | ||
| 1079 | BUG_ON((retain & CEPH_CAP_PIN) == 0); | ||
| 1080 | |||
| 1081 | session = cap->session; | ||
| 1082 | |||
| 1083 | /* don't release wanted unless we've waited a bit. */ | ||
| 1084 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && | ||
| 1085 | time_before(jiffies, ci->i_hold_caps_min)) { | ||
| 1086 | dout(" delaying issued %s -> %s, wanted %s -> %s on send\n", | ||
| 1087 | ceph_cap_string(cap->issued), | ||
| 1088 | ceph_cap_string(cap->issued & retain), | ||
| 1089 | ceph_cap_string(cap->mds_wanted), | ||
| 1090 | ceph_cap_string(want)); | ||
| 1091 | want |= cap->mds_wanted; | ||
| 1092 | retain |= cap->issued; | ||
| 1093 | delayed = 1; | ||
| 1094 | } | ||
| 1095 | ci->i_ceph_flags &= ~(CEPH_I_NODELAY | CEPH_I_FLUSH); | ||
| 1096 | |||
| 1097 | cap->issued &= retain; /* drop bits we don't want */ | ||
| 1098 | if (cap->implemented & ~cap->issued) { | ||
| 1099 | /* | ||
| 1100 | * Wake up any waiters on wanted -> needed transition. | ||
| 1101 | * This is due to the weird transition from buffered | ||
| 1102 | * to sync IO... we need to flush dirty pages _before_ | ||
| 1103 | * allowing sync writes to avoid reordering. | ||
| 1104 | */ | ||
| 1105 | wake = 1; | ||
| 1106 | } | ||
| 1107 | cap->implemented &= cap->issued | used; | ||
| 1108 | cap->mds_wanted = want; | ||
| 1109 | |||
| 1110 | if (flushing) { | ||
| 1111 | /* | ||
| 1112 | * assign a tid for flush operations so we can avoid | ||
| 1113 | * flush1 -> dirty1 -> flush2 -> flushack1 -> mark | ||
| 1114 | * clean type races. track latest tid for every bit | ||
| 1115 | * so we can handle flush AxFw, flush Fw, and have the | ||
| 1116 | * first ack clean Ax. | ||
| 1117 | */ | ||
| 1118 | flush_tid = ++ci->i_cap_flush_last_tid; | ||
| 1119 | if (pflush_tid) | ||
| 1120 | *pflush_tid = flush_tid; | ||
| 1121 | dout(" cap_flush_tid %d\n", (int)flush_tid); | ||
| 1122 | for (i = 0; i < CEPH_CAP_BITS; i++) | ||
| 1123 | if (flushing & (1 << i)) | ||
| 1124 | ci->i_cap_flush_tid[i] = flush_tid; | ||
| 1125 | } | ||
| 1126 | |||
| 1127 | keep = cap->implemented; | ||
| 1128 | seq = cap->seq; | ||
| 1129 | issue_seq = cap->issue_seq; | ||
| 1130 | mseq = cap->mseq; | ||
| 1131 | size = inode->i_size; | ||
| 1132 | ci->i_reported_size = size; | ||
| 1133 | max_size = ci->i_wanted_max_size; | ||
| 1134 | ci->i_requested_max_size = max_size; | ||
| 1135 | mtime = inode->i_mtime; | ||
| 1136 | atime = inode->i_atime; | ||
| 1137 | time_warp_seq = ci->i_time_warp_seq; | ||
| 1138 | follows = ci->i_snap_realm->cached_context->seq; | ||
| 1139 | uid = inode->i_uid; | ||
| 1140 | gid = inode->i_gid; | ||
| 1141 | mode = inode->i_mode; | ||
| 1142 | |||
| 1143 | if (dropping & CEPH_CAP_XATTR_EXCL) { | ||
| 1144 | __ceph_build_xattrs_blob(ci); | ||
| 1145 | xattr_version = ci->i_xattrs.version + 1; | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | spin_unlock(&inode->i_lock); | ||
| 1149 | |||
| 1150 | ret = send_cap_msg(session, ceph_vino(inode).ino, cap_id, | ||
| 1151 | op, keep, want, flushing, seq, flush_tid, issue_seq, mseq, | ||
| 1152 | size, max_size, &mtime, &atime, time_warp_seq, | ||
| 1153 | uid, gid, mode, | ||
| 1154 | xattr_version, | ||
| 1155 | (flushing & CEPH_CAP_XATTR_EXCL) ? ci->i_xattrs.blob : NULL, | ||
| 1156 | follows); | ||
| 1157 | if (ret < 0) { | ||
| 1158 | dout("error sending cap msg, must requeue %p\n", inode); | ||
| 1159 | delayed = 1; | ||
| 1160 | } | ||
| 1161 | |||
| 1162 | if (wake) | ||
| 1163 | wake_up(&ci->i_cap_wq); | ||
| 1164 | |||
| 1165 | return delayed; | ||
| 1166 | } | ||
| 1167 | |||
| 1168 | /* | ||
| 1169 | * When a snapshot is taken, clients accumulate dirty metadata on | ||
| 1170 | * inodes with capabilities in ceph_cap_snaps to describe the file | ||
| 1171 | * state at the time the snapshot was taken. This must be flushed | ||
| 1172 | * asynchronously back to the MDS once sync writes complete and dirty | ||
| 1173 | * data is written out. | ||
| 1174 | * | ||
| 1175 | * Called under i_lock. Takes s_mutex as needed. | ||
| 1176 | */ | ||
| 1177 | void __ceph_flush_snaps(struct ceph_inode_info *ci, | ||
| 1178 | struct ceph_mds_session **psession) | ||
| 1179 | { | ||
| 1180 | struct inode *inode = &ci->vfs_inode; | ||
| 1181 | int mds; | ||
| 1182 | struct ceph_cap_snap *capsnap; | ||
| 1183 | u32 mseq; | ||
| 1184 | struct ceph_mds_client *mdsc = &ceph_inode_to_client(inode)->mdsc; | ||
| 1185 | struct ceph_mds_session *session = NULL; /* if session != NULL, we hold | ||
| 1186 | session->s_mutex */ | ||
| 1187 | u64 next_follows = 0; /* keep track of how far we've gotten through the | ||
| 1188 | i_cap_snaps list, and skip these entries next time | ||
| 1189 | around to avoid an infinite loop */ | ||
| 1190 | |||
| 1191 | if (psession) | ||
| 1192 | session = *psession; | ||
| 1193 | |||
| 1194 | dout("__flush_snaps %p\n", inode); | ||
| 1195 | retry: | ||
| 1196 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { | ||
| 1197 | /* avoid an infiniute loop after retry */ | ||
| 1198 | if (capsnap->follows < next_follows) | ||
| 1199 | continue; | ||
| 1200 | /* | ||
| 1201 | * we need to wait for sync writes to complete and for dirty | ||
| 1202 | * pages to be written out. | ||
| 1203 | */ | ||
| 1204 | if (capsnap->dirty_pages || capsnap->writing) | ||
| 1205 | continue; | ||
| 1206 | |||
| 1207 | /* pick mds, take s_mutex */ | ||
| 1208 | mds = __ceph_get_cap_mds(ci, &mseq); | ||
| 1209 | if (session && session->s_mds != mds) { | ||
| 1210 | dout("oops, wrong session %p mutex\n", session); | ||
| 1211 | mutex_unlock(&session->s_mutex); | ||
| 1212 | ceph_put_mds_session(session); | ||
| 1213 | session = NULL; | ||
| 1214 | } | ||
| 1215 | if (!session) { | ||
| 1216 | spin_unlock(&inode->i_lock); | ||
| 1217 | mutex_lock(&mdsc->mutex); | ||
| 1218 | session = __ceph_lookup_mds_session(mdsc, mds); | ||
| 1219 | mutex_unlock(&mdsc->mutex); | ||
| 1220 | if (session) { | ||
| 1221 | dout("inverting session/ino locks on %p\n", | ||
| 1222 | session); | ||
| 1223 | mutex_lock(&session->s_mutex); | ||
| 1224 | } | ||
| 1225 | /* | ||
| 1226 | * if session == NULL, we raced against a cap | ||
| 1227 | * deletion. retry, and we'll get a better | ||
| 1228 | * @mds value next time. | ||
| 1229 | */ | ||
| 1230 | spin_lock(&inode->i_lock); | ||
| 1231 | goto retry; | ||
| 1232 | } | ||
| 1233 | |||
| 1234 | capsnap->flush_tid = ++ci->i_cap_flush_last_tid; | ||
| 1235 | atomic_inc(&capsnap->nref); | ||
| 1236 | if (!list_empty(&capsnap->flushing_item)) | ||
| 1237 | list_del_init(&capsnap->flushing_item); | ||
| 1238 | list_add_tail(&capsnap->flushing_item, | ||
| 1239 | &session->s_cap_snaps_flushing); | ||
| 1240 | spin_unlock(&inode->i_lock); | ||
| 1241 | |||
| 1242 | dout("flush_snaps %p cap_snap %p follows %lld size %llu\n", | ||
| 1243 | inode, capsnap, next_follows, capsnap->size); | ||
| 1244 | send_cap_msg(session, ceph_vino(inode).ino, 0, | ||
| 1245 | CEPH_CAP_OP_FLUSHSNAP, capsnap->issued, 0, | ||
| 1246 | capsnap->dirty, 0, capsnap->flush_tid, 0, mseq, | ||
| 1247 | capsnap->size, 0, | ||
| 1248 | &capsnap->mtime, &capsnap->atime, | ||
| 1249 | capsnap->time_warp_seq, | ||
| 1250 | capsnap->uid, capsnap->gid, capsnap->mode, | ||
| 1251 | 0, NULL, | ||
| 1252 | capsnap->follows); | ||
| 1253 | |||
| 1254 | next_follows = capsnap->follows + 1; | ||
| 1255 | ceph_put_cap_snap(capsnap); | ||
| 1256 | |||
| 1257 | spin_lock(&inode->i_lock); | ||
| 1258 | goto retry; | ||
| 1259 | } | ||
| 1260 | |||
| 1261 | /* we flushed them all; remove this inode from the queue */ | ||
| 1262 | spin_lock(&mdsc->snap_flush_lock); | ||
| 1263 | list_del_init(&ci->i_snap_flush_item); | ||
| 1264 | spin_unlock(&mdsc->snap_flush_lock); | ||
| 1265 | |||
| 1266 | if (psession) | ||
| 1267 | *psession = session; | ||
| 1268 | else if (session) { | ||
| 1269 | mutex_unlock(&session->s_mutex); | ||
| 1270 | ceph_put_mds_session(session); | ||
| 1271 | } | ||
| 1272 | } | ||
| 1273 | |||
| 1274 | static void ceph_flush_snaps(struct ceph_inode_info *ci) | ||
| 1275 | { | ||
| 1276 | struct inode *inode = &ci->vfs_inode; | ||
| 1277 | |||
| 1278 | spin_lock(&inode->i_lock); | ||
| 1279 | __ceph_flush_snaps(ci, NULL); | ||
| 1280 | spin_unlock(&inode->i_lock); | ||
| 1281 | } | ||
| 1282 | |||
| 1283 | /* | ||
| 1284 | * Mark caps dirty. If inode is newly dirty, add to the global dirty | ||
| 1285 | * list. | ||
| 1286 | */ | ||
| 1287 | void __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask) | ||
| 1288 | { | ||
| 1289 | struct ceph_mds_client *mdsc = &ceph_client(ci->vfs_inode.i_sb)->mdsc; | ||
| 1290 | struct inode *inode = &ci->vfs_inode; | ||
| 1291 | int was = ci->i_dirty_caps; | ||
| 1292 | int dirty = 0; | ||
| 1293 | |||
| 1294 | dout("__mark_dirty_caps %p %s dirty %s -> %s\n", &ci->vfs_inode, | ||
| 1295 | ceph_cap_string(mask), ceph_cap_string(was), | ||
| 1296 | ceph_cap_string(was | mask)); | ||
| 1297 | ci->i_dirty_caps |= mask; | ||
| 1298 | if (was == 0) { | ||
| 1299 | dout(" inode %p now dirty\n", &ci->vfs_inode); | ||
| 1300 | BUG_ON(!list_empty(&ci->i_dirty_item)); | ||
| 1301 | spin_lock(&mdsc->cap_dirty_lock); | ||
| 1302 | list_add(&ci->i_dirty_item, &mdsc->cap_dirty); | ||
| 1303 | spin_unlock(&mdsc->cap_dirty_lock); | ||
| 1304 | if (ci->i_flushing_caps == 0) { | ||
| 1305 | igrab(inode); | ||
| 1306 | dirty |= I_DIRTY_SYNC; | ||
| 1307 | } | ||
| 1308 | } | ||
| 1309 | BUG_ON(list_empty(&ci->i_dirty_item)); | ||
| 1310 | if (((was | ci->i_flushing_caps) & CEPH_CAP_FILE_BUFFER) && | ||
| 1311 | (mask & CEPH_CAP_FILE_BUFFER)) | ||
| 1312 | dirty |= I_DIRTY_DATASYNC; | ||
| 1313 | if (dirty) | ||
| 1314 | __mark_inode_dirty(inode, dirty); | ||
| 1315 | __cap_delay_requeue(mdsc, ci); | ||
| 1316 | } | ||
| 1317 | |||
| 1318 | /* | ||
| 1319 | * Add dirty inode to the flushing list. Assigned a seq number so we | ||
| 1320 | * can wait for caps to flush without starving. | ||
| 1321 | * | ||
| 1322 | * Called under i_lock. | ||
| 1323 | */ | ||
| 1324 | static int __mark_caps_flushing(struct inode *inode, | ||
| 1325 | struct ceph_mds_session *session) | ||
| 1326 | { | ||
| 1327 | struct ceph_mds_client *mdsc = &ceph_client(inode->i_sb)->mdsc; | ||
| 1328 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 1329 | int flushing; | ||
| 1330 | |||
| 1331 | BUG_ON(ci->i_dirty_caps == 0); | ||
| 1332 | BUG_ON(list_empty(&ci->i_dirty_item)); | ||
| 1333 | |||
| 1334 | flushing = ci->i_dirty_caps; | ||
| 1335 | dout("__mark_caps_flushing flushing %s, flushing_caps %s -> %s\n", | ||
| 1336 | ceph_cap_string(flushing), | ||
| 1337 | ceph_cap_string(ci->i_flushing_caps), | ||
| 1338 | ceph_cap_string(ci->i_flushing_caps | flushing)); | ||
| 1339 | ci->i_flushing_caps |= flushing; | ||
| 1340 | ci->i_dirty_caps = 0; | ||
| 1341 | dout(" inode %p now !dirty\n", inode); | ||
| 1342 | |||
| 1343 | spin_lock(&mdsc->cap_dirty_lock); | ||
| 1344 | list_del_init(&ci->i_dirty_item); | ||
| 1345 | |||
| 1346 | ci->i_cap_flush_seq = ++mdsc->cap_flush_seq; | ||
| 1347 | if (list_empty(&ci->i_flushing_item)) { | ||
| 1348 | list_add_tail(&ci->i_flushing_item, &session->s_cap_flushing); | ||
| 1349 | mdsc->num_cap_flushing++; | ||
| 1350 | dout(" inode %p now flushing seq %lld\n", inode, | ||
| 1351 | ci->i_cap_flush_seq); | ||
| 1352 | } else { | ||
| 1353 | list_move_tail(&ci->i_flushing_item, &session->s_cap_flushing); | ||
| 1354 | dout(" inode %p now flushing (more) seq %lld\n", inode, | ||
| 1355 | ci->i_cap_flush_seq); | ||
| 1356 | } | ||
| 1357 | spin_unlock(&mdsc->cap_dirty_lock); | ||
| 1358 | |||
| 1359 | return flushing; | ||
| 1360 | } | ||
| 1361 | |||
| 1362 | /* | ||
| 1363 | * try to invalidate mapping pages without blocking. | ||
| 1364 | */ | ||
| 1365 | static int mapping_is_empty(struct address_space *mapping) | ||
| 1366 | { | ||
| 1367 | struct page *page = find_get_page(mapping, 0); | ||
| 1368 | |||
| 1369 | if (!page) | ||
| 1370 | return 1; | ||
| 1371 | |||
| 1372 | put_page(page); | ||
| 1373 | return 0; | ||
| 1374 | } | ||
| 1375 | |||
| 1376 | static int try_nonblocking_invalidate(struct inode *inode) | ||
| 1377 | { | ||
| 1378 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 1379 | u32 invalidating_gen = ci->i_rdcache_gen; | ||
| 1380 | |||
| 1381 | spin_unlock(&inode->i_lock); | ||
| 1382 | invalidate_mapping_pages(&inode->i_data, 0, -1); | ||
| 1383 | spin_lock(&inode->i_lock); | ||
| 1384 | |||
| 1385 | if (mapping_is_empty(&inode->i_data) && | ||
| 1386 | invalidating_gen == ci->i_rdcache_gen) { | ||
| 1387 | /* success. */ | ||
| 1388 | dout("try_nonblocking_invalidate %p success\n", inode); | ||
| 1389 | ci->i_rdcache_gen = 0; | ||
| 1390 | ci->i_rdcache_revoking = 0; | ||
| 1391 | return 0; | ||
| 1392 | } | ||
| 1393 | dout("try_nonblocking_invalidate %p failed\n", inode); | ||
| 1394 | return -1; | ||
| 1395 | } | ||
| 1396 | |||
| 1397 | /* | ||
| 1398 | * Swiss army knife function to examine currently used and wanted | ||
| 1399 | * versus held caps. Release, flush, ack revoked caps to mds as | ||
| 1400 | * appropriate. | ||
| 1401 | * | ||
| 1402 | * CHECK_CAPS_NODELAY - caller is delayed work and we should not delay | ||
| 1403 | * cap release further. | ||
| 1404 | * CHECK_CAPS_AUTHONLY - we should only check the auth cap | ||
| 1405 | * CHECK_CAPS_FLUSH - we should flush any dirty caps immediately, without | ||
| 1406 | * further delay. | ||
| 1407 | */ | ||
| 1408 | void ceph_check_caps(struct ceph_inode_info *ci, int flags, | ||
| 1409 | struct ceph_mds_session *session) | ||
| 1410 | __releases(session->s_mutex) | ||
| 1411 | { | ||
| 1412 | struct ceph_client *client = ceph_inode_to_client(&ci->vfs_inode); | ||
| 1413 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 1414 | struct inode *inode = &ci->vfs_inode; | ||
| 1415 | struct ceph_cap *cap; | ||
| 1416 | int file_wanted, used; | ||
| 1417 | int took_snap_rwsem = 0; /* true if mdsc->snap_rwsem held */ | ||
| 1418 | int issued, implemented, want, retain, revoking, flushing = 0; | ||
| 1419 | int mds = -1; /* keep track of how far we've gone through i_caps list | ||
| 1420 | to avoid an infinite loop on retry */ | ||
| 1421 | struct rb_node *p; | ||
| 1422 | int tried_invalidate = 0; | ||
| 1423 | int delayed = 0, sent = 0, force_requeue = 0, num; | ||
| 1424 | int queue_invalidate = 0; | ||
| 1425 | int is_delayed = flags & CHECK_CAPS_NODELAY; | ||
| 1426 | |||
| 1427 | /* if we are unmounting, flush any unused caps immediately. */ | ||
| 1428 | if (mdsc->stopping) | ||
| 1429 | is_delayed = 1; | ||
| 1430 | |||
| 1431 | spin_lock(&inode->i_lock); | ||
| 1432 | |||
| 1433 | if (ci->i_ceph_flags & CEPH_I_FLUSH) | ||
| 1434 | flags |= CHECK_CAPS_FLUSH; | ||
| 1435 | |||
| 1436 | /* flush snaps first time around only */ | ||
| 1437 | if (!list_empty(&ci->i_cap_snaps)) | ||
| 1438 | __ceph_flush_snaps(ci, &session); | ||
| 1439 | goto retry_locked; | ||
| 1440 | retry: | ||
| 1441 | spin_lock(&inode->i_lock); | ||
| 1442 | retry_locked: | ||
| 1443 | file_wanted = __ceph_caps_file_wanted(ci); | ||
| 1444 | used = __ceph_caps_used(ci); | ||
| 1445 | want = file_wanted | used; | ||
| 1446 | issued = __ceph_caps_issued(ci, &implemented); | ||
| 1447 | revoking = implemented & ~issued; | ||
| 1448 | |||
| 1449 | retain = want | CEPH_CAP_PIN; | ||
| 1450 | if (!mdsc->stopping && inode->i_nlink > 0) { | ||
| 1451 | if (want) { | ||
| 1452 | retain |= CEPH_CAP_ANY; /* be greedy */ | ||
| 1453 | } else { | ||
| 1454 | retain |= CEPH_CAP_ANY_SHARED; | ||
| 1455 | /* | ||
| 1456 | * keep RD only if we didn't have the file open RW, | ||
| 1457 | * because then the mds would revoke it anyway to | ||
| 1458 | * journal max_size=0. | ||
| 1459 | */ | ||
| 1460 | if (ci->i_max_size == 0) | ||
| 1461 | retain |= CEPH_CAP_ANY_RD; | ||
| 1462 | } | ||
| 1463 | } | ||
| 1464 | |||
| 1465 | dout("check_caps %p file_want %s used %s dirty %s flushing %s" | ||
| 1466 | " issued %s revoking %s retain %s %s%s%s\n", inode, | ||
| 1467 | ceph_cap_string(file_wanted), | ||
| 1468 | ceph_cap_string(used), ceph_cap_string(ci->i_dirty_caps), | ||
| 1469 | ceph_cap_string(ci->i_flushing_caps), | ||
| 1470 | ceph_cap_string(issued), ceph_cap_string(revoking), | ||
| 1471 | ceph_cap_string(retain), | ||
| 1472 | (flags & CHECK_CAPS_AUTHONLY) ? " AUTHONLY" : "", | ||
| 1473 | (flags & CHECK_CAPS_NODELAY) ? " NODELAY" : "", | ||
| 1474 | (flags & CHECK_CAPS_FLUSH) ? " FLUSH" : ""); | ||
| 1475 | |||
| 1476 | /* | ||
| 1477 | * If we no longer need to hold onto old our caps, and we may | ||
| 1478 | * have cached pages, but don't want them, then try to invalidate. | ||
| 1479 | * If we fail, it's because pages are locked.... try again later. | ||
| 1480 | */ | ||
| 1481 | if ((!is_delayed || mdsc->stopping) && | ||
| 1482 | ci->i_wrbuffer_ref == 0 && /* no dirty pages... */ | ||
| 1483 | ci->i_rdcache_gen && /* may have cached pages */ | ||
| 1484 | (file_wanted == 0 || /* no open files */ | ||
| 1485 | (revoking & CEPH_CAP_FILE_CACHE)) && /* or revoking cache */ | ||
| 1486 | !tried_invalidate) { | ||
| 1487 | dout("check_caps trying to invalidate on %p\n", inode); | ||
| 1488 | if (try_nonblocking_invalidate(inode) < 0) { | ||
| 1489 | if (revoking & CEPH_CAP_FILE_CACHE) { | ||
| 1490 | dout("check_caps queuing invalidate\n"); | ||
| 1491 | queue_invalidate = 1; | ||
| 1492 | ci->i_rdcache_revoking = ci->i_rdcache_gen; | ||
| 1493 | } else { | ||
| 1494 | dout("check_caps failed to invalidate pages\n"); | ||
| 1495 | /* we failed to invalidate pages. check these | ||
| 1496 | caps again later. */ | ||
| 1497 | force_requeue = 1; | ||
| 1498 | __cap_set_timeouts(mdsc, ci); | ||
| 1499 | } | ||
| 1500 | } | ||
| 1501 | tried_invalidate = 1; | ||
| 1502 | goto retry_locked; | ||
| 1503 | } | ||
| 1504 | |||
| 1505 | num = 0; | ||
| 1506 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | ||
| 1507 | cap = rb_entry(p, struct ceph_cap, ci_node); | ||
| 1508 | num++; | ||
| 1509 | |||
| 1510 | /* avoid looping forever */ | ||
| 1511 | if (mds >= cap->mds || | ||
| 1512 | ((flags & CHECK_CAPS_AUTHONLY) && cap != ci->i_auth_cap)) | ||
| 1513 | continue; | ||
| 1514 | |||
| 1515 | /* NOTE: no side-effects allowed, until we take s_mutex */ | ||
| 1516 | |||
| 1517 | revoking = cap->implemented & ~cap->issued; | ||
| 1518 | if (revoking) | ||
| 1519 | dout(" mds%d revoking %s\n", cap->mds, | ||
| 1520 | ceph_cap_string(revoking)); | ||
| 1521 | |||
| 1522 | if (cap == ci->i_auth_cap && | ||
| 1523 | (cap->issued & CEPH_CAP_FILE_WR)) { | ||
| 1524 | /* request larger max_size from MDS? */ | ||
| 1525 | if (ci->i_wanted_max_size > ci->i_max_size && | ||
| 1526 | ci->i_wanted_max_size > ci->i_requested_max_size) { | ||
| 1527 | dout("requesting new max_size\n"); | ||
| 1528 | goto ack; | ||
| 1529 | } | ||
| 1530 | |||
| 1531 | /* approaching file_max? */ | ||
| 1532 | if ((inode->i_size << 1) >= ci->i_max_size && | ||
| 1533 | (ci->i_reported_size << 1) < ci->i_max_size) { | ||
| 1534 | dout("i_size approaching max_size\n"); | ||
| 1535 | goto ack; | ||
| 1536 | } | ||
| 1537 | } | ||
| 1538 | /* flush anything dirty? */ | ||
| 1539 | if (cap == ci->i_auth_cap && (flags & CHECK_CAPS_FLUSH) && | ||
| 1540 | ci->i_dirty_caps) { | ||
| 1541 | dout("flushing dirty caps\n"); | ||
| 1542 | goto ack; | ||
| 1543 | } | ||
| 1544 | |||
| 1545 | /* completed revocation? going down and there are no caps? */ | ||
| 1546 | if (revoking && (revoking & used) == 0) { | ||
| 1547 | dout("completed revocation of %s\n", | ||
| 1548 | ceph_cap_string(cap->implemented & ~cap->issued)); | ||
| 1549 | goto ack; | ||
| 1550 | } | ||
| 1551 | |||
| 1552 | /* want more caps from mds? */ | ||
| 1553 | if (want & ~(cap->mds_wanted | cap->issued)) | ||
| 1554 | goto ack; | ||
| 1555 | |||
| 1556 | /* things we might delay */ | ||
| 1557 | if ((cap->issued & ~retain) == 0 && | ||
| 1558 | cap->mds_wanted == want) | ||
| 1559 | continue; /* nope, all good */ | ||
| 1560 | |||
| 1561 | if (is_delayed) | ||
| 1562 | goto ack; | ||
| 1563 | |||
| 1564 | /* delay? */ | ||
| 1565 | if ((ci->i_ceph_flags & CEPH_I_NODELAY) == 0 && | ||
| 1566 | time_before(jiffies, ci->i_hold_caps_max)) { | ||
| 1567 | dout(" delaying issued %s -> %s, wanted %s -> %s\n", | ||
| 1568 | ceph_cap_string(cap->issued), | ||
| 1569 | ceph_cap_string(cap->issued & retain), | ||
| 1570 | ceph_cap_string(cap->mds_wanted), | ||
| 1571 | ceph_cap_string(want)); | ||
| 1572 | delayed++; | ||
| 1573 | continue; | ||
| 1574 | } | ||
| 1575 | |||
| 1576 | ack: | ||
| 1577 | if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { | ||
| 1578 | dout(" skipping %p I_NOFLUSH set\n", inode); | ||
| 1579 | continue; | ||
| 1580 | } | ||
| 1581 | |||
| 1582 | if (session && session != cap->session) { | ||
| 1583 | dout("oops, wrong session %p mutex\n", session); | ||
| 1584 | mutex_unlock(&session->s_mutex); | ||
| 1585 | session = NULL; | ||
| 1586 | } | ||
| 1587 | if (!session) { | ||
| 1588 | session = cap->session; | ||
| 1589 | if (mutex_trylock(&session->s_mutex) == 0) { | ||
| 1590 | dout("inverting session/ino locks on %p\n", | ||
| 1591 | session); | ||
| 1592 | spin_unlock(&inode->i_lock); | ||
| 1593 | if (took_snap_rwsem) { | ||
| 1594 | up_read(&mdsc->snap_rwsem); | ||
| 1595 | took_snap_rwsem = 0; | ||
| 1596 | } | ||
| 1597 | mutex_lock(&session->s_mutex); | ||
| 1598 | goto retry; | ||
| 1599 | } | ||
| 1600 | } | ||
| 1601 | /* take snap_rwsem after session mutex */ | ||
| 1602 | if (!took_snap_rwsem) { | ||
| 1603 | if (down_read_trylock(&mdsc->snap_rwsem) == 0) { | ||
| 1604 | dout("inverting snap/in locks on %p\n", | ||
| 1605 | inode); | ||
| 1606 | spin_unlock(&inode->i_lock); | ||
| 1607 | down_read(&mdsc->snap_rwsem); | ||
| 1608 | took_snap_rwsem = 1; | ||
| 1609 | goto retry; | ||
| 1610 | } | ||
| 1611 | took_snap_rwsem = 1; | ||
| 1612 | } | ||
| 1613 | |||
| 1614 | if (cap == ci->i_auth_cap && ci->i_dirty_caps) | ||
| 1615 | flushing = __mark_caps_flushing(inode, session); | ||
| 1616 | |||
| 1617 | mds = cap->mds; /* remember mds, so we don't repeat */ | ||
| 1618 | sent++; | ||
| 1619 | |||
| 1620 | /* __send_cap drops i_lock */ | ||
| 1621 | delayed += __send_cap(mdsc, cap, CEPH_CAP_OP_UPDATE, used, want, | ||
| 1622 | retain, flushing, NULL); | ||
| 1623 | goto retry; /* retake i_lock and restart our cap scan. */ | ||
| 1624 | } | ||
| 1625 | |||
| 1626 | /* | ||
| 1627 | * Reschedule delayed caps release if we delayed anything, | ||
| 1628 | * otherwise cancel. | ||
| 1629 | */ | ||
| 1630 | if (delayed && is_delayed) | ||
| 1631 | force_requeue = 1; /* __send_cap delayed release; requeue */ | ||
| 1632 | if (!delayed && !is_delayed) | ||
| 1633 | __cap_delay_cancel(mdsc, ci); | ||
| 1634 | else if (!is_delayed || force_requeue) | ||
| 1635 | __cap_delay_requeue(mdsc, ci); | ||
| 1636 | |||
| 1637 | spin_unlock(&inode->i_lock); | ||
| 1638 | |||
| 1639 | if (queue_invalidate) | ||
| 1640 | ceph_queue_invalidate(inode); | ||
| 1641 | |||
| 1642 | if (session) | ||
| 1643 | mutex_unlock(&session->s_mutex); | ||
| 1644 | if (took_snap_rwsem) | ||
| 1645 | up_read(&mdsc->snap_rwsem); | ||
| 1646 | } | ||
| 1647 | |||
| 1648 | /* | ||
| 1649 | * Try to flush dirty caps back to the auth mds. | ||
| 1650 | */ | ||
| 1651 | static int try_flush_caps(struct inode *inode, struct ceph_mds_session *session, | ||
| 1652 | unsigned *flush_tid) | ||
| 1653 | { | ||
| 1654 | struct ceph_mds_client *mdsc = &ceph_client(inode->i_sb)->mdsc; | ||
| 1655 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 1656 | int unlock_session = session ? 0 : 1; | ||
| 1657 | int flushing = 0; | ||
| 1658 | |||
| 1659 | retry: | ||
| 1660 | spin_lock(&inode->i_lock); | ||
| 1661 | if (ci->i_ceph_flags & CEPH_I_NOFLUSH) { | ||
| 1662 | dout("try_flush_caps skipping %p I_NOFLUSH set\n", inode); | ||
| 1663 | goto out; | ||
| 1664 | } | ||
| 1665 | if (ci->i_dirty_caps && ci->i_auth_cap) { | ||
| 1666 | struct ceph_cap *cap = ci->i_auth_cap; | ||
| 1667 | int used = __ceph_caps_used(ci); | ||
| 1668 | int want = __ceph_caps_wanted(ci); | ||
| 1669 | int delayed; | ||
| 1670 | |||
| 1671 | if (!session) { | ||
| 1672 | spin_unlock(&inode->i_lock); | ||
| 1673 | session = cap->session; | ||
| 1674 | mutex_lock(&session->s_mutex); | ||
| 1675 | goto retry; | ||
| 1676 | } | ||
| 1677 | BUG_ON(session != cap->session); | ||
| 1678 | if (cap->session->s_state < CEPH_MDS_SESSION_OPEN) | ||
| 1679 | goto out; | ||
| 1680 | |||
| 1681 | flushing = __mark_caps_flushing(inode, session); | ||
| 1682 | |||
| 1683 | /* __send_cap drops i_lock */ | ||
| 1684 | delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, used, want, | ||
| 1685 | cap->issued | cap->implemented, flushing, | ||
| 1686 | flush_tid); | ||
| 1687 | if (!delayed) | ||
| 1688 | goto out_unlocked; | ||
| 1689 | |||
| 1690 | spin_lock(&inode->i_lock); | ||
| 1691 | __cap_delay_requeue(mdsc, ci); | ||
| 1692 | } | ||
| 1693 | out: | ||
| 1694 | spin_unlock(&inode->i_lock); | ||
| 1695 | out_unlocked: | ||
| 1696 | if (session && unlock_session) | ||
| 1697 | mutex_unlock(&session->s_mutex); | ||
| 1698 | return flushing; | ||
| 1699 | } | ||
| 1700 | |||
| 1701 | /* | ||
| 1702 | * Return true if we've flushed caps through the given flush_tid. | ||
| 1703 | */ | ||
| 1704 | static int caps_are_flushed(struct inode *inode, unsigned tid) | ||
| 1705 | { | ||
| 1706 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 1707 | int dirty, i, ret = 1; | ||
| 1708 | |||
| 1709 | spin_lock(&inode->i_lock); | ||
| 1710 | dirty = __ceph_caps_dirty(ci); | ||
| 1711 | for (i = 0; i < CEPH_CAP_BITS; i++) | ||
| 1712 | if ((ci->i_flushing_caps & (1 << i)) && | ||
| 1713 | ci->i_cap_flush_tid[i] <= tid) { | ||
| 1714 | /* still flushing this bit */ | ||
| 1715 | ret = 0; | ||
| 1716 | break; | ||
| 1717 | } | ||
| 1718 | spin_unlock(&inode->i_lock); | ||
| 1719 | return ret; | ||
| 1720 | } | ||
| 1721 | |||
| 1722 | /* | ||
| 1723 | * Wait on any unsafe replies for the given inode. First wait on the | ||
| 1724 | * newest request, and make that the upper bound. Then, if there are | ||
| 1725 | * more requests, keep waiting on the oldest as long as it is still older | ||
| 1726 | * than the original request. | ||
| 1727 | */ | ||
| 1728 | static void sync_write_wait(struct inode *inode) | ||
| 1729 | { | ||
| 1730 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 1731 | struct list_head *head = &ci->i_unsafe_writes; | ||
| 1732 | struct ceph_osd_request *req; | ||
| 1733 | u64 last_tid; | ||
| 1734 | |||
| 1735 | spin_lock(&ci->i_unsafe_lock); | ||
| 1736 | if (list_empty(head)) | ||
| 1737 | goto out; | ||
| 1738 | |||
| 1739 | /* set upper bound as _last_ entry in chain */ | ||
| 1740 | req = list_entry(head->prev, struct ceph_osd_request, | ||
| 1741 | r_unsafe_item); | ||
| 1742 | last_tid = req->r_tid; | ||
| 1743 | |||
| 1744 | do { | ||
| 1745 | ceph_osdc_get_request(req); | ||
| 1746 | spin_unlock(&ci->i_unsafe_lock); | ||
| 1747 | dout("sync_write_wait on tid %llu (until %llu)\n", | ||
| 1748 | req->r_tid, last_tid); | ||
| 1749 | wait_for_completion(&req->r_safe_completion); | ||
| 1750 | spin_lock(&ci->i_unsafe_lock); | ||
| 1751 | ceph_osdc_put_request(req); | ||
| 1752 | |||
| 1753 | /* | ||
| 1754 | * from here on look at first entry in chain, since we | ||
| 1755 | * only want to wait for anything older than last_tid | ||
| 1756 | */ | ||
| 1757 | if (list_empty(head)) | ||
| 1758 | break; | ||
| 1759 | req = list_entry(head->next, struct ceph_osd_request, | ||
| 1760 | r_unsafe_item); | ||
| 1761 | } while (req->r_tid < last_tid); | ||
| 1762 | out: | ||
| 1763 | spin_unlock(&ci->i_unsafe_lock); | ||
| 1764 | } | ||
| 1765 | |||
| 1766 | int ceph_fsync(struct file *file, struct dentry *dentry, int datasync) | ||
| 1767 | { | ||
| 1768 | struct inode *inode = dentry->d_inode; | ||
| 1769 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 1770 | unsigned flush_tid; | ||
| 1771 | int ret; | ||
| 1772 | int dirty; | ||
| 1773 | |||
| 1774 | dout("fsync %p%s\n", inode, datasync ? " datasync" : ""); | ||
| 1775 | sync_write_wait(inode); | ||
| 1776 | |||
| 1777 | ret = filemap_write_and_wait(inode->i_mapping); | ||
| 1778 | if (ret < 0) | ||
| 1779 | return ret; | ||
| 1780 | |||
| 1781 | dirty = try_flush_caps(inode, NULL, &flush_tid); | ||
| 1782 | dout("fsync dirty caps are %s\n", ceph_cap_string(dirty)); | ||
| 1783 | |||
| 1784 | /* | ||
| 1785 | * only wait on non-file metadata writeback (the mds | ||
| 1786 | * can recover size and mtime, so we don't need to | ||
| 1787 | * wait for that) | ||
| 1788 | */ | ||
| 1789 | if (!datasync && (dirty & ~CEPH_CAP_ANY_FILE_WR)) { | ||
| 1790 | dout("fsync waiting for flush_tid %u\n", flush_tid); | ||
| 1791 | ret = wait_event_interruptible(ci->i_cap_wq, | ||
| 1792 | caps_are_flushed(inode, flush_tid)); | ||
| 1793 | } | ||
| 1794 | |||
| 1795 | dout("fsync %p%s done\n", inode, datasync ? " datasync" : ""); | ||
| 1796 | return ret; | ||
| 1797 | } | ||
| 1798 | |||
| 1799 | /* | ||
| 1800 | * Flush any dirty caps back to the mds. If we aren't asked to wait, | ||
| 1801 | * queue inode for flush but don't do so immediately, because we can | ||
| 1802 | * get by with fewer MDS messages if we wait for data writeback to | ||
| 1803 | * complete first. | ||
| 1804 | */ | ||
| 1805 | int ceph_write_inode(struct inode *inode, struct writeback_control *wbc) | ||
| 1806 | { | ||
| 1807 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 1808 | unsigned flush_tid; | ||
| 1809 | int err = 0; | ||
| 1810 | int dirty; | ||
| 1811 | int wait = wbc->sync_mode == WB_SYNC_ALL; | ||
| 1812 | |||
| 1813 | dout("write_inode %p wait=%d\n", inode, wait); | ||
| 1814 | if (wait) { | ||
| 1815 | dirty = try_flush_caps(inode, NULL, &flush_tid); | ||
| 1816 | if (dirty) | ||
| 1817 | err = wait_event_interruptible(ci->i_cap_wq, | ||
| 1818 | caps_are_flushed(inode, flush_tid)); | ||
| 1819 | } else { | ||
| 1820 | struct ceph_mds_client *mdsc = &ceph_client(inode->i_sb)->mdsc; | ||
| 1821 | |||
| 1822 | spin_lock(&inode->i_lock); | ||
| 1823 | if (__ceph_caps_dirty(ci)) | ||
| 1824 | __cap_delay_requeue_front(mdsc, ci); | ||
| 1825 | spin_unlock(&inode->i_lock); | ||
| 1826 | } | ||
| 1827 | return err; | ||
| 1828 | } | ||
| 1829 | |||
| 1830 | /* | ||
| 1831 | * After a recovering MDS goes active, we need to resend any caps | ||
| 1832 | * we were flushing. | ||
| 1833 | * | ||
| 1834 | * Caller holds session->s_mutex. | ||
| 1835 | */ | ||
| 1836 | static void kick_flushing_capsnaps(struct ceph_mds_client *mdsc, | ||
| 1837 | struct ceph_mds_session *session) | ||
| 1838 | { | ||
| 1839 | struct ceph_cap_snap *capsnap; | ||
| 1840 | |||
| 1841 | dout("kick_flushing_capsnaps mds%d\n", session->s_mds); | ||
| 1842 | list_for_each_entry(capsnap, &session->s_cap_snaps_flushing, | ||
| 1843 | flushing_item) { | ||
| 1844 | struct ceph_inode_info *ci = capsnap->ci; | ||
| 1845 | struct inode *inode = &ci->vfs_inode; | ||
| 1846 | struct ceph_cap *cap; | ||
| 1847 | |||
| 1848 | spin_lock(&inode->i_lock); | ||
| 1849 | cap = ci->i_auth_cap; | ||
| 1850 | if (cap && cap->session == session) { | ||
| 1851 | dout("kick_flushing_caps %p cap %p capsnap %p\n", inode, | ||
| 1852 | cap, capsnap); | ||
| 1853 | __ceph_flush_snaps(ci, &session); | ||
| 1854 | } else { | ||
| 1855 | pr_err("%p auth cap %p not mds%d ???\n", inode, | ||
| 1856 | cap, session->s_mds); | ||
| 1857 | spin_unlock(&inode->i_lock); | ||
| 1858 | } | ||
| 1859 | } | ||
| 1860 | } | ||
| 1861 | |||
| 1862 | void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc, | ||
| 1863 | struct ceph_mds_session *session) | ||
| 1864 | { | ||
| 1865 | struct ceph_inode_info *ci; | ||
| 1866 | |||
| 1867 | kick_flushing_capsnaps(mdsc, session); | ||
| 1868 | |||
| 1869 | dout("kick_flushing_caps mds%d\n", session->s_mds); | ||
| 1870 | list_for_each_entry(ci, &session->s_cap_flushing, i_flushing_item) { | ||
| 1871 | struct inode *inode = &ci->vfs_inode; | ||
| 1872 | struct ceph_cap *cap; | ||
| 1873 | int delayed = 0; | ||
| 1874 | |||
| 1875 | spin_lock(&inode->i_lock); | ||
| 1876 | cap = ci->i_auth_cap; | ||
| 1877 | if (cap && cap->session == session) { | ||
| 1878 | dout("kick_flushing_caps %p cap %p %s\n", inode, | ||
| 1879 | cap, ceph_cap_string(ci->i_flushing_caps)); | ||
| 1880 | delayed = __send_cap(mdsc, cap, CEPH_CAP_OP_FLUSH, | ||
| 1881 | __ceph_caps_used(ci), | ||
| 1882 | __ceph_caps_wanted(ci), | ||
| 1883 | cap->issued | cap->implemented, | ||
| 1884 | ci->i_flushing_caps, NULL); | ||
| 1885 | if (delayed) { | ||
| 1886 | spin_lock(&inode->i_lock); | ||
| 1887 | __cap_delay_requeue(mdsc, ci); | ||
| 1888 | spin_unlock(&inode->i_lock); | ||
| 1889 | } | ||
| 1890 | } else { | ||
| 1891 | pr_err("%p auth cap %p not mds%d ???\n", inode, | ||
| 1892 | cap, session->s_mds); | ||
| 1893 | spin_unlock(&inode->i_lock); | ||
| 1894 | } | ||
| 1895 | } | ||
| 1896 | } | ||
| 1897 | |||
| 1898 | |||
| 1899 | /* | ||
| 1900 | * Take references to capabilities we hold, so that we don't release | ||
| 1901 | * them to the MDS prematurely. | ||
| 1902 | * | ||
| 1903 | * Protected by i_lock. | ||
| 1904 | */ | ||
| 1905 | static void __take_cap_refs(struct ceph_inode_info *ci, int got) | ||
| 1906 | { | ||
| 1907 | if (got & CEPH_CAP_PIN) | ||
| 1908 | ci->i_pin_ref++; | ||
| 1909 | if (got & CEPH_CAP_FILE_RD) | ||
| 1910 | ci->i_rd_ref++; | ||
| 1911 | if (got & CEPH_CAP_FILE_CACHE) | ||
| 1912 | ci->i_rdcache_ref++; | ||
| 1913 | if (got & CEPH_CAP_FILE_WR) | ||
| 1914 | ci->i_wr_ref++; | ||
| 1915 | if (got & CEPH_CAP_FILE_BUFFER) { | ||
| 1916 | if (ci->i_wrbuffer_ref == 0) | ||
| 1917 | igrab(&ci->vfs_inode); | ||
| 1918 | ci->i_wrbuffer_ref++; | ||
| 1919 | dout("__take_cap_refs %p wrbuffer %d -> %d (?)\n", | ||
| 1920 | &ci->vfs_inode, ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref); | ||
| 1921 | } | ||
| 1922 | } | ||
| 1923 | |||
| 1924 | /* | ||
| 1925 | * Try to grab cap references. Specify those refs we @want, and the | ||
| 1926 | * minimal set we @need. Also include the larger offset we are writing | ||
| 1927 | * to (when applicable), and check against max_size here as well. | ||
| 1928 | * Note that caller is responsible for ensuring max_size increases are | ||
| 1929 | * requested from the MDS. | ||
| 1930 | */ | ||
| 1931 | static int try_get_cap_refs(struct ceph_inode_info *ci, int need, int want, | ||
| 1932 | int *got, loff_t endoff, int *check_max, int *err) | ||
| 1933 | { | ||
| 1934 | struct inode *inode = &ci->vfs_inode; | ||
| 1935 | int ret = 0; | ||
| 1936 | int have, implemented; | ||
| 1937 | int file_wanted; | ||
| 1938 | |||
| 1939 | dout("get_cap_refs %p need %s want %s\n", inode, | ||
| 1940 | ceph_cap_string(need), ceph_cap_string(want)); | ||
| 1941 | spin_lock(&inode->i_lock); | ||
| 1942 | |||
| 1943 | /* make sure file is actually open */ | ||
| 1944 | file_wanted = __ceph_caps_file_wanted(ci); | ||
| 1945 | if ((file_wanted & need) == 0) { | ||
| 1946 | dout("try_get_cap_refs need %s file_wanted %s, EBADF\n", | ||
| 1947 | ceph_cap_string(need), ceph_cap_string(file_wanted)); | ||
| 1948 | *err = -EBADF; | ||
| 1949 | ret = 1; | ||
| 1950 | goto out; | ||
| 1951 | } | ||
| 1952 | |||
| 1953 | if (need & CEPH_CAP_FILE_WR) { | ||
| 1954 | if (endoff >= 0 && endoff > (loff_t)ci->i_max_size) { | ||
| 1955 | dout("get_cap_refs %p endoff %llu > maxsize %llu\n", | ||
| 1956 | inode, endoff, ci->i_max_size); | ||
| 1957 | if (endoff > ci->i_wanted_max_size) { | ||
| 1958 | *check_max = 1; | ||
| 1959 | ret = 1; | ||
| 1960 | } | ||
| 1961 | goto out; | ||
| 1962 | } | ||
| 1963 | /* | ||
| 1964 | * If a sync write is in progress, we must wait, so that we | ||
| 1965 | * can get a final snapshot value for size+mtime. | ||
| 1966 | */ | ||
| 1967 | if (__ceph_have_pending_cap_snap(ci)) { | ||
| 1968 | dout("get_cap_refs %p cap_snap_pending\n", inode); | ||
| 1969 | goto out; | ||
| 1970 | } | ||
| 1971 | } | ||
| 1972 | have = __ceph_caps_issued(ci, &implemented); | ||
| 1973 | |||
| 1974 | /* | ||
| 1975 | * disallow writes while a truncate is pending | ||
| 1976 | */ | ||
| 1977 | if (ci->i_truncate_pending) | ||
| 1978 | have &= ~CEPH_CAP_FILE_WR; | ||
| 1979 | |||
| 1980 | if ((have & need) == need) { | ||
| 1981 | /* | ||
| 1982 | * Look at (implemented & ~have & not) so that we keep waiting | ||
| 1983 | * on transition from wanted -> needed caps. This is needed | ||
| 1984 | * for WRBUFFER|WR -> WR to avoid a new WR sync write from | ||
| 1985 | * going before a prior buffered writeback happens. | ||
| 1986 | */ | ||
| 1987 | int not = want & ~(have & need); | ||
| 1988 | int revoking = implemented & ~have; | ||
| 1989 | dout("get_cap_refs %p have %s but not %s (revoking %s)\n", | ||
| 1990 | inode, ceph_cap_string(have), ceph_cap_string(not), | ||
| 1991 | ceph_cap_string(revoking)); | ||
| 1992 | if ((revoking & not) == 0) { | ||
| 1993 | *got = need | (have & want); | ||
| 1994 | __take_cap_refs(ci, *got); | ||
| 1995 | ret = 1; | ||
| 1996 | } | ||
| 1997 | } else { | ||
| 1998 | dout("get_cap_refs %p have %s needed %s\n", inode, | ||
| 1999 | ceph_cap_string(have), ceph_cap_string(need)); | ||
| 2000 | } | ||
| 2001 | out: | ||
| 2002 | spin_unlock(&inode->i_lock); | ||
| 2003 | dout("get_cap_refs %p ret %d got %s\n", inode, | ||
| 2004 | ret, ceph_cap_string(*got)); | ||
| 2005 | return ret; | ||
| 2006 | } | ||
| 2007 | |||
| 2008 | /* | ||
| 2009 | * Check the offset we are writing up to against our current | ||
| 2010 | * max_size. If necessary, tell the MDS we want to write to | ||
| 2011 | * a larger offset. | ||
| 2012 | */ | ||
| 2013 | static void check_max_size(struct inode *inode, loff_t endoff) | ||
| 2014 | { | ||
| 2015 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 2016 | int check = 0; | ||
| 2017 | |||
| 2018 | /* do we need to explicitly request a larger max_size? */ | ||
| 2019 | spin_lock(&inode->i_lock); | ||
| 2020 | if ((endoff >= ci->i_max_size || | ||
| 2021 | endoff > (inode->i_size << 1)) && | ||
| 2022 | endoff > ci->i_wanted_max_size) { | ||
| 2023 | dout("write %p at large endoff %llu, req max_size\n", | ||
| 2024 | inode, endoff); | ||
| 2025 | ci->i_wanted_max_size = endoff; | ||
| 2026 | check = 1; | ||
| 2027 | } | ||
| 2028 | spin_unlock(&inode->i_lock); | ||
| 2029 | if (check) | ||
| 2030 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); | ||
| 2031 | } | ||
| 2032 | |||
| 2033 | /* | ||
| 2034 | * Wait for caps, and take cap references. If we can't get a WR cap | ||
| 2035 | * due to a small max_size, make sure we check_max_size (and possibly | ||
| 2036 | * ask the mds) so we don't get hung up indefinitely. | ||
| 2037 | */ | ||
| 2038 | int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, int *got, | ||
| 2039 | loff_t endoff) | ||
| 2040 | { | ||
| 2041 | int check_max, ret, err; | ||
| 2042 | |||
| 2043 | retry: | ||
| 2044 | if (endoff > 0) | ||
| 2045 | check_max_size(&ci->vfs_inode, endoff); | ||
| 2046 | check_max = 0; | ||
| 2047 | err = 0; | ||
| 2048 | ret = wait_event_interruptible(ci->i_cap_wq, | ||
| 2049 | try_get_cap_refs(ci, need, want, | ||
| 2050 | got, endoff, | ||
| 2051 | &check_max, &err)); | ||
| 2052 | if (err) | ||
| 2053 | ret = err; | ||
| 2054 | if (check_max) | ||
| 2055 | goto retry; | ||
| 2056 | return ret; | ||
| 2057 | } | ||
| 2058 | |||
| 2059 | /* | ||
| 2060 | * Take cap refs. Caller must already know we hold at least one ref | ||
| 2061 | * on the caps in question or we don't know this is safe. | ||
| 2062 | */ | ||
| 2063 | void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps) | ||
| 2064 | { | ||
| 2065 | spin_lock(&ci->vfs_inode.i_lock); | ||
| 2066 | __take_cap_refs(ci, caps); | ||
| 2067 | spin_unlock(&ci->vfs_inode.i_lock); | ||
| 2068 | } | ||
| 2069 | |||
| 2070 | /* | ||
| 2071 | * Release cap refs. | ||
| 2072 | * | ||
| 2073 | * If we released the last ref on any given cap, call ceph_check_caps | ||
| 2074 | * to release (or schedule a release). | ||
| 2075 | * | ||
| 2076 | * If we are releasing a WR cap (from a sync write), finalize any affected | ||
| 2077 | * cap_snap, and wake up any waiters. | ||
| 2078 | */ | ||
| 2079 | void ceph_put_cap_refs(struct ceph_inode_info *ci, int had) | ||
| 2080 | { | ||
| 2081 | struct inode *inode = &ci->vfs_inode; | ||
| 2082 | int last = 0, put = 0, flushsnaps = 0, wake = 0; | ||
| 2083 | struct ceph_cap_snap *capsnap; | ||
| 2084 | |||
| 2085 | spin_lock(&inode->i_lock); | ||
| 2086 | if (had & CEPH_CAP_PIN) | ||
| 2087 | --ci->i_pin_ref; | ||
| 2088 | if (had & CEPH_CAP_FILE_RD) | ||
| 2089 | if (--ci->i_rd_ref == 0) | ||
| 2090 | last++; | ||
| 2091 | if (had & CEPH_CAP_FILE_CACHE) | ||
| 2092 | if (--ci->i_rdcache_ref == 0) | ||
| 2093 | last++; | ||
| 2094 | if (had & CEPH_CAP_FILE_BUFFER) { | ||
| 2095 | if (--ci->i_wrbuffer_ref == 0) { | ||
| 2096 | last++; | ||
| 2097 | put++; | ||
| 2098 | } | ||
| 2099 | dout("put_cap_refs %p wrbuffer %d -> %d (?)\n", | ||
| 2100 | inode, ci->i_wrbuffer_ref+1, ci->i_wrbuffer_ref); | ||
| 2101 | } | ||
| 2102 | if (had & CEPH_CAP_FILE_WR) | ||
| 2103 | if (--ci->i_wr_ref == 0) { | ||
| 2104 | last++; | ||
| 2105 | if (!list_empty(&ci->i_cap_snaps)) { | ||
| 2106 | capsnap = list_first_entry(&ci->i_cap_snaps, | ||
| 2107 | struct ceph_cap_snap, | ||
| 2108 | ci_item); | ||
| 2109 | if (capsnap->writing) { | ||
| 2110 | capsnap->writing = 0; | ||
| 2111 | flushsnaps = | ||
| 2112 | __ceph_finish_cap_snap(ci, | ||
| 2113 | capsnap); | ||
| 2114 | wake = 1; | ||
| 2115 | } | ||
| 2116 | } | ||
| 2117 | } | ||
| 2118 | spin_unlock(&inode->i_lock); | ||
| 2119 | |||
| 2120 | dout("put_cap_refs %p had %s %s\n", inode, ceph_cap_string(had), | ||
| 2121 | last ? "last" : ""); | ||
| 2122 | |||
| 2123 | if (last && !flushsnaps) | ||
| 2124 | ceph_check_caps(ci, 0, NULL); | ||
| 2125 | else if (flushsnaps) | ||
| 2126 | ceph_flush_snaps(ci); | ||
| 2127 | if (wake) | ||
| 2128 | wake_up(&ci->i_cap_wq); | ||
| 2129 | if (put) | ||
| 2130 | iput(inode); | ||
| 2131 | } | ||
| 2132 | |||
| 2133 | /* | ||
| 2134 | * Release @nr WRBUFFER refs on dirty pages for the given @snapc snap | ||
| 2135 | * context. Adjust per-snap dirty page accounting as appropriate. | ||
| 2136 | * Once all dirty data for a cap_snap is flushed, flush snapped file | ||
| 2137 | * metadata back to the MDS. If we dropped the last ref, call | ||
| 2138 | * ceph_check_caps. | ||
| 2139 | */ | ||
| 2140 | void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr, | ||
| 2141 | struct ceph_snap_context *snapc) | ||
| 2142 | { | ||
| 2143 | struct inode *inode = &ci->vfs_inode; | ||
| 2144 | int last = 0; | ||
| 2145 | int last_snap = 0; | ||
| 2146 | int found = 0; | ||
| 2147 | struct ceph_cap_snap *capsnap = NULL; | ||
| 2148 | |||
| 2149 | spin_lock(&inode->i_lock); | ||
| 2150 | ci->i_wrbuffer_ref -= nr; | ||
| 2151 | last = !ci->i_wrbuffer_ref; | ||
| 2152 | |||
| 2153 | if (ci->i_head_snapc == snapc) { | ||
| 2154 | ci->i_wrbuffer_ref_head -= nr; | ||
| 2155 | if (!ci->i_wrbuffer_ref_head) { | ||
| 2156 | ceph_put_snap_context(ci->i_head_snapc); | ||
| 2157 | ci->i_head_snapc = NULL; | ||
| 2158 | } | ||
| 2159 | dout("put_wrbuffer_cap_refs on %p head %d/%d -> %d/%d %s\n", | ||
| 2160 | inode, | ||
| 2161 | ci->i_wrbuffer_ref+nr, ci->i_wrbuffer_ref_head+nr, | ||
| 2162 | ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head, | ||
| 2163 | last ? " LAST" : ""); | ||
| 2164 | } else { | ||
| 2165 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { | ||
| 2166 | if (capsnap->context == snapc) { | ||
| 2167 | found = 1; | ||
| 2168 | capsnap->dirty_pages -= nr; | ||
| 2169 | last_snap = !capsnap->dirty_pages; | ||
| 2170 | break; | ||
| 2171 | } | ||
| 2172 | } | ||
| 2173 | BUG_ON(!found); | ||
| 2174 | dout("put_wrbuffer_cap_refs on %p cap_snap %p " | ||
| 2175 | " snap %lld %d/%d -> %d/%d %s%s\n", | ||
| 2176 | inode, capsnap, capsnap->context->seq, | ||
| 2177 | ci->i_wrbuffer_ref+nr, capsnap->dirty_pages + nr, | ||
| 2178 | ci->i_wrbuffer_ref, capsnap->dirty_pages, | ||
| 2179 | last ? " (wrbuffer last)" : "", | ||
| 2180 | last_snap ? " (capsnap last)" : ""); | ||
| 2181 | } | ||
| 2182 | |||
| 2183 | spin_unlock(&inode->i_lock); | ||
| 2184 | |||
| 2185 | if (last) { | ||
| 2186 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); | ||
| 2187 | iput(inode); | ||
| 2188 | } else if (last_snap) { | ||
| 2189 | ceph_flush_snaps(ci); | ||
| 2190 | wake_up(&ci->i_cap_wq); | ||
| 2191 | } | ||
| 2192 | } | ||
| 2193 | |||
| 2194 | /* | ||
| 2195 | * Handle a cap GRANT message from the MDS. (Note that a GRANT may | ||
| 2196 | * actually be a revocation if it specifies a smaller cap set.) | ||
| 2197 | * | ||
| 2198 | * caller holds s_mutex and i_lock, we drop both. | ||
| 2199 | * | ||
| 2200 | * return value: | ||
| 2201 | * 0 - ok | ||
| 2202 | * 1 - check_caps on auth cap only (writeback) | ||
| 2203 | * 2 - check_caps (ack revoke) | ||
| 2204 | */ | ||
| 2205 | static void handle_cap_grant(struct inode *inode, struct ceph_mds_caps *grant, | ||
| 2206 | struct ceph_mds_session *session, | ||
| 2207 | struct ceph_cap *cap, | ||
| 2208 | struct ceph_buffer *xattr_buf) | ||
| 2209 | __releases(inode->i_lock) | ||
| 2210 | __releases(session->s_mutex) | ||
| 2211 | { | ||
| 2212 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 2213 | int mds = session->s_mds; | ||
| 2214 | int seq = le32_to_cpu(grant->seq); | ||
| 2215 | int newcaps = le32_to_cpu(grant->caps); | ||
| 2216 | int issued, implemented, used, wanted, dirty; | ||
| 2217 | u64 size = le64_to_cpu(grant->size); | ||
| 2218 | u64 max_size = le64_to_cpu(grant->max_size); | ||
| 2219 | struct timespec mtime, atime, ctime; | ||
| 2220 | int check_caps = 0; | ||
| 2221 | int wake = 0; | ||
| 2222 | int writeback = 0; | ||
| 2223 | int revoked_rdcache = 0; | ||
| 2224 | int queue_invalidate = 0; | ||
| 2225 | |||
| 2226 | dout("handle_cap_grant inode %p cap %p mds%d seq %d %s\n", | ||
| 2227 | inode, cap, mds, seq, ceph_cap_string(newcaps)); | ||
| 2228 | dout(" size %llu max_size %llu, i_size %llu\n", size, max_size, | ||
| 2229 | inode->i_size); | ||
| 2230 | |||
| 2231 | /* | ||
| 2232 | * If CACHE is being revoked, and we have no dirty buffers, | ||
| 2233 | * try to invalidate (once). (If there are dirty buffers, we | ||
| 2234 | * will invalidate _after_ writeback.) | ||
| 2235 | */ | ||
| 2236 | if (((cap->issued & ~newcaps) & CEPH_CAP_FILE_CACHE) && | ||
| 2237 | !ci->i_wrbuffer_ref) { | ||
| 2238 | if (try_nonblocking_invalidate(inode) == 0) { | ||
| 2239 | revoked_rdcache = 1; | ||
| 2240 | } else { | ||
| 2241 | /* there were locked pages.. invalidate later | ||
| 2242 | in a separate thread. */ | ||
| 2243 | if (ci->i_rdcache_revoking != ci->i_rdcache_gen) { | ||
| 2244 | queue_invalidate = 1; | ||
| 2245 | ci->i_rdcache_revoking = ci->i_rdcache_gen; | ||
| 2246 | } | ||
| 2247 | } | ||
| 2248 | } | ||
| 2249 | |||
| 2250 | /* side effects now are allowed */ | ||
| 2251 | |||
| 2252 | issued = __ceph_caps_issued(ci, &implemented); | ||
| 2253 | issued |= implemented | __ceph_caps_dirty(ci); | ||
| 2254 | |||
| 2255 | cap->cap_gen = session->s_cap_gen; | ||
| 2256 | |||
| 2257 | __check_cap_issue(ci, cap, newcaps); | ||
| 2258 | |||
| 2259 | if ((issued & CEPH_CAP_AUTH_EXCL) == 0) { | ||
| 2260 | inode->i_mode = le32_to_cpu(grant->mode); | ||
| 2261 | inode->i_uid = le32_to_cpu(grant->uid); | ||
| 2262 | inode->i_gid = le32_to_cpu(grant->gid); | ||
| 2263 | dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode, | ||
| 2264 | inode->i_uid, inode->i_gid); | ||
| 2265 | } | ||
| 2266 | |||
| 2267 | if ((issued & CEPH_CAP_LINK_EXCL) == 0) | ||
| 2268 | inode->i_nlink = le32_to_cpu(grant->nlink); | ||
| 2269 | |||
| 2270 | if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && grant->xattr_len) { | ||
| 2271 | int len = le32_to_cpu(grant->xattr_len); | ||
| 2272 | u64 version = le64_to_cpu(grant->xattr_version); | ||
| 2273 | |||
| 2274 | if (version > ci->i_xattrs.version) { | ||
| 2275 | dout(" got new xattrs v%llu on %p len %d\n", | ||
| 2276 | version, inode, len); | ||
| 2277 | if (ci->i_xattrs.blob) | ||
| 2278 | ceph_buffer_put(ci->i_xattrs.blob); | ||
| 2279 | ci->i_xattrs.blob = ceph_buffer_get(xattr_buf); | ||
| 2280 | ci->i_xattrs.version = version; | ||
| 2281 | } | ||
| 2282 | } | ||
| 2283 | |||
| 2284 | /* size/ctime/mtime/atime? */ | ||
| 2285 | ceph_fill_file_size(inode, issued, | ||
| 2286 | le32_to_cpu(grant->truncate_seq), | ||
| 2287 | le64_to_cpu(grant->truncate_size), size); | ||
| 2288 | ceph_decode_timespec(&mtime, &grant->mtime); | ||
| 2289 | ceph_decode_timespec(&atime, &grant->atime); | ||
| 2290 | ceph_decode_timespec(&ctime, &grant->ctime); | ||
| 2291 | ceph_fill_file_time(inode, issued, | ||
| 2292 | le32_to_cpu(grant->time_warp_seq), &ctime, &mtime, | ||
| 2293 | &atime); | ||
| 2294 | |||
| 2295 | /* max size increase? */ | ||
| 2296 | if (max_size != ci->i_max_size) { | ||
| 2297 | dout("max_size %lld -> %llu\n", ci->i_max_size, max_size); | ||
| 2298 | ci->i_max_size = max_size; | ||
| 2299 | if (max_size >= ci->i_wanted_max_size) { | ||
| 2300 | ci->i_wanted_max_size = 0; /* reset */ | ||
| 2301 | ci->i_requested_max_size = 0; | ||
| 2302 | } | ||
| 2303 | wake = 1; | ||
| 2304 | } | ||
| 2305 | |||
| 2306 | /* check cap bits */ | ||
| 2307 | wanted = __ceph_caps_wanted(ci); | ||
| 2308 | used = __ceph_caps_used(ci); | ||
| 2309 | dirty = __ceph_caps_dirty(ci); | ||
| 2310 | dout(" my wanted = %s, used = %s, dirty %s\n", | ||
| 2311 | ceph_cap_string(wanted), | ||
| 2312 | ceph_cap_string(used), | ||
| 2313 | ceph_cap_string(dirty)); | ||
| 2314 | if (wanted != le32_to_cpu(grant->wanted)) { | ||
| 2315 | dout("mds wanted %s -> %s\n", | ||
| 2316 | ceph_cap_string(le32_to_cpu(grant->wanted)), | ||
| 2317 | ceph_cap_string(wanted)); | ||
| 2318 | grant->wanted = cpu_to_le32(wanted); | ||
| 2319 | } | ||
| 2320 | |||
| 2321 | cap->seq = seq; | ||
| 2322 | |||
| 2323 | /* file layout may have changed */ | ||
| 2324 | ci->i_layout = grant->layout; | ||
| 2325 | |||
| 2326 | /* revocation, grant, or no-op? */ | ||
| 2327 | if (cap->issued & ~newcaps) { | ||
| 2328 | dout("revocation: %s -> %s\n", ceph_cap_string(cap->issued), | ||
| 2329 | ceph_cap_string(newcaps)); | ||
| 2330 | if ((used & ~newcaps) & CEPH_CAP_FILE_BUFFER) | ||
| 2331 | writeback = 1; /* will delay ack */ | ||
| 2332 | else if (dirty & ~newcaps) | ||
| 2333 | check_caps = 1; /* initiate writeback in check_caps */ | ||
| 2334 | else if (((used & ~newcaps) & CEPH_CAP_FILE_CACHE) == 0 || | ||
| 2335 | revoked_rdcache) | ||
| 2336 | check_caps = 2; /* send revoke ack in check_caps */ | ||
| 2337 | cap->issued = newcaps; | ||
| 2338 | cap->implemented |= newcaps; | ||
| 2339 | } else if (cap->issued == newcaps) { | ||
| 2340 | dout("caps unchanged: %s -> %s\n", | ||
| 2341 | ceph_cap_string(cap->issued), ceph_cap_string(newcaps)); | ||
| 2342 | } else { | ||
| 2343 | dout("grant: %s -> %s\n", ceph_cap_string(cap->issued), | ||
| 2344 | ceph_cap_string(newcaps)); | ||
| 2345 | cap->issued = newcaps; | ||
| 2346 | cap->implemented |= newcaps; /* add bits only, to | ||
| 2347 | * avoid stepping on a | ||
| 2348 | * pending revocation */ | ||
| 2349 | wake = 1; | ||
| 2350 | } | ||
| 2351 | BUG_ON(cap->issued & ~cap->implemented); | ||
| 2352 | |||
| 2353 | spin_unlock(&inode->i_lock); | ||
| 2354 | if (writeback) | ||
| 2355 | /* | ||
| 2356 | * queue inode for writeback: we can't actually call | ||
| 2357 | * filemap_write_and_wait, etc. from message handler | ||
| 2358 | * context. | ||
| 2359 | */ | ||
| 2360 | ceph_queue_writeback(inode); | ||
| 2361 | if (queue_invalidate) | ||
| 2362 | ceph_queue_invalidate(inode); | ||
| 2363 | if (wake) | ||
| 2364 | wake_up(&ci->i_cap_wq); | ||
| 2365 | |||
| 2366 | if (check_caps == 1) | ||
| 2367 | ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_AUTHONLY, | ||
| 2368 | session); | ||
| 2369 | else if (check_caps == 2) | ||
| 2370 | ceph_check_caps(ci, CHECK_CAPS_NODELAY, session); | ||
| 2371 | else | ||
| 2372 | mutex_unlock(&session->s_mutex); | ||
| 2373 | } | ||
| 2374 | |||
| 2375 | /* | ||
| 2376 | * Handle FLUSH_ACK from MDS, indicating that metadata we sent to the | ||
| 2377 | * MDS has been safely committed. | ||
| 2378 | */ | ||
| 2379 | static void handle_cap_flush_ack(struct inode *inode, u64 flush_tid, | ||
| 2380 | struct ceph_mds_caps *m, | ||
| 2381 | struct ceph_mds_session *session, | ||
| 2382 | struct ceph_cap *cap) | ||
| 2383 | __releases(inode->i_lock) | ||
| 2384 | { | ||
| 2385 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 2386 | struct ceph_mds_client *mdsc = &ceph_client(inode->i_sb)->mdsc; | ||
| 2387 | unsigned seq = le32_to_cpu(m->seq); | ||
| 2388 | int dirty = le32_to_cpu(m->dirty); | ||
| 2389 | int cleaned = 0; | ||
| 2390 | int drop = 0; | ||
| 2391 | int i; | ||
| 2392 | |||
| 2393 | for (i = 0; i < CEPH_CAP_BITS; i++) | ||
| 2394 | if ((dirty & (1 << i)) && | ||
| 2395 | flush_tid == ci->i_cap_flush_tid[i]) | ||
| 2396 | cleaned |= 1 << i; | ||
| 2397 | |||
| 2398 | dout("handle_cap_flush_ack inode %p mds%d seq %d on %s cleaned %s," | ||
| 2399 | " flushing %s -> %s\n", | ||
| 2400 | inode, session->s_mds, seq, ceph_cap_string(dirty), | ||
| 2401 | ceph_cap_string(cleaned), ceph_cap_string(ci->i_flushing_caps), | ||
| 2402 | ceph_cap_string(ci->i_flushing_caps & ~cleaned)); | ||
| 2403 | |||
| 2404 | if (ci->i_flushing_caps == (ci->i_flushing_caps & ~cleaned)) | ||
| 2405 | goto out; | ||
| 2406 | |||
| 2407 | ci->i_flushing_caps &= ~cleaned; | ||
| 2408 | |||
| 2409 | spin_lock(&mdsc->cap_dirty_lock); | ||
| 2410 | if (ci->i_flushing_caps == 0) { | ||
| 2411 | list_del_init(&ci->i_flushing_item); | ||
| 2412 | if (!list_empty(&session->s_cap_flushing)) | ||
| 2413 | dout(" mds%d still flushing cap on %p\n", | ||
| 2414 | session->s_mds, | ||
| 2415 | &list_entry(session->s_cap_flushing.next, | ||
| 2416 | struct ceph_inode_info, | ||
| 2417 | i_flushing_item)->vfs_inode); | ||
| 2418 | mdsc->num_cap_flushing--; | ||
| 2419 | wake_up(&mdsc->cap_flushing_wq); | ||
| 2420 | dout(" inode %p now !flushing\n", inode); | ||
| 2421 | |||
| 2422 | if (ci->i_dirty_caps == 0) { | ||
| 2423 | dout(" inode %p now clean\n", inode); | ||
| 2424 | BUG_ON(!list_empty(&ci->i_dirty_item)); | ||
| 2425 | drop = 1; | ||
| 2426 | } else { | ||
| 2427 | BUG_ON(list_empty(&ci->i_dirty_item)); | ||
| 2428 | } | ||
| 2429 | } | ||
| 2430 | spin_unlock(&mdsc->cap_dirty_lock); | ||
| 2431 | wake_up(&ci->i_cap_wq); | ||
| 2432 | |||
| 2433 | out: | ||
| 2434 | spin_unlock(&inode->i_lock); | ||
| 2435 | if (drop) | ||
| 2436 | iput(inode); | ||
| 2437 | } | ||
| 2438 | |||
| 2439 | /* | ||
| 2440 | * Handle FLUSHSNAP_ACK. MDS has flushed snap data to disk and we can | ||
| 2441 | * throw away our cap_snap. | ||
| 2442 | * | ||
| 2443 | * Caller hold s_mutex. | ||
| 2444 | */ | ||
| 2445 | static void handle_cap_flushsnap_ack(struct inode *inode, u64 flush_tid, | ||
| 2446 | struct ceph_mds_caps *m, | ||
| 2447 | struct ceph_mds_session *session) | ||
| 2448 | { | ||
| 2449 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 2450 | u64 follows = le64_to_cpu(m->snap_follows); | ||
| 2451 | struct ceph_cap_snap *capsnap; | ||
| 2452 | int drop = 0; | ||
| 2453 | |||
| 2454 | dout("handle_cap_flushsnap_ack inode %p ci %p mds%d follows %lld\n", | ||
| 2455 | inode, ci, session->s_mds, follows); | ||
| 2456 | |||
| 2457 | spin_lock(&inode->i_lock); | ||
| 2458 | list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) { | ||
| 2459 | if (capsnap->follows == follows) { | ||
| 2460 | if (capsnap->flush_tid != flush_tid) { | ||
| 2461 | dout(" cap_snap %p follows %lld tid %lld !=" | ||
| 2462 | " %lld\n", capsnap, follows, | ||
| 2463 | flush_tid, capsnap->flush_tid); | ||
| 2464 | break; | ||
| 2465 | } | ||
| 2466 | WARN_ON(capsnap->dirty_pages || capsnap->writing); | ||
| 2467 | dout(" removing cap_snap %p follows %lld\n", | ||
| 2468 | capsnap, follows); | ||
| 2469 | ceph_put_snap_context(capsnap->context); | ||
| 2470 | list_del(&capsnap->ci_item); | ||
| 2471 | list_del(&capsnap->flushing_item); | ||
| 2472 | ceph_put_cap_snap(capsnap); | ||
| 2473 | drop = 1; | ||
| 2474 | break; | ||
| 2475 | } else { | ||
| 2476 | dout(" skipping cap_snap %p follows %lld\n", | ||
| 2477 | capsnap, capsnap->follows); | ||
| 2478 | } | ||
| 2479 | } | ||
| 2480 | spin_unlock(&inode->i_lock); | ||
| 2481 | if (drop) | ||
| 2482 | iput(inode); | ||
| 2483 | } | ||
| 2484 | |||
| 2485 | /* | ||
| 2486 | * Handle TRUNC from MDS, indicating file truncation. | ||
| 2487 | * | ||
| 2488 | * caller hold s_mutex. | ||
| 2489 | */ | ||
| 2490 | static void handle_cap_trunc(struct inode *inode, | ||
| 2491 | struct ceph_mds_caps *trunc, | ||
| 2492 | struct ceph_mds_session *session) | ||
| 2493 | __releases(inode->i_lock) | ||
| 2494 | { | ||
| 2495 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 2496 | int mds = session->s_mds; | ||
| 2497 | int seq = le32_to_cpu(trunc->seq); | ||
| 2498 | u32 truncate_seq = le32_to_cpu(trunc->truncate_seq); | ||
| 2499 | u64 truncate_size = le64_to_cpu(trunc->truncate_size); | ||
| 2500 | u64 size = le64_to_cpu(trunc->size); | ||
| 2501 | int implemented = 0; | ||
| 2502 | int dirty = __ceph_caps_dirty(ci); | ||
| 2503 | int issued = __ceph_caps_issued(ceph_inode(inode), &implemented); | ||
| 2504 | int queue_trunc = 0; | ||
| 2505 | |||
| 2506 | issued |= implemented | dirty; | ||
| 2507 | |||
| 2508 | dout("handle_cap_trunc inode %p mds%d seq %d to %lld seq %d\n", | ||
| 2509 | inode, mds, seq, truncate_size, truncate_seq); | ||
| 2510 | queue_trunc = ceph_fill_file_size(inode, issued, | ||
| 2511 | truncate_seq, truncate_size, size); | ||
| 2512 | spin_unlock(&inode->i_lock); | ||
| 2513 | |||
| 2514 | if (queue_trunc) | ||
| 2515 | ceph_queue_vmtruncate(inode); | ||
| 2516 | } | ||
| 2517 | |||
| 2518 | /* | ||
| 2519 | * Handle EXPORT from MDS. Cap is being migrated _from_ this mds to a | ||
| 2520 | * different one. If we are the most recent migration we've seen (as | ||
| 2521 | * indicated by mseq), make note of the migrating cap bits for the | ||
| 2522 | * duration (until we see the corresponding IMPORT). | ||
| 2523 | * | ||
| 2524 | * caller holds s_mutex | ||
| 2525 | */ | ||
| 2526 | static void handle_cap_export(struct inode *inode, struct ceph_mds_caps *ex, | ||
| 2527 | struct ceph_mds_session *session) | ||
| 2528 | { | ||
| 2529 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 2530 | int mds = session->s_mds; | ||
| 2531 | unsigned mseq = le32_to_cpu(ex->migrate_seq); | ||
| 2532 | struct ceph_cap *cap = NULL, *t; | ||
| 2533 | struct rb_node *p; | ||
| 2534 | int remember = 1; | ||
| 2535 | |||
| 2536 | dout("handle_cap_export inode %p ci %p mds%d mseq %d\n", | ||
| 2537 | inode, ci, mds, mseq); | ||
| 2538 | |||
| 2539 | spin_lock(&inode->i_lock); | ||
| 2540 | |||
| 2541 | /* make sure we haven't seen a higher mseq */ | ||
| 2542 | for (p = rb_first(&ci->i_caps); p; p = rb_next(p)) { | ||
| 2543 | t = rb_entry(p, struct ceph_cap, ci_node); | ||
| 2544 | if (ceph_seq_cmp(t->mseq, mseq) > 0) { | ||
| 2545 | dout(" higher mseq on cap from mds%d\n", | ||
| 2546 | t->session->s_mds); | ||
| 2547 | remember = 0; | ||
| 2548 | } | ||
| 2549 | if (t->session->s_mds == mds) | ||
| 2550 | cap = t; | ||
| 2551 | } | ||
| 2552 | |||
| 2553 | if (cap) { | ||
| 2554 | if (remember) { | ||
| 2555 | /* make note */ | ||
| 2556 | ci->i_cap_exporting_mds = mds; | ||
| 2557 | ci->i_cap_exporting_mseq = mseq; | ||
| 2558 | ci->i_cap_exporting_issued = cap->issued; | ||
| 2559 | } | ||
| 2560 | __ceph_remove_cap(cap); | ||
| 2561 | } | ||
| 2562 | /* else, we already released it */ | ||
| 2563 | |||
| 2564 | spin_unlock(&inode->i_lock); | ||
| 2565 | } | ||
| 2566 | |||
| 2567 | /* | ||
| 2568 | * Handle cap IMPORT. If there are temp bits from an older EXPORT, | ||
| 2569 | * clean them up. | ||
| 2570 | * | ||
| 2571 | * caller holds s_mutex. | ||
| 2572 | */ | ||
| 2573 | static void handle_cap_import(struct ceph_mds_client *mdsc, | ||
| 2574 | struct inode *inode, struct ceph_mds_caps *im, | ||
| 2575 | struct ceph_mds_session *session, | ||
| 2576 | void *snaptrace, int snaptrace_len) | ||
| 2577 | { | ||
| 2578 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 2579 | int mds = session->s_mds; | ||
| 2580 | unsigned issued = le32_to_cpu(im->caps); | ||
| 2581 | unsigned wanted = le32_to_cpu(im->wanted); | ||
| 2582 | unsigned seq = le32_to_cpu(im->seq); | ||
| 2583 | unsigned mseq = le32_to_cpu(im->migrate_seq); | ||
| 2584 | u64 realmino = le64_to_cpu(im->realm); | ||
| 2585 | u64 cap_id = le64_to_cpu(im->cap_id); | ||
| 2586 | |||
| 2587 | if (ci->i_cap_exporting_mds >= 0 && | ||
| 2588 | ceph_seq_cmp(ci->i_cap_exporting_mseq, mseq) < 0) { | ||
| 2589 | dout("handle_cap_import inode %p ci %p mds%d mseq %d" | ||
| 2590 | " - cleared exporting from mds%d\n", | ||
| 2591 | inode, ci, mds, mseq, | ||
| 2592 | ci->i_cap_exporting_mds); | ||
| 2593 | ci->i_cap_exporting_issued = 0; | ||
| 2594 | ci->i_cap_exporting_mseq = 0; | ||
| 2595 | ci->i_cap_exporting_mds = -1; | ||
| 2596 | } else { | ||
| 2597 | dout("handle_cap_import inode %p ci %p mds%d mseq %d\n", | ||
| 2598 | inode, ci, mds, mseq); | ||
| 2599 | } | ||
| 2600 | |||
| 2601 | down_write(&mdsc->snap_rwsem); | ||
| 2602 | ceph_update_snap_trace(mdsc, snaptrace, snaptrace+snaptrace_len, | ||
| 2603 | false); | ||
| 2604 | downgrade_write(&mdsc->snap_rwsem); | ||
| 2605 | ceph_add_cap(inode, session, cap_id, -1, | ||
| 2606 | issued, wanted, seq, mseq, realmino, CEPH_CAP_FLAG_AUTH, | ||
| 2607 | NULL /* no caps context */); | ||
| 2608 | try_flush_caps(inode, session, NULL); | ||
| 2609 | up_read(&mdsc->snap_rwsem); | ||
| 2610 | } | ||
| 2611 | |||
| 2612 | /* | ||
| 2613 | * Handle a caps message from the MDS. | ||
| 2614 | * | ||
| 2615 | * Identify the appropriate session, inode, and call the right handler | ||
| 2616 | * based on the cap op. | ||
| 2617 | */ | ||
| 2618 | void ceph_handle_caps(struct ceph_mds_session *session, | ||
| 2619 | struct ceph_msg *msg) | ||
| 2620 | { | ||
| 2621 | struct ceph_mds_client *mdsc = session->s_mdsc; | ||
| 2622 | struct super_block *sb = mdsc->client->sb; | ||
| 2623 | struct inode *inode; | ||
| 2624 | struct ceph_cap *cap; | ||
| 2625 | struct ceph_mds_caps *h; | ||
| 2626 | int mds = session->s_mds; | ||
| 2627 | int op; | ||
| 2628 | u32 seq; | ||
| 2629 | struct ceph_vino vino; | ||
| 2630 | u64 cap_id; | ||
| 2631 | u64 size, max_size; | ||
| 2632 | u64 tid; | ||
| 2633 | void *snaptrace; | ||
| 2634 | |||
| 2635 | dout("handle_caps from mds%d\n", mds); | ||
| 2636 | |||
| 2637 | /* decode */ | ||
| 2638 | tid = le64_to_cpu(msg->hdr.tid); | ||
| 2639 | if (msg->front.iov_len < sizeof(*h)) | ||
| 2640 | goto bad; | ||
| 2641 | h = msg->front.iov_base; | ||
| 2642 | snaptrace = h + 1; | ||
| 2643 | op = le32_to_cpu(h->op); | ||
| 2644 | vino.ino = le64_to_cpu(h->ino); | ||
| 2645 | vino.snap = CEPH_NOSNAP; | ||
| 2646 | cap_id = le64_to_cpu(h->cap_id); | ||
| 2647 | seq = le32_to_cpu(h->seq); | ||
| 2648 | size = le64_to_cpu(h->size); | ||
| 2649 | max_size = le64_to_cpu(h->max_size); | ||
| 2650 | |||
| 2651 | mutex_lock(&session->s_mutex); | ||
| 2652 | session->s_seq++; | ||
| 2653 | dout(" mds%d seq %lld cap seq %u\n", session->s_mds, session->s_seq, | ||
| 2654 | (unsigned)seq); | ||
| 2655 | |||
| 2656 | /* lookup ino */ | ||
| 2657 | inode = ceph_find_inode(sb, vino); | ||
| 2658 | dout(" op %s ino %llx.%llx inode %p\n", ceph_cap_op_name(op), vino.ino, | ||
| 2659 | vino.snap, inode); | ||
| 2660 | if (!inode) { | ||
| 2661 | dout(" i don't have ino %llx\n", vino.ino); | ||
| 2662 | goto done; | ||
| 2663 | } | ||
| 2664 | |||
| 2665 | /* these will work even if we don't have a cap yet */ | ||
| 2666 | switch (op) { | ||
| 2667 | case CEPH_CAP_OP_FLUSHSNAP_ACK: | ||
| 2668 | handle_cap_flushsnap_ack(inode, tid, h, session); | ||
| 2669 | goto done; | ||
| 2670 | |||
| 2671 | case CEPH_CAP_OP_EXPORT: | ||
| 2672 | handle_cap_export(inode, h, session); | ||
| 2673 | goto done; | ||
| 2674 | |||
| 2675 | case CEPH_CAP_OP_IMPORT: | ||
| 2676 | handle_cap_import(mdsc, inode, h, session, | ||
| 2677 | snaptrace, le32_to_cpu(h->snap_trace_len)); | ||
| 2678 | ceph_check_caps(ceph_inode(inode), CHECK_CAPS_NODELAY, | ||
| 2679 | session); | ||
| 2680 | goto done_unlocked; | ||
| 2681 | } | ||
| 2682 | |||
| 2683 | /* the rest require a cap */ | ||
| 2684 | spin_lock(&inode->i_lock); | ||
| 2685 | cap = __get_cap_for_mds(ceph_inode(inode), mds); | ||
| 2686 | if (!cap) { | ||
| 2687 | dout("no cap on %p ino %llx.%llx from mds%d, releasing\n", | ||
| 2688 | inode, ceph_ino(inode), ceph_snap(inode), mds); | ||
| 2689 | spin_unlock(&inode->i_lock); | ||
| 2690 | goto done; | ||
| 2691 | } | ||
| 2692 | |||
| 2693 | /* note that each of these drops i_lock for us */ | ||
| 2694 | switch (op) { | ||
| 2695 | case CEPH_CAP_OP_REVOKE: | ||
| 2696 | case CEPH_CAP_OP_GRANT: | ||
| 2697 | handle_cap_grant(inode, h, session, cap, msg->middle); | ||
| 2698 | goto done_unlocked; | ||
| 2699 | |||
| 2700 | case CEPH_CAP_OP_FLUSH_ACK: | ||
| 2701 | handle_cap_flush_ack(inode, tid, h, session, cap); | ||
| 2702 | break; | ||
| 2703 | |||
| 2704 | case CEPH_CAP_OP_TRUNC: | ||
| 2705 | handle_cap_trunc(inode, h, session); | ||
| 2706 | break; | ||
| 2707 | |||
| 2708 | default: | ||
| 2709 | spin_unlock(&inode->i_lock); | ||
| 2710 | pr_err("ceph_handle_caps: unknown cap op %d %s\n", op, | ||
| 2711 | ceph_cap_op_name(op)); | ||
| 2712 | } | ||
| 2713 | |||
| 2714 | done: | ||
| 2715 | mutex_unlock(&session->s_mutex); | ||
| 2716 | done_unlocked: | ||
| 2717 | if (inode) | ||
| 2718 | iput(inode); | ||
| 2719 | return; | ||
| 2720 | |||
| 2721 | bad: | ||
| 2722 | pr_err("ceph_handle_caps: corrupt message\n"); | ||
| 2723 | ceph_msg_dump(msg); | ||
| 2724 | return; | ||
| 2725 | } | ||
| 2726 | |||
| 2727 | /* | ||
| 2728 | * Delayed work handler to process end of delayed cap release LRU list. | ||
| 2729 | */ | ||
| 2730 | void ceph_check_delayed_caps(struct ceph_mds_client *mdsc) | ||
| 2731 | { | ||
| 2732 | struct ceph_inode_info *ci; | ||
| 2733 | int flags = CHECK_CAPS_NODELAY; | ||
| 2734 | |||
| 2735 | dout("check_delayed_caps\n"); | ||
| 2736 | while (1) { | ||
| 2737 | spin_lock(&mdsc->cap_delay_lock); | ||
| 2738 | if (list_empty(&mdsc->cap_delay_list)) | ||
| 2739 | break; | ||
| 2740 | ci = list_first_entry(&mdsc->cap_delay_list, | ||
| 2741 | struct ceph_inode_info, | ||
| 2742 | i_cap_delay_list); | ||
| 2743 | if ((ci->i_ceph_flags & CEPH_I_FLUSH) == 0 && | ||
| 2744 | time_before(jiffies, ci->i_hold_caps_max)) | ||
| 2745 | break; | ||
| 2746 | list_del_init(&ci->i_cap_delay_list); | ||
| 2747 | spin_unlock(&mdsc->cap_delay_lock); | ||
| 2748 | dout("check_delayed_caps on %p\n", &ci->vfs_inode); | ||
| 2749 | ceph_check_caps(ci, flags, NULL); | ||
| 2750 | } | ||
| 2751 | spin_unlock(&mdsc->cap_delay_lock); | ||
| 2752 | } | ||
| 2753 | |||
| 2754 | /* | ||
| 2755 | * Flush all dirty caps to the mds | ||
| 2756 | */ | ||
| 2757 | void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc) | ||
| 2758 | { | ||
| 2759 | struct ceph_inode_info *ci, *nci = NULL; | ||
| 2760 | struct inode *inode, *ninode = NULL; | ||
| 2761 | struct list_head *p, *n; | ||
| 2762 | |||
| 2763 | dout("flush_dirty_caps\n"); | ||
| 2764 | spin_lock(&mdsc->cap_dirty_lock); | ||
| 2765 | list_for_each_safe(p, n, &mdsc->cap_dirty) { | ||
| 2766 | if (nci) { | ||
| 2767 | ci = nci; | ||
| 2768 | inode = ninode; | ||
| 2769 | ci->i_ceph_flags &= ~CEPH_I_NOFLUSH; | ||
| 2770 | dout("flush_dirty_caps inode %p (was next inode)\n", | ||
| 2771 | inode); | ||
| 2772 | } else { | ||
| 2773 | ci = list_entry(p, struct ceph_inode_info, | ||
| 2774 | i_dirty_item); | ||
| 2775 | inode = igrab(&ci->vfs_inode); | ||
| 2776 | BUG_ON(!inode); | ||
| 2777 | dout("flush_dirty_caps inode %p\n", inode); | ||
| 2778 | } | ||
| 2779 | if (n != &mdsc->cap_dirty) { | ||
| 2780 | nci = list_entry(n, struct ceph_inode_info, | ||
| 2781 | i_dirty_item); | ||
| 2782 | ninode = igrab(&nci->vfs_inode); | ||
| 2783 | BUG_ON(!ninode); | ||
| 2784 | nci->i_ceph_flags |= CEPH_I_NOFLUSH; | ||
| 2785 | dout("flush_dirty_caps next inode %p, noflush\n", | ||
| 2786 | ninode); | ||
| 2787 | } else { | ||
| 2788 | nci = NULL; | ||
| 2789 | ninode = NULL; | ||
| 2790 | } | ||
| 2791 | spin_unlock(&mdsc->cap_dirty_lock); | ||
| 2792 | if (inode) { | ||
| 2793 | ceph_check_caps(ci, CHECK_CAPS_NODELAY|CHECK_CAPS_FLUSH, | ||
| 2794 | NULL); | ||
| 2795 | iput(inode); | ||
| 2796 | } | ||
| 2797 | spin_lock(&mdsc->cap_dirty_lock); | ||
| 2798 | } | ||
| 2799 | spin_unlock(&mdsc->cap_dirty_lock); | ||
| 2800 | } | ||
| 2801 | |||
| 2802 | /* | ||
| 2803 | * Drop open file reference. If we were the last open file, | ||
| 2804 | * we may need to release capabilities to the MDS (or schedule | ||
| 2805 | * their delayed release). | ||
| 2806 | */ | ||
| 2807 | void ceph_put_fmode(struct ceph_inode_info *ci, int fmode) | ||
| 2808 | { | ||
| 2809 | struct inode *inode = &ci->vfs_inode; | ||
| 2810 | int last = 0; | ||
| 2811 | |||
| 2812 | spin_lock(&inode->i_lock); | ||
| 2813 | dout("put_fmode %p fmode %d %d -> %d\n", inode, fmode, | ||
| 2814 | ci->i_nr_by_mode[fmode], ci->i_nr_by_mode[fmode]-1); | ||
| 2815 | BUG_ON(ci->i_nr_by_mode[fmode] == 0); | ||
| 2816 | if (--ci->i_nr_by_mode[fmode] == 0) | ||
| 2817 | last++; | ||
| 2818 | spin_unlock(&inode->i_lock); | ||
| 2819 | |||
| 2820 | if (last && ci->i_vino.snap == CEPH_NOSNAP) | ||
| 2821 | ceph_check_caps(ci, 0, NULL); | ||
| 2822 | } | ||
| 2823 | |||
| 2824 | /* | ||
| 2825 | * Helpers for embedding cap and dentry lease releases into mds | ||
| 2826 | * requests. | ||
| 2827 | * | ||
| 2828 | * @force is used by dentry_release (below) to force inclusion of a | ||
| 2829 | * record for the directory inode, even when there aren't any caps to | ||
| 2830 | * drop. | ||
| 2831 | */ | ||
| 2832 | int ceph_encode_inode_release(void **p, struct inode *inode, | ||
| 2833 | int mds, int drop, int unless, int force) | ||
| 2834 | { | ||
| 2835 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 2836 | struct ceph_cap *cap; | ||
| 2837 | struct ceph_mds_request_release *rel = *p; | ||
| 2838 | int ret = 0; | ||
| 2839 | int used = 0; | ||
| 2840 | |||
| 2841 | spin_lock(&inode->i_lock); | ||
| 2842 | used = __ceph_caps_used(ci); | ||
| 2843 | |||
| 2844 | dout("encode_inode_release %p mds%d used %s drop %s unless %s\n", inode, | ||
| 2845 | mds, ceph_cap_string(used), ceph_cap_string(drop), | ||
| 2846 | ceph_cap_string(unless)); | ||
| 2847 | |||
| 2848 | /* only drop unused caps */ | ||
| 2849 | drop &= ~used; | ||
| 2850 | |||
| 2851 | cap = __get_cap_for_mds(ci, mds); | ||
| 2852 | if (cap && __cap_is_valid(cap)) { | ||
| 2853 | if (force || | ||
| 2854 | ((cap->issued & drop) && | ||
| 2855 | (cap->issued & unless) == 0)) { | ||
| 2856 | if ((cap->issued & drop) && | ||
| 2857 | (cap->issued & unless) == 0) { | ||
| 2858 | dout("encode_inode_release %p cap %p %s -> " | ||
| 2859 | "%s\n", inode, cap, | ||
| 2860 | ceph_cap_string(cap->issued), | ||
| 2861 | ceph_cap_string(cap->issued & ~drop)); | ||
| 2862 | cap->issued &= ~drop; | ||
| 2863 | cap->implemented &= ~drop; | ||
| 2864 | if (ci->i_ceph_flags & CEPH_I_NODELAY) { | ||
| 2865 | int wanted = __ceph_caps_wanted(ci); | ||
| 2866 | dout(" wanted %s -> %s (act %s)\n", | ||
| 2867 | ceph_cap_string(cap->mds_wanted), | ||
| 2868 | ceph_cap_string(cap->mds_wanted & | ||
| 2869 | ~wanted), | ||
| 2870 | ceph_cap_string(wanted)); | ||
| 2871 | cap->mds_wanted &= wanted; | ||
| 2872 | } | ||
| 2873 | } else { | ||
| 2874 | dout("encode_inode_release %p cap %p %s" | ||
| 2875 | " (force)\n", inode, cap, | ||
| 2876 | ceph_cap_string(cap->issued)); | ||
| 2877 | } | ||
| 2878 | |||
| 2879 | rel->ino = cpu_to_le64(ceph_ino(inode)); | ||
| 2880 | rel->cap_id = cpu_to_le64(cap->cap_id); | ||
| 2881 | rel->seq = cpu_to_le32(cap->seq); | ||
| 2882 | rel->issue_seq = cpu_to_le32(cap->issue_seq), | ||
| 2883 | rel->mseq = cpu_to_le32(cap->mseq); | ||
| 2884 | rel->caps = cpu_to_le32(cap->issued); | ||
| 2885 | rel->wanted = cpu_to_le32(cap->mds_wanted); | ||
| 2886 | rel->dname_len = 0; | ||
| 2887 | rel->dname_seq = 0; | ||
| 2888 | *p += sizeof(*rel); | ||
| 2889 | ret = 1; | ||
| 2890 | } else { | ||
| 2891 | dout("encode_inode_release %p cap %p %s\n", | ||
| 2892 | inode, cap, ceph_cap_string(cap->issued)); | ||
| 2893 | } | ||
| 2894 | } | ||
| 2895 | spin_unlock(&inode->i_lock); | ||
| 2896 | return ret; | ||
| 2897 | } | ||
| 2898 | |||
| 2899 | int ceph_encode_dentry_release(void **p, struct dentry *dentry, | ||
| 2900 | int mds, int drop, int unless) | ||
| 2901 | { | ||
| 2902 | struct inode *dir = dentry->d_parent->d_inode; | ||
| 2903 | struct ceph_mds_request_release *rel = *p; | ||
| 2904 | struct ceph_dentry_info *di = ceph_dentry(dentry); | ||
| 2905 | int force = 0; | ||
| 2906 | int ret; | ||
| 2907 | |||
| 2908 | /* | ||
| 2909 | * force an record for the directory caps if we have a dentry lease. | ||
| 2910 | * this is racy (can't take i_lock and d_lock together), but it | ||
| 2911 | * doesn't have to be perfect; the mds will revoke anything we don't | ||
| 2912 | * release. | ||
| 2913 | */ | ||
| 2914 | spin_lock(&dentry->d_lock); | ||
| 2915 | if (di->lease_session && di->lease_session->s_mds == mds) | ||
| 2916 | force = 1; | ||
| 2917 | spin_unlock(&dentry->d_lock); | ||
| 2918 | |||
| 2919 | ret = ceph_encode_inode_release(p, dir, mds, drop, unless, force); | ||
| 2920 | |||
| 2921 | spin_lock(&dentry->d_lock); | ||
| 2922 | if (ret && di->lease_session && di->lease_session->s_mds == mds) { | ||
| 2923 | dout("encode_dentry_release %p mds%d seq %d\n", | ||
| 2924 | dentry, mds, (int)di->lease_seq); | ||
| 2925 | rel->dname_len = cpu_to_le32(dentry->d_name.len); | ||
| 2926 | memcpy(*p, dentry->d_name.name, dentry->d_name.len); | ||
| 2927 | *p += dentry->d_name.len; | ||
| 2928 | rel->dname_seq = cpu_to_le32(di->lease_seq); | ||
| 2929 | } | ||
| 2930 | spin_unlock(&dentry->d_lock); | ||
| 2931 | return ret; | ||
| 2932 | } | ||
diff --git a/fs/ceph/ceph_debug.h b/fs/ceph/ceph_debug.h new file mode 100644 index 000000000000..1818c2305610 --- /dev/null +++ b/fs/ceph/ceph_debug.h | |||
| @@ -0,0 +1,37 @@ | |||
| 1 | #ifndef _FS_CEPH_DEBUG_H | ||
| 2 | #define _FS_CEPH_DEBUG_H | ||
| 3 | |||
| 4 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt | ||
| 5 | |||
| 6 | #ifdef CONFIG_CEPH_FS_PRETTYDEBUG | ||
| 7 | |||
| 8 | /* | ||
| 9 | * wrap pr_debug to include a filename:lineno prefix on each line. | ||
| 10 | * this incurs some overhead (kernel size and execution time) due to | ||
| 11 | * the extra function call at each call site. | ||
| 12 | */ | ||
| 13 | |||
| 14 | # if defined(DEBUG) || defined(CONFIG_DYNAMIC_DEBUG) | ||
| 15 | extern const char *ceph_file_part(const char *s, int len); | ||
| 16 | # define dout(fmt, ...) \ | ||
| 17 | pr_debug(" %12.12s:%-4d : " fmt, \ | ||
| 18 | ceph_file_part(__FILE__, sizeof(__FILE__)), \ | ||
| 19 | __LINE__, ##__VA_ARGS__) | ||
| 20 | # else | ||
| 21 | /* faux printk call just to see any compiler warnings. */ | ||
| 22 | # define dout(fmt, ...) do { \ | ||
| 23 | if (0) \ | ||
| 24 | printk(KERN_DEBUG fmt, ##__VA_ARGS__); \ | ||
| 25 | } while (0) | ||
| 26 | # endif | ||
| 27 | |||
| 28 | #else | ||
| 29 | |||
| 30 | /* | ||
| 31 | * or, just wrap pr_debug | ||
| 32 | */ | ||
| 33 | # define dout(fmt, ...) pr_debug(" " fmt, ##__VA_ARGS__) | ||
| 34 | |||
| 35 | #endif | ||
| 36 | |||
| 37 | #endif | ||
diff --git a/fs/ceph/ceph_frag.c b/fs/ceph/ceph_frag.c new file mode 100644 index 000000000000..ab6cf35c4091 --- /dev/null +++ b/fs/ceph/ceph_frag.c | |||
| @@ -0,0 +1,21 @@ | |||
| 1 | /* | ||
| 2 | * Ceph 'frag' type | ||
| 3 | */ | ||
| 4 | #include "types.h" | ||
| 5 | |||
| 6 | int ceph_frag_compare(__u32 a, __u32 b) | ||
| 7 | { | ||
| 8 | unsigned va = ceph_frag_value(a); | ||
| 9 | unsigned vb = ceph_frag_value(b); | ||
| 10 | if (va < vb) | ||
| 11 | return -1; | ||
| 12 | if (va > vb) | ||
| 13 | return 1; | ||
| 14 | va = ceph_frag_bits(a); | ||
| 15 | vb = ceph_frag_bits(b); | ||
| 16 | if (va < vb) | ||
| 17 | return -1; | ||
| 18 | if (va > vb) | ||
| 19 | return 1; | ||
| 20 | return 0; | ||
| 21 | } | ||
diff --git a/fs/ceph/ceph_frag.h b/fs/ceph/ceph_frag.h new file mode 100644 index 000000000000..793f50cb7c22 --- /dev/null +++ b/fs/ceph/ceph_frag.h | |||
| @@ -0,0 +1,109 @@ | |||
| 1 | #ifndef _FS_CEPH_FRAG_H | ||
| 2 | #define _FS_CEPH_FRAG_H | ||
| 3 | |||
| 4 | /* | ||
| 5 | * "Frags" are a way to describe a subset of a 32-bit number space, | ||
| 6 | * using a mask and a value to match against that mask. Any given frag | ||
| 7 | * (subset of the number space) can be partitioned into 2^n sub-frags. | ||
| 8 | * | ||
| 9 | * Frags are encoded into a 32-bit word: | ||
| 10 | * 8 upper bits = "bits" | ||
| 11 | * 24 lower bits = "value" | ||
| 12 | * (We could go to 5+27 bits, but who cares.) | ||
| 13 | * | ||
| 14 | * We use the _most_ significant bits of the 24 bit value. This makes | ||
| 15 | * values logically sort. | ||
| 16 | * | ||
| 17 | * Unfortunately, because the "bits" field is still in the high bits, we | ||
| 18 | * can't sort encoded frags numerically. However, it does allow you | ||
| 19 | * to feed encoded frags as values into frag_contains_value. | ||
| 20 | */ | ||
| 21 | static inline __u32 ceph_frag_make(__u32 b, __u32 v) | ||
| 22 | { | ||
| 23 | return (b << 24) | | ||
| 24 | (v & (0xffffffu << (24-b)) & 0xffffffu); | ||
| 25 | } | ||
| 26 | static inline __u32 ceph_frag_bits(__u32 f) | ||
| 27 | { | ||
| 28 | return f >> 24; | ||
| 29 | } | ||
| 30 | static inline __u32 ceph_frag_value(__u32 f) | ||
| 31 | { | ||
| 32 | return f & 0xffffffu; | ||
| 33 | } | ||
| 34 | static inline __u32 ceph_frag_mask(__u32 f) | ||
| 35 | { | ||
| 36 | return (0xffffffu << (24-ceph_frag_bits(f))) & 0xffffffu; | ||
| 37 | } | ||
| 38 | static inline __u32 ceph_frag_mask_shift(__u32 f) | ||
| 39 | { | ||
| 40 | return 24 - ceph_frag_bits(f); | ||
| 41 | } | ||
| 42 | |||
| 43 | static inline int ceph_frag_contains_value(__u32 f, __u32 v) | ||
| 44 | { | ||
| 45 | return (v & ceph_frag_mask(f)) == ceph_frag_value(f); | ||
| 46 | } | ||
| 47 | static inline int ceph_frag_contains_frag(__u32 f, __u32 sub) | ||
| 48 | { | ||
| 49 | /* is sub as specific as us, and contained by us? */ | ||
| 50 | return ceph_frag_bits(sub) >= ceph_frag_bits(f) && | ||
| 51 | (ceph_frag_value(sub) & ceph_frag_mask(f)) == ceph_frag_value(f); | ||
| 52 | } | ||
| 53 | |||
| 54 | static inline __u32 ceph_frag_parent(__u32 f) | ||
| 55 | { | ||
| 56 | return ceph_frag_make(ceph_frag_bits(f) - 1, | ||
| 57 | ceph_frag_value(f) & (ceph_frag_mask(f) << 1)); | ||
| 58 | } | ||
| 59 | static inline int ceph_frag_is_left_child(__u32 f) | ||
| 60 | { | ||
| 61 | return ceph_frag_bits(f) > 0 && | ||
| 62 | (ceph_frag_value(f) & (0x1000000 >> ceph_frag_bits(f))) == 0; | ||
| 63 | } | ||
| 64 | static inline int ceph_frag_is_right_child(__u32 f) | ||
| 65 | { | ||
| 66 | return ceph_frag_bits(f) > 0 && | ||
| 67 | (ceph_frag_value(f) & (0x1000000 >> ceph_frag_bits(f))) == 1; | ||
| 68 | } | ||
| 69 | static inline __u32 ceph_frag_sibling(__u32 f) | ||
| 70 | { | ||
| 71 | return ceph_frag_make(ceph_frag_bits(f), | ||
| 72 | ceph_frag_value(f) ^ (0x1000000 >> ceph_frag_bits(f))); | ||
| 73 | } | ||
| 74 | static inline __u32 ceph_frag_left_child(__u32 f) | ||
| 75 | { | ||
| 76 | return ceph_frag_make(ceph_frag_bits(f)+1, ceph_frag_value(f)); | ||
| 77 | } | ||
| 78 | static inline __u32 ceph_frag_right_child(__u32 f) | ||
| 79 | { | ||
| 80 | return ceph_frag_make(ceph_frag_bits(f)+1, | ||
| 81 | ceph_frag_value(f) | (0x1000000 >> (1+ceph_frag_bits(f)))); | ||
| 82 | } | ||
| 83 | static inline __u32 ceph_frag_make_child(__u32 f, int by, int i) | ||
| 84 | { | ||
| 85 | int newbits = ceph_frag_bits(f) + by; | ||
| 86 | return ceph_frag_make(newbits, | ||
| 87 | ceph_frag_value(f) | (i << (24 - newbits))); | ||
| 88 | } | ||
| 89 | static inline int ceph_frag_is_leftmost(__u32 f) | ||
| 90 | { | ||
| 91 | return ceph_frag_value(f) == 0; | ||
| 92 | } | ||
| 93 | static inline int ceph_frag_is_rightmost(__u32 f) | ||
| 94 | { | ||
| 95 | return ceph_frag_value(f) == ceph_frag_mask(f); | ||
| 96 | } | ||
| 97 | static inline __u32 ceph_frag_next(__u32 f) | ||
| 98 | { | ||
| 99 | return ceph_frag_make(ceph_frag_bits(f), | ||
| 100 | ceph_frag_value(f) + (0x1000000 >> ceph_frag_bits(f))); | ||
| 101 | } | ||
| 102 | |||
| 103 | /* | ||
| 104 | * comparator to sort frags logically, as when traversing the | ||
| 105 | * number space in ascending order... | ||
| 106 | */ | ||
| 107 | int ceph_frag_compare(__u32 a, __u32 b); | ||
| 108 | |||
| 109 | #endif | ||
diff --git a/fs/ceph/ceph_fs.c b/fs/ceph/ceph_fs.c new file mode 100644 index 000000000000..79d76bc4303f --- /dev/null +++ b/fs/ceph/ceph_fs.c | |||
| @@ -0,0 +1,74 @@ | |||
| 1 | /* | ||
| 2 | * Some non-inline ceph helpers | ||
| 3 | */ | ||
| 4 | #include "types.h" | ||
| 5 | |||
| 6 | /* | ||
| 7 | * return true if @layout appears to be valid | ||
| 8 | */ | ||
| 9 | int ceph_file_layout_is_valid(const struct ceph_file_layout *layout) | ||
| 10 | { | ||
| 11 | __u32 su = le32_to_cpu(layout->fl_stripe_unit); | ||
| 12 | __u32 sc = le32_to_cpu(layout->fl_stripe_count); | ||
| 13 | __u32 os = le32_to_cpu(layout->fl_object_size); | ||
| 14 | |||
| 15 | /* stripe unit, object size must be non-zero, 64k increment */ | ||
| 16 | if (!su || (su & (CEPH_MIN_STRIPE_UNIT-1))) | ||
| 17 | return 0; | ||
| 18 | if (!os || (os & (CEPH_MIN_STRIPE_UNIT-1))) | ||
| 19 | return 0; | ||
| 20 | /* object size must be a multiple of stripe unit */ | ||
| 21 | if (os < su || os % su) | ||
| 22 | return 0; | ||
| 23 | /* stripe count must be non-zero */ | ||
| 24 | if (!sc) | ||
| 25 | return 0; | ||
| 26 | return 1; | ||
| 27 | } | ||
| 28 | |||
| 29 | |||
| 30 | int ceph_flags_to_mode(int flags) | ||
| 31 | { | ||
| 32 | #ifdef O_DIRECTORY /* fixme */ | ||
| 33 | if ((flags & O_DIRECTORY) == O_DIRECTORY) | ||
| 34 | return CEPH_FILE_MODE_PIN; | ||
| 35 | #endif | ||
| 36 | #ifdef O_LAZY | ||
| 37 | if (flags & O_LAZY) | ||
| 38 | return CEPH_FILE_MODE_LAZY; | ||
| 39 | #endif | ||
| 40 | if ((flags & O_APPEND) == O_APPEND) | ||
| 41 | flags |= O_WRONLY; | ||
| 42 | |||
| 43 | flags &= O_ACCMODE; | ||
| 44 | if ((flags & O_RDWR) == O_RDWR) | ||
| 45 | return CEPH_FILE_MODE_RDWR; | ||
| 46 | if ((flags & O_WRONLY) == O_WRONLY) | ||
| 47 | return CEPH_FILE_MODE_WR; | ||
| 48 | return CEPH_FILE_MODE_RD; | ||
| 49 | } | ||
| 50 | |||
| 51 | int ceph_caps_for_mode(int mode) | ||
| 52 | { | ||
| 53 | switch (mode) { | ||
| 54 | case CEPH_FILE_MODE_PIN: | ||
| 55 | return CEPH_CAP_PIN; | ||
| 56 | case CEPH_FILE_MODE_RD: | ||
| 57 | return CEPH_CAP_PIN | CEPH_CAP_FILE_SHARED | | ||
| 58 | CEPH_CAP_FILE_RD | CEPH_CAP_FILE_CACHE; | ||
| 59 | case CEPH_FILE_MODE_RDWR: | ||
| 60 | return CEPH_CAP_PIN | CEPH_CAP_FILE_SHARED | | ||
| 61 | CEPH_CAP_FILE_EXCL | | ||
| 62 | CEPH_CAP_FILE_RD | CEPH_CAP_FILE_CACHE | | ||
| 63 | CEPH_CAP_FILE_WR | CEPH_CAP_FILE_BUFFER | | ||
| 64 | CEPH_CAP_AUTH_SHARED | CEPH_CAP_AUTH_EXCL | | ||
| 65 | CEPH_CAP_XATTR_SHARED | CEPH_CAP_XATTR_EXCL; | ||
| 66 | case CEPH_FILE_MODE_WR: | ||
| 67 | return CEPH_CAP_PIN | CEPH_CAP_FILE_SHARED | | ||
| 68 | CEPH_CAP_FILE_EXCL | | ||
| 69 | CEPH_CAP_FILE_WR | CEPH_CAP_FILE_BUFFER | | ||
| 70 | CEPH_CAP_AUTH_SHARED | CEPH_CAP_AUTH_EXCL | | ||
| 71 | CEPH_CAP_XATTR_SHARED | CEPH_CAP_XATTR_EXCL; | ||
| 72 | } | ||
| 73 | return 0; | ||
| 74 | } | ||
diff --git a/fs/ceph/ceph_fs.h b/fs/ceph/ceph_fs.h new file mode 100644 index 000000000000..0c2241ef3653 --- /dev/null +++ b/fs/ceph/ceph_fs.h | |||
| @@ -0,0 +1,650 @@ | |||
| 1 | /* | ||
| 2 | * ceph_fs.h - Ceph constants and data types to share between kernel and | ||
| 3 | * user space. | ||
| 4 | * | ||
| 5 | * Most types in this file are defined as little-endian, and are | ||
| 6 | * primarily intended to describe data structures that pass over the | ||
| 7 | * wire or that are stored on disk. | ||
| 8 | * | ||
| 9 | * LGPL2 | ||
| 10 | */ | ||
| 11 | |||
| 12 | #ifndef _FS_CEPH_CEPH_FS_H | ||
| 13 | #define _FS_CEPH_CEPH_FS_H | ||
| 14 | |||
| 15 | #include "msgr.h" | ||
| 16 | #include "rados.h" | ||
| 17 | |||
| 18 | /* | ||
| 19 | * Ceph release version | ||
| 20 | */ | ||
| 21 | #define CEPH_VERSION_MAJOR 0 | ||
| 22 | #define CEPH_VERSION_MINOR 19 | ||
| 23 | #define CEPH_VERSION_PATCH 0 | ||
| 24 | |||
| 25 | #define _CEPH_STRINGIFY(x) #x | ||
| 26 | #define CEPH_STRINGIFY(x) _CEPH_STRINGIFY(x) | ||
| 27 | #define CEPH_MAKE_VERSION(x, y, z) CEPH_STRINGIFY(x) "." CEPH_STRINGIFY(y) \ | ||
| 28 | "." CEPH_STRINGIFY(z) | ||
| 29 | #define CEPH_VERSION CEPH_MAKE_VERSION(CEPH_VERSION_MAJOR, \ | ||
| 30 | CEPH_VERSION_MINOR, CEPH_VERSION_PATCH) | ||
| 31 | |||
| 32 | /* | ||
| 33 | * subprotocol versions. when specific messages types or high-level | ||
| 34 | * protocols change, bump the affected components. we keep rev | ||
| 35 | * internal cluster protocols separately from the public, | ||
| 36 | * client-facing protocol. | ||
| 37 | */ | ||
| 38 | #define CEPH_OSD_PROTOCOL 8 /* cluster internal */ | ||
| 39 | #define CEPH_MDS_PROTOCOL 9 /* cluster internal */ | ||
| 40 | #define CEPH_MON_PROTOCOL 5 /* cluster internal */ | ||
| 41 | #define CEPH_OSDC_PROTOCOL 24 /* server/client */ | ||
| 42 | #define CEPH_MDSC_PROTOCOL 32 /* server/client */ | ||
| 43 | #define CEPH_MONC_PROTOCOL 15 /* server/client */ | ||
| 44 | |||
| 45 | |||
| 46 | #define CEPH_INO_ROOT 1 | ||
| 47 | #define CEPH_INO_CEPH 2 /* hidden .ceph dir */ | ||
| 48 | |||
| 49 | /* arbitrary limit on max # of monitors (cluster of 3 is typical) */ | ||
| 50 | #define CEPH_MAX_MON 31 | ||
| 51 | |||
| 52 | |||
| 53 | /* | ||
| 54 | * feature bits | ||
| 55 | */ | ||
| 56 | #define CEPH_FEATURE_SUPPORTED 0 | ||
| 57 | #define CEPH_FEATURE_REQUIRED 0 | ||
| 58 | |||
| 59 | |||
| 60 | /* | ||
| 61 | * ceph_file_layout - describe data layout for a file/inode | ||
| 62 | */ | ||
| 63 | struct ceph_file_layout { | ||
| 64 | /* file -> object mapping */ | ||
| 65 | __le32 fl_stripe_unit; /* stripe unit, in bytes. must be multiple | ||
| 66 | of page size. */ | ||
| 67 | __le32 fl_stripe_count; /* over this many objects */ | ||
| 68 | __le32 fl_object_size; /* until objects are this big, then move to | ||
| 69 | new objects */ | ||
| 70 | __le32 fl_cas_hash; /* 0 = none; 1 = sha256 */ | ||
| 71 | |||
| 72 | /* pg -> disk layout */ | ||
| 73 | __le32 fl_object_stripe_unit; /* for per-object parity, if any */ | ||
| 74 | |||
| 75 | /* object -> pg layout */ | ||
| 76 | __le32 fl_pg_preferred; /* preferred primary for pg (-1 for none) */ | ||
| 77 | __le32 fl_pg_pool; /* namespace, crush ruleset, rep level */ | ||
| 78 | } __attribute__ ((packed)); | ||
| 79 | |||
| 80 | #define CEPH_MIN_STRIPE_UNIT 65536 | ||
| 81 | |||
| 82 | int ceph_file_layout_is_valid(const struct ceph_file_layout *layout); | ||
| 83 | |||
| 84 | |||
| 85 | /* crypto algorithms */ | ||
| 86 | #define CEPH_CRYPTO_NONE 0x0 | ||
| 87 | #define CEPH_CRYPTO_AES 0x1 | ||
| 88 | |||
| 89 | /* security/authentication protocols */ | ||
| 90 | #define CEPH_AUTH_UNKNOWN 0x0 | ||
| 91 | #define CEPH_AUTH_NONE 0x1 | ||
| 92 | #define CEPH_AUTH_CEPHX 0x2 | ||
| 93 | |||
| 94 | |||
| 95 | /********************************************* | ||
| 96 | * message layer | ||
| 97 | */ | ||
| 98 | |||
| 99 | /* | ||
| 100 | * message types | ||
| 101 | */ | ||
| 102 | |||
| 103 | /* misc */ | ||
| 104 | #define CEPH_MSG_SHUTDOWN 1 | ||
| 105 | #define CEPH_MSG_PING 2 | ||
| 106 | |||
| 107 | /* client <-> monitor */ | ||
| 108 | #define CEPH_MSG_MON_MAP 4 | ||
| 109 | #define CEPH_MSG_MON_GET_MAP 5 | ||
| 110 | #define CEPH_MSG_STATFS 13 | ||
| 111 | #define CEPH_MSG_STATFS_REPLY 14 | ||
| 112 | #define CEPH_MSG_MON_SUBSCRIBE 15 | ||
| 113 | #define CEPH_MSG_MON_SUBSCRIBE_ACK 16 | ||
| 114 | #define CEPH_MSG_AUTH 17 | ||
| 115 | #define CEPH_MSG_AUTH_REPLY 18 | ||
| 116 | |||
| 117 | /* client <-> mds */ | ||
| 118 | #define CEPH_MSG_MDS_MAP 21 | ||
| 119 | |||
| 120 | #define CEPH_MSG_CLIENT_SESSION 22 | ||
| 121 | #define CEPH_MSG_CLIENT_RECONNECT 23 | ||
| 122 | |||
| 123 | #define CEPH_MSG_CLIENT_REQUEST 24 | ||
| 124 | #define CEPH_MSG_CLIENT_REQUEST_FORWARD 25 | ||
| 125 | #define CEPH_MSG_CLIENT_REPLY 26 | ||
| 126 | #define CEPH_MSG_CLIENT_CAPS 0x310 | ||
| 127 | #define CEPH_MSG_CLIENT_LEASE 0x311 | ||
| 128 | #define CEPH_MSG_CLIENT_SNAP 0x312 | ||
| 129 | #define CEPH_MSG_CLIENT_CAPRELEASE 0x313 | ||
| 130 | |||
| 131 | /* osd */ | ||
| 132 | #define CEPH_MSG_OSD_MAP 41 | ||
| 133 | #define CEPH_MSG_OSD_OP 42 | ||
| 134 | #define CEPH_MSG_OSD_OPREPLY 43 | ||
| 135 | |||
| 136 | struct ceph_mon_request_header { | ||
| 137 | __le64 have_version; | ||
| 138 | __le16 session_mon; | ||
| 139 | __le64 session_mon_tid; | ||
| 140 | } __attribute__ ((packed)); | ||
| 141 | |||
| 142 | struct ceph_mon_statfs { | ||
| 143 | struct ceph_mon_request_header monhdr; | ||
| 144 | struct ceph_fsid fsid; | ||
| 145 | } __attribute__ ((packed)); | ||
| 146 | |||
| 147 | struct ceph_statfs { | ||
| 148 | __le64 kb, kb_used, kb_avail; | ||
| 149 | __le64 num_objects; | ||
| 150 | } __attribute__ ((packed)); | ||
| 151 | |||
| 152 | struct ceph_mon_statfs_reply { | ||
| 153 | struct ceph_fsid fsid; | ||
| 154 | __le64 version; | ||
| 155 | struct ceph_statfs st; | ||
| 156 | } __attribute__ ((packed)); | ||
| 157 | |||
| 158 | struct ceph_osd_getmap { | ||
| 159 | struct ceph_mon_request_header monhdr; | ||
| 160 | struct ceph_fsid fsid; | ||
| 161 | __le32 start; | ||
| 162 | } __attribute__ ((packed)); | ||
| 163 | |||
| 164 | struct ceph_mds_getmap { | ||
| 165 | struct ceph_mon_request_header monhdr; | ||
| 166 | struct ceph_fsid fsid; | ||
| 167 | } __attribute__ ((packed)); | ||
| 168 | |||
| 169 | struct ceph_client_mount { | ||
| 170 | struct ceph_mon_request_header monhdr; | ||
| 171 | } __attribute__ ((packed)); | ||
| 172 | |||
| 173 | struct ceph_mon_subscribe_item { | ||
| 174 | __le64 have_version; __le64 have; | ||
| 175 | __u8 onetime; | ||
| 176 | } __attribute__ ((packed)); | ||
| 177 | |||
| 178 | struct ceph_mon_subscribe_ack { | ||
| 179 | __le32 duration; /* seconds */ | ||
| 180 | struct ceph_fsid fsid; | ||
| 181 | } __attribute__ ((packed)); | ||
| 182 | |||
| 183 | /* | ||
| 184 | * mds states | ||
| 185 | * > 0 -> in | ||
| 186 | * <= 0 -> out | ||
| 187 | */ | ||
| 188 | #define CEPH_MDS_STATE_DNE 0 /* down, does not exist. */ | ||
| 189 | #define CEPH_MDS_STATE_STOPPED -1 /* down, once existed, but no subtrees. | ||
| 190 | empty log. */ | ||
| 191 | #define CEPH_MDS_STATE_BOOT -4 /* up, boot announcement. */ | ||
| 192 | #define CEPH_MDS_STATE_STANDBY -5 /* up, idle. waiting for assignment. */ | ||
| 193 | #define CEPH_MDS_STATE_CREATING -6 /* up, creating MDS instance. */ | ||
| 194 | #define CEPH_MDS_STATE_STARTING -7 /* up, starting previously stopped mds */ | ||
| 195 | #define CEPH_MDS_STATE_STANDBY_REPLAY -8 /* up, tailing active node's journal */ | ||
| 196 | |||
| 197 | #define CEPH_MDS_STATE_REPLAY 8 /* up, replaying journal. */ | ||
| 198 | #define CEPH_MDS_STATE_RESOLVE 9 /* up, disambiguating distributed | ||
| 199 | operations (import, rename, etc.) */ | ||
| 200 | #define CEPH_MDS_STATE_RECONNECT 10 /* up, reconnect to clients */ | ||
| 201 | #define CEPH_MDS_STATE_REJOIN 11 /* up, rejoining distributed cache */ | ||
| 202 | #define CEPH_MDS_STATE_CLIENTREPLAY 12 /* up, replaying client operations */ | ||
| 203 | #define CEPH_MDS_STATE_ACTIVE 13 /* up, active */ | ||
| 204 | #define CEPH_MDS_STATE_STOPPING 14 /* up, but exporting metadata */ | ||
| 205 | |||
| 206 | extern const char *ceph_mds_state_name(int s); | ||
| 207 | |||
| 208 | |||
| 209 | /* | ||
| 210 | * metadata lock types. | ||
| 211 | * - these are bitmasks.. we can compose them | ||
| 212 | * - they also define the lock ordering by the MDS | ||
| 213 | * - a few of these are internal to the mds | ||
| 214 | */ | ||
| 215 | #define CEPH_LOCK_DN 1 | ||
| 216 | #define CEPH_LOCK_ISNAP 2 | ||
| 217 | #define CEPH_LOCK_IVERSION 4 /* mds internal */ | ||
| 218 | #define CEPH_LOCK_IFILE 8 /* mds internal */ | ||
| 219 | #define CEPH_LOCK_IAUTH 32 | ||
| 220 | #define CEPH_LOCK_ILINK 64 | ||
| 221 | #define CEPH_LOCK_IDFT 128 /* dir frag tree */ | ||
| 222 | #define CEPH_LOCK_INEST 256 /* mds internal */ | ||
| 223 | #define CEPH_LOCK_IXATTR 512 | ||
| 224 | #define CEPH_LOCK_INO 2048 /* immutable inode bits; not a lock */ | ||
| 225 | |||
| 226 | /* client_session ops */ | ||
| 227 | enum { | ||
| 228 | CEPH_SESSION_REQUEST_OPEN, | ||
| 229 | CEPH_SESSION_OPEN, | ||
| 230 | CEPH_SESSION_REQUEST_CLOSE, | ||
| 231 | CEPH_SESSION_CLOSE, | ||
| 232 | CEPH_SESSION_REQUEST_RENEWCAPS, | ||
| 233 | CEPH_SESSION_RENEWCAPS, | ||
| 234 | CEPH_SESSION_STALE, | ||
| 235 | CEPH_SESSION_RECALL_STATE, | ||
| 236 | }; | ||
| 237 | |||
| 238 | extern const char *ceph_session_op_name(int op); | ||
| 239 | |||
| 240 | struct ceph_mds_session_head { | ||
| 241 | __le32 op; | ||
| 242 | __le64 seq; | ||
| 243 | struct ceph_timespec stamp; | ||
| 244 | __le32 max_caps, max_leases; | ||
| 245 | } __attribute__ ((packed)); | ||
| 246 | |||
| 247 | /* client_request */ | ||
| 248 | /* | ||
| 249 | * metadata ops. | ||
| 250 | * & 0x001000 -> write op | ||
| 251 | * & 0x010000 -> follow symlink (e.g. stat(), not lstat()). | ||
| 252 | & & 0x100000 -> use weird ino/path trace | ||
| 253 | */ | ||
| 254 | #define CEPH_MDS_OP_WRITE 0x001000 | ||
| 255 | enum { | ||
| 256 | CEPH_MDS_OP_LOOKUP = 0x00100, | ||
| 257 | CEPH_MDS_OP_GETATTR = 0x00101, | ||
| 258 | CEPH_MDS_OP_LOOKUPHASH = 0x00102, | ||
| 259 | CEPH_MDS_OP_LOOKUPPARENT = 0x00103, | ||
| 260 | |||
| 261 | CEPH_MDS_OP_SETXATTR = 0x01105, | ||
| 262 | CEPH_MDS_OP_RMXATTR = 0x01106, | ||
| 263 | CEPH_MDS_OP_SETLAYOUT = 0x01107, | ||
| 264 | CEPH_MDS_OP_SETATTR = 0x01108, | ||
| 265 | |||
| 266 | CEPH_MDS_OP_MKNOD = 0x01201, | ||
| 267 | CEPH_MDS_OP_LINK = 0x01202, | ||
| 268 | CEPH_MDS_OP_UNLINK = 0x01203, | ||
| 269 | CEPH_MDS_OP_RENAME = 0x01204, | ||
| 270 | CEPH_MDS_OP_MKDIR = 0x01220, | ||
| 271 | CEPH_MDS_OP_RMDIR = 0x01221, | ||
| 272 | CEPH_MDS_OP_SYMLINK = 0x01222, | ||
| 273 | |||
| 274 | CEPH_MDS_OP_CREATE = 0x01301, | ||
| 275 | CEPH_MDS_OP_OPEN = 0x00302, | ||
| 276 | CEPH_MDS_OP_READDIR = 0x00305, | ||
| 277 | |||
| 278 | CEPH_MDS_OP_LOOKUPSNAP = 0x00400, | ||
| 279 | CEPH_MDS_OP_MKSNAP = 0x01400, | ||
| 280 | CEPH_MDS_OP_RMSNAP = 0x01401, | ||
| 281 | CEPH_MDS_OP_LSSNAP = 0x00402, | ||
| 282 | }; | ||
| 283 | |||
| 284 | extern const char *ceph_mds_op_name(int op); | ||
| 285 | |||
| 286 | |||
| 287 | #define CEPH_SETATTR_MODE 1 | ||
| 288 | #define CEPH_SETATTR_UID 2 | ||
| 289 | #define CEPH_SETATTR_GID 4 | ||
| 290 | #define CEPH_SETATTR_MTIME 8 | ||
| 291 | #define CEPH_SETATTR_ATIME 16 | ||
| 292 | #define CEPH_SETATTR_SIZE 32 | ||
| 293 | #define CEPH_SETATTR_CTIME 64 | ||
| 294 | |||
| 295 | union ceph_mds_request_args { | ||
| 296 | struct { | ||
| 297 | __le32 mask; /* CEPH_CAP_* */ | ||
| 298 | } __attribute__ ((packed)) getattr; | ||
| 299 | struct { | ||
| 300 | __le32 mode; | ||
| 301 | __le32 uid; | ||
| 302 | __le32 gid; | ||
| 303 | struct ceph_timespec mtime; | ||
| 304 | struct ceph_timespec atime; | ||
| 305 | __le64 size, old_size; /* old_size needed by truncate */ | ||
| 306 | __le32 mask; /* CEPH_SETATTR_* */ | ||
| 307 | } __attribute__ ((packed)) setattr; | ||
| 308 | struct { | ||
| 309 | __le32 frag; /* which dir fragment */ | ||
| 310 | __le32 max_entries; /* how many dentries to grab */ | ||
| 311 | } __attribute__ ((packed)) readdir; | ||
| 312 | struct { | ||
| 313 | __le32 mode; | ||
| 314 | __le32 rdev; | ||
| 315 | } __attribute__ ((packed)) mknod; | ||
| 316 | struct { | ||
| 317 | __le32 mode; | ||
| 318 | } __attribute__ ((packed)) mkdir; | ||
| 319 | struct { | ||
| 320 | __le32 flags; | ||
| 321 | __le32 mode; | ||
| 322 | __le32 stripe_unit; /* layout for newly created file */ | ||
| 323 | __le32 stripe_count; /* ... */ | ||
| 324 | __le32 object_size; | ||
| 325 | __le32 file_replication; | ||
| 326 | __le32 preferred; | ||
| 327 | } __attribute__ ((packed)) open; | ||
| 328 | struct { | ||
| 329 | __le32 flags; | ||
| 330 | } __attribute__ ((packed)) setxattr; | ||
| 331 | struct { | ||
| 332 | struct ceph_file_layout layout; | ||
| 333 | } __attribute__ ((packed)) setlayout; | ||
| 334 | } __attribute__ ((packed)); | ||
| 335 | |||
| 336 | #define CEPH_MDS_FLAG_REPLAY 1 /* this is a replayed op */ | ||
| 337 | #define CEPH_MDS_FLAG_WANT_DENTRY 2 /* want dentry in reply */ | ||
| 338 | |||
| 339 | struct ceph_mds_request_head { | ||
| 340 | __le64 oldest_client_tid; | ||
| 341 | __le32 mdsmap_epoch; /* on client */ | ||
| 342 | __le32 flags; /* CEPH_MDS_FLAG_* */ | ||
| 343 | __u8 num_retry, num_fwd; /* count retry, fwd attempts */ | ||
| 344 | __le16 num_releases; /* # include cap/lease release records */ | ||
| 345 | __le32 op; /* mds op code */ | ||
| 346 | __le32 caller_uid, caller_gid; | ||
| 347 | __le64 ino; /* use this ino for openc, mkdir, mknod, | ||
| 348 | etc. (if replaying) */ | ||
| 349 | union ceph_mds_request_args args; | ||
| 350 | } __attribute__ ((packed)); | ||
| 351 | |||
| 352 | /* cap/lease release record */ | ||
| 353 | struct ceph_mds_request_release { | ||
| 354 | __le64 ino, cap_id; /* ino and unique cap id */ | ||
| 355 | __le32 caps, wanted; /* new issued, wanted */ | ||
| 356 | __le32 seq, issue_seq, mseq; | ||
| 357 | __le32 dname_seq; /* if releasing a dentry lease, a */ | ||
| 358 | __le32 dname_len; /* string follows. */ | ||
| 359 | } __attribute__ ((packed)); | ||
| 360 | |||
| 361 | /* client reply */ | ||
| 362 | struct ceph_mds_reply_head { | ||
| 363 | __le32 op; | ||
| 364 | __le32 result; | ||
| 365 | __le32 mdsmap_epoch; | ||
| 366 | __u8 safe; /* true if committed to disk */ | ||
| 367 | __u8 is_dentry, is_target; /* true if dentry, target inode records | ||
| 368 | are included with reply */ | ||
| 369 | } __attribute__ ((packed)); | ||
| 370 | |||
| 371 | /* one for each node split */ | ||
| 372 | struct ceph_frag_tree_split { | ||
| 373 | __le32 frag; /* this frag splits... */ | ||
| 374 | __le32 by; /* ...by this many bits */ | ||
| 375 | } __attribute__ ((packed)); | ||
| 376 | |||
| 377 | struct ceph_frag_tree_head { | ||
| 378 | __le32 nsplits; /* num ceph_frag_tree_split records */ | ||
| 379 | struct ceph_frag_tree_split splits[]; | ||
| 380 | } __attribute__ ((packed)); | ||
| 381 | |||
| 382 | /* capability issue, for bundling with mds reply */ | ||
| 383 | struct ceph_mds_reply_cap { | ||
| 384 | __le32 caps, wanted; /* caps issued, wanted */ | ||
| 385 | __le64 cap_id; | ||
| 386 | __le32 seq, mseq; | ||
| 387 | __le64 realm; /* snap realm */ | ||
| 388 | __u8 flags; /* CEPH_CAP_FLAG_* */ | ||
| 389 | } __attribute__ ((packed)); | ||
| 390 | |||
| 391 | #define CEPH_CAP_FLAG_AUTH 1 /* cap is issued by auth mds */ | ||
| 392 | |||
| 393 | /* inode record, for bundling with mds reply */ | ||
| 394 | struct ceph_mds_reply_inode { | ||
| 395 | __le64 ino; | ||
| 396 | __le64 snapid; | ||
| 397 | __le32 rdev; | ||
| 398 | __le64 version; /* inode version */ | ||
| 399 | __le64 xattr_version; /* version for xattr blob */ | ||
| 400 | struct ceph_mds_reply_cap cap; /* caps issued for this inode */ | ||
| 401 | struct ceph_file_layout layout; | ||
| 402 | struct ceph_timespec ctime, mtime, atime; | ||
| 403 | __le32 time_warp_seq; | ||
| 404 | __le64 size, max_size, truncate_size; | ||
| 405 | __le32 truncate_seq; | ||
| 406 | __le32 mode, uid, gid; | ||
| 407 | __le32 nlink; | ||
| 408 | __le64 files, subdirs, rbytes, rfiles, rsubdirs; /* dir stats */ | ||
| 409 | struct ceph_timespec rctime; | ||
| 410 | struct ceph_frag_tree_head fragtree; /* (must be at end of struct) */ | ||
| 411 | } __attribute__ ((packed)); | ||
| 412 | /* followed by frag array, then symlink string, then xattr blob */ | ||
| 413 | |||
| 414 | /* reply_lease follows dname, and reply_inode */ | ||
| 415 | struct ceph_mds_reply_lease { | ||
| 416 | __le16 mask; /* lease type(s) */ | ||
| 417 | __le32 duration_ms; /* lease duration */ | ||
| 418 | __le32 seq; | ||
| 419 | } __attribute__ ((packed)); | ||
| 420 | |||
| 421 | struct ceph_mds_reply_dirfrag { | ||
| 422 | __le32 frag; /* fragment */ | ||
| 423 | __le32 auth; /* auth mds, if this is a delegation point */ | ||
| 424 | __le32 ndist; /* number of mds' this is replicated on */ | ||
| 425 | __le32 dist[]; | ||
| 426 | } __attribute__ ((packed)); | ||
| 427 | |||
| 428 | /* file access modes */ | ||
| 429 | #define CEPH_FILE_MODE_PIN 0 | ||
| 430 | #define CEPH_FILE_MODE_RD 1 | ||
| 431 | #define CEPH_FILE_MODE_WR 2 | ||
| 432 | #define CEPH_FILE_MODE_RDWR 3 /* RD | WR */ | ||
| 433 | #define CEPH_FILE_MODE_LAZY 4 /* lazy io */ | ||
| 434 | #define CEPH_FILE_MODE_NUM 8 /* bc these are bit fields.. mostly */ | ||
| 435 | |||
| 436 | int ceph_flags_to_mode(int flags); | ||
| 437 | |||
| 438 | |||
| 439 | /* capability bits */ | ||
| 440 | #define CEPH_CAP_PIN 1 /* no specific capabilities beyond the pin */ | ||
| 441 | |||
| 442 | /* generic cap bits */ | ||
| 443 | #define CEPH_CAP_GSHARED 1 /* client can reads */ | ||
| 444 | #define CEPH_CAP_GEXCL 2 /* client can read and update */ | ||
| 445 | #define CEPH_CAP_GCACHE 4 /* (file) client can cache reads */ | ||
| 446 | #define CEPH_CAP_GRD 8 /* (file) client can read */ | ||
| 447 | #define CEPH_CAP_GWR 16 /* (file) client can write */ | ||
| 448 | #define CEPH_CAP_GBUFFER 32 /* (file) client can buffer writes */ | ||
| 449 | #define CEPH_CAP_GWREXTEND 64 /* (file) client can extend EOF */ | ||
| 450 | #define CEPH_CAP_GLAZYIO 128 /* (file) client can perform lazy io */ | ||
| 451 | |||
| 452 | /* per-lock shift */ | ||
| 453 | #define CEPH_CAP_SAUTH 2 | ||
| 454 | #define CEPH_CAP_SLINK 4 | ||
| 455 | #define CEPH_CAP_SXATTR 6 | ||
| 456 | #define CEPH_CAP_SFILE 8 /* goes at the end (uses >2 cap bits) */ | ||
| 457 | |||
| 458 | #define CEPH_CAP_BITS 16 | ||
| 459 | |||
| 460 | /* composed values */ | ||
| 461 | #define CEPH_CAP_AUTH_SHARED (CEPH_CAP_GSHARED << CEPH_CAP_SAUTH) | ||
| 462 | #define CEPH_CAP_AUTH_EXCL (CEPH_CAP_GEXCL << CEPH_CAP_SAUTH) | ||
| 463 | #define CEPH_CAP_LINK_SHARED (CEPH_CAP_GSHARED << CEPH_CAP_SLINK) | ||
| 464 | #define CEPH_CAP_LINK_EXCL (CEPH_CAP_GEXCL << CEPH_CAP_SLINK) | ||
| 465 | #define CEPH_CAP_XATTR_SHARED (CEPH_CAP_GSHARED << CEPH_CAP_SXATTR) | ||
| 466 | #define CEPH_CAP_XATTR_EXCL (CEPH_CAP_GEXCL << CEPH_CAP_SXATTR) | ||
| 467 | #define CEPH_CAP_FILE(x) (x << CEPH_CAP_SFILE) | ||
| 468 | #define CEPH_CAP_FILE_SHARED (CEPH_CAP_GSHARED << CEPH_CAP_SFILE) | ||
| 469 | #define CEPH_CAP_FILE_EXCL (CEPH_CAP_GEXCL << CEPH_CAP_SFILE) | ||
| 470 | #define CEPH_CAP_FILE_CACHE (CEPH_CAP_GCACHE << CEPH_CAP_SFILE) | ||
| 471 | #define CEPH_CAP_FILE_RD (CEPH_CAP_GRD << CEPH_CAP_SFILE) | ||
| 472 | #define CEPH_CAP_FILE_WR (CEPH_CAP_GWR << CEPH_CAP_SFILE) | ||
| 473 | #define CEPH_CAP_FILE_BUFFER (CEPH_CAP_GBUFFER << CEPH_CAP_SFILE) | ||
| 474 | #define CEPH_CAP_FILE_WREXTEND (CEPH_CAP_GWREXTEND << CEPH_CAP_SFILE) | ||
| 475 | #define CEPH_CAP_FILE_LAZYIO (CEPH_CAP_GLAZYIO << CEPH_CAP_SFILE) | ||
| 476 | |||
| 477 | /* cap masks (for getattr) */ | ||
| 478 | #define CEPH_STAT_CAP_INODE CEPH_CAP_PIN | ||
| 479 | #define CEPH_STAT_CAP_TYPE CEPH_CAP_PIN /* mode >> 12 */ | ||
| 480 | #define CEPH_STAT_CAP_SYMLINK CEPH_CAP_PIN | ||
| 481 | #define CEPH_STAT_CAP_UID CEPH_CAP_AUTH_SHARED | ||
| 482 | #define CEPH_STAT_CAP_GID CEPH_CAP_AUTH_SHARED | ||
| 483 | #define CEPH_STAT_CAP_MODE CEPH_CAP_AUTH_SHARED | ||
| 484 | #define CEPH_STAT_CAP_NLINK CEPH_CAP_LINK_SHARED | ||
| 485 | #define CEPH_STAT_CAP_LAYOUT CEPH_CAP_FILE_SHARED | ||
| 486 | #define CEPH_STAT_CAP_MTIME CEPH_CAP_FILE_SHARED | ||
| 487 | #define CEPH_STAT_CAP_SIZE CEPH_CAP_FILE_SHARED | ||
| 488 | #define CEPH_STAT_CAP_ATIME CEPH_CAP_FILE_SHARED /* fixme */ | ||
| 489 | #define CEPH_STAT_CAP_XATTR CEPH_CAP_XATTR_SHARED | ||
| 490 | #define CEPH_STAT_CAP_INODE_ALL (CEPH_CAP_PIN | \ | ||
| 491 | CEPH_CAP_AUTH_SHARED | \ | ||
| 492 | CEPH_CAP_LINK_SHARED | \ | ||
| 493 | CEPH_CAP_FILE_SHARED | \ | ||
| 494 | CEPH_CAP_XATTR_SHARED) | ||
| 495 | |||
| 496 | #define CEPH_CAP_ANY_SHARED (CEPH_CAP_AUTH_SHARED | \ | ||
| 497 | CEPH_CAP_LINK_SHARED | \ | ||
| 498 | CEPH_CAP_XATTR_SHARED | \ | ||
| 499 | CEPH_CAP_FILE_SHARED) | ||
| 500 | #define CEPH_CAP_ANY_RD (CEPH_CAP_ANY_SHARED | CEPH_CAP_FILE_RD | \ | ||
| 501 | CEPH_CAP_FILE_CACHE) | ||
| 502 | |||
| 503 | #define CEPH_CAP_ANY_EXCL (CEPH_CAP_AUTH_EXCL | \ | ||
| 504 | CEPH_CAP_LINK_EXCL | \ | ||
| 505 | CEPH_CAP_XATTR_EXCL | \ | ||
| 506 | CEPH_CAP_FILE_EXCL) | ||
| 507 | #define CEPH_CAP_ANY_FILE_WR (CEPH_CAP_FILE_WR | CEPH_CAP_FILE_BUFFER | \ | ||
| 508 | CEPH_CAP_FILE_EXCL) | ||
| 509 | #define CEPH_CAP_ANY_WR (CEPH_CAP_ANY_EXCL | CEPH_CAP_ANY_FILE_WR) | ||
| 510 | #define CEPH_CAP_ANY (CEPH_CAP_ANY_RD | CEPH_CAP_ANY_EXCL | \ | ||
| 511 | CEPH_CAP_ANY_FILE_WR | CEPH_CAP_PIN) | ||
| 512 | |||
| 513 | #define CEPH_CAP_LOCKS (CEPH_LOCK_IFILE | CEPH_LOCK_IAUTH | CEPH_LOCK_ILINK | \ | ||
| 514 | CEPH_LOCK_IXATTR) | ||
| 515 | |||
| 516 | int ceph_caps_for_mode(int mode); | ||
| 517 | |||
| 518 | enum { | ||
| 519 | CEPH_CAP_OP_GRANT, /* mds->client grant */ | ||
| 520 | CEPH_CAP_OP_REVOKE, /* mds->client revoke */ | ||
| 521 | CEPH_CAP_OP_TRUNC, /* mds->client trunc notify */ | ||
| 522 | CEPH_CAP_OP_EXPORT, /* mds has exported the cap */ | ||
| 523 | CEPH_CAP_OP_IMPORT, /* mds has imported the cap */ | ||
| 524 | CEPH_CAP_OP_UPDATE, /* client->mds update */ | ||
| 525 | CEPH_CAP_OP_DROP, /* client->mds drop cap bits */ | ||
| 526 | CEPH_CAP_OP_FLUSH, /* client->mds cap writeback */ | ||
| 527 | CEPH_CAP_OP_FLUSH_ACK, /* mds->client flushed */ | ||
| 528 | CEPH_CAP_OP_FLUSHSNAP, /* client->mds flush snapped metadata */ | ||
| 529 | CEPH_CAP_OP_FLUSHSNAP_ACK, /* mds->client flushed snapped metadata */ | ||
| 530 | CEPH_CAP_OP_RELEASE, /* client->mds release (clean) cap */ | ||
| 531 | CEPH_CAP_OP_RENEW, /* client->mds renewal request */ | ||
| 532 | }; | ||
| 533 | |||
| 534 | extern const char *ceph_cap_op_name(int op); | ||
| 535 | |||
| 536 | /* | ||
| 537 | * caps message, used for capability callbacks, acks, requests, etc. | ||
| 538 | */ | ||
| 539 | struct ceph_mds_caps { | ||
| 540 | __le32 op; /* CEPH_CAP_OP_* */ | ||
| 541 | __le64 ino, realm; | ||
| 542 | __le64 cap_id; | ||
| 543 | __le32 seq, issue_seq; | ||
| 544 | __le32 caps, wanted, dirty; /* latest issued/wanted/dirty */ | ||
| 545 | __le32 migrate_seq; | ||
| 546 | __le64 snap_follows; | ||
| 547 | __le32 snap_trace_len; | ||
| 548 | |||
| 549 | /* authlock */ | ||
| 550 | __le32 uid, gid, mode; | ||
| 551 | |||
| 552 | /* linklock */ | ||
| 553 | __le32 nlink; | ||
| 554 | |||
| 555 | /* xattrlock */ | ||
| 556 | __le32 xattr_len; | ||
| 557 | __le64 xattr_version; | ||
| 558 | |||
| 559 | /* filelock */ | ||
| 560 | __le64 size, max_size, truncate_size; | ||
| 561 | __le32 truncate_seq; | ||
| 562 | struct ceph_timespec mtime, atime, ctime; | ||
| 563 | struct ceph_file_layout layout; | ||
| 564 | __le32 time_warp_seq; | ||
| 565 | } __attribute__ ((packed)); | ||
| 566 | |||
| 567 | /* cap release msg head */ | ||
| 568 | struct ceph_mds_cap_release { | ||
| 569 | __le32 num; /* number of cap_items that follow */ | ||
| 570 | } __attribute__ ((packed)); | ||
| 571 | |||
| 572 | struct ceph_mds_cap_item { | ||
| 573 | __le64 ino; | ||
| 574 | __le64 cap_id; | ||
| 575 | __le32 migrate_seq, seq; | ||
| 576 | } __attribute__ ((packed)); | ||
| 577 | |||
| 578 | #define CEPH_MDS_LEASE_REVOKE 1 /* mds -> client */ | ||
| 579 | #define CEPH_MDS_LEASE_RELEASE 2 /* client -> mds */ | ||
| 580 | #define CEPH_MDS_LEASE_RENEW 3 /* client <-> mds */ | ||
| 581 | #define CEPH_MDS_LEASE_REVOKE_ACK 4 /* client -> mds */ | ||
| 582 | |||
| 583 | extern const char *ceph_lease_op_name(int o); | ||
| 584 | |||
| 585 | /* lease msg header */ | ||
| 586 | struct ceph_mds_lease { | ||
| 587 | __u8 action; /* CEPH_MDS_LEASE_* */ | ||
| 588 | __le16 mask; /* which lease */ | ||
| 589 | __le64 ino; | ||
| 590 | __le64 first, last; /* snap range */ | ||
| 591 | __le32 seq; | ||
| 592 | __le32 duration_ms; /* duration of renewal */ | ||
| 593 | } __attribute__ ((packed)); | ||
| 594 | /* followed by a __le32+string for dname */ | ||
| 595 | |||
| 596 | /* client reconnect */ | ||
| 597 | struct ceph_mds_cap_reconnect { | ||
| 598 | __le64 cap_id; | ||
| 599 | __le32 wanted; | ||
| 600 | __le32 issued; | ||
| 601 | __le64 size; | ||
| 602 | struct ceph_timespec mtime, atime; | ||
| 603 | __le64 snaprealm; | ||
| 604 | __le64 pathbase; /* base ino for our path to this ino */ | ||
| 605 | } __attribute__ ((packed)); | ||
| 606 | /* followed by encoded string */ | ||
| 607 | |||
| 608 | struct ceph_mds_snaprealm_reconnect { | ||
| 609 | __le64 ino; /* snap realm base */ | ||
| 610 | __le64 seq; /* snap seq for this snap realm */ | ||
| 611 | __le64 parent; /* parent realm */ | ||
| 612 | } __attribute__ ((packed)); | ||
| 613 | |||
| 614 | /* | ||
| 615 | * snaps | ||
| 616 | */ | ||
| 617 | enum { | ||
| 618 | CEPH_SNAP_OP_UPDATE, /* CREATE or DESTROY */ | ||
| 619 | CEPH_SNAP_OP_CREATE, | ||
| 620 | CEPH_SNAP_OP_DESTROY, | ||
| 621 | CEPH_SNAP_OP_SPLIT, | ||
| 622 | }; | ||
| 623 | |||
| 624 | extern const char *ceph_snap_op_name(int o); | ||
| 625 | |||
| 626 | /* snap msg header */ | ||
| 627 | struct ceph_mds_snap_head { | ||
| 628 | __le32 op; /* CEPH_SNAP_OP_* */ | ||
| 629 | __le64 split; /* ino to split off, if any */ | ||
| 630 | __le32 num_split_inos; /* # inos belonging to new child realm */ | ||
| 631 | __le32 num_split_realms; /* # child realms udner new child realm */ | ||
| 632 | __le32 trace_len; /* size of snap trace blob */ | ||
| 633 | } __attribute__ ((packed)); | ||
| 634 | /* followed by split ino list, then split realms, then the trace blob */ | ||
| 635 | |||
| 636 | /* | ||
| 637 | * encode info about a snaprealm, as viewed by a client | ||
| 638 | */ | ||
| 639 | struct ceph_mds_snap_realm { | ||
| 640 | __le64 ino; /* ino */ | ||
| 641 | __le64 created; /* snap: when created */ | ||
| 642 | __le64 parent; /* ino: parent realm */ | ||
| 643 | __le64 parent_since; /* snap: same parent since */ | ||
| 644 | __le64 seq; /* snap: version */ | ||
| 645 | __le32 num_snaps; | ||
| 646 | __le32 num_prior_parent_snaps; | ||
| 647 | } __attribute__ ((packed)); | ||
| 648 | /* followed by my snap list, then prior parent snap list */ | ||
| 649 | |||
| 650 | #endif | ||
diff --git a/fs/ceph/ceph_hash.c b/fs/ceph/ceph_hash.c new file mode 100644 index 000000000000..bd570015d147 --- /dev/null +++ b/fs/ceph/ceph_hash.c | |||
| @@ -0,0 +1,118 @@ | |||
| 1 | |||
| 2 | #include "types.h" | ||
| 3 | |||
| 4 | /* | ||
| 5 | * Robert Jenkin's hash function. | ||
| 6 | * http://burtleburtle.net/bob/hash/evahash.html | ||
| 7 | * This is in the public domain. | ||
| 8 | */ | ||
| 9 | #define mix(a, b, c) \ | ||
| 10 | do { \ | ||
| 11 | a = a - b; a = a - c; a = a ^ (c >> 13); \ | ||
| 12 | b = b - c; b = b - a; b = b ^ (a << 8); \ | ||
| 13 | c = c - a; c = c - b; c = c ^ (b >> 13); \ | ||
| 14 | a = a - b; a = a - c; a = a ^ (c >> 12); \ | ||
| 15 | b = b - c; b = b - a; b = b ^ (a << 16); \ | ||
| 16 | c = c - a; c = c - b; c = c ^ (b >> 5); \ | ||
| 17 | a = a - b; a = a - c; a = a ^ (c >> 3); \ | ||
| 18 | b = b - c; b = b - a; b = b ^ (a << 10); \ | ||
| 19 | c = c - a; c = c - b; c = c ^ (b >> 15); \ | ||
| 20 | } while (0) | ||
| 21 | |||
| 22 | unsigned ceph_str_hash_rjenkins(const char *str, unsigned length) | ||
| 23 | { | ||
| 24 | const unsigned char *k = (const unsigned char *)str; | ||
| 25 | __u32 a, b, c; /* the internal state */ | ||
| 26 | __u32 len; /* how many key bytes still need mixing */ | ||
| 27 | |||
| 28 | /* Set up the internal state */ | ||
| 29 | len = length; | ||
| 30 | a = 0x9e3779b9; /* the golden ratio; an arbitrary value */ | ||
| 31 | b = a; | ||
| 32 | c = 0; /* variable initialization of internal state */ | ||
| 33 | |||
| 34 | /* handle most of the key */ | ||
| 35 | while (len >= 12) { | ||
| 36 | a = a + (k[0] + ((__u32)k[1] << 8) + ((__u32)k[2] << 16) + | ||
| 37 | ((__u32)k[3] << 24)); | ||
| 38 | b = b + (k[4] + ((__u32)k[5] << 8) + ((__u32)k[6] << 16) + | ||
| 39 | ((__u32)k[7] << 24)); | ||
| 40 | c = c + (k[8] + ((__u32)k[9] << 8) + ((__u32)k[10] << 16) + | ||
| 41 | ((__u32)k[11] << 24)); | ||
| 42 | mix(a, b, c); | ||
| 43 | k = k + 12; | ||
| 44 | len = len - 12; | ||
| 45 | } | ||
| 46 | |||
| 47 | /* handle the last 11 bytes */ | ||
| 48 | c = c + length; | ||
| 49 | switch (len) { /* all the case statements fall through */ | ||
| 50 | case 11: | ||
| 51 | c = c + ((__u32)k[10] << 24); | ||
| 52 | case 10: | ||
| 53 | c = c + ((__u32)k[9] << 16); | ||
| 54 | case 9: | ||
| 55 | c = c + ((__u32)k[8] << 8); | ||
| 56 | /* the first byte of c is reserved for the length */ | ||
| 57 | case 8: | ||
| 58 | b = b + ((__u32)k[7] << 24); | ||
| 59 | case 7: | ||
| 60 | b = b + ((__u32)k[6] << 16); | ||
| 61 | case 6: | ||
| 62 | b = b + ((__u32)k[5] << 8); | ||
| 63 | case 5: | ||
| 64 | b = b + k[4]; | ||
| 65 | case 4: | ||
| 66 | a = a + ((__u32)k[3] << 24); | ||
| 67 | case 3: | ||
| 68 | a = a + ((__u32)k[2] << 16); | ||
| 69 | case 2: | ||
| 70 | a = a + ((__u32)k[1] << 8); | ||
| 71 | case 1: | ||
| 72 | a = a + k[0]; | ||
| 73 | /* case 0: nothing left to add */ | ||
| 74 | } | ||
| 75 | mix(a, b, c); | ||
| 76 | |||
| 77 | return c; | ||
| 78 | } | ||
| 79 | |||
| 80 | /* | ||
| 81 | * linux dcache hash | ||
| 82 | */ | ||
| 83 | unsigned ceph_str_hash_linux(const char *str, unsigned length) | ||
| 84 | { | ||
| 85 | unsigned long hash = 0; | ||
| 86 | unsigned char c; | ||
| 87 | |||
| 88 | while (length--) { | ||
| 89 | c = *str++; | ||
| 90 | hash = (hash + (c << 4) + (c >> 4)) * 11; | ||
| 91 | } | ||
| 92 | return hash; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | unsigned ceph_str_hash(int type, const char *s, unsigned len) | ||
| 97 | { | ||
| 98 | switch (type) { | ||
| 99 | case CEPH_STR_HASH_LINUX: | ||
| 100 | return ceph_str_hash_linux(s, len); | ||
| 101 | case CEPH_STR_HASH_RJENKINS: | ||
| 102 | return ceph_str_hash_rjenkins(s, len); | ||
| 103 | default: | ||
| 104 | return -1; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | const char *ceph_str_hash_name(int type) | ||
| 109 | { | ||
| 110 | switch (type) { | ||
| 111 | case CEPH_STR_HASH_LINUX: | ||
| 112 | return "linux"; | ||
| 113 | case CEPH_STR_HASH_RJENKINS: | ||
| 114 | return "rjenkins"; | ||
| 115 | default: | ||
| 116 | return "unknown"; | ||
| 117 | } | ||
| 118 | } | ||
diff --git a/fs/ceph/ceph_hash.h b/fs/ceph/ceph_hash.h new file mode 100644 index 000000000000..5ac470c433c9 --- /dev/null +++ b/fs/ceph/ceph_hash.h | |||
| @@ -0,0 +1,13 @@ | |||
| 1 | #ifndef _FS_CEPH_HASH_H | ||
| 2 | #define _FS_CEPH_HASH_H | ||
| 3 | |||
| 4 | #define CEPH_STR_HASH_LINUX 0x1 /* linux dcache hash */ | ||
| 5 | #define CEPH_STR_HASH_RJENKINS 0x2 /* robert jenkins' */ | ||
| 6 | |||
| 7 | extern unsigned ceph_str_hash_linux(const char *s, unsigned len); | ||
| 8 | extern unsigned ceph_str_hash_rjenkins(const char *s, unsigned len); | ||
| 9 | |||
| 10 | extern unsigned ceph_str_hash(int type, const char *s, unsigned len); | ||
| 11 | extern const char *ceph_str_hash_name(int type); | ||
| 12 | |||
| 13 | #endif | ||
diff --git a/fs/ceph/ceph_strings.c b/fs/ceph/ceph_strings.c new file mode 100644 index 000000000000..8e4be6a80c62 --- /dev/null +++ b/fs/ceph/ceph_strings.c | |||
| @@ -0,0 +1,176 @@ | |||
| 1 | /* | ||
| 2 | * Ceph string constants | ||
| 3 | */ | ||
| 4 | #include "types.h" | ||
| 5 | |||
| 6 | const char *ceph_entity_type_name(int type) | ||
| 7 | { | ||
| 8 | switch (type) { | ||
| 9 | case CEPH_ENTITY_TYPE_MDS: return "mds"; | ||
| 10 | case CEPH_ENTITY_TYPE_OSD: return "osd"; | ||
| 11 | case CEPH_ENTITY_TYPE_MON: return "mon"; | ||
| 12 | case CEPH_ENTITY_TYPE_CLIENT: return "client"; | ||
| 13 | case CEPH_ENTITY_TYPE_ADMIN: return "admin"; | ||
| 14 | case CEPH_ENTITY_TYPE_AUTH: return "auth"; | ||
| 15 | default: return "unknown"; | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | const char *ceph_osd_op_name(int op) | ||
| 20 | { | ||
| 21 | switch (op) { | ||
| 22 | case CEPH_OSD_OP_READ: return "read"; | ||
| 23 | case CEPH_OSD_OP_STAT: return "stat"; | ||
| 24 | |||
| 25 | case CEPH_OSD_OP_MASKTRUNC: return "masktrunc"; | ||
| 26 | |||
| 27 | case CEPH_OSD_OP_WRITE: return "write"; | ||
| 28 | case CEPH_OSD_OP_DELETE: return "delete"; | ||
| 29 | case CEPH_OSD_OP_TRUNCATE: return "truncate"; | ||
| 30 | case CEPH_OSD_OP_ZERO: return "zero"; | ||
| 31 | case CEPH_OSD_OP_WRITEFULL: return "writefull"; | ||
| 32 | |||
| 33 | case CEPH_OSD_OP_APPEND: return "append"; | ||
| 34 | case CEPH_OSD_OP_STARTSYNC: return "startsync"; | ||
| 35 | case CEPH_OSD_OP_SETTRUNC: return "settrunc"; | ||
| 36 | case CEPH_OSD_OP_TRIMTRUNC: return "trimtrunc"; | ||
| 37 | |||
| 38 | case CEPH_OSD_OP_TMAPUP: return "tmapup"; | ||
| 39 | case CEPH_OSD_OP_TMAPGET: return "tmapget"; | ||
| 40 | case CEPH_OSD_OP_TMAPPUT: return "tmapput"; | ||
| 41 | |||
| 42 | case CEPH_OSD_OP_GETXATTR: return "getxattr"; | ||
| 43 | case CEPH_OSD_OP_GETXATTRS: return "getxattrs"; | ||
| 44 | case CEPH_OSD_OP_SETXATTR: return "setxattr"; | ||
| 45 | case CEPH_OSD_OP_SETXATTRS: return "setxattrs"; | ||
| 46 | case CEPH_OSD_OP_RESETXATTRS: return "resetxattrs"; | ||
| 47 | case CEPH_OSD_OP_RMXATTR: return "rmxattr"; | ||
| 48 | |||
| 49 | case CEPH_OSD_OP_PULL: return "pull"; | ||
| 50 | case CEPH_OSD_OP_PUSH: return "push"; | ||
| 51 | case CEPH_OSD_OP_BALANCEREADS: return "balance-reads"; | ||
| 52 | case CEPH_OSD_OP_UNBALANCEREADS: return "unbalance-reads"; | ||
| 53 | case CEPH_OSD_OP_SCRUB: return "scrub"; | ||
| 54 | |||
| 55 | case CEPH_OSD_OP_WRLOCK: return "wrlock"; | ||
| 56 | case CEPH_OSD_OP_WRUNLOCK: return "wrunlock"; | ||
| 57 | case CEPH_OSD_OP_RDLOCK: return "rdlock"; | ||
| 58 | case CEPH_OSD_OP_RDUNLOCK: return "rdunlock"; | ||
| 59 | case CEPH_OSD_OP_UPLOCK: return "uplock"; | ||
| 60 | case CEPH_OSD_OP_DNLOCK: return "dnlock"; | ||
| 61 | |||
| 62 | case CEPH_OSD_OP_CALL: return "call"; | ||
| 63 | |||
| 64 | case CEPH_OSD_OP_PGLS: return "pgls"; | ||
| 65 | } | ||
| 66 | return "???"; | ||
| 67 | } | ||
| 68 | |||
| 69 | const char *ceph_mds_state_name(int s) | ||
| 70 | { | ||
| 71 | switch (s) { | ||
| 72 | /* down and out */ | ||
| 73 | case CEPH_MDS_STATE_DNE: return "down:dne"; | ||
| 74 | case CEPH_MDS_STATE_STOPPED: return "down:stopped"; | ||
| 75 | /* up and out */ | ||
| 76 | case CEPH_MDS_STATE_BOOT: return "up:boot"; | ||
| 77 | case CEPH_MDS_STATE_STANDBY: return "up:standby"; | ||
| 78 | case CEPH_MDS_STATE_STANDBY_REPLAY: return "up:standby-replay"; | ||
| 79 | case CEPH_MDS_STATE_CREATING: return "up:creating"; | ||
| 80 | case CEPH_MDS_STATE_STARTING: return "up:starting"; | ||
| 81 | /* up and in */ | ||
| 82 | case CEPH_MDS_STATE_REPLAY: return "up:replay"; | ||
| 83 | case CEPH_MDS_STATE_RESOLVE: return "up:resolve"; | ||
| 84 | case CEPH_MDS_STATE_RECONNECT: return "up:reconnect"; | ||
| 85 | case CEPH_MDS_STATE_REJOIN: return "up:rejoin"; | ||
| 86 | case CEPH_MDS_STATE_CLIENTREPLAY: return "up:clientreplay"; | ||
| 87 | case CEPH_MDS_STATE_ACTIVE: return "up:active"; | ||
| 88 | case CEPH_MDS_STATE_STOPPING: return "up:stopping"; | ||
| 89 | } | ||
| 90 | return "???"; | ||
| 91 | } | ||
| 92 | |||
| 93 | const char *ceph_session_op_name(int op) | ||
| 94 | { | ||
| 95 | switch (op) { | ||
| 96 | case CEPH_SESSION_REQUEST_OPEN: return "request_open"; | ||
| 97 | case CEPH_SESSION_OPEN: return "open"; | ||
| 98 | case CEPH_SESSION_REQUEST_CLOSE: return "request_close"; | ||
| 99 | case CEPH_SESSION_CLOSE: return "close"; | ||
| 100 | case CEPH_SESSION_REQUEST_RENEWCAPS: return "request_renewcaps"; | ||
| 101 | case CEPH_SESSION_RENEWCAPS: return "renewcaps"; | ||
| 102 | case CEPH_SESSION_STALE: return "stale"; | ||
| 103 | case CEPH_SESSION_RECALL_STATE: return "recall_state"; | ||
| 104 | } | ||
| 105 | return "???"; | ||
| 106 | } | ||
| 107 | |||
| 108 | const char *ceph_mds_op_name(int op) | ||
| 109 | { | ||
| 110 | switch (op) { | ||
| 111 | case CEPH_MDS_OP_LOOKUP: return "lookup"; | ||
| 112 | case CEPH_MDS_OP_LOOKUPHASH: return "lookuphash"; | ||
| 113 | case CEPH_MDS_OP_LOOKUPPARENT: return "lookupparent"; | ||
| 114 | case CEPH_MDS_OP_GETATTR: return "getattr"; | ||
| 115 | case CEPH_MDS_OP_SETXATTR: return "setxattr"; | ||
| 116 | case CEPH_MDS_OP_SETATTR: return "setattr"; | ||
| 117 | case CEPH_MDS_OP_RMXATTR: return "rmxattr"; | ||
| 118 | case CEPH_MDS_OP_READDIR: return "readdir"; | ||
| 119 | case CEPH_MDS_OP_MKNOD: return "mknod"; | ||
| 120 | case CEPH_MDS_OP_LINK: return "link"; | ||
| 121 | case CEPH_MDS_OP_UNLINK: return "unlink"; | ||
| 122 | case CEPH_MDS_OP_RENAME: return "rename"; | ||
| 123 | case CEPH_MDS_OP_MKDIR: return "mkdir"; | ||
| 124 | case CEPH_MDS_OP_RMDIR: return "rmdir"; | ||
| 125 | case CEPH_MDS_OP_SYMLINK: return "symlink"; | ||
| 126 | case CEPH_MDS_OP_CREATE: return "create"; | ||
| 127 | case CEPH_MDS_OP_OPEN: return "open"; | ||
| 128 | case CEPH_MDS_OP_LOOKUPSNAP: return "lookupsnap"; | ||
| 129 | case CEPH_MDS_OP_LSSNAP: return "lssnap"; | ||
| 130 | case CEPH_MDS_OP_MKSNAP: return "mksnap"; | ||
| 131 | case CEPH_MDS_OP_RMSNAP: return "rmsnap"; | ||
| 132 | } | ||
| 133 | return "???"; | ||
| 134 | } | ||
| 135 | |||
| 136 | const char *ceph_cap_op_name(int op) | ||
| 137 | { | ||
| 138 | switch (op) { | ||
| 139 | case CEPH_CAP_OP_GRANT: return "grant"; | ||
| 140 | case CEPH_CAP_OP_REVOKE: return "revoke"; | ||
| 141 | case CEPH_CAP_OP_TRUNC: return "trunc"; | ||
| 142 | case CEPH_CAP_OP_EXPORT: return "export"; | ||
| 143 | case CEPH_CAP_OP_IMPORT: return "import"; | ||
| 144 | case CEPH_CAP_OP_UPDATE: return "update"; | ||
| 145 | case CEPH_CAP_OP_DROP: return "drop"; | ||
| 146 | case CEPH_CAP_OP_FLUSH: return "flush"; | ||
| 147 | case CEPH_CAP_OP_FLUSH_ACK: return "flush_ack"; | ||
| 148 | case CEPH_CAP_OP_FLUSHSNAP: return "flushsnap"; | ||
| 149 | case CEPH_CAP_OP_FLUSHSNAP_ACK: return "flushsnap_ack"; | ||
| 150 | case CEPH_CAP_OP_RELEASE: return "release"; | ||
| 151 | case CEPH_CAP_OP_RENEW: return "renew"; | ||
| 152 | } | ||
| 153 | return "???"; | ||
| 154 | } | ||
| 155 | |||
| 156 | const char *ceph_lease_op_name(int o) | ||
| 157 | { | ||
| 158 | switch (o) { | ||
| 159 | case CEPH_MDS_LEASE_REVOKE: return "revoke"; | ||
| 160 | case CEPH_MDS_LEASE_RELEASE: return "release"; | ||
| 161 | case CEPH_MDS_LEASE_RENEW: return "renew"; | ||
| 162 | case CEPH_MDS_LEASE_REVOKE_ACK: return "revoke_ack"; | ||
| 163 | } | ||
| 164 | return "???"; | ||
| 165 | } | ||
| 166 | |||
| 167 | const char *ceph_snap_op_name(int o) | ||
| 168 | { | ||
| 169 | switch (o) { | ||
| 170 | case CEPH_SNAP_OP_UPDATE: return "update"; | ||
| 171 | case CEPH_SNAP_OP_CREATE: return "create"; | ||
| 172 | case CEPH_SNAP_OP_DESTROY: return "destroy"; | ||
| 173 | case CEPH_SNAP_OP_SPLIT: return "split"; | ||
| 174 | } | ||
| 175 | return "???"; | ||
| 176 | } | ||
diff --git a/fs/ceph/crush/crush.c b/fs/ceph/crush/crush.c new file mode 100644 index 000000000000..fabd302e5779 --- /dev/null +++ b/fs/ceph/crush/crush.c | |||
| @@ -0,0 +1,151 @@ | |||
| 1 | |||
| 2 | #ifdef __KERNEL__ | ||
| 3 | # include <linux/slab.h> | ||
| 4 | #else | ||
| 5 | # include <stdlib.h> | ||
| 6 | # include <assert.h> | ||
| 7 | # define kfree(x) do { if (x) free(x); } while (0) | ||
| 8 | # define BUG_ON(x) assert(!(x)) | ||
| 9 | #endif | ||
| 10 | |||
| 11 | #include "crush.h" | ||
| 12 | |||
| 13 | const char *crush_bucket_alg_name(int alg) | ||
| 14 | { | ||
| 15 | switch (alg) { | ||
| 16 | case CRUSH_BUCKET_UNIFORM: return "uniform"; | ||
| 17 | case CRUSH_BUCKET_LIST: return "list"; | ||
| 18 | case CRUSH_BUCKET_TREE: return "tree"; | ||
| 19 | case CRUSH_BUCKET_STRAW: return "straw"; | ||
| 20 | default: return "unknown"; | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | /** | ||
| 25 | * crush_get_bucket_item_weight - Get weight of an item in given bucket | ||
| 26 | * @b: bucket pointer | ||
| 27 | * @p: item index in bucket | ||
| 28 | */ | ||
| 29 | int crush_get_bucket_item_weight(struct crush_bucket *b, int p) | ||
| 30 | { | ||
| 31 | if (p >= b->size) | ||
| 32 | return 0; | ||
| 33 | |||
| 34 | switch (b->alg) { | ||
| 35 | case CRUSH_BUCKET_UNIFORM: | ||
| 36 | return ((struct crush_bucket_uniform *)b)->item_weight; | ||
| 37 | case CRUSH_BUCKET_LIST: | ||
| 38 | return ((struct crush_bucket_list *)b)->item_weights[p]; | ||
| 39 | case CRUSH_BUCKET_TREE: | ||
| 40 | if (p & 1) | ||
| 41 | return ((struct crush_bucket_tree *)b)->node_weights[p]; | ||
| 42 | return 0; | ||
| 43 | case CRUSH_BUCKET_STRAW: | ||
| 44 | return ((struct crush_bucket_straw *)b)->item_weights[p]; | ||
| 45 | } | ||
| 46 | return 0; | ||
| 47 | } | ||
| 48 | |||
| 49 | /** | ||
| 50 | * crush_calc_parents - Calculate parent vectors for the given crush map. | ||
| 51 | * @map: crush_map pointer | ||
| 52 | */ | ||
| 53 | void crush_calc_parents(struct crush_map *map) | ||
| 54 | { | ||
| 55 | int i, b, c; | ||
| 56 | |||
| 57 | for (b = 0; b < map->max_buckets; b++) { | ||
| 58 | if (map->buckets[b] == NULL) | ||
| 59 | continue; | ||
| 60 | for (i = 0; i < map->buckets[b]->size; i++) { | ||
| 61 | c = map->buckets[b]->items[i]; | ||
| 62 | BUG_ON(c >= map->max_devices || | ||
| 63 | c < -map->max_buckets); | ||
| 64 | if (c >= 0) | ||
| 65 | map->device_parents[c] = map->buckets[b]->id; | ||
| 66 | else | ||
| 67 | map->bucket_parents[-1-c] = map->buckets[b]->id; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | |||
| 72 | void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b) | ||
| 73 | { | ||
| 74 | kfree(b->h.perm); | ||
| 75 | kfree(b->h.items); | ||
| 76 | kfree(b); | ||
| 77 | } | ||
| 78 | |||
| 79 | void crush_destroy_bucket_list(struct crush_bucket_list *b) | ||
| 80 | { | ||
| 81 | kfree(b->item_weights); | ||
| 82 | kfree(b->sum_weights); | ||
| 83 | kfree(b->h.perm); | ||
| 84 | kfree(b->h.items); | ||
| 85 | kfree(b); | ||
| 86 | } | ||
| 87 | |||
| 88 | void crush_destroy_bucket_tree(struct crush_bucket_tree *b) | ||
| 89 | { | ||
| 90 | kfree(b->node_weights); | ||
| 91 | kfree(b); | ||
| 92 | } | ||
| 93 | |||
| 94 | void crush_destroy_bucket_straw(struct crush_bucket_straw *b) | ||
| 95 | { | ||
| 96 | kfree(b->straws); | ||
| 97 | kfree(b->item_weights); | ||
| 98 | kfree(b->h.perm); | ||
| 99 | kfree(b->h.items); | ||
| 100 | kfree(b); | ||
| 101 | } | ||
| 102 | |||
| 103 | void crush_destroy_bucket(struct crush_bucket *b) | ||
| 104 | { | ||
| 105 | switch (b->alg) { | ||
| 106 | case CRUSH_BUCKET_UNIFORM: | ||
| 107 | crush_destroy_bucket_uniform((struct crush_bucket_uniform *)b); | ||
| 108 | break; | ||
| 109 | case CRUSH_BUCKET_LIST: | ||
| 110 | crush_destroy_bucket_list((struct crush_bucket_list *)b); | ||
| 111 | break; | ||
| 112 | case CRUSH_BUCKET_TREE: | ||
| 113 | crush_destroy_bucket_tree((struct crush_bucket_tree *)b); | ||
| 114 | break; | ||
| 115 | case CRUSH_BUCKET_STRAW: | ||
| 116 | crush_destroy_bucket_straw((struct crush_bucket_straw *)b); | ||
| 117 | break; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | /** | ||
| 122 | * crush_destroy - Destroy a crush_map | ||
| 123 | * @map: crush_map pointer | ||
| 124 | */ | ||
| 125 | void crush_destroy(struct crush_map *map) | ||
| 126 | { | ||
| 127 | int b; | ||
| 128 | |||
| 129 | /* buckets */ | ||
| 130 | if (map->buckets) { | ||
| 131 | for (b = 0; b < map->max_buckets; b++) { | ||
| 132 | if (map->buckets[b] == NULL) | ||
| 133 | continue; | ||
| 134 | crush_destroy_bucket(map->buckets[b]); | ||
| 135 | } | ||
| 136 | kfree(map->buckets); | ||
| 137 | } | ||
| 138 | |||
| 139 | /* rules */ | ||
| 140 | if (map->rules) { | ||
| 141 | for (b = 0; b < map->max_rules; b++) | ||
| 142 | kfree(map->rules[b]); | ||
| 143 | kfree(map->rules); | ||
| 144 | } | ||
| 145 | |||
| 146 | kfree(map->bucket_parents); | ||
| 147 | kfree(map->device_parents); | ||
| 148 | kfree(map); | ||
| 149 | } | ||
| 150 | |||
| 151 | |||
diff --git a/fs/ceph/crush/crush.h b/fs/ceph/crush/crush.h new file mode 100644 index 000000000000..dcd7e7523700 --- /dev/null +++ b/fs/ceph/crush/crush.h | |||
| @@ -0,0 +1,180 @@ | |||
| 1 | #ifndef _CRUSH_CRUSH_H | ||
| 2 | #define _CRUSH_CRUSH_H | ||
| 3 | |||
| 4 | #include <linux/types.h> | ||
| 5 | |||
| 6 | /* | ||
| 7 | * CRUSH is a pseudo-random data distribution algorithm that | ||
| 8 | * efficiently distributes input values (typically, data objects) | ||
| 9 | * across a heterogeneous, structured storage cluster. | ||
| 10 | * | ||
| 11 | * The algorithm was originally described in detail in this paper | ||
| 12 | * (although the algorithm has evolved somewhat since then): | ||
| 13 | * | ||
| 14 | * http://www.ssrc.ucsc.edu/Papers/weil-sc06.pdf | ||
| 15 | * | ||
| 16 | * LGPL2 | ||
| 17 | */ | ||
| 18 | |||
| 19 | |||
| 20 | #define CRUSH_MAGIC 0x00010000ul /* for detecting algorithm revisions */ | ||
| 21 | |||
| 22 | |||
| 23 | #define CRUSH_MAX_DEPTH 10 /* max crush hierarchy depth */ | ||
| 24 | #define CRUSH_MAX_SET 10 /* max size of a mapping result */ | ||
| 25 | |||
| 26 | |||
| 27 | /* | ||
| 28 | * CRUSH uses user-defined "rules" to describe how inputs should be | ||
| 29 | * mapped to devices. A rule consists of sequence of steps to perform | ||
| 30 | * to generate the set of output devices. | ||
| 31 | */ | ||
| 32 | struct crush_rule_step { | ||
| 33 | __u32 op; | ||
| 34 | __s32 arg1; | ||
| 35 | __s32 arg2; | ||
| 36 | }; | ||
| 37 | |||
| 38 | /* step op codes */ | ||
| 39 | enum { | ||
| 40 | CRUSH_RULE_NOOP = 0, | ||
| 41 | CRUSH_RULE_TAKE = 1, /* arg1 = value to start with */ | ||
| 42 | CRUSH_RULE_CHOOSE_FIRSTN = 2, /* arg1 = num items to pick */ | ||
| 43 | /* arg2 = type */ | ||
| 44 | CRUSH_RULE_CHOOSE_INDEP = 3, /* same */ | ||
| 45 | CRUSH_RULE_EMIT = 4, /* no args */ | ||
| 46 | CRUSH_RULE_CHOOSE_LEAF_FIRSTN = 6, | ||
| 47 | CRUSH_RULE_CHOOSE_LEAF_INDEP = 7, | ||
| 48 | }; | ||
| 49 | |||
| 50 | /* | ||
| 51 | * for specifying choose num (arg1) relative to the max parameter | ||
| 52 | * passed to do_rule | ||
| 53 | */ | ||
| 54 | #define CRUSH_CHOOSE_N 0 | ||
| 55 | #define CRUSH_CHOOSE_N_MINUS(x) (-(x)) | ||
| 56 | |||
| 57 | /* | ||
| 58 | * The rule mask is used to describe what the rule is intended for. | ||
| 59 | * Given a ruleset and size of output set, we search through the | ||
| 60 | * rule list for a matching rule_mask. | ||
| 61 | */ | ||
| 62 | struct crush_rule_mask { | ||
| 63 | __u8 ruleset; | ||
| 64 | __u8 type; | ||
| 65 | __u8 min_size; | ||
| 66 | __u8 max_size; | ||
| 67 | }; | ||
| 68 | |||
| 69 | struct crush_rule { | ||
| 70 | __u32 len; | ||
| 71 | struct crush_rule_mask mask; | ||
| 72 | struct crush_rule_step steps[0]; | ||
| 73 | }; | ||
| 74 | |||
| 75 | #define crush_rule_size(len) (sizeof(struct crush_rule) + \ | ||
| 76 | (len)*sizeof(struct crush_rule_step)) | ||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | /* | ||
| 81 | * A bucket is a named container of other items (either devices or | ||
| 82 | * other buckets). Items within a bucket are chosen using one of a | ||
| 83 | * few different algorithms. The table summarizes how the speed of | ||
| 84 | * each option measures up against mapping stability when items are | ||
| 85 | * added or removed. | ||
| 86 | * | ||
| 87 | * Bucket Alg Speed Additions Removals | ||
| 88 | * ------------------------------------------------ | ||
| 89 | * uniform O(1) poor poor | ||
| 90 | * list O(n) optimal poor | ||
| 91 | * tree O(log n) good good | ||
| 92 | * straw O(n) optimal optimal | ||
| 93 | */ | ||
| 94 | enum { | ||
| 95 | CRUSH_BUCKET_UNIFORM = 1, | ||
| 96 | CRUSH_BUCKET_LIST = 2, | ||
| 97 | CRUSH_BUCKET_TREE = 3, | ||
| 98 | CRUSH_BUCKET_STRAW = 4 | ||
| 99 | }; | ||
| 100 | extern const char *crush_bucket_alg_name(int alg); | ||
| 101 | |||
| 102 | struct crush_bucket { | ||
| 103 | __s32 id; /* this'll be negative */ | ||
| 104 | __u16 type; /* non-zero; type=0 is reserved for devices */ | ||
| 105 | __u8 alg; /* one of CRUSH_BUCKET_* */ | ||
| 106 | __u8 hash; /* which hash function to use, CRUSH_HASH_* */ | ||
| 107 | __u32 weight; /* 16-bit fixed point */ | ||
| 108 | __u32 size; /* num items */ | ||
| 109 | __s32 *items; | ||
| 110 | |||
| 111 | /* | ||
| 112 | * cached random permutation: used for uniform bucket and for | ||
| 113 | * the linear search fallback for the other bucket types. | ||
| 114 | */ | ||
| 115 | __u32 perm_x; /* @x for which *perm is defined */ | ||
| 116 | __u32 perm_n; /* num elements of *perm that are permuted/defined */ | ||
| 117 | __u32 *perm; | ||
| 118 | }; | ||
| 119 | |||
| 120 | struct crush_bucket_uniform { | ||
| 121 | struct crush_bucket h; | ||
| 122 | __u32 item_weight; /* 16-bit fixed point; all items equally weighted */ | ||
| 123 | }; | ||
| 124 | |||
| 125 | struct crush_bucket_list { | ||
| 126 | struct crush_bucket h; | ||
| 127 | __u32 *item_weights; /* 16-bit fixed point */ | ||
| 128 | __u32 *sum_weights; /* 16-bit fixed point. element i is sum | ||
| 129 | of weights 0..i, inclusive */ | ||
| 130 | }; | ||
| 131 | |||
| 132 | struct crush_bucket_tree { | ||
| 133 | struct crush_bucket h; /* note: h.size is _tree_ size, not number of | ||
| 134 | actual items */ | ||
| 135 | __u8 num_nodes; | ||
| 136 | __u32 *node_weights; | ||
| 137 | }; | ||
| 138 | |||
| 139 | struct crush_bucket_straw { | ||
| 140 | struct crush_bucket h; | ||
| 141 | __u32 *item_weights; /* 16-bit fixed point */ | ||
| 142 | __u32 *straws; /* 16-bit fixed point */ | ||
| 143 | }; | ||
| 144 | |||
| 145 | |||
| 146 | |||
| 147 | /* | ||
| 148 | * CRUSH map includes all buckets, rules, etc. | ||
| 149 | */ | ||
| 150 | struct crush_map { | ||
| 151 | struct crush_bucket **buckets; | ||
| 152 | struct crush_rule **rules; | ||
| 153 | |||
| 154 | /* | ||
| 155 | * Parent pointers to identify the parent bucket a device or | ||
| 156 | * bucket in the hierarchy. If an item appears more than | ||
| 157 | * once, this is the _last_ time it appeared (where buckets | ||
| 158 | * are processed in bucket id order, from -1 on down to | ||
| 159 | * -max_buckets. | ||
| 160 | */ | ||
| 161 | __u32 *bucket_parents; | ||
| 162 | __u32 *device_parents; | ||
| 163 | |||
| 164 | __s32 max_buckets; | ||
| 165 | __u32 max_rules; | ||
| 166 | __s32 max_devices; | ||
| 167 | }; | ||
| 168 | |||
| 169 | |||
| 170 | /* crush.c */ | ||
| 171 | extern int crush_get_bucket_item_weight(struct crush_bucket *b, int pos); | ||
| 172 | extern void crush_calc_parents(struct crush_map *map); | ||
| 173 | extern void crush_destroy_bucket_uniform(struct crush_bucket_uniform *b); | ||
| 174 | extern void crush_destroy_bucket_list(struct crush_bucket_list *b); | ||
| 175 | extern void crush_destroy_bucket_tree(struct crush_bucket_tree *b); | ||
| 176 | extern void crush_destroy_bucket_straw(struct crush_bucket_straw *b); | ||
| 177 | extern void crush_destroy_bucket(struct crush_bucket *b); | ||
| 178 | extern void crush_destroy(struct crush_map *map); | ||
| 179 | |||
| 180 | #endif | ||
diff --git a/fs/ceph/crush/hash.c b/fs/ceph/crush/hash.c new file mode 100644 index 000000000000..5873aed694bf --- /dev/null +++ b/fs/ceph/crush/hash.c | |||
| @@ -0,0 +1,149 @@ | |||
| 1 | |||
| 2 | #include <linux/types.h> | ||
| 3 | #include "hash.h" | ||
| 4 | |||
| 5 | /* | ||
| 6 | * Robert Jenkins' function for mixing 32-bit values | ||
| 7 | * http://burtleburtle.net/bob/hash/evahash.html | ||
| 8 | * a, b = random bits, c = input and output | ||
| 9 | */ | ||
| 10 | #define crush_hashmix(a, b, c) do { \ | ||
| 11 | a = a-b; a = a-c; a = a^(c>>13); \ | ||
| 12 | b = b-c; b = b-a; b = b^(a<<8); \ | ||
| 13 | c = c-a; c = c-b; c = c^(b>>13); \ | ||
| 14 | a = a-b; a = a-c; a = a^(c>>12); \ | ||
| 15 | b = b-c; b = b-a; b = b^(a<<16); \ | ||
| 16 | c = c-a; c = c-b; c = c^(b>>5); \ | ||
| 17 | a = a-b; a = a-c; a = a^(c>>3); \ | ||
| 18 | b = b-c; b = b-a; b = b^(a<<10); \ | ||
| 19 | c = c-a; c = c-b; c = c^(b>>15); \ | ||
| 20 | } while (0) | ||
| 21 | |||
| 22 | #define crush_hash_seed 1315423911 | ||
| 23 | |||
| 24 | static __u32 crush_hash32_rjenkins1(__u32 a) | ||
| 25 | { | ||
| 26 | __u32 hash = crush_hash_seed ^ a; | ||
| 27 | __u32 b = a; | ||
| 28 | __u32 x = 231232; | ||
| 29 | __u32 y = 1232; | ||
| 30 | crush_hashmix(b, x, hash); | ||
| 31 | crush_hashmix(y, a, hash); | ||
| 32 | return hash; | ||
| 33 | } | ||
| 34 | |||
| 35 | static __u32 crush_hash32_rjenkins1_2(__u32 a, __u32 b) | ||
| 36 | { | ||
| 37 | __u32 hash = crush_hash_seed ^ a ^ b; | ||
| 38 | __u32 x = 231232; | ||
| 39 | __u32 y = 1232; | ||
| 40 | crush_hashmix(a, b, hash); | ||
| 41 | crush_hashmix(x, a, hash); | ||
| 42 | crush_hashmix(b, y, hash); | ||
| 43 | return hash; | ||
| 44 | } | ||
| 45 | |||
| 46 | static __u32 crush_hash32_rjenkins1_3(__u32 a, __u32 b, __u32 c) | ||
| 47 | { | ||
| 48 | __u32 hash = crush_hash_seed ^ a ^ b ^ c; | ||
| 49 | __u32 x = 231232; | ||
| 50 | __u32 y = 1232; | ||
| 51 | crush_hashmix(a, b, hash); | ||
| 52 | crush_hashmix(c, x, hash); | ||
| 53 | crush_hashmix(y, a, hash); | ||
| 54 | crush_hashmix(b, x, hash); | ||
| 55 | crush_hashmix(y, c, hash); | ||
| 56 | return hash; | ||
| 57 | } | ||
| 58 | |||
| 59 | static __u32 crush_hash32_rjenkins1_4(__u32 a, __u32 b, __u32 c, __u32 d) | ||
| 60 | { | ||
| 61 | __u32 hash = crush_hash_seed ^ a ^ b ^ c ^ d; | ||
| 62 | __u32 x = 231232; | ||
| 63 | __u32 y = 1232; | ||
| 64 | crush_hashmix(a, b, hash); | ||
| 65 | crush_hashmix(c, d, hash); | ||
| 66 | crush_hashmix(a, x, hash); | ||
| 67 | crush_hashmix(y, b, hash); | ||
| 68 | crush_hashmix(c, x, hash); | ||
| 69 | crush_hashmix(y, d, hash); | ||
| 70 | return hash; | ||
| 71 | } | ||
| 72 | |||
| 73 | static __u32 crush_hash32_rjenkins1_5(__u32 a, __u32 b, __u32 c, __u32 d, | ||
| 74 | __u32 e) | ||
| 75 | { | ||
| 76 | __u32 hash = crush_hash_seed ^ a ^ b ^ c ^ d ^ e; | ||
| 77 | __u32 x = 231232; | ||
| 78 | __u32 y = 1232; | ||
| 79 | crush_hashmix(a, b, hash); | ||
| 80 | crush_hashmix(c, d, hash); | ||
| 81 | crush_hashmix(e, x, hash); | ||
| 82 | crush_hashmix(y, a, hash); | ||
| 83 | crush_hashmix(b, x, hash); | ||
| 84 | crush_hashmix(y, c, hash); | ||
| 85 | crush_hashmix(d, x, hash); | ||
| 86 | crush_hashmix(y, e, hash); | ||
| 87 | return hash; | ||
| 88 | } | ||
| 89 | |||
| 90 | |||
| 91 | __u32 crush_hash32(int type, __u32 a) | ||
| 92 | { | ||
| 93 | switch (type) { | ||
| 94 | case CRUSH_HASH_RJENKINS1: | ||
| 95 | return crush_hash32_rjenkins1(a); | ||
| 96 | default: | ||
| 97 | return 0; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | __u32 crush_hash32_2(int type, __u32 a, __u32 b) | ||
| 102 | { | ||
| 103 | switch (type) { | ||
| 104 | case CRUSH_HASH_RJENKINS1: | ||
| 105 | return crush_hash32_rjenkins1_2(a, b); | ||
| 106 | default: | ||
| 107 | return 0; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | __u32 crush_hash32_3(int type, __u32 a, __u32 b, __u32 c) | ||
| 112 | { | ||
| 113 | switch (type) { | ||
| 114 | case CRUSH_HASH_RJENKINS1: | ||
| 115 | return crush_hash32_rjenkins1_3(a, b, c); | ||
| 116 | default: | ||
| 117 | return 0; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | __u32 crush_hash32_4(int type, __u32 a, __u32 b, __u32 c, __u32 d) | ||
| 122 | { | ||
| 123 | switch (type) { | ||
| 124 | case CRUSH_HASH_RJENKINS1: | ||
| 125 | return crush_hash32_rjenkins1_4(a, b, c, d); | ||
| 126 | default: | ||
| 127 | return 0; | ||
| 128 | } | ||
| 129 | } | ||
| 130 | |||
| 131 | __u32 crush_hash32_5(int type, __u32 a, __u32 b, __u32 c, __u32 d, __u32 e) | ||
| 132 | { | ||
| 133 | switch (type) { | ||
| 134 | case CRUSH_HASH_RJENKINS1: | ||
| 135 | return crush_hash32_rjenkins1_5(a, b, c, d, e); | ||
| 136 | default: | ||
| 137 | return 0; | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | const char *crush_hash_name(int type) | ||
| 142 | { | ||
| 143 | switch (type) { | ||
| 144 | case CRUSH_HASH_RJENKINS1: | ||
| 145 | return "rjenkins1"; | ||
| 146 | default: | ||
| 147 | return "unknown"; | ||
| 148 | } | ||
| 149 | } | ||
diff --git a/fs/ceph/crush/hash.h b/fs/ceph/crush/hash.h new file mode 100644 index 000000000000..ff48e110e4bb --- /dev/null +++ b/fs/ceph/crush/hash.h | |||
| @@ -0,0 +1,17 @@ | |||
| 1 | #ifndef _CRUSH_HASH_H | ||
| 2 | #define _CRUSH_HASH_H | ||
| 3 | |||
| 4 | #define CRUSH_HASH_RJENKINS1 0 | ||
| 5 | |||
| 6 | #define CRUSH_HASH_DEFAULT CRUSH_HASH_RJENKINS1 | ||
| 7 | |||
| 8 | extern const char *crush_hash_name(int type); | ||
| 9 | |||
| 10 | extern __u32 crush_hash32(int type, __u32 a); | ||
| 11 | extern __u32 crush_hash32_2(int type, __u32 a, __u32 b); | ||
| 12 | extern __u32 crush_hash32_3(int type, __u32 a, __u32 b, __u32 c); | ||
| 13 | extern __u32 crush_hash32_4(int type, __u32 a, __u32 b, __u32 c, __u32 d); | ||
| 14 | extern __u32 crush_hash32_5(int type, __u32 a, __u32 b, __u32 c, __u32 d, | ||
| 15 | __u32 e); | ||
| 16 | |||
| 17 | #endif | ||
diff --git a/fs/ceph/crush/mapper.c b/fs/ceph/crush/mapper.c new file mode 100644 index 000000000000..9ba54efb6543 --- /dev/null +++ b/fs/ceph/crush/mapper.c | |||
| @@ -0,0 +1,596 @@ | |||
| 1 | |||
| 2 | #ifdef __KERNEL__ | ||
| 3 | # include <linux/string.h> | ||
| 4 | # include <linux/slab.h> | ||
| 5 | # include <linux/bug.h> | ||
| 6 | # include <linux/kernel.h> | ||
| 7 | # ifndef dprintk | ||
| 8 | # define dprintk(args...) | ||
| 9 | # endif | ||
| 10 | #else | ||
| 11 | # include <string.h> | ||
| 12 | # include <stdio.h> | ||
| 13 | # include <stdlib.h> | ||
| 14 | # include <assert.h> | ||
| 15 | # define BUG_ON(x) assert(!(x)) | ||
| 16 | # define dprintk(args...) /* printf(args) */ | ||
| 17 | # define kmalloc(x, f) malloc(x) | ||
| 18 | # define kfree(x) free(x) | ||
| 19 | #endif | ||
| 20 | |||
| 21 | #include "crush.h" | ||
| 22 | #include "hash.h" | ||
| 23 | |||
| 24 | /* | ||
| 25 | * Implement the core CRUSH mapping algorithm. | ||
| 26 | */ | ||
| 27 | |||
| 28 | /** | ||
| 29 | * crush_find_rule - find a crush_rule id for a given ruleset, type, and size. | ||
| 30 | * @map: the crush_map | ||
| 31 | * @ruleset: the storage ruleset id (user defined) | ||
| 32 | * @type: storage ruleset type (user defined) | ||
| 33 | * @size: output set size | ||
| 34 | */ | ||
| 35 | int crush_find_rule(struct crush_map *map, int ruleset, int type, int size) | ||
| 36 | { | ||
| 37 | int i; | ||
| 38 | |||
| 39 | for (i = 0; i < map->max_rules; i++) { | ||
| 40 | if (map->rules[i] && | ||
| 41 | map->rules[i]->mask.ruleset == ruleset && | ||
| 42 | map->rules[i]->mask.type == type && | ||
| 43 | map->rules[i]->mask.min_size <= size && | ||
| 44 | map->rules[i]->mask.max_size >= size) | ||
| 45 | return i; | ||
| 46 | } | ||
| 47 | return -1; | ||
| 48 | } | ||
| 49 | |||
| 50 | |||
| 51 | /* | ||
| 52 | * bucket choose methods | ||
| 53 | * | ||
| 54 | * For each bucket algorithm, we have a "choose" method that, given a | ||
| 55 | * crush input @x and replica position (usually, position in output set) @r, | ||
| 56 | * will produce an item in the bucket. | ||
| 57 | */ | ||
| 58 | |||
| 59 | /* | ||
| 60 | * Choose based on a random permutation of the bucket. | ||
| 61 | * | ||
| 62 | * We used to use some prime number arithmetic to do this, but it | ||
| 63 | * wasn't very random, and had some other bad behaviors. Instead, we | ||
| 64 | * calculate an actual random permutation of the bucket members. | ||
| 65 | * Since this is expensive, we optimize for the r=0 case, which | ||
| 66 | * captures the vast majority of calls. | ||
| 67 | */ | ||
| 68 | static int bucket_perm_choose(struct crush_bucket *bucket, | ||
| 69 | int x, int r) | ||
| 70 | { | ||
| 71 | unsigned pr = r % bucket->size; | ||
| 72 | unsigned i, s; | ||
| 73 | |||
| 74 | /* start a new permutation if @x has changed */ | ||
| 75 | if (bucket->perm_x != x || bucket->perm_n == 0) { | ||
| 76 | dprintk("bucket %d new x=%d\n", bucket->id, x); | ||
| 77 | bucket->perm_x = x; | ||
| 78 | |||
| 79 | /* optimize common r=0 case */ | ||
| 80 | if (pr == 0) { | ||
| 81 | s = crush_hash32_3(bucket->hash, x, bucket->id, 0) % | ||
| 82 | bucket->size; | ||
| 83 | bucket->perm[0] = s; | ||
| 84 | bucket->perm_n = 0xffff; /* magic value, see below */ | ||
| 85 | goto out; | ||
| 86 | } | ||
| 87 | |||
| 88 | for (i = 0; i < bucket->size; i++) | ||
| 89 | bucket->perm[i] = i; | ||
| 90 | bucket->perm_n = 0; | ||
| 91 | } else if (bucket->perm_n == 0xffff) { | ||
| 92 | /* clean up after the r=0 case above */ | ||
| 93 | for (i = 1; i < bucket->size; i++) | ||
| 94 | bucket->perm[i] = i; | ||
| 95 | bucket->perm[bucket->perm[0]] = 0; | ||
| 96 | bucket->perm_n = 1; | ||
| 97 | } | ||
| 98 | |||
| 99 | /* calculate permutation up to pr */ | ||
| 100 | for (i = 0; i < bucket->perm_n; i++) | ||
| 101 | dprintk(" perm_choose have %d: %d\n", i, bucket->perm[i]); | ||
| 102 | while (bucket->perm_n <= pr) { | ||
| 103 | unsigned p = bucket->perm_n; | ||
| 104 | /* no point in swapping the final entry */ | ||
| 105 | if (p < bucket->size - 1) { | ||
| 106 | i = crush_hash32_3(bucket->hash, x, bucket->id, p) % | ||
| 107 | (bucket->size - p); | ||
| 108 | if (i) { | ||
| 109 | unsigned t = bucket->perm[p + i]; | ||
| 110 | bucket->perm[p + i] = bucket->perm[p]; | ||
| 111 | bucket->perm[p] = t; | ||
| 112 | } | ||
| 113 | dprintk(" perm_choose swap %d with %d\n", p, p+i); | ||
| 114 | } | ||
| 115 | bucket->perm_n++; | ||
| 116 | } | ||
| 117 | for (i = 0; i < bucket->size; i++) | ||
| 118 | dprintk(" perm_choose %d: %d\n", i, bucket->perm[i]); | ||
| 119 | |||
| 120 | s = bucket->perm[pr]; | ||
| 121 | out: | ||
| 122 | dprintk(" perm_choose %d sz=%d x=%d r=%d (%d) s=%d\n", bucket->id, | ||
| 123 | bucket->size, x, r, pr, s); | ||
| 124 | return bucket->items[s]; | ||
| 125 | } | ||
| 126 | |||
| 127 | /* uniform */ | ||
| 128 | static int bucket_uniform_choose(struct crush_bucket_uniform *bucket, | ||
| 129 | int x, int r) | ||
| 130 | { | ||
| 131 | return bucket_perm_choose(&bucket->h, x, r); | ||
| 132 | } | ||
| 133 | |||
| 134 | /* list */ | ||
| 135 | static int bucket_list_choose(struct crush_bucket_list *bucket, | ||
| 136 | int x, int r) | ||
| 137 | { | ||
| 138 | int i; | ||
| 139 | |||
| 140 | for (i = bucket->h.size-1; i >= 0; i--) { | ||
| 141 | __u64 w = crush_hash32_4(bucket->h.hash,x, bucket->h.items[i], | ||
| 142 | r, bucket->h.id); | ||
| 143 | w &= 0xffff; | ||
| 144 | dprintk("list_choose i=%d x=%d r=%d item %d weight %x " | ||
| 145 | "sw %x rand %llx", | ||
| 146 | i, x, r, bucket->h.items[i], bucket->item_weights[i], | ||
| 147 | bucket->sum_weights[i], w); | ||
| 148 | w *= bucket->sum_weights[i]; | ||
| 149 | w = w >> 16; | ||
| 150 | /*dprintk(" scaled %llx\n", w);*/ | ||
| 151 | if (w < bucket->item_weights[i]) | ||
| 152 | return bucket->h.items[i]; | ||
| 153 | } | ||
| 154 | |||
| 155 | BUG_ON(1); | ||
| 156 | return 0; | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | /* (binary) tree */ | ||
| 161 | static int height(int n) | ||
| 162 | { | ||
| 163 | int h = 0; | ||
| 164 | while ((n & 1) == 0) { | ||
| 165 | h++; | ||
| 166 | n = n >> 1; | ||
| 167 | } | ||
| 168 | return h; | ||
| 169 | } | ||
| 170 | |||
| 171 | static int left(int x) | ||
| 172 | { | ||
| 173 | int h = height(x); | ||
| 174 | return x - (1 << (h-1)); | ||
| 175 | } | ||
| 176 | |||
| 177 | static int right(int x) | ||
| 178 | { | ||
| 179 | int h = height(x); | ||
| 180 | return x + (1 << (h-1)); | ||
| 181 | } | ||
| 182 | |||
| 183 | static int terminal(int x) | ||
| 184 | { | ||
| 185 | return x & 1; | ||
| 186 | } | ||
| 187 | |||
| 188 | static int bucket_tree_choose(struct crush_bucket_tree *bucket, | ||
| 189 | int x, int r) | ||
| 190 | { | ||
| 191 | int n, l; | ||
| 192 | __u32 w; | ||
| 193 | __u64 t; | ||
| 194 | |||
| 195 | /* start at root */ | ||
| 196 | n = bucket->num_nodes >> 1; | ||
| 197 | |||
| 198 | while (!terminal(n)) { | ||
| 199 | /* pick point in [0, w) */ | ||
| 200 | w = bucket->node_weights[n]; | ||
| 201 | t = (__u64)crush_hash32_4(bucket->h.hash, x, n, r, | ||
| 202 | bucket->h.id) * (__u64)w; | ||
| 203 | t = t >> 32; | ||
| 204 | |||
| 205 | /* descend to the left or right? */ | ||
| 206 | l = left(n); | ||
| 207 | if (t < bucket->node_weights[l]) | ||
| 208 | n = l; | ||
| 209 | else | ||
| 210 | n = right(n); | ||
| 211 | } | ||
| 212 | |||
| 213 | return bucket->h.items[n >> 1]; | ||
| 214 | } | ||
| 215 | |||
| 216 | |||
| 217 | /* straw */ | ||
| 218 | |||
| 219 | static int bucket_straw_choose(struct crush_bucket_straw *bucket, | ||
| 220 | int x, int r) | ||
| 221 | { | ||
| 222 | int i; | ||
| 223 | int high = 0; | ||
| 224 | __u64 high_draw = 0; | ||
| 225 | __u64 draw; | ||
| 226 | |||
| 227 | for (i = 0; i < bucket->h.size; i++) { | ||
| 228 | draw = crush_hash32_3(bucket->h.hash, x, bucket->h.items[i], r); | ||
| 229 | draw &= 0xffff; | ||
| 230 | draw *= bucket->straws[i]; | ||
| 231 | if (i == 0 || draw > high_draw) { | ||
| 232 | high = i; | ||
| 233 | high_draw = draw; | ||
| 234 | } | ||
| 235 | } | ||
| 236 | return bucket->h.items[high]; | ||
| 237 | } | ||
| 238 | |||
| 239 | static int crush_bucket_choose(struct crush_bucket *in, int x, int r) | ||
| 240 | { | ||
| 241 | dprintk("choose %d x=%d r=%d\n", in->id, x, r); | ||
| 242 | switch (in->alg) { | ||
| 243 | case CRUSH_BUCKET_UNIFORM: | ||
| 244 | return bucket_uniform_choose((struct crush_bucket_uniform *)in, | ||
| 245 | x, r); | ||
| 246 | case CRUSH_BUCKET_LIST: | ||
| 247 | return bucket_list_choose((struct crush_bucket_list *)in, | ||
| 248 | x, r); | ||
| 249 | case CRUSH_BUCKET_TREE: | ||
| 250 | return bucket_tree_choose((struct crush_bucket_tree *)in, | ||
| 251 | x, r); | ||
| 252 | case CRUSH_BUCKET_STRAW: | ||
| 253 | return bucket_straw_choose((struct crush_bucket_straw *)in, | ||
| 254 | x, r); | ||
| 255 | default: | ||
| 256 | BUG_ON(1); | ||
| 257 | return in->items[0]; | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | /* | ||
| 262 | * true if device is marked "out" (failed, fully offloaded) | ||
| 263 | * of the cluster | ||
| 264 | */ | ||
| 265 | static int is_out(struct crush_map *map, __u32 *weight, int item, int x) | ||
| 266 | { | ||
| 267 | if (weight[item] >= 0x1000) | ||
| 268 | return 0; | ||
| 269 | if (weight[item] == 0) | ||
| 270 | return 1; | ||
| 271 | if ((crush_hash32_2(CRUSH_HASH_RJENKINS1, x, item) & 0xffff) | ||
| 272 | < weight[item]) | ||
| 273 | return 0; | ||
| 274 | return 1; | ||
| 275 | } | ||
| 276 | |||
| 277 | /** | ||
| 278 | * crush_choose - choose numrep distinct items of given type | ||
| 279 | * @map: the crush_map | ||
| 280 | * @bucket: the bucket we are choose an item from | ||
| 281 | * @x: crush input value | ||
| 282 | * @numrep: the number of items to choose | ||
| 283 | * @type: the type of item to choose | ||
| 284 | * @out: pointer to output vector | ||
| 285 | * @outpos: our position in that vector | ||
| 286 | * @firstn: true if choosing "first n" items, false if choosing "indep" | ||
| 287 | * @recurse_to_leaf: true if we want one device under each item of given type | ||
| 288 | * @out2: second output vector for leaf items (if @recurse_to_leaf) | ||
| 289 | */ | ||
| 290 | static int crush_choose(struct crush_map *map, | ||
| 291 | struct crush_bucket *bucket, | ||
| 292 | __u32 *weight, | ||
| 293 | int x, int numrep, int type, | ||
| 294 | int *out, int outpos, | ||
| 295 | int firstn, int recurse_to_leaf, | ||
| 296 | int *out2) | ||
| 297 | { | ||
| 298 | int rep; | ||
| 299 | int ftotal, flocal; | ||
| 300 | int retry_descent, retry_bucket, skip_rep; | ||
| 301 | struct crush_bucket *in = bucket; | ||
| 302 | int r; | ||
| 303 | int i; | ||
| 304 | int item = 0; | ||
| 305 | int itemtype; | ||
| 306 | int collide, reject; | ||
| 307 | const int orig_tries = 5; /* attempts before we fall back to search */ | ||
| 308 | dprintk("choose bucket %d x %d outpos %d\n", bucket->id, x, outpos); | ||
| 309 | |||
| 310 | for (rep = outpos; rep < numrep; rep++) { | ||
| 311 | /* keep trying until we get a non-out, non-colliding item */ | ||
| 312 | ftotal = 0; | ||
| 313 | skip_rep = 0; | ||
| 314 | do { | ||
| 315 | retry_descent = 0; | ||
| 316 | in = bucket; /* initial bucket */ | ||
| 317 | |||
| 318 | /* choose through intervening buckets */ | ||
| 319 | flocal = 0; | ||
| 320 | do { | ||
| 321 | collide = 0; | ||
| 322 | retry_bucket = 0; | ||
| 323 | r = rep; | ||
| 324 | if (in->alg == CRUSH_BUCKET_UNIFORM) { | ||
| 325 | /* be careful */ | ||
| 326 | if (firstn || numrep >= in->size) | ||
| 327 | /* r' = r + f_total */ | ||
| 328 | r += ftotal; | ||
| 329 | else if (in->size % numrep == 0) | ||
| 330 | /* r'=r+(n+1)*f_local */ | ||
| 331 | r += (numrep+1) * | ||
| 332 | (flocal+ftotal); | ||
| 333 | else | ||
| 334 | /* r' = r + n*f_local */ | ||
| 335 | r += numrep * (flocal+ftotal); | ||
| 336 | } else { | ||
| 337 | if (firstn) | ||
| 338 | /* r' = r + f_total */ | ||
| 339 | r += ftotal; | ||
| 340 | else | ||
| 341 | /* r' = r + n*f_local */ | ||
| 342 | r += numrep * (flocal+ftotal); | ||
| 343 | } | ||
| 344 | |||
| 345 | /* bucket choose */ | ||
| 346 | if (in->size == 0) { | ||
| 347 | reject = 1; | ||
| 348 | goto reject; | ||
| 349 | } | ||
| 350 | if (flocal >= (in->size>>1) && | ||
| 351 | flocal > orig_tries) | ||
| 352 | item = bucket_perm_choose(in, x, r); | ||
| 353 | else | ||
| 354 | item = crush_bucket_choose(in, x, r); | ||
| 355 | BUG_ON(item >= map->max_devices); | ||
| 356 | |||
| 357 | /* desired type? */ | ||
| 358 | if (item < 0) | ||
| 359 | itemtype = map->buckets[-1-item]->type; | ||
| 360 | else | ||
| 361 | itemtype = 0; | ||
| 362 | dprintk(" item %d type %d\n", item, itemtype); | ||
| 363 | |||
| 364 | /* keep going? */ | ||
| 365 | if (itemtype != type) { | ||
| 366 | BUG_ON(item >= 0 || | ||
| 367 | (-1-item) >= map->max_buckets); | ||
| 368 | in = map->buckets[-1-item]; | ||
| 369 | continue; | ||
| 370 | } | ||
| 371 | |||
| 372 | /* collision? */ | ||
| 373 | for (i = 0; i < outpos; i++) { | ||
| 374 | if (out[i] == item) { | ||
| 375 | collide = 1; | ||
| 376 | break; | ||
| 377 | } | ||
| 378 | } | ||
| 379 | |||
| 380 | if (recurse_to_leaf && | ||
| 381 | item < 0 && | ||
| 382 | crush_choose(map, map->buckets[-1-item], | ||
| 383 | weight, | ||
| 384 | x, outpos+1, 0, | ||
| 385 | out2, outpos, | ||
| 386 | firstn, 0, NULL) <= outpos) { | ||
| 387 | reject = 1; | ||
| 388 | } else { | ||
| 389 | /* out? */ | ||
| 390 | if (itemtype == 0) | ||
| 391 | reject = is_out(map, weight, | ||
| 392 | item, x); | ||
| 393 | else | ||
| 394 | reject = 0; | ||
| 395 | } | ||
| 396 | |||
| 397 | reject: | ||
| 398 | if (reject || collide) { | ||
| 399 | ftotal++; | ||
| 400 | flocal++; | ||
| 401 | |||
| 402 | if (collide && flocal < 3) | ||
| 403 | /* retry locally a few times */ | ||
| 404 | retry_bucket = 1; | ||
| 405 | else if (flocal < in->size + orig_tries) | ||
| 406 | /* exhaustive bucket search */ | ||
| 407 | retry_bucket = 1; | ||
| 408 | else if (ftotal < 20) | ||
| 409 | /* then retry descent */ | ||
| 410 | retry_descent = 1; | ||
| 411 | else | ||
| 412 | /* else give up */ | ||
| 413 | skip_rep = 1; | ||
| 414 | dprintk(" reject %d collide %d " | ||
| 415 | "ftotal %d flocal %d\n", | ||
| 416 | reject, collide, ftotal, | ||
| 417 | flocal); | ||
| 418 | } | ||
| 419 | } while (retry_bucket); | ||
| 420 | } while (retry_descent); | ||
| 421 | |||
| 422 | if (skip_rep) { | ||
| 423 | dprintk("skip rep\n"); | ||
| 424 | continue; | ||
| 425 | } | ||
| 426 | |||
| 427 | dprintk("choose got %d\n", item); | ||
| 428 | out[outpos] = item; | ||
| 429 | outpos++; | ||
| 430 | } | ||
| 431 | |||
| 432 | dprintk("choose returns %d\n", outpos); | ||
| 433 | return outpos; | ||
| 434 | } | ||
| 435 | |||
| 436 | |||
| 437 | /** | ||
| 438 | * crush_do_rule - calculate a mapping with the given input and rule | ||
| 439 | * @map: the crush_map | ||
| 440 | * @ruleno: the rule id | ||
| 441 | * @x: hash input | ||
| 442 | * @result: pointer to result vector | ||
| 443 | * @result_max: maximum result size | ||
| 444 | * @force: force initial replica choice; -1 for none | ||
| 445 | */ | ||
| 446 | int crush_do_rule(struct crush_map *map, | ||
| 447 | int ruleno, int x, int *result, int result_max, | ||
| 448 | int force, __u32 *weight) | ||
| 449 | { | ||
| 450 | int result_len; | ||
| 451 | int force_context[CRUSH_MAX_DEPTH]; | ||
| 452 | int force_pos = -1; | ||
| 453 | int a[CRUSH_MAX_SET]; | ||
| 454 | int b[CRUSH_MAX_SET]; | ||
| 455 | int c[CRUSH_MAX_SET]; | ||
| 456 | int recurse_to_leaf; | ||
| 457 | int *w; | ||
| 458 | int wsize = 0; | ||
| 459 | int *o; | ||
| 460 | int osize; | ||
| 461 | int *tmp; | ||
| 462 | struct crush_rule *rule; | ||
| 463 | int step; | ||
| 464 | int i, j; | ||
| 465 | int numrep; | ||
| 466 | int firstn; | ||
| 467 | int rc = -1; | ||
| 468 | |||
| 469 | BUG_ON(ruleno >= map->max_rules); | ||
| 470 | |||
| 471 | rule = map->rules[ruleno]; | ||
| 472 | result_len = 0; | ||
| 473 | w = a; | ||
| 474 | o = b; | ||
| 475 | |||
| 476 | /* | ||
| 477 | * determine hierarchical context of force, if any. note | ||
| 478 | * that this may or may not correspond to the specific types | ||
| 479 | * referenced by the crush rule. | ||
| 480 | */ | ||
| 481 | if (force >= 0) { | ||
| 482 | if (force >= map->max_devices || | ||
| 483 | map->device_parents[force] == 0) { | ||
| 484 | /*dprintk("CRUSH: forcefed device dne\n");*/ | ||
| 485 | rc = -1; /* force fed device dne */ | ||
| 486 | goto out; | ||
| 487 | } | ||
| 488 | if (!is_out(map, weight, force, x)) { | ||
| 489 | while (1) { | ||
| 490 | force_context[++force_pos] = force; | ||
| 491 | if (force >= 0) | ||
| 492 | force = map->device_parents[force]; | ||
| 493 | else | ||
| 494 | force = map->bucket_parents[-1-force]; | ||
| 495 | if (force == 0) | ||
| 496 | break; | ||
| 497 | } | ||
| 498 | } | ||
| 499 | } | ||
| 500 | |||
| 501 | for (step = 0; step < rule->len; step++) { | ||
| 502 | firstn = 0; | ||
| 503 | switch (rule->steps[step].op) { | ||
| 504 | case CRUSH_RULE_TAKE: | ||
| 505 | w[0] = rule->steps[step].arg1; | ||
| 506 | if (force_pos >= 0) { | ||
| 507 | BUG_ON(force_context[force_pos] != w[0]); | ||
| 508 | force_pos--; | ||
| 509 | } | ||
| 510 | wsize = 1; | ||
| 511 | break; | ||
| 512 | |||
| 513 | case CRUSH_RULE_CHOOSE_LEAF_FIRSTN: | ||
| 514 | case CRUSH_RULE_CHOOSE_FIRSTN: | ||
| 515 | firstn = 1; | ||
| 516 | case CRUSH_RULE_CHOOSE_LEAF_INDEP: | ||
| 517 | case CRUSH_RULE_CHOOSE_INDEP: | ||
| 518 | BUG_ON(wsize == 0); | ||
| 519 | |||
| 520 | recurse_to_leaf = | ||
| 521 | rule->steps[step].op == | ||
| 522 | CRUSH_RULE_CHOOSE_LEAF_FIRSTN || | ||
| 523 | rule->steps[step].op == | ||
| 524 | CRUSH_RULE_CHOOSE_LEAF_INDEP; | ||
| 525 | |||
| 526 | /* reset output */ | ||
| 527 | osize = 0; | ||
| 528 | |||
| 529 | for (i = 0; i < wsize; i++) { | ||
| 530 | /* | ||
| 531 | * see CRUSH_N, CRUSH_N_MINUS macros. | ||
| 532 | * basically, numrep <= 0 means relative to | ||
| 533 | * the provided result_max | ||
| 534 | */ | ||
| 535 | numrep = rule->steps[step].arg1; | ||
| 536 | if (numrep <= 0) { | ||
| 537 | numrep += result_max; | ||
| 538 | if (numrep <= 0) | ||
| 539 | continue; | ||
| 540 | } | ||
| 541 | j = 0; | ||
| 542 | if (osize == 0 && force_pos >= 0) { | ||
| 543 | /* skip any intermediate types */ | ||
| 544 | while (force_pos && | ||
| 545 | force_context[force_pos] < 0 && | ||
| 546 | rule->steps[step].arg2 != | ||
| 547 | map->buckets[-1 - | ||
| 548 | force_context[force_pos]]->type) | ||
| 549 | force_pos--; | ||
| 550 | o[osize] = force_context[force_pos]; | ||
| 551 | if (recurse_to_leaf) | ||
| 552 | c[osize] = force_context[0]; | ||
| 553 | j++; | ||
| 554 | force_pos--; | ||
| 555 | } | ||
| 556 | osize += crush_choose(map, | ||
| 557 | map->buckets[-1-w[i]], | ||
| 558 | weight, | ||
| 559 | x, numrep, | ||
| 560 | rule->steps[step].arg2, | ||
| 561 | o+osize, j, | ||
| 562 | firstn, | ||
| 563 | recurse_to_leaf, c+osize); | ||
| 564 | } | ||
| 565 | |||
| 566 | if (recurse_to_leaf) | ||
| 567 | /* copy final _leaf_ values to output set */ | ||
| 568 | memcpy(o, c, osize*sizeof(*o)); | ||
| 569 | |||
| 570 | /* swap t and w arrays */ | ||
| 571 | tmp = o; | ||
| 572 | o = w; | ||
| 573 | w = tmp; | ||
| 574 | wsize = osize; | ||
| 575 | break; | ||
| 576 | |||
| 577 | |||
| 578 | case CRUSH_RULE_EMIT: | ||
| 579 | for (i = 0; i < wsize && result_len < result_max; i++) { | ||
| 580 | result[result_len] = w[i]; | ||
| 581 | result_len++; | ||
| 582 | } | ||
| 583 | wsize = 0; | ||
| 584 | break; | ||
| 585 | |||
| 586 | default: | ||
| 587 | BUG_ON(1); | ||
| 588 | } | ||
| 589 | } | ||
| 590 | rc = result_len; | ||
| 591 | |||
| 592 | out: | ||
| 593 | return rc; | ||
| 594 | } | ||
| 595 | |||
| 596 | |||
diff --git a/fs/ceph/crush/mapper.h b/fs/ceph/crush/mapper.h new file mode 100644 index 000000000000..98e90046fd9f --- /dev/null +++ b/fs/ceph/crush/mapper.h | |||
| @@ -0,0 +1,20 @@ | |||
| 1 | #ifndef _CRUSH_MAPPER_H | ||
| 2 | #define _CRUSH_MAPPER_H | ||
| 3 | |||
| 4 | /* | ||
| 5 | * CRUSH functions for find rules and then mapping an input to an | ||
| 6 | * output set. | ||
| 7 | * | ||
| 8 | * LGPL2 | ||
| 9 | */ | ||
| 10 | |||
| 11 | #include "crush.h" | ||
| 12 | |||
| 13 | extern int crush_find_rule(struct crush_map *map, int pool, int type, int size); | ||
| 14 | extern int crush_do_rule(struct crush_map *map, | ||
| 15 | int ruleno, | ||
| 16 | int x, int *result, int result_max, | ||
| 17 | int forcefeed, /* -1 for none */ | ||
| 18 | __u32 *weights); | ||
| 19 | |||
| 20 | #endif | ||
diff --git a/fs/ceph/crypto.c b/fs/ceph/crypto.c new file mode 100644 index 000000000000..291ac288e791 --- /dev/null +++ b/fs/ceph/crypto.c | |||
| @@ -0,0 +1,408 @@ | |||
| 1 | |||
| 2 | #include "ceph_debug.h" | ||
| 3 | |||
| 4 | #include <linux/err.h> | ||
| 5 | #include <linux/scatterlist.h> | ||
| 6 | #include <crypto/hash.h> | ||
| 7 | |||
| 8 | #include "crypto.h" | ||
| 9 | #include "decode.h" | ||
| 10 | |||
| 11 | int ceph_crypto_key_encode(struct ceph_crypto_key *key, void **p, void *end) | ||
| 12 | { | ||
| 13 | if (*p + sizeof(u16) + sizeof(key->created) + | ||
| 14 | sizeof(u16) + key->len > end) | ||
| 15 | return -ERANGE; | ||
| 16 | ceph_encode_16(p, key->type); | ||
| 17 | ceph_encode_copy(p, &key->created, sizeof(key->created)); | ||
| 18 | ceph_encode_16(p, key->len); | ||
| 19 | ceph_encode_copy(p, key->key, key->len); | ||
| 20 | return 0; | ||
| 21 | } | ||
| 22 | |||
| 23 | int ceph_crypto_key_decode(struct ceph_crypto_key *key, void **p, void *end) | ||
| 24 | { | ||
| 25 | ceph_decode_need(p, end, 2*sizeof(u16) + sizeof(key->created), bad); | ||
| 26 | key->type = ceph_decode_16(p); | ||
| 27 | ceph_decode_copy(p, &key->created, sizeof(key->created)); | ||
| 28 | key->len = ceph_decode_16(p); | ||
| 29 | ceph_decode_need(p, end, key->len, bad); | ||
| 30 | key->key = kmalloc(key->len, GFP_NOFS); | ||
| 31 | if (!key->key) | ||
| 32 | return -ENOMEM; | ||
| 33 | ceph_decode_copy(p, key->key, key->len); | ||
| 34 | return 0; | ||
| 35 | |||
| 36 | bad: | ||
| 37 | dout("failed to decode crypto key\n"); | ||
| 38 | return -EINVAL; | ||
| 39 | } | ||
| 40 | |||
| 41 | int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *inkey) | ||
| 42 | { | ||
| 43 | int inlen = strlen(inkey); | ||
| 44 | int blen = inlen * 3 / 4; | ||
| 45 | void *buf, *p; | ||
| 46 | int ret; | ||
| 47 | |||
| 48 | dout("crypto_key_unarmor %s\n", inkey); | ||
| 49 | buf = kmalloc(blen, GFP_NOFS); | ||
| 50 | if (!buf) | ||
| 51 | return -ENOMEM; | ||
| 52 | blen = ceph_unarmor(buf, inkey, inkey+inlen); | ||
| 53 | if (blen < 0) { | ||
| 54 | kfree(buf); | ||
| 55 | return blen; | ||
| 56 | } | ||
| 57 | |||
| 58 | p = buf; | ||
| 59 | ret = ceph_crypto_key_decode(key, &p, p + blen); | ||
| 60 | kfree(buf); | ||
| 61 | if (ret) | ||
| 62 | return ret; | ||
| 63 | dout("crypto_key_unarmor key %p type %d len %d\n", key, | ||
| 64 | key->type, key->len); | ||
| 65 | return 0; | ||
| 66 | } | ||
| 67 | |||
| 68 | |||
| 69 | |||
| 70 | #define AES_KEY_SIZE 16 | ||
| 71 | |||
| 72 | static struct crypto_blkcipher *ceph_crypto_alloc_cipher(void) | ||
| 73 | { | ||
| 74 | return crypto_alloc_blkcipher("cbc(aes)", 0, CRYPTO_ALG_ASYNC); | ||
| 75 | } | ||
| 76 | |||
| 77 | const u8 *aes_iv = "cephsageyudagreg"; | ||
| 78 | |||
| 79 | int ceph_aes_encrypt(const void *key, int key_len, void *dst, size_t *dst_len, | ||
| 80 | const void *src, size_t src_len) | ||
| 81 | { | ||
| 82 | struct scatterlist sg_in[2], sg_out[1]; | ||
| 83 | struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher(); | ||
| 84 | struct blkcipher_desc desc = { .tfm = tfm, .flags = 0 }; | ||
| 85 | int ret; | ||
| 86 | void *iv; | ||
| 87 | int ivsize; | ||
| 88 | size_t zero_padding = (0x10 - (src_len & 0x0f)); | ||
| 89 | char pad[16]; | ||
| 90 | |||
| 91 | if (IS_ERR(tfm)) | ||
| 92 | return PTR_ERR(tfm); | ||
| 93 | |||
| 94 | memset(pad, zero_padding, zero_padding); | ||
| 95 | |||
| 96 | *dst_len = src_len + zero_padding; | ||
| 97 | |||
| 98 | crypto_blkcipher_setkey((void *)tfm, key, key_len); | ||
| 99 | sg_init_table(sg_in, 2); | ||
| 100 | sg_set_buf(&sg_in[0], src, src_len); | ||
| 101 | sg_set_buf(&sg_in[1], pad, zero_padding); | ||
| 102 | sg_init_table(sg_out, 1); | ||
| 103 | sg_set_buf(sg_out, dst, *dst_len); | ||
| 104 | iv = crypto_blkcipher_crt(tfm)->iv; | ||
| 105 | ivsize = crypto_blkcipher_ivsize(tfm); | ||
| 106 | |||
| 107 | memcpy(iv, aes_iv, ivsize); | ||
| 108 | /* | ||
| 109 | print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 110 | key, key_len, 1); | ||
| 111 | print_hex_dump(KERN_ERR, "enc src: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 112 | src, src_len, 1); | ||
| 113 | print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 114 | pad, zero_padding, 1); | ||
| 115 | */ | ||
| 116 | ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in, | ||
| 117 | src_len + zero_padding); | ||
| 118 | crypto_free_blkcipher(tfm); | ||
| 119 | if (ret < 0) | ||
| 120 | pr_err("ceph_aes_crypt failed %d\n", ret); | ||
| 121 | /* | ||
| 122 | print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 123 | dst, *dst_len, 1); | ||
| 124 | */ | ||
| 125 | return 0; | ||
| 126 | } | ||
| 127 | |||
| 128 | int ceph_aes_encrypt2(const void *key, int key_len, void *dst, size_t *dst_len, | ||
| 129 | const void *src1, size_t src1_len, | ||
| 130 | const void *src2, size_t src2_len) | ||
| 131 | { | ||
| 132 | struct scatterlist sg_in[3], sg_out[1]; | ||
| 133 | struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher(); | ||
| 134 | struct blkcipher_desc desc = { .tfm = tfm, .flags = 0 }; | ||
| 135 | int ret; | ||
| 136 | void *iv; | ||
| 137 | int ivsize; | ||
| 138 | size_t zero_padding = (0x10 - ((src1_len + src2_len) & 0x0f)); | ||
| 139 | char pad[16]; | ||
| 140 | |||
| 141 | if (IS_ERR(tfm)) | ||
| 142 | return PTR_ERR(tfm); | ||
| 143 | |||
| 144 | memset(pad, zero_padding, zero_padding); | ||
| 145 | |||
| 146 | *dst_len = src1_len + src2_len + zero_padding; | ||
| 147 | |||
| 148 | crypto_blkcipher_setkey((void *)tfm, key, key_len); | ||
| 149 | sg_init_table(sg_in, 3); | ||
| 150 | sg_set_buf(&sg_in[0], src1, src1_len); | ||
| 151 | sg_set_buf(&sg_in[1], src2, src2_len); | ||
| 152 | sg_set_buf(&sg_in[2], pad, zero_padding); | ||
| 153 | sg_init_table(sg_out, 1); | ||
| 154 | sg_set_buf(sg_out, dst, *dst_len); | ||
| 155 | iv = crypto_blkcipher_crt(tfm)->iv; | ||
| 156 | ivsize = crypto_blkcipher_ivsize(tfm); | ||
| 157 | |||
| 158 | memcpy(iv, aes_iv, ivsize); | ||
| 159 | /* | ||
| 160 | print_hex_dump(KERN_ERR, "enc key: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 161 | key, key_len, 1); | ||
| 162 | print_hex_dump(KERN_ERR, "enc src1: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 163 | src1, src1_len, 1); | ||
| 164 | print_hex_dump(KERN_ERR, "enc src2: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 165 | src2, src2_len, 1); | ||
| 166 | print_hex_dump(KERN_ERR, "enc pad: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 167 | pad, zero_padding, 1); | ||
| 168 | */ | ||
| 169 | ret = crypto_blkcipher_encrypt(&desc, sg_out, sg_in, | ||
| 170 | src1_len + src2_len + zero_padding); | ||
| 171 | crypto_free_blkcipher(tfm); | ||
| 172 | if (ret < 0) | ||
| 173 | pr_err("ceph_aes_crypt2 failed %d\n", ret); | ||
| 174 | /* | ||
| 175 | print_hex_dump(KERN_ERR, "enc out: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 176 | dst, *dst_len, 1); | ||
| 177 | */ | ||
| 178 | return 0; | ||
| 179 | } | ||
| 180 | |||
| 181 | int ceph_aes_decrypt(const void *key, int key_len, void *dst, size_t *dst_len, | ||
| 182 | const void *src, size_t src_len) | ||
| 183 | { | ||
| 184 | struct scatterlist sg_in[1], sg_out[2]; | ||
| 185 | struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher(); | ||
| 186 | struct blkcipher_desc desc = { .tfm = tfm }; | ||
| 187 | char pad[16]; | ||
| 188 | void *iv; | ||
| 189 | int ivsize; | ||
| 190 | int ret; | ||
| 191 | int last_byte; | ||
| 192 | |||
| 193 | if (IS_ERR(tfm)) | ||
| 194 | return PTR_ERR(tfm); | ||
| 195 | |||
| 196 | crypto_blkcipher_setkey((void *)tfm, key, key_len); | ||
| 197 | sg_init_table(sg_in, 1); | ||
| 198 | sg_init_table(sg_out, 2); | ||
| 199 | sg_set_buf(sg_in, src, src_len); | ||
| 200 | sg_set_buf(&sg_out[0], dst, *dst_len); | ||
| 201 | sg_set_buf(&sg_out[1], pad, sizeof(pad)); | ||
| 202 | |||
| 203 | iv = crypto_blkcipher_crt(tfm)->iv; | ||
| 204 | ivsize = crypto_blkcipher_ivsize(tfm); | ||
| 205 | |||
| 206 | memcpy(iv, aes_iv, ivsize); | ||
| 207 | |||
| 208 | /* | ||
| 209 | print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 210 | key, key_len, 1); | ||
| 211 | print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 212 | src, src_len, 1); | ||
| 213 | */ | ||
| 214 | |||
| 215 | ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in, src_len); | ||
| 216 | crypto_free_blkcipher(tfm); | ||
| 217 | if (ret < 0) { | ||
| 218 | pr_err("ceph_aes_decrypt failed %d\n", ret); | ||
| 219 | return ret; | ||
| 220 | } | ||
| 221 | |||
| 222 | if (src_len <= *dst_len) | ||
| 223 | last_byte = ((char *)dst)[src_len - 1]; | ||
| 224 | else | ||
| 225 | last_byte = pad[src_len - *dst_len - 1]; | ||
| 226 | if (last_byte <= 16 && src_len >= last_byte) { | ||
| 227 | *dst_len = src_len - last_byte; | ||
| 228 | } else { | ||
| 229 | pr_err("ceph_aes_decrypt got bad padding %d on src len %d\n", | ||
| 230 | last_byte, (int)src_len); | ||
| 231 | return -EPERM; /* bad padding */ | ||
| 232 | } | ||
| 233 | /* | ||
| 234 | print_hex_dump(KERN_ERR, "dec out: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 235 | dst, *dst_len, 1); | ||
| 236 | */ | ||
| 237 | return 0; | ||
| 238 | } | ||
| 239 | |||
| 240 | int ceph_aes_decrypt2(const void *key, int key_len, | ||
| 241 | void *dst1, size_t *dst1_len, | ||
| 242 | void *dst2, size_t *dst2_len, | ||
| 243 | const void *src, size_t src_len) | ||
| 244 | { | ||
| 245 | struct scatterlist sg_in[1], sg_out[3]; | ||
| 246 | struct crypto_blkcipher *tfm = ceph_crypto_alloc_cipher(); | ||
| 247 | struct blkcipher_desc desc = { .tfm = tfm }; | ||
| 248 | char pad[16]; | ||
| 249 | void *iv; | ||
| 250 | int ivsize; | ||
| 251 | int ret; | ||
| 252 | int last_byte; | ||
| 253 | |||
| 254 | if (IS_ERR(tfm)) | ||
| 255 | return PTR_ERR(tfm); | ||
| 256 | |||
| 257 | sg_init_table(sg_in, 1); | ||
| 258 | sg_set_buf(sg_in, src, src_len); | ||
| 259 | sg_init_table(sg_out, 3); | ||
| 260 | sg_set_buf(&sg_out[0], dst1, *dst1_len); | ||
| 261 | sg_set_buf(&sg_out[1], dst2, *dst2_len); | ||
| 262 | sg_set_buf(&sg_out[2], pad, sizeof(pad)); | ||
| 263 | |||
| 264 | crypto_blkcipher_setkey((void *)tfm, key, key_len); | ||
| 265 | iv = crypto_blkcipher_crt(tfm)->iv; | ||
| 266 | ivsize = crypto_blkcipher_ivsize(tfm); | ||
| 267 | |||
| 268 | memcpy(iv, aes_iv, ivsize); | ||
| 269 | |||
| 270 | /* | ||
| 271 | print_hex_dump(KERN_ERR, "dec key: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 272 | key, key_len, 1); | ||
| 273 | print_hex_dump(KERN_ERR, "dec in: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 274 | src, src_len, 1); | ||
| 275 | */ | ||
| 276 | |||
| 277 | ret = crypto_blkcipher_decrypt(&desc, sg_out, sg_in, src_len); | ||
| 278 | crypto_free_blkcipher(tfm); | ||
| 279 | if (ret < 0) { | ||
| 280 | pr_err("ceph_aes_decrypt failed %d\n", ret); | ||
| 281 | return ret; | ||
| 282 | } | ||
| 283 | |||
| 284 | if (src_len <= *dst1_len) | ||
| 285 | last_byte = ((char *)dst1)[src_len - 1]; | ||
| 286 | else if (src_len <= *dst1_len + *dst2_len) | ||
| 287 | last_byte = ((char *)dst2)[src_len - *dst1_len - 1]; | ||
| 288 | else | ||
| 289 | last_byte = pad[src_len - *dst1_len - *dst2_len - 1]; | ||
| 290 | if (last_byte <= 16 && src_len >= last_byte) { | ||
| 291 | src_len -= last_byte; | ||
| 292 | } else { | ||
| 293 | pr_err("ceph_aes_decrypt got bad padding %d on src len %d\n", | ||
| 294 | last_byte, (int)src_len); | ||
| 295 | return -EPERM; /* bad padding */ | ||
| 296 | } | ||
| 297 | |||
| 298 | if (src_len < *dst1_len) { | ||
| 299 | *dst1_len = src_len; | ||
| 300 | *dst2_len = 0; | ||
| 301 | } else { | ||
| 302 | *dst2_len = src_len - *dst1_len; | ||
| 303 | } | ||
| 304 | /* | ||
| 305 | print_hex_dump(KERN_ERR, "dec out1: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 306 | dst1, *dst1_len, 1); | ||
| 307 | print_hex_dump(KERN_ERR, "dec out2: ", DUMP_PREFIX_NONE, 16, 1, | ||
| 308 | dst2, *dst2_len, 1); | ||
| 309 | */ | ||
| 310 | |||
| 311 | return 0; | ||
| 312 | } | ||
| 313 | |||
| 314 | |||
| 315 | int ceph_decrypt(struct ceph_crypto_key *secret, void *dst, size_t *dst_len, | ||
| 316 | const void *src, size_t src_len) | ||
| 317 | { | ||
| 318 | switch (secret->type) { | ||
| 319 | case CEPH_CRYPTO_NONE: | ||
| 320 | if (*dst_len < src_len) | ||
| 321 | return -ERANGE; | ||
| 322 | memcpy(dst, src, src_len); | ||
| 323 | *dst_len = src_len; | ||
| 324 | return 0; | ||
| 325 | |||
| 326 | case CEPH_CRYPTO_AES: | ||
| 327 | return ceph_aes_decrypt(secret->key, secret->len, dst, | ||
| 328 | dst_len, src, src_len); | ||
| 329 | |||
| 330 | default: | ||
| 331 | return -EINVAL; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | |||
| 335 | int ceph_decrypt2(struct ceph_crypto_key *secret, | ||
| 336 | void *dst1, size_t *dst1_len, | ||
| 337 | void *dst2, size_t *dst2_len, | ||
| 338 | const void *src, size_t src_len) | ||
| 339 | { | ||
| 340 | size_t t; | ||
| 341 | |||
| 342 | switch (secret->type) { | ||
| 343 | case CEPH_CRYPTO_NONE: | ||
| 344 | if (*dst1_len + *dst2_len < src_len) | ||
| 345 | return -ERANGE; | ||
| 346 | t = min(*dst1_len, src_len); | ||
| 347 | memcpy(dst1, src, t); | ||
| 348 | *dst1_len = t; | ||
| 349 | src += t; | ||
| 350 | src_len -= t; | ||
| 351 | if (src_len) { | ||
| 352 | t = min(*dst2_len, src_len); | ||
| 353 | memcpy(dst2, src, t); | ||
| 354 | *dst2_len = t; | ||
| 355 | } | ||
| 356 | return 0; | ||
| 357 | |||
| 358 | case CEPH_CRYPTO_AES: | ||
| 359 | return ceph_aes_decrypt2(secret->key, secret->len, | ||
| 360 | dst1, dst1_len, dst2, dst2_len, | ||
| 361 | src, src_len); | ||
| 362 | |||
| 363 | default: | ||
| 364 | return -EINVAL; | ||
| 365 | } | ||
| 366 | } | ||
| 367 | |||
| 368 | int ceph_encrypt(struct ceph_crypto_key *secret, void *dst, size_t *dst_len, | ||
| 369 | const void *src, size_t src_len) | ||
| 370 | { | ||
| 371 | switch (secret->type) { | ||
| 372 | case CEPH_CRYPTO_NONE: | ||
| 373 | if (*dst_len < src_len) | ||
| 374 | return -ERANGE; | ||
| 375 | memcpy(dst, src, src_len); | ||
| 376 | *dst_len = src_len; | ||
| 377 | return 0; | ||
| 378 | |||
| 379 | case CEPH_CRYPTO_AES: | ||
| 380 | return ceph_aes_encrypt(secret->key, secret->len, dst, | ||
| 381 | dst_len, src, src_len); | ||
| 382 | |||
| 383 | default: | ||
| 384 | return -EINVAL; | ||
| 385 | } | ||
| 386 | } | ||
| 387 | |||
| 388 | int ceph_encrypt2(struct ceph_crypto_key *secret, void *dst, size_t *dst_len, | ||
| 389 | const void *src1, size_t src1_len, | ||
| 390 | const void *src2, size_t src2_len) | ||
| 391 | { | ||
| 392 | switch (secret->type) { | ||
| 393 | case CEPH_CRYPTO_NONE: | ||
| 394 | if (*dst_len < src1_len + src2_len) | ||
| 395 | return -ERANGE; | ||
| 396 | memcpy(dst, src1, src1_len); | ||
| 397 | memcpy(dst + src1_len, src2, src2_len); | ||
| 398 | *dst_len = src1_len + src2_len; | ||
| 399 | return 0; | ||
| 400 | |||
| 401 | case CEPH_CRYPTO_AES: | ||
| 402 | return ceph_aes_encrypt2(secret->key, secret->len, dst, dst_len, | ||
| 403 | src1, src1_len, src2, src2_len); | ||
| 404 | |||
| 405 | default: | ||
| 406 | return -EINVAL; | ||
| 407 | } | ||
| 408 | } | ||
diff --git a/fs/ceph/crypto.h b/fs/ceph/crypto.h new file mode 100644 index 000000000000..40b502e6bd89 --- /dev/null +++ b/fs/ceph/crypto.h | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | #ifndef _FS_CEPH_CRYPTO_H | ||
| 2 | #define _FS_CEPH_CRYPTO_H | ||
| 3 | |||
| 4 | #include "types.h" | ||
| 5 | #include "buffer.h" | ||
| 6 | |||
| 7 | /* | ||
| 8 | * cryptographic secret | ||
| 9 | */ | ||
| 10 | struct ceph_crypto_key { | ||
| 11 | int type; | ||
| 12 | struct ceph_timespec created; | ||
| 13 | int len; | ||
| 14 | void *key; | ||
| 15 | }; | ||
| 16 | |||
| 17 | static inline void ceph_crypto_key_destroy(struct ceph_crypto_key *key) | ||
| 18 | { | ||
| 19 | kfree(key->key); | ||
| 20 | } | ||
| 21 | |||
| 22 | extern int ceph_crypto_key_encode(struct ceph_crypto_key *key, | ||
| 23 | void **p, void *end); | ||
| 24 | extern int ceph_crypto_key_decode(struct ceph_crypto_key *key, | ||
| 25 | void **p, void *end); | ||
| 26 | extern int ceph_crypto_key_unarmor(struct ceph_crypto_key *key, const char *in); | ||
| 27 | |||
| 28 | /* crypto.c */ | ||
| 29 | extern int ceph_decrypt(struct ceph_crypto_key *secret, | ||
| 30 | void *dst, size_t *dst_len, | ||
| 31 | const void *src, size_t src_len); | ||
| 32 | extern int ceph_encrypt(struct ceph_crypto_key *secret, | ||
| 33 | void *dst, size_t *dst_len, | ||
| 34 | const void *src, size_t src_len); | ||
| 35 | extern int ceph_decrypt2(struct ceph_crypto_key *secret, | ||
| 36 | void *dst1, size_t *dst1_len, | ||
| 37 | void *dst2, size_t *dst2_len, | ||
| 38 | const void *src, size_t src_len); | ||
| 39 | extern int ceph_encrypt2(struct ceph_crypto_key *secret, | ||
| 40 | void *dst, size_t *dst_len, | ||
| 41 | const void *src1, size_t src1_len, | ||
| 42 | const void *src2, size_t src2_len); | ||
| 43 | |||
| 44 | /* armor.c */ | ||
| 45 | extern int ceph_armor(char *dst, const void *src, const void *end); | ||
| 46 | extern int ceph_unarmor(void *dst, const char *src, const char *end); | ||
| 47 | |||
| 48 | #endif | ||
diff --git a/fs/ceph/debugfs.c b/fs/ceph/debugfs.c new file mode 100644 index 000000000000..e159f1415110 --- /dev/null +++ b/fs/ceph/debugfs.c | |||
| @@ -0,0 +1,483 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/device.h> | ||
| 4 | #include <linux/module.h> | ||
| 5 | #include <linux/ctype.h> | ||
| 6 | #include <linux/debugfs.h> | ||
| 7 | #include <linux/seq_file.h> | ||
| 8 | |||
| 9 | #include "super.h" | ||
| 10 | #include "mds_client.h" | ||
| 11 | #include "mon_client.h" | ||
| 12 | #include "auth.h" | ||
| 13 | |||
| 14 | #ifdef CONFIG_DEBUG_FS | ||
| 15 | |||
| 16 | /* | ||
| 17 | * Implement /sys/kernel/debug/ceph fun | ||
| 18 | * | ||
| 19 | * /sys/kernel/debug/ceph/client* - an instance of the ceph client | ||
| 20 | * .../osdmap - current osdmap | ||
| 21 | * .../mdsmap - current mdsmap | ||
| 22 | * .../monmap - current monmap | ||
| 23 | * .../osdc - active osd requests | ||
| 24 | * .../mdsc - active mds requests | ||
| 25 | * .../monc - mon client state | ||
| 26 | * .../dentry_lru - dump contents of dentry lru | ||
| 27 | * .../caps - expose cap (reservation) stats | ||
| 28 | * .../bdi - symlink to ../../bdi/something | ||
| 29 | */ | ||
| 30 | |||
| 31 | static struct dentry *ceph_debugfs_dir; | ||
| 32 | |||
| 33 | static int monmap_show(struct seq_file *s, void *p) | ||
| 34 | { | ||
| 35 | int i; | ||
| 36 | struct ceph_client *client = s->private; | ||
| 37 | |||
| 38 | if (client->monc.monmap == NULL) | ||
| 39 | return 0; | ||
| 40 | |||
| 41 | seq_printf(s, "epoch %d\n", client->monc.monmap->epoch); | ||
| 42 | for (i = 0; i < client->monc.monmap->num_mon; i++) { | ||
| 43 | struct ceph_entity_inst *inst = | ||
| 44 | &client->monc.monmap->mon_inst[i]; | ||
| 45 | |||
| 46 | seq_printf(s, "\t%s%lld\t%s\n", | ||
| 47 | ENTITY_NAME(inst->name), | ||
| 48 | pr_addr(&inst->addr.in_addr)); | ||
| 49 | } | ||
| 50 | return 0; | ||
| 51 | } | ||
| 52 | |||
| 53 | static int mdsmap_show(struct seq_file *s, void *p) | ||
| 54 | { | ||
| 55 | int i; | ||
| 56 | struct ceph_client *client = s->private; | ||
| 57 | |||
| 58 | if (client->mdsc.mdsmap == NULL) | ||
| 59 | return 0; | ||
| 60 | seq_printf(s, "epoch %d\n", client->mdsc.mdsmap->m_epoch); | ||
| 61 | seq_printf(s, "root %d\n", client->mdsc.mdsmap->m_root); | ||
| 62 | seq_printf(s, "session_timeout %d\n", | ||
| 63 | client->mdsc.mdsmap->m_session_timeout); | ||
| 64 | seq_printf(s, "session_autoclose %d\n", | ||
| 65 | client->mdsc.mdsmap->m_session_autoclose); | ||
| 66 | for (i = 0; i < client->mdsc.mdsmap->m_max_mds; i++) { | ||
| 67 | struct ceph_entity_addr *addr = | ||
| 68 | &client->mdsc.mdsmap->m_info[i].addr; | ||
| 69 | int state = client->mdsc.mdsmap->m_info[i].state; | ||
| 70 | |||
| 71 | seq_printf(s, "\tmds%d\t%s\t(%s)\n", i, pr_addr(&addr->in_addr), | ||
| 72 | ceph_mds_state_name(state)); | ||
| 73 | } | ||
| 74 | return 0; | ||
| 75 | } | ||
| 76 | |||
| 77 | static int osdmap_show(struct seq_file *s, void *p) | ||
| 78 | { | ||
| 79 | int i; | ||
| 80 | struct ceph_client *client = s->private; | ||
| 81 | struct rb_node *n; | ||
| 82 | |||
| 83 | if (client->osdc.osdmap == NULL) | ||
| 84 | return 0; | ||
| 85 | seq_printf(s, "epoch %d\n", client->osdc.osdmap->epoch); | ||
| 86 | seq_printf(s, "flags%s%s\n", | ||
| 87 | (client->osdc.osdmap->flags & CEPH_OSDMAP_NEARFULL) ? | ||
| 88 | " NEARFULL" : "", | ||
| 89 | (client->osdc.osdmap->flags & CEPH_OSDMAP_FULL) ? | ||
| 90 | " FULL" : ""); | ||
| 91 | for (n = rb_first(&client->osdc.osdmap->pg_pools); n; n = rb_next(n)) { | ||
| 92 | struct ceph_pg_pool_info *pool = | ||
| 93 | rb_entry(n, struct ceph_pg_pool_info, node); | ||
| 94 | seq_printf(s, "pg_pool %d pg_num %d / %d, lpg_num %d / %d\n", | ||
| 95 | pool->id, pool->v.pg_num, pool->pg_num_mask, | ||
| 96 | pool->v.lpg_num, pool->lpg_num_mask); | ||
| 97 | } | ||
| 98 | for (i = 0; i < client->osdc.osdmap->max_osd; i++) { | ||
| 99 | struct ceph_entity_addr *addr = | ||
| 100 | &client->osdc.osdmap->osd_addr[i]; | ||
| 101 | int state = client->osdc.osdmap->osd_state[i]; | ||
| 102 | char sb[64]; | ||
| 103 | |||
| 104 | seq_printf(s, "\tosd%d\t%s\t%3d%%\t(%s)\n", | ||
| 105 | i, pr_addr(&addr->in_addr), | ||
| 106 | ((client->osdc.osdmap->osd_weight[i]*100) >> 16), | ||
| 107 | ceph_osdmap_state_str(sb, sizeof(sb), state)); | ||
| 108 | } | ||
| 109 | return 0; | ||
| 110 | } | ||
| 111 | |||
| 112 | static int monc_show(struct seq_file *s, void *p) | ||
| 113 | { | ||
| 114 | struct ceph_client *client = s->private; | ||
| 115 | struct ceph_mon_statfs_request *req; | ||
| 116 | struct ceph_mon_client *monc = &client->monc; | ||
| 117 | struct rb_node *rp; | ||
| 118 | |||
| 119 | mutex_lock(&monc->mutex); | ||
| 120 | |||
| 121 | if (monc->have_mdsmap) | ||
| 122 | seq_printf(s, "have mdsmap %u\n", (unsigned)monc->have_mdsmap); | ||
| 123 | if (monc->have_osdmap) | ||
| 124 | seq_printf(s, "have osdmap %u\n", (unsigned)monc->have_osdmap); | ||
| 125 | if (monc->want_next_osdmap) | ||
| 126 | seq_printf(s, "want next osdmap\n"); | ||
| 127 | |||
| 128 | for (rp = rb_first(&monc->statfs_request_tree); rp; rp = rb_next(rp)) { | ||
| 129 | req = rb_entry(rp, struct ceph_mon_statfs_request, node); | ||
| 130 | seq_printf(s, "%lld statfs\n", req->tid); | ||
| 131 | } | ||
| 132 | |||
| 133 | mutex_unlock(&monc->mutex); | ||
| 134 | return 0; | ||
| 135 | } | ||
| 136 | |||
| 137 | static int mdsc_show(struct seq_file *s, void *p) | ||
| 138 | { | ||
| 139 | struct ceph_client *client = s->private; | ||
| 140 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 141 | struct ceph_mds_request *req; | ||
| 142 | struct rb_node *rp; | ||
| 143 | int pathlen; | ||
| 144 | u64 pathbase; | ||
| 145 | char *path; | ||
| 146 | |||
| 147 | mutex_lock(&mdsc->mutex); | ||
| 148 | for (rp = rb_first(&mdsc->request_tree); rp; rp = rb_next(rp)) { | ||
| 149 | req = rb_entry(rp, struct ceph_mds_request, r_node); | ||
| 150 | |||
| 151 | if (req->r_request) | ||
| 152 | seq_printf(s, "%lld\tmds%d\t", req->r_tid, req->r_mds); | ||
| 153 | else | ||
| 154 | seq_printf(s, "%lld\t(no request)\t", req->r_tid); | ||
| 155 | |||
| 156 | seq_printf(s, "%s", ceph_mds_op_name(req->r_op)); | ||
| 157 | |||
| 158 | if (req->r_got_unsafe) | ||
| 159 | seq_printf(s, "\t(unsafe)"); | ||
| 160 | else | ||
| 161 | seq_printf(s, "\t"); | ||
| 162 | |||
| 163 | if (req->r_inode) { | ||
| 164 | seq_printf(s, " #%llx", ceph_ino(req->r_inode)); | ||
| 165 | } else if (req->r_dentry) { | ||
| 166 | path = ceph_mdsc_build_path(req->r_dentry, &pathlen, | ||
| 167 | &pathbase, 0); | ||
| 168 | spin_lock(&req->r_dentry->d_lock); | ||
| 169 | seq_printf(s, " #%llx/%.*s (%s)", | ||
| 170 | ceph_ino(req->r_dentry->d_parent->d_inode), | ||
| 171 | req->r_dentry->d_name.len, | ||
| 172 | req->r_dentry->d_name.name, | ||
| 173 | path ? path : ""); | ||
| 174 | spin_unlock(&req->r_dentry->d_lock); | ||
| 175 | kfree(path); | ||
| 176 | } else if (req->r_path1) { | ||
| 177 | seq_printf(s, " #%llx/%s", req->r_ino1.ino, | ||
| 178 | req->r_path1); | ||
| 179 | } | ||
| 180 | |||
| 181 | if (req->r_old_dentry) { | ||
| 182 | path = ceph_mdsc_build_path(req->r_old_dentry, &pathlen, | ||
| 183 | &pathbase, 0); | ||
| 184 | spin_lock(&req->r_old_dentry->d_lock); | ||
| 185 | seq_printf(s, " #%llx/%.*s (%s)", | ||
| 186 | ceph_ino(req->r_old_dentry->d_parent->d_inode), | ||
| 187 | req->r_old_dentry->d_name.len, | ||
| 188 | req->r_old_dentry->d_name.name, | ||
| 189 | path ? path : ""); | ||
| 190 | spin_unlock(&req->r_old_dentry->d_lock); | ||
| 191 | kfree(path); | ||
| 192 | } else if (req->r_path2) { | ||
| 193 | if (req->r_ino2.ino) | ||
| 194 | seq_printf(s, " #%llx/%s", req->r_ino2.ino, | ||
| 195 | req->r_path2); | ||
| 196 | else | ||
| 197 | seq_printf(s, " %s", req->r_path2); | ||
| 198 | } | ||
| 199 | |||
| 200 | seq_printf(s, "\n"); | ||
| 201 | } | ||
| 202 | mutex_unlock(&mdsc->mutex); | ||
| 203 | |||
| 204 | return 0; | ||
| 205 | } | ||
| 206 | |||
| 207 | static int osdc_show(struct seq_file *s, void *pp) | ||
| 208 | { | ||
| 209 | struct ceph_client *client = s->private; | ||
| 210 | struct ceph_osd_client *osdc = &client->osdc; | ||
| 211 | struct rb_node *p; | ||
| 212 | |||
| 213 | mutex_lock(&osdc->request_mutex); | ||
| 214 | for (p = rb_first(&osdc->requests); p; p = rb_next(p)) { | ||
| 215 | struct ceph_osd_request *req; | ||
| 216 | struct ceph_osd_request_head *head; | ||
| 217 | struct ceph_osd_op *op; | ||
| 218 | int num_ops; | ||
| 219 | int opcode, olen; | ||
| 220 | int i; | ||
| 221 | |||
| 222 | req = rb_entry(p, struct ceph_osd_request, r_node); | ||
| 223 | |||
| 224 | seq_printf(s, "%lld\tosd%d\t%d.%x\t", req->r_tid, | ||
| 225 | req->r_osd ? req->r_osd->o_osd : -1, | ||
| 226 | le32_to_cpu(req->r_pgid.pool), | ||
| 227 | le16_to_cpu(req->r_pgid.ps)); | ||
| 228 | |||
| 229 | head = req->r_request->front.iov_base; | ||
| 230 | op = (void *)(head + 1); | ||
| 231 | |||
| 232 | num_ops = le16_to_cpu(head->num_ops); | ||
| 233 | olen = le32_to_cpu(head->object_len); | ||
| 234 | seq_printf(s, "%.*s", olen, | ||
| 235 | (const char *)(head->ops + num_ops)); | ||
| 236 | |||
| 237 | if (req->r_reassert_version.epoch) | ||
| 238 | seq_printf(s, "\t%u'%llu", | ||
| 239 | (unsigned)le32_to_cpu(req->r_reassert_version.epoch), | ||
| 240 | le64_to_cpu(req->r_reassert_version.version)); | ||
| 241 | else | ||
| 242 | seq_printf(s, "\t"); | ||
| 243 | |||
| 244 | for (i = 0; i < num_ops; i++) { | ||
| 245 | opcode = le16_to_cpu(op->op); | ||
| 246 | seq_printf(s, "\t%s", ceph_osd_op_name(opcode)); | ||
| 247 | op++; | ||
| 248 | } | ||
| 249 | |||
| 250 | seq_printf(s, "\n"); | ||
| 251 | } | ||
| 252 | mutex_unlock(&osdc->request_mutex); | ||
| 253 | return 0; | ||
| 254 | } | ||
| 255 | |||
| 256 | static int caps_show(struct seq_file *s, void *p) | ||
| 257 | { | ||
| 258 | struct ceph_client *client = p; | ||
| 259 | int total, avail, used, reserved, min; | ||
| 260 | |||
| 261 | ceph_reservation_status(client, &total, &avail, &used, &reserved, &min); | ||
| 262 | seq_printf(s, "total\t\t%d\n" | ||
| 263 | "avail\t\t%d\n" | ||
| 264 | "used\t\t%d\n" | ||
| 265 | "reserved\t%d\n" | ||
| 266 | "min\t%d\n", | ||
| 267 | total, avail, used, reserved, min); | ||
| 268 | return 0; | ||
| 269 | } | ||
| 270 | |||
| 271 | static int dentry_lru_show(struct seq_file *s, void *ptr) | ||
| 272 | { | ||
| 273 | struct ceph_client *client = s->private; | ||
| 274 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 275 | struct ceph_dentry_info *di; | ||
| 276 | |||
| 277 | spin_lock(&mdsc->dentry_lru_lock); | ||
| 278 | list_for_each_entry(di, &mdsc->dentry_lru, lru) { | ||
| 279 | struct dentry *dentry = di->dentry; | ||
| 280 | seq_printf(s, "%p %p\t%.*s\n", | ||
| 281 | di, dentry, dentry->d_name.len, dentry->d_name.name); | ||
| 282 | } | ||
| 283 | spin_unlock(&mdsc->dentry_lru_lock); | ||
| 284 | |||
| 285 | return 0; | ||
| 286 | } | ||
| 287 | |||
| 288 | #define DEFINE_SHOW_FUNC(name) \ | ||
| 289 | static int name##_open(struct inode *inode, struct file *file) \ | ||
| 290 | { \ | ||
| 291 | struct seq_file *sf; \ | ||
| 292 | int ret; \ | ||
| 293 | \ | ||
| 294 | ret = single_open(file, name, NULL); \ | ||
| 295 | sf = file->private_data; \ | ||
| 296 | sf->private = inode->i_private; \ | ||
| 297 | return ret; \ | ||
| 298 | } \ | ||
| 299 | \ | ||
| 300 | static const struct file_operations name##_fops = { \ | ||
| 301 | .open = name##_open, \ | ||
| 302 | .read = seq_read, \ | ||
| 303 | .llseek = seq_lseek, \ | ||
| 304 | .release = single_release, \ | ||
| 305 | }; | ||
| 306 | |||
| 307 | DEFINE_SHOW_FUNC(monmap_show) | ||
| 308 | DEFINE_SHOW_FUNC(mdsmap_show) | ||
| 309 | DEFINE_SHOW_FUNC(osdmap_show) | ||
| 310 | DEFINE_SHOW_FUNC(monc_show) | ||
| 311 | DEFINE_SHOW_FUNC(mdsc_show) | ||
| 312 | DEFINE_SHOW_FUNC(osdc_show) | ||
| 313 | DEFINE_SHOW_FUNC(dentry_lru_show) | ||
| 314 | DEFINE_SHOW_FUNC(caps_show) | ||
| 315 | |||
| 316 | static int congestion_kb_set(void *data, u64 val) | ||
| 317 | { | ||
| 318 | struct ceph_client *client = (struct ceph_client *)data; | ||
| 319 | |||
| 320 | if (client) | ||
| 321 | client->mount_args->congestion_kb = (int)val; | ||
| 322 | |||
| 323 | return 0; | ||
| 324 | } | ||
| 325 | |||
| 326 | static int congestion_kb_get(void *data, u64 *val) | ||
| 327 | { | ||
| 328 | struct ceph_client *client = (struct ceph_client *)data; | ||
| 329 | |||
| 330 | if (client) | ||
| 331 | *val = (u64)client->mount_args->congestion_kb; | ||
| 332 | |||
| 333 | return 0; | ||
| 334 | } | ||
| 335 | |||
| 336 | |||
| 337 | DEFINE_SIMPLE_ATTRIBUTE(congestion_kb_fops, congestion_kb_get, | ||
| 338 | congestion_kb_set, "%llu\n"); | ||
| 339 | |||
| 340 | int __init ceph_debugfs_init(void) | ||
| 341 | { | ||
| 342 | ceph_debugfs_dir = debugfs_create_dir("ceph", NULL); | ||
| 343 | if (!ceph_debugfs_dir) | ||
| 344 | return -ENOMEM; | ||
| 345 | return 0; | ||
| 346 | } | ||
| 347 | |||
| 348 | void ceph_debugfs_cleanup(void) | ||
| 349 | { | ||
| 350 | debugfs_remove(ceph_debugfs_dir); | ||
| 351 | } | ||
| 352 | |||
| 353 | int ceph_debugfs_client_init(struct ceph_client *client) | ||
| 354 | { | ||
| 355 | int ret = 0; | ||
| 356 | char name[80]; | ||
| 357 | |||
| 358 | snprintf(name, sizeof(name), FSID_FORMAT ".client%lld", | ||
| 359 | PR_FSID(&client->fsid), client->monc.auth->global_id); | ||
| 360 | |||
| 361 | client->debugfs_dir = debugfs_create_dir(name, ceph_debugfs_dir); | ||
| 362 | if (!client->debugfs_dir) | ||
| 363 | goto out; | ||
| 364 | |||
| 365 | client->monc.debugfs_file = debugfs_create_file("monc", | ||
| 366 | 0600, | ||
| 367 | client->debugfs_dir, | ||
| 368 | client, | ||
| 369 | &monc_show_fops); | ||
| 370 | if (!client->monc.debugfs_file) | ||
| 371 | goto out; | ||
| 372 | |||
| 373 | client->mdsc.debugfs_file = debugfs_create_file("mdsc", | ||
| 374 | 0600, | ||
| 375 | client->debugfs_dir, | ||
| 376 | client, | ||
| 377 | &mdsc_show_fops); | ||
| 378 | if (!client->mdsc.debugfs_file) | ||
| 379 | goto out; | ||
| 380 | |||
| 381 | client->osdc.debugfs_file = debugfs_create_file("osdc", | ||
| 382 | 0600, | ||
| 383 | client->debugfs_dir, | ||
| 384 | client, | ||
| 385 | &osdc_show_fops); | ||
| 386 | if (!client->osdc.debugfs_file) | ||
| 387 | goto out; | ||
| 388 | |||
| 389 | client->debugfs_monmap = debugfs_create_file("monmap", | ||
| 390 | 0600, | ||
| 391 | client->debugfs_dir, | ||
| 392 | client, | ||
| 393 | &monmap_show_fops); | ||
| 394 | if (!client->debugfs_monmap) | ||
| 395 | goto out; | ||
| 396 | |||
| 397 | client->debugfs_mdsmap = debugfs_create_file("mdsmap", | ||
| 398 | 0600, | ||
| 399 | client->debugfs_dir, | ||
| 400 | client, | ||
| 401 | &mdsmap_show_fops); | ||
| 402 | if (!client->debugfs_mdsmap) | ||
| 403 | goto out; | ||
| 404 | |||
| 405 | client->debugfs_osdmap = debugfs_create_file("osdmap", | ||
| 406 | 0600, | ||
| 407 | client->debugfs_dir, | ||
| 408 | client, | ||
| 409 | &osdmap_show_fops); | ||
| 410 | if (!client->debugfs_osdmap) | ||
| 411 | goto out; | ||
| 412 | |||
| 413 | client->debugfs_dentry_lru = debugfs_create_file("dentry_lru", | ||
| 414 | 0600, | ||
| 415 | client->debugfs_dir, | ||
| 416 | client, | ||
| 417 | &dentry_lru_show_fops); | ||
| 418 | if (!client->debugfs_dentry_lru) | ||
| 419 | goto out; | ||
| 420 | |||
| 421 | client->debugfs_caps = debugfs_create_file("caps", | ||
| 422 | 0400, | ||
| 423 | client->debugfs_dir, | ||
| 424 | client, | ||
| 425 | &caps_show_fops); | ||
| 426 | if (!client->debugfs_caps) | ||
| 427 | goto out; | ||
| 428 | |||
| 429 | client->debugfs_congestion_kb = debugfs_create_file("writeback_congestion_kb", | ||
| 430 | 0600, | ||
| 431 | client->debugfs_dir, | ||
| 432 | client, | ||
| 433 | &congestion_kb_fops); | ||
| 434 | if (!client->debugfs_congestion_kb) | ||
| 435 | goto out; | ||
| 436 | |||
| 437 | sprintf(name, "../../bdi/%s", dev_name(client->sb->s_bdi->dev)); | ||
| 438 | client->debugfs_bdi = debugfs_create_symlink("bdi", client->debugfs_dir, | ||
| 439 | name); | ||
| 440 | |||
| 441 | return 0; | ||
| 442 | |||
| 443 | out: | ||
| 444 | ceph_debugfs_client_cleanup(client); | ||
| 445 | return ret; | ||
| 446 | } | ||
| 447 | |||
| 448 | void ceph_debugfs_client_cleanup(struct ceph_client *client) | ||
| 449 | { | ||
| 450 | debugfs_remove(client->debugfs_bdi); | ||
| 451 | debugfs_remove(client->debugfs_caps); | ||
| 452 | debugfs_remove(client->debugfs_dentry_lru); | ||
| 453 | debugfs_remove(client->debugfs_osdmap); | ||
| 454 | debugfs_remove(client->debugfs_mdsmap); | ||
| 455 | debugfs_remove(client->debugfs_monmap); | ||
| 456 | debugfs_remove(client->osdc.debugfs_file); | ||
| 457 | debugfs_remove(client->mdsc.debugfs_file); | ||
| 458 | debugfs_remove(client->monc.debugfs_file); | ||
| 459 | debugfs_remove(client->debugfs_congestion_kb); | ||
| 460 | debugfs_remove(client->debugfs_dir); | ||
| 461 | } | ||
| 462 | |||
| 463 | #else // CONFIG_DEBUG_FS | ||
| 464 | |||
| 465 | int __init ceph_debugfs_init(void) | ||
| 466 | { | ||
| 467 | return 0; | ||
| 468 | } | ||
| 469 | |||
| 470 | void ceph_debugfs_cleanup(void) | ||
| 471 | { | ||
| 472 | } | ||
| 473 | |||
| 474 | int ceph_debugfs_client_init(struct ceph_client *client) | ||
| 475 | { | ||
| 476 | return 0; | ||
| 477 | } | ||
| 478 | |||
| 479 | void ceph_debugfs_client_cleanup(struct ceph_client *client) | ||
| 480 | { | ||
| 481 | } | ||
| 482 | |||
| 483 | #endif // CONFIG_DEBUG_FS | ||
diff --git a/fs/ceph/decode.h b/fs/ceph/decode.h new file mode 100644 index 000000000000..65b3e022eaf5 --- /dev/null +++ b/fs/ceph/decode.h | |||
| @@ -0,0 +1,194 @@ | |||
| 1 | #ifndef __CEPH_DECODE_H | ||
| 2 | #define __CEPH_DECODE_H | ||
| 3 | |||
| 4 | #include <asm/unaligned.h> | ||
| 5 | #include <linux/time.h> | ||
| 6 | |||
| 7 | #include "types.h" | ||
| 8 | |||
| 9 | /* | ||
| 10 | * in all cases, | ||
| 11 | * void **p pointer to position pointer | ||
| 12 | * void *end pointer to end of buffer (last byte + 1) | ||
| 13 | */ | ||
| 14 | |||
| 15 | static inline u64 ceph_decode_64(void **p) | ||
| 16 | { | ||
| 17 | u64 v = get_unaligned_le64(*p); | ||
| 18 | *p += sizeof(u64); | ||
| 19 | return v; | ||
| 20 | } | ||
| 21 | static inline u32 ceph_decode_32(void **p) | ||
| 22 | { | ||
| 23 | u32 v = get_unaligned_le32(*p); | ||
| 24 | *p += sizeof(u32); | ||
| 25 | return v; | ||
| 26 | } | ||
| 27 | static inline u16 ceph_decode_16(void **p) | ||
| 28 | { | ||
| 29 | u16 v = get_unaligned_le16(*p); | ||
| 30 | *p += sizeof(u16); | ||
| 31 | return v; | ||
| 32 | } | ||
| 33 | static inline u8 ceph_decode_8(void **p) | ||
| 34 | { | ||
| 35 | u8 v = *(u8 *)*p; | ||
| 36 | (*p)++; | ||
| 37 | return v; | ||
| 38 | } | ||
| 39 | static inline void ceph_decode_copy(void **p, void *pv, size_t n) | ||
| 40 | { | ||
| 41 | memcpy(pv, *p, n); | ||
| 42 | *p += n; | ||
| 43 | } | ||
| 44 | |||
| 45 | /* | ||
| 46 | * bounds check input. | ||
| 47 | */ | ||
| 48 | #define ceph_decode_need(p, end, n, bad) \ | ||
| 49 | do { \ | ||
| 50 | if (unlikely(*(p) + (n) > (end))) \ | ||
| 51 | goto bad; \ | ||
| 52 | } while (0) | ||
| 53 | |||
| 54 | #define ceph_decode_64_safe(p, end, v, bad) \ | ||
| 55 | do { \ | ||
| 56 | ceph_decode_need(p, end, sizeof(u64), bad); \ | ||
| 57 | v = ceph_decode_64(p); \ | ||
| 58 | } while (0) | ||
| 59 | #define ceph_decode_32_safe(p, end, v, bad) \ | ||
| 60 | do { \ | ||
| 61 | ceph_decode_need(p, end, sizeof(u32), bad); \ | ||
| 62 | v = ceph_decode_32(p); \ | ||
| 63 | } while (0) | ||
| 64 | #define ceph_decode_16_safe(p, end, v, bad) \ | ||
| 65 | do { \ | ||
| 66 | ceph_decode_need(p, end, sizeof(u16), bad); \ | ||
| 67 | v = ceph_decode_16(p); \ | ||
| 68 | } while (0) | ||
| 69 | #define ceph_decode_8_safe(p, end, v, bad) \ | ||
| 70 | do { \ | ||
| 71 | ceph_decode_need(p, end, sizeof(u8), bad); \ | ||
| 72 | v = ceph_decode_8(p); \ | ||
| 73 | } while (0) | ||
| 74 | |||
| 75 | #define ceph_decode_copy_safe(p, end, pv, n, bad) \ | ||
| 76 | do { \ | ||
| 77 | ceph_decode_need(p, end, n, bad); \ | ||
| 78 | ceph_decode_copy(p, pv, n); \ | ||
| 79 | } while (0) | ||
| 80 | |||
| 81 | /* | ||
| 82 | * struct ceph_timespec <-> struct timespec | ||
| 83 | */ | ||
| 84 | static inline void ceph_decode_timespec(struct timespec *ts, | ||
| 85 | const struct ceph_timespec *tv) | ||
| 86 | { | ||
| 87 | ts->tv_sec = le32_to_cpu(tv->tv_sec); | ||
| 88 | ts->tv_nsec = le32_to_cpu(tv->tv_nsec); | ||
| 89 | } | ||
| 90 | static inline void ceph_encode_timespec(struct ceph_timespec *tv, | ||
| 91 | const struct timespec *ts) | ||
| 92 | { | ||
| 93 | tv->tv_sec = cpu_to_le32(ts->tv_sec); | ||
| 94 | tv->tv_nsec = cpu_to_le32(ts->tv_nsec); | ||
| 95 | } | ||
| 96 | |||
| 97 | /* | ||
| 98 | * sockaddr_storage <-> ceph_sockaddr | ||
| 99 | */ | ||
| 100 | static inline void ceph_encode_addr(struct ceph_entity_addr *a) | ||
| 101 | { | ||
| 102 | a->in_addr.ss_family = htons(a->in_addr.ss_family); | ||
| 103 | } | ||
| 104 | static inline void ceph_decode_addr(struct ceph_entity_addr *a) | ||
| 105 | { | ||
| 106 | a->in_addr.ss_family = ntohs(a->in_addr.ss_family); | ||
| 107 | WARN_ON(a->in_addr.ss_family == 512); | ||
| 108 | } | ||
| 109 | |||
| 110 | /* | ||
| 111 | * encoders | ||
| 112 | */ | ||
| 113 | static inline void ceph_encode_64(void **p, u64 v) | ||
| 114 | { | ||
| 115 | put_unaligned_le64(v, (__le64 *)*p); | ||
| 116 | *p += sizeof(u64); | ||
| 117 | } | ||
| 118 | static inline void ceph_encode_32(void **p, u32 v) | ||
| 119 | { | ||
| 120 | put_unaligned_le32(v, (__le32 *)*p); | ||
| 121 | *p += sizeof(u32); | ||
| 122 | } | ||
| 123 | static inline void ceph_encode_16(void **p, u16 v) | ||
| 124 | { | ||
| 125 | put_unaligned_le16(v, (__le16 *)*p); | ||
| 126 | *p += sizeof(u16); | ||
| 127 | } | ||
| 128 | static inline void ceph_encode_8(void **p, u8 v) | ||
| 129 | { | ||
| 130 | *(u8 *)*p = v; | ||
| 131 | (*p)++; | ||
| 132 | } | ||
| 133 | static inline void ceph_encode_copy(void **p, const void *s, int len) | ||
| 134 | { | ||
| 135 | memcpy(*p, s, len); | ||
| 136 | *p += len; | ||
| 137 | } | ||
| 138 | |||
| 139 | /* | ||
| 140 | * filepath, string encoders | ||
| 141 | */ | ||
| 142 | static inline void ceph_encode_filepath(void **p, void *end, | ||
| 143 | u64 ino, const char *path) | ||
| 144 | { | ||
| 145 | u32 len = path ? strlen(path) : 0; | ||
| 146 | BUG_ON(*p + sizeof(ino) + sizeof(len) + len > end); | ||
| 147 | ceph_encode_8(p, 1); | ||
| 148 | ceph_encode_64(p, ino); | ||
| 149 | ceph_encode_32(p, len); | ||
| 150 | if (len) | ||
| 151 | memcpy(*p, path, len); | ||
| 152 | *p += len; | ||
| 153 | } | ||
| 154 | |||
| 155 | static inline void ceph_encode_string(void **p, void *end, | ||
| 156 | const char *s, u32 len) | ||
| 157 | { | ||
| 158 | BUG_ON(*p + sizeof(len) + len > end); | ||
| 159 | ceph_encode_32(p, len); | ||
| 160 | if (len) | ||
| 161 | memcpy(*p, s, len); | ||
| 162 | *p += len; | ||
| 163 | } | ||
| 164 | |||
| 165 | #define ceph_encode_need(p, end, n, bad) \ | ||
| 166 | do { \ | ||
| 167 | if (unlikely(*(p) + (n) > (end))) \ | ||
| 168 | goto bad; \ | ||
| 169 | } while (0) | ||
| 170 | |||
| 171 | #define ceph_encode_64_safe(p, end, v, bad) \ | ||
| 172 | do { \ | ||
| 173 | ceph_encode_need(p, end, sizeof(u64), bad); \ | ||
| 174 | ceph_encode_64(p, v); \ | ||
| 175 | } while (0) | ||
| 176 | #define ceph_encode_32_safe(p, end, v, bad) \ | ||
| 177 | do { \ | ||
| 178 | ceph_encode_need(p, end, sizeof(u32), bad); \ | ||
| 179 | ceph_encode_32(p, v); \ | ||
| 180 | } while (0) | ||
| 181 | #define ceph_encode_16_safe(p, end, v, bad) \ | ||
| 182 | do { \ | ||
| 183 | ceph_encode_need(p, end, sizeof(u16), bad); \ | ||
| 184 | ceph_encode_16(p, v); \ | ||
| 185 | } while (0) | ||
| 186 | |||
| 187 | #define ceph_encode_copy_safe(p, end, pv, n, bad) \ | ||
| 188 | do { \ | ||
| 189 | ceph_encode_need(p, end, n, bad); \ | ||
| 190 | ceph_encode_copy(p, pv, n); \ | ||
| 191 | } while (0) | ||
| 192 | |||
| 193 | |||
| 194 | #endif | ||
diff --git a/fs/ceph/dir.c b/fs/ceph/dir.c new file mode 100644 index 000000000000..8a9116e15b70 --- /dev/null +++ b/fs/ceph/dir.c | |||
| @@ -0,0 +1,1222 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/spinlock.h> | ||
| 4 | #include <linux/fs_struct.h> | ||
| 5 | #include <linux/namei.h> | ||
| 6 | #include <linux/sched.h> | ||
| 7 | |||
| 8 | #include "super.h" | ||
| 9 | |||
| 10 | /* | ||
| 11 | * Directory operations: readdir, lookup, create, link, unlink, | ||
| 12 | * rename, etc. | ||
| 13 | */ | ||
| 14 | |||
| 15 | /* | ||
| 16 | * Ceph MDS operations are specified in terms of a base ino and | ||
| 17 | * relative path. Thus, the client can specify an operation on a | ||
| 18 | * specific inode (e.g., a getattr due to fstat(2)), or as a path | ||
| 19 | * relative to, say, the root directory. | ||
| 20 | * | ||
| 21 | * Normally, we limit ourselves to strict inode ops (no path component) | ||
| 22 | * or dentry operations (a single path component relative to an ino). The | ||
| 23 | * exception to this is open_root_dentry(), which will open the mount | ||
| 24 | * point by name. | ||
| 25 | */ | ||
| 26 | |||
| 27 | const struct inode_operations ceph_dir_iops; | ||
| 28 | const struct file_operations ceph_dir_fops; | ||
| 29 | struct dentry_operations ceph_dentry_ops; | ||
| 30 | |||
| 31 | /* | ||
| 32 | * Initialize ceph dentry state. | ||
| 33 | */ | ||
| 34 | int ceph_init_dentry(struct dentry *dentry) | ||
| 35 | { | ||
| 36 | struct ceph_dentry_info *di; | ||
| 37 | |||
| 38 | if (dentry->d_fsdata) | ||
| 39 | return 0; | ||
| 40 | |||
| 41 | if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) | ||
| 42 | dentry->d_op = &ceph_dentry_ops; | ||
| 43 | else if (ceph_snap(dentry->d_parent->d_inode) == CEPH_SNAPDIR) | ||
| 44 | dentry->d_op = &ceph_snapdir_dentry_ops; | ||
| 45 | else | ||
| 46 | dentry->d_op = &ceph_snap_dentry_ops; | ||
| 47 | |||
| 48 | di = kmem_cache_alloc(ceph_dentry_cachep, GFP_NOFS); | ||
| 49 | if (!di) | ||
| 50 | return -ENOMEM; /* oh well */ | ||
| 51 | |||
| 52 | spin_lock(&dentry->d_lock); | ||
| 53 | if (dentry->d_fsdata) /* lost a race */ | ||
| 54 | goto out_unlock; | ||
| 55 | di->dentry = dentry; | ||
| 56 | di->lease_session = NULL; | ||
| 57 | dentry->d_fsdata = di; | ||
| 58 | dentry->d_time = jiffies; | ||
| 59 | ceph_dentry_lru_add(dentry); | ||
| 60 | out_unlock: | ||
| 61 | spin_unlock(&dentry->d_lock); | ||
| 62 | return 0; | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | /* | ||
| 68 | * for readdir, we encode the directory frag and offset within that | ||
| 69 | * frag into f_pos. | ||
| 70 | */ | ||
| 71 | static unsigned fpos_frag(loff_t p) | ||
| 72 | { | ||
| 73 | return p >> 32; | ||
| 74 | } | ||
| 75 | static unsigned fpos_off(loff_t p) | ||
| 76 | { | ||
| 77 | return p & 0xffffffff; | ||
| 78 | } | ||
| 79 | |||
| 80 | /* | ||
| 81 | * When possible, we try to satisfy a readdir by peeking at the | ||
| 82 | * dcache. We make this work by carefully ordering dentries on | ||
| 83 | * d_u.d_child when we initially get results back from the MDS, and | ||
| 84 | * falling back to a "normal" sync readdir if any dentries in the dir | ||
| 85 | * are dropped. | ||
| 86 | * | ||
| 87 | * I_COMPLETE tells indicates we have all dentries in the dir. It is | ||
| 88 | * defined IFF we hold CEPH_CAP_FILE_SHARED (which will be revoked by | ||
| 89 | * the MDS if/when the directory is modified). | ||
| 90 | */ | ||
| 91 | static int __dcache_readdir(struct file *filp, | ||
| 92 | void *dirent, filldir_t filldir) | ||
| 93 | { | ||
| 94 | struct inode *inode = filp->f_dentry->d_inode; | ||
| 95 | struct ceph_file_info *fi = filp->private_data; | ||
| 96 | struct dentry *parent = filp->f_dentry; | ||
| 97 | struct inode *dir = parent->d_inode; | ||
| 98 | struct list_head *p; | ||
| 99 | struct dentry *dentry, *last; | ||
| 100 | struct ceph_dentry_info *di; | ||
| 101 | int err = 0; | ||
| 102 | |||
| 103 | /* claim ref on last dentry we returned */ | ||
| 104 | last = fi->dentry; | ||
| 105 | fi->dentry = NULL; | ||
| 106 | |||
| 107 | dout("__dcache_readdir %p at %llu (last %p)\n", dir, filp->f_pos, | ||
| 108 | last); | ||
| 109 | |||
| 110 | spin_lock(&dcache_lock); | ||
| 111 | |||
| 112 | /* start at beginning? */ | ||
| 113 | if (filp->f_pos == 2 || (last && | ||
| 114 | filp->f_pos < ceph_dentry(last)->offset)) { | ||
| 115 | if (list_empty(&parent->d_subdirs)) | ||
| 116 | goto out_unlock; | ||
| 117 | p = parent->d_subdirs.prev; | ||
| 118 | dout(" initial p %p/%p\n", p->prev, p->next); | ||
| 119 | } else { | ||
| 120 | p = last->d_u.d_child.prev; | ||
| 121 | } | ||
| 122 | |||
| 123 | more: | ||
| 124 | dentry = list_entry(p, struct dentry, d_u.d_child); | ||
| 125 | di = ceph_dentry(dentry); | ||
| 126 | while (1) { | ||
| 127 | dout(" p %p/%p d_subdirs %p/%p\n", p->prev, p->next, | ||
| 128 | parent->d_subdirs.prev, parent->d_subdirs.next); | ||
| 129 | if (p == &parent->d_subdirs) { | ||
| 130 | fi->at_end = 1; | ||
| 131 | goto out_unlock; | ||
| 132 | } | ||
| 133 | if (!d_unhashed(dentry) && dentry->d_inode && | ||
| 134 | ceph_snap(dentry->d_inode) != CEPH_SNAPDIR && | ||
| 135 | ceph_ino(dentry->d_inode) != CEPH_INO_CEPH && | ||
| 136 | filp->f_pos <= di->offset) | ||
| 137 | break; | ||
| 138 | dout(" skipping %p %.*s at %llu (%llu)%s%s\n", dentry, | ||
| 139 | dentry->d_name.len, dentry->d_name.name, di->offset, | ||
| 140 | filp->f_pos, d_unhashed(dentry) ? " unhashed" : "", | ||
| 141 | !dentry->d_inode ? " null" : ""); | ||
| 142 | p = p->prev; | ||
| 143 | dentry = list_entry(p, struct dentry, d_u.d_child); | ||
| 144 | di = ceph_dentry(dentry); | ||
| 145 | } | ||
| 146 | |||
| 147 | atomic_inc(&dentry->d_count); | ||
| 148 | spin_unlock(&dcache_lock); | ||
| 149 | spin_unlock(&inode->i_lock); | ||
| 150 | |||
| 151 | dout(" %llu (%llu) dentry %p %.*s %p\n", di->offset, filp->f_pos, | ||
| 152 | dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode); | ||
| 153 | filp->f_pos = di->offset; | ||
| 154 | err = filldir(dirent, dentry->d_name.name, | ||
| 155 | dentry->d_name.len, di->offset, | ||
| 156 | dentry->d_inode->i_ino, | ||
| 157 | dentry->d_inode->i_mode >> 12); | ||
| 158 | |||
| 159 | if (last) { | ||
| 160 | if (err < 0) { | ||
| 161 | /* remember our position */ | ||
| 162 | fi->dentry = last; | ||
| 163 | fi->next_offset = di->offset; | ||
| 164 | } else { | ||
| 165 | dput(last); | ||
| 166 | } | ||
| 167 | last = NULL; | ||
| 168 | } | ||
| 169 | |||
| 170 | spin_lock(&inode->i_lock); | ||
| 171 | spin_lock(&dcache_lock); | ||
| 172 | |||
| 173 | if (err < 0) | ||
| 174 | goto out_unlock; | ||
| 175 | |||
| 176 | last = dentry; | ||
| 177 | |||
| 178 | p = p->prev; | ||
| 179 | filp->f_pos++; | ||
| 180 | |||
| 181 | /* make sure a dentry wasn't dropped while we didn't have dcache_lock */ | ||
| 182 | if ((ceph_inode(dir)->i_ceph_flags & CEPH_I_COMPLETE)) | ||
| 183 | goto more; | ||
| 184 | dout(" lost I_COMPLETE on %p; falling back to mds\n", dir); | ||
| 185 | err = -EAGAIN; | ||
| 186 | |||
| 187 | out_unlock: | ||
| 188 | spin_unlock(&dcache_lock); | ||
| 189 | |||
| 190 | if (last) { | ||
| 191 | spin_unlock(&inode->i_lock); | ||
| 192 | dput(last); | ||
| 193 | spin_lock(&inode->i_lock); | ||
| 194 | } | ||
| 195 | |||
| 196 | return err; | ||
| 197 | } | ||
| 198 | |||
| 199 | /* | ||
| 200 | * make note of the last dentry we read, so we can | ||
| 201 | * continue at the same lexicographical point, | ||
| 202 | * regardless of what dir changes take place on the | ||
| 203 | * server. | ||
| 204 | */ | ||
| 205 | static int note_last_dentry(struct ceph_file_info *fi, const char *name, | ||
| 206 | int len) | ||
| 207 | { | ||
| 208 | kfree(fi->last_name); | ||
| 209 | fi->last_name = kmalloc(len+1, GFP_NOFS); | ||
| 210 | if (!fi->last_name) | ||
| 211 | return -ENOMEM; | ||
| 212 | memcpy(fi->last_name, name, len); | ||
| 213 | fi->last_name[len] = 0; | ||
| 214 | dout("note_last_dentry '%s'\n", fi->last_name); | ||
| 215 | return 0; | ||
| 216 | } | ||
| 217 | |||
| 218 | static int ceph_readdir(struct file *filp, void *dirent, filldir_t filldir) | ||
| 219 | { | ||
| 220 | struct ceph_file_info *fi = filp->private_data; | ||
| 221 | struct inode *inode = filp->f_dentry->d_inode; | ||
| 222 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 223 | struct ceph_client *client = ceph_inode_to_client(inode); | ||
| 224 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 225 | unsigned frag = fpos_frag(filp->f_pos); | ||
| 226 | int off = fpos_off(filp->f_pos); | ||
| 227 | int err; | ||
| 228 | u32 ftype; | ||
| 229 | struct ceph_mds_reply_info_parsed *rinfo; | ||
| 230 | const int max_entries = client->mount_args->max_readdir; | ||
| 231 | |||
| 232 | dout("readdir %p filp %p frag %u off %u\n", inode, filp, frag, off); | ||
| 233 | if (fi->at_end) | ||
| 234 | return 0; | ||
| 235 | |||
| 236 | /* always start with . and .. */ | ||
| 237 | if (filp->f_pos == 0) { | ||
| 238 | /* note dir version at start of readdir so we can tell | ||
| 239 | * if any dentries get dropped */ | ||
| 240 | fi->dir_release_count = ci->i_release_count; | ||
| 241 | |||
| 242 | dout("readdir off 0 -> '.'\n"); | ||
| 243 | if (filldir(dirent, ".", 1, ceph_make_fpos(0, 0), | ||
| 244 | inode->i_ino, inode->i_mode >> 12) < 0) | ||
| 245 | return 0; | ||
| 246 | filp->f_pos = 1; | ||
| 247 | off = 1; | ||
| 248 | } | ||
| 249 | if (filp->f_pos == 1) { | ||
| 250 | dout("readdir off 1 -> '..'\n"); | ||
| 251 | if (filldir(dirent, "..", 2, ceph_make_fpos(0, 1), | ||
| 252 | filp->f_dentry->d_parent->d_inode->i_ino, | ||
| 253 | inode->i_mode >> 12) < 0) | ||
| 254 | return 0; | ||
| 255 | filp->f_pos = 2; | ||
| 256 | off = 2; | ||
| 257 | } | ||
| 258 | |||
| 259 | /* can we use the dcache? */ | ||
| 260 | spin_lock(&inode->i_lock); | ||
| 261 | if ((filp->f_pos == 2 || fi->dentry) && | ||
| 262 | !ceph_test_opt(client, NOASYNCREADDIR) && | ||
| 263 | (ci->i_ceph_flags & CEPH_I_COMPLETE) && | ||
| 264 | __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1)) { | ||
| 265 | err = __dcache_readdir(filp, dirent, filldir); | ||
| 266 | if (err != -EAGAIN) { | ||
| 267 | spin_unlock(&inode->i_lock); | ||
| 268 | return err; | ||
| 269 | } | ||
| 270 | } | ||
| 271 | spin_unlock(&inode->i_lock); | ||
| 272 | if (fi->dentry) { | ||
| 273 | err = note_last_dentry(fi, fi->dentry->d_name.name, | ||
| 274 | fi->dentry->d_name.len); | ||
| 275 | if (err) | ||
| 276 | return err; | ||
| 277 | dput(fi->dentry); | ||
| 278 | fi->dentry = NULL; | ||
| 279 | } | ||
| 280 | |||
| 281 | /* proceed with a normal readdir */ | ||
| 282 | |||
| 283 | more: | ||
| 284 | /* do we have the correct frag content buffered? */ | ||
| 285 | if (fi->frag != frag || fi->last_readdir == NULL) { | ||
| 286 | struct ceph_mds_request *req; | ||
| 287 | int op = ceph_snap(inode) == CEPH_SNAPDIR ? | ||
| 288 | CEPH_MDS_OP_LSSNAP : CEPH_MDS_OP_READDIR; | ||
| 289 | |||
| 290 | /* discard old result, if any */ | ||
| 291 | if (fi->last_readdir) { | ||
| 292 | ceph_mdsc_put_request(fi->last_readdir); | ||
| 293 | fi->last_readdir = NULL; | ||
| 294 | } | ||
| 295 | |||
| 296 | /* requery frag tree, as the frag topology may have changed */ | ||
| 297 | frag = ceph_choose_frag(ceph_inode(inode), frag, NULL, NULL); | ||
| 298 | |||
| 299 | dout("readdir fetching %llx.%llx frag %x offset '%s'\n", | ||
| 300 | ceph_vinop(inode), frag, fi->last_name); | ||
| 301 | req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); | ||
| 302 | if (IS_ERR(req)) | ||
| 303 | return PTR_ERR(req); | ||
| 304 | req->r_inode = igrab(inode); | ||
| 305 | req->r_dentry = dget(filp->f_dentry); | ||
| 306 | /* hints to request -> mds selection code */ | ||
| 307 | req->r_direct_mode = USE_AUTH_MDS; | ||
| 308 | req->r_direct_hash = ceph_frag_value(frag); | ||
| 309 | req->r_direct_is_hash = true; | ||
| 310 | req->r_path2 = kstrdup(fi->last_name, GFP_NOFS); | ||
| 311 | req->r_readdir_offset = fi->next_offset; | ||
| 312 | req->r_args.readdir.frag = cpu_to_le32(frag); | ||
| 313 | req->r_args.readdir.max_entries = cpu_to_le32(max_entries); | ||
| 314 | req->r_num_caps = max_entries; | ||
| 315 | err = ceph_mdsc_do_request(mdsc, NULL, req); | ||
| 316 | if (err < 0) { | ||
| 317 | ceph_mdsc_put_request(req); | ||
| 318 | return err; | ||
| 319 | } | ||
| 320 | dout("readdir got and parsed readdir result=%d" | ||
| 321 | " on frag %x, end=%d, complete=%d\n", err, frag, | ||
| 322 | (int)req->r_reply_info.dir_end, | ||
| 323 | (int)req->r_reply_info.dir_complete); | ||
| 324 | |||
| 325 | if (!req->r_did_prepopulate) { | ||
| 326 | dout("readdir !did_prepopulate"); | ||
| 327 | fi->dir_release_count--; /* preclude I_COMPLETE */ | ||
| 328 | } | ||
| 329 | |||
| 330 | /* note next offset and last dentry name */ | ||
| 331 | fi->offset = fi->next_offset; | ||
| 332 | fi->last_readdir = req; | ||
| 333 | |||
| 334 | if (req->r_reply_info.dir_end) { | ||
| 335 | kfree(fi->last_name); | ||
| 336 | fi->last_name = NULL; | ||
| 337 | fi->next_offset = 0; | ||
| 338 | } else { | ||
| 339 | rinfo = &req->r_reply_info; | ||
| 340 | err = note_last_dentry(fi, | ||
| 341 | rinfo->dir_dname[rinfo->dir_nr-1], | ||
| 342 | rinfo->dir_dname_len[rinfo->dir_nr-1]); | ||
| 343 | if (err) | ||
| 344 | return err; | ||
| 345 | fi->next_offset += rinfo->dir_nr; | ||
| 346 | } | ||
| 347 | } | ||
| 348 | |||
| 349 | rinfo = &fi->last_readdir->r_reply_info; | ||
| 350 | dout("readdir frag %x num %d off %d chunkoff %d\n", frag, | ||
| 351 | rinfo->dir_nr, off, fi->offset); | ||
| 352 | while (off - fi->offset >= 0 && off - fi->offset < rinfo->dir_nr) { | ||
| 353 | u64 pos = ceph_make_fpos(frag, off); | ||
| 354 | struct ceph_mds_reply_inode *in = | ||
| 355 | rinfo->dir_in[off - fi->offset].in; | ||
| 356 | dout("readdir off %d (%d/%d) -> %lld '%.*s' %p\n", | ||
| 357 | off, off - fi->offset, rinfo->dir_nr, pos, | ||
| 358 | rinfo->dir_dname_len[off - fi->offset], | ||
| 359 | rinfo->dir_dname[off - fi->offset], in); | ||
| 360 | BUG_ON(!in); | ||
| 361 | ftype = le32_to_cpu(in->mode) >> 12; | ||
| 362 | if (filldir(dirent, | ||
| 363 | rinfo->dir_dname[off - fi->offset], | ||
| 364 | rinfo->dir_dname_len[off - fi->offset], | ||
| 365 | pos, | ||
| 366 | le64_to_cpu(in->ino), | ||
| 367 | ftype) < 0) { | ||
| 368 | dout("filldir stopping us...\n"); | ||
| 369 | return 0; | ||
| 370 | } | ||
| 371 | off++; | ||
| 372 | filp->f_pos = pos + 1; | ||
| 373 | } | ||
| 374 | |||
| 375 | if (fi->last_name) { | ||
| 376 | ceph_mdsc_put_request(fi->last_readdir); | ||
| 377 | fi->last_readdir = NULL; | ||
| 378 | goto more; | ||
| 379 | } | ||
| 380 | |||
| 381 | /* more frags? */ | ||
| 382 | if (!ceph_frag_is_rightmost(frag)) { | ||
| 383 | frag = ceph_frag_next(frag); | ||
| 384 | off = 0; | ||
| 385 | filp->f_pos = ceph_make_fpos(frag, off); | ||
| 386 | dout("readdir next frag is %x\n", frag); | ||
| 387 | goto more; | ||
| 388 | } | ||
| 389 | fi->at_end = 1; | ||
| 390 | |||
| 391 | /* | ||
| 392 | * if dir_release_count still matches the dir, no dentries | ||
| 393 | * were released during the whole readdir, and we should have | ||
| 394 | * the complete dir contents in our cache. | ||
| 395 | */ | ||
| 396 | spin_lock(&inode->i_lock); | ||
| 397 | if (ci->i_release_count == fi->dir_release_count) { | ||
| 398 | dout(" marking %p complete\n", inode); | ||
| 399 | ci->i_ceph_flags |= CEPH_I_COMPLETE; | ||
| 400 | ci->i_max_offset = filp->f_pos; | ||
| 401 | } | ||
| 402 | spin_unlock(&inode->i_lock); | ||
| 403 | |||
| 404 | dout("readdir %p filp %p done.\n", inode, filp); | ||
| 405 | return 0; | ||
| 406 | } | ||
| 407 | |||
| 408 | static void reset_readdir(struct ceph_file_info *fi) | ||
| 409 | { | ||
| 410 | if (fi->last_readdir) { | ||
| 411 | ceph_mdsc_put_request(fi->last_readdir); | ||
| 412 | fi->last_readdir = NULL; | ||
| 413 | } | ||
| 414 | kfree(fi->last_name); | ||
| 415 | fi->next_offset = 2; /* compensate for . and .. */ | ||
| 416 | if (fi->dentry) { | ||
| 417 | dput(fi->dentry); | ||
| 418 | fi->dentry = NULL; | ||
| 419 | } | ||
| 420 | fi->at_end = 0; | ||
| 421 | } | ||
| 422 | |||
| 423 | static loff_t ceph_dir_llseek(struct file *file, loff_t offset, int origin) | ||
| 424 | { | ||
| 425 | struct ceph_file_info *fi = file->private_data; | ||
| 426 | struct inode *inode = file->f_mapping->host; | ||
| 427 | loff_t old_offset = offset; | ||
| 428 | loff_t retval; | ||
| 429 | |||
| 430 | mutex_lock(&inode->i_mutex); | ||
| 431 | switch (origin) { | ||
| 432 | case SEEK_END: | ||
| 433 | offset += inode->i_size + 2; /* FIXME */ | ||
| 434 | break; | ||
| 435 | case SEEK_CUR: | ||
| 436 | offset += file->f_pos; | ||
| 437 | } | ||
| 438 | retval = -EINVAL; | ||
| 439 | if (offset >= 0 && offset <= inode->i_sb->s_maxbytes) { | ||
| 440 | if (offset != file->f_pos) { | ||
| 441 | file->f_pos = offset; | ||
| 442 | file->f_version = 0; | ||
| 443 | fi->at_end = 0; | ||
| 444 | } | ||
| 445 | retval = offset; | ||
| 446 | |||
| 447 | /* | ||
| 448 | * discard buffered readdir content on seekdir(0), or | ||
| 449 | * seek to new frag, or seek prior to current chunk. | ||
| 450 | */ | ||
| 451 | if (offset == 0 || | ||
| 452 | fpos_frag(offset) != fpos_frag(old_offset) || | ||
| 453 | fpos_off(offset) < fi->offset) { | ||
| 454 | dout("dir_llseek dropping %p content\n", file); | ||
| 455 | reset_readdir(fi); | ||
| 456 | } | ||
| 457 | |||
| 458 | /* bump dir_release_count if we did a forward seek */ | ||
| 459 | if (offset > old_offset) | ||
| 460 | fi->dir_release_count--; | ||
| 461 | } | ||
| 462 | mutex_unlock(&inode->i_mutex); | ||
| 463 | return retval; | ||
| 464 | } | ||
| 465 | |||
| 466 | /* | ||
| 467 | * Process result of a lookup/open request. | ||
| 468 | * | ||
| 469 | * Mainly, make sure we return the final req->r_dentry (if it already | ||
| 470 | * existed) in place of the original VFS-provided dentry when they | ||
| 471 | * differ. | ||
| 472 | * | ||
| 473 | * Gracefully handle the case where the MDS replies with -ENOENT and | ||
| 474 | * no trace (which it may do, at its discretion, e.g., if it doesn't | ||
| 475 | * care to issue a lease on the negative dentry). | ||
| 476 | */ | ||
| 477 | struct dentry *ceph_finish_lookup(struct ceph_mds_request *req, | ||
| 478 | struct dentry *dentry, int err) | ||
| 479 | { | ||
| 480 | struct ceph_client *client = ceph_client(dentry->d_sb); | ||
| 481 | struct inode *parent = dentry->d_parent->d_inode; | ||
| 482 | |||
| 483 | /* .snap dir? */ | ||
| 484 | if (err == -ENOENT && | ||
| 485 | ceph_vino(parent).ino != CEPH_INO_ROOT && /* no .snap in root dir */ | ||
| 486 | strcmp(dentry->d_name.name, | ||
| 487 | client->mount_args->snapdir_name) == 0) { | ||
| 488 | struct inode *inode = ceph_get_snapdir(parent); | ||
| 489 | dout("ENOENT on snapdir %p '%.*s', linking to snapdir %p\n", | ||
| 490 | dentry, dentry->d_name.len, dentry->d_name.name, inode); | ||
| 491 | d_add(dentry, inode); | ||
| 492 | err = 0; | ||
| 493 | } | ||
| 494 | |||
| 495 | if (err == -ENOENT) { | ||
| 496 | /* no trace? */ | ||
| 497 | err = 0; | ||
| 498 | if (!req->r_reply_info.head->is_dentry) { | ||
| 499 | dout("ENOENT and no trace, dentry %p inode %p\n", | ||
| 500 | dentry, dentry->d_inode); | ||
| 501 | if (dentry->d_inode) { | ||
| 502 | d_drop(dentry); | ||
| 503 | err = -ENOENT; | ||
| 504 | } else { | ||
| 505 | d_add(dentry, NULL); | ||
| 506 | } | ||
| 507 | } | ||
| 508 | } | ||
| 509 | if (err) | ||
| 510 | dentry = ERR_PTR(err); | ||
| 511 | else if (dentry != req->r_dentry) | ||
| 512 | dentry = dget(req->r_dentry); /* we got spliced */ | ||
| 513 | else | ||
| 514 | dentry = NULL; | ||
| 515 | return dentry; | ||
| 516 | } | ||
| 517 | |||
| 518 | static int is_root_ceph_dentry(struct inode *inode, struct dentry *dentry) | ||
| 519 | { | ||
| 520 | return ceph_ino(inode) == CEPH_INO_ROOT && | ||
| 521 | strncmp(dentry->d_name.name, ".ceph", 5) == 0; | ||
| 522 | } | ||
| 523 | |||
| 524 | /* | ||
| 525 | * Look up a single dir entry. If there is a lookup intent, inform | ||
| 526 | * the MDS so that it gets our 'caps wanted' value in a single op. | ||
| 527 | */ | ||
| 528 | static struct dentry *ceph_lookup(struct inode *dir, struct dentry *dentry, | ||
| 529 | struct nameidata *nd) | ||
| 530 | { | ||
| 531 | struct ceph_client *client = ceph_sb_to_client(dir->i_sb); | ||
| 532 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 533 | struct ceph_mds_request *req; | ||
| 534 | int op; | ||
| 535 | int err; | ||
| 536 | |||
| 537 | dout("lookup %p dentry %p '%.*s'\n", | ||
| 538 | dir, dentry, dentry->d_name.len, dentry->d_name.name); | ||
| 539 | |||
| 540 | if (dentry->d_name.len > NAME_MAX) | ||
| 541 | return ERR_PTR(-ENAMETOOLONG); | ||
| 542 | |||
| 543 | err = ceph_init_dentry(dentry); | ||
| 544 | if (err < 0) | ||
| 545 | return ERR_PTR(err); | ||
| 546 | |||
| 547 | /* open (but not create!) intent? */ | ||
| 548 | if (nd && | ||
| 549 | (nd->flags & LOOKUP_OPEN) && | ||
| 550 | (nd->flags & LOOKUP_CONTINUE) == 0 && /* only open last component */ | ||
| 551 | !(nd->intent.open.flags & O_CREAT)) { | ||
| 552 | int mode = nd->intent.open.create_mode & ~current->fs->umask; | ||
| 553 | return ceph_lookup_open(dir, dentry, nd, mode, 1); | ||
| 554 | } | ||
| 555 | |||
| 556 | /* can we conclude ENOENT locally? */ | ||
| 557 | if (dentry->d_inode == NULL) { | ||
| 558 | struct ceph_inode_info *ci = ceph_inode(dir); | ||
| 559 | struct ceph_dentry_info *di = ceph_dentry(dentry); | ||
| 560 | |||
| 561 | spin_lock(&dir->i_lock); | ||
| 562 | dout(" dir %p flags are %d\n", dir, ci->i_ceph_flags); | ||
| 563 | if (strncmp(dentry->d_name.name, | ||
| 564 | client->mount_args->snapdir_name, | ||
| 565 | dentry->d_name.len) && | ||
| 566 | !is_root_ceph_dentry(dir, dentry) && | ||
| 567 | (ci->i_ceph_flags & CEPH_I_COMPLETE) && | ||
| 568 | (__ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1))) { | ||
| 569 | di->offset = ci->i_max_offset++; | ||
| 570 | spin_unlock(&dir->i_lock); | ||
| 571 | dout(" dir %p complete, -ENOENT\n", dir); | ||
| 572 | d_add(dentry, NULL); | ||
| 573 | di->lease_shared_gen = ci->i_shared_gen; | ||
| 574 | return NULL; | ||
| 575 | } | ||
| 576 | spin_unlock(&dir->i_lock); | ||
| 577 | } | ||
| 578 | |||
| 579 | op = ceph_snap(dir) == CEPH_SNAPDIR ? | ||
| 580 | CEPH_MDS_OP_LOOKUPSNAP : CEPH_MDS_OP_LOOKUP; | ||
| 581 | req = ceph_mdsc_create_request(mdsc, op, USE_ANY_MDS); | ||
| 582 | if (IS_ERR(req)) | ||
| 583 | return ERR_PTR(PTR_ERR(req)); | ||
| 584 | req->r_dentry = dget(dentry); | ||
| 585 | req->r_num_caps = 2; | ||
| 586 | /* we only need inode linkage */ | ||
| 587 | req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE); | ||
| 588 | req->r_locked_dir = dir; | ||
| 589 | err = ceph_mdsc_do_request(mdsc, NULL, req); | ||
| 590 | dentry = ceph_finish_lookup(req, dentry, err); | ||
| 591 | ceph_mdsc_put_request(req); /* will dput(dentry) */ | ||
| 592 | dout("lookup result=%p\n", dentry); | ||
| 593 | return dentry; | ||
| 594 | } | ||
| 595 | |||
| 596 | /* | ||
| 597 | * If we do a create but get no trace back from the MDS, follow up with | ||
| 598 | * a lookup (the VFS expects us to link up the provided dentry). | ||
| 599 | */ | ||
| 600 | int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry) | ||
| 601 | { | ||
| 602 | struct dentry *result = ceph_lookup(dir, dentry, NULL); | ||
| 603 | |||
| 604 | if (result && !IS_ERR(result)) { | ||
| 605 | /* | ||
| 606 | * We created the item, then did a lookup, and found | ||
| 607 | * it was already linked to another inode we already | ||
| 608 | * had in our cache (and thus got spliced). Link our | ||
| 609 | * dentry to that inode, but don't hash it, just in | ||
| 610 | * case the VFS wants to dereference it. | ||
| 611 | */ | ||
| 612 | BUG_ON(!result->d_inode); | ||
| 613 | d_instantiate(dentry, result->d_inode); | ||
| 614 | return 0; | ||
| 615 | } | ||
| 616 | return PTR_ERR(result); | ||
| 617 | } | ||
| 618 | |||
| 619 | static int ceph_mknod(struct inode *dir, struct dentry *dentry, | ||
| 620 | int mode, dev_t rdev) | ||
| 621 | { | ||
| 622 | struct ceph_client *client = ceph_sb_to_client(dir->i_sb); | ||
| 623 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 624 | struct ceph_mds_request *req; | ||
| 625 | int err; | ||
| 626 | |||
| 627 | if (ceph_snap(dir) != CEPH_NOSNAP) | ||
| 628 | return -EROFS; | ||
| 629 | |||
| 630 | dout("mknod in dir %p dentry %p mode 0%o rdev %d\n", | ||
| 631 | dir, dentry, mode, rdev); | ||
| 632 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_MKNOD, USE_AUTH_MDS); | ||
| 633 | if (IS_ERR(req)) { | ||
| 634 | d_drop(dentry); | ||
| 635 | return PTR_ERR(req); | ||
| 636 | } | ||
| 637 | req->r_dentry = dget(dentry); | ||
| 638 | req->r_num_caps = 2; | ||
| 639 | req->r_locked_dir = dir; | ||
| 640 | req->r_args.mknod.mode = cpu_to_le32(mode); | ||
| 641 | req->r_args.mknod.rdev = cpu_to_le32(rdev); | ||
| 642 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; | ||
| 643 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; | ||
| 644 | err = ceph_mdsc_do_request(mdsc, dir, req); | ||
| 645 | if (!err && !req->r_reply_info.head->is_dentry) | ||
| 646 | err = ceph_handle_notrace_create(dir, dentry); | ||
| 647 | ceph_mdsc_put_request(req); | ||
| 648 | if (err) | ||
| 649 | d_drop(dentry); | ||
| 650 | return err; | ||
| 651 | } | ||
| 652 | |||
| 653 | static int ceph_create(struct inode *dir, struct dentry *dentry, int mode, | ||
| 654 | struct nameidata *nd) | ||
| 655 | { | ||
| 656 | dout("create in dir %p dentry %p name '%.*s'\n", | ||
| 657 | dir, dentry, dentry->d_name.len, dentry->d_name.name); | ||
| 658 | |||
| 659 | if (ceph_snap(dir) != CEPH_NOSNAP) | ||
| 660 | return -EROFS; | ||
| 661 | |||
| 662 | if (nd) { | ||
| 663 | BUG_ON((nd->flags & LOOKUP_OPEN) == 0); | ||
| 664 | dentry = ceph_lookup_open(dir, dentry, nd, mode, 0); | ||
| 665 | /* hrm, what should i do here if we get aliased? */ | ||
| 666 | if (IS_ERR(dentry)) | ||
| 667 | return PTR_ERR(dentry); | ||
| 668 | return 0; | ||
| 669 | } | ||
| 670 | |||
| 671 | /* fall back to mknod */ | ||
| 672 | return ceph_mknod(dir, dentry, (mode & ~S_IFMT) | S_IFREG, 0); | ||
| 673 | } | ||
| 674 | |||
| 675 | static int ceph_symlink(struct inode *dir, struct dentry *dentry, | ||
| 676 | const char *dest) | ||
| 677 | { | ||
| 678 | struct ceph_client *client = ceph_sb_to_client(dir->i_sb); | ||
| 679 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 680 | struct ceph_mds_request *req; | ||
| 681 | int err; | ||
| 682 | |||
| 683 | if (ceph_snap(dir) != CEPH_NOSNAP) | ||
| 684 | return -EROFS; | ||
| 685 | |||
| 686 | dout("symlink in dir %p dentry %p to '%s'\n", dir, dentry, dest); | ||
| 687 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SYMLINK, USE_AUTH_MDS); | ||
| 688 | if (IS_ERR(req)) { | ||
| 689 | d_drop(dentry); | ||
| 690 | return PTR_ERR(req); | ||
| 691 | } | ||
| 692 | req->r_dentry = dget(dentry); | ||
| 693 | req->r_num_caps = 2; | ||
| 694 | req->r_path2 = kstrdup(dest, GFP_NOFS); | ||
| 695 | req->r_locked_dir = dir; | ||
| 696 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; | ||
| 697 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; | ||
| 698 | err = ceph_mdsc_do_request(mdsc, dir, req); | ||
| 699 | if (!err && !req->r_reply_info.head->is_dentry) | ||
| 700 | err = ceph_handle_notrace_create(dir, dentry); | ||
| 701 | ceph_mdsc_put_request(req); | ||
| 702 | if (err) | ||
| 703 | d_drop(dentry); | ||
| 704 | return err; | ||
| 705 | } | ||
| 706 | |||
| 707 | static int ceph_mkdir(struct inode *dir, struct dentry *dentry, int mode) | ||
| 708 | { | ||
| 709 | struct ceph_client *client = ceph_sb_to_client(dir->i_sb); | ||
| 710 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 711 | struct ceph_mds_request *req; | ||
| 712 | int err = -EROFS; | ||
| 713 | int op; | ||
| 714 | |||
| 715 | if (ceph_snap(dir) == CEPH_SNAPDIR) { | ||
| 716 | /* mkdir .snap/foo is a MKSNAP */ | ||
| 717 | op = CEPH_MDS_OP_MKSNAP; | ||
| 718 | dout("mksnap dir %p snap '%.*s' dn %p\n", dir, | ||
| 719 | dentry->d_name.len, dentry->d_name.name, dentry); | ||
| 720 | } else if (ceph_snap(dir) == CEPH_NOSNAP) { | ||
| 721 | dout("mkdir dir %p dn %p mode 0%o\n", dir, dentry, mode); | ||
| 722 | op = CEPH_MDS_OP_MKDIR; | ||
| 723 | } else { | ||
| 724 | goto out; | ||
| 725 | } | ||
| 726 | req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); | ||
| 727 | if (IS_ERR(req)) { | ||
| 728 | err = PTR_ERR(req); | ||
| 729 | goto out; | ||
| 730 | } | ||
| 731 | |||
| 732 | req->r_dentry = dget(dentry); | ||
| 733 | req->r_num_caps = 2; | ||
| 734 | req->r_locked_dir = dir; | ||
| 735 | req->r_args.mkdir.mode = cpu_to_le32(mode); | ||
| 736 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; | ||
| 737 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; | ||
| 738 | err = ceph_mdsc_do_request(mdsc, dir, req); | ||
| 739 | if (!err && !req->r_reply_info.head->is_dentry) | ||
| 740 | err = ceph_handle_notrace_create(dir, dentry); | ||
| 741 | ceph_mdsc_put_request(req); | ||
| 742 | out: | ||
| 743 | if (err < 0) | ||
| 744 | d_drop(dentry); | ||
| 745 | return err; | ||
| 746 | } | ||
| 747 | |||
| 748 | static int ceph_link(struct dentry *old_dentry, struct inode *dir, | ||
| 749 | struct dentry *dentry) | ||
| 750 | { | ||
| 751 | struct ceph_client *client = ceph_sb_to_client(dir->i_sb); | ||
| 752 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 753 | struct ceph_mds_request *req; | ||
| 754 | int err; | ||
| 755 | |||
| 756 | if (ceph_snap(dir) != CEPH_NOSNAP) | ||
| 757 | return -EROFS; | ||
| 758 | |||
| 759 | dout("link in dir %p old_dentry %p dentry %p\n", dir, | ||
| 760 | old_dentry, dentry); | ||
| 761 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LINK, USE_AUTH_MDS); | ||
| 762 | if (IS_ERR(req)) { | ||
| 763 | d_drop(dentry); | ||
| 764 | return PTR_ERR(req); | ||
| 765 | } | ||
| 766 | req->r_dentry = dget(dentry); | ||
| 767 | req->r_num_caps = 2; | ||
| 768 | req->r_old_dentry = dget(old_dentry); /* or inode? hrm. */ | ||
| 769 | req->r_locked_dir = dir; | ||
| 770 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; | ||
| 771 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; | ||
| 772 | err = ceph_mdsc_do_request(mdsc, dir, req); | ||
| 773 | if (err) | ||
| 774 | d_drop(dentry); | ||
| 775 | else if (!req->r_reply_info.head->is_dentry) | ||
| 776 | d_instantiate(dentry, igrab(old_dentry->d_inode)); | ||
| 777 | ceph_mdsc_put_request(req); | ||
| 778 | return err; | ||
| 779 | } | ||
| 780 | |||
| 781 | /* | ||
| 782 | * For a soon-to-be unlinked file, drop the AUTH_RDCACHE caps. If it | ||
| 783 | * looks like the link count will hit 0, drop any other caps (other | ||
| 784 | * than PIN) we don't specifically want (due to the file still being | ||
| 785 | * open). | ||
| 786 | */ | ||
| 787 | static int drop_caps_for_unlink(struct inode *inode) | ||
| 788 | { | ||
| 789 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 790 | int drop = CEPH_CAP_LINK_SHARED | CEPH_CAP_LINK_EXCL; | ||
| 791 | |||
| 792 | spin_lock(&inode->i_lock); | ||
| 793 | if (inode->i_nlink == 1) { | ||
| 794 | drop |= ~(__ceph_caps_wanted(ci) | CEPH_CAP_PIN); | ||
| 795 | ci->i_ceph_flags |= CEPH_I_NODELAY; | ||
| 796 | } | ||
| 797 | spin_unlock(&inode->i_lock); | ||
| 798 | return drop; | ||
| 799 | } | ||
| 800 | |||
| 801 | /* | ||
| 802 | * rmdir and unlink are differ only by the metadata op code | ||
| 803 | */ | ||
| 804 | static int ceph_unlink(struct inode *dir, struct dentry *dentry) | ||
| 805 | { | ||
| 806 | struct ceph_client *client = ceph_sb_to_client(dir->i_sb); | ||
| 807 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 808 | struct inode *inode = dentry->d_inode; | ||
| 809 | struct ceph_mds_request *req; | ||
| 810 | int err = -EROFS; | ||
| 811 | int op; | ||
| 812 | |||
| 813 | if (ceph_snap(dir) == CEPH_SNAPDIR) { | ||
| 814 | /* rmdir .snap/foo is RMSNAP */ | ||
| 815 | dout("rmsnap dir %p '%.*s' dn %p\n", dir, dentry->d_name.len, | ||
| 816 | dentry->d_name.name, dentry); | ||
| 817 | op = CEPH_MDS_OP_RMSNAP; | ||
| 818 | } else if (ceph_snap(dir) == CEPH_NOSNAP) { | ||
| 819 | dout("unlink/rmdir dir %p dn %p inode %p\n", | ||
| 820 | dir, dentry, inode); | ||
| 821 | op = ((dentry->d_inode->i_mode & S_IFMT) == S_IFDIR) ? | ||
| 822 | CEPH_MDS_OP_RMDIR : CEPH_MDS_OP_UNLINK; | ||
| 823 | } else | ||
| 824 | goto out; | ||
| 825 | req = ceph_mdsc_create_request(mdsc, op, USE_AUTH_MDS); | ||
| 826 | if (IS_ERR(req)) { | ||
| 827 | err = PTR_ERR(req); | ||
| 828 | goto out; | ||
| 829 | } | ||
| 830 | req->r_dentry = dget(dentry); | ||
| 831 | req->r_num_caps = 2; | ||
| 832 | req->r_locked_dir = dir; | ||
| 833 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; | ||
| 834 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; | ||
| 835 | req->r_inode_drop = drop_caps_for_unlink(inode); | ||
| 836 | err = ceph_mdsc_do_request(mdsc, dir, req); | ||
| 837 | if (!err && !req->r_reply_info.head->is_dentry) | ||
| 838 | d_delete(dentry); | ||
| 839 | ceph_mdsc_put_request(req); | ||
| 840 | out: | ||
| 841 | return err; | ||
| 842 | } | ||
| 843 | |||
| 844 | static int ceph_rename(struct inode *old_dir, struct dentry *old_dentry, | ||
| 845 | struct inode *new_dir, struct dentry *new_dentry) | ||
| 846 | { | ||
| 847 | struct ceph_client *client = ceph_sb_to_client(old_dir->i_sb); | ||
| 848 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 849 | struct ceph_mds_request *req; | ||
| 850 | int err; | ||
| 851 | |||
| 852 | if (ceph_snap(old_dir) != ceph_snap(new_dir)) | ||
| 853 | return -EXDEV; | ||
| 854 | if (ceph_snap(old_dir) != CEPH_NOSNAP || | ||
| 855 | ceph_snap(new_dir) != CEPH_NOSNAP) | ||
| 856 | return -EROFS; | ||
| 857 | dout("rename dir %p dentry %p to dir %p dentry %p\n", | ||
| 858 | old_dir, old_dentry, new_dir, new_dentry); | ||
| 859 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RENAME, USE_AUTH_MDS); | ||
| 860 | if (IS_ERR(req)) | ||
| 861 | return PTR_ERR(req); | ||
| 862 | req->r_dentry = dget(new_dentry); | ||
| 863 | req->r_num_caps = 2; | ||
| 864 | req->r_old_dentry = dget(old_dentry); | ||
| 865 | req->r_locked_dir = new_dir; | ||
| 866 | req->r_old_dentry_drop = CEPH_CAP_FILE_SHARED; | ||
| 867 | req->r_old_dentry_unless = CEPH_CAP_FILE_EXCL; | ||
| 868 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; | ||
| 869 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; | ||
| 870 | /* release LINK_RDCACHE on source inode (mds will lock it) */ | ||
| 871 | req->r_old_inode_drop = CEPH_CAP_LINK_SHARED; | ||
| 872 | if (new_dentry->d_inode) | ||
| 873 | req->r_inode_drop = drop_caps_for_unlink(new_dentry->d_inode); | ||
| 874 | err = ceph_mdsc_do_request(mdsc, old_dir, req); | ||
| 875 | if (!err && !req->r_reply_info.head->is_dentry) { | ||
| 876 | /* | ||
| 877 | * Normally d_move() is done by fill_trace (called by | ||
| 878 | * do_request, above). If there is no trace, we need | ||
| 879 | * to do it here. | ||
| 880 | */ | ||
| 881 | d_move(old_dentry, new_dentry); | ||
| 882 | } | ||
| 883 | ceph_mdsc_put_request(req); | ||
| 884 | return err; | ||
| 885 | } | ||
| 886 | |||
| 887 | |||
| 888 | /* | ||
| 889 | * Check if dentry lease is valid. If not, delete the lease. Try to | ||
| 890 | * renew if the least is more than half up. | ||
| 891 | */ | ||
| 892 | static int dentry_lease_is_valid(struct dentry *dentry) | ||
| 893 | { | ||
| 894 | struct ceph_dentry_info *di; | ||
| 895 | struct ceph_mds_session *s; | ||
| 896 | int valid = 0; | ||
| 897 | u32 gen; | ||
| 898 | unsigned long ttl; | ||
| 899 | struct ceph_mds_session *session = NULL; | ||
| 900 | struct inode *dir = NULL; | ||
| 901 | u32 seq = 0; | ||
| 902 | |||
| 903 | spin_lock(&dentry->d_lock); | ||
| 904 | di = ceph_dentry(dentry); | ||
| 905 | if (di && di->lease_session) { | ||
| 906 | s = di->lease_session; | ||
| 907 | spin_lock(&s->s_cap_lock); | ||
| 908 | gen = s->s_cap_gen; | ||
| 909 | ttl = s->s_cap_ttl; | ||
| 910 | spin_unlock(&s->s_cap_lock); | ||
| 911 | |||
| 912 | if (di->lease_gen == gen && | ||
| 913 | time_before(jiffies, dentry->d_time) && | ||
| 914 | time_before(jiffies, ttl)) { | ||
| 915 | valid = 1; | ||
| 916 | if (di->lease_renew_after && | ||
| 917 | time_after(jiffies, di->lease_renew_after)) { | ||
| 918 | /* we should renew */ | ||
| 919 | dir = dentry->d_parent->d_inode; | ||
| 920 | session = ceph_get_mds_session(s); | ||
| 921 | seq = di->lease_seq; | ||
| 922 | di->lease_renew_after = 0; | ||
| 923 | di->lease_renew_from = jiffies; | ||
| 924 | } | ||
| 925 | } | ||
| 926 | } | ||
| 927 | spin_unlock(&dentry->d_lock); | ||
| 928 | |||
| 929 | if (session) { | ||
| 930 | ceph_mdsc_lease_send_msg(session, dir, dentry, | ||
| 931 | CEPH_MDS_LEASE_RENEW, seq); | ||
| 932 | ceph_put_mds_session(session); | ||
| 933 | } | ||
| 934 | dout("dentry_lease_is_valid - dentry %p = %d\n", dentry, valid); | ||
| 935 | return valid; | ||
| 936 | } | ||
| 937 | |||
| 938 | /* | ||
| 939 | * Check if directory-wide content lease/cap is valid. | ||
| 940 | */ | ||
| 941 | static int dir_lease_is_valid(struct inode *dir, struct dentry *dentry) | ||
| 942 | { | ||
| 943 | struct ceph_inode_info *ci = ceph_inode(dir); | ||
| 944 | struct ceph_dentry_info *di = ceph_dentry(dentry); | ||
| 945 | int valid = 0; | ||
| 946 | |||
| 947 | spin_lock(&dir->i_lock); | ||
| 948 | if (ci->i_shared_gen == di->lease_shared_gen) | ||
| 949 | valid = __ceph_caps_issued_mask(ci, CEPH_CAP_FILE_SHARED, 1); | ||
| 950 | spin_unlock(&dir->i_lock); | ||
| 951 | dout("dir_lease_is_valid dir %p v%u dentry %p v%u = %d\n", | ||
| 952 | dir, (unsigned)ci->i_shared_gen, dentry, | ||
| 953 | (unsigned)di->lease_shared_gen, valid); | ||
| 954 | return valid; | ||
| 955 | } | ||
| 956 | |||
| 957 | /* | ||
| 958 | * Check if cached dentry can be trusted. | ||
| 959 | */ | ||
| 960 | static int ceph_d_revalidate(struct dentry *dentry, struct nameidata *nd) | ||
| 961 | { | ||
| 962 | struct inode *dir = dentry->d_parent->d_inode; | ||
| 963 | |||
| 964 | dout("d_revalidate %p '%.*s' inode %p\n", dentry, | ||
| 965 | dentry->d_name.len, dentry->d_name.name, dentry->d_inode); | ||
| 966 | |||
| 967 | /* always trust cached snapped dentries, snapdir dentry */ | ||
| 968 | if (ceph_snap(dir) != CEPH_NOSNAP) { | ||
| 969 | dout("d_revalidate %p '%.*s' inode %p is SNAPPED\n", dentry, | ||
| 970 | dentry->d_name.len, dentry->d_name.name, dentry->d_inode); | ||
| 971 | goto out_touch; | ||
| 972 | } | ||
| 973 | if (dentry->d_inode && ceph_snap(dentry->d_inode) == CEPH_SNAPDIR) | ||
| 974 | goto out_touch; | ||
| 975 | |||
| 976 | if (dentry_lease_is_valid(dentry) || | ||
| 977 | dir_lease_is_valid(dir, dentry)) | ||
| 978 | goto out_touch; | ||
| 979 | |||
| 980 | dout("d_revalidate %p invalid\n", dentry); | ||
| 981 | d_drop(dentry); | ||
| 982 | return 0; | ||
| 983 | out_touch: | ||
| 984 | ceph_dentry_lru_touch(dentry); | ||
| 985 | return 1; | ||
| 986 | } | ||
| 987 | |||
| 988 | /* | ||
| 989 | * When a dentry is released, clear the dir I_COMPLETE if it was part | ||
| 990 | * of the current dir gen. | ||
| 991 | */ | ||
| 992 | static void ceph_dentry_release(struct dentry *dentry) | ||
| 993 | { | ||
| 994 | struct ceph_dentry_info *di = ceph_dentry(dentry); | ||
| 995 | struct inode *parent_inode = dentry->d_parent->d_inode; | ||
| 996 | |||
| 997 | if (parent_inode) { | ||
| 998 | struct ceph_inode_info *ci = ceph_inode(parent_inode); | ||
| 999 | |||
| 1000 | spin_lock(&parent_inode->i_lock); | ||
| 1001 | if (ci->i_shared_gen == di->lease_shared_gen) { | ||
| 1002 | dout(" clearing %p complete (d_release)\n", | ||
| 1003 | parent_inode); | ||
| 1004 | ci->i_ceph_flags &= ~CEPH_I_COMPLETE; | ||
| 1005 | ci->i_release_count++; | ||
| 1006 | } | ||
| 1007 | spin_unlock(&parent_inode->i_lock); | ||
| 1008 | } | ||
| 1009 | if (di) { | ||
| 1010 | ceph_dentry_lru_del(dentry); | ||
| 1011 | if (di->lease_session) | ||
| 1012 | ceph_put_mds_session(di->lease_session); | ||
| 1013 | kmem_cache_free(ceph_dentry_cachep, di); | ||
| 1014 | dentry->d_fsdata = NULL; | ||
| 1015 | } | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | static int ceph_snapdir_d_revalidate(struct dentry *dentry, | ||
| 1019 | struct nameidata *nd) | ||
| 1020 | { | ||
| 1021 | /* | ||
| 1022 | * Eventually, we'll want to revalidate snapped metadata | ||
| 1023 | * too... probably... | ||
| 1024 | */ | ||
| 1025 | return 1; | ||
| 1026 | } | ||
| 1027 | |||
| 1028 | |||
| 1029 | |||
| 1030 | /* | ||
| 1031 | * read() on a dir. This weird interface hack only works if mounted | ||
| 1032 | * with '-o dirstat'. | ||
| 1033 | */ | ||
| 1034 | static ssize_t ceph_read_dir(struct file *file, char __user *buf, size_t size, | ||
| 1035 | loff_t *ppos) | ||
| 1036 | { | ||
| 1037 | struct ceph_file_info *cf = file->private_data; | ||
| 1038 | struct inode *inode = file->f_dentry->d_inode; | ||
| 1039 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 1040 | int left; | ||
| 1041 | |||
| 1042 | if (!ceph_test_opt(ceph_client(inode->i_sb), DIRSTAT)) | ||
| 1043 | return -EISDIR; | ||
| 1044 | |||
| 1045 | if (!cf->dir_info) { | ||
| 1046 | cf->dir_info = kmalloc(1024, GFP_NOFS); | ||
| 1047 | if (!cf->dir_info) | ||
| 1048 | return -ENOMEM; | ||
| 1049 | cf->dir_info_len = | ||
| 1050 | sprintf(cf->dir_info, | ||
| 1051 | "entries: %20lld\n" | ||
| 1052 | " files: %20lld\n" | ||
| 1053 | " subdirs: %20lld\n" | ||
| 1054 | "rentries: %20lld\n" | ||
| 1055 | " rfiles: %20lld\n" | ||
| 1056 | " rsubdirs: %20lld\n" | ||
| 1057 | "rbytes: %20lld\n" | ||
| 1058 | "rctime: %10ld.%09ld\n", | ||
| 1059 | ci->i_files + ci->i_subdirs, | ||
| 1060 | ci->i_files, | ||
| 1061 | ci->i_subdirs, | ||
| 1062 | ci->i_rfiles + ci->i_rsubdirs, | ||
| 1063 | ci->i_rfiles, | ||
| 1064 | ci->i_rsubdirs, | ||
| 1065 | ci->i_rbytes, | ||
| 1066 | (long)ci->i_rctime.tv_sec, | ||
| 1067 | (long)ci->i_rctime.tv_nsec); | ||
| 1068 | } | ||
| 1069 | |||
| 1070 | if (*ppos >= cf->dir_info_len) | ||
| 1071 | return 0; | ||
| 1072 | size = min_t(unsigned, size, cf->dir_info_len-*ppos); | ||
| 1073 | left = copy_to_user(buf, cf->dir_info + *ppos, size); | ||
| 1074 | if (left == size) | ||
| 1075 | return -EFAULT; | ||
| 1076 | *ppos += (size - left); | ||
| 1077 | return size - left; | ||
| 1078 | } | ||
| 1079 | |||
| 1080 | /* | ||
| 1081 | * an fsync() on a dir will wait for any uncommitted directory | ||
| 1082 | * operations to commit. | ||
| 1083 | */ | ||
| 1084 | static int ceph_dir_fsync(struct file *file, struct dentry *dentry, | ||
| 1085 | int datasync) | ||
| 1086 | { | ||
| 1087 | struct inode *inode = dentry->d_inode; | ||
| 1088 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 1089 | struct list_head *head = &ci->i_unsafe_dirops; | ||
| 1090 | struct ceph_mds_request *req; | ||
| 1091 | u64 last_tid; | ||
| 1092 | int ret = 0; | ||
| 1093 | |||
| 1094 | dout("dir_fsync %p\n", inode); | ||
| 1095 | spin_lock(&ci->i_unsafe_lock); | ||
| 1096 | if (list_empty(head)) | ||
| 1097 | goto out; | ||
| 1098 | |||
| 1099 | req = list_entry(head->prev, | ||
| 1100 | struct ceph_mds_request, r_unsafe_dir_item); | ||
| 1101 | last_tid = req->r_tid; | ||
| 1102 | |||
| 1103 | do { | ||
| 1104 | ceph_mdsc_get_request(req); | ||
| 1105 | spin_unlock(&ci->i_unsafe_lock); | ||
| 1106 | dout("dir_fsync %p wait on tid %llu (until %llu)\n", | ||
| 1107 | inode, req->r_tid, last_tid); | ||
| 1108 | if (req->r_timeout) { | ||
| 1109 | ret = wait_for_completion_timeout( | ||
| 1110 | &req->r_safe_completion, req->r_timeout); | ||
| 1111 | if (ret > 0) | ||
| 1112 | ret = 0; | ||
| 1113 | else if (ret == 0) | ||
| 1114 | ret = -EIO; /* timed out */ | ||
| 1115 | } else { | ||
| 1116 | wait_for_completion(&req->r_safe_completion); | ||
| 1117 | } | ||
| 1118 | spin_lock(&ci->i_unsafe_lock); | ||
| 1119 | ceph_mdsc_put_request(req); | ||
| 1120 | |||
| 1121 | if (ret || list_empty(head)) | ||
| 1122 | break; | ||
| 1123 | req = list_entry(head->next, | ||
| 1124 | struct ceph_mds_request, r_unsafe_dir_item); | ||
| 1125 | } while (req->r_tid < last_tid); | ||
| 1126 | out: | ||
| 1127 | spin_unlock(&ci->i_unsafe_lock); | ||
| 1128 | return ret; | ||
| 1129 | } | ||
| 1130 | |||
| 1131 | /* | ||
| 1132 | * We maintain a private dentry LRU. | ||
| 1133 | * | ||
| 1134 | * FIXME: this needs to be changed to a per-mds lru to be useful. | ||
| 1135 | */ | ||
| 1136 | void ceph_dentry_lru_add(struct dentry *dn) | ||
| 1137 | { | ||
| 1138 | struct ceph_dentry_info *di = ceph_dentry(dn); | ||
| 1139 | struct ceph_mds_client *mdsc; | ||
| 1140 | |||
| 1141 | dout("dentry_lru_add %p %p '%.*s'\n", di, dn, | ||
| 1142 | dn->d_name.len, dn->d_name.name); | ||
| 1143 | if (di) { | ||
| 1144 | mdsc = &ceph_client(dn->d_sb)->mdsc; | ||
| 1145 | spin_lock(&mdsc->dentry_lru_lock); | ||
| 1146 | list_add_tail(&di->lru, &mdsc->dentry_lru); | ||
| 1147 | mdsc->num_dentry++; | ||
| 1148 | spin_unlock(&mdsc->dentry_lru_lock); | ||
| 1149 | } | ||
| 1150 | } | ||
| 1151 | |||
| 1152 | void ceph_dentry_lru_touch(struct dentry *dn) | ||
| 1153 | { | ||
| 1154 | struct ceph_dentry_info *di = ceph_dentry(dn); | ||
| 1155 | struct ceph_mds_client *mdsc; | ||
| 1156 | |||
| 1157 | dout("dentry_lru_touch %p %p '%.*s'\n", di, dn, | ||
| 1158 | dn->d_name.len, dn->d_name.name); | ||
| 1159 | if (di) { | ||
| 1160 | mdsc = &ceph_client(dn->d_sb)->mdsc; | ||
| 1161 | spin_lock(&mdsc->dentry_lru_lock); | ||
| 1162 | list_move_tail(&di->lru, &mdsc->dentry_lru); | ||
| 1163 | spin_unlock(&mdsc->dentry_lru_lock); | ||
| 1164 | } | ||
| 1165 | } | ||
| 1166 | |||
| 1167 | void ceph_dentry_lru_del(struct dentry *dn) | ||
| 1168 | { | ||
| 1169 | struct ceph_dentry_info *di = ceph_dentry(dn); | ||
| 1170 | struct ceph_mds_client *mdsc; | ||
| 1171 | |||
| 1172 | dout("dentry_lru_del %p %p '%.*s'\n", di, dn, | ||
| 1173 | dn->d_name.len, dn->d_name.name); | ||
| 1174 | if (di) { | ||
| 1175 | mdsc = &ceph_client(dn->d_sb)->mdsc; | ||
| 1176 | spin_lock(&mdsc->dentry_lru_lock); | ||
| 1177 | list_del_init(&di->lru); | ||
| 1178 | mdsc->num_dentry--; | ||
| 1179 | spin_unlock(&mdsc->dentry_lru_lock); | ||
| 1180 | } | ||
| 1181 | } | ||
| 1182 | |||
| 1183 | const struct file_operations ceph_dir_fops = { | ||
| 1184 | .read = ceph_read_dir, | ||
| 1185 | .readdir = ceph_readdir, | ||
| 1186 | .llseek = ceph_dir_llseek, | ||
| 1187 | .open = ceph_open, | ||
| 1188 | .release = ceph_release, | ||
| 1189 | .unlocked_ioctl = ceph_ioctl, | ||
| 1190 | .fsync = ceph_dir_fsync, | ||
| 1191 | }; | ||
| 1192 | |||
| 1193 | const struct inode_operations ceph_dir_iops = { | ||
| 1194 | .lookup = ceph_lookup, | ||
| 1195 | .permission = ceph_permission, | ||
| 1196 | .getattr = ceph_getattr, | ||
| 1197 | .setattr = ceph_setattr, | ||
| 1198 | .setxattr = ceph_setxattr, | ||
| 1199 | .getxattr = ceph_getxattr, | ||
| 1200 | .listxattr = ceph_listxattr, | ||
| 1201 | .removexattr = ceph_removexattr, | ||
| 1202 | .mknod = ceph_mknod, | ||
| 1203 | .symlink = ceph_symlink, | ||
| 1204 | .mkdir = ceph_mkdir, | ||
| 1205 | .link = ceph_link, | ||
| 1206 | .unlink = ceph_unlink, | ||
| 1207 | .rmdir = ceph_unlink, | ||
| 1208 | .rename = ceph_rename, | ||
| 1209 | .create = ceph_create, | ||
| 1210 | }; | ||
| 1211 | |||
| 1212 | struct dentry_operations ceph_dentry_ops = { | ||
| 1213 | .d_revalidate = ceph_d_revalidate, | ||
| 1214 | .d_release = ceph_dentry_release, | ||
| 1215 | }; | ||
| 1216 | |||
| 1217 | struct dentry_operations ceph_snapdir_dentry_ops = { | ||
| 1218 | .d_revalidate = ceph_snapdir_d_revalidate, | ||
| 1219 | }; | ||
| 1220 | |||
| 1221 | struct dentry_operations ceph_snap_dentry_ops = { | ||
| 1222 | }; | ||
diff --git a/fs/ceph/export.c b/fs/ceph/export.c new file mode 100644 index 000000000000..fc68e39cbad6 --- /dev/null +++ b/fs/ceph/export.c | |||
| @@ -0,0 +1,223 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/exportfs.h> | ||
| 4 | #include <asm/unaligned.h> | ||
| 5 | |||
| 6 | #include "super.h" | ||
| 7 | |||
| 8 | /* | ||
| 9 | * NFS export support | ||
| 10 | * | ||
| 11 | * NFS re-export of a ceph mount is, at present, only semireliable. | ||
| 12 | * The basic issue is that the Ceph architectures doesn't lend itself | ||
| 13 | * well to generating filehandles that will remain valid forever. | ||
| 14 | * | ||
| 15 | * So, we do our best. If you're lucky, your inode will be in the | ||
| 16 | * client's cache. If it's not, and you have a connectable fh, then | ||
| 17 | * the MDS server may be able to find it for you. Otherwise, you get | ||
| 18 | * ESTALE. | ||
| 19 | * | ||
| 20 | * There are ways to this more reliable, but in the non-connectable fh | ||
| 21 | * case, we won't every work perfectly, and in the connectable case, | ||
| 22 | * some changes are needed on the MDS side to work better. | ||
| 23 | */ | ||
| 24 | |||
| 25 | /* | ||
| 26 | * Basic fh | ||
| 27 | */ | ||
| 28 | struct ceph_nfs_fh { | ||
| 29 | u64 ino; | ||
| 30 | } __attribute__ ((packed)); | ||
| 31 | |||
| 32 | /* | ||
| 33 | * Larger 'connectable' fh that includes parent ino and name hash. | ||
| 34 | * Use this whenever possible, as it works more reliably. | ||
| 35 | */ | ||
| 36 | struct ceph_nfs_confh { | ||
| 37 | u64 ino, parent_ino; | ||
| 38 | u32 parent_name_hash; | ||
| 39 | } __attribute__ ((packed)); | ||
| 40 | |||
| 41 | static int ceph_encode_fh(struct dentry *dentry, u32 *rawfh, int *max_len, | ||
| 42 | int connectable) | ||
| 43 | { | ||
| 44 | struct ceph_nfs_fh *fh = (void *)rawfh; | ||
| 45 | struct ceph_nfs_confh *cfh = (void *)rawfh; | ||
| 46 | struct dentry *parent = dentry->d_parent; | ||
| 47 | struct inode *inode = dentry->d_inode; | ||
| 48 | int type; | ||
| 49 | |||
| 50 | /* don't re-export snaps */ | ||
| 51 | if (ceph_snap(inode) != CEPH_NOSNAP) | ||
| 52 | return -EINVAL; | ||
| 53 | |||
| 54 | if (*max_len >= sizeof(*cfh)) { | ||
| 55 | dout("encode_fh %p connectable\n", dentry); | ||
| 56 | cfh->ino = ceph_ino(dentry->d_inode); | ||
| 57 | cfh->parent_ino = ceph_ino(parent->d_inode); | ||
| 58 | cfh->parent_name_hash = parent->d_name.hash; | ||
| 59 | *max_len = sizeof(*cfh); | ||
| 60 | type = 2; | ||
| 61 | } else if (*max_len > sizeof(*fh)) { | ||
| 62 | if (connectable) | ||
| 63 | return -ENOSPC; | ||
| 64 | dout("encode_fh %p\n", dentry); | ||
| 65 | fh->ino = ceph_ino(dentry->d_inode); | ||
| 66 | *max_len = sizeof(*fh); | ||
| 67 | type = 1; | ||
| 68 | } else { | ||
| 69 | return -ENOSPC; | ||
| 70 | } | ||
| 71 | return type; | ||
| 72 | } | ||
| 73 | |||
| 74 | /* | ||
| 75 | * convert regular fh to dentry | ||
| 76 | * | ||
| 77 | * FIXME: we should try harder by querying the mds for the ino. | ||
| 78 | */ | ||
| 79 | static struct dentry *__fh_to_dentry(struct super_block *sb, | ||
| 80 | struct ceph_nfs_fh *fh) | ||
| 81 | { | ||
| 82 | struct inode *inode; | ||
| 83 | struct dentry *dentry; | ||
| 84 | struct ceph_vino vino; | ||
| 85 | int err; | ||
| 86 | |||
| 87 | dout("__fh_to_dentry %llx\n", fh->ino); | ||
| 88 | vino.ino = fh->ino; | ||
| 89 | vino.snap = CEPH_NOSNAP; | ||
| 90 | inode = ceph_find_inode(sb, vino); | ||
| 91 | if (!inode) | ||
| 92 | return ERR_PTR(-ESTALE); | ||
| 93 | |||
| 94 | dentry = d_obtain_alias(inode); | ||
| 95 | if (!dentry) { | ||
| 96 | pr_err("fh_to_dentry %llx -- inode %p but ENOMEM\n", | ||
| 97 | fh->ino, inode); | ||
| 98 | iput(inode); | ||
| 99 | return ERR_PTR(-ENOMEM); | ||
| 100 | } | ||
| 101 | err = ceph_init_dentry(dentry); | ||
| 102 | |||
| 103 | if (err < 0) { | ||
| 104 | iput(inode); | ||
| 105 | return ERR_PTR(err); | ||
| 106 | } | ||
| 107 | dout("__fh_to_dentry %llx %p dentry %p\n", fh->ino, inode, dentry); | ||
| 108 | return dentry; | ||
| 109 | } | ||
| 110 | |||
| 111 | /* | ||
| 112 | * convert connectable fh to dentry | ||
| 113 | */ | ||
| 114 | static struct dentry *__cfh_to_dentry(struct super_block *sb, | ||
| 115 | struct ceph_nfs_confh *cfh) | ||
| 116 | { | ||
| 117 | struct ceph_mds_client *mdsc = &ceph_client(sb)->mdsc; | ||
| 118 | struct inode *inode; | ||
| 119 | struct dentry *dentry; | ||
| 120 | struct ceph_vino vino; | ||
| 121 | int err; | ||
| 122 | |||
| 123 | dout("__cfh_to_dentry %llx (%llx/%x)\n", | ||
| 124 | cfh->ino, cfh->parent_ino, cfh->parent_name_hash); | ||
| 125 | |||
| 126 | vino.ino = cfh->ino; | ||
| 127 | vino.snap = CEPH_NOSNAP; | ||
| 128 | inode = ceph_find_inode(sb, vino); | ||
| 129 | if (!inode) { | ||
| 130 | struct ceph_mds_request *req; | ||
| 131 | |||
| 132 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_LOOKUPHASH, | ||
| 133 | USE_ANY_MDS); | ||
| 134 | if (IS_ERR(req)) | ||
| 135 | return ERR_PTR(PTR_ERR(req)); | ||
| 136 | |||
| 137 | req->r_ino1 = vino; | ||
| 138 | req->r_ino2.ino = cfh->parent_ino; | ||
| 139 | req->r_ino2.snap = CEPH_NOSNAP; | ||
| 140 | req->r_path2 = kmalloc(16, GFP_NOFS); | ||
| 141 | snprintf(req->r_path2, 16, "%d", cfh->parent_name_hash); | ||
| 142 | req->r_num_caps = 1; | ||
| 143 | err = ceph_mdsc_do_request(mdsc, NULL, req); | ||
| 144 | ceph_mdsc_put_request(req); | ||
| 145 | inode = ceph_find_inode(sb, vino); | ||
| 146 | if (!inode) | ||
| 147 | return ERR_PTR(err ? err : -ESTALE); | ||
| 148 | } | ||
| 149 | |||
| 150 | dentry = d_obtain_alias(inode); | ||
| 151 | if (!dentry) { | ||
| 152 | pr_err("cfh_to_dentry %llx -- inode %p but ENOMEM\n", | ||
| 153 | cfh->ino, inode); | ||
| 154 | iput(inode); | ||
| 155 | return ERR_PTR(-ENOMEM); | ||
| 156 | } | ||
| 157 | err = ceph_init_dentry(dentry); | ||
| 158 | if (err < 0) { | ||
| 159 | iput(inode); | ||
| 160 | return ERR_PTR(err); | ||
| 161 | } | ||
| 162 | dout("__cfh_to_dentry %llx %p dentry %p\n", cfh->ino, inode, dentry); | ||
| 163 | return dentry; | ||
| 164 | } | ||
| 165 | |||
| 166 | static struct dentry *ceph_fh_to_dentry(struct super_block *sb, struct fid *fid, | ||
| 167 | int fh_len, int fh_type) | ||
| 168 | { | ||
| 169 | if (fh_type == 1) | ||
| 170 | return __fh_to_dentry(sb, (struct ceph_nfs_fh *)fid->raw); | ||
| 171 | else | ||
| 172 | return __cfh_to_dentry(sb, (struct ceph_nfs_confh *)fid->raw); | ||
| 173 | } | ||
| 174 | |||
| 175 | /* | ||
| 176 | * get parent, if possible. | ||
| 177 | * | ||
| 178 | * FIXME: we could do better by querying the mds to discover the | ||
| 179 | * parent. | ||
| 180 | */ | ||
| 181 | static struct dentry *ceph_fh_to_parent(struct super_block *sb, | ||
| 182 | struct fid *fid, | ||
| 183 | int fh_len, int fh_type) | ||
| 184 | { | ||
| 185 | struct ceph_nfs_confh *cfh = (void *)fid->raw; | ||
| 186 | struct ceph_vino vino; | ||
| 187 | struct inode *inode; | ||
| 188 | struct dentry *dentry; | ||
| 189 | int err; | ||
| 190 | |||
| 191 | if (fh_type == 1) | ||
| 192 | return ERR_PTR(-ESTALE); | ||
| 193 | |||
| 194 | pr_debug("fh_to_parent %llx/%d\n", cfh->parent_ino, | ||
| 195 | cfh->parent_name_hash); | ||
| 196 | |||
| 197 | vino.ino = cfh->ino; | ||
| 198 | vino.snap = CEPH_NOSNAP; | ||
| 199 | inode = ceph_find_inode(sb, vino); | ||
| 200 | if (!inode) | ||
| 201 | return ERR_PTR(-ESTALE); | ||
| 202 | |||
| 203 | dentry = d_obtain_alias(inode); | ||
| 204 | if (!dentry) { | ||
| 205 | pr_err("fh_to_parent %llx -- inode %p but ENOMEM\n", | ||
| 206 | cfh->ino, inode); | ||
| 207 | iput(inode); | ||
| 208 | return ERR_PTR(-ENOMEM); | ||
| 209 | } | ||
| 210 | err = ceph_init_dentry(dentry); | ||
| 211 | if (err < 0) { | ||
| 212 | iput(inode); | ||
| 213 | return ERR_PTR(err); | ||
| 214 | } | ||
| 215 | dout("fh_to_parent %llx %p dentry %p\n", cfh->ino, inode, dentry); | ||
| 216 | return dentry; | ||
| 217 | } | ||
| 218 | |||
| 219 | const struct export_operations ceph_export_ops = { | ||
| 220 | .encode_fh = ceph_encode_fh, | ||
| 221 | .fh_to_dentry = ceph_fh_to_dentry, | ||
| 222 | .fh_to_parent = ceph_fh_to_parent, | ||
| 223 | }; | ||
diff --git a/fs/ceph/file.c b/fs/ceph/file.c new file mode 100644 index 000000000000..5d2af8464f6a --- /dev/null +++ b/fs/ceph/file.c | |||
| @@ -0,0 +1,937 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/sched.h> | ||
| 4 | #include <linux/file.h> | ||
| 5 | #include <linux/namei.h> | ||
| 6 | #include <linux/writeback.h> | ||
| 7 | |||
| 8 | #include "super.h" | ||
| 9 | #include "mds_client.h" | ||
| 10 | |||
| 11 | /* | ||
| 12 | * Ceph file operations | ||
| 13 | * | ||
| 14 | * Implement basic open/close functionality, and implement | ||
| 15 | * read/write. | ||
| 16 | * | ||
| 17 | * We implement three modes of file I/O: | ||
| 18 | * - buffered uses the generic_file_aio_{read,write} helpers | ||
| 19 | * | ||
| 20 | * - synchronous is used when there is multi-client read/write | ||
| 21 | * sharing, avoids the page cache, and synchronously waits for an | ||
| 22 | * ack from the OSD. | ||
| 23 | * | ||
| 24 | * - direct io takes the variant of the sync path that references | ||
| 25 | * user pages directly. | ||
| 26 | * | ||
| 27 | * fsync() flushes and waits on dirty pages, but just queues metadata | ||
| 28 | * for writeback: since the MDS can recover size and mtime there is no | ||
| 29 | * need to wait for MDS acknowledgement. | ||
| 30 | */ | ||
| 31 | |||
| 32 | |||
| 33 | /* | ||
| 34 | * Prepare an open request. Preallocate ceph_cap to avoid an | ||
| 35 | * inopportune ENOMEM later. | ||
| 36 | */ | ||
| 37 | static struct ceph_mds_request * | ||
| 38 | prepare_open_request(struct super_block *sb, int flags, int create_mode) | ||
| 39 | { | ||
| 40 | struct ceph_client *client = ceph_sb_to_client(sb); | ||
| 41 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 42 | struct ceph_mds_request *req; | ||
| 43 | int want_auth = USE_ANY_MDS; | ||
| 44 | int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN; | ||
| 45 | |||
| 46 | if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC)) | ||
| 47 | want_auth = USE_AUTH_MDS; | ||
| 48 | |||
| 49 | req = ceph_mdsc_create_request(mdsc, op, want_auth); | ||
| 50 | if (IS_ERR(req)) | ||
| 51 | goto out; | ||
| 52 | req->r_fmode = ceph_flags_to_mode(flags); | ||
| 53 | req->r_args.open.flags = cpu_to_le32(flags); | ||
| 54 | req->r_args.open.mode = cpu_to_le32(create_mode); | ||
| 55 | req->r_args.open.preferred = cpu_to_le32(-1); | ||
| 56 | out: | ||
| 57 | return req; | ||
| 58 | } | ||
| 59 | |||
| 60 | /* | ||
| 61 | * initialize private struct file data. | ||
| 62 | * if we fail, clean up by dropping fmode reference on the ceph_inode | ||
| 63 | */ | ||
| 64 | static int ceph_init_file(struct inode *inode, struct file *file, int fmode) | ||
| 65 | { | ||
| 66 | struct ceph_file_info *cf; | ||
| 67 | int ret = 0; | ||
| 68 | |||
| 69 | switch (inode->i_mode & S_IFMT) { | ||
| 70 | case S_IFREG: | ||
| 71 | case S_IFDIR: | ||
| 72 | dout("init_file %p %p 0%o (regular)\n", inode, file, | ||
| 73 | inode->i_mode); | ||
| 74 | cf = kmem_cache_alloc(ceph_file_cachep, GFP_NOFS | __GFP_ZERO); | ||
| 75 | if (cf == NULL) { | ||
| 76 | ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */ | ||
| 77 | return -ENOMEM; | ||
| 78 | } | ||
| 79 | cf->fmode = fmode; | ||
| 80 | cf->next_offset = 2; | ||
| 81 | file->private_data = cf; | ||
| 82 | BUG_ON(inode->i_fop->release != ceph_release); | ||
| 83 | break; | ||
| 84 | |||
| 85 | case S_IFLNK: | ||
| 86 | dout("init_file %p %p 0%o (symlink)\n", inode, file, | ||
| 87 | inode->i_mode); | ||
| 88 | ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */ | ||
| 89 | break; | ||
| 90 | |||
| 91 | default: | ||
| 92 | dout("init_file %p %p 0%o (special)\n", inode, file, | ||
| 93 | inode->i_mode); | ||
| 94 | /* | ||
| 95 | * we need to drop the open ref now, since we don't | ||
| 96 | * have .release set to ceph_release. | ||
| 97 | */ | ||
| 98 | ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */ | ||
| 99 | BUG_ON(inode->i_fop->release == ceph_release); | ||
| 100 | |||
| 101 | /* call the proper open fop */ | ||
| 102 | ret = inode->i_fop->open(inode, file); | ||
| 103 | } | ||
| 104 | return ret; | ||
| 105 | } | ||
| 106 | |||
| 107 | /* | ||
| 108 | * If the filp already has private_data, that means the file was | ||
| 109 | * already opened by intent during lookup, and we do nothing. | ||
| 110 | * | ||
| 111 | * If we already have the requisite capabilities, we can satisfy | ||
| 112 | * the open request locally (no need to request new caps from the | ||
| 113 | * MDS). We do, however, need to inform the MDS (asynchronously) | ||
| 114 | * if our wanted caps set expands. | ||
| 115 | */ | ||
| 116 | int ceph_open(struct inode *inode, struct file *file) | ||
| 117 | { | ||
| 118 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 119 | struct ceph_client *client = ceph_sb_to_client(inode->i_sb); | ||
| 120 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 121 | struct ceph_mds_request *req; | ||
| 122 | struct ceph_file_info *cf = file->private_data; | ||
| 123 | struct inode *parent_inode = file->f_dentry->d_parent->d_inode; | ||
| 124 | int err; | ||
| 125 | int flags, fmode, wanted; | ||
| 126 | |||
| 127 | if (cf) { | ||
| 128 | dout("open file %p is already opened\n", file); | ||
| 129 | return 0; | ||
| 130 | } | ||
| 131 | |||
| 132 | /* filter out O_CREAT|O_EXCL; vfs did that already. yuck. */ | ||
| 133 | flags = file->f_flags & ~(O_CREAT|O_EXCL); | ||
| 134 | if (S_ISDIR(inode->i_mode)) | ||
| 135 | flags = O_DIRECTORY; /* mds likes to know */ | ||
| 136 | |||
| 137 | dout("open inode %p ino %llx.%llx file %p flags %d (%d)\n", inode, | ||
| 138 | ceph_vinop(inode), file, flags, file->f_flags); | ||
| 139 | fmode = ceph_flags_to_mode(flags); | ||
| 140 | wanted = ceph_caps_for_mode(fmode); | ||
| 141 | |||
| 142 | /* snapped files are read-only */ | ||
| 143 | if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE)) | ||
| 144 | return -EROFS; | ||
| 145 | |||
| 146 | /* trivially open snapdir */ | ||
| 147 | if (ceph_snap(inode) == CEPH_SNAPDIR) { | ||
| 148 | spin_lock(&inode->i_lock); | ||
| 149 | __ceph_get_fmode(ci, fmode); | ||
| 150 | spin_unlock(&inode->i_lock); | ||
| 151 | return ceph_init_file(inode, file, fmode); | ||
| 152 | } | ||
| 153 | |||
| 154 | /* | ||
| 155 | * No need to block if we have any caps. Update wanted set | ||
| 156 | * asynchronously. | ||
| 157 | */ | ||
| 158 | spin_lock(&inode->i_lock); | ||
| 159 | if (__ceph_is_any_real_caps(ci)) { | ||
| 160 | int mds_wanted = __ceph_caps_mds_wanted(ci); | ||
| 161 | int issued = __ceph_caps_issued(ci, NULL); | ||
| 162 | |||
| 163 | dout("open %p fmode %d want %s issued %s using existing\n", | ||
| 164 | inode, fmode, ceph_cap_string(wanted), | ||
| 165 | ceph_cap_string(issued)); | ||
| 166 | __ceph_get_fmode(ci, fmode); | ||
| 167 | spin_unlock(&inode->i_lock); | ||
| 168 | |||
| 169 | /* adjust wanted? */ | ||
| 170 | if ((issued & wanted) != wanted && | ||
| 171 | (mds_wanted & wanted) != wanted && | ||
| 172 | ceph_snap(inode) != CEPH_SNAPDIR) | ||
| 173 | ceph_check_caps(ci, 0, NULL); | ||
| 174 | |||
| 175 | return ceph_init_file(inode, file, fmode); | ||
| 176 | } else if (ceph_snap(inode) != CEPH_NOSNAP && | ||
| 177 | (ci->i_snap_caps & wanted) == wanted) { | ||
| 178 | __ceph_get_fmode(ci, fmode); | ||
| 179 | spin_unlock(&inode->i_lock); | ||
| 180 | return ceph_init_file(inode, file, fmode); | ||
| 181 | } | ||
| 182 | spin_unlock(&inode->i_lock); | ||
| 183 | |||
| 184 | dout("open fmode %d wants %s\n", fmode, ceph_cap_string(wanted)); | ||
| 185 | req = prepare_open_request(inode->i_sb, flags, 0); | ||
| 186 | if (IS_ERR(req)) { | ||
| 187 | err = PTR_ERR(req); | ||
| 188 | goto out; | ||
| 189 | } | ||
| 190 | req->r_inode = igrab(inode); | ||
| 191 | req->r_num_caps = 1; | ||
| 192 | err = ceph_mdsc_do_request(mdsc, parent_inode, req); | ||
| 193 | if (!err) | ||
| 194 | err = ceph_init_file(inode, file, req->r_fmode); | ||
| 195 | ceph_mdsc_put_request(req); | ||
| 196 | dout("open result=%d on %llx.%llx\n", err, ceph_vinop(inode)); | ||
| 197 | out: | ||
| 198 | return err; | ||
| 199 | } | ||
| 200 | |||
| 201 | |||
| 202 | /* | ||
| 203 | * Do a lookup + open with a single request. | ||
| 204 | * | ||
| 205 | * If this succeeds, but some subsequent check in the vfs | ||
| 206 | * may_open() fails, the struct *file gets cleaned up (i.e. | ||
| 207 | * ceph_release gets called). So fear not! | ||
| 208 | */ | ||
| 209 | /* | ||
| 210 | * flags | ||
| 211 | * path_lookup_open -> LOOKUP_OPEN | ||
| 212 | * path_lookup_create -> LOOKUP_OPEN|LOOKUP_CREATE | ||
| 213 | */ | ||
| 214 | struct dentry *ceph_lookup_open(struct inode *dir, struct dentry *dentry, | ||
| 215 | struct nameidata *nd, int mode, | ||
| 216 | int locked_dir) | ||
| 217 | { | ||
| 218 | struct ceph_client *client = ceph_sb_to_client(dir->i_sb); | ||
| 219 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 220 | struct file *file = nd->intent.open.file; | ||
| 221 | struct inode *parent_inode = get_dentry_parent_inode(file->f_dentry); | ||
| 222 | struct ceph_mds_request *req; | ||
| 223 | int err; | ||
| 224 | int flags = nd->intent.open.flags - 1; /* silly vfs! */ | ||
| 225 | |||
| 226 | dout("ceph_lookup_open dentry %p '%.*s' flags %d mode 0%o\n", | ||
| 227 | dentry, dentry->d_name.len, dentry->d_name.name, flags, mode); | ||
| 228 | |||
| 229 | /* do the open */ | ||
| 230 | req = prepare_open_request(dir->i_sb, flags, mode); | ||
| 231 | if (IS_ERR(req)) | ||
| 232 | return ERR_PTR(PTR_ERR(req)); | ||
| 233 | req->r_dentry = dget(dentry); | ||
| 234 | req->r_num_caps = 2; | ||
| 235 | if (flags & O_CREAT) { | ||
| 236 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; | ||
| 237 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; | ||
| 238 | } | ||
| 239 | req->r_locked_dir = dir; /* caller holds dir->i_mutex */ | ||
| 240 | err = ceph_mdsc_do_request(mdsc, parent_inode, req); | ||
| 241 | dentry = ceph_finish_lookup(req, dentry, err); | ||
| 242 | if (!err && (flags & O_CREAT) && !req->r_reply_info.head->is_dentry) | ||
| 243 | err = ceph_handle_notrace_create(dir, dentry); | ||
| 244 | if (!err) | ||
| 245 | err = ceph_init_file(req->r_dentry->d_inode, file, | ||
| 246 | req->r_fmode); | ||
| 247 | ceph_mdsc_put_request(req); | ||
| 248 | dout("ceph_lookup_open result=%p\n", dentry); | ||
| 249 | return dentry; | ||
| 250 | } | ||
| 251 | |||
| 252 | int ceph_release(struct inode *inode, struct file *file) | ||
| 253 | { | ||
| 254 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 255 | struct ceph_file_info *cf = file->private_data; | ||
| 256 | |||
| 257 | dout("release inode %p file %p\n", inode, file); | ||
| 258 | ceph_put_fmode(ci, cf->fmode); | ||
| 259 | if (cf->last_readdir) | ||
| 260 | ceph_mdsc_put_request(cf->last_readdir); | ||
| 261 | kfree(cf->last_name); | ||
| 262 | kfree(cf->dir_info); | ||
| 263 | dput(cf->dentry); | ||
| 264 | kmem_cache_free(ceph_file_cachep, cf); | ||
| 265 | |||
| 266 | /* wake up anyone waiting for caps on this inode */ | ||
| 267 | wake_up(&ci->i_cap_wq); | ||
| 268 | return 0; | ||
| 269 | } | ||
| 270 | |||
| 271 | /* | ||
| 272 | * build a vector of user pages | ||
| 273 | */ | ||
| 274 | static struct page **get_direct_page_vector(const char __user *data, | ||
| 275 | int num_pages, | ||
| 276 | loff_t off, size_t len) | ||
| 277 | { | ||
| 278 | struct page **pages; | ||
| 279 | int rc; | ||
| 280 | |||
| 281 | pages = kmalloc(sizeof(*pages) * num_pages, GFP_NOFS); | ||
| 282 | if (!pages) | ||
| 283 | return ERR_PTR(-ENOMEM); | ||
| 284 | |||
| 285 | down_read(¤t->mm->mmap_sem); | ||
| 286 | rc = get_user_pages(current, current->mm, (unsigned long)data, | ||
| 287 | num_pages, 0, 0, pages, NULL); | ||
| 288 | up_read(¤t->mm->mmap_sem); | ||
| 289 | if (rc < 0) | ||
| 290 | goto fail; | ||
| 291 | return pages; | ||
| 292 | |||
| 293 | fail: | ||
| 294 | kfree(pages); | ||
| 295 | return ERR_PTR(rc); | ||
| 296 | } | ||
| 297 | |||
| 298 | static void put_page_vector(struct page **pages, int num_pages) | ||
| 299 | { | ||
| 300 | int i; | ||
| 301 | |||
| 302 | for (i = 0; i < num_pages; i++) | ||
| 303 | put_page(pages[i]); | ||
| 304 | kfree(pages); | ||
| 305 | } | ||
| 306 | |||
| 307 | void ceph_release_page_vector(struct page **pages, int num_pages) | ||
| 308 | { | ||
| 309 | int i; | ||
| 310 | |||
| 311 | for (i = 0; i < num_pages; i++) | ||
| 312 | __free_pages(pages[i], 0); | ||
| 313 | kfree(pages); | ||
| 314 | } | ||
| 315 | |||
| 316 | /* | ||
| 317 | * allocate a vector new pages | ||
| 318 | */ | ||
| 319 | static struct page **alloc_page_vector(int num_pages) | ||
| 320 | { | ||
| 321 | struct page **pages; | ||
| 322 | int i; | ||
| 323 | |||
| 324 | pages = kmalloc(sizeof(*pages) * num_pages, GFP_NOFS); | ||
| 325 | if (!pages) | ||
| 326 | return ERR_PTR(-ENOMEM); | ||
| 327 | for (i = 0; i < num_pages; i++) { | ||
| 328 | pages[i] = alloc_page(GFP_NOFS); | ||
| 329 | if (pages[i] == NULL) { | ||
| 330 | ceph_release_page_vector(pages, i); | ||
| 331 | return ERR_PTR(-ENOMEM); | ||
| 332 | } | ||
| 333 | } | ||
| 334 | return pages; | ||
| 335 | } | ||
| 336 | |||
| 337 | /* | ||
| 338 | * copy user data into a page vector | ||
| 339 | */ | ||
| 340 | static int copy_user_to_page_vector(struct page **pages, | ||
| 341 | const char __user *data, | ||
| 342 | loff_t off, size_t len) | ||
| 343 | { | ||
| 344 | int i = 0; | ||
| 345 | int po = off & ~PAGE_CACHE_MASK; | ||
| 346 | int left = len; | ||
| 347 | int l, bad; | ||
| 348 | |||
| 349 | while (left > 0) { | ||
| 350 | l = min_t(int, PAGE_CACHE_SIZE-po, left); | ||
| 351 | bad = copy_from_user(page_address(pages[i]) + po, data, l); | ||
| 352 | if (bad == l) | ||
| 353 | return -EFAULT; | ||
| 354 | data += l - bad; | ||
| 355 | left -= l - bad; | ||
| 356 | po += l - bad; | ||
| 357 | if (po == PAGE_CACHE_SIZE) { | ||
| 358 | po = 0; | ||
| 359 | i++; | ||
| 360 | } | ||
| 361 | } | ||
| 362 | return len; | ||
| 363 | } | ||
| 364 | |||
| 365 | /* | ||
| 366 | * copy user data from a page vector into a user pointer | ||
| 367 | */ | ||
| 368 | static int copy_page_vector_to_user(struct page **pages, char __user *data, | ||
| 369 | loff_t off, size_t len) | ||
| 370 | { | ||
| 371 | int i = 0; | ||
| 372 | int po = off & ~PAGE_CACHE_MASK; | ||
| 373 | int left = len; | ||
| 374 | int l, bad; | ||
| 375 | |||
| 376 | while (left > 0) { | ||
| 377 | l = min_t(int, left, PAGE_CACHE_SIZE-po); | ||
| 378 | bad = copy_to_user(data, page_address(pages[i]) + po, l); | ||
| 379 | if (bad == l) | ||
| 380 | return -EFAULT; | ||
| 381 | data += l - bad; | ||
| 382 | left -= l - bad; | ||
| 383 | if (po) { | ||
| 384 | po += l - bad; | ||
| 385 | if (po == PAGE_CACHE_SIZE) | ||
| 386 | po = 0; | ||
| 387 | } | ||
| 388 | i++; | ||
| 389 | } | ||
| 390 | return len; | ||
| 391 | } | ||
| 392 | |||
| 393 | /* | ||
| 394 | * Zero an extent within a page vector. Offset is relative to the | ||
| 395 | * start of the first page. | ||
| 396 | */ | ||
| 397 | static void zero_page_vector_range(int off, int len, struct page **pages) | ||
| 398 | { | ||
| 399 | int i = off >> PAGE_CACHE_SHIFT; | ||
| 400 | |||
| 401 | off &= ~PAGE_CACHE_MASK; | ||
| 402 | |||
| 403 | dout("zero_page_vector_page %u~%u\n", off, len); | ||
| 404 | |||
| 405 | /* leading partial page? */ | ||
| 406 | if (off) { | ||
| 407 | int end = min((int)PAGE_CACHE_SIZE, off + len); | ||
| 408 | dout("zeroing %d %p head from %d\n", i, pages[i], | ||
| 409 | (int)off); | ||
| 410 | zero_user_segment(pages[i], off, end); | ||
| 411 | len -= (end - off); | ||
| 412 | i++; | ||
| 413 | } | ||
| 414 | while (len >= PAGE_CACHE_SIZE) { | ||
| 415 | dout("zeroing %d %p len=%d\n", i, pages[i], len); | ||
| 416 | zero_user_segment(pages[i], 0, PAGE_CACHE_SIZE); | ||
| 417 | len -= PAGE_CACHE_SIZE; | ||
| 418 | i++; | ||
| 419 | } | ||
| 420 | /* trailing partial page? */ | ||
| 421 | if (len) { | ||
| 422 | dout("zeroing %d %p tail to %d\n", i, pages[i], (int)len); | ||
| 423 | zero_user_segment(pages[i], 0, len); | ||
| 424 | } | ||
| 425 | } | ||
| 426 | |||
| 427 | |||
| 428 | /* | ||
| 429 | * Read a range of bytes striped over one or more objects. Iterate over | ||
| 430 | * objects we stripe over. (That's not atomic, but good enough for now.) | ||
| 431 | * | ||
| 432 | * If we get a short result from the OSD, check against i_size; we need to | ||
| 433 | * only return a short read to the caller if we hit EOF. | ||
| 434 | */ | ||
| 435 | static int striped_read(struct inode *inode, | ||
| 436 | u64 off, u64 len, | ||
| 437 | struct page **pages, int num_pages, | ||
| 438 | int *checkeof) | ||
| 439 | { | ||
| 440 | struct ceph_client *client = ceph_inode_to_client(inode); | ||
| 441 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 442 | u64 pos, this_len; | ||
| 443 | int page_off = off & ~PAGE_CACHE_MASK; /* first byte's offset in page */ | ||
| 444 | int left, pages_left; | ||
| 445 | int read; | ||
| 446 | struct page **page_pos; | ||
| 447 | int ret; | ||
| 448 | bool hit_stripe, was_short; | ||
| 449 | |||
| 450 | /* | ||
| 451 | * we may need to do multiple reads. not atomic, unfortunately. | ||
| 452 | */ | ||
| 453 | pos = off; | ||
| 454 | left = len; | ||
| 455 | page_pos = pages; | ||
| 456 | pages_left = num_pages; | ||
| 457 | read = 0; | ||
| 458 | |||
| 459 | more: | ||
| 460 | this_len = left; | ||
| 461 | ret = ceph_osdc_readpages(&client->osdc, ceph_vino(inode), | ||
| 462 | &ci->i_layout, pos, &this_len, | ||
| 463 | ci->i_truncate_seq, | ||
| 464 | ci->i_truncate_size, | ||
| 465 | page_pos, pages_left); | ||
| 466 | hit_stripe = this_len < left; | ||
| 467 | was_short = ret >= 0 && ret < this_len; | ||
| 468 | if (ret == -ENOENT) | ||
| 469 | ret = 0; | ||
| 470 | dout("striped_read %llu~%u (read %u) got %d%s%s\n", pos, left, read, | ||
| 471 | ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : ""); | ||
| 472 | |||
| 473 | if (ret > 0) { | ||
| 474 | int didpages = | ||
| 475 | ((pos & ~PAGE_CACHE_MASK) + ret) >> PAGE_CACHE_SHIFT; | ||
| 476 | |||
| 477 | if (read < pos - off) { | ||
| 478 | dout(" zero gap %llu to %llu\n", off + read, pos); | ||
| 479 | zero_page_vector_range(page_off + read, | ||
| 480 | pos - off - read, pages); | ||
| 481 | } | ||
| 482 | pos += ret; | ||
| 483 | read = pos - off; | ||
| 484 | left -= ret; | ||
| 485 | page_pos += didpages; | ||
| 486 | pages_left -= didpages; | ||
| 487 | |||
| 488 | /* hit stripe? */ | ||
| 489 | if (left && hit_stripe) | ||
| 490 | goto more; | ||
| 491 | } | ||
| 492 | |||
| 493 | if (was_short) { | ||
| 494 | /* was original extent fully inside i_size? */ | ||
| 495 | if (pos + left <= inode->i_size) { | ||
| 496 | dout("zero tail\n"); | ||
| 497 | zero_page_vector_range(page_off + read, len - read, | ||
| 498 | pages); | ||
| 499 | read = len; | ||
| 500 | goto out; | ||
| 501 | } | ||
| 502 | |||
| 503 | /* check i_size */ | ||
| 504 | *checkeof = 1; | ||
| 505 | } | ||
| 506 | |||
| 507 | out: | ||
| 508 | if (ret >= 0) | ||
| 509 | ret = read; | ||
| 510 | dout("striped_read returns %d\n", ret); | ||
| 511 | return ret; | ||
| 512 | } | ||
| 513 | |||
| 514 | /* | ||
| 515 | * Completely synchronous read and write methods. Direct from __user | ||
| 516 | * buffer to osd, or directly to user pages (if O_DIRECT). | ||
| 517 | * | ||
| 518 | * If the read spans object boundary, just do multiple reads. | ||
| 519 | */ | ||
| 520 | static ssize_t ceph_sync_read(struct file *file, char __user *data, | ||
| 521 | unsigned len, loff_t *poff, int *checkeof) | ||
| 522 | { | ||
| 523 | struct inode *inode = file->f_dentry->d_inode; | ||
| 524 | struct page **pages; | ||
| 525 | u64 off = *poff; | ||
| 526 | int num_pages = calc_pages_for(off, len); | ||
| 527 | int ret; | ||
| 528 | |||
| 529 | dout("sync_read on file %p %llu~%u %s\n", file, off, len, | ||
| 530 | (file->f_flags & O_DIRECT) ? "O_DIRECT" : ""); | ||
| 531 | |||
| 532 | if (file->f_flags & O_DIRECT) { | ||
| 533 | pages = get_direct_page_vector(data, num_pages, off, len); | ||
| 534 | |||
| 535 | /* | ||
| 536 | * flush any page cache pages in this range. this | ||
| 537 | * will make concurrent normal and O_DIRECT io slow, | ||
| 538 | * but it will at least behave sensibly when they are | ||
| 539 | * in sequence. | ||
| 540 | */ | ||
| 541 | } else { | ||
| 542 | pages = alloc_page_vector(num_pages); | ||
| 543 | } | ||
| 544 | if (IS_ERR(pages)) | ||
| 545 | return PTR_ERR(pages); | ||
| 546 | |||
| 547 | ret = filemap_write_and_wait(inode->i_mapping); | ||
| 548 | if (ret < 0) | ||
| 549 | goto done; | ||
| 550 | |||
| 551 | ret = striped_read(inode, off, len, pages, num_pages, checkeof); | ||
| 552 | |||
| 553 | if (ret >= 0 && (file->f_flags & O_DIRECT) == 0) | ||
| 554 | ret = copy_page_vector_to_user(pages, data, off, ret); | ||
| 555 | if (ret >= 0) | ||
| 556 | *poff = off + ret; | ||
| 557 | |||
| 558 | done: | ||
| 559 | if (file->f_flags & O_DIRECT) | ||
| 560 | put_page_vector(pages, num_pages); | ||
| 561 | else | ||
| 562 | ceph_release_page_vector(pages, num_pages); | ||
| 563 | dout("sync_read result %d\n", ret); | ||
| 564 | return ret; | ||
| 565 | } | ||
| 566 | |||
| 567 | /* | ||
| 568 | * Write commit callback, called if we requested both an ACK and | ||
| 569 | * ONDISK commit reply from the OSD. | ||
| 570 | */ | ||
| 571 | static void sync_write_commit(struct ceph_osd_request *req, | ||
| 572 | struct ceph_msg *msg) | ||
| 573 | { | ||
| 574 | struct ceph_inode_info *ci = ceph_inode(req->r_inode); | ||
| 575 | |||
| 576 | dout("sync_write_commit %p tid %llu\n", req, req->r_tid); | ||
| 577 | spin_lock(&ci->i_unsafe_lock); | ||
| 578 | list_del_init(&req->r_unsafe_item); | ||
| 579 | spin_unlock(&ci->i_unsafe_lock); | ||
| 580 | ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR); | ||
| 581 | } | ||
| 582 | |||
| 583 | /* | ||
| 584 | * Synchronous write, straight from __user pointer or user pages (if | ||
| 585 | * O_DIRECT). | ||
| 586 | * | ||
| 587 | * If write spans object boundary, just do multiple writes. (For a | ||
| 588 | * correct atomic write, we should e.g. take write locks on all | ||
| 589 | * objects, rollback on failure, etc.) | ||
| 590 | */ | ||
| 591 | static ssize_t ceph_sync_write(struct file *file, const char __user *data, | ||
| 592 | size_t left, loff_t *offset) | ||
| 593 | { | ||
| 594 | struct inode *inode = file->f_dentry->d_inode; | ||
| 595 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 596 | struct ceph_client *client = ceph_inode_to_client(inode); | ||
| 597 | struct ceph_osd_request *req; | ||
| 598 | struct page **pages; | ||
| 599 | int num_pages; | ||
| 600 | long long unsigned pos; | ||
| 601 | u64 len; | ||
| 602 | int written = 0; | ||
| 603 | int flags; | ||
| 604 | int do_sync = 0; | ||
| 605 | int check_caps = 0; | ||
| 606 | int ret; | ||
| 607 | struct timespec mtime = CURRENT_TIME; | ||
| 608 | |||
| 609 | if (ceph_snap(file->f_dentry->d_inode) != CEPH_NOSNAP) | ||
| 610 | return -EROFS; | ||
| 611 | |||
| 612 | dout("sync_write on file %p %lld~%u %s\n", file, *offset, | ||
| 613 | (unsigned)left, (file->f_flags & O_DIRECT) ? "O_DIRECT" : ""); | ||
| 614 | |||
| 615 | if (file->f_flags & O_APPEND) | ||
| 616 | pos = i_size_read(inode); | ||
| 617 | else | ||
| 618 | pos = *offset; | ||
| 619 | |||
| 620 | ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + left); | ||
| 621 | if (ret < 0) | ||
| 622 | return ret; | ||
| 623 | |||
| 624 | ret = invalidate_inode_pages2_range(inode->i_mapping, | ||
| 625 | pos >> PAGE_CACHE_SHIFT, | ||
| 626 | (pos + left) >> PAGE_CACHE_SHIFT); | ||
| 627 | if (ret < 0) | ||
| 628 | dout("invalidate_inode_pages2_range returned %d\n", ret); | ||
| 629 | |||
| 630 | flags = CEPH_OSD_FLAG_ORDERSNAP | | ||
| 631 | CEPH_OSD_FLAG_ONDISK | | ||
| 632 | CEPH_OSD_FLAG_WRITE; | ||
| 633 | if ((file->f_flags & (O_SYNC|O_DIRECT)) == 0) | ||
| 634 | flags |= CEPH_OSD_FLAG_ACK; | ||
| 635 | else | ||
| 636 | do_sync = 1; | ||
| 637 | |||
| 638 | /* | ||
| 639 | * we may need to do multiple writes here if we span an object | ||
| 640 | * boundary. this isn't atomic, unfortunately. :( | ||
| 641 | */ | ||
| 642 | more: | ||
| 643 | len = left; | ||
| 644 | req = ceph_osdc_new_request(&client->osdc, &ci->i_layout, | ||
| 645 | ceph_vino(inode), pos, &len, | ||
| 646 | CEPH_OSD_OP_WRITE, flags, | ||
| 647 | ci->i_snap_realm->cached_context, | ||
| 648 | do_sync, | ||
| 649 | ci->i_truncate_seq, ci->i_truncate_size, | ||
| 650 | &mtime, false, 2); | ||
| 651 | if (IS_ERR(req)) | ||
| 652 | return PTR_ERR(req); | ||
| 653 | |||
| 654 | num_pages = calc_pages_for(pos, len); | ||
| 655 | |||
| 656 | if (file->f_flags & O_DIRECT) { | ||
| 657 | pages = get_direct_page_vector(data, num_pages, pos, len); | ||
| 658 | if (IS_ERR(pages)) { | ||
| 659 | ret = PTR_ERR(pages); | ||
| 660 | goto out; | ||
| 661 | } | ||
| 662 | |||
| 663 | /* | ||
| 664 | * throw out any page cache pages in this range. this | ||
| 665 | * may block. | ||
| 666 | */ | ||
| 667 | truncate_inode_pages_range(inode->i_mapping, pos, pos+len); | ||
| 668 | } else { | ||
| 669 | pages = alloc_page_vector(num_pages); | ||
| 670 | if (IS_ERR(pages)) { | ||
| 671 | ret = PTR_ERR(pages); | ||
| 672 | goto out; | ||
| 673 | } | ||
| 674 | ret = copy_user_to_page_vector(pages, data, pos, len); | ||
| 675 | if (ret < 0) { | ||
| 676 | ceph_release_page_vector(pages, num_pages); | ||
| 677 | goto out; | ||
| 678 | } | ||
| 679 | |||
| 680 | if ((file->f_flags & O_SYNC) == 0) { | ||
| 681 | /* get a second commit callback */ | ||
| 682 | req->r_safe_callback = sync_write_commit; | ||
| 683 | req->r_own_pages = 1; | ||
| 684 | } | ||
| 685 | } | ||
| 686 | req->r_pages = pages; | ||
| 687 | req->r_num_pages = num_pages; | ||
| 688 | req->r_inode = inode; | ||
| 689 | |||
| 690 | ret = ceph_osdc_start_request(&client->osdc, req, false); | ||
| 691 | if (!ret) { | ||
| 692 | if (req->r_safe_callback) { | ||
| 693 | /* | ||
| 694 | * Add to inode unsafe list only after we | ||
| 695 | * start_request so that a tid has been assigned. | ||
| 696 | */ | ||
| 697 | spin_lock(&ci->i_unsafe_lock); | ||
| 698 | list_add(&ci->i_unsafe_writes, &req->r_unsafe_item); | ||
| 699 | spin_unlock(&ci->i_unsafe_lock); | ||
| 700 | ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR); | ||
| 701 | } | ||
| 702 | ret = ceph_osdc_wait_request(&client->osdc, req); | ||
| 703 | } | ||
| 704 | |||
| 705 | if (file->f_flags & O_DIRECT) | ||
| 706 | put_page_vector(pages, num_pages); | ||
| 707 | else if (file->f_flags & O_SYNC) | ||
| 708 | ceph_release_page_vector(pages, num_pages); | ||
| 709 | |||
| 710 | out: | ||
| 711 | ceph_osdc_put_request(req); | ||
| 712 | if (ret == 0) { | ||
| 713 | pos += len; | ||
| 714 | written += len; | ||
| 715 | left -= len; | ||
| 716 | if (left) | ||
| 717 | goto more; | ||
| 718 | |||
| 719 | ret = written; | ||
| 720 | *offset = pos; | ||
| 721 | if (pos > i_size_read(inode)) | ||
| 722 | check_caps = ceph_inode_set_size(inode, pos); | ||
| 723 | if (check_caps) | ||
| 724 | ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, | ||
| 725 | NULL); | ||
| 726 | } | ||
| 727 | return ret; | ||
| 728 | } | ||
| 729 | |||
| 730 | /* | ||
| 731 | * Wrap generic_file_aio_read with checks for cap bits on the inode. | ||
| 732 | * Atomically grab references, so that those bits are not released | ||
| 733 | * back to the MDS mid-read. | ||
| 734 | * | ||
| 735 | * Hmm, the sync read case isn't actually async... should it be? | ||
| 736 | */ | ||
| 737 | static ssize_t ceph_aio_read(struct kiocb *iocb, const struct iovec *iov, | ||
| 738 | unsigned long nr_segs, loff_t pos) | ||
| 739 | { | ||
| 740 | struct file *filp = iocb->ki_filp; | ||
| 741 | loff_t *ppos = &iocb->ki_pos; | ||
| 742 | size_t len = iov->iov_len; | ||
| 743 | struct inode *inode = filp->f_dentry->d_inode; | ||
| 744 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 745 | void *base = iov->iov_base; | ||
| 746 | ssize_t ret; | ||
| 747 | int got = 0; | ||
| 748 | int checkeof = 0, read = 0; | ||
| 749 | |||
| 750 | dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n", | ||
| 751 | inode, ceph_vinop(inode), pos, (unsigned)len, inode); | ||
| 752 | again: | ||
| 753 | __ceph_do_pending_vmtruncate(inode); | ||
| 754 | ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, CEPH_CAP_FILE_CACHE, | ||
| 755 | &got, -1); | ||
| 756 | if (ret < 0) | ||
| 757 | goto out; | ||
| 758 | dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n", | ||
| 759 | inode, ceph_vinop(inode), pos, (unsigned)len, | ||
| 760 | ceph_cap_string(got)); | ||
| 761 | |||
| 762 | if ((got & CEPH_CAP_FILE_CACHE) == 0 || | ||
| 763 | (iocb->ki_filp->f_flags & O_DIRECT) || | ||
| 764 | (inode->i_sb->s_flags & MS_SYNCHRONOUS)) | ||
| 765 | /* hmm, this isn't really async... */ | ||
| 766 | ret = ceph_sync_read(filp, base, len, ppos, &checkeof); | ||
| 767 | else | ||
| 768 | ret = generic_file_aio_read(iocb, iov, nr_segs, pos); | ||
| 769 | |||
| 770 | out: | ||
| 771 | dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n", | ||
| 772 | inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret); | ||
| 773 | ceph_put_cap_refs(ci, got); | ||
| 774 | |||
| 775 | if (checkeof && ret >= 0) { | ||
| 776 | int statret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE); | ||
| 777 | |||
| 778 | /* hit EOF or hole? */ | ||
| 779 | if (statret == 0 && *ppos < inode->i_size) { | ||
| 780 | dout("aio_read sync_read hit hole, reading more\n"); | ||
| 781 | read += ret; | ||
| 782 | base += ret; | ||
| 783 | len -= ret; | ||
| 784 | checkeof = 0; | ||
| 785 | goto again; | ||
| 786 | } | ||
| 787 | } | ||
| 788 | if (ret >= 0) | ||
| 789 | ret += read; | ||
| 790 | |||
| 791 | return ret; | ||
| 792 | } | ||
| 793 | |||
| 794 | /* | ||
| 795 | * Take cap references to avoid releasing caps to MDS mid-write. | ||
| 796 | * | ||
| 797 | * If we are synchronous, and write with an old snap context, the OSD | ||
| 798 | * may return EOLDSNAPC. In that case, retry the write.. _after_ | ||
| 799 | * dropping our cap refs and allowing the pending snap to logically | ||
| 800 | * complete _before_ this write occurs. | ||
| 801 | * | ||
| 802 | * If we are near ENOSPC, write synchronously. | ||
| 803 | */ | ||
| 804 | static ssize_t ceph_aio_write(struct kiocb *iocb, const struct iovec *iov, | ||
| 805 | unsigned long nr_segs, loff_t pos) | ||
| 806 | { | ||
| 807 | struct file *file = iocb->ki_filp; | ||
| 808 | struct inode *inode = file->f_dentry->d_inode; | ||
| 809 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 810 | struct ceph_osd_client *osdc = &ceph_client(inode->i_sb)->osdc; | ||
| 811 | loff_t endoff = pos + iov->iov_len; | ||
| 812 | int got = 0; | ||
| 813 | int ret, err; | ||
| 814 | |||
| 815 | if (ceph_snap(inode) != CEPH_NOSNAP) | ||
| 816 | return -EROFS; | ||
| 817 | |||
| 818 | retry_snap: | ||
| 819 | if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) | ||
| 820 | return -ENOSPC; | ||
| 821 | __ceph_do_pending_vmtruncate(inode); | ||
| 822 | dout("aio_write %p %llx.%llx %llu~%u getting caps. i_size %llu\n", | ||
| 823 | inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len, | ||
| 824 | inode->i_size); | ||
| 825 | ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, CEPH_CAP_FILE_BUFFER, | ||
| 826 | &got, endoff); | ||
| 827 | if (ret < 0) | ||
| 828 | goto out; | ||
| 829 | |||
| 830 | dout("aio_write %p %llx.%llx %llu~%u got cap refs on %s\n", | ||
| 831 | inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len, | ||
| 832 | ceph_cap_string(got)); | ||
| 833 | |||
| 834 | if ((got & CEPH_CAP_FILE_BUFFER) == 0 || | ||
| 835 | (iocb->ki_filp->f_flags & O_DIRECT) || | ||
| 836 | (inode->i_sb->s_flags & MS_SYNCHRONOUS)) { | ||
| 837 | ret = ceph_sync_write(file, iov->iov_base, iov->iov_len, | ||
| 838 | &iocb->ki_pos); | ||
| 839 | } else { | ||
| 840 | ret = generic_file_aio_write(iocb, iov, nr_segs, pos); | ||
| 841 | |||
| 842 | if ((ret >= 0 || ret == -EIOCBQUEUED) && | ||
| 843 | ((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host) | ||
| 844 | || ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL))) { | ||
| 845 | err = vfs_fsync_range(file, file->f_path.dentry, | ||
| 846 | pos, pos + ret - 1, 1); | ||
| 847 | if (err < 0) | ||
| 848 | ret = err; | ||
| 849 | } | ||
| 850 | } | ||
| 851 | if (ret >= 0) { | ||
| 852 | spin_lock(&inode->i_lock); | ||
| 853 | __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR); | ||
| 854 | spin_unlock(&inode->i_lock); | ||
| 855 | } | ||
| 856 | |||
| 857 | out: | ||
| 858 | dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n", | ||
| 859 | inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len, | ||
| 860 | ceph_cap_string(got)); | ||
| 861 | ceph_put_cap_refs(ci, got); | ||
| 862 | |||
| 863 | if (ret == -EOLDSNAPC) { | ||
| 864 | dout("aio_write %p %llx.%llx %llu~%u got EOLDSNAPC, retrying\n", | ||
| 865 | inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len); | ||
| 866 | goto retry_snap; | ||
| 867 | } | ||
| 868 | |||
| 869 | return ret; | ||
| 870 | } | ||
| 871 | |||
| 872 | /* | ||
| 873 | * llseek. be sure to verify file size on SEEK_END. | ||
| 874 | */ | ||
| 875 | static loff_t ceph_llseek(struct file *file, loff_t offset, int origin) | ||
| 876 | { | ||
| 877 | struct inode *inode = file->f_mapping->host; | ||
| 878 | int ret; | ||
| 879 | |||
| 880 | mutex_lock(&inode->i_mutex); | ||
| 881 | __ceph_do_pending_vmtruncate(inode); | ||
| 882 | switch (origin) { | ||
| 883 | case SEEK_END: | ||
| 884 | ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE); | ||
| 885 | if (ret < 0) { | ||
| 886 | offset = ret; | ||
| 887 | goto out; | ||
| 888 | } | ||
| 889 | offset += inode->i_size; | ||
| 890 | break; | ||
| 891 | case SEEK_CUR: | ||
| 892 | /* | ||
| 893 | * Here we special-case the lseek(fd, 0, SEEK_CUR) | ||
| 894 | * position-querying operation. Avoid rewriting the "same" | ||
| 895 | * f_pos value back to the file because a concurrent read(), | ||
| 896 | * write() or lseek() might have altered it | ||
| 897 | */ | ||
| 898 | if (offset == 0) { | ||
| 899 | offset = file->f_pos; | ||
| 900 | goto out; | ||
| 901 | } | ||
| 902 | offset += file->f_pos; | ||
| 903 | break; | ||
| 904 | } | ||
| 905 | |||
| 906 | if (offset < 0 || offset > inode->i_sb->s_maxbytes) { | ||
| 907 | offset = -EINVAL; | ||
| 908 | goto out; | ||
| 909 | } | ||
| 910 | |||
| 911 | /* Special lock needed here? */ | ||
| 912 | if (offset != file->f_pos) { | ||
| 913 | file->f_pos = offset; | ||
| 914 | file->f_version = 0; | ||
| 915 | } | ||
| 916 | |||
| 917 | out: | ||
| 918 | mutex_unlock(&inode->i_mutex); | ||
| 919 | return offset; | ||
| 920 | } | ||
| 921 | |||
| 922 | const struct file_operations ceph_file_fops = { | ||
| 923 | .open = ceph_open, | ||
| 924 | .release = ceph_release, | ||
| 925 | .llseek = ceph_llseek, | ||
| 926 | .read = do_sync_read, | ||
| 927 | .write = do_sync_write, | ||
| 928 | .aio_read = ceph_aio_read, | ||
| 929 | .aio_write = ceph_aio_write, | ||
| 930 | .mmap = ceph_mmap, | ||
| 931 | .fsync = ceph_fsync, | ||
| 932 | .splice_read = generic_file_splice_read, | ||
| 933 | .splice_write = generic_file_splice_write, | ||
| 934 | .unlocked_ioctl = ceph_ioctl, | ||
| 935 | .compat_ioctl = ceph_ioctl, | ||
| 936 | }; | ||
| 937 | |||
diff --git a/fs/ceph/inode.c b/fs/ceph/inode.c new file mode 100644 index 000000000000..aca82d55cc53 --- /dev/null +++ b/fs/ceph/inode.c | |||
| @@ -0,0 +1,1766 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/module.h> | ||
| 4 | #include <linux/fs.h> | ||
| 5 | #include <linux/smp_lock.h> | ||
| 6 | #include <linux/slab.h> | ||
| 7 | #include <linux/string.h> | ||
| 8 | #include <linux/uaccess.h> | ||
| 9 | #include <linux/kernel.h> | ||
| 10 | #include <linux/namei.h> | ||
| 11 | #include <linux/writeback.h> | ||
| 12 | #include <linux/vmalloc.h> | ||
| 13 | #include <linux/pagevec.h> | ||
| 14 | |||
| 15 | #include "super.h" | ||
| 16 | #include "decode.h" | ||
| 17 | |||
| 18 | /* | ||
| 19 | * Ceph inode operations | ||
| 20 | * | ||
| 21 | * Implement basic inode helpers (get, alloc) and inode ops (getattr, | ||
| 22 | * setattr, etc.), xattr helpers, and helpers for assimilating | ||
| 23 | * metadata returned by the MDS into our cache. | ||
| 24 | * | ||
| 25 | * Also define helpers for doing asynchronous writeback, invalidation, | ||
| 26 | * and truncation for the benefit of those who can't afford to block | ||
| 27 | * (typically because they are in the message handler path). | ||
| 28 | */ | ||
| 29 | |||
| 30 | static const struct inode_operations ceph_symlink_iops; | ||
| 31 | |||
| 32 | static void ceph_invalidate_work(struct work_struct *work); | ||
| 33 | static void ceph_writeback_work(struct work_struct *work); | ||
| 34 | static void ceph_vmtruncate_work(struct work_struct *work); | ||
| 35 | |||
| 36 | /* | ||
| 37 | * find or create an inode, given the ceph ino number | ||
| 38 | */ | ||
| 39 | struct inode *ceph_get_inode(struct super_block *sb, struct ceph_vino vino) | ||
| 40 | { | ||
| 41 | struct inode *inode; | ||
| 42 | ino_t t = ceph_vino_to_ino(vino); | ||
| 43 | |||
| 44 | inode = iget5_locked(sb, t, ceph_ino_compare, ceph_set_ino_cb, &vino); | ||
| 45 | if (inode == NULL) | ||
| 46 | return ERR_PTR(-ENOMEM); | ||
| 47 | if (inode->i_state & I_NEW) { | ||
| 48 | dout("get_inode created new inode %p %llx.%llx ino %llx\n", | ||
| 49 | inode, ceph_vinop(inode), (u64)inode->i_ino); | ||
| 50 | unlock_new_inode(inode); | ||
| 51 | } | ||
| 52 | |||
| 53 | dout("get_inode on %lu=%llx.%llx got %p\n", inode->i_ino, vino.ino, | ||
| 54 | vino.snap, inode); | ||
| 55 | return inode; | ||
| 56 | } | ||
| 57 | |||
| 58 | /* | ||
| 59 | * get/constuct snapdir inode for a given directory | ||
| 60 | */ | ||
| 61 | struct inode *ceph_get_snapdir(struct inode *parent) | ||
| 62 | { | ||
| 63 | struct ceph_vino vino = { | ||
| 64 | .ino = ceph_ino(parent), | ||
| 65 | .snap = CEPH_SNAPDIR, | ||
| 66 | }; | ||
| 67 | struct inode *inode = ceph_get_inode(parent->i_sb, vino); | ||
| 68 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 69 | |||
| 70 | BUG_ON(!S_ISDIR(parent->i_mode)); | ||
| 71 | if (IS_ERR(inode)) | ||
| 72 | return ERR_PTR(PTR_ERR(inode)); | ||
| 73 | inode->i_mode = parent->i_mode; | ||
| 74 | inode->i_uid = parent->i_uid; | ||
| 75 | inode->i_gid = parent->i_gid; | ||
| 76 | inode->i_op = &ceph_dir_iops; | ||
| 77 | inode->i_fop = &ceph_dir_fops; | ||
| 78 | ci->i_snap_caps = CEPH_CAP_PIN; /* so we can open */ | ||
| 79 | ci->i_rbytes = 0; | ||
| 80 | return inode; | ||
| 81 | } | ||
| 82 | |||
| 83 | const struct inode_operations ceph_file_iops = { | ||
| 84 | .permission = ceph_permission, | ||
| 85 | .setattr = ceph_setattr, | ||
| 86 | .getattr = ceph_getattr, | ||
| 87 | .setxattr = ceph_setxattr, | ||
| 88 | .getxattr = ceph_getxattr, | ||
| 89 | .listxattr = ceph_listxattr, | ||
| 90 | .removexattr = ceph_removexattr, | ||
| 91 | }; | ||
| 92 | |||
| 93 | |||
| 94 | /* | ||
| 95 | * We use a 'frag tree' to keep track of the MDS's directory fragments | ||
| 96 | * for a given inode (usually there is just a single fragment). We | ||
| 97 | * need to know when a child frag is delegated to a new MDS, or when | ||
| 98 | * it is flagged as replicated, so we can direct our requests | ||
| 99 | * accordingly. | ||
| 100 | */ | ||
| 101 | |||
| 102 | /* | ||
| 103 | * find/create a frag in the tree | ||
| 104 | */ | ||
| 105 | static struct ceph_inode_frag *__get_or_create_frag(struct ceph_inode_info *ci, | ||
| 106 | u32 f) | ||
| 107 | { | ||
| 108 | struct rb_node **p; | ||
| 109 | struct rb_node *parent = NULL; | ||
| 110 | struct ceph_inode_frag *frag; | ||
| 111 | int c; | ||
| 112 | |||
| 113 | p = &ci->i_fragtree.rb_node; | ||
| 114 | while (*p) { | ||
| 115 | parent = *p; | ||
| 116 | frag = rb_entry(parent, struct ceph_inode_frag, node); | ||
| 117 | c = ceph_frag_compare(f, frag->frag); | ||
| 118 | if (c < 0) | ||
| 119 | p = &(*p)->rb_left; | ||
| 120 | else if (c > 0) | ||
| 121 | p = &(*p)->rb_right; | ||
| 122 | else | ||
| 123 | return frag; | ||
| 124 | } | ||
| 125 | |||
| 126 | frag = kmalloc(sizeof(*frag), GFP_NOFS); | ||
| 127 | if (!frag) { | ||
| 128 | pr_err("__get_or_create_frag ENOMEM on %p %llx.%llx " | ||
| 129 | "frag %x\n", &ci->vfs_inode, | ||
| 130 | ceph_vinop(&ci->vfs_inode), f); | ||
| 131 | return ERR_PTR(-ENOMEM); | ||
| 132 | } | ||
| 133 | frag->frag = f; | ||
| 134 | frag->split_by = 0; | ||
| 135 | frag->mds = -1; | ||
| 136 | frag->ndist = 0; | ||
| 137 | |||
| 138 | rb_link_node(&frag->node, parent, p); | ||
| 139 | rb_insert_color(&frag->node, &ci->i_fragtree); | ||
| 140 | |||
| 141 | dout("get_or_create_frag added %llx.%llx frag %x\n", | ||
| 142 | ceph_vinop(&ci->vfs_inode), f); | ||
| 143 | return frag; | ||
| 144 | } | ||
| 145 | |||
| 146 | /* | ||
| 147 | * find a specific frag @f | ||
| 148 | */ | ||
| 149 | struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, u32 f) | ||
| 150 | { | ||
| 151 | struct rb_node *n = ci->i_fragtree.rb_node; | ||
| 152 | |||
| 153 | while (n) { | ||
| 154 | struct ceph_inode_frag *frag = | ||
| 155 | rb_entry(n, struct ceph_inode_frag, node); | ||
| 156 | int c = ceph_frag_compare(f, frag->frag); | ||
| 157 | if (c < 0) | ||
| 158 | n = n->rb_left; | ||
| 159 | else if (c > 0) | ||
| 160 | n = n->rb_right; | ||
| 161 | else | ||
| 162 | return frag; | ||
| 163 | } | ||
| 164 | return NULL; | ||
| 165 | } | ||
| 166 | |||
| 167 | /* | ||
| 168 | * Choose frag containing the given value @v. If @pfrag is | ||
| 169 | * specified, copy the frag delegation info to the caller if | ||
| 170 | * it is present. | ||
| 171 | */ | ||
| 172 | u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v, | ||
| 173 | struct ceph_inode_frag *pfrag, | ||
| 174 | int *found) | ||
| 175 | { | ||
| 176 | u32 t = ceph_frag_make(0, 0); | ||
| 177 | struct ceph_inode_frag *frag; | ||
| 178 | unsigned nway, i; | ||
| 179 | u32 n; | ||
| 180 | |||
| 181 | if (found) | ||
| 182 | *found = 0; | ||
| 183 | |||
| 184 | mutex_lock(&ci->i_fragtree_mutex); | ||
| 185 | while (1) { | ||
| 186 | WARN_ON(!ceph_frag_contains_value(t, v)); | ||
| 187 | frag = __ceph_find_frag(ci, t); | ||
| 188 | if (!frag) | ||
| 189 | break; /* t is a leaf */ | ||
| 190 | if (frag->split_by == 0) { | ||
| 191 | if (pfrag) | ||
| 192 | memcpy(pfrag, frag, sizeof(*pfrag)); | ||
| 193 | if (found) | ||
| 194 | *found = 1; | ||
| 195 | break; | ||
| 196 | } | ||
| 197 | |||
| 198 | /* choose child */ | ||
| 199 | nway = 1 << frag->split_by; | ||
| 200 | dout("choose_frag(%x) %x splits by %d (%d ways)\n", v, t, | ||
| 201 | frag->split_by, nway); | ||
| 202 | for (i = 0; i < nway; i++) { | ||
| 203 | n = ceph_frag_make_child(t, frag->split_by, i); | ||
| 204 | if (ceph_frag_contains_value(n, v)) { | ||
| 205 | t = n; | ||
| 206 | break; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | BUG_ON(i == nway); | ||
| 210 | } | ||
| 211 | dout("choose_frag(%x) = %x\n", v, t); | ||
| 212 | |||
| 213 | mutex_unlock(&ci->i_fragtree_mutex); | ||
| 214 | return t; | ||
| 215 | } | ||
| 216 | |||
| 217 | /* | ||
| 218 | * Process dirfrag (delegation) info from the mds. Include leaf | ||
| 219 | * fragment in tree ONLY if ndist > 0. Otherwise, only | ||
| 220 | * branches/splits are included in i_fragtree) | ||
| 221 | */ | ||
| 222 | static int ceph_fill_dirfrag(struct inode *inode, | ||
| 223 | struct ceph_mds_reply_dirfrag *dirinfo) | ||
| 224 | { | ||
| 225 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 226 | struct ceph_inode_frag *frag; | ||
| 227 | u32 id = le32_to_cpu(dirinfo->frag); | ||
| 228 | int mds = le32_to_cpu(dirinfo->auth); | ||
| 229 | int ndist = le32_to_cpu(dirinfo->ndist); | ||
| 230 | int i; | ||
| 231 | int err = 0; | ||
| 232 | |||
| 233 | mutex_lock(&ci->i_fragtree_mutex); | ||
| 234 | if (ndist == 0) { | ||
| 235 | /* no delegation info needed. */ | ||
| 236 | frag = __ceph_find_frag(ci, id); | ||
| 237 | if (!frag) | ||
| 238 | goto out; | ||
| 239 | if (frag->split_by == 0) { | ||
| 240 | /* tree leaf, remove */ | ||
| 241 | dout("fill_dirfrag removed %llx.%llx frag %x" | ||
| 242 | " (no ref)\n", ceph_vinop(inode), id); | ||
| 243 | rb_erase(&frag->node, &ci->i_fragtree); | ||
| 244 | kfree(frag); | ||
| 245 | } else { | ||
| 246 | /* tree branch, keep and clear */ | ||
| 247 | dout("fill_dirfrag cleared %llx.%llx frag %x" | ||
| 248 | " referral\n", ceph_vinop(inode), id); | ||
| 249 | frag->mds = -1; | ||
| 250 | frag->ndist = 0; | ||
| 251 | } | ||
| 252 | goto out; | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | /* find/add this frag to store mds delegation info */ | ||
| 257 | frag = __get_or_create_frag(ci, id); | ||
| 258 | if (IS_ERR(frag)) { | ||
| 259 | /* this is not the end of the world; we can continue | ||
| 260 | with bad/inaccurate delegation info */ | ||
| 261 | pr_err("fill_dirfrag ENOMEM on mds ref %llx.%llx fg %x\n", | ||
| 262 | ceph_vinop(inode), le32_to_cpu(dirinfo->frag)); | ||
| 263 | err = -ENOMEM; | ||
| 264 | goto out; | ||
| 265 | } | ||
| 266 | |||
| 267 | frag->mds = mds; | ||
| 268 | frag->ndist = min_t(u32, ndist, CEPH_MAX_DIRFRAG_REP); | ||
| 269 | for (i = 0; i < frag->ndist; i++) | ||
| 270 | frag->dist[i] = le32_to_cpu(dirinfo->dist[i]); | ||
| 271 | dout("fill_dirfrag %llx.%llx frag %x ndist=%d\n", | ||
| 272 | ceph_vinop(inode), frag->frag, frag->ndist); | ||
| 273 | |||
| 274 | out: | ||
| 275 | mutex_unlock(&ci->i_fragtree_mutex); | ||
| 276 | return err; | ||
| 277 | } | ||
| 278 | |||
| 279 | |||
| 280 | /* | ||
| 281 | * initialize a newly allocated inode. | ||
| 282 | */ | ||
| 283 | struct inode *ceph_alloc_inode(struct super_block *sb) | ||
| 284 | { | ||
| 285 | struct ceph_inode_info *ci; | ||
| 286 | int i; | ||
| 287 | |||
| 288 | ci = kmem_cache_alloc(ceph_inode_cachep, GFP_NOFS); | ||
| 289 | if (!ci) | ||
| 290 | return NULL; | ||
| 291 | |||
| 292 | dout("alloc_inode %p\n", &ci->vfs_inode); | ||
| 293 | |||
| 294 | ci->i_version = 0; | ||
| 295 | ci->i_time_warp_seq = 0; | ||
| 296 | ci->i_ceph_flags = 0; | ||
| 297 | ci->i_release_count = 0; | ||
| 298 | ci->i_symlink = NULL; | ||
| 299 | |||
| 300 | ci->i_fragtree = RB_ROOT; | ||
| 301 | mutex_init(&ci->i_fragtree_mutex); | ||
| 302 | |||
| 303 | ci->i_xattrs.blob = NULL; | ||
| 304 | ci->i_xattrs.prealloc_blob = NULL; | ||
| 305 | ci->i_xattrs.dirty = false; | ||
| 306 | ci->i_xattrs.index = RB_ROOT; | ||
| 307 | ci->i_xattrs.count = 0; | ||
| 308 | ci->i_xattrs.names_size = 0; | ||
| 309 | ci->i_xattrs.vals_size = 0; | ||
| 310 | ci->i_xattrs.version = 0; | ||
| 311 | ci->i_xattrs.index_version = 0; | ||
| 312 | |||
| 313 | ci->i_caps = RB_ROOT; | ||
| 314 | ci->i_auth_cap = NULL; | ||
| 315 | ci->i_dirty_caps = 0; | ||
| 316 | ci->i_flushing_caps = 0; | ||
| 317 | INIT_LIST_HEAD(&ci->i_dirty_item); | ||
| 318 | INIT_LIST_HEAD(&ci->i_flushing_item); | ||
| 319 | ci->i_cap_flush_seq = 0; | ||
| 320 | ci->i_cap_flush_last_tid = 0; | ||
| 321 | memset(&ci->i_cap_flush_tid, 0, sizeof(ci->i_cap_flush_tid)); | ||
| 322 | init_waitqueue_head(&ci->i_cap_wq); | ||
| 323 | ci->i_hold_caps_min = 0; | ||
| 324 | ci->i_hold_caps_max = 0; | ||
| 325 | INIT_LIST_HEAD(&ci->i_cap_delay_list); | ||
| 326 | ci->i_cap_exporting_mds = 0; | ||
| 327 | ci->i_cap_exporting_mseq = 0; | ||
| 328 | ci->i_cap_exporting_issued = 0; | ||
| 329 | INIT_LIST_HEAD(&ci->i_cap_snaps); | ||
| 330 | ci->i_head_snapc = NULL; | ||
| 331 | ci->i_snap_caps = 0; | ||
| 332 | |||
| 333 | for (i = 0; i < CEPH_FILE_MODE_NUM; i++) | ||
| 334 | ci->i_nr_by_mode[i] = 0; | ||
| 335 | |||
| 336 | ci->i_truncate_seq = 0; | ||
| 337 | ci->i_truncate_size = 0; | ||
| 338 | ci->i_truncate_pending = 0; | ||
| 339 | |||
| 340 | ci->i_max_size = 0; | ||
| 341 | ci->i_reported_size = 0; | ||
| 342 | ci->i_wanted_max_size = 0; | ||
| 343 | ci->i_requested_max_size = 0; | ||
| 344 | |||
| 345 | ci->i_pin_ref = 0; | ||
| 346 | ci->i_rd_ref = 0; | ||
| 347 | ci->i_rdcache_ref = 0; | ||
| 348 | ci->i_wr_ref = 0; | ||
| 349 | ci->i_wrbuffer_ref = 0; | ||
| 350 | ci->i_wrbuffer_ref_head = 0; | ||
| 351 | ci->i_shared_gen = 0; | ||
| 352 | ci->i_rdcache_gen = 0; | ||
| 353 | ci->i_rdcache_revoking = 0; | ||
| 354 | |||
| 355 | INIT_LIST_HEAD(&ci->i_unsafe_writes); | ||
| 356 | INIT_LIST_HEAD(&ci->i_unsafe_dirops); | ||
| 357 | spin_lock_init(&ci->i_unsafe_lock); | ||
| 358 | |||
| 359 | ci->i_snap_realm = NULL; | ||
| 360 | INIT_LIST_HEAD(&ci->i_snap_realm_item); | ||
| 361 | INIT_LIST_HEAD(&ci->i_snap_flush_item); | ||
| 362 | |||
| 363 | INIT_WORK(&ci->i_wb_work, ceph_writeback_work); | ||
| 364 | INIT_WORK(&ci->i_pg_inv_work, ceph_invalidate_work); | ||
| 365 | |||
| 366 | INIT_WORK(&ci->i_vmtruncate_work, ceph_vmtruncate_work); | ||
| 367 | |||
| 368 | return &ci->vfs_inode; | ||
| 369 | } | ||
| 370 | |||
| 371 | void ceph_destroy_inode(struct inode *inode) | ||
| 372 | { | ||
| 373 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 374 | struct ceph_inode_frag *frag; | ||
| 375 | struct rb_node *n; | ||
| 376 | |||
| 377 | dout("destroy_inode %p ino %llx.%llx\n", inode, ceph_vinop(inode)); | ||
| 378 | |||
| 379 | ceph_queue_caps_release(inode); | ||
| 380 | |||
| 381 | /* | ||
| 382 | * we may still have a snap_realm reference if there are stray | ||
| 383 | * caps in i_cap_exporting_issued or i_snap_caps. | ||
| 384 | */ | ||
| 385 | if (ci->i_snap_realm) { | ||
| 386 | struct ceph_mds_client *mdsc = | ||
| 387 | &ceph_client(ci->vfs_inode.i_sb)->mdsc; | ||
| 388 | struct ceph_snap_realm *realm = ci->i_snap_realm; | ||
| 389 | |||
| 390 | dout(" dropping residual ref to snap realm %p\n", realm); | ||
| 391 | spin_lock(&realm->inodes_with_caps_lock); | ||
| 392 | list_del_init(&ci->i_snap_realm_item); | ||
| 393 | spin_unlock(&realm->inodes_with_caps_lock); | ||
| 394 | ceph_put_snap_realm(mdsc, realm); | ||
| 395 | } | ||
| 396 | |||
| 397 | kfree(ci->i_symlink); | ||
| 398 | while ((n = rb_first(&ci->i_fragtree)) != NULL) { | ||
| 399 | frag = rb_entry(n, struct ceph_inode_frag, node); | ||
| 400 | rb_erase(n, &ci->i_fragtree); | ||
| 401 | kfree(frag); | ||
| 402 | } | ||
| 403 | |||
| 404 | __ceph_destroy_xattrs(ci); | ||
| 405 | if (ci->i_xattrs.blob) | ||
| 406 | ceph_buffer_put(ci->i_xattrs.blob); | ||
| 407 | if (ci->i_xattrs.prealloc_blob) | ||
| 408 | ceph_buffer_put(ci->i_xattrs.prealloc_blob); | ||
| 409 | |||
| 410 | kmem_cache_free(ceph_inode_cachep, ci); | ||
| 411 | } | ||
| 412 | |||
| 413 | |||
| 414 | /* | ||
| 415 | * Helpers to fill in size, ctime, mtime, and atime. We have to be | ||
| 416 | * careful because either the client or MDS may have more up to date | ||
| 417 | * info, depending on which capabilities are held, and whether | ||
| 418 | * time_warp_seq or truncate_seq have increased. (Ordinarily, mtime | ||
| 419 | * and size are monotonically increasing, except when utimes() or | ||
| 420 | * truncate() increments the corresponding _seq values.) | ||
| 421 | */ | ||
| 422 | int ceph_fill_file_size(struct inode *inode, int issued, | ||
| 423 | u32 truncate_seq, u64 truncate_size, u64 size) | ||
| 424 | { | ||
| 425 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 426 | int queue_trunc = 0; | ||
| 427 | |||
| 428 | if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) > 0 || | ||
| 429 | (truncate_seq == ci->i_truncate_seq && size > inode->i_size)) { | ||
| 430 | dout("size %lld -> %llu\n", inode->i_size, size); | ||
| 431 | inode->i_size = size; | ||
| 432 | inode->i_blocks = (size + (1<<9) - 1) >> 9; | ||
| 433 | ci->i_reported_size = size; | ||
| 434 | if (truncate_seq != ci->i_truncate_seq) { | ||
| 435 | dout("truncate_seq %u -> %u\n", | ||
| 436 | ci->i_truncate_seq, truncate_seq); | ||
| 437 | ci->i_truncate_seq = truncate_seq; | ||
| 438 | /* | ||
| 439 | * If we hold relevant caps, or in the case where we're | ||
| 440 | * not the only client referencing this file and we | ||
| 441 | * don't hold those caps, then we need to check whether | ||
| 442 | * the file is either opened or mmaped | ||
| 443 | */ | ||
| 444 | if ((issued & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_RD| | ||
| 445 | CEPH_CAP_FILE_WR|CEPH_CAP_FILE_BUFFER| | ||
| 446 | CEPH_CAP_FILE_EXCL)) || | ||
| 447 | mapping_mapped(inode->i_mapping) || | ||
| 448 | __ceph_caps_file_wanted(ci)) { | ||
| 449 | ci->i_truncate_pending++; | ||
| 450 | queue_trunc = 1; | ||
| 451 | } | ||
| 452 | } | ||
| 453 | } | ||
| 454 | if (ceph_seq_cmp(truncate_seq, ci->i_truncate_seq) >= 0 && | ||
| 455 | ci->i_truncate_size != truncate_size) { | ||
| 456 | dout("truncate_size %lld -> %llu\n", ci->i_truncate_size, | ||
| 457 | truncate_size); | ||
| 458 | ci->i_truncate_size = truncate_size; | ||
| 459 | } | ||
| 460 | return queue_trunc; | ||
| 461 | } | ||
| 462 | |||
| 463 | void ceph_fill_file_time(struct inode *inode, int issued, | ||
| 464 | u64 time_warp_seq, struct timespec *ctime, | ||
| 465 | struct timespec *mtime, struct timespec *atime) | ||
| 466 | { | ||
| 467 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 468 | int warn = 0; | ||
| 469 | |||
| 470 | if (issued & (CEPH_CAP_FILE_EXCL| | ||
| 471 | CEPH_CAP_FILE_WR| | ||
| 472 | CEPH_CAP_FILE_BUFFER)) { | ||
| 473 | if (timespec_compare(ctime, &inode->i_ctime) > 0) { | ||
| 474 | dout("ctime %ld.%09ld -> %ld.%09ld inc w/ cap\n", | ||
| 475 | inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec, | ||
| 476 | ctime->tv_sec, ctime->tv_nsec); | ||
| 477 | inode->i_ctime = *ctime; | ||
| 478 | } | ||
| 479 | if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) > 0) { | ||
| 480 | /* the MDS did a utimes() */ | ||
| 481 | dout("mtime %ld.%09ld -> %ld.%09ld " | ||
| 482 | "tw %d -> %d\n", | ||
| 483 | inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec, | ||
| 484 | mtime->tv_sec, mtime->tv_nsec, | ||
| 485 | ci->i_time_warp_seq, (int)time_warp_seq); | ||
| 486 | |||
| 487 | inode->i_mtime = *mtime; | ||
| 488 | inode->i_atime = *atime; | ||
| 489 | ci->i_time_warp_seq = time_warp_seq; | ||
| 490 | } else if (time_warp_seq == ci->i_time_warp_seq) { | ||
| 491 | /* nobody did utimes(); take the max */ | ||
| 492 | if (timespec_compare(mtime, &inode->i_mtime) > 0) { | ||
| 493 | dout("mtime %ld.%09ld -> %ld.%09ld inc\n", | ||
| 494 | inode->i_mtime.tv_sec, | ||
| 495 | inode->i_mtime.tv_nsec, | ||
| 496 | mtime->tv_sec, mtime->tv_nsec); | ||
| 497 | inode->i_mtime = *mtime; | ||
| 498 | } | ||
| 499 | if (timespec_compare(atime, &inode->i_atime) > 0) { | ||
| 500 | dout("atime %ld.%09ld -> %ld.%09ld inc\n", | ||
| 501 | inode->i_atime.tv_sec, | ||
| 502 | inode->i_atime.tv_nsec, | ||
| 503 | atime->tv_sec, atime->tv_nsec); | ||
| 504 | inode->i_atime = *atime; | ||
| 505 | } | ||
| 506 | } else if (issued & CEPH_CAP_FILE_EXCL) { | ||
| 507 | /* we did a utimes(); ignore mds values */ | ||
| 508 | } else { | ||
| 509 | warn = 1; | ||
| 510 | } | ||
| 511 | } else { | ||
| 512 | /* we have no write caps; whatever the MDS says is true */ | ||
| 513 | if (ceph_seq_cmp(time_warp_seq, ci->i_time_warp_seq) >= 0) { | ||
| 514 | inode->i_ctime = *ctime; | ||
| 515 | inode->i_mtime = *mtime; | ||
| 516 | inode->i_atime = *atime; | ||
| 517 | ci->i_time_warp_seq = time_warp_seq; | ||
| 518 | } else { | ||
| 519 | warn = 1; | ||
| 520 | } | ||
| 521 | } | ||
| 522 | if (warn) /* time_warp_seq shouldn't go backwards */ | ||
| 523 | dout("%p mds time_warp_seq %llu < %u\n", | ||
| 524 | inode, time_warp_seq, ci->i_time_warp_seq); | ||
| 525 | } | ||
| 526 | |||
| 527 | /* | ||
| 528 | * Populate an inode based on info from mds. May be called on new or | ||
| 529 | * existing inodes. | ||
| 530 | */ | ||
| 531 | static int fill_inode(struct inode *inode, | ||
| 532 | struct ceph_mds_reply_info_in *iinfo, | ||
| 533 | struct ceph_mds_reply_dirfrag *dirinfo, | ||
| 534 | struct ceph_mds_session *session, | ||
| 535 | unsigned long ttl_from, int cap_fmode, | ||
| 536 | struct ceph_cap_reservation *caps_reservation) | ||
| 537 | { | ||
| 538 | struct ceph_mds_reply_inode *info = iinfo->in; | ||
| 539 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 540 | int i; | ||
| 541 | int issued, implemented; | ||
| 542 | struct timespec mtime, atime, ctime; | ||
| 543 | u32 nsplits; | ||
| 544 | struct ceph_buffer *xattr_blob = NULL; | ||
| 545 | int err = 0; | ||
| 546 | int queue_trunc = 0; | ||
| 547 | |||
| 548 | dout("fill_inode %p ino %llx.%llx v %llu had %llu\n", | ||
| 549 | inode, ceph_vinop(inode), le64_to_cpu(info->version), | ||
| 550 | ci->i_version); | ||
| 551 | |||
| 552 | /* | ||
| 553 | * prealloc xattr data, if it looks like we'll need it. only | ||
| 554 | * if len > 4 (meaning there are actually xattrs; the first 4 | ||
| 555 | * bytes are the xattr count). | ||
| 556 | */ | ||
| 557 | if (iinfo->xattr_len > 4) { | ||
| 558 | xattr_blob = ceph_buffer_new(iinfo->xattr_len, GFP_NOFS); | ||
| 559 | if (!xattr_blob) | ||
| 560 | pr_err("fill_inode ENOMEM xattr blob %d bytes\n", | ||
| 561 | iinfo->xattr_len); | ||
| 562 | } | ||
| 563 | |||
| 564 | spin_lock(&inode->i_lock); | ||
| 565 | |||
| 566 | /* | ||
| 567 | * provided version will be odd if inode value is projected, | ||
| 568 | * even if stable. skip the update if we have a newer info | ||
| 569 | * (e.g., due to inode info racing form multiple MDSs), or if | ||
| 570 | * we are getting projected (unstable) inode info. | ||
| 571 | */ | ||
| 572 | if (le64_to_cpu(info->version) > 0 && | ||
| 573 | (ci->i_version & ~1) > le64_to_cpu(info->version)) | ||
| 574 | goto no_change; | ||
| 575 | |||
| 576 | issued = __ceph_caps_issued(ci, &implemented); | ||
| 577 | issued |= implemented | __ceph_caps_dirty(ci); | ||
| 578 | |||
| 579 | /* update inode */ | ||
| 580 | ci->i_version = le64_to_cpu(info->version); | ||
| 581 | inode->i_version++; | ||
| 582 | inode->i_rdev = le32_to_cpu(info->rdev); | ||
| 583 | |||
| 584 | if ((issued & CEPH_CAP_AUTH_EXCL) == 0) { | ||
| 585 | inode->i_mode = le32_to_cpu(info->mode); | ||
| 586 | inode->i_uid = le32_to_cpu(info->uid); | ||
| 587 | inode->i_gid = le32_to_cpu(info->gid); | ||
| 588 | dout("%p mode 0%o uid.gid %d.%d\n", inode, inode->i_mode, | ||
| 589 | inode->i_uid, inode->i_gid); | ||
| 590 | } | ||
| 591 | |||
| 592 | if ((issued & CEPH_CAP_LINK_EXCL) == 0) | ||
| 593 | inode->i_nlink = le32_to_cpu(info->nlink); | ||
| 594 | |||
| 595 | /* be careful with mtime, atime, size */ | ||
| 596 | ceph_decode_timespec(&atime, &info->atime); | ||
| 597 | ceph_decode_timespec(&mtime, &info->mtime); | ||
| 598 | ceph_decode_timespec(&ctime, &info->ctime); | ||
| 599 | queue_trunc = ceph_fill_file_size(inode, issued, | ||
| 600 | le32_to_cpu(info->truncate_seq), | ||
| 601 | le64_to_cpu(info->truncate_size), | ||
| 602 | le64_to_cpu(info->size)); | ||
| 603 | ceph_fill_file_time(inode, issued, | ||
| 604 | le32_to_cpu(info->time_warp_seq), | ||
| 605 | &ctime, &mtime, &atime); | ||
| 606 | |||
| 607 | ci->i_max_size = le64_to_cpu(info->max_size); | ||
| 608 | ci->i_layout = info->layout; | ||
| 609 | inode->i_blkbits = fls(le32_to_cpu(info->layout.fl_stripe_unit)) - 1; | ||
| 610 | |||
| 611 | /* xattrs */ | ||
| 612 | /* note that if i_xattrs.len <= 4, i_xattrs.data will still be NULL. */ | ||
| 613 | if ((issued & CEPH_CAP_XATTR_EXCL) == 0 && | ||
| 614 | le64_to_cpu(info->xattr_version) > ci->i_xattrs.version) { | ||
| 615 | if (ci->i_xattrs.blob) | ||
| 616 | ceph_buffer_put(ci->i_xattrs.blob); | ||
| 617 | ci->i_xattrs.blob = xattr_blob; | ||
| 618 | if (xattr_blob) | ||
| 619 | memcpy(ci->i_xattrs.blob->vec.iov_base, | ||
| 620 | iinfo->xattr_data, iinfo->xattr_len); | ||
| 621 | ci->i_xattrs.version = le64_to_cpu(info->xattr_version); | ||
| 622 | } | ||
| 623 | |||
| 624 | inode->i_mapping->a_ops = &ceph_aops; | ||
| 625 | inode->i_mapping->backing_dev_info = | ||
| 626 | &ceph_client(inode->i_sb)->backing_dev_info; | ||
| 627 | |||
| 628 | switch (inode->i_mode & S_IFMT) { | ||
| 629 | case S_IFIFO: | ||
| 630 | case S_IFBLK: | ||
| 631 | case S_IFCHR: | ||
| 632 | case S_IFSOCK: | ||
| 633 | init_special_inode(inode, inode->i_mode, inode->i_rdev); | ||
| 634 | inode->i_op = &ceph_file_iops; | ||
| 635 | break; | ||
| 636 | case S_IFREG: | ||
| 637 | inode->i_op = &ceph_file_iops; | ||
| 638 | inode->i_fop = &ceph_file_fops; | ||
| 639 | break; | ||
| 640 | case S_IFLNK: | ||
| 641 | inode->i_op = &ceph_symlink_iops; | ||
| 642 | if (!ci->i_symlink) { | ||
| 643 | int symlen = iinfo->symlink_len; | ||
| 644 | char *sym; | ||
| 645 | |||
| 646 | BUG_ON(symlen != inode->i_size); | ||
| 647 | spin_unlock(&inode->i_lock); | ||
| 648 | |||
| 649 | err = -ENOMEM; | ||
| 650 | sym = kmalloc(symlen+1, GFP_NOFS); | ||
| 651 | if (!sym) | ||
| 652 | goto out; | ||
| 653 | memcpy(sym, iinfo->symlink, symlen); | ||
| 654 | sym[symlen] = 0; | ||
| 655 | |||
| 656 | spin_lock(&inode->i_lock); | ||
| 657 | if (!ci->i_symlink) | ||
| 658 | ci->i_symlink = sym; | ||
| 659 | else | ||
| 660 | kfree(sym); /* lost a race */ | ||
| 661 | } | ||
| 662 | break; | ||
| 663 | case S_IFDIR: | ||
| 664 | inode->i_op = &ceph_dir_iops; | ||
| 665 | inode->i_fop = &ceph_dir_fops; | ||
| 666 | |||
| 667 | ci->i_files = le64_to_cpu(info->files); | ||
| 668 | ci->i_subdirs = le64_to_cpu(info->subdirs); | ||
| 669 | ci->i_rbytes = le64_to_cpu(info->rbytes); | ||
| 670 | ci->i_rfiles = le64_to_cpu(info->rfiles); | ||
| 671 | ci->i_rsubdirs = le64_to_cpu(info->rsubdirs); | ||
| 672 | ceph_decode_timespec(&ci->i_rctime, &info->rctime); | ||
| 673 | |||
| 674 | /* set dir completion flag? */ | ||
| 675 | if (ci->i_files == 0 && ci->i_subdirs == 0 && | ||
| 676 | ceph_snap(inode) == CEPH_NOSNAP && | ||
| 677 | (le32_to_cpu(info->cap.caps) & CEPH_CAP_FILE_SHARED)) { | ||
| 678 | dout(" marking %p complete (empty)\n", inode); | ||
| 679 | ci->i_ceph_flags |= CEPH_I_COMPLETE; | ||
| 680 | ci->i_max_offset = 2; | ||
| 681 | } | ||
| 682 | |||
| 683 | /* it may be better to set st_size in getattr instead? */ | ||
| 684 | if (ceph_test_opt(ceph_client(inode->i_sb), RBYTES)) | ||
| 685 | inode->i_size = ci->i_rbytes; | ||
| 686 | break; | ||
| 687 | default: | ||
| 688 | pr_err("fill_inode %llx.%llx BAD mode 0%o\n", | ||
| 689 | ceph_vinop(inode), inode->i_mode); | ||
| 690 | } | ||
| 691 | |||
| 692 | no_change: | ||
| 693 | spin_unlock(&inode->i_lock); | ||
| 694 | |||
| 695 | /* queue truncate if we saw i_size decrease */ | ||
| 696 | if (queue_trunc) | ||
| 697 | ceph_queue_vmtruncate(inode); | ||
| 698 | |||
| 699 | /* populate frag tree */ | ||
| 700 | /* FIXME: move me up, if/when version reflects fragtree changes */ | ||
| 701 | nsplits = le32_to_cpu(info->fragtree.nsplits); | ||
| 702 | mutex_lock(&ci->i_fragtree_mutex); | ||
| 703 | for (i = 0; i < nsplits; i++) { | ||
| 704 | u32 id = le32_to_cpu(info->fragtree.splits[i].frag); | ||
| 705 | struct ceph_inode_frag *frag = __get_or_create_frag(ci, id); | ||
| 706 | |||
| 707 | if (IS_ERR(frag)) | ||
| 708 | continue; | ||
| 709 | frag->split_by = le32_to_cpu(info->fragtree.splits[i].by); | ||
| 710 | dout(" frag %x split by %d\n", frag->frag, frag->split_by); | ||
| 711 | } | ||
| 712 | mutex_unlock(&ci->i_fragtree_mutex); | ||
| 713 | |||
| 714 | /* were we issued a capability? */ | ||
| 715 | if (info->cap.caps) { | ||
| 716 | if (ceph_snap(inode) == CEPH_NOSNAP) { | ||
| 717 | ceph_add_cap(inode, session, | ||
| 718 | le64_to_cpu(info->cap.cap_id), | ||
| 719 | cap_fmode, | ||
| 720 | le32_to_cpu(info->cap.caps), | ||
| 721 | le32_to_cpu(info->cap.wanted), | ||
| 722 | le32_to_cpu(info->cap.seq), | ||
| 723 | le32_to_cpu(info->cap.mseq), | ||
| 724 | le64_to_cpu(info->cap.realm), | ||
| 725 | info->cap.flags, | ||
| 726 | caps_reservation); | ||
| 727 | } else { | ||
| 728 | spin_lock(&inode->i_lock); | ||
| 729 | dout(" %p got snap_caps %s\n", inode, | ||
| 730 | ceph_cap_string(le32_to_cpu(info->cap.caps))); | ||
| 731 | ci->i_snap_caps |= le32_to_cpu(info->cap.caps); | ||
| 732 | if (cap_fmode >= 0) | ||
| 733 | __ceph_get_fmode(ci, cap_fmode); | ||
| 734 | spin_unlock(&inode->i_lock); | ||
| 735 | } | ||
| 736 | } | ||
| 737 | |||
| 738 | /* update delegation info? */ | ||
| 739 | if (dirinfo) | ||
| 740 | ceph_fill_dirfrag(inode, dirinfo); | ||
| 741 | |||
| 742 | err = 0; | ||
| 743 | |||
| 744 | out: | ||
| 745 | if (xattr_blob) | ||
| 746 | ceph_buffer_put(xattr_blob); | ||
| 747 | return err; | ||
| 748 | } | ||
| 749 | |||
| 750 | /* | ||
| 751 | * caller should hold session s_mutex. | ||
| 752 | */ | ||
| 753 | static void update_dentry_lease(struct dentry *dentry, | ||
| 754 | struct ceph_mds_reply_lease *lease, | ||
| 755 | struct ceph_mds_session *session, | ||
| 756 | unsigned long from_time) | ||
| 757 | { | ||
| 758 | struct ceph_dentry_info *di = ceph_dentry(dentry); | ||
| 759 | long unsigned duration = le32_to_cpu(lease->duration_ms); | ||
| 760 | long unsigned ttl = from_time + (duration * HZ) / 1000; | ||
| 761 | long unsigned half_ttl = from_time + (duration * HZ / 2) / 1000; | ||
| 762 | struct inode *dir; | ||
| 763 | |||
| 764 | /* only track leases on regular dentries */ | ||
| 765 | if (dentry->d_op != &ceph_dentry_ops) | ||
| 766 | return; | ||
| 767 | |||
| 768 | spin_lock(&dentry->d_lock); | ||
| 769 | dout("update_dentry_lease %p mask %d duration %lu ms ttl %lu\n", | ||
| 770 | dentry, le16_to_cpu(lease->mask), duration, ttl); | ||
| 771 | |||
| 772 | /* make lease_rdcache_gen match directory */ | ||
| 773 | dir = dentry->d_parent->d_inode; | ||
| 774 | di->lease_shared_gen = ceph_inode(dir)->i_shared_gen; | ||
| 775 | |||
| 776 | if (lease->mask == 0) | ||
| 777 | goto out_unlock; | ||
| 778 | |||
| 779 | if (di->lease_gen == session->s_cap_gen && | ||
| 780 | time_before(ttl, dentry->d_time)) | ||
| 781 | goto out_unlock; /* we already have a newer lease. */ | ||
| 782 | |||
| 783 | if (di->lease_session && di->lease_session != session) | ||
| 784 | goto out_unlock; | ||
| 785 | |||
| 786 | ceph_dentry_lru_touch(dentry); | ||
| 787 | |||
| 788 | if (!di->lease_session) | ||
| 789 | di->lease_session = ceph_get_mds_session(session); | ||
| 790 | di->lease_gen = session->s_cap_gen; | ||
| 791 | di->lease_seq = le32_to_cpu(lease->seq); | ||
| 792 | di->lease_renew_after = half_ttl; | ||
| 793 | di->lease_renew_from = 0; | ||
| 794 | dentry->d_time = ttl; | ||
| 795 | out_unlock: | ||
| 796 | spin_unlock(&dentry->d_lock); | ||
| 797 | return; | ||
| 798 | } | ||
| 799 | |||
| 800 | /* | ||
| 801 | * splice a dentry to an inode. | ||
| 802 | * caller must hold directory i_mutex for this to be safe. | ||
| 803 | * | ||
| 804 | * we will only rehash the resulting dentry if @prehash is | ||
| 805 | * true; @prehash will be set to false (for the benefit of | ||
| 806 | * the caller) if we fail. | ||
| 807 | */ | ||
| 808 | static struct dentry *splice_dentry(struct dentry *dn, struct inode *in, | ||
| 809 | bool *prehash) | ||
| 810 | { | ||
| 811 | struct dentry *realdn; | ||
| 812 | |||
| 813 | /* dn must be unhashed */ | ||
| 814 | if (!d_unhashed(dn)) | ||
| 815 | d_drop(dn); | ||
| 816 | realdn = d_materialise_unique(dn, in); | ||
| 817 | if (IS_ERR(realdn)) { | ||
| 818 | pr_err("splice_dentry error %p inode %p ino %llx.%llx\n", | ||
| 819 | dn, in, ceph_vinop(in)); | ||
| 820 | if (prehash) | ||
| 821 | *prehash = false; /* don't rehash on error */ | ||
| 822 | dn = realdn; /* note realdn contains the error */ | ||
| 823 | goto out; | ||
| 824 | } else if (realdn) { | ||
| 825 | dout("dn %p (%d) spliced with %p (%d) " | ||
| 826 | "inode %p ino %llx.%llx\n", | ||
| 827 | dn, atomic_read(&dn->d_count), | ||
| 828 | realdn, atomic_read(&realdn->d_count), | ||
| 829 | realdn->d_inode, ceph_vinop(realdn->d_inode)); | ||
| 830 | dput(dn); | ||
| 831 | dn = realdn; | ||
| 832 | } else { | ||
| 833 | BUG_ON(!ceph_dentry(dn)); | ||
| 834 | |||
| 835 | dout("dn %p attached to %p ino %llx.%llx\n", | ||
| 836 | dn, dn->d_inode, ceph_vinop(dn->d_inode)); | ||
| 837 | } | ||
| 838 | if ((!prehash || *prehash) && d_unhashed(dn)) | ||
| 839 | d_rehash(dn); | ||
| 840 | out: | ||
| 841 | return dn; | ||
| 842 | } | ||
| 843 | |||
| 844 | /* | ||
| 845 | * Set dentry's directory position based on the current dir's max, and | ||
| 846 | * order it in d_subdirs, so that dcache_readdir behaves. | ||
| 847 | */ | ||
| 848 | static void ceph_set_dentry_offset(struct dentry *dn) | ||
| 849 | { | ||
| 850 | struct dentry *dir = dn->d_parent; | ||
| 851 | struct inode *inode = dn->d_parent->d_inode; | ||
| 852 | struct ceph_dentry_info *di; | ||
| 853 | |||
| 854 | BUG_ON(!inode); | ||
| 855 | |||
| 856 | di = ceph_dentry(dn); | ||
| 857 | |||
| 858 | spin_lock(&inode->i_lock); | ||
| 859 | di->offset = ceph_inode(inode)->i_max_offset++; | ||
| 860 | spin_unlock(&inode->i_lock); | ||
| 861 | |||
| 862 | spin_lock(&dcache_lock); | ||
| 863 | spin_lock(&dn->d_lock); | ||
| 864 | list_move_tail(&dir->d_subdirs, &dn->d_u.d_child); | ||
| 865 | dout("set_dentry_offset %p %lld (%p %p)\n", dn, di->offset, | ||
| 866 | dn->d_u.d_child.prev, dn->d_u.d_child.next); | ||
| 867 | spin_unlock(&dn->d_lock); | ||
| 868 | spin_unlock(&dcache_lock); | ||
| 869 | } | ||
| 870 | |||
| 871 | /* | ||
| 872 | * Incorporate results into the local cache. This is either just | ||
| 873 | * one inode, or a directory, dentry, and possibly linked-to inode (e.g., | ||
| 874 | * after a lookup). | ||
| 875 | * | ||
| 876 | * A reply may contain | ||
| 877 | * a directory inode along with a dentry. | ||
| 878 | * and/or a target inode | ||
| 879 | * | ||
| 880 | * Called with snap_rwsem (read). | ||
| 881 | */ | ||
| 882 | int ceph_fill_trace(struct super_block *sb, struct ceph_mds_request *req, | ||
| 883 | struct ceph_mds_session *session) | ||
| 884 | { | ||
| 885 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; | ||
| 886 | struct inode *in = NULL; | ||
| 887 | struct ceph_mds_reply_inode *ininfo; | ||
| 888 | struct ceph_vino vino; | ||
| 889 | int i = 0; | ||
| 890 | int err = 0; | ||
| 891 | |||
| 892 | dout("fill_trace %p is_dentry %d is_target %d\n", req, | ||
| 893 | rinfo->head->is_dentry, rinfo->head->is_target); | ||
| 894 | |||
| 895 | #if 0 | ||
| 896 | /* | ||
| 897 | * Debugging hook: | ||
| 898 | * | ||
| 899 | * If we resend completed ops to a recovering mds, we get no | ||
| 900 | * trace. Since that is very rare, pretend this is the case | ||
| 901 | * to ensure the 'no trace' handlers in the callers behave. | ||
| 902 | * | ||
| 903 | * Fill in inodes unconditionally to avoid breaking cap | ||
| 904 | * invariants. | ||
| 905 | */ | ||
| 906 | if (rinfo->head->op & CEPH_MDS_OP_WRITE) { | ||
| 907 | pr_info("fill_trace faking empty trace on %lld %s\n", | ||
| 908 | req->r_tid, ceph_mds_op_name(rinfo->head->op)); | ||
| 909 | if (rinfo->head->is_dentry) { | ||
| 910 | rinfo->head->is_dentry = 0; | ||
| 911 | err = fill_inode(req->r_locked_dir, | ||
| 912 | &rinfo->diri, rinfo->dirfrag, | ||
| 913 | session, req->r_request_started, -1); | ||
| 914 | } | ||
| 915 | if (rinfo->head->is_target) { | ||
| 916 | rinfo->head->is_target = 0; | ||
| 917 | ininfo = rinfo->targeti.in; | ||
| 918 | vino.ino = le64_to_cpu(ininfo->ino); | ||
| 919 | vino.snap = le64_to_cpu(ininfo->snapid); | ||
| 920 | in = ceph_get_inode(sb, vino); | ||
| 921 | err = fill_inode(in, &rinfo->targeti, NULL, | ||
| 922 | session, req->r_request_started, | ||
| 923 | req->r_fmode); | ||
| 924 | iput(in); | ||
| 925 | } | ||
| 926 | } | ||
| 927 | #endif | ||
| 928 | |||
| 929 | if (!rinfo->head->is_target && !rinfo->head->is_dentry) { | ||
| 930 | dout("fill_trace reply is empty!\n"); | ||
| 931 | if (rinfo->head->result == 0 && req->r_locked_dir) { | ||
| 932 | struct ceph_inode_info *ci = | ||
| 933 | ceph_inode(req->r_locked_dir); | ||
| 934 | dout(" clearing %p complete (empty trace)\n", | ||
| 935 | req->r_locked_dir); | ||
| 936 | ci->i_ceph_flags &= ~CEPH_I_COMPLETE; | ||
| 937 | ci->i_release_count++; | ||
| 938 | } | ||
| 939 | return 0; | ||
| 940 | } | ||
| 941 | |||
| 942 | if (rinfo->head->is_dentry) { | ||
| 943 | struct inode *dir = req->r_locked_dir; | ||
| 944 | |||
| 945 | err = fill_inode(dir, &rinfo->diri, rinfo->dirfrag, | ||
| 946 | session, req->r_request_started, -1, | ||
| 947 | &req->r_caps_reservation); | ||
| 948 | if (err < 0) | ||
| 949 | return err; | ||
| 950 | } | ||
| 951 | |||
| 952 | if (rinfo->head->is_dentry && !req->r_aborted) { | ||
| 953 | /* | ||
| 954 | * lookup link rename : null -> possibly existing inode | ||
| 955 | * mknod symlink mkdir : null -> new inode | ||
| 956 | * unlink : linked -> null | ||
| 957 | */ | ||
| 958 | struct inode *dir = req->r_locked_dir; | ||
| 959 | struct dentry *dn = req->r_dentry; | ||
| 960 | bool have_dir_cap, have_lease; | ||
| 961 | |||
| 962 | BUG_ON(!dn); | ||
| 963 | BUG_ON(!dir); | ||
| 964 | BUG_ON(dn->d_parent->d_inode != dir); | ||
| 965 | BUG_ON(ceph_ino(dir) != | ||
| 966 | le64_to_cpu(rinfo->diri.in->ino)); | ||
| 967 | BUG_ON(ceph_snap(dir) != | ||
| 968 | le64_to_cpu(rinfo->diri.in->snapid)); | ||
| 969 | |||
| 970 | /* do we have a lease on the whole dir? */ | ||
| 971 | have_dir_cap = | ||
| 972 | (le32_to_cpu(rinfo->diri.in->cap.caps) & | ||
| 973 | CEPH_CAP_FILE_SHARED); | ||
| 974 | |||
| 975 | /* do we have a dn lease? */ | ||
| 976 | have_lease = have_dir_cap || | ||
| 977 | (le16_to_cpu(rinfo->dlease->mask) & | ||
| 978 | CEPH_LOCK_DN); | ||
| 979 | |||
| 980 | if (!have_lease) | ||
| 981 | dout("fill_trace no dentry lease or dir cap\n"); | ||
| 982 | |||
| 983 | /* rename? */ | ||
| 984 | if (req->r_old_dentry && req->r_op == CEPH_MDS_OP_RENAME) { | ||
| 985 | dout(" src %p '%.*s' dst %p '%.*s'\n", | ||
| 986 | req->r_old_dentry, | ||
| 987 | req->r_old_dentry->d_name.len, | ||
| 988 | req->r_old_dentry->d_name.name, | ||
| 989 | dn, dn->d_name.len, dn->d_name.name); | ||
| 990 | dout("fill_trace doing d_move %p -> %p\n", | ||
| 991 | req->r_old_dentry, dn); | ||
| 992 | d_move(req->r_old_dentry, dn); | ||
| 993 | dout(" src %p '%.*s' dst %p '%.*s'\n", | ||
| 994 | req->r_old_dentry, | ||
| 995 | req->r_old_dentry->d_name.len, | ||
| 996 | req->r_old_dentry->d_name.name, | ||
| 997 | dn, dn->d_name.len, dn->d_name.name); | ||
| 998 | /* ensure target dentry is invalidated, despite | ||
| 999 | rehashing bug in vfs_rename_dir */ | ||
| 1000 | dn->d_time = jiffies; | ||
| 1001 | ceph_dentry(dn)->lease_shared_gen = 0; | ||
| 1002 | /* take overwritten dentry's readdir offset */ | ||
| 1003 | ceph_dentry(req->r_old_dentry)->offset = | ||
| 1004 | ceph_dentry(dn)->offset; | ||
| 1005 | dn = req->r_old_dentry; /* use old_dentry */ | ||
| 1006 | in = dn->d_inode; | ||
| 1007 | } | ||
| 1008 | |||
| 1009 | /* null dentry? */ | ||
| 1010 | if (!rinfo->head->is_target) { | ||
| 1011 | dout("fill_trace null dentry\n"); | ||
| 1012 | if (dn->d_inode) { | ||
| 1013 | dout("d_delete %p\n", dn); | ||
| 1014 | d_delete(dn); | ||
| 1015 | } else { | ||
| 1016 | dout("d_instantiate %p NULL\n", dn); | ||
| 1017 | d_instantiate(dn, NULL); | ||
| 1018 | if (have_lease && d_unhashed(dn)) | ||
| 1019 | d_rehash(dn); | ||
| 1020 | update_dentry_lease(dn, rinfo->dlease, | ||
| 1021 | session, | ||
| 1022 | req->r_request_started); | ||
| 1023 | } | ||
| 1024 | goto done; | ||
| 1025 | } | ||
| 1026 | |||
| 1027 | /* attach proper inode */ | ||
| 1028 | ininfo = rinfo->targeti.in; | ||
| 1029 | vino.ino = le64_to_cpu(ininfo->ino); | ||
| 1030 | vino.snap = le64_to_cpu(ininfo->snapid); | ||
| 1031 | if (!dn->d_inode) { | ||
| 1032 | in = ceph_get_inode(sb, vino); | ||
| 1033 | if (IS_ERR(in)) { | ||
| 1034 | pr_err("fill_trace bad get_inode " | ||
| 1035 | "%llx.%llx\n", vino.ino, vino.snap); | ||
| 1036 | err = PTR_ERR(in); | ||
| 1037 | d_delete(dn); | ||
| 1038 | goto done; | ||
| 1039 | } | ||
| 1040 | dn = splice_dentry(dn, in, &have_lease); | ||
| 1041 | if (IS_ERR(dn)) { | ||
| 1042 | err = PTR_ERR(dn); | ||
| 1043 | goto done; | ||
| 1044 | } | ||
| 1045 | req->r_dentry = dn; /* may have spliced */ | ||
| 1046 | ceph_set_dentry_offset(dn); | ||
| 1047 | igrab(in); | ||
| 1048 | } else if (ceph_ino(in) == vino.ino && | ||
| 1049 | ceph_snap(in) == vino.snap) { | ||
| 1050 | igrab(in); | ||
| 1051 | } else { | ||
| 1052 | dout(" %p links to %p %llx.%llx, not %llx.%llx\n", | ||
| 1053 | dn, in, ceph_ino(in), ceph_snap(in), | ||
| 1054 | vino.ino, vino.snap); | ||
| 1055 | have_lease = false; | ||
| 1056 | in = NULL; | ||
| 1057 | } | ||
| 1058 | |||
| 1059 | if (have_lease) | ||
| 1060 | update_dentry_lease(dn, rinfo->dlease, session, | ||
| 1061 | req->r_request_started); | ||
| 1062 | dout(" final dn %p\n", dn); | ||
| 1063 | i++; | ||
| 1064 | } else if (req->r_op == CEPH_MDS_OP_LOOKUPSNAP || | ||
| 1065 | req->r_op == CEPH_MDS_OP_MKSNAP) { | ||
| 1066 | struct dentry *dn = req->r_dentry; | ||
| 1067 | |||
| 1068 | /* fill out a snapdir LOOKUPSNAP dentry */ | ||
| 1069 | BUG_ON(!dn); | ||
| 1070 | BUG_ON(!req->r_locked_dir); | ||
| 1071 | BUG_ON(ceph_snap(req->r_locked_dir) != CEPH_SNAPDIR); | ||
| 1072 | ininfo = rinfo->targeti.in; | ||
| 1073 | vino.ino = le64_to_cpu(ininfo->ino); | ||
| 1074 | vino.snap = le64_to_cpu(ininfo->snapid); | ||
| 1075 | in = ceph_get_inode(sb, vino); | ||
| 1076 | if (IS_ERR(in)) { | ||
| 1077 | pr_err("fill_inode get_inode badness %llx.%llx\n", | ||
| 1078 | vino.ino, vino.snap); | ||
| 1079 | err = PTR_ERR(in); | ||
| 1080 | d_delete(dn); | ||
| 1081 | goto done; | ||
| 1082 | } | ||
| 1083 | dout(" linking snapped dir %p to dn %p\n", in, dn); | ||
| 1084 | dn = splice_dentry(dn, in, NULL); | ||
| 1085 | if (IS_ERR(dn)) { | ||
| 1086 | err = PTR_ERR(dn); | ||
| 1087 | goto done; | ||
| 1088 | } | ||
| 1089 | ceph_set_dentry_offset(dn); | ||
| 1090 | req->r_dentry = dn; /* may have spliced */ | ||
| 1091 | igrab(in); | ||
| 1092 | rinfo->head->is_dentry = 1; /* fool notrace handlers */ | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | if (rinfo->head->is_target) { | ||
| 1096 | vino.ino = le64_to_cpu(rinfo->targeti.in->ino); | ||
| 1097 | vino.snap = le64_to_cpu(rinfo->targeti.in->snapid); | ||
| 1098 | |||
| 1099 | if (in == NULL || ceph_ino(in) != vino.ino || | ||
| 1100 | ceph_snap(in) != vino.snap) { | ||
| 1101 | in = ceph_get_inode(sb, vino); | ||
| 1102 | if (IS_ERR(in)) { | ||
| 1103 | err = PTR_ERR(in); | ||
| 1104 | goto done; | ||
| 1105 | } | ||
| 1106 | } | ||
| 1107 | req->r_target_inode = in; | ||
| 1108 | |||
| 1109 | err = fill_inode(in, | ||
| 1110 | &rinfo->targeti, NULL, | ||
| 1111 | session, req->r_request_started, | ||
| 1112 | (le32_to_cpu(rinfo->head->result) == 0) ? | ||
| 1113 | req->r_fmode : -1, | ||
| 1114 | &req->r_caps_reservation); | ||
| 1115 | if (err < 0) { | ||
| 1116 | pr_err("fill_inode badness %p %llx.%llx\n", | ||
| 1117 | in, ceph_vinop(in)); | ||
| 1118 | goto done; | ||
| 1119 | } | ||
| 1120 | } | ||
| 1121 | |||
| 1122 | done: | ||
| 1123 | dout("fill_trace done err=%d\n", err); | ||
| 1124 | return err; | ||
| 1125 | } | ||
| 1126 | |||
| 1127 | /* | ||
| 1128 | * Prepopulate our cache with readdir results, leases, etc. | ||
| 1129 | */ | ||
| 1130 | int ceph_readdir_prepopulate(struct ceph_mds_request *req, | ||
| 1131 | struct ceph_mds_session *session) | ||
| 1132 | { | ||
| 1133 | struct dentry *parent = req->r_dentry; | ||
| 1134 | struct ceph_mds_reply_info_parsed *rinfo = &req->r_reply_info; | ||
| 1135 | struct qstr dname; | ||
| 1136 | struct dentry *dn; | ||
| 1137 | struct inode *in; | ||
| 1138 | int err = 0, i; | ||
| 1139 | struct inode *snapdir = NULL; | ||
| 1140 | struct ceph_mds_request_head *rhead = req->r_request->front.iov_base; | ||
| 1141 | u64 frag = le32_to_cpu(rhead->args.readdir.frag); | ||
| 1142 | struct ceph_dentry_info *di; | ||
| 1143 | |||
| 1144 | if (le32_to_cpu(rinfo->head->op) == CEPH_MDS_OP_LSSNAP) { | ||
| 1145 | snapdir = ceph_get_snapdir(parent->d_inode); | ||
| 1146 | parent = d_find_alias(snapdir); | ||
| 1147 | dout("readdir_prepopulate %d items under SNAPDIR dn %p\n", | ||
| 1148 | rinfo->dir_nr, parent); | ||
| 1149 | } else { | ||
| 1150 | dout("readdir_prepopulate %d items under dn %p\n", | ||
| 1151 | rinfo->dir_nr, parent); | ||
| 1152 | if (rinfo->dir_dir) | ||
| 1153 | ceph_fill_dirfrag(parent->d_inode, rinfo->dir_dir); | ||
| 1154 | } | ||
| 1155 | |||
| 1156 | for (i = 0; i < rinfo->dir_nr; i++) { | ||
| 1157 | struct ceph_vino vino; | ||
| 1158 | |||
| 1159 | dname.name = rinfo->dir_dname[i]; | ||
| 1160 | dname.len = rinfo->dir_dname_len[i]; | ||
| 1161 | dname.hash = full_name_hash(dname.name, dname.len); | ||
| 1162 | |||
| 1163 | vino.ino = le64_to_cpu(rinfo->dir_in[i].in->ino); | ||
| 1164 | vino.snap = le64_to_cpu(rinfo->dir_in[i].in->snapid); | ||
| 1165 | |||
| 1166 | retry_lookup: | ||
| 1167 | dn = d_lookup(parent, &dname); | ||
| 1168 | dout("d_lookup on parent=%p name=%.*s got %p\n", | ||
| 1169 | parent, dname.len, dname.name, dn); | ||
| 1170 | |||
| 1171 | if (!dn) { | ||
| 1172 | dn = d_alloc(parent, &dname); | ||
| 1173 | dout("d_alloc %p '%.*s' = %p\n", parent, | ||
| 1174 | dname.len, dname.name, dn); | ||
| 1175 | if (dn == NULL) { | ||
| 1176 | dout("d_alloc badness\n"); | ||
| 1177 | err = -ENOMEM; | ||
| 1178 | goto out; | ||
| 1179 | } | ||
| 1180 | err = ceph_init_dentry(dn); | ||
| 1181 | if (err < 0) | ||
| 1182 | goto out; | ||
| 1183 | } else if (dn->d_inode && | ||
| 1184 | (ceph_ino(dn->d_inode) != vino.ino || | ||
| 1185 | ceph_snap(dn->d_inode) != vino.snap)) { | ||
| 1186 | dout(" dn %p points to wrong inode %p\n", | ||
| 1187 | dn, dn->d_inode); | ||
| 1188 | d_delete(dn); | ||
| 1189 | dput(dn); | ||
| 1190 | goto retry_lookup; | ||
| 1191 | } else { | ||
| 1192 | /* reorder parent's d_subdirs */ | ||
| 1193 | spin_lock(&dcache_lock); | ||
| 1194 | spin_lock(&dn->d_lock); | ||
| 1195 | list_move(&dn->d_u.d_child, &parent->d_subdirs); | ||
| 1196 | spin_unlock(&dn->d_lock); | ||
| 1197 | spin_unlock(&dcache_lock); | ||
| 1198 | } | ||
| 1199 | |||
| 1200 | di = dn->d_fsdata; | ||
| 1201 | di->offset = ceph_make_fpos(frag, i + req->r_readdir_offset); | ||
| 1202 | |||
| 1203 | /* inode */ | ||
| 1204 | if (dn->d_inode) { | ||
| 1205 | in = dn->d_inode; | ||
| 1206 | } else { | ||
| 1207 | in = ceph_get_inode(parent->d_sb, vino); | ||
| 1208 | if (in == NULL) { | ||
| 1209 | dout("new_inode badness\n"); | ||
| 1210 | d_delete(dn); | ||
| 1211 | dput(dn); | ||
| 1212 | err = -ENOMEM; | ||
| 1213 | goto out; | ||
| 1214 | } | ||
| 1215 | dn = splice_dentry(dn, in, NULL); | ||
| 1216 | } | ||
| 1217 | |||
| 1218 | if (fill_inode(in, &rinfo->dir_in[i], NULL, session, | ||
| 1219 | req->r_request_started, -1, | ||
| 1220 | &req->r_caps_reservation) < 0) { | ||
| 1221 | pr_err("fill_inode badness on %p\n", in); | ||
| 1222 | dput(dn); | ||
| 1223 | continue; | ||
| 1224 | } | ||
| 1225 | update_dentry_lease(dn, rinfo->dir_dlease[i], | ||
| 1226 | req->r_session, req->r_request_started); | ||
| 1227 | dput(dn); | ||
| 1228 | } | ||
| 1229 | req->r_did_prepopulate = true; | ||
| 1230 | |||
| 1231 | out: | ||
| 1232 | if (snapdir) { | ||
| 1233 | iput(snapdir); | ||
| 1234 | dput(parent); | ||
| 1235 | } | ||
| 1236 | dout("readdir_prepopulate done\n"); | ||
| 1237 | return err; | ||
| 1238 | } | ||
| 1239 | |||
| 1240 | int ceph_inode_set_size(struct inode *inode, loff_t size) | ||
| 1241 | { | ||
| 1242 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 1243 | int ret = 0; | ||
| 1244 | |||
| 1245 | spin_lock(&inode->i_lock); | ||
| 1246 | dout("set_size %p %llu -> %llu\n", inode, inode->i_size, size); | ||
| 1247 | inode->i_size = size; | ||
| 1248 | inode->i_blocks = (size + (1 << 9) - 1) >> 9; | ||
| 1249 | |||
| 1250 | /* tell the MDS if we are approaching max_size */ | ||
| 1251 | if ((size << 1) >= ci->i_max_size && | ||
| 1252 | (ci->i_reported_size << 1) < ci->i_max_size) | ||
| 1253 | ret = 1; | ||
| 1254 | |||
| 1255 | spin_unlock(&inode->i_lock); | ||
| 1256 | return ret; | ||
| 1257 | } | ||
| 1258 | |||
| 1259 | /* | ||
| 1260 | * Write back inode data in a worker thread. (This can't be done | ||
| 1261 | * in the message handler context.) | ||
| 1262 | */ | ||
| 1263 | void ceph_queue_writeback(struct inode *inode) | ||
| 1264 | { | ||
| 1265 | if (queue_work(ceph_inode_to_client(inode)->wb_wq, | ||
| 1266 | &ceph_inode(inode)->i_wb_work)) { | ||
| 1267 | dout("ceph_queue_writeback %p\n", inode); | ||
| 1268 | igrab(inode); | ||
| 1269 | } else { | ||
| 1270 | dout("ceph_queue_writeback %p failed\n", inode); | ||
| 1271 | } | ||
| 1272 | } | ||
| 1273 | |||
| 1274 | static void ceph_writeback_work(struct work_struct *work) | ||
| 1275 | { | ||
| 1276 | struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info, | ||
| 1277 | i_wb_work); | ||
| 1278 | struct inode *inode = &ci->vfs_inode; | ||
| 1279 | |||
| 1280 | dout("writeback %p\n", inode); | ||
| 1281 | filemap_fdatawrite(&inode->i_data); | ||
| 1282 | iput(inode); | ||
| 1283 | } | ||
| 1284 | |||
| 1285 | /* | ||
| 1286 | * queue an async invalidation | ||
| 1287 | */ | ||
| 1288 | void ceph_queue_invalidate(struct inode *inode) | ||
| 1289 | { | ||
| 1290 | if (queue_work(ceph_inode_to_client(inode)->pg_inv_wq, | ||
| 1291 | &ceph_inode(inode)->i_pg_inv_work)) { | ||
| 1292 | dout("ceph_queue_invalidate %p\n", inode); | ||
| 1293 | igrab(inode); | ||
| 1294 | } else { | ||
| 1295 | dout("ceph_queue_invalidate %p failed\n", inode); | ||
| 1296 | } | ||
| 1297 | } | ||
| 1298 | |||
| 1299 | /* | ||
| 1300 | * invalidate any pages that are not dirty or under writeback. this | ||
| 1301 | * includes pages that are clean and mapped. | ||
| 1302 | */ | ||
| 1303 | static void ceph_invalidate_nondirty_pages(struct address_space *mapping) | ||
| 1304 | { | ||
| 1305 | struct pagevec pvec; | ||
| 1306 | pgoff_t next = 0; | ||
| 1307 | int i; | ||
| 1308 | |||
| 1309 | pagevec_init(&pvec, 0); | ||
| 1310 | while (pagevec_lookup(&pvec, mapping, next, PAGEVEC_SIZE)) { | ||
| 1311 | for (i = 0; i < pagevec_count(&pvec); i++) { | ||
| 1312 | struct page *page = pvec.pages[i]; | ||
| 1313 | pgoff_t index; | ||
| 1314 | int skip_page = | ||
| 1315 | (PageDirty(page) || PageWriteback(page)); | ||
| 1316 | |||
| 1317 | if (!skip_page) | ||
| 1318 | skip_page = !trylock_page(page); | ||
| 1319 | |||
| 1320 | /* | ||
| 1321 | * We really shouldn't be looking at the ->index of an | ||
| 1322 | * unlocked page. But we're not allowed to lock these | ||
| 1323 | * pages. So we rely upon nobody altering the ->index | ||
| 1324 | * of this (pinned-by-us) page. | ||
| 1325 | */ | ||
| 1326 | index = page->index; | ||
| 1327 | if (index > next) | ||
| 1328 | next = index; | ||
| 1329 | next++; | ||
| 1330 | |||
| 1331 | if (skip_page) | ||
| 1332 | continue; | ||
| 1333 | |||
| 1334 | generic_error_remove_page(mapping, page); | ||
| 1335 | unlock_page(page); | ||
| 1336 | } | ||
| 1337 | pagevec_release(&pvec); | ||
| 1338 | cond_resched(); | ||
| 1339 | } | ||
| 1340 | } | ||
| 1341 | |||
| 1342 | /* | ||
| 1343 | * Invalidate inode pages in a worker thread. (This can't be done | ||
| 1344 | * in the message handler context.) | ||
| 1345 | */ | ||
| 1346 | static void ceph_invalidate_work(struct work_struct *work) | ||
| 1347 | { | ||
| 1348 | struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info, | ||
| 1349 | i_pg_inv_work); | ||
| 1350 | struct inode *inode = &ci->vfs_inode; | ||
| 1351 | u32 orig_gen; | ||
| 1352 | int check = 0; | ||
| 1353 | |||
| 1354 | spin_lock(&inode->i_lock); | ||
| 1355 | dout("invalidate_pages %p gen %d revoking %d\n", inode, | ||
| 1356 | ci->i_rdcache_gen, ci->i_rdcache_revoking); | ||
| 1357 | if (ci->i_rdcache_gen == 0 || | ||
| 1358 | ci->i_rdcache_revoking != ci->i_rdcache_gen) { | ||
| 1359 | BUG_ON(ci->i_rdcache_revoking > ci->i_rdcache_gen); | ||
| 1360 | /* nevermind! */ | ||
| 1361 | ci->i_rdcache_revoking = 0; | ||
| 1362 | spin_unlock(&inode->i_lock); | ||
| 1363 | goto out; | ||
| 1364 | } | ||
| 1365 | orig_gen = ci->i_rdcache_gen; | ||
| 1366 | spin_unlock(&inode->i_lock); | ||
| 1367 | |||
| 1368 | ceph_invalidate_nondirty_pages(inode->i_mapping); | ||
| 1369 | |||
| 1370 | spin_lock(&inode->i_lock); | ||
| 1371 | if (orig_gen == ci->i_rdcache_gen) { | ||
| 1372 | dout("invalidate_pages %p gen %d successful\n", inode, | ||
| 1373 | ci->i_rdcache_gen); | ||
| 1374 | ci->i_rdcache_gen = 0; | ||
| 1375 | ci->i_rdcache_revoking = 0; | ||
| 1376 | check = 1; | ||
| 1377 | } else { | ||
| 1378 | dout("invalidate_pages %p gen %d raced, gen now %d\n", | ||
| 1379 | inode, orig_gen, ci->i_rdcache_gen); | ||
| 1380 | } | ||
| 1381 | spin_unlock(&inode->i_lock); | ||
| 1382 | |||
| 1383 | if (check) | ||
| 1384 | ceph_check_caps(ci, 0, NULL); | ||
| 1385 | out: | ||
| 1386 | iput(inode); | ||
| 1387 | } | ||
| 1388 | |||
| 1389 | |||
| 1390 | /* | ||
| 1391 | * called by trunc_wq; take i_mutex ourselves | ||
| 1392 | * | ||
| 1393 | * We also truncate in a separate thread as well. | ||
| 1394 | */ | ||
| 1395 | static void ceph_vmtruncate_work(struct work_struct *work) | ||
| 1396 | { | ||
| 1397 | struct ceph_inode_info *ci = container_of(work, struct ceph_inode_info, | ||
| 1398 | i_vmtruncate_work); | ||
| 1399 | struct inode *inode = &ci->vfs_inode; | ||
| 1400 | |||
| 1401 | dout("vmtruncate_work %p\n", inode); | ||
| 1402 | mutex_lock(&inode->i_mutex); | ||
| 1403 | __ceph_do_pending_vmtruncate(inode); | ||
| 1404 | mutex_unlock(&inode->i_mutex); | ||
| 1405 | iput(inode); | ||
| 1406 | } | ||
| 1407 | |||
| 1408 | /* | ||
| 1409 | * Queue an async vmtruncate. If we fail to queue work, we will handle | ||
| 1410 | * the truncation the next time we call __ceph_do_pending_vmtruncate. | ||
| 1411 | */ | ||
| 1412 | void ceph_queue_vmtruncate(struct inode *inode) | ||
| 1413 | { | ||
| 1414 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 1415 | |||
| 1416 | if (queue_work(ceph_client(inode->i_sb)->trunc_wq, | ||
| 1417 | &ci->i_vmtruncate_work)) { | ||
| 1418 | dout("ceph_queue_vmtruncate %p\n", inode); | ||
| 1419 | igrab(inode); | ||
| 1420 | } else { | ||
| 1421 | dout("ceph_queue_vmtruncate %p failed, pending=%d\n", | ||
| 1422 | inode, ci->i_truncate_pending); | ||
| 1423 | } | ||
| 1424 | } | ||
| 1425 | |||
| 1426 | /* | ||
| 1427 | * called with i_mutex held. | ||
| 1428 | * | ||
| 1429 | * Make sure any pending truncation is applied before doing anything | ||
| 1430 | * that may depend on it. | ||
| 1431 | */ | ||
| 1432 | void __ceph_do_pending_vmtruncate(struct inode *inode) | ||
| 1433 | { | ||
| 1434 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 1435 | u64 to; | ||
| 1436 | int wrbuffer_refs, wake = 0; | ||
| 1437 | |||
| 1438 | retry: | ||
| 1439 | spin_lock(&inode->i_lock); | ||
| 1440 | if (ci->i_truncate_pending == 0) { | ||
| 1441 | dout("__do_pending_vmtruncate %p none pending\n", inode); | ||
| 1442 | spin_unlock(&inode->i_lock); | ||
| 1443 | return; | ||
| 1444 | } | ||
| 1445 | |||
| 1446 | /* | ||
| 1447 | * make sure any dirty snapped pages are flushed before we | ||
| 1448 | * possibly truncate them.. so write AND block! | ||
| 1449 | */ | ||
| 1450 | if (ci->i_wrbuffer_ref_head < ci->i_wrbuffer_ref) { | ||
| 1451 | dout("__do_pending_vmtruncate %p flushing snaps first\n", | ||
| 1452 | inode); | ||
| 1453 | spin_unlock(&inode->i_lock); | ||
| 1454 | filemap_write_and_wait_range(&inode->i_data, 0, | ||
| 1455 | inode->i_sb->s_maxbytes); | ||
| 1456 | goto retry; | ||
| 1457 | } | ||
| 1458 | |||
| 1459 | to = ci->i_truncate_size; | ||
| 1460 | wrbuffer_refs = ci->i_wrbuffer_ref; | ||
| 1461 | dout("__do_pending_vmtruncate %p (%d) to %lld\n", inode, | ||
| 1462 | ci->i_truncate_pending, to); | ||
| 1463 | spin_unlock(&inode->i_lock); | ||
| 1464 | |||
| 1465 | truncate_inode_pages(inode->i_mapping, to); | ||
| 1466 | |||
| 1467 | spin_lock(&inode->i_lock); | ||
| 1468 | ci->i_truncate_pending--; | ||
| 1469 | if (ci->i_truncate_pending == 0) | ||
| 1470 | wake = 1; | ||
| 1471 | spin_unlock(&inode->i_lock); | ||
| 1472 | |||
| 1473 | if (wrbuffer_refs == 0) | ||
| 1474 | ceph_check_caps(ci, CHECK_CAPS_AUTHONLY, NULL); | ||
| 1475 | if (wake) | ||
| 1476 | wake_up(&ci->i_cap_wq); | ||
| 1477 | } | ||
| 1478 | |||
| 1479 | |||
| 1480 | /* | ||
| 1481 | * symlinks | ||
| 1482 | */ | ||
| 1483 | static void *ceph_sym_follow_link(struct dentry *dentry, struct nameidata *nd) | ||
| 1484 | { | ||
| 1485 | struct ceph_inode_info *ci = ceph_inode(dentry->d_inode); | ||
| 1486 | nd_set_link(nd, ci->i_symlink); | ||
| 1487 | return NULL; | ||
| 1488 | } | ||
| 1489 | |||
| 1490 | static const struct inode_operations ceph_symlink_iops = { | ||
| 1491 | .readlink = generic_readlink, | ||
| 1492 | .follow_link = ceph_sym_follow_link, | ||
| 1493 | }; | ||
| 1494 | |||
| 1495 | /* | ||
| 1496 | * setattr | ||
| 1497 | */ | ||
| 1498 | int ceph_setattr(struct dentry *dentry, struct iattr *attr) | ||
| 1499 | { | ||
| 1500 | struct inode *inode = dentry->d_inode; | ||
| 1501 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 1502 | struct inode *parent_inode = dentry->d_parent->d_inode; | ||
| 1503 | const unsigned int ia_valid = attr->ia_valid; | ||
| 1504 | struct ceph_mds_request *req; | ||
| 1505 | struct ceph_mds_client *mdsc = &ceph_client(dentry->d_sb)->mdsc; | ||
| 1506 | int issued; | ||
| 1507 | int release = 0, dirtied = 0; | ||
| 1508 | int mask = 0; | ||
| 1509 | int err = 0; | ||
| 1510 | |||
| 1511 | if (ceph_snap(inode) != CEPH_NOSNAP) | ||
| 1512 | return -EROFS; | ||
| 1513 | |||
| 1514 | __ceph_do_pending_vmtruncate(inode); | ||
| 1515 | |||
| 1516 | err = inode_change_ok(inode, attr); | ||
| 1517 | if (err != 0) | ||
| 1518 | return err; | ||
| 1519 | |||
| 1520 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETATTR, | ||
| 1521 | USE_AUTH_MDS); | ||
| 1522 | if (IS_ERR(req)) | ||
| 1523 | return PTR_ERR(req); | ||
| 1524 | |||
| 1525 | spin_lock(&inode->i_lock); | ||
| 1526 | issued = __ceph_caps_issued(ci, NULL); | ||
| 1527 | dout("setattr %p issued %s\n", inode, ceph_cap_string(issued)); | ||
| 1528 | |||
| 1529 | if (ia_valid & ATTR_UID) { | ||
| 1530 | dout("setattr %p uid %d -> %d\n", inode, | ||
| 1531 | inode->i_uid, attr->ia_uid); | ||
| 1532 | if (issued & CEPH_CAP_AUTH_EXCL) { | ||
| 1533 | inode->i_uid = attr->ia_uid; | ||
| 1534 | dirtied |= CEPH_CAP_AUTH_EXCL; | ||
| 1535 | } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || | ||
| 1536 | attr->ia_uid != inode->i_uid) { | ||
| 1537 | req->r_args.setattr.uid = cpu_to_le32(attr->ia_uid); | ||
| 1538 | mask |= CEPH_SETATTR_UID; | ||
| 1539 | release |= CEPH_CAP_AUTH_SHARED; | ||
| 1540 | } | ||
| 1541 | } | ||
| 1542 | if (ia_valid & ATTR_GID) { | ||
| 1543 | dout("setattr %p gid %d -> %d\n", inode, | ||
| 1544 | inode->i_gid, attr->ia_gid); | ||
| 1545 | if (issued & CEPH_CAP_AUTH_EXCL) { | ||
| 1546 | inode->i_gid = attr->ia_gid; | ||
| 1547 | dirtied |= CEPH_CAP_AUTH_EXCL; | ||
| 1548 | } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || | ||
| 1549 | attr->ia_gid != inode->i_gid) { | ||
| 1550 | req->r_args.setattr.gid = cpu_to_le32(attr->ia_gid); | ||
| 1551 | mask |= CEPH_SETATTR_GID; | ||
| 1552 | release |= CEPH_CAP_AUTH_SHARED; | ||
| 1553 | } | ||
| 1554 | } | ||
| 1555 | if (ia_valid & ATTR_MODE) { | ||
| 1556 | dout("setattr %p mode 0%o -> 0%o\n", inode, inode->i_mode, | ||
| 1557 | attr->ia_mode); | ||
| 1558 | if (issued & CEPH_CAP_AUTH_EXCL) { | ||
| 1559 | inode->i_mode = attr->ia_mode; | ||
| 1560 | dirtied |= CEPH_CAP_AUTH_EXCL; | ||
| 1561 | } else if ((issued & CEPH_CAP_AUTH_SHARED) == 0 || | ||
| 1562 | attr->ia_mode != inode->i_mode) { | ||
| 1563 | req->r_args.setattr.mode = cpu_to_le32(attr->ia_mode); | ||
| 1564 | mask |= CEPH_SETATTR_MODE; | ||
| 1565 | release |= CEPH_CAP_AUTH_SHARED; | ||
| 1566 | } | ||
| 1567 | } | ||
| 1568 | |||
| 1569 | if (ia_valid & ATTR_ATIME) { | ||
| 1570 | dout("setattr %p atime %ld.%ld -> %ld.%ld\n", inode, | ||
| 1571 | inode->i_atime.tv_sec, inode->i_atime.tv_nsec, | ||
| 1572 | attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec); | ||
| 1573 | if (issued & CEPH_CAP_FILE_EXCL) { | ||
| 1574 | ci->i_time_warp_seq++; | ||
| 1575 | inode->i_atime = attr->ia_atime; | ||
| 1576 | dirtied |= CEPH_CAP_FILE_EXCL; | ||
| 1577 | } else if ((issued & CEPH_CAP_FILE_WR) && | ||
| 1578 | timespec_compare(&inode->i_atime, | ||
| 1579 | &attr->ia_atime) < 0) { | ||
| 1580 | inode->i_atime = attr->ia_atime; | ||
| 1581 | dirtied |= CEPH_CAP_FILE_WR; | ||
| 1582 | } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || | ||
| 1583 | !timespec_equal(&inode->i_atime, &attr->ia_atime)) { | ||
| 1584 | ceph_encode_timespec(&req->r_args.setattr.atime, | ||
| 1585 | &attr->ia_atime); | ||
| 1586 | mask |= CEPH_SETATTR_ATIME; | ||
| 1587 | release |= CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_RD | | ||
| 1588 | CEPH_CAP_FILE_WR; | ||
| 1589 | } | ||
| 1590 | } | ||
| 1591 | if (ia_valid & ATTR_MTIME) { | ||
| 1592 | dout("setattr %p mtime %ld.%ld -> %ld.%ld\n", inode, | ||
| 1593 | inode->i_mtime.tv_sec, inode->i_mtime.tv_nsec, | ||
| 1594 | attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec); | ||
| 1595 | if (issued & CEPH_CAP_FILE_EXCL) { | ||
| 1596 | ci->i_time_warp_seq++; | ||
| 1597 | inode->i_mtime = attr->ia_mtime; | ||
| 1598 | dirtied |= CEPH_CAP_FILE_EXCL; | ||
| 1599 | } else if ((issued & CEPH_CAP_FILE_WR) && | ||
| 1600 | timespec_compare(&inode->i_mtime, | ||
| 1601 | &attr->ia_mtime) < 0) { | ||
| 1602 | inode->i_mtime = attr->ia_mtime; | ||
| 1603 | dirtied |= CEPH_CAP_FILE_WR; | ||
| 1604 | } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || | ||
| 1605 | !timespec_equal(&inode->i_mtime, &attr->ia_mtime)) { | ||
| 1606 | ceph_encode_timespec(&req->r_args.setattr.mtime, | ||
| 1607 | &attr->ia_mtime); | ||
| 1608 | mask |= CEPH_SETATTR_MTIME; | ||
| 1609 | release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD | | ||
| 1610 | CEPH_CAP_FILE_WR; | ||
| 1611 | } | ||
| 1612 | } | ||
| 1613 | if (ia_valid & ATTR_SIZE) { | ||
| 1614 | dout("setattr %p size %lld -> %lld\n", inode, | ||
| 1615 | inode->i_size, attr->ia_size); | ||
| 1616 | if (attr->ia_size > inode->i_sb->s_maxbytes) { | ||
| 1617 | err = -EINVAL; | ||
| 1618 | goto out; | ||
| 1619 | } | ||
| 1620 | if ((issued & CEPH_CAP_FILE_EXCL) && | ||
| 1621 | attr->ia_size > inode->i_size) { | ||
| 1622 | inode->i_size = attr->ia_size; | ||
| 1623 | inode->i_blocks = | ||
| 1624 | (attr->ia_size + (1 << 9) - 1) >> 9; | ||
| 1625 | inode->i_ctime = attr->ia_ctime; | ||
| 1626 | ci->i_reported_size = attr->ia_size; | ||
| 1627 | dirtied |= CEPH_CAP_FILE_EXCL; | ||
| 1628 | } else if ((issued & CEPH_CAP_FILE_SHARED) == 0 || | ||
| 1629 | attr->ia_size != inode->i_size) { | ||
| 1630 | req->r_args.setattr.size = cpu_to_le64(attr->ia_size); | ||
| 1631 | req->r_args.setattr.old_size = | ||
| 1632 | cpu_to_le64(inode->i_size); | ||
| 1633 | mask |= CEPH_SETATTR_SIZE; | ||
| 1634 | release |= CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_RD | | ||
| 1635 | CEPH_CAP_FILE_WR; | ||
| 1636 | } | ||
| 1637 | } | ||
| 1638 | |||
| 1639 | /* these do nothing */ | ||
| 1640 | if (ia_valid & ATTR_CTIME) { | ||
| 1641 | bool only = (ia_valid & (ATTR_SIZE|ATTR_MTIME|ATTR_ATIME| | ||
| 1642 | ATTR_MODE|ATTR_UID|ATTR_GID)) == 0; | ||
| 1643 | dout("setattr %p ctime %ld.%ld -> %ld.%ld (%s)\n", inode, | ||
| 1644 | inode->i_ctime.tv_sec, inode->i_ctime.tv_nsec, | ||
| 1645 | attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec, | ||
| 1646 | only ? "ctime only" : "ignored"); | ||
| 1647 | inode->i_ctime = attr->ia_ctime; | ||
| 1648 | if (only) { | ||
| 1649 | /* | ||
| 1650 | * if kernel wants to dirty ctime but nothing else, | ||
| 1651 | * we need to choose a cap to dirty under, or do | ||
| 1652 | * a almost-no-op setattr | ||
| 1653 | */ | ||
| 1654 | if (issued & CEPH_CAP_AUTH_EXCL) | ||
| 1655 | dirtied |= CEPH_CAP_AUTH_EXCL; | ||
| 1656 | else if (issued & CEPH_CAP_FILE_EXCL) | ||
| 1657 | dirtied |= CEPH_CAP_FILE_EXCL; | ||
| 1658 | else if (issued & CEPH_CAP_XATTR_EXCL) | ||
| 1659 | dirtied |= CEPH_CAP_XATTR_EXCL; | ||
| 1660 | else | ||
| 1661 | mask |= CEPH_SETATTR_CTIME; | ||
| 1662 | } | ||
| 1663 | } | ||
| 1664 | if (ia_valid & ATTR_FILE) | ||
| 1665 | dout("setattr %p ATTR_FILE ... hrm!\n", inode); | ||
| 1666 | |||
| 1667 | if (dirtied) { | ||
| 1668 | __ceph_mark_dirty_caps(ci, dirtied); | ||
| 1669 | inode->i_ctime = CURRENT_TIME; | ||
| 1670 | } | ||
| 1671 | |||
| 1672 | release &= issued; | ||
| 1673 | spin_unlock(&inode->i_lock); | ||
| 1674 | |||
| 1675 | if (mask) { | ||
| 1676 | req->r_inode = igrab(inode); | ||
| 1677 | req->r_inode_drop = release; | ||
| 1678 | req->r_args.setattr.mask = cpu_to_le32(mask); | ||
| 1679 | req->r_num_caps = 1; | ||
| 1680 | err = ceph_mdsc_do_request(mdsc, parent_inode, req); | ||
| 1681 | } | ||
| 1682 | dout("setattr %p result=%d (%s locally, %d remote)\n", inode, err, | ||
| 1683 | ceph_cap_string(dirtied), mask); | ||
| 1684 | |||
| 1685 | ceph_mdsc_put_request(req); | ||
| 1686 | __ceph_do_pending_vmtruncate(inode); | ||
| 1687 | return err; | ||
| 1688 | out: | ||
| 1689 | spin_unlock(&inode->i_lock); | ||
| 1690 | ceph_mdsc_put_request(req); | ||
| 1691 | return err; | ||
| 1692 | } | ||
| 1693 | |||
| 1694 | /* | ||
| 1695 | * Verify that we have a lease on the given mask. If not, | ||
| 1696 | * do a getattr against an mds. | ||
| 1697 | */ | ||
| 1698 | int ceph_do_getattr(struct inode *inode, int mask) | ||
| 1699 | { | ||
| 1700 | struct ceph_client *client = ceph_sb_to_client(inode->i_sb); | ||
| 1701 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 1702 | struct ceph_mds_request *req; | ||
| 1703 | int err; | ||
| 1704 | |||
| 1705 | if (ceph_snap(inode) == CEPH_SNAPDIR) { | ||
| 1706 | dout("do_getattr inode %p SNAPDIR\n", inode); | ||
| 1707 | return 0; | ||
| 1708 | } | ||
| 1709 | |||
| 1710 | dout("do_getattr inode %p mask %s\n", inode, ceph_cap_string(mask)); | ||
| 1711 | if (ceph_caps_issued_mask(ceph_inode(inode), mask, 1)) | ||
| 1712 | return 0; | ||
| 1713 | |||
| 1714 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS); | ||
| 1715 | if (IS_ERR(req)) | ||
| 1716 | return PTR_ERR(req); | ||
| 1717 | req->r_inode = igrab(inode); | ||
| 1718 | req->r_num_caps = 1; | ||
| 1719 | req->r_args.getattr.mask = cpu_to_le32(mask); | ||
| 1720 | err = ceph_mdsc_do_request(mdsc, NULL, req); | ||
| 1721 | ceph_mdsc_put_request(req); | ||
| 1722 | dout("do_getattr result=%d\n", err); | ||
| 1723 | return err; | ||
| 1724 | } | ||
| 1725 | |||
| 1726 | |||
| 1727 | /* | ||
| 1728 | * Check inode permissions. We verify we have a valid value for | ||
| 1729 | * the AUTH cap, then call the generic handler. | ||
| 1730 | */ | ||
| 1731 | int ceph_permission(struct inode *inode, int mask) | ||
| 1732 | { | ||
| 1733 | int err = ceph_do_getattr(inode, CEPH_CAP_AUTH_SHARED); | ||
| 1734 | |||
| 1735 | if (!err) | ||
| 1736 | err = generic_permission(inode, mask, NULL); | ||
| 1737 | return err; | ||
| 1738 | } | ||
| 1739 | |||
| 1740 | /* | ||
| 1741 | * Get all attributes. Hopefully somedata we'll have a statlite() | ||
| 1742 | * and can limit the fields we require to be accurate. | ||
| 1743 | */ | ||
| 1744 | int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry, | ||
| 1745 | struct kstat *stat) | ||
| 1746 | { | ||
| 1747 | struct inode *inode = dentry->d_inode; | ||
| 1748 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 1749 | int err; | ||
| 1750 | |||
| 1751 | err = ceph_do_getattr(inode, CEPH_STAT_CAP_INODE_ALL); | ||
| 1752 | if (!err) { | ||
| 1753 | generic_fillattr(inode, stat); | ||
| 1754 | stat->ino = inode->i_ino; | ||
| 1755 | if (ceph_snap(inode) != CEPH_NOSNAP) | ||
| 1756 | stat->dev = ceph_snap(inode); | ||
| 1757 | else | ||
| 1758 | stat->dev = 0; | ||
| 1759 | if (S_ISDIR(inode->i_mode)) { | ||
| 1760 | stat->size = ci->i_rbytes; | ||
| 1761 | stat->blocks = 0; | ||
| 1762 | stat->blksize = 65536; | ||
| 1763 | } | ||
| 1764 | } | ||
| 1765 | return err; | ||
| 1766 | } | ||
diff --git a/fs/ceph/ioctl.c b/fs/ceph/ioctl.c new file mode 100644 index 000000000000..8a5bcae62846 --- /dev/null +++ b/fs/ceph/ioctl.c | |||
| @@ -0,0 +1,160 @@ | |||
| 1 | #include <linux/in.h> | ||
| 2 | |||
| 3 | #include "ioctl.h" | ||
| 4 | #include "super.h" | ||
| 5 | #include "ceph_debug.h" | ||
| 6 | |||
| 7 | |||
| 8 | /* | ||
| 9 | * ioctls | ||
| 10 | */ | ||
| 11 | |||
| 12 | /* | ||
| 13 | * get and set the file layout | ||
| 14 | */ | ||
| 15 | static long ceph_ioctl_get_layout(struct file *file, void __user *arg) | ||
| 16 | { | ||
| 17 | struct ceph_inode_info *ci = ceph_inode(file->f_dentry->d_inode); | ||
| 18 | struct ceph_ioctl_layout l; | ||
| 19 | int err; | ||
| 20 | |||
| 21 | err = ceph_do_getattr(file->f_dentry->d_inode, CEPH_STAT_CAP_LAYOUT); | ||
| 22 | if (!err) { | ||
| 23 | l.stripe_unit = ceph_file_layout_su(ci->i_layout); | ||
| 24 | l.stripe_count = ceph_file_layout_stripe_count(ci->i_layout); | ||
| 25 | l.object_size = ceph_file_layout_object_size(ci->i_layout); | ||
| 26 | l.data_pool = le32_to_cpu(ci->i_layout.fl_pg_pool); | ||
| 27 | l.preferred_osd = | ||
| 28 | (s32)le32_to_cpu(ci->i_layout.fl_pg_preferred); | ||
| 29 | if (copy_to_user(arg, &l, sizeof(l))) | ||
| 30 | return -EFAULT; | ||
| 31 | } | ||
| 32 | |||
| 33 | return err; | ||
| 34 | } | ||
| 35 | |||
| 36 | static long ceph_ioctl_set_layout(struct file *file, void __user *arg) | ||
| 37 | { | ||
| 38 | struct inode *inode = file->f_dentry->d_inode; | ||
| 39 | struct inode *parent_inode = file->f_dentry->d_parent->d_inode; | ||
| 40 | struct ceph_mds_client *mdsc = &ceph_sb_to_client(inode->i_sb)->mdsc; | ||
| 41 | struct ceph_mds_request *req; | ||
| 42 | struct ceph_ioctl_layout l; | ||
| 43 | int err, i; | ||
| 44 | |||
| 45 | /* copy and validate */ | ||
| 46 | if (copy_from_user(&l, arg, sizeof(l))) | ||
| 47 | return -EFAULT; | ||
| 48 | |||
| 49 | if ((l.object_size & ~PAGE_MASK) || | ||
| 50 | (l.stripe_unit & ~PAGE_MASK) || | ||
| 51 | !l.stripe_unit || | ||
| 52 | (l.object_size && | ||
| 53 | (unsigned)l.object_size % (unsigned)l.stripe_unit)) | ||
| 54 | return -EINVAL; | ||
| 55 | |||
| 56 | /* make sure it's a valid data pool */ | ||
| 57 | if (l.data_pool > 0) { | ||
| 58 | mutex_lock(&mdsc->mutex); | ||
| 59 | err = -EINVAL; | ||
| 60 | for (i = 0; i < mdsc->mdsmap->m_num_data_pg_pools; i++) | ||
| 61 | if (mdsc->mdsmap->m_data_pg_pools[i] == l.data_pool) { | ||
| 62 | err = 0; | ||
| 63 | break; | ||
| 64 | } | ||
| 65 | mutex_unlock(&mdsc->mutex); | ||
| 66 | if (err) | ||
| 67 | return err; | ||
| 68 | } | ||
| 69 | |||
| 70 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETLAYOUT, | ||
| 71 | USE_AUTH_MDS); | ||
| 72 | if (IS_ERR(req)) | ||
| 73 | return PTR_ERR(req); | ||
| 74 | req->r_inode = igrab(inode); | ||
| 75 | req->r_inode_drop = CEPH_CAP_FILE_SHARED | CEPH_CAP_FILE_EXCL; | ||
| 76 | |||
| 77 | req->r_args.setlayout.layout.fl_stripe_unit = | ||
| 78 | cpu_to_le32(l.stripe_unit); | ||
| 79 | req->r_args.setlayout.layout.fl_stripe_count = | ||
| 80 | cpu_to_le32(l.stripe_count); | ||
| 81 | req->r_args.setlayout.layout.fl_object_size = | ||
| 82 | cpu_to_le32(l.object_size); | ||
| 83 | req->r_args.setlayout.layout.fl_pg_pool = cpu_to_le32(l.data_pool); | ||
| 84 | req->r_args.setlayout.layout.fl_pg_preferred = | ||
| 85 | cpu_to_le32(l.preferred_osd); | ||
| 86 | |||
| 87 | err = ceph_mdsc_do_request(mdsc, parent_inode, req); | ||
| 88 | ceph_mdsc_put_request(req); | ||
| 89 | return err; | ||
| 90 | } | ||
| 91 | |||
| 92 | /* | ||
| 93 | * Return object name, size/offset information, and location (OSD | ||
| 94 | * number, network address) for a given file offset. | ||
| 95 | */ | ||
| 96 | static long ceph_ioctl_get_dataloc(struct file *file, void __user *arg) | ||
| 97 | { | ||
| 98 | struct ceph_ioctl_dataloc dl; | ||
| 99 | struct inode *inode = file->f_dentry->d_inode; | ||
| 100 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 101 | struct ceph_osd_client *osdc = &ceph_client(inode->i_sb)->osdc; | ||
| 102 | u64 len = 1, olen; | ||
| 103 | u64 tmp; | ||
| 104 | struct ceph_object_layout ol; | ||
| 105 | struct ceph_pg pgid; | ||
| 106 | |||
| 107 | /* copy and validate */ | ||
| 108 | if (copy_from_user(&dl, arg, sizeof(dl))) | ||
| 109 | return -EFAULT; | ||
| 110 | |||
| 111 | down_read(&osdc->map_sem); | ||
| 112 | ceph_calc_file_object_mapping(&ci->i_layout, dl.file_offset, &len, | ||
| 113 | &dl.object_no, &dl.object_offset, &olen); | ||
| 114 | dl.file_offset -= dl.object_offset; | ||
| 115 | dl.object_size = ceph_file_layout_object_size(ci->i_layout); | ||
| 116 | dl.block_size = ceph_file_layout_su(ci->i_layout); | ||
| 117 | |||
| 118 | /* block_offset = object_offset % block_size */ | ||
| 119 | tmp = dl.object_offset; | ||
| 120 | dl.block_offset = do_div(tmp, dl.block_size); | ||
| 121 | |||
| 122 | snprintf(dl.object_name, sizeof(dl.object_name), "%llx.%08llx", | ||
| 123 | ceph_ino(inode), dl.object_no); | ||
| 124 | ceph_calc_object_layout(&ol, dl.object_name, &ci->i_layout, | ||
| 125 | osdc->osdmap); | ||
| 126 | |||
| 127 | pgid = ol.ol_pgid; | ||
| 128 | dl.osd = ceph_calc_pg_primary(osdc->osdmap, pgid); | ||
| 129 | if (dl.osd >= 0) { | ||
| 130 | struct ceph_entity_addr *a = | ||
| 131 | ceph_osd_addr(osdc->osdmap, dl.osd); | ||
| 132 | if (a) | ||
| 133 | memcpy(&dl.osd_addr, &a->in_addr, sizeof(dl.osd_addr)); | ||
| 134 | } else { | ||
| 135 | memset(&dl.osd_addr, 0, sizeof(dl.osd_addr)); | ||
| 136 | } | ||
| 137 | up_read(&osdc->map_sem); | ||
| 138 | |||
| 139 | /* send result back to user */ | ||
| 140 | if (copy_to_user(arg, &dl, sizeof(dl))) | ||
| 141 | return -EFAULT; | ||
| 142 | |||
| 143 | return 0; | ||
| 144 | } | ||
| 145 | |||
| 146 | long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg) | ||
| 147 | { | ||
| 148 | dout("ioctl file %p cmd %u arg %lu\n", file, cmd, arg); | ||
| 149 | switch (cmd) { | ||
| 150 | case CEPH_IOC_GET_LAYOUT: | ||
| 151 | return ceph_ioctl_get_layout(file, (void __user *)arg); | ||
| 152 | |||
| 153 | case CEPH_IOC_SET_LAYOUT: | ||
| 154 | return ceph_ioctl_set_layout(file, (void __user *)arg); | ||
| 155 | |||
| 156 | case CEPH_IOC_GET_DATALOC: | ||
| 157 | return ceph_ioctl_get_dataloc(file, (void __user *)arg); | ||
| 158 | } | ||
| 159 | return -ENOTTY; | ||
| 160 | } | ||
diff --git a/fs/ceph/ioctl.h b/fs/ceph/ioctl.h new file mode 100644 index 000000000000..25e4f1a9d059 --- /dev/null +++ b/fs/ceph/ioctl.h | |||
| @@ -0,0 +1,40 @@ | |||
| 1 | #ifndef FS_CEPH_IOCTL_H | ||
| 2 | #define FS_CEPH_IOCTL_H | ||
| 3 | |||
| 4 | #include <linux/ioctl.h> | ||
| 5 | #include <linux/types.h> | ||
| 6 | |||
| 7 | #define CEPH_IOCTL_MAGIC 0x97 | ||
| 8 | |||
| 9 | /* just use u64 to align sanely on all archs */ | ||
| 10 | struct ceph_ioctl_layout { | ||
| 11 | __u64 stripe_unit, stripe_count, object_size; | ||
| 12 | __u64 data_pool; | ||
| 13 | __s64 preferred_osd; | ||
| 14 | }; | ||
| 15 | |||
| 16 | #define CEPH_IOC_GET_LAYOUT _IOR(CEPH_IOCTL_MAGIC, 1, \ | ||
| 17 | struct ceph_ioctl_layout) | ||
| 18 | #define CEPH_IOC_SET_LAYOUT _IOW(CEPH_IOCTL_MAGIC, 2, \ | ||
| 19 | struct ceph_ioctl_layout) | ||
| 20 | |||
| 21 | /* | ||
| 22 | * Extract identity, address of the OSD and object storing a given | ||
| 23 | * file offset. | ||
| 24 | */ | ||
| 25 | struct ceph_ioctl_dataloc { | ||
| 26 | __u64 file_offset; /* in+out: file offset */ | ||
| 27 | __u64 object_offset; /* out: offset in object */ | ||
| 28 | __u64 object_no; /* out: object # */ | ||
| 29 | __u64 object_size; /* out: object size */ | ||
| 30 | char object_name[64]; /* out: object name */ | ||
| 31 | __u64 block_offset; /* out: offset in block */ | ||
| 32 | __u64 block_size; /* out: block length */ | ||
| 33 | __s64 osd; /* out: osd # */ | ||
| 34 | struct sockaddr_storage osd_addr; /* out: osd address */ | ||
| 35 | }; | ||
| 36 | |||
| 37 | #define CEPH_IOC_GET_DATALOC _IOWR(CEPH_IOCTL_MAGIC, 3, \ | ||
| 38 | struct ceph_ioctl_dataloc) | ||
| 39 | |||
| 40 | #endif | ||
diff --git a/fs/ceph/mds_client.c b/fs/ceph/mds_client.c new file mode 100644 index 000000000000..5c7920be6420 --- /dev/null +++ b/fs/ceph/mds_client.c | |||
| @@ -0,0 +1,3042 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/wait.h> | ||
| 4 | #include <linux/sched.h> | ||
| 5 | |||
| 6 | #include "mds_client.h" | ||
| 7 | #include "mon_client.h" | ||
| 8 | #include "super.h" | ||
| 9 | #include "messenger.h" | ||
| 10 | #include "decode.h" | ||
| 11 | #include "auth.h" | ||
| 12 | #include "pagelist.h" | ||
| 13 | |||
| 14 | /* | ||
| 15 | * A cluster of MDS (metadata server) daemons is responsible for | ||
| 16 | * managing the file system namespace (the directory hierarchy and | ||
| 17 | * inodes) and for coordinating shared access to storage. Metadata is | ||
| 18 | * partitioning hierarchically across a number of servers, and that | ||
| 19 | * partition varies over time as the cluster adjusts the distribution | ||
| 20 | * in order to balance load. | ||
| 21 | * | ||
| 22 | * The MDS client is primarily responsible to managing synchronous | ||
| 23 | * metadata requests for operations like open, unlink, and so forth. | ||
| 24 | * If there is a MDS failure, we find out about it when we (possibly | ||
| 25 | * request and) receive a new MDS map, and can resubmit affected | ||
| 26 | * requests. | ||
| 27 | * | ||
| 28 | * For the most part, though, we take advantage of a lossless | ||
| 29 | * communications channel to the MDS, and do not need to worry about | ||
| 30 | * timing out or resubmitting requests. | ||
| 31 | * | ||
| 32 | * We maintain a stateful "session" with each MDS we interact with. | ||
| 33 | * Within each session, we sent periodic heartbeat messages to ensure | ||
| 34 | * any capabilities or leases we have been issues remain valid. If | ||
| 35 | * the session times out and goes stale, our leases and capabilities | ||
| 36 | * are no longer valid. | ||
| 37 | */ | ||
| 38 | |||
| 39 | static void __wake_requests(struct ceph_mds_client *mdsc, | ||
| 40 | struct list_head *head); | ||
| 41 | |||
| 42 | const static struct ceph_connection_operations mds_con_ops; | ||
| 43 | |||
| 44 | |||
| 45 | /* | ||
| 46 | * mds reply parsing | ||
| 47 | */ | ||
| 48 | |||
| 49 | /* | ||
| 50 | * parse individual inode info | ||
| 51 | */ | ||
| 52 | static int parse_reply_info_in(void **p, void *end, | ||
| 53 | struct ceph_mds_reply_info_in *info) | ||
| 54 | { | ||
| 55 | int err = -EIO; | ||
| 56 | |||
| 57 | info->in = *p; | ||
| 58 | *p += sizeof(struct ceph_mds_reply_inode) + | ||
| 59 | sizeof(*info->in->fragtree.splits) * | ||
| 60 | le32_to_cpu(info->in->fragtree.nsplits); | ||
| 61 | |||
| 62 | ceph_decode_32_safe(p, end, info->symlink_len, bad); | ||
| 63 | ceph_decode_need(p, end, info->symlink_len, bad); | ||
| 64 | info->symlink = *p; | ||
| 65 | *p += info->symlink_len; | ||
| 66 | |||
| 67 | ceph_decode_32_safe(p, end, info->xattr_len, bad); | ||
| 68 | ceph_decode_need(p, end, info->xattr_len, bad); | ||
| 69 | info->xattr_data = *p; | ||
| 70 | *p += info->xattr_len; | ||
| 71 | return 0; | ||
| 72 | bad: | ||
| 73 | return err; | ||
| 74 | } | ||
| 75 | |||
| 76 | /* | ||
| 77 | * parse a normal reply, which may contain a (dir+)dentry and/or a | ||
| 78 | * target inode. | ||
| 79 | */ | ||
| 80 | static int parse_reply_info_trace(void **p, void *end, | ||
| 81 | struct ceph_mds_reply_info_parsed *info) | ||
| 82 | { | ||
| 83 | int err; | ||
| 84 | |||
| 85 | if (info->head->is_dentry) { | ||
| 86 | err = parse_reply_info_in(p, end, &info->diri); | ||
| 87 | if (err < 0) | ||
| 88 | goto out_bad; | ||
| 89 | |||
| 90 | if (unlikely(*p + sizeof(*info->dirfrag) > end)) | ||
| 91 | goto bad; | ||
| 92 | info->dirfrag = *p; | ||
| 93 | *p += sizeof(*info->dirfrag) + | ||
| 94 | sizeof(u32)*le32_to_cpu(info->dirfrag->ndist); | ||
| 95 | if (unlikely(*p > end)) | ||
| 96 | goto bad; | ||
| 97 | |||
| 98 | ceph_decode_32_safe(p, end, info->dname_len, bad); | ||
| 99 | ceph_decode_need(p, end, info->dname_len, bad); | ||
| 100 | info->dname = *p; | ||
| 101 | *p += info->dname_len; | ||
| 102 | info->dlease = *p; | ||
| 103 | *p += sizeof(*info->dlease); | ||
| 104 | } | ||
| 105 | |||
| 106 | if (info->head->is_target) { | ||
| 107 | err = parse_reply_info_in(p, end, &info->targeti); | ||
| 108 | if (err < 0) | ||
| 109 | goto out_bad; | ||
| 110 | } | ||
| 111 | |||
| 112 | if (unlikely(*p != end)) | ||
| 113 | goto bad; | ||
| 114 | return 0; | ||
| 115 | |||
| 116 | bad: | ||
| 117 | err = -EIO; | ||
| 118 | out_bad: | ||
| 119 | pr_err("problem parsing mds trace %d\n", err); | ||
| 120 | return err; | ||
| 121 | } | ||
| 122 | |||
| 123 | /* | ||
| 124 | * parse readdir results | ||
| 125 | */ | ||
| 126 | static int parse_reply_info_dir(void **p, void *end, | ||
| 127 | struct ceph_mds_reply_info_parsed *info) | ||
| 128 | { | ||
| 129 | u32 num, i = 0; | ||
| 130 | int err; | ||
| 131 | |||
| 132 | info->dir_dir = *p; | ||
| 133 | if (*p + sizeof(*info->dir_dir) > end) | ||
| 134 | goto bad; | ||
| 135 | *p += sizeof(*info->dir_dir) + | ||
| 136 | sizeof(u32)*le32_to_cpu(info->dir_dir->ndist); | ||
| 137 | if (*p > end) | ||
| 138 | goto bad; | ||
| 139 | |||
| 140 | ceph_decode_need(p, end, sizeof(num) + 2, bad); | ||
| 141 | num = ceph_decode_32(p); | ||
| 142 | info->dir_end = ceph_decode_8(p); | ||
| 143 | info->dir_complete = ceph_decode_8(p); | ||
| 144 | if (num == 0) | ||
| 145 | goto done; | ||
| 146 | |||
| 147 | /* alloc large array */ | ||
| 148 | info->dir_nr = num; | ||
| 149 | info->dir_in = kcalloc(num, sizeof(*info->dir_in) + | ||
| 150 | sizeof(*info->dir_dname) + | ||
| 151 | sizeof(*info->dir_dname_len) + | ||
| 152 | sizeof(*info->dir_dlease), | ||
| 153 | GFP_NOFS); | ||
| 154 | if (info->dir_in == NULL) { | ||
| 155 | err = -ENOMEM; | ||
| 156 | goto out_bad; | ||
| 157 | } | ||
| 158 | info->dir_dname = (void *)(info->dir_in + num); | ||
| 159 | info->dir_dname_len = (void *)(info->dir_dname + num); | ||
| 160 | info->dir_dlease = (void *)(info->dir_dname_len + num); | ||
| 161 | |||
| 162 | while (num) { | ||
| 163 | /* dentry */ | ||
| 164 | ceph_decode_need(p, end, sizeof(u32)*2, bad); | ||
| 165 | info->dir_dname_len[i] = ceph_decode_32(p); | ||
| 166 | ceph_decode_need(p, end, info->dir_dname_len[i], bad); | ||
| 167 | info->dir_dname[i] = *p; | ||
| 168 | *p += info->dir_dname_len[i]; | ||
| 169 | dout("parsed dir dname '%.*s'\n", info->dir_dname_len[i], | ||
| 170 | info->dir_dname[i]); | ||
| 171 | info->dir_dlease[i] = *p; | ||
| 172 | *p += sizeof(struct ceph_mds_reply_lease); | ||
| 173 | |||
| 174 | /* inode */ | ||
| 175 | err = parse_reply_info_in(p, end, &info->dir_in[i]); | ||
| 176 | if (err < 0) | ||
| 177 | goto out_bad; | ||
| 178 | i++; | ||
| 179 | num--; | ||
| 180 | } | ||
| 181 | |||
| 182 | done: | ||
| 183 | if (*p != end) | ||
| 184 | goto bad; | ||
| 185 | return 0; | ||
| 186 | |||
| 187 | bad: | ||
| 188 | err = -EIO; | ||
| 189 | out_bad: | ||
| 190 | pr_err("problem parsing dir contents %d\n", err); | ||
| 191 | return err; | ||
| 192 | } | ||
| 193 | |||
| 194 | /* | ||
| 195 | * parse entire mds reply | ||
| 196 | */ | ||
| 197 | static int parse_reply_info(struct ceph_msg *msg, | ||
| 198 | struct ceph_mds_reply_info_parsed *info) | ||
| 199 | { | ||
| 200 | void *p, *end; | ||
| 201 | u32 len; | ||
| 202 | int err; | ||
| 203 | |||
| 204 | info->head = msg->front.iov_base; | ||
| 205 | p = msg->front.iov_base + sizeof(struct ceph_mds_reply_head); | ||
| 206 | end = p + msg->front.iov_len - sizeof(struct ceph_mds_reply_head); | ||
| 207 | |||
| 208 | /* trace */ | ||
| 209 | ceph_decode_32_safe(&p, end, len, bad); | ||
| 210 | if (len > 0) { | ||
| 211 | err = parse_reply_info_trace(&p, p+len, info); | ||
| 212 | if (err < 0) | ||
| 213 | goto out_bad; | ||
| 214 | } | ||
| 215 | |||
| 216 | /* dir content */ | ||
| 217 | ceph_decode_32_safe(&p, end, len, bad); | ||
| 218 | if (len > 0) { | ||
| 219 | err = parse_reply_info_dir(&p, p+len, info); | ||
| 220 | if (err < 0) | ||
| 221 | goto out_bad; | ||
| 222 | } | ||
| 223 | |||
| 224 | /* snap blob */ | ||
| 225 | ceph_decode_32_safe(&p, end, len, bad); | ||
| 226 | info->snapblob_len = len; | ||
| 227 | info->snapblob = p; | ||
| 228 | p += len; | ||
| 229 | |||
| 230 | if (p != end) | ||
| 231 | goto bad; | ||
| 232 | return 0; | ||
| 233 | |||
| 234 | bad: | ||
| 235 | err = -EIO; | ||
| 236 | out_bad: | ||
| 237 | pr_err("mds parse_reply err %d\n", err); | ||
| 238 | return err; | ||
| 239 | } | ||
| 240 | |||
| 241 | static void destroy_reply_info(struct ceph_mds_reply_info_parsed *info) | ||
| 242 | { | ||
| 243 | kfree(info->dir_in); | ||
| 244 | } | ||
| 245 | |||
| 246 | |||
| 247 | /* | ||
| 248 | * sessions | ||
| 249 | */ | ||
| 250 | static const char *session_state_name(int s) | ||
| 251 | { | ||
| 252 | switch (s) { | ||
| 253 | case CEPH_MDS_SESSION_NEW: return "new"; | ||
| 254 | case CEPH_MDS_SESSION_OPENING: return "opening"; | ||
| 255 | case CEPH_MDS_SESSION_OPEN: return "open"; | ||
| 256 | case CEPH_MDS_SESSION_HUNG: return "hung"; | ||
| 257 | case CEPH_MDS_SESSION_CLOSING: return "closing"; | ||
| 258 | case CEPH_MDS_SESSION_RESTARTING: return "restarting"; | ||
| 259 | case CEPH_MDS_SESSION_RECONNECTING: return "reconnecting"; | ||
| 260 | default: return "???"; | ||
| 261 | } | ||
| 262 | } | ||
| 263 | |||
| 264 | static struct ceph_mds_session *get_session(struct ceph_mds_session *s) | ||
| 265 | { | ||
| 266 | if (atomic_inc_not_zero(&s->s_ref)) { | ||
| 267 | dout("mdsc get_session %p %d -> %d\n", s, | ||
| 268 | atomic_read(&s->s_ref)-1, atomic_read(&s->s_ref)); | ||
| 269 | return s; | ||
| 270 | } else { | ||
| 271 | dout("mdsc get_session %p 0 -- FAIL", s); | ||
| 272 | return NULL; | ||
| 273 | } | ||
| 274 | } | ||
| 275 | |||
| 276 | void ceph_put_mds_session(struct ceph_mds_session *s) | ||
| 277 | { | ||
| 278 | dout("mdsc put_session %p %d -> %d\n", s, | ||
| 279 | atomic_read(&s->s_ref), atomic_read(&s->s_ref)-1); | ||
| 280 | if (atomic_dec_and_test(&s->s_ref)) { | ||
| 281 | if (s->s_authorizer) | ||
| 282 | s->s_mdsc->client->monc.auth->ops->destroy_authorizer( | ||
| 283 | s->s_mdsc->client->monc.auth, s->s_authorizer); | ||
| 284 | kfree(s); | ||
| 285 | } | ||
| 286 | } | ||
| 287 | |||
| 288 | /* | ||
| 289 | * called under mdsc->mutex | ||
| 290 | */ | ||
| 291 | struct ceph_mds_session *__ceph_lookup_mds_session(struct ceph_mds_client *mdsc, | ||
| 292 | int mds) | ||
| 293 | { | ||
| 294 | struct ceph_mds_session *session; | ||
| 295 | |||
| 296 | if (mds >= mdsc->max_sessions || mdsc->sessions[mds] == NULL) | ||
| 297 | return NULL; | ||
| 298 | session = mdsc->sessions[mds]; | ||
| 299 | dout("lookup_mds_session %p %d\n", session, | ||
| 300 | atomic_read(&session->s_ref)); | ||
| 301 | get_session(session); | ||
| 302 | return session; | ||
| 303 | } | ||
| 304 | |||
| 305 | static bool __have_session(struct ceph_mds_client *mdsc, int mds) | ||
| 306 | { | ||
| 307 | if (mds >= mdsc->max_sessions) | ||
| 308 | return false; | ||
| 309 | return mdsc->sessions[mds]; | ||
| 310 | } | ||
| 311 | |||
| 312 | static int __verify_registered_session(struct ceph_mds_client *mdsc, | ||
| 313 | struct ceph_mds_session *s) | ||
| 314 | { | ||
| 315 | if (s->s_mds >= mdsc->max_sessions || | ||
| 316 | mdsc->sessions[s->s_mds] != s) | ||
| 317 | return -ENOENT; | ||
| 318 | return 0; | ||
| 319 | } | ||
| 320 | |||
| 321 | /* | ||
| 322 | * create+register a new session for given mds. | ||
| 323 | * called under mdsc->mutex. | ||
| 324 | */ | ||
| 325 | static struct ceph_mds_session *register_session(struct ceph_mds_client *mdsc, | ||
| 326 | int mds) | ||
| 327 | { | ||
| 328 | struct ceph_mds_session *s; | ||
| 329 | |||
| 330 | s = kzalloc(sizeof(*s), GFP_NOFS); | ||
| 331 | if (!s) | ||
| 332 | return ERR_PTR(-ENOMEM); | ||
| 333 | s->s_mdsc = mdsc; | ||
| 334 | s->s_mds = mds; | ||
| 335 | s->s_state = CEPH_MDS_SESSION_NEW; | ||
| 336 | s->s_ttl = 0; | ||
| 337 | s->s_seq = 0; | ||
| 338 | mutex_init(&s->s_mutex); | ||
| 339 | |||
| 340 | ceph_con_init(mdsc->client->msgr, &s->s_con); | ||
| 341 | s->s_con.private = s; | ||
| 342 | s->s_con.ops = &mds_con_ops; | ||
| 343 | s->s_con.peer_name.type = CEPH_ENTITY_TYPE_MDS; | ||
| 344 | s->s_con.peer_name.num = cpu_to_le64(mds); | ||
| 345 | |||
| 346 | spin_lock_init(&s->s_cap_lock); | ||
| 347 | s->s_cap_gen = 0; | ||
| 348 | s->s_cap_ttl = 0; | ||
| 349 | s->s_renew_requested = 0; | ||
| 350 | s->s_renew_seq = 0; | ||
| 351 | INIT_LIST_HEAD(&s->s_caps); | ||
| 352 | s->s_nr_caps = 0; | ||
| 353 | s->s_trim_caps = 0; | ||
| 354 | atomic_set(&s->s_ref, 1); | ||
| 355 | INIT_LIST_HEAD(&s->s_waiting); | ||
| 356 | INIT_LIST_HEAD(&s->s_unsafe); | ||
| 357 | s->s_num_cap_releases = 0; | ||
| 358 | s->s_cap_iterator = NULL; | ||
| 359 | INIT_LIST_HEAD(&s->s_cap_releases); | ||
| 360 | INIT_LIST_HEAD(&s->s_cap_releases_done); | ||
| 361 | INIT_LIST_HEAD(&s->s_cap_flushing); | ||
| 362 | INIT_LIST_HEAD(&s->s_cap_snaps_flushing); | ||
| 363 | |||
| 364 | dout("register_session mds%d\n", mds); | ||
| 365 | if (mds >= mdsc->max_sessions) { | ||
| 366 | int newmax = 1 << get_count_order(mds+1); | ||
| 367 | struct ceph_mds_session **sa; | ||
| 368 | |||
| 369 | dout("register_session realloc to %d\n", newmax); | ||
| 370 | sa = kcalloc(newmax, sizeof(void *), GFP_NOFS); | ||
| 371 | if (sa == NULL) | ||
| 372 | goto fail_realloc; | ||
| 373 | if (mdsc->sessions) { | ||
| 374 | memcpy(sa, mdsc->sessions, | ||
| 375 | mdsc->max_sessions * sizeof(void *)); | ||
| 376 | kfree(mdsc->sessions); | ||
| 377 | } | ||
| 378 | mdsc->sessions = sa; | ||
| 379 | mdsc->max_sessions = newmax; | ||
| 380 | } | ||
| 381 | mdsc->sessions[mds] = s; | ||
| 382 | atomic_inc(&s->s_ref); /* one ref to sessions[], one to caller */ | ||
| 383 | |||
| 384 | ceph_con_open(&s->s_con, ceph_mdsmap_get_addr(mdsc->mdsmap, mds)); | ||
| 385 | |||
| 386 | return s; | ||
| 387 | |||
| 388 | fail_realloc: | ||
| 389 | kfree(s); | ||
| 390 | return ERR_PTR(-ENOMEM); | ||
| 391 | } | ||
| 392 | |||
| 393 | /* | ||
| 394 | * called under mdsc->mutex | ||
| 395 | */ | ||
| 396 | static void __unregister_session(struct ceph_mds_client *mdsc, | ||
| 397 | struct ceph_mds_session *s) | ||
| 398 | { | ||
| 399 | dout("__unregister_session mds%d %p\n", s->s_mds, s); | ||
| 400 | BUG_ON(mdsc->sessions[s->s_mds] != s); | ||
| 401 | mdsc->sessions[s->s_mds] = NULL; | ||
| 402 | ceph_con_close(&s->s_con); | ||
| 403 | ceph_put_mds_session(s); | ||
| 404 | } | ||
| 405 | |||
| 406 | /* | ||
| 407 | * drop session refs in request. | ||
| 408 | * | ||
| 409 | * should be last request ref, or hold mdsc->mutex | ||
| 410 | */ | ||
| 411 | static void put_request_session(struct ceph_mds_request *req) | ||
| 412 | { | ||
| 413 | if (req->r_session) { | ||
| 414 | ceph_put_mds_session(req->r_session); | ||
| 415 | req->r_session = NULL; | ||
| 416 | } | ||
| 417 | } | ||
| 418 | |||
| 419 | void ceph_mdsc_release_request(struct kref *kref) | ||
| 420 | { | ||
| 421 | struct ceph_mds_request *req = container_of(kref, | ||
| 422 | struct ceph_mds_request, | ||
| 423 | r_kref); | ||
| 424 | if (req->r_request) | ||
| 425 | ceph_msg_put(req->r_request); | ||
| 426 | if (req->r_reply) { | ||
| 427 | ceph_msg_put(req->r_reply); | ||
| 428 | destroy_reply_info(&req->r_reply_info); | ||
| 429 | } | ||
| 430 | if (req->r_inode) { | ||
| 431 | ceph_put_cap_refs(ceph_inode(req->r_inode), | ||
| 432 | CEPH_CAP_PIN); | ||
| 433 | iput(req->r_inode); | ||
| 434 | } | ||
| 435 | if (req->r_locked_dir) | ||
| 436 | ceph_put_cap_refs(ceph_inode(req->r_locked_dir), | ||
| 437 | CEPH_CAP_PIN); | ||
| 438 | if (req->r_target_inode) | ||
| 439 | iput(req->r_target_inode); | ||
| 440 | if (req->r_dentry) | ||
| 441 | dput(req->r_dentry); | ||
| 442 | if (req->r_old_dentry) { | ||
| 443 | ceph_put_cap_refs( | ||
| 444 | ceph_inode(req->r_old_dentry->d_parent->d_inode), | ||
| 445 | CEPH_CAP_PIN); | ||
| 446 | dput(req->r_old_dentry); | ||
| 447 | } | ||
| 448 | kfree(req->r_path1); | ||
| 449 | kfree(req->r_path2); | ||
| 450 | put_request_session(req); | ||
| 451 | ceph_unreserve_caps(&req->r_caps_reservation); | ||
| 452 | kfree(req); | ||
| 453 | } | ||
| 454 | |||
| 455 | /* | ||
| 456 | * lookup session, bump ref if found. | ||
| 457 | * | ||
| 458 | * called under mdsc->mutex. | ||
| 459 | */ | ||
| 460 | static struct ceph_mds_request *__lookup_request(struct ceph_mds_client *mdsc, | ||
| 461 | u64 tid) | ||
| 462 | { | ||
| 463 | struct ceph_mds_request *req; | ||
| 464 | struct rb_node *n = mdsc->request_tree.rb_node; | ||
| 465 | |||
| 466 | while (n) { | ||
| 467 | req = rb_entry(n, struct ceph_mds_request, r_node); | ||
| 468 | if (tid < req->r_tid) | ||
| 469 | n = n->rb_left; | ||
| 470 | else if (tid > req->r_tid) | ||
| 471 | n = n->rb_right; | ||
| 472 | else { | ||
| 473 | ceph_mdsc_get_request(req); | ||
| 474 | return req; | ||
| 475 | } | ||
| 476 | } | ||
| 477 | return NULL; | ||
| 478 | } | ||
| 479 | |||
| 480 | static void __insert_request(struct ceph_mds_client *mdsc, | ||
| 481 | struct ceph_mds_request *new) | ||
| 482 | { | ||
| 483 | struct rb_node **p = &mdsc->request_tree.rb_node; | ||
| 484 | struct rb_node *parent = NULL; | ||
| 485 | struct ceph_mds_request *req = NULL; | ||
| 486 | |||
| 487 | while (*p) { | ||
| 488 | parent = *p; | ||
| 489 | req = rb_entry(parent, struct ceph_mds_request, r_node); | ||
| 490 | if (new->r_tid < req->r_tid) | ||
| 491 | p = &(*p)->rb_left; | ||
| 492 | else if (new->r_tid > req->r_tid) | ||
| 493 | p = &(*p)->rb_right; | ||
| 494 | else | ||
| 495 | BUG(); | ||
| 496 | } | ||
| 497 | |||
| 498 | rb_link_node(&new->r_node, parent, p); | ||
| 499 | rb_insert_color(&new->r_node, &mdsc->request_tree); | ||
| 500 | } | ||
| 501 | |||
| 502 | /* | ||
| 503 | * Register an in-flight request, and assign a tid. Link to directory | ||
| 504 | * are modifying (if any). | ||
| 505 | * | ||
| 506 | * Called under mdsc->mutex. | ||
| 507 | */ | ||
| 508 | static void __register_request(struct ceph_mds_client *mdsc, | ||
| 509 | struct ceph_mds_request *req, | ||
| 510 | struct inode *dir) | ||
| 511 | { | ||
| 512 | req->r_tid = ++mdsc->last_tid; | ||
| 513 | if (req->r_num_caps) | ||
| 514 | ceph_reserve_caps(&req->r_caps_reservation, req->r_num_caps); | ||
| 515 | dout("__register_request %p tid %lld\n", req, req->r_tid); | ||
| 516 | ceph_mdsc_get_request(req); | ||
| 517 | __insert_request(mdsc, req); | ||
| 518 | |||
| 519 | if (dir) { | ||
| 520 | struct ceph_inode_info *ci = ceph_inode(dir); | ||
| 521 | |||
| 522 | spin_lock(&ci->i_unsafe_lock); | ||
| 523 | req->r_unsafe_dir = dir; | ||
| 524 | list_add_tail(&req->r_unsafe_dir_item, &ci->i_unsafe_dirops); | ||
| 525 | spin_unlock(&ci->i_unsafe_lock); | ||
| 526 | } | ||
| 527 | } | ||
| 528 | |||
| 529 | static void __unregister_request(struct ceph_mds_client *mdsc, | ||
| 530 | struct ceph_mds_request *req) | ||
| 531 | { | ||
| 532 | dout("__unregister_request %p tid %lld\n", req, req->r_tid); | ||
| 533 | rb_erase(&req->r_node, &mdsc->request_tree); | ||
| 534 | RB_CLEAR_NODE(&req->r_node); | ||
| 535 | |||
| 536 | if (req->r_unsafe_dir) { | ||
| 537 | struct ceph_inode_info *ci = ceph_inode(req->r_unsafe_dir); | ||
| 538 | |||
| 539 | spin_lock(&ci->i_unsafe_lock); | ||
| 540 | list_del_init(&req->r_unsafe_dir_item); | ||
| 541 | spin_unlock(&ci->i_unsafe_lock); | ||
| 542 | } | ||
| 543 | |||
| 544 | ceph_mdsc_put_request(req); | ||
| 545 | } | ||
| 546 | |||
| 547 | /* | ||
| 548 | * Choose mds to send request to next. If there is a hint set in the | ||
| 549 | * request (e.g., due to a prior forward hint from the mds), use that. | ||
| 550 | * Otherwise, consult frag tree and/or caps to identify the | ||
| 551 | * appropriate mds. If all else fails, choose randomly. | ||
| 552 | * | ||
| 553 | * Called under mdsc->mutex. | ||
| 554 | */ | ||
| 555 | static int __choose_mds(struct ceph_mds_client *mdsc, | ||
| 556 | struct ceph_mds_request *req) | ||
| 557 | { | ||
| 558 | struct inode *inode; | ||
| 559 | struct ceph_inode_info *ci; | ||
| 560 | struct ceph_cap *cap; | ||
| 561 | int mode = req->r_direct_mode; | ||
| 562 | int mds = -1; | ||
| 563 | u32 hash = req->r_direct_hash; | ||
| 564 | bool is_hash = req->r_direct_is_hash; | ||
| 565 | |||
| 566 | /* | ||
| 567 | * is there a specific mds we should try? ignore hint if we have | ||
| 568 | * no session and the mds is not up (active or recovering). | ||
| 569 | */ | ||
| 570 | if (req->r_resend_mds >= 0 && | ||
| 571 | (__have_session(mdsc, req->r_resend_mds) || | ||
| 572 | ceph_mdsmap_get_state(mdsc->mdsmap, req->r_resend_mds) > 0)) { | ||
| 573 | dout("choose_mds using resend_mds mds%d\n", | ||
| 574 | req->r_resend_mds); | ||
| 575 | return req->r_resend_mds; | ||
| 576 | } | ||
| 577 | |||
| 578 | if (mode == USE_RANDOM_MDS) | ||
| 579 | goto random; | ||
| 580 | |||
| 581 | inode = NULL; | ||
| 582 | if (req->r_inode) { | ||
| 583 | inode = req->r_inode; | ||
| 584 | } else if (req->r_dentry) { | ||
| 585 | if (req->r_dentry->d_inode) { | ||
| 586 | inode = req->r_dentry->d_inode; | ||
| 587 | } else { | ||
| 588 | inode = req->r_dentry->d_parent->d_inode; | ||
| 589 | hash = req->r_dentry->d_name.hash; | ||
| 590 | is_hash = true; | ||
| 591 | } | ||
| 592 | } | ||
| 593 | dout("__choose_mds %p is_hash=%d (%d) mode %d\n", inode, (int)is_hash, | ||
| 594 | (int)hash, mode); | ||
| 595 | if (!inode) | ||
| 596 | goto random; | ||
| 597 | ci = ceph_inode(inode); | ||
| 598 | |||
| 599 | if (is_hash && S_ISDIR(inode->i_mode)) { | ||
| 600 | struct ceph_inode_frag frag; | ||
| 601 | int found; | ||
| 602 | |||
| 603 | ceph_choose_frag(ci, hash, &frag, &found); | ||
| 604 | if (found) { | ||
| 605 | if (mode == USE_ANY_MDS && frag.ndist > 0) { | ||
| 606 | u8 r; | ||
| 607 | |||
| 608 | /* choose a random replica */ | ||
| 609 | get_random_bytes(&r, 1); | ||
| 610 | r %= frag.ndist; | ||
| 611 | mds = frag.dist[r]; | ||
| 612 | dout("choose_mds %p %llx.%llx " | ||
| 613 | "frag %u mds%d (%d/%d)\n", | ||
| 614 | inode, ceph_vinop(inode), | ||
| 615 | frag.frag, frag.mds, | ||
| 616 | (int)r, frag.ndist); | ||
| 617 | return mds; | ||
| 618 | } | ||
| 619 | |||
| 620 | /* since this file/dir wasn't known to be | ||
| 621 | * replicated, then we want to look for the | ||
| 622 | * authoritative mds. */ | ||
| 623 | mode = USE_AUTH_MDS; | ||
| 624 | if (frag.mds >= 0) { | ||
| 625 | /* choose auth mds */ | ||
| 626 | mds = frag.mds; | ||
| 627 | dout("choose_mds %p %llx.%llx " | ||
| 628 | "frag %u mds%d (auth)\n", | ||
| 629 | inode, ceph_vinop(inode), frag.frag, mds); | ||
| 630 | return mds; | ||
| 631 | } | ||
| 632 | } | ||
| 633 | } | ||
| 634 | |||
| 635 | spin_lock(&inode->i_lock); | ||
| 636 | cap = NULL; | ||
| 637 | if (mode == USE_AUTH_MDS) | ||
| 638 | cap = ci->i_auth_cap; | ||
| 639 | if (!cap && !RB_EMPTY_ROOT(&ci->i_caps)) | ||
| 640 | cap = rb_entry(rb_first(&ci->i_caps), struct ceph_cap, ci_node); | ||
| 641 | if (!cap) { | ||
| 642 | spin_unlock(&inode->i_lock); | ||
| 643 | goto random; | ||
| 644 | } | ||
| 645 | mds = cap->session->s_mds; | ||
| 646 | dout("choose_mds %p %llx.%llx mds%d (%scap %p)\n", | ||
| 647 | inode, ceph_vinop(inode), mds, | ||
| 648 | cap == ci->i_auth_cap ? "auth " : "", cap); | ||
| 649 | spin_unlock(&inode->i_lock); | ||
| 650 | return mds; | ||
| 651 | |||
| 652 | random: | ||
| 653 | mds = ceph_mdsmap_get_random_mds(mdsc->mdsmap); | ||
| 654 | dout("choose_mds chose random mds%d\n", mds); | ||
| 655 | return mds; | ||
| 656 | } | ||
| 657 | |||
| 658 | |||
| 659 | /* | ||
| 660 | * session messages | ||
| 661 | */ | ||
| 662 | static struct ceph_msg *create_session_msg(u32 op, u64 seq) | ||
| 663 | { | ||
| 664 | struct ceph_msg *msg; | ||
| 665 | struct ceph_mds_session_head *h; | ||
| 666 | |||
| 667 | msg = ceph_msg_new(CEPH_MSG_CLIENT_SESSION, sizeof(*h), 0, 0, NULL); | ||
| 668 | if (IS_ERR(msg)) { | ||
| 669 | pr_err("create_session_msg ENOMEM creating msg\n"); | ||
| 670 | return ERR_PTR(PTR_ERR(msg)); | ||
| 671 | } | ||
| 672 | h = msg->front.iov_base; | ||
| 673 | h->op = cpu_to_le32(op); | ||
| 674 | h->seq = cpu_to_le64(seq); | ||
| 675 | return msg; | ||
| 676 | } | ||
| 677 | |||
| 678 | /* | ||
| 679 | * send session open request. | ||
| 680 | * | ||
| 681 | * called under mdsc->mutex | ||
| 682 | */ | ||
| 683 | static int __open_session(struct ceph_mds_client *mdsc, | ||
| 684 | struct ceph_mds_session *session) | ||
| 685 | { | ||
| 686 | struct ceph_msg *msg; | ||
| 687 | int mstate; | ||
| 688 | int mds = session->s_mds; | ||
| 689 | int err = 0; | ||
| 690 | |||
| 691 | /* wait for mds to go active? */ | ||
| 692 | mstate = ceph_mdsmap_get_state(mdsc->mdsmap, mds); | ||
| 693 | dout("open_session to mds%d (%s)\n", mds, | ||
| 694 | ceph_mds_state_name(mstate)); | ||
| 695 | session->s_state = CEPH_MDS_SESSION_OPENING; | ||
| 696 | session->s_renew_requested = jiffies; | ||
| 697 | |||
| 698 | /* send connect message */ | ||
| 699 | msg = create_session_msg(CEPH_SESSION_REQUEST_OPEN, session->s_seq); | ||
| 700 | if (IS_ERR(msg)) { | ||
| 701 | err = PTR_ERR(msg); | ||
| 702 | goto out; | ||
| 703 | } | ||
| 704 | ceph_con_send(&session->s_con, msg); | ||
| 705 | |||
| 706 | out: | ||
| 707 | return 0; | ||
| 708 | } | ||
| 709 | |||
| 710 | /* | ||
| 711 | * session caps | ||
| 712 | */ | ||
| 713 | |||
| 714 | /* | ||
| 715 | * Free preallocated cap messages assigned to this session | ||
| 716 | */ | ||
| 717 | static void cleanup_cap_releases(struct ceph_mds_session *session) | ||
| 718 | { | ||
| 719 | struct ceph_msg *msg; | ||
| 720 | |||
| 721 | spin_lock(&session->s_cap_lock); | ||
| 722 | while (!list_empty(&session->s_cap_releases)) { | ||
| 723 | msg = list_first_entry(&session->s_cap_releases, | ||
| 724 | struct ceph_msg, list_head); | ||
| 725 | list_del_init(&msg->list_head); | ||
| 726 | ceph_msg_put(msg); | ||
| 727 | } | ||
| 728 | while (!list_empty(&session->s_cap_releases_done)) { | ||
| 729 | msg = list_first_entry(&session->s_cap_releases_done, | ||
| 730 | struct ceph_msg, list_head); | ||
| 731 | list_del_init(&msg->list_head); | ||
| 732 | ceph_msg_put(msg); | ||
| 733 | } | ||
| 734 | spin_unlock(&session->s_cap_lock); | ||
| 735 | } | ||
| 736 | |||
| 737 | /* | ||
| 738 | * Helper to safely iterate over all caps associated with a session. | ||
| 739 | * | ||
| 740 | * caller must hold session s_mutex | ||
| 741 | */ | ||
| 742 | static int iterate_session_caps(struct ceph_mds_session *session, | ||
| 743 | int (*cb)(struct inode *, struct ceph_cap *, | ||
| 744 | void *), void *arg) | ||
| 745 | { | ||
| 746 | struct list_head *p; | ||
| 747 | struct ceph_cap *cap; | ||
| 748 | struct inode *inode, *last_inode = NULL; | ||
| 749 | struct ceph_cap *old_cap = NULL; | ||
| 750 | int ret; | ||
| 751 | |||
| 752 | dout("iterate_session_caps %p mds%d\n", session, session->s_mds); | ||
| 753 | spin_lock(&session->s_cap_lock); | ||
| 754 | p = session->s_caps.next; | ||
| 755 | while (p != &session->s_caps) { | ||
| 756 | cap = list_entry(p, struct ceph_cap, session_caps); | ||
| 757 | inode = igrab(&cap->ci->vfs_inode); | ||
| 758 | if (!inode) { | ||
| 759 | p = p->next; | ||
| 760 | continue; | ||
| 761 | } | ||
| 762 | session->s_cap_iterator = cap; | ||
| 763 | spin_unlock(&session->s_cap_lock); | ||
| 764 | |||
| 765 | if (last_inode) { | ||
| 766 | iput(last_inode); | ||
| 767 | last_inode = NULL; | ||
| 768 | } | ||
| 769 | if (old_cap) { | ||
| 770 | ceph_put_cap(old_cap); | ||
| 771 | old_cap = NULL; | ||
| 772 | } | ||
| 773 | |||
| 774 | ret = cb(inode, cap, arg); | ||
| 775 | last_inode = inode; | ||
| 776 | |||
| 777 | spin_lock(&session->s_cap_lock); | ||
| 778 | p = p->next; | ||
| 779 | if (cap->ci == NULL) { | ||
| 780 | dout("iterate_session_caps finishing cap %p removal\n", | ||
| 781 | cap); | ||
| 782 | BUG_ON(cap->session != session); | ||
| 783 | list_del_init(&cap->session_caps); | ||
| 784 | session->s_nr_caps--; | ||
| 785 | cap->session = NULL; | ||
| 786 | old_cap = cap; /* put_cap it w/o locks held */ | ||
| 787 | } | ||
| 788 | if (ret < 0) | ||
| 789 | goto out; | ||
| 790 | } | ||
| 791 | ret = 0; | ||
| 792 | out: | ||
| 793 | session->s_cap_iterator = NULL; | ||
| 794 | spin_unlock(&session->s_cap_lock); | ||
| 795 | |||
| 796 | if (last_inode) | ||
| 797 | iput(last_inode); | ||
| 798 | if (old_cap) | ||
| 799 | ceph_put_cap(old_cap); | ||
| 800 | |||
| 801 | return ret; | ||
| 802 | } | ||
| 803 | |||
| 804 | static int remove_session_caps_cb(struct inode *inode, struct ceph_cap *cap, | ||
| 805 | void *arg) | ||
| 806 | { | ||
| 807 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 808 | dout("removing cap %p, ci is %p, inode is %p\n", | ||
| 809 | cap, ci, &ci->vfs_inode); | ||
| 810 | ceph_remove_cap(cap); | ||
| 811 | return 0; | ||
| 812 | } | ||
| 813 | |||
| 814 | /* | ||
| 815 | * caller must hold session s_mutex | ||
| 816 | */ | ||
| 817 | static void remove_session_caps(struct ceph_mds_session *session) | ||
| 818 | { | ||
| 819 | dout("remove_session_caps on %p\n", session); | ||
| 820 | iterate_session_caps(session, remove_session_caps_cb, NULL); | ||
| 821 | BUG_ON(session->s_nr_caps > 0); | ||
| 822 | cleanup_cap_releases(session); | ||
| 823 | } | ||
| 824 | |||
| 825 | /* | ||
| 826 | * wake up any threads waiting on this session's caps. if the cap is | ||
| 827 | * old (didn't get renewed on the client reconnect), remove it now. | ||
| 828 | * | ||
| 829 | * caller must hold s_mutex. | ||
| 830 | */ | ||
| 831 | static int wake_up_session_cb(struct inode *inode, struct ceph_cap *cap, | ||
| 832 | void *arg) | ||
| 833 | { | ||
| 834 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 835 | |||
| 836 | wake_up(&ci->i_cap_wq); | ||
| 837 | if (arg) { | ||
| 838 | spin_lock(&inode->i_lock); | ||
| 839 | ci->i_wanted_max_size = 0; | ||
| 840 | ci->i_requested_max_size = 0; | ||
| 841 | spin_unlock(&inode->i_lock); | ||
| 842 | } | ||
| 843 | return 0; | ||
| 844 | } | ||
| 845 | |||
| 846 | static void wake_up_session_caps(struct ceph_mds_session *session, | ||
| 847 | int reconnect) | ||
| 848 | { | ||
| 849 | dout("wake_up_session_caps %p mds%d\n", session, session->s_mds); | ||
| 850 | iterate_session_caps(session, wake_up_session_cb, | ||
| 851 | (void *)(unsigned long)reconnect); | ||
| 852 | } | ||
| 853 | |||
| 854 | /* | ||
| 855 | * Send periodic message to MDS renewing all currently held caps. The | ||
| 856 | * ack will reset the expiration for all caps from this session. | ||
| 857 | * | ||
| 858 | * caller holds s_mutex | ||
| 859 | */ | ||
| 860 | static int send_renew_caps(struct ceph_mds_client *mdsc, | ||
| 861 | struct ceph_mds_session *session) | ||
| 862 | { | ||
| 863 | struct ceph_msg *msg; | ||
| 864 | int state; | ||
| 865 | |||
| 866 | if (time_after_eq(jiffies, session->s_cap_ttl) && | ||
| 867 | time_after_eq(session->s_cap_ttl, session->s_renew_requested)) | ||
| 868 | pr_info("mds%d caps stale\n", session->s_mds); | ||
| 869 | session->s_renew_requested = jiffies; | ||
| 870 | |||
| 871 | /* do not try to renew caps until a recovering mds has reconnected | ||
| 872 | * with its clients. */ | ||
| 873 | state = ceph_mdsmap_get_state(mdsc->mdsmap, session->s_mds); | ||
| 874 | if (state < CEPH_MDS_STATE_RECONNECT) { | ||
| 875 | dout("send_renew_caps ignoring mds%d (%s)\n", | ||
| 876 | session->s_mds, ceph_mds_state_name(state)); | ||
| 877 | return 0; | ||
| 878 | } | ||
| 879 | |||
| 880 | dout("send_renew_caps to mds%d (%s)\n", session->s_mds, | ||
| 881 | ceph_mds_state_name(state)); | ||
| 882 | msg = create_session_msg(CEPH_SESSION_REQUEST_RENEWCAPS, | ||
| 883 | ++session->s_renew_seq); | ||
| 884 | if (IS_ERR(msg)) | ||
| 885 | return PTR_ERR(msg); | ||
| 886 | ceph_con_send(&session->s_con, msg); | ||
| 887 | return 0; | ||
| 888 | } | ||
| 889 | |||
| 890 | /* | ||
| 891 | * Note new cap ttl, and any transition from stale -> not stale (fresh?). | ||
| 892 | * | ||
| 893 | * Called under session->s_mutex | ||
| 894 | */ | ||
| 895 | static void renewed_caps(struct ceph_mds_client *mdsc, | ||
| 896 | struct ceph_mds_session *session, int is_renew) | ||
| 897 | { | ||
| 898 | int was_stale; | ||
| 899 | int wake = 0; | ||
| 900 | |||
| 901 | spin_lock(&session->s_cap_lock); | ||
| 902 | was_stale = is_renew && (session->s_cap_ttl == 0 || | ||
| 903 | time_after_eq(jiffies, session->s_cap_ttl)); | ||
| 904 | |||
| 905 | session->s_cap_ttl = session->s_renew_requested + | ||
| 906 | mdsc->mdsmap->m_session_timeout*HZ; | ||
| 907 | |||
| 908 | if (was_stale) { | ||
| 909 | if (time_before(jiffies, session->s_cap_ttl)) { | ||
| 910 | pr_info("mds%d caps renewed\n", session->s_mds); | ||
| 911 | wake = 1; | ||
| 912 | } else { | ||
| 913 | pr_info("mds%d caps still stale\n", session->s_mds); | ||
| 914 | } | ||
| 915 | } | ||
| 916 | dout("renewed_caps mds%d ttl now %lu, was %s, now %s\n", | ||
| 917 | session->s_mds, session->s_cap_ttl, was_stale ? "stale" : "fresh", | ||
| 918 | time_before(jiffies, session->s_cap_ttl) ? "stale" : "fresh"); | ||
| 919 | spin_unlock(&session->s_cap_lock); | ||
| 920 | |||
| 921 | if (wake) | ||
| 922 | wake_up_session_caps(session, 0); | ||
| 923 | } | ||
| 924 | |||
| 925 | /* | ||
| 926 | * send a session close request | ||
| 927 | */ | ||
| 928 | static int request_close_session(struct ceph_mds_client *mdsc, | ||
| 929 | struct ceph_mds_session *session) | ||
| 930 | { | ||
| 931 | struct ceph_msg *msg; | ||
| 932 | int err = 0; | ||
| 933 | |||
| 934 | dout("request_close_session mds%d state %s seq %lld\n", | ||
| 935 | session->s_mds, session_state_name(session->s_state), | ||
| 936 | session->s_seq); | ||
| 937 | msg = create_session_msg(CEPH_SESSION_REQUEST_CLOSE, session->s_seq); | ||
| 938 | if (IS_ERR(msg)) | ||
| 939 | err = PTR_ERR(msg); | ||
| 940 | else | ||
| 941 | ceph_con_send(&session->s_con, msg); | ||
| 942 | return err; | ||
| 943 | } | ||
| 944 | |||
| 945 | /* | ||
| 946 | * Called with s_mutex held. | ||
| 947 | */ | ||
| 948 | static int __close_session(struct ceph_mds_client *mdsc, | ||
| 949 | struct ceph_mds_session *session) | ||
| 950 | { | ||
| 951 | if (session->s_state >= CEPH_MDS_SESSION_CLOSING) | ||
| 952 | return 0; | ||
| 953 | session->s_state = CEPH_MDS_SESSION_CLOSING; | ||
| 954 | return request_close_session(mdsc, session); | ||
| 955 | } | ||
| 956 | |||
| 957 | /* | ||
| 958 | * Trim old(er) caps. | ||
| 959 | * | ||
| 960 | * Because we can't cache an inode without one or more caps, we do | ||
| 961 | * this indirectly: if a cap is unused, we prune its aliases, at which | ||
| 962 | * point the inode will hopefully get dropped to. | ||
| 963 | * | ||
| 964 | * Yes, this is a bit sloppy. Our only real goal here is to respond to | ||
| 965 | * memory pressure from the MDS, though, so it needn't be perfect. | ||
| 966 | */ | ||
| 967 | static int trim_caps_cb(struct inode *inode, struct ceph_cap *cap, void *arg) | ||
| 968 | { | ||
| 969 | struct ceph_mds_session *session = arg; | ||
| 970 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 971 | int used, oissued, mine; | ||
| 972 | |||
| 973 | if (session->s_trim_caps <= 0) | ||
| 974 | return -1; | ||
| 975 | |||
| 976 | spin_lock(&inode->i_lock); | ||
| 977 | mine = cap->issued | cap->implemented; | ||
| 978 | used = __ceph_caps_used(ci); | ||
| 979 | oissued = __ceph_caps_issued_other(ci, cap); | ||
| 980 | |||
| 981 | dout("trim_caps_cb %p cap %p mine %s oissued %s used %s\n", | ||
| 982 | inode, cap, ceph_cap_string(mine), ceph_cap_string(oissued), | ||
| 983 | ceph_cap_string(used)); | ||
| 984 | if (ci->i_dirty_caps) | ||
| 985 | goto out; /* dirty caps */ | ||
| 986 | if ((used & ~oissued) & mine) | ||
| 987 | goto out; /* we need these caps */ | ||
| 988 | |||
| 989 | session->s_trim_caps--; | ||
| 990 | if (oissued) { | ||
| 991 | /* we aren't the only cap.. just remove us */ | ||
| 992 | __ceph_remove_cap(cap); | ||
| 993 | } else { | ||
| 994 | /* try to drop referring dentries */ | ||
| 995 | spin_unlock(&inode->i_lock); | ||
| 996 | d_prune_aliases(inode); | ||
| 997 | dout("trim_caps_cb %p cap %p pruned, count now %d\n", | ||
| 998 | inode, cap, atomic_read(&inode->i_count)); | ||
| 999 | return 0; | ||
| 1000 | } | ||
| 1001 | |||
| 1002 | out: | ||
| 1003 | spin_unlock(&inode->i_lock); | ||
| 1004 | return 0; | ||
| 1005 | } | ||
| 1006 | |||
| 1007 | /* | ||
| 1008 | * Trim session cap count down to some max number. | ||
| 1009 | */ | ||
| 1010 | static int trim_caps(struct ceph_mds_client *mdsc, | ||
| 1011 | struct ceph_mds_session *session, | ||
| 1012 | int max_caps) | ||
| 1013 | { | ||
| 1014 | int trim_caps = session->s_nr_caps - max_caps; | ||
| 1015 | |||
| 1016 | dout("trim_caps mds%d start: %d / %d, trim %d\n", | ||
| 1017 | session->s_mds, session->s_nr_caps, max_caps, trim_caps); | ||
| 1018 | if (trim_caps > 0) { | ||
| 1019 | session->s_trim_caps = trim_caps; | ||
| 1020 | iterate_session_caps(session, trim_caps_cb, session); | ||
| 1021 | dout("trim_caps mds%d done: %d / %d, trimmed %d\n", | ||
| 1022 | session->s_mds, session->s_nr_caps, max_caps, | ||
| 1023 | trim_caps - session->s_trim_caps); | ||
| 1024 | session->s_trim_caps = 0; | ||
| 1025 | } | ||
| 1026 | return 0; | ||
| 1027 | } | ||
| 1028 | |||
| 1029 | /* | ||
| 1030 | * Allocate cap_release messages. If there is a partially full message | ||
| 1031 | * in the queue, try to allocate enough to cover it's remainder, so that | ||
| 1032 | * we can send it immediately. | ||
| 1033 | * | ||
| 1034 | * Called under s_mutex. | ||
| 1035 | */ | ||
| 1036 | static int add_cap_releases(struct ceph_mds_client *mdsc, | ||
| 1037 | struct ceph_mds_session *session, | ||
| 1038 | int extra) | ||
| 1039 | { | ||
| 1040 | struct ceph_msg *msg; | ||
| 1041 | struct ceph_mds_cap_release *head; | ||
| 1042 | int err = -ENOMEM; | ||
| 1043 | |||
| 1044 | if (extra < 0) | ||
| 1045 | extra = mdsc->client->mount_args->cap_release_safety; | ||
| 1046 | |||
| 1047 | spin_lock(&session->s_cap_lock); | ||
| 1048 | |||
| 1049 | if (!list_empty(&session->s_cap_releases)) { | ||
| 1050 | msg = list_first_entry(&session->s_cap_releases, | ||
| 1051 | struct ceph_msg, | ||
| 1052 | list_head); | ||
| 1053 | head = msg->front.iov_base; | ||
| 1054 | extra += CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num); | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | while (session->s_num_cap_releases < session->s_nr_caps + extra) { | ||
| 1058 | spin_unlock(&session->s_cap_lock); | ||
| 1059 | msg = ceph_msg_new(CEPH_MSG_CLIENT_CAPRELEASE, PAGE_CACHE_SIZE, | ||
| 1060 | 0, 0, NULL); | ||
| 1061 | if (!msg) | ||
| 1062 | goto out_unlocked; | ||
| 1063 | dout("add_cap_releases %p msg %p now %d\n", session, msg, | ||
| 1064 | (int)msg->front.iov_len); | ||
| 1065 | head = msg->front.iov_base; | ||
| 1066 | head->num = cpu_to_le32(0); | ||
| 1067 | msg->front.iov_len = sizeof(*head); | ||
| 1068 | spin_lock(&session->s_cap_lock); | ||
| 1069 | list_add(&msg->list_head, &session->s_cap_releases); | ||
| 1070 | session->s_num_cap_releases += CEPH_CAPS_PER_RELEASE; | ||
| 1071 | } | ||
| 1072 | |||
| 1073 | if (!list_empty(&session->s_cap_releases)) { | ||
| 1074 | msg = list_first_entry(&session->s_cap_releases, | ||
| 1075 | struct ceph_msg, | ||
| 1076 | list_head); | ||
| 1077 | head = msg->front.iov_base; | ||
| 1078 | if (head->num) { | ||
| 1079 | dout(" queueing non-full %p (%d)\n", msg, | ||
| 1080 | le32_to_cpu(head->num)); | ||
| 1081 | list_move_tail(&msg->list_head, | ||
| 1082 | &session->s_cap_releases_done); | ||
| 1083 | session->s_num_cap_releases -= | ||
| 1084 | CEPH_CAPS_PER_RELEASE - le32_to_cpu(head->num); | ||
| 1085 | } | ||
| 1086 | } | ||
| 1087 | err = 0; | ||
| 1088 | spin_unlock(&session->s_cap_lock); | ||
| 1089 | out_unlocked: | ||
| 1090 | return err; | ||
| 1091 | } | ||
| 1092 | |||
| 1093 | /* | ||
| 1094 | * flush all dirty inode data to disk. | ||
| 1095 | * | ||
| 1096 | * returns true if we've flushed through want_flush_seq | ||
| 1097 | */ | ||
| 1098 | static int check_cap_flush(struct ceph_mds_client *mdsc, u64 want_flush_seq) | ||
| 1099 | { | ||
| 1100 | int mds, ret = 1; | ||
| 1101 | |||
| 1102 | dout("check_cap_flush want %lld\n", want_flush_seq); | ||
| 1103 | mutex_lock(&mdsc->mutex); | ||
| 1104 | for (mds = 0; ret && mds < mdsc->max_sessions; mds++) { | ||
| 1105 | struct ceph_mds_session *session = mdsc->sessions[mds]; | ||
| 1106 | |||
| 1107 | if (!session) | ||
| 1108 | continue; | ||
| 1109 | get_session(session); | ||
| 1110 | mutex_unlock(&mdsc->mutex); | ||
| 1111 | |||
| 1112 | mutex_lock(&session->s_mutex); | ||
| 1113 | if (!list_empty(&session->s_cap_flushing)) { | ||
| 1114 | struct ceph_inode_info *ci = | ||
| 1115 | list_entry(session->s_cap_flushing.next, | ||
| 1116 | struct ceph_inode_info, | ||
| 1117 | i_flushing_item); | ||
| 1118 | struct inode *inode = &ci->vfs_inode; | ||
| 1119 | |||
| 1120 | spin_lock(&inode->i_lock); | ||
| 1121 | if (ci->i_cap_flush_seq <= want_flush_seq) { | ||
| 1122 | dout("check_cap_flush still flushing %p " | ||
| 1123 | "seq %lld <= %lld to mds%d\n", inode, | ||
| 1124 | ci->i_cap_flush_seq, want_flush_seq, | ||
| 1125 | session->s_mds); | ||
| 1126 | ret = 0; | ||
| 1127 | } | ||
| 1128 | spin_unlock(&inode->i_lock); | ||
| 1129 | } | ||
| 1130 | mutex_unlock(&session->s_mutex); | ||
| 1131 | ceph_put_mds_session(session); | ||
| 1132 | |||
| 1133 | if (!ret) | ||
| 1134 | return ret; | ||
| 1135 | mutex_lock(&mdsc->mutex); | ||
| 1136 | } | ||
| 1137 | |||
| 1138 | mutex_unlock(&mdsc->mutex); | ||
| 1139 | dout("check_cap_flush ok, flushed thru %lld\n", want_flush_seq); | ||
| 1140 | return ret; | ||
| 1141 | } | ||
| 1142 | |||
| 1143 | /* | ||
| 1144 | * called under s_mutex | ||
| 1145 | */ | ||
| 1146 | static void send_cap_releases(struct ceph_mds_client *mdsc, | ||
| 1147 | struct ceph_mds_session *session) | ||
| 1148 | { | ||
| 1149 | struct ceph_msg *msg; | ||
| 1150 | |||
| 1151 | dout("send_cap_releases mds%d\n", session->s_mds); | ||
| 1152 | while (1) { | ||
| 1153 | spin_lock(&session->s_cap_lock); | ||
| 1154 | if (list_empty(&session->s_cap_releases_done)) | ||
| 1155 | break; | ||
| 1156 | msg = list_first_entry(&session->s_cap_releases_done, | ||
| 1157 | struct ceph_msg, list_head); | ||
| 1158 | list_del_init(&msg->list_head); | ||
| 1159 | spin_unlock(&session->s_cap_lock); | ||
| 1160 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); | ||
| 1161 | dout("send_cap_releases mds%d %p\n", session->s_mds, msg); | ||
| 1162 | ceph_con_send(&session->s_con, msg); | ||
| 1163 | } | ||
| 1164 | spin_unlock(&session->s_cap_lock); | ||
| 1165 | } | ||
| 1166 | |||
| 1167 | /* | ||
| 1168 | * requests | ||
| 1169 | */ | ||
| 1170 | |||
| 1171 | /* | ||
| 1172 | * Create an mds request. | ||
| 1173 | */ | ||
| 1174 | struct ceph_mds_request * | ||
| 1175 | ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode) | ||
| 1176 | { | ||
| 1177 | struct ceph_mds_request *req = kzalloc(sizeof(*req), GFP_NOFS); | ||
| 1178 | |||
| 1179 | if (!req) | ||
| 1180 | return ERR_PTR(-ENOMEM); | ||
| 1181 | |||
| 1182 | req->r_started = jiffies; | ||
| 1183 | req->r_resend_mds = -1; | ||
| 1184 | INIT_LIST_HEAD(&req->r_unsafe_dir_item); | ||
| 1185 | req->r_fmode = -1; | ||
| 1186 | kref_init(&req->r_kref); | ||
| 1187 | INIT_LIST_HEAD(&req->r_wait); | ||
| 1188 | init_completion(&req->r_completion); | ||
| 1189 | init_completion(&req->r_safe_completion); | ||
| 1190 | INIT_LIST_HEAD(&req->r_unsafe_item); | ||
| 1191 | |||
| 1192 | req->r_op = op; | ||
| 1193 | req->r_direct_mode = mode; | ||
| 1194 | return req; | ||
| 1195 | } | ||
| 1196 | |||
| 1197 | /* | ||
| 1198 | * return oldest (lowest) request, tid in request tree, 0 if none. | ||
| 1199 | * | ||
| 1200 | * called under mdsc->mutex. | ||
| 1201 | */ | ||
| 1202 | static struct ceph_mds_request *__get_oldest_req(struct ceph_mds_client *mdsc) | ||
| 1203 | { | ||
| 1204 | if (RB_EMPTY_ROOT(&mdsc->request_tree)) | ||
| 1205 | return NULL; | ||
| 1206 | return rb_entry(rb_first(&mdsc->request_tree), | ||
| 1207 | struct ceph_mds_request, r_node); | ||
| 1208 | } | ||
| 1209 | |||
| 1210 | static u64 __get_oldest_tid(struct ceph_mds_client *mdsc) | ||
| 1211 | { | ||
| 1212 | struct ceph_mds_request *req = __get_oldest_req(mdsc); | ||
| 1213 | |||
| 1214 | if (req) | ||
| 1215 | return req->r_tid; | ||
| 1216 | return 0; | ||
| 1217 | } | ||
| 1218 | |||
| 1219 | /* | ||
| 1220 | * Build a dentry's path. Allocate on heap; caller must kfree. Based | ||
| 1221 | * on build_path_from_dentry in fs/cifs/dir.c. | ||
| 1222 | * | ||
| 1223 | * If @stop_on_nosnap, generate path relative to the first non-snapped | ||
| 1224 | * inode. | ||
| 1225 | * | ||
| 1226 | * Encode hidden .snap dirs as a double /, i.e. | ||
| 1227 | * foo/.snap/bar -> foo//bar | ||
| 1228 | */ | ||
| 1229 | char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base, | ||
| 1230 | int stop_on_nosnap) | ||
| 1231 | { | ||
| 1232 | struct dentry *temp; | ||
| 1233 | char *path; | ||
| 1234 | int len, pos; | ||
| 1235 | |||
| 1236 | if (dentry == NULL) | ||
| 1237 | return ERR_PTR(-EINVAL); | ||
| 1238 | |||
| 1239 | retry: | ||
| 1240 | len = 0; | ||
| 1241 | for (temp = dentry; !IS_ROOT(temp);) { | ||
| 1242 | struct inode *inode = temp->d_inode; | ||
| 1243 | if (inode && ceph_snap(inode) == CEPH_SNAPDIR) | ||
| 1244 | len++; /* slash only */ | ||
| 1245 | else if (stop_on_nosnap && inode && | ||
| 1246 | ceph_snap(inode) == CEPH_NOSNAP) | ||
| 1247 | break; | ||
| 1248 | else | ||
| 1249 | len += 1 + temp->d_name.len; | ||
| 1250 | temp = temp->d_parent; | ||
| 1251 | if (temp == NULL) { | ||
| 1252 | pr_err("build_path_dentry corrupt dentry %p\n", dentry); | ||
| 1253 | return ERR_PTR(-EINVAL); | ||
| 1254 | } | ||
| 1255 | } | ||
| 1256 | if (len) | ||
| 1257 | len--; /* no leading '/' */ | ||
| 1258 | |||
| 1259 | path = kmalloc(len+1, GFP_NOFS); | ||
| 1260 | if (path == NULL) | ||
| 1261 | return ERR_PTR(-ENOMEM); | ||
| 1262 | pos = len; | ||
| 1263 | path[pos] = 0; /* trailing null */ | ||
| 1264 | for (temp = dentry; !IS_ROOT(temp) && pos != 0; ) { | ||
| 1265 | struct inode *inode = temp->d_inode; | ||
| 1266 | |||
| 1267 | if (inode && ceph_snap(inode) == CEPH_SNAPDIR) { | ||
| 1268 | dout("build_path_dentry path+%d: %p SNAPDIR\n", | ||
| 1269 | pos, temp); | ||
| 1270 | } else if (stop_on_nosnap && inode && | ||
| 1271 | ceph_snap(inode) == CEPH_NOSNAP) { | ||
| 1272 | break; | ||
| 1273 | } else { | ||
| 1274 | pos -= temp->d_name.len; | ||
| 1275 | if (pos < 0) | ||
| 1276 | break; | ||
| 1277 | strncpy(path + pos, temp->d_name.name, | ||
| 1278 | temp->d_name.len); | ||
| 1279 | dout("build_path_dentry path+%d: %p '%.*s'\n", | ||
| 1280 | pos, temp, temp->d_name.len, path + pos); | ||
| 1281 | } | ||
| 1282 | if (pos) | ||
| 1283 | path[--pos] = '/'; | ||
| 1284 | temp = temp->d_parent; | ||
| 1285 | if (temp == NULL) { | ||
| 1286 | pr_err("build_path_dentry corrupt dentry\n"); | ||
| 1287 | kfree(path); | ||
| 1288 | return ERR_PTR(-EINVAL); | ||
| 1289 | } | ||
| 1290 | } | ||
| 1291 | if (pos != 0) { | ||
| 1292 | pr_err("build_path_dentry did not end path lookup where " | ||
| 1293 | "expected, namelen is %d, pos is %d\n", len, pos); | ||
| 1294 | /* presumably this is only possible if racing with a | ||
| 1295 | rename of one of the parent directories (we can not | ||
| 1296 | lock the dentries above us to prevent this, but | ||
| 1297 | retrying should be harmless) */ | ||
| 1298 | kfree(path); | ||
| 1299 | goto retry; | ||
| 1300 | } | ||
| 1301 | |||
| 1302 | *base = ceph_ino(temp->d_inode); | ||
| 1303 | *plen = len; | ||
| 1304 | dout("build_path_dentry on %p %d built %llx '%.*s'\n", | ||
| 1305 | dentry, atomic_read(&dentry->d_count), *base, len, path); | ||
| 1306 | return path; | ||
| 1307 | } | ||
| 1308 | |||
| 1309 | static int build_dentry_path(struct dentry *dentry, | ||
| 1310 | const char **ppath, int *ppathlen, u64 *pino, | ||
| 1311 | int *pfreepath) | ||
| 1312 | { | ||
| 1313 | char *path; | ||
| 1314 | |||
| 1315 | if (ceph_snap(dentry->d_parent->d_inode) == CEPH_NOSNAP) { | ||
| 1316 | *pino = ceph_ino(dentry->d_parent->d_inode); | ||
| 1317 | *ppath = dentry->d_name.name; | ||
| 1318 | *ppathlen = dentry->d_name.len; | ||
| 1319 | return 0; | ||
| 1320 | } | ||
| 1321 | path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1); | ||
| 1322 | if (IS_ERR(path)) | ||
| 1323 | return PTR_ERR(path); | ||
| 1324 | *ppath = path; | ||
| 1325 | *pfreepath = 1; | ||
| 1326 | return 0; | ||
| 1327 | } | ||
| 1328 | |||
| 1329 | static int build_inode_path(struct inode *inode, | ||
| 1330 | const char **ppath, int *ppathlen, u64 *pino, | ||
| 1331 | int *pfreepath) | ||
| 1332 | { | ||
| 1333 | struct dentry *dentry; | ||
| 1334 | char *path; | ||
| 1335 | |||
| 1336 | if (ceph_snap(inode) == CEPH_NOSNAP) { | ||
| 1337 | *pino = ceph_ino(inode); | ||
| 1338 | *ppathlen = 0; | ||
| 1339 | return 0; | ||
| 1340 | } | ||
| 1341 | dentry = d_find_alias(inode); | ||
| 1342 | path = ceph_mdsc_build_path(dentry, ppathlen, pino, 1); | ||
| 1343 | dput(dentry); | ||
| 1344 | if (IS_ERR(path)) | ||
| 1345 | return PTR_ERR(path); | ||
| 1346 | *ppath = path; | ||
| 1347 | *pfreepath = 1; | ||
| 1348 | return 0; | ||
| 1349 | } | ||
| 1350 | |||
| 1351 | /* | ||
| 1352 | * request arguments may be specified via an inode *, a dentry *, or | ||
| 1353 | * an explicit ino+path. | ||
| 1354 | */ | ||
| 1355 | static int set_request_path_attr(struct inode *rinode, struct dentry *rdentry, | ||
| 1356 | const char *rpath, u64 rino, | ||
| 1357 | const char **ppath, int *pathlen, | ||
| 1358 | u64 *ino, int *freepath) | ||
| 1359 | { | ||
| 1360 | int r = 0; | ||
| 1361 | |||
| 1362 | if (rinode) { | ||
| 1363 | r = build_inode_path(rinode, ppath, pathlen, ino, freepath); | ||
| 1364 | dout(" inode %p %llx.%llx\n", rinode, ceph_ino(rinode), | ||
| 1365 | ceph_snap(rinode)); | ||
| 1366 | } else if (rdentry) { | ||
| 1367 | r = build_dentry_path(rdentry, ppath, pathlen, ino, freepath); | ||
| 1368 | dout(" dentry %p %llx/%.*s\n", rdentry, *ino, *pathlen, | ||
| 1369 | *ppath); | ||
| 1370 | } else if (rpath) { | ||
| 1371 | *ino = rino; | ||
| 1372 | *ppath = rpath; | ||
| 1373 | *pathlen = strlen(rpath); | ||
| 1374 | dout(" path %.*s\n", *pathlen, rpath); | ||
| 1375 | } | ||
| 1376 | |||
| 1377 | return r; | ||
| 1378 | } | ||
| 1379 | |||
| 1380 | /* | ||
| 1381 | * called under mdsc->mutex | ||
| 1382 | */ | ||
| 1383 | static struct ceph_msg *create_request_message(struct ceph_mds_client *mdsc, | ||
| 1384 | struct ceph_mds_request *req, | ||
| 1385 | int mds) | ||
| 1386 | { | ||
| 1387 | struct ceph_msg *msg; | ||
| 1388 | struct ceph_mds_request_head *head; | ||
| 1389 | const char *path1 = NULL; | ||
| 1390 | const char *path2 = NULL; | ||
| 1391 | u64 ino1 = 0, ino2 = 0; | ||
| 1392 | int pathlen1 = 0, pathlen2 = 0; | ||
| 1393 | int freepath1 = 0, freepath2 = 0; | ||
| 1394 | int len; | ||
| 1395 | u16 releases; | ||
| 1396 | void *p, *end; | ||
| 1397 | int ret; | ||
| 1398 | |||
| 1399 | ret = set_request_path_attr(req->r_inode, req->r_dentry, | ||
| 1400 | req->r_path1, req->r_ino1.ino, | ||
| 1401 | &path1, &pathlen1, &ino1, &freepath1); | ||
| 1402 | if (ret < 0) { | ||
| 1403 | msg = ERR_PTR(ret); | ||
| 1404 | goto out; | ||
| 1405 | } | ||
| 1406 | |||
| 1407 | ret = set_request_path_attr(NULL, req->r_old_dentry, | ||
| 1408 | req->r_path2, req->r_ino2.ino, | ||
| 1409 | &path2, &pathlen2, &ino2, &freepath2); | ||
| 1410 | if (ret < 0) { | ||
| 1411 | msg = ERR_PTR(ret); | ||
| 1412 | goto out_free1; | ||
| 1413 | } | ||
| 1414 | |||
| 1415 | len = sizeof(*head) + | ||
| 1416 | pathlen1 + pathlen2 + 2*(1 + sizeof(u32) + sizeof(u64)); | ||
| 1417 | |||
| 1418 | /* calculate (max) length for cap releases */ | ||
| 1419 | len += sizeof(struct ceph_mds_request_release) * | ||
| 1420 | (!!req->r_inode_drop + !!req->r_dentry_drop + | ||
| 1421 | !!req->r_old_inode_drop + !!req->r_old_dentry_drop); | ||
| 1422 | if (req->r_dentry_drop) | ||
| 1423 | len += req->r_dentry->d_name.len; | ||
| 1424 | if (req->r_old_dentry_drop) | ||
| 1425 | len += req->r_old_dentry->d_name.len; | ||
| 1426 | |||
| 1427 | msg = ceph_msg_new(CEPH_MSG_CLIENT_REQUEST, len, 0, 0, NULL); | ||
| 1428 | if (IS_ERR(msg)) | ||
| 1429 | goto out_free2; | ||
| 1430 | |||
| 1431 | msg->hdr.tid = cpu_to_le64(req->r_tid); | ||
| 1432 | |||
| 1433 | head = msg->front.iov_base; | ||
| 1434 | p = msg->front.iov_base + sizeof(*head); | ||
| 1435 | end = msg->front.iov_base + msg->front.iov_len; | ||
| 1436 | |||
| 1437 | head->mdsmap_epoch = cpu_to_le32(mdsc->mdsmap->m_epoch); | ||
| 1438 | head->op = cpu_to_le32(req->r_op); | ||
| 1439 | head->caller_uid = cpu_to_le32(current_fsuid()); | ||
| 1440 | head->caller_gid = cpu_to_le32(current_fsgid()); | ||
| 1441 | head->args = req->r_args; | ||
| 1442 | |||
| 1443 | ceph_encode_filepath(&p, end, ino1, path1); | ||
| 1444 | ceph_encode_filepath(&p, end, ino2, path2); | ||
| 1445 | |||
| 1446 | /* cap releases */ | ||
| 1447 | releases = 0; | ||
| 1448 | if (req->r_inode_drop) | ||
| 1449 | releases += ceph_encode_inode_release(&p, | ||
| 1450 | req->r_inode ? req->r_inode : req->r_dentry->d_inode, | ||
| 1451 | mds, req->r_inode_drop, req->r_inode_unless, 0); | ||
| 1452 | if (req->r_dentry_drop) | ||
| 1453 | releases += ceph_encode_dentry_release(&p, req->r_dentry, | ||
| 1454 | mds, req->r_dentry_drop, req->r_dentry_unless); | ||
| 1455 | if (req->r_old_dentry_drop) | ||
| 1456 | releases += ceph_encode_dentry_release(&p, req->r_old_dentry, | ||
| 1457 | mds, req->r_old_dentry_drop, req->r_old_dentry_unless); | ||
| 1458 | if (req->r_old_inode_drop) | ||
| 1459 | releases += ceph_encode_inode_release(&p, | ||
| 1460 | req->r_old_dentry->d_inode, | ||
| 1461 | mds, req->r_old_inode_drop, req->r_old_inode_unless, 0); | ||
| 1462 | head->num_releases = cpu_to_le16(releases); | ||
| 1463 | |||
| 1464 | BUG_ON(p > end); | ||
| 1465 | msg->front.iov_len = p - msg->front.iov_base; | ||
| 1466 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); | ||
| 1467 | |||
| 1468 | msg->pages = req->r_pages; | ||
| 1469 | msg->nr_pages = req->r_num_pages; | ||
| 1470 | msg->hdr.data_len = cpu_to_le32(req->r_data_len); | ||
| 1471 | msg->hdr.data_off = cpu_to_le16(0); | ||
| 1472 | |||
| 1473 | out_free2: | ||
| 1474 | if (freepath2) | ||
| 1475 | kfree((char *)path2); | ||
| 1476 | out_free1: | ||
| 1477 | if (freepath1) | ||
| 1478 | kfree((char *)path1); | ||
| 1479 | out: | ||
| 1480 | return msg; | ||
| 1481 | } | ||
| 1482 | |||
| 1483 | /* | ||
| 1484 | * called under mdsc->mutex if error, under no mutex if | ||
| 1485 | * success. | ||
| 1486 | */ | ||
| 1487 | static void complete_request(struct ceph_mds_client *mdsc, | ||
| 1488 | struct ceph_mds_request *req) | ||
| 1489 | { | ||
| 1490 | if (req->r_callback) | ||
| 1491 | req->r_callback(mdsc, req); | ||
| 1492 | else | ||
| 1493 | complete(&req->r_completion); | ||
| 1494 | } | ||
| 1495 | |||
| 1496 | /* | ||
| 1497 | * called under mdsc->mutex | ||
| 1498 | */ | ||
| 1499 | static int __prepare_send_request(struct ceph_mds_client *mdsc, | ||
| 1500 | struct ceph_mds_request *req, | ||
| 1501 | int mds) | ||
| 1502 | { | ||
| 1503 | struct ceph_mds_request_head *rhead; | ||
| 1504 | struct ceph_msg *msg; | ||
| 1505 | int flags = 0; | ||
| 1506 | |||
| 1507 | req->r_mds = mds; | ||
| 1508 | req->r_attempts++; | ||
| 1509 | dout("prepare_send_request %p tid %lld %s (attempt %d)\n", req, | ||
| 1510 | req->r_tid, ceph_mds_op_name(req->r_op), req->r_attempts); | ||
| 1511 | |||
| 1512 | if (req->r_request) { | ||
| 1513 | ceph_msg_put(req->r_request); | ||
| 1514 | req->r_request = NULL; | ||
| 1515 | } | ||
| 1516 | msg = create_request_message(mdsc, req, mds); | ||
| 1517 | if (IS_ERR(msg)) { | ||
| 1518 | req->r_reply = ERR_PTR(PTR_ERR(msg)); | ||
| 1519 | complete_request(mdsc, req); | ||
| 1520 | return -PTR_ERR(msg); | ||
| 1521 | } | ||
| 1522 | req->r_request = msg; | ||
| 1523 | |||
| 1524 | rhead = msg->front.iov_base; | ||
| 1525 | rhead->oldest_client_tid = cpu_to_le64(__get_oldest_tid(mdsc)); | ||
| 1526 | if (req->r_got_unsafe) | ||
| 1527 | flags |= CEPH_MDS_FLAG_REPLAY; | ||
| 1528 | if (req->r_locked_dir) | ||
| 1529 | flags |= CEPH_MDS_FLAG_WANT_DENTRY; | ||
| 1530 | rhead->flags = cpu_to_le32(flags); | ||
| 1531 | rhead->num_fwd = req->r_num_fwd; | ||
| 1532 | rhead->num_retry = req->r_attempts - 1; | ||
| 1533 | |||
| 1534 | dout(" r_locked_dir = %p\n", req->r_locked_dir); | ||
| 1535 | |||
| 1536 | if (req->r_target_inode && req->r_got_unsafe) | ||
| 1537 | rhead->ino = cpu_to_le64(ceph_ino(req->r_target_inode)); | ||
| 1538 | else | ||
| 1539 | rhead->ino = 0; | ||
| 1540 | return 0; | ||
| 1541 | } | ||
| 1542 | |||
| 1543 | /* | ||
| 1544 | * send request, or put it on the appropriate wait list. | ||
| 1545 | */ | ||
| 1546 | static int __do_request(struct ceph_mds_client *mdsc, | ||
| 1547 | struct ceph_mds_request *req) | ||
| 1548 | { | ||
| 1549 | struct ceph_mds_session *session = NULL; | ||
| 1550 | int mds = -1; | ||
| 1551 | int err = -EAGAIN; | ||
| 1552 | |||
| 1553 | if (req->r_reply) | ||
| 1554 | goto out; | ||
| 1555 | |||
| 1556 | if (req->r_timeout && | ||
| 1557 | time_after_eq(jiffies, req->r_started + req->r_timeout)) { | ||
| 1558 | dout("do_request timed out\n"); | ||
| 1559 | err = -EIO; | ||
| 1560 | goto finish; | ||
| 1561 | } | ||
| 1562 | |||
| 1563 | mds = __choose_mds(mdsc, req); | ||
| 1564 | if (mds < 0 || | ||
| 1565 | ceph_mdsmap_get_state(mdsc->mdsmap, mds) < CEPH_MDS_STATE_ACTIVE) { | ||
| 1566 | dout("do_request no mds or not active, waiting for map\n"); | ||
| 1567 | list_add(&req->r_wait, &mdsc->waiting_for_map); | ||
| 1568 | goto out; | ||
| 1569 | } | ||
| 1570 | |||
| 1571 | /* get, open session */ | ||
| 1572 | session = __ceph_lookup_mds_session(mdsc, mds); | ||
| 1573 | if (!session) { | ||
| 1574 | session = register_session(mdsc, mds); | ||
| 1575 | if (IS_ERR(session)) { | ||
| 1576 | err = PTR_ERR(session); | ||
| 1577 | goto finish; | ||
| 1578 | } | ||
| 1579 | } | ||
| 1580 | dout("do_request mds%d session %p state %s\n", mds, session, | ||
| 1581 | session_state_name(session->s_state)); | ||
| 1582 | if (session->s_state != CEPH_MDS_SESSION_OPEN && | ||
| 1583 | session->s_state != CEPH_MDS_SESSION_HUNG) { | ||
| 1584 | if (session->s_state == CEPH_MDS_SESSION_NEW || | ||
| 1585 | session->s_state == CEPH_MDS_SESSION_CLOSING) | ||
| 1586 | __open_session(mdsc, session); | ||
| 1587 | list_add(&req->r_wait, &session->s_waiting); | ||
| 1588 | goto out_session; | ||
| 1589 | } | ||
| 1590 | |||
| 1591 | /* send request */ | ||
| 1592 | req->r_session = get_session(session); | ||
| 1593 | req->r_resend_mds = -1; /* forget any previous mds hint */ | ||
| 1594 | |||
| 1595 | if (req->r_request_started == 0) /* note request start time */ | ||
| 1596 | req->r_request_started = jiffies; | ||
| 1597 | |||
| 1598 | err = __prepare_send_request(mdsc, req, mds); | ||
| 1599 | if (!err) { | ||
| 1600 | ceph_msg_get(req->r_request); | ||
| 1601 | ceph_con_send(&session->s_con, req->r_request); | ||
| 1602 | } | ||
| 1603 | |||
| 1604 | out_session: | ||
| 1605 | ceph_put_mds_session(session); | ||
| 1606 | out: | ||
| 1607 | return err; | ||
| 1608 | |||
| 1609 | finish: | ||
| 1610 | req->r_reply = ERR_PTR(err); | ||
| 1611 | complete_request(mdsc, req); | ||
| 1612 | goto out; | ||
| 1613 | } | ||
| 1614 | |||
| 1615 | /* | ||
| 1616 | * called under mdsc->mutex | ||
| 1617 | */ | ||
| 1618 | static void __wake_requests(struct ceph_mds_client *mdsc, | ||
| 1619 | struct list_head *head) | ||
| 1620 | { | ||
| 1621 | struct ceph_mds_request *req, *nreq; | ||
| 1622 | |||
| 1623 | list_for_each_entry_safe(req, nreq, head, r_wait) { | ||
| 1624 | list_del_init(&req->r_wait); | ||
| 1625 | __do_request(mdsc, req); | ||
| 1626 | } | ||
| 1627 | } | ||
| 1628 | |||
| 1629 | /* | ||
| 1630 | * Wake up threads with requests pending for @mds, so that they can | ||
| 1631 | * resubmit their requests to a possibly different mds. If @all is set, | ||
| 1632 | * wake up if their requests has been forwarded to @mds, too. | ||
| 1633 | */ | ||
| 1634 | static void kick_requests(struct ceph_mds_client *mdsc, int mds, int all) | ||
| 1635 | { | ||
| 1636 | struct ceph_mds_request *req; | ||
| 1637 | struct rb_node *p; | ||
| 1638 | |||
| 1639 | dout("kick_requests mds%d\n", mds); | ||
| 1640 | for (p = rb_first(&mdsc->request_tree); p; p = rb_next(p)) { | ||
| 1641 | req = rb_entry(p, struct ceph_mds_request, r_node); | ||
| 1642 | if (req->r_got_unsafe) | ||
| 1643 | continue; | ||
| 1644 | if (req->r_session && | ||
| 1645 | req->r_session->s_mds == mds) { | ||
| 1646 | dout(" kicking tid %llu\n", req->r_tid); | ||
| 1647 | put_request_session(req); | ||
| 1648 | __do_request(mdsc, req); | ||
| 1649 | } | ||
| 1650 | } | ||
| 1651 | } | ||
| 1652 | |||
| 1653 | void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, | ||
| 1654 | struct ceph_mds_request *req) | ||
| 1655 | { | ||
| 1656 | dout("submit_request on %p\n", req); | ||
| 1657 | mutex_lock(&mdsc->mutex); | ||
| 1658 | __register_request(mdsc, req, NULL); | ||
| 1659 | __do_request(mdsc, req); | ||
| 1660 | mutex_unlock(&mdsc->mutex); | ||
| 1661 | } | ||
| 1662 | |||
| 1663 | /* | ||
| 1664 | * Synchrously perform an mds request. Take care of all of the | ||
| 1665 | * session setup, forwarding, retry details. | ||
| 1666 | */ | ||
| 1667 | int ceph_mdsc_do_request(struct ceph_mds_client *mdsc, | ||
| 1668 | struct inode *dir, | ||
| 1669 | struct ceph_mds_request *req) | ||
| 1670 | { | ||
| 1671 | int err; | ||
| 1672 | |||
| 1673 | dout("do_request on %p\n", req); | ||
| 1674 | |||
| 1675 | /* take CAP_PIN refs for r_inode, r_locked_dir, r_old_dentry */ | ||
| 1676 | if (req->r_inode) | ||
| 1677 | ceph_get_cap_refs(ceph_inode(req->r_inode), CEPH_CAP_PIN); | ||
| 1678 | if (req->r_locked_dir) | ||
| 1679 | ceph_get_cap_refs(ceph_inode(req->r_locked_dir), CEPH_CAP_PIN); | ||
| 1680 | if (req->r_old_dentry) | ||
| 1681 | ceph_get_cap_refs( | ||
| 1682 | ceph_inode(req->r_old_dentry->d_parent->d_inode), | ||
| 1683 | CEPH_CAP_PIN); | ||
| 1684 | |||
| 1685 | /* issue */ | ||
| 1686 | mutex_lock(&mdsc->mutex); | ||
| 1687 | __register_request(mdsc, req, dir); | ||
| 1688 | __do_request(mdsc, req); | ||
| 1689 | |||
| 1690 | /* wait */ | ||
| 1691 | if (!req->r_reply) { | ||
| 1692 | mutex_unlock(&mdsc->mutex); | ||
| 1693 | if (req->r_timeout) { | ||
| 1694 | err = (long)wait_for_completion_interruptible_timeout( | ||
| 1695 | &req->r_completion, req->r_timeout); | ||
| 1696 | if (err == 0) | ||
| 1697 | req->r_reply = ERR_PTR(-EIO); | ||
| 1698 | else if (err < 0) | ||
| 1699 | req->r_reply = ERR_PTR(err); | ||
| 1700 | } else { | ||
| 1701 | err = wait_for_completion_interruptible( | ||
| 1702 | &req->r_completion); | ||
| 1703 | if (err) | ||
| 1704 | req->r_reply = ERR_PTR(err); | ||
| 1705 | } | ||
| 1706 | mutex_lock(&mdsc->mutex); | ||
| 1707 | } | ||
| 1708 | |||
| 1709 | if (IS_ERR(req->r_reply)) { | ||
| 1710 | err = PTR_ERR(req->r_reply); | ||
| 1711 | req->r_reply = NULL; | ||
| 1712 | |||
| 1713 | if (err == -ERESTARTSYS) { | ||
| 1714 | /* aborted */ | ||
| 1715 | req->r_aborted = true; | ||
| 1716 | |||
| 1717 | if (req->r_locked_dir && | ||
| 1718 | (req->r_op & CEPH_MDS_OP_WRITE)) { | ||
| 1719 | struct ceph_inode_info *ci = | ||
| 1720 | ceph_inode(req->r_locked_dir); | ||
| 1721 | |||
| 1722 | dout("aborted, clearing I_COMPLETE on %p\n", | ||
| 1723 | req->r_locked_dir); | ||
| 1724 | spin_lock(&req->r_locked_dir->i_lock); | ||
| 1725 | ci->i_ceph_flags &= ~CEPH_I_COMPLETE; | ||
| 1726 | ci->i_release_count++; | ||
| 1727 | spin_unlock(&req->r_locked_dir->i_lock); | ||
| 1728 | } | ||
| 1729 | } else { | ||
| 1730 | /* clean up this request */ | ||
| 1731 | __unregister_request(mdsc, req); | ||
| 1732 | if (!list_empty(&req->r_unsafe_item)) | ||
| 1733 | list_del_init(&req->r_unsafe_item); | ||
| 1734 | complete(&req->r_safe_completion); | ||
| 1735 | } | ||
| 1736 | } else if (req->r_err) { | ||
| 1737 | err = req->r_err; | ||
| 1738 | } else { | ||
| 1739 | err = le32_to_cpu(req->r_reply_info.head->result); | ||
| 1740 | } | ||
| 1741 | mutex_unlock(&mdsc->mutex); | ||
| 1742 | |||
| 1743 | dout("do_request %p done, result %d\n", req, err); | ||
| 1744 | return err; | ||
| 1745 | } | ||
| 1746 | |||
| 1747 | /* | ||
| 1748 | * Handle mds reply. | ||
| 1749 | * | ||
| 1750 | * We take the session mutex and parse and process the reply immediately. | ||
| 1751 | * This preserves the logical ordering of replies, capabilities, etc., sent | ||
| 1752 | * by the MDS as they are applied to our local cache. | ||
| 1753 | */ | ||
| 1754 | static void handle_reply(struct ceph_mds_session *session, struct ceph_msg *msg) | ||
| 1755 | { | ||
| 1756 | struct ceph_mds_client *mdsc = session->s_mdsc; | ||
| 1757 | struct ceph_mds_request *req; | ||
| 1758 | struct ceph_mds_reply_head *head = msg->front.iov_base; | ||
| 1759 | struct ceph_mds_reply_info_parsed *rinfo; /* parsed reply info */ | ||
| 1760 | u64 tid; | ||
| 1761 | int err, result; | ||
| 1762 | int mds = session->s_mds; | ||
| 1763 | |||
| 1764 | if (msg->front.iov_len < sizeof(*head)) { | ||
| 1765 | pr_err("mdsc_handle_reply got corrupt (short) reply\n"); | ||
| 1766 | ceph_msg_dump(msg); | ||
| 1767 | return; | ||
| 1768 | } | ||
| 1769 | |||
| 1770 | /* get request, session */ | ||
| 1771 | tid = le64_to_cpu(msg->hdr.tid); | ||
| 1772 | mutex_lock(&mdsc->mutex); | ||
| 1773 | req = __lookup_request(mdsc, tid); | ||
| 1774 | if (!req) { | ||
| 1775 | dout("handle_reply on unknown tid %llu\n", tid); | ||
| 1776 | mutex_unlock(&mdsc->mutex); | ||
| 1777 | return; | ||
| 1778 | } | ||
| 1779 | dout("handle_reply %p\n", req); | ||
| 1780 | |||
| 1781 | /* correct session? */ | ||
| 1782 | if (req->r_session != session) { | ||
| 1783 | pr_err("mdsc_handle_reply got %llu on session mds%d" | ||
| 1784 | " not mds%d\n", tid, session->s_mds, | ||
| 1785 | req->r_session ? req->r_session->s_mds : -1); | ||
| 1786 | mutex_unlock(&mdsc->mutex); | ||
| 1787 | goto out; | ||
| 1788 | } | ||
| 1789 | |||
| 1790 | /* dup? */ | ||
| 1791 | if ((req->r_got_unsafe && !head->safe) || | ||
| 1792 | (req->r_got_safe && head->safe)) { | ||
| 1793 | pr_warning("got a dup %s reply on %llu from mds%d\n", | ||
| 1794 | head->safe ? "safe" : "unsafe", tid, mds); | ||
| 1795 | mutex_unlock(&mdsc->mutex); | ||
| 1796 | goto out; | ||
| 1797 | } | ||
| 1798 | |||
| 1799 | result = le32_to_cpu(head->result); | ||
| 1800 | |||
| 1801 | /* | ||
| 1802 | * Tolerate 2 consecutive ESTALEs from the same mds. | ||
| 1803 | * FIXME: we should be looking at the cap migrate_seq. | ||
| 1804 | */ | ||
| 1805 | if (result == -ESTALE) { | ||
| 1806 | req->r_direct_mode = USE_AUTH_MDS; | ||
| 1807 | req->r_num_stale++; | ||
| 1808 | if (req->r_num_stale <= 2) { | ||
| 1809 | __do_request(mdsc, req); | ||
| 1810 | mutex_unlock(&mdsc->mutex); | ||
| 1811 | goto out; | ||
| 1812 | } | ||
| 1813 | } else { | ||
| 1814 | req->r_num_stale = 0; | ||
| 1815 | } | ||
| 1816 | |||
| 1817 | if (head->safe) { | ||
| 1818 | req->r_got_safe = true; | ||
| 1819 | __unregister_request(mdsc, req); | ||
| 1820 | complete(&req->r_safe_completion); | ||
| 1821 | |||
| 1822 | if (req->r_got_unsafe) { | ||
| 1823 | /* | ||
| 1824 | * We already handled the unsafe response, now do the | ||
| 1825 | * cleanup. No need to examine the response; the MDS | ||
| 1826 | * doesn't include any result info in the safe | ||
| 1827 | * response. And even if it did, there is nothing | ||
| 1828 | * useful we could do with a revised return value. | ||
| 1829 | */ | ||
| 1830 | dout("got safe reply %llu, mds%d\n", tid, mds); | ||
| 1831 | list_del_init(&req->r_unsafe_item); | ||
| 1832 | |||
| 1833 | /* last unsafe request during umount? */ | ||
| 1834 | if (mdsc->stopping && !__get_oldest_req(mdsc)) | ||
| 1835 | complete(&mdsc->safe_umount_waiters); | ||
| 1836 | mutex_unlock(&mdsc->mutex); | ||
| 1837 | goto out; | ||
| 1838 | } | ||
| 1839 | } | ||
| 1840 | |||
| 1841 | BUG_ON(req->r_reply); | ||
| 1842 | |||
| 1843 | if (!head->safe) { | ||
| 1844 | req->r_got_unsafe = true; | ||
| 1845 | list_add_tail(&req->r_unsafe_item, &req->r_session->s_unsafe); | ||
| 1846 | } | ||
| 1847 | |||
| 1848 | dout("handle_reply tid %lld result %d\n", tid, result); | ||
| 1849 | rinfo = &req->r_reply_info; | ||
| 1850 | err = parse_reply_info(msg, rinfo); | ||
| 1851 | mutex_unlock(&mdsc->mutex); | ||
| 1852 | |||
| 1853 | mutex_lock(&session->s_mutex); | ||
| 1854 | if (err < 0) { | ||
| 1855 | pr_err("mdsc_handle_reply got corrupt reply mds%d\n", mds); | ||
| 1856 | ceph_msg_dump(msg); | ||
| 1857 | goto out_err; | ||
| 1858 | } | ||
| 1859 | |||
| 1860 | /* snap trace */ | ||
| 1861 | if (rinfo->snapblob_len) { | ||
| 1862 | down_write(&mdsc->snap_rwsem); | ||
| 1863 | ceph_update_snap_trace(mdsc, rinfo->snapblob, | ||
| 1864 | rinfo->snapblob + rinfo->snapblob_len, | ||
| 1865 | le32_to_cpu(head->op) == CEPH_MDS_OP_RMSNAP); | ||
| 1866 | downgrade_write(&mdsc->snap_rwsem); | ||
| 1867 | } else { | ||
| 1868 | down_read(&mdsc->snap_rwsem); | ||
| 1869 | } | ||
| 1870 | |||
| 1871 | /* insert trace into our cache */ | ||
| 1872 | err = ceph_fill_trace(mdsc->client->sb, req, req->r_session); | ||
| 1873 | if (err == 0) { | ||
| 1874 | if (result == 0 && rinfo->dir_nr) | ||
| 1875 | ceph_readdir_prepopulate(req, req->r_session); | ||
| 1876 | ceph_unreserve_caps(&req->r_caps_reservation); | ||
| 1877 | } | ||
| 1878 | |||
| 1879 | up_read(&mdsc->snap_rwsem); | ||
| 1880 | out_err: | ||
| 1881 | if (err) { | ||
| 1882 | req->r_err = err; | ||
| 1883 | } else { | ||
| 1884 | req->r_reply = msg; | ||
| 1885 | ceph_msg_get(msg); | ||
| 1886 | } | ||
| 1887 | |||
| 1888 | add_cap_releases(mdsc, req->r_session, -1); | ||
| 1889 | mutex_unlock(&session->s_mutex); | ||
| 1890 | |||
| 1891 | /* kick calling process */ | ||
| 1892 | complete_request(mdsc, req); | ||
| 1893 | out: | ||
| 1894 | ceph_mdsc_put_request(req); | ||
| 1895 | return; | ||
| 1896 | } | ||
| 1897 | |||
| 1898 | |||
| 1899 | |||
| 1900 | /* | ||
| 1901 | * handle mds notification that our request has been forwarded. | ||
| 1902 | */ | ||
| 1903 | static void handle_forward(struct ceph_mds_client *mdsc, | ||
| 1904 | struct ceph_mds_session *session, | ||
| 1905 | struct ceph_msg *msg) | ||
| 1906 | { | ||
| 1907 | struct ceph_mds_request *req; | ||
| 1908 | u64 tid = le64_to_cpu(msg->hdr.tid); | ||
| 1909 | u32 next_mds; | ||
| 1910 | u32 fwd_seq; | ||
| 1911 | int err = -EINVAL; | ||
| 1912 | void *p = msg->front.iov_base; | ||
| 1913 | void *end = p + msg->front.iov_len; | ||
| 1914 | |||
| 1915 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); | ||
| 1916 | next_mds = ceph_decode_32(&p); | ||
| 1917 | fwd_seq = ceph_decode_32(&p); | ||
| 1918 | |||
| 1919 | mutex_lock(&mdsc->mutex); | ||
| 1920 | req = __lookup_request(mdsc, tid); | ||
| 1921 | if (!req) { | ||
| 1922 | dout("forward %llu to mds%d - req dne\n", tid, next_mds); | ||
| 1923 | goto out; /* dup reply? */ | ||
| 1924 | } | ||
| 1925 | |||
| 1926 | if (fwd_seq <= req->r_num_fwd) { | ||
| 1927 | dout("forward %llu to mds%d - old seq %d <= %d\n", | ||
| 1928 | tid, next_mds, req->r_num_fwd, fwd_seq); | ||
| 1929 | } else { | ||
| 1930 | /* resend. forward race not possible; mds would drop */ | ||
| 1931 | dout("forward %llu to mds%d (we resend)\n", tid, next_mds); | ||
| 1932 | req->r_num_fwd = fwd_seq; | ||
| 1933 | req->r_resend_mds = next_mds; | ||
| 1934 | put_request_session(req); | ||
| 1935 | __do_request(mdsc, req); | ||
| 1936 | } | ||
| 1937 | ceph_mdsc_put_request(req); | ||
| 1938 | out: | ||
| 1939 | mutex_unlock(&mdsc->mutex); | ||
| 1940 | return; | ||
| 1941 | |||
| 1942 | bad: | ||
| 1943 | pr_err("mdsc_handle_forward decode error err=%d\n", err); | ||
| 1944 | } | ||
| 1945 | |||
| 1946 | /* | ||
| 1947 | * handle a mds session control message | ||
| 1948 | */ | ||
| 1949 | static void handle_session(struct ceph_mds_session *session, | ||
| 1950 | struct ceph_msg *msg) | ||
| 1951 | { | ||
| 1952 | struct ceph_mds_client *mdsc = session->s_mdsc; | ||
| 1953 | u32 op; | ||
| 1954 | u64 seq; | ||
| 1955 | int mds = session->s_mds; | ||
| 1956 | struct ceph_mds_session_head *h = msg->front.iov_base; | ||
| 1957 | int wake = 0; | ||
| 1958 | |||
| 1959 | /* decode */ | ||
| 1960 | if (msg->front.iov_len != sizeof(*h)) | ||
| 1961 | goto bad; | ||
| 1962 | op = le32_to_cpu(h->op); | ||
| 1963 | seq = le64_to_cpu(h->seq); | ||
| 1964 | |||
| 1965 | mutex_lock(&mdsc->mutex); | ||
| 1966 | if (op == CEPH_SESSION_CLOSE) | ||
| 1967 | __unregister_session(mdsc, session); | ||
| 1968 | /* FIXME: this ttl calculation is generous */ | ||
| 1969 | session->s_ttl = jiffies + HZ*mdsc->mdsmap->m_session_autoclose; | ||
| 1970 | mutex_unlock(&mdsc->mutex); | ||
| 1971 | |||
| 1972 | mutex_lock(&session->s_mutex); | ||
| 1973 | |||
| 1974 | dout("handle_session mds%d %s %p state %s seq %llu\n", | ||
| 1975 | mds, ceph_session_op_name(op), session, | ||
| 1976 | session_state_name(session->s_state), seq); | ||
| 1977 | |||
| 1978 | if (session->s_state == CEPH_MDS_SESSION_HUNG) { | ||
| 1979 | session->s_state = CEPH_MDS_SESSION_OPEN; | ||
| 1980 | pr_info("mds%d came back\n", session->s_mds); | ||
| 1981 | } | ||
| 1982 | |||
| 1983 | switch (op) { | ||
| 1984 | case CEPH_SESSION_OPEN: | ||
| 1985 | session->s_state = CEPH_MDS_SESSION_OPEN; | ||
| 1986 | renewed_caps(mdsc, session, 0); | ||
| 1987 | wake = 1; | ||
| 1988 | if (mdsc->stopping) | ||
| 1989 | __close_session(mdsc, session); | ||
| 1990 | break; | ||
| 1991 | |||
| 1992 | case CEPH_SESSION_RENEWCAPS: | ||
| 1993 | if (session->s_renew_seq == seq) | ||
| 1994 | renewed_caps(mdsc, session, 1); | ||
| 1995 | break; | ||
| 1996 | |||
| 1997 | case CEPH_SESSION_CLOSE: | ||
| 1998 | remove_session_caps(session); | ||
| 1999 | wake = 1; /* for good measure */ | ||
| 2000 | complete(&mdsc->session_close_waiters); | ||
| 2001 | kick_requests(mdsc, mds, 0); /* cur only */ | ||
| 2002 | break; | ||
| 2003 | |||
| 2004 | case CEPH_SESSION_STALE: | ||
| 2005 | pr_info("mds%d caps went stale, renewing\n", | ||
| 2006 | session->s_mds); | ||
| 2007 | spin_lock(&session->s_cap_lock); | ||
| 2008 | session->s_cap_gen++; | ||
| 2009 | session->s_cap_ttl = 0; | ||
| 2010 | spin_unlock(&session->s_cap_lock); | ||
| 2011 | send_renew_caps(mdsc, session); | ||
| 2012 | break; | ||
| 2013 | |||
| 2014 | case CEPH_SESSION_RECALL_STATE: | ||
| 2015 | trim_caps(mdsc, session, le32_to_cpu(h->max_caps)); | ||
| 2016 | break; | ||
| 2017 | |||
| 2018 | default: | ||
| 2019 | pr_err("mdsc_handle_session bad op %d mds%d\n", op, mds); | ||
| 2020 | WARN_ON(1); | ||
| 2021 | } | ||
| 2022 | |||
| 2023 | mutex_unlock(&session->s_mutex); | ||
| 2024 | if (wake) { | ||
| 2025 | mutex_lock(&mdsc->mutex); | ||
| 2026 | __wake_requests(mdsc, &session->s_waiting); | ||
| 2027 | mutex_unlock(&mdsc->mutex); | ||
| 2028 | } | ||
| 2029 | return; | ||
| 2030 | |||
| 2031 | bad: | ||
| 2032 | pr_err("mdsc_handle_session corrupt message mds%d len %d\n", mds, | ||
| 2033 | (int)msg->front.iov_len); | ||
| 2034 | ceph_msg_dump(msg); | ||
| 2035 | return; | ||
| 2036 | } | ||
| 2037 | |||
| 2038 | |||
| 2039 | /* | ||
| 2040 | * called under session->mutex. | ||
| 2041 | */ | ||
| 2042 | static void replay_unsafe_requests(struct ceph_mds_client *mdsc, | ||
| 2043 | struct ceph_mds_session *session) | ||
| 2044 | { | ||
| 2045 | struct ceph_mds_request *req, *nreq; | ||
| 2046 | int err; | ||
| 2047 | |||
| 2048 | dout("replay_unsafe_requests mds%d\n", session->s_mds); | ||
| 2049 | |||
| 2050 | mutex_lock(&mdsc->mutex); | ||
| 2051 | list_for_each_entry_safe(req, nreq, &session->s_unsafe, r_unsafe_item) { | ||
| 2052 | err = __prepare_send_request(mdsc, req, session->s_mds); | ||
| 2053 | if (!err) { | ||
| 2054 | ceph_msg_get(req->r_request); | ||
| 2055 | ceph_con_send(&session->s_con, req->r_request); | ||
| 2056 | } | ||
| 2057 | } | ||
| 2058 | mutex_unlock(&mdsc->mutex); | ||
| 2059 | } | ||
| 2060 | |||
| 2061 | /* | ||
| 2062 | * Encode information about a cap for a reconnect with the MDS. | ||
| 2063 | */ | ||
| 2064 | static int encode_caps_cb(struct inode *inode, struct ceph_cap *cap, | ||
| 2065 | void *arg) | ||
| 2066 | { | ||
| 2067 | struct ceph_mds_cap_reconnect rec; | ||
| 2068 | struct ceph_inode_info *ci; | ||
| 2069 | struct ceph_pagelist *pagelist = arg; | ||
| 2070 | char *path; | ||
| 2071 | int pathlen, err; | ||
| 2072 | u64 pathbase; | ||
| 2073 | struct dentry *dentry; | ||
| 2074 | |||
| 2075 | ci = cap->ci; | ||
| 2076 | |||
| 2077 | dout(" adding %p ino %llx.%llx cap %p %lld %s\n", | ||
| 2078 | inode, ceph_vinop(inode), cap, cap->cap_id, | ||
| 2079 | ceph_cap_string(cap->issued)); | ||
| 2080 | err = ceph_pagelist_encode_64(pagelist, ceph_ino(inode)); | ||
| 2081 | if (err) | ||
| 2082 | return err; | ||
| 2083 | |||
| 2084 | dentry = d_find_alias(inode); | ||
| 2085 | if (dentry) { | ||
| 2086 | path = ceph_mdsc_build_path(dentry, &pathlen, &pathbase, 0); | ||
| 2087 | if (IS_ERR(path)) { | ||
| 2088 | err = PTR_ERR(path); | ||
| 2089 | BUG_ON(err); | ||
| 2090 | } | ||
| 2091 | } else { | ||
| 2092 | path = NULL; | ||
| 2093 | pathlen = 0; | ||
| 2094 | } | ||
| 2095 | err = ceph_pagelist_encode_string(pagelist, path, pathlen); | ||
| 2096 | if (err) | ||
| 2097 | goto out; | ||
| 2098 | |||
| 2099 | spin_lock(&inode->i_lock); | ||
| 2100 | cap->seq = 0; /* reset cap seq */ | ||
| 2101 | cap->issue_seq = 0; /* and issue_seq */ | ||
| 2102 | rec.cap_id = cpu_to_le64(cap->cap_id); | ||
| 2103 | rec.pathbase = cpu_to_le64(pathbase); | ||
| 2104 | rec.wanted = cpu_to_le32(__ceph_caps_wanted(ci)); | ||
| 2105 | rec.issued = cpu_to_le32(cap->issued); | ||
| 2106 | rec.size = cpu_to_le64(inode->i_size); | ||
| 2107 | ceph_encode_timespec(&rec.mtime, &inode->i_mtime); | ||
| 2108 | ceph_encode_timespec(&rec.atime, &inode->i_atime); | ||
| 2109 | rec.snaprealm = cpu_to_le64(ci->i_snap_realm->ino); | ||
| 2110 | spin_unlock(&inode->i_lock); | ||
| 2111 | |||
| 2112 | err = ceph_pagelist_append(pagelist, &rec, sizeof(rec)); | ||
| 2113 | |||
| 2114 | out: | ||
| 2115 | kfree(path); | ||
| 2116 | dput(dentry); | ||
| 2117 | return err; | ||
| 2118 | } | ||
| 2119 | |||
| 2120 | |||
| 2121 | /* | ||
| 2122 | * If an MDS fails and recovers, clients need to reconnect in order to | ||
| 2123 | * reestablish shared state. This includes all caps issued through | ||
| 2124 | * this session _and_ the snap_realm hierarchy. Because it's not | ||
| 2125 | * clear which snap realms the mds cares about, we send everything we | ||
| 2126 | * know about.. that ensures we'll then get any new info the | ||
| 2127 | * recovering MDS might have. | ||
| 2128 | * | ||
| 2129 | * This is a relatively heavyweight operation, but it's rare. | ||
| 2130 | * | ||
| 2131 | * called with mdsc->mutex held. | ||
| 2132 | */ | ||
| 2133 | static void send_mds_reconnect(struct ceph_mds_client *mdsc, int mds) | ||
| 2134 | { | ||
| 2135 | struct ceph_mds_session *session = NULL; | ||
| 2136 | struct ceph_msg *reply; | ||
| 2137 | struct rb_node *p; | ||
| 2138 | int err; | ||
| 2139 | struct ceph_pagelist *pagelist; | ||
| 2140 | |||
| 2141 | pr_info("reconnect to recovering mds%d\n", mds); | ||
| 2142 | |||
| 2143 | pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS); | ||
| 2144 | if (!pagelist) | ||
| 2145 | goto fail_nopagelist; | ||
| 2146 | ceph_pagelist_init(pagelist); | ||
| 2147 | |||
| 2148 | reply = ceph_msg_new(CEPH_MSG_CLIENT_RECONNECT, 0, 0, 0, NULL); | ||
| 2149 | if (IS_ERR(reply)) { | ||
| 2150 | err = PTR_ERR(reply); | ||
| 2151 | goto fail_nomsg; | ||
| 2152 | } | ||
| 2153 | |||
| 2154 | /* find session */ | ||
| 2155 | session = __ceph_lookup_mds_session(mdsc, mds); | ||
| 2156 | mutex_unlock(&mdsc->mutex); /* drop lock for duration */ | ||
| 2157 | |||
| 2158 | if (session) { | ||
| 2159 | mutex_lock(&session->s_mutex); | ||
| 2160 | |||
| 2161 | session->s_state = CEPH_MDS_SESSION_RECONNECTING; | ||
| 2162 | session->s_seq = 0; | ||
| 2163 | |||
| 2164 | ceph_con_open(&session->s_con, | ||
| 2165 | ceph_mdsmap_get_addr(mdsc->mdsmap, mds)); | ||
| 2166 | |||
| 2167 | /* replay unsafe requests */ | ||
| 2168 | replay_unsafe_requests(mdsc, session); | ||
| 2169 | } else { | ||
| 2170 | dout("no session for mds%d, will send short reconnect\n", | ||
| 2171 | mds); | ||
| 2172 | } | ||
| 2173 | |||
| 2174 | down_read(&mdsc->snap_rwsem); | ||
| 2175 | |||
| 2176 | if (!session) | ||
| 2177 | goto send; | ||
| 2178 | dout("session %p state %s\n", session, | ||
| 2179 | session_state_name(session->s_state)); | ||
| 2180 | |||
| 2181 | /* traverse this session's caps */ | ||
| 2182 | err = ceph_pagelist_encode_32(pagelist, session->s_nr_caps); | ||
| 2183 | if (err) | ||
| 2184 | goto fail; | ||
| 2185 | err = iterate_session_caps(session, encode_caps_cb, pagelist); | ||
| 2186 | if (err < 0) | ||
| 2187 | goto out; | ||
| 2188 | |||
| 2189 | /* | ||
| 2190 | * snaprealms. we provide mds with the ino, seq (version), and | ||
| 2191 | * parent for all of our realms. If the mds has any newer info, | ||
| 2192 | * it will tell us. | ||
| 2193 | */ | ||
| 2194 | for (p = rb_first(&mdsc->snap_realms); p; p = rb_next(p)) { | ||
| 2195 | struct ceph_snap_realm *realm = | ||
| 2196 | rb_entry(p, struct ceph_snap_realm, node); | ||
| 2197 | struct ceph_mds_snaprealm_reconnect sr_rec; | ||
| 2198 | |||
| 2199 | dout(" adding snap realm %llx seq %lld parent %llx\n", | ||
| 2200 | realm->ino, realm->seq, realm->parent_ino); | ||
| 2201 | sr_rec.ino = cpu_to_le64(realm->ino); | ||
| 2202 | sr_rec.seq = cpu_to_le64(realm->seq); | ||
| 2203 | sr_rec.parent = cpu_to_le64(realm->parent_ino); | ||
| 2204 | err = ceph_pagelist_append(pagelist, &sr_rec, sizeof(sr_rec)); | ||
| 2205 | if (err) | ||
| 2206 | goto fail; | ||
| 2207 | } | ||
| 2208 | |||
| 2209 | send: | ||
| 2210 | reply->pagelist = pagelist; | ||
| 2211 | reply->hdr.data_len = cpu_to_le32(pagelist->length); | ||
| 2212 | reply->nr_pages = calc_pages_for(0, pagelist->length); | ||
| 2213 | ceph_con_send(&session->s_con, reply); | ||
| 2214 | |||
| 2215 | if (session) { | ||
| 2216 | session->s_state = CEPH_MDS_SESSION_OPEN; | ||
| 2217 | __wake_requests(mdsc, &session->s_waiting); | ||
| 2218 | } | ||
| 2219 | |||
| 2220 | out: | ||
| 2221 | up_read(&mdsc->snap_rwsem); | ||
| 2222 | if (session) { | ||
| 2223 | mutex_unlock(&session->s_mutex); | ||
| 2224 | ceph_put_mds_session(session); | ||
| 2225 | } | ||
| 2226 | mutex_lock(&mdsc->mutex); | ||
| 2227 | return; | ||
| 2228 | |||
| 2229 | fail: | ||
| 2230 | ceph_msg_put(reply); | ||
| 2231 | fail_nomsg: | ||
| 2232 | ceph_pagelist_release(pagelist); | ||
| 2233 | kfree(pagelist); | ||
| 2234 | fail_nopagelist: | ||
| 2235 | pr_err("ENOMEM preparing reconnect for mds%d\n", mds); | ||
| 2236 | goto out; | ||
| 2237 | } | ||
| 2238 | |||
| 2239 | |||
| 2240 | /* | ||
| 2241 | * compare old and new mdsmaps, kicking requests | ||
| 2242 | * and closing out old connections as necessary | ||
| 2243 | * | ||
| 2244 | * called under mdsc->mutex. | ||
| 2245 | */ | ||
| 2246 | static void check_new_map(struct ceph_mds_client *mdsc, | ||
| 2247 | struct ceph_mdsmap *newmap, | ||
| 2248 | struct ceph_mdsmap *oldmap) | ||
| 2249 | { | ||
| 2250 | int i; | ||
| 2251 | int oldstate, newstate; | ||
| 2252 | struct ceph_mds_session *s; | ||
| 2253 | |||
| 2254 | dout("check_new_map new %u old %u\n", | ||
| 2255 | newmap->m_epoch, oldmap->m_epoch); | ||
| 2256 | |||
| 2257 | for (i = 0; i < oldmap->m_max_mds && i < mdsc->max_sessions; i++) { | ||
| 2258 | if (mdsc->sessions[i] == NULL) | ||
| 2259 | continue; | ||
| 2260 | s = mdsc->sessions[i]; | ||
| 2261 | oldstate = ceph_mdsmap_get_state(oldmap, i); | ||
| 2262 | newstate = ceph_mdsmap_get_state(newmap, i); | ||
| 2263 | |||
| 2264 | dout("check_new_map mds%d state %s -> %s (session %s)\n", | ||
| 2265 | i, ceph_mds_state_name(oldstate), | ||
| 2266 | ceph_mds_state_name(newstate), | ||
| 2267 | session_state_name(s->s_state)); | ||
| 2268 | |||
| 2269 | if (memcmp(ceph_mdsmap_get_addr(oldmap, i), | ||
| 2270 | ceph_mdsmap_get_addr(newmap, i), | ||
| 2271 | sizeof(struct ceph_entity_addr))) { | ||
| 2272 | if (s->s_state == CEPH_MDS_SESSION_OPENING) { | ||
| 2273 | /* the session never opened, just close it | ||
| 2274 | * out now */ | ||
| 2275 | __wake_requests(mdsc, &s->s_waiting); | ||
| 2276 | __unregister_session(mdsc, s); | ||
| 2277 | } else { | ||
| 2278 | /* just close it */ | ||
| 2279 | mutex_unlock(&mdsc->mutex); | ||
| 2280 | mutex_lock(&s->s_mutex); | ||
| 2281 | mutex_lock(&mdsc->mutex); | ||
| 2282 | ceph_con_close(&s->s_con); | ||
| 2283 | mutex_unlock(&s->s_mutex); | ||
| 2284 | s->s_state = CEPH_MDS_SESSION_RESTARTING; | ||
| 2285 | } | ||
| 2286 | |||
| 2287 | /* kick any requests waiting on the recovering mds */ | ||
| 2288 | kick_requests(mdsc, i, 1); | ||
| 2289 | } else if (oldstate == newstate) { | ||
| 2290 | continue; /* nothing new with this mds */ | ||
| 2291 | } | ||
| 2292 | |||
| 2293 | /* | ||
| 2294 | * send reconnect? | ||
| 2295 | */ | ||
| 2296 | if (s->s_state == CEPH_MDS_SESSION_RESTARTING && | ||
| 2297 | newstate >= CEPH_MDS_STATE_RECONNECT) | ||
| 2298 | send_mds_reconnect(mdsc, i); | ||
| 2299 | |||
| 2300 | /* | ||
| 2301 | * kick requests on any mds that has gone active. | ||
| 2302 | * | ||
| 2303 | * kick requests on cur or forwarder: we may have sent | ||
| 2304 | * the request to mds1, mds1 told us it forwarded it | ||
| 2305 | * to mds2, but then we learn mds1 failed and can't be | ||
| 2306 | * sure it successfully forwarded our request before | ||
| 2307 | * it died. | ||
| 2308 | */ | ||
| 2309 | if (oldstate < CEPH_MDS_STATE_ACTIVE && | ||
| 2310 | newstate >= CEPH_MDS_STATE_ACTIVE) { | ||
| 2311 | pr_info("mds%d reconnect completed\n", s->s_mds); | ||
| 2312 | kick_requests(mdsc, i, 1); | ||
| 2313 | ceph_kick_flushing_caps(mdsc, s); | ||
| 2314 | wake_up_session_caps(s, 1); | ||
| 2315 | } | ||
| 2316 | } | ||
| 2317 | } | ||
| 2318 | |||
| 2319 | |||
| 2320 | |||
| 2321 | /* | ||
| 2322 | * leases | ||
| 2323 | */ | ||
| 2324 | |||
| 2325 | /* | ||
| 2326 | * caller must hold session s_mutex, dentry->d_lock | ||
| 2327 | */ | ||
| 2328 | void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry) | ||
| 2329 | { | ||
| 2330 | struct ceph_dentry_info *di = ceph_dentry(dentry); | ||
| 2331 | |||
| 2332 | ceph_put_mds_session(di->lease_session); | ||
| 2333 | di->lease_session = NULL; | ||
| 2334 | } | ||
| 2335 | |||
| 2336 | static void handle_lease(struct ceph_mds_client *mdsc, | ||
| 2337 | struct ceph_mds_session *session, | ||
| 2338 | struct ceph_msg *msg) | ||
| 2339 | { | ||
| 2340 | struct super_block *sb = mdsc->client->sb; | ||
| 2341 | struct inode *inode; | ||
| 2342 | struct ceph_inode_info *ci; | ||
| 2343 | struct dentry *parent, *dentry; | ||
| 2344 | struct ceph_dentry_info *di; | ||
| 2345 | int mds = session->s_mds; | ||
| 2346 | struct ceph_mds_lease *h = msg->front.iov_base; | ||
| 2347 | struct ceph_vino vino; | ||
| 2348 | int mask; | ||
| 2349 | struct qstr dname; | ||
| 2350 | int release = 0; | ||
| 2351 | |||
| 2352 | dout("handle_lease from mds%d\n", mds); | ||
| 2353 | |||
| 2354 | /* decode */ | ||
| 2355 | if (msg->front.iov_len < sizeof(*h) + sizeof(u32)) | ||
| 2356 | goto bad; | ||
| 2357 | vino.ino = le64_to_cpu(h->ino); | ||
| 2358 | vino.snap = CEPH_NOSNAP; | ||
| 2359 | mask = le16_to_cpu(h->mask); | ||
| 2360 | dname.name = (void *)h + sizeof(*h) + sizeof(u32); | ||
| 2361 | dname.len = msg->front.iov_len - sizeof(*h) - sizeof(u32); | ||
| 2362 | if (dname.len != get_unaligned_le32(h+1)) | ||
| 2363 | goto bad; | ||
| 2364 | |||
| 2365 | mutex_lock(&session->s_mutex); | ||
| 2366 | session->s_seq++; | ||
| 2367 | |||
| 2368 | /* lookup inode */ | ||
| 2369 | inode = ceph_find_inode(sb, vino); | ||
| 2370 | dout("handle_lease '%s', mask %d, ino %llx %p\n", | ||
| 2371 | ceph_lease_op_name(h->action), mask, vino.ino, inode); | ||
| 2372 | if (inode == NULL) { | ||
| 2373 | dout("handle_lease no inode %llx\n", vino.ino); | ||
| 2374 | goto release; | ||
| 2375 | } | ||
| 2376 | ci = ceph_inode(inode); | ||
| 2377 | |||
| 2378 | /* dentry */ | ||
| 2379 | parent = d_find_alias(inode); | ||
| 2380 | if (!parent) { | ||
| 2381 | dout("no parent dentry on inode %p\n", inode); | ||
| 2382 | WARN_ON(1); | ||
| 2383 | goto release; /* hrm... */ | ||
| 2384 | } | ||
| 2385 | dname.hash = full_name_hash(dname.name, dname.len); | ||
| 2386 | dentry = d_lookup(parent, &dname); | ||
| 2387 | dput(parent); | ||
| 2388 | if (!dentry) | ||
| 2389 | goto release; | ||
| 2390 | |||
| 2391 | spin_lock(&dentry->d_lock); | ||
| 2392 | di = ceph_dentry(dentry); | ||
| 2393 | switch (h->action) { | ||
| 2394 | case CEPH_MDS_LEASE_REVOKE: | ||
| 2395 | if (di && di->lease_session == session) { | ||
| 2396 | h->seq = cpu_to_le32(di->lease_seq); | ||
| 2397 | __ceph_mdsc_drop_dentry_lease(dentry); | ||
| 2398 | } | ||
| 2399 | release = 1; | ||
| 2400 | break; | ||
| 2401 | |||
| 2402 | case CEPH_MDS_LEASE_RENEW: | ||
| 2403 | if (di && di->lease_session == session && | ||
| 2404 | di->lease_gen == session->s_cap_gen && | ||
| 2405 | di->lease_renew_from && | ||
| 2406 | di->lease_renew_after == 0) { | ||
| 2407 | unsigned long duration = | ||
| 2408 | le32_to_cpu(h->duration_ms) * HZ / 1000; | ||
| 2409 | |||
| 2410 | di->lease_seq = le32_to_cpu(h->seq); | ||
| 2411 | dentry->d_time = di->lease_renew_from + duration; | ||
| 2412 | di->lease_renew_after = di->lease_renew_from + | ||
| 2413 | (duration >> 1); | ||
| 2414 | di->lease_renew_from = 0; | ||
| 2415 | } | ||
| 2416 | break; | ||
| 2417 | } | ||
| 2418 | spin_unlock(&dentry->d_lock); | ||
| 2419 | dput(dentry); | ||
| 2420 | |||
| 2421 | if (!release) | ||
| 2422 | goto out; | ||
| 2423 | |||
| 2424 | release: | ||
| 2425 | /* let's just reuse the same message */ | ||
| 2426 | h->action = CEPH_MDS_LEASE_REVOKE_ACK; | ||
| 2427 | ceph_msg_get(msg); | ||
| 2428 | ceph_con_send(&session->s_con, msg); | ||
| 2429 | |||
| 2430 | out: | ||
| 2431 | iput(inode); | ||
| 2432 | mutex_unlock(&session->s_mutex); | ||
| 2433 | return; | ||
| 2434 | |||
| 2435 | bad: | ||
| 2436 | pr_err("corrupt lease message\n"); | ||
| 2437 | ceph_msg_dump(msg); | ||
| 2438 | } | ||
| 2439 | |||
| 2440 | void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session, | ||
| 2441 | struct inode *inode, | ||
| 2442 | struct dentry *dentry, char action, | ||
| 2443 | u32 seq) | ||
| 2444 | { | ||
| 2445 | struct ceph_msg *msg; | ||
| 2446 | struct ceph_mds_lease *lease; | ||
| 2447 | int len = sizeof(*lease) + sizeof(u32); | ||
| 2448 | int dnamelen = 0; | ||
| 2449 | |||
| 2450 | dout("lease_send_msg inode %p dentry %p %s to mds%d\n", | ||
| 2451 | inode, dentry, ceph_lease_op_name(action), session->s_mds); | ||
| 2452 | dnamelen = dentry->d_name.len; | ||
| 2453 | len += dnamelen; | ||
| 2454 | |||
| 2455 | msg = ceph_msg_new(CEPH_MSG_CLIENT_LEASE, len, 0, 0, NULL); | ||
| 2456 | if (IS_ERR(msg)) | ||
| 2457 | return; | ||
| 2458 | lease = msg->front.iov_base; | ||
| 2459 | lease->action = action; | ||
| 2460 | lease->mask = cpu_to_le16(CEPH_LOCK_DN); | ||
| 2461 | lease->ino = cpu_to_le64(ceph_vino(inode).ino); | ||
| 2462 | lease->first = lease->last = cpu_to_le64(ceph_vino(inode).snap); | ||
| 2463 | lease->seq = cpu_to_le32(seq); | ||
| 2464 | put_unaligned_le32(dnamelen, lease + 1); | ||
| 2465 | memcpy((void *)(lease + 1) + 4, dentry->d_name.name, dnamelen); | ||
| 2466 | |||
| 2467 | /* | ||
| 2468 | * if this is a preemptive lease RELEASE, no need to | ||
| 2469 | * flush request stream, since the actual request will | ||
| 2470 | * soon follow. | ||
| 2471 | */ | ||
| 2472 | msg->more_to_follow = (action == CEPH_MDS_LEASE_RELEASE); | ||
| 2473 | |||
| 2474 | ceph_con_send(&session->s_con, msg); | ||
| 2475 | } | ||
| 2476 | |||
| 2477 | /* | ||
| 2478 | * Preemptively release a lease we expect to invalidate anyway. | ||
| 2479 | * Pass @inode always, @dentry is optional. | ||
| 2480 | */ | ||
| 2481 | void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, struct inode *inode, | ||
| 2482 | struct dentry *dentry, int mask) | ||
| 2483 | { | ||
| 2484 | struct ceph_dentry_info *di; | ||
| 2485 | struct ceph_mds_session *session; | ||
| 2486 | u32 seq; | ||
| 2487 | |||
| 2488 | BUG_ON(inode == NULL); | ||
| 2489 | BUG_ON(dentry == NULL); | ||
| 2490 | BUG_ON(mask != CEPH_LOCK_DN); | ||
| 2491 | |||
| 2492 | /* is dentry lease valid? */ | ||
| 2493 | spin_lock(&dentry->d_lock); | ||
| 2494 | di = ceph_dentry(dentry); | ||
| 2495 | if (!di || !di->lease_session || | ||
| 2496 | di->lease_session->s_mds < 0 || | ||
| 2497 | di->lease_gen != di->lease_session->s_cap_gen || | ||
| 2498 | !time_before(jiffies, dentry->d_time)) { | ||
| 2499 | dout("lease_release inode %p dentry %p -- " | ||
| 2500 | "no lease on %d\n", | ||
| 2501 | inode, dentry, mask); | ||
| 2502 | spin_unlock(&dentry->d_lock); | ||
| 2503 | return; | ||
| 2504 | } | ||
| 2505 | |||
| 2506 | /* we do have a lease on this dentry; note mds and seq */ | ||
| 2507 | session = ceph_get_mds_session(di->lease_session); | ||
| 2508 | seq = di->lease_seq; | ||
| 2509 | __ceph_mdsc_drop_dentry_lease(dentry); | ||
| 2510 | spin_unlock(&dentry->d_lock); | ||
| 2511 | |||
| 2512 | dout("lease_release inode %p dentry %p mask %d to mds%d\n", | ||
| 2513 | inode, dentry, mask, session->s_mds); | ||
| 2514 | ceph_mdsc_lease_send_msg(session, inode, dentry, | ||
| 2515 | CEPH_MDS_LEASE_RELEASE, seq); | ||
| 2516 | ceph_put_mds_session(session); | ||
| 2517 | } | ||
| 2518 | |||
| 2519 | /* | ||
| 2520 | * drop all leases (and dentry refs) in preparation for umount | ||
| 2521 | */ | ||
| 2522 | static void drop_leases(struct ceph_mds_client *mdsc) | ||
| 2523 | { | ||
| 2524 | int i; | ||
| 2525 | |||
| 2526 | dout("drop_leases\n"); | ||
| 2527 | mutex_lock(&mdsc->mutex); | ||
| 2528 | for (i = 0; i < mdsc->max_sessions; i++) { | ||
| 2529 | struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i); | ||
| 2530 | if (!s) | ||
| 2531 | continue; | ||
| 2532 | mutex_unlock(&mdsc->mutex); | ||
| 2533 | mutex_lock(&s->s_mutex); | ||
| 2534 | mutex_unlock(&s->s_mutex); | ||
| 2535 | ceph_put_mds_session(s); | ||
| 2536 | mutex_lock(&mdsc->mutex); | ||
| 2537 | } | ||
| 2538 | mutex_unlock(&mdsc->mutex); | ||
| 2539 | } | ||
| 2540 | |||
| 2541 | |||
| 2542 | |||
| 2543 | /* | ||
| 2544 | * delayed work -- periodically trim expired leases, renew caps with mds | ||
| 2545 | */ | ||
| 2546 | static void schedule_delayed(struct ceph_mds_client *mdsc) | ||
| 2547 | { | ||
| 2548 | int delay = 5; | ||
| 2549 | unsigned hz = round_jiffies_relative(HZ * delay); | ||
| 2550 | schedule_delayed_work(&mdsc->delayed_work, hz); | ||
| 2551 | } | ||
| 2552 | |||
| 2553 | static void delayed_work(struct work_struct *work) | ||
| 2554 | { | ||
| 2555 | int i; | ||
| 2556 | struct ceph_mds_client *mdsc = | ||
| 2557 | container_of(work, struct ceph_mds_client, delayed_work.work); | ||
| 2558 | int renew_interval; | ||
| 2559 | int renew_caps; | ||
| 2560 | |||
| 2561 | dout("mdsc delayed_work\n"); | ||
| 2562 | ceph_check_delayed_caps(mdsc); | ||
| 2563 | |||
| 2564 | mutex_lock(&mdsc->mutex); | ||
| 2565 | renew_interval = mdsc->mdsmap->m_session_timeout >> 2; | ||
| 2566 | renew_caps = time_after_eq(jiffies, HZ*renew_interval + | ||
| 2567 | mdsc->last_renew_caps); | ||
| 2568 | if (renew_caps) | ||
| 2569 | mdsc->last_renew_caps = jiffies; | ||
| 2570 | |||
| 2571 | for (i = 0; i < mdsc->max_sessions; i++) { | ||
| 2572 | struct ceph_mds_session *s = __ceph_lookup_mds_session(mdsc, i); | ||
| 2573 | if (s == NULL) | ||
| 2574 | continue; | ||
| 2575 | if (s->s_state == CEPH_MDS_SESSION_CLOSING) { | ||
| 2576 | dout("resending session close request for mds%d\n", | ||
| 2577 | s->s_mds); | ||
| 2578 | request_close_session(mdsc, s); | ||
| 2579 | ceph_put_mds_session(s); | ||
| 2580 | continue; | ||
| 2581 | } | ||
| 2582 | if (s->s_ttl && time_after(jiffies, s->s_ttl)) { | ||
| 2583 | if (s->s_state == CEPH_MDS_SESSION_OPEN) { | ||
| 2584 | s->s_state = CEPH_MDS_SESSION_HUNG; | ||
| 2585 | pr_info("mds%d hung\n", s->s_mds); | ||
| 2586 | } | ||
| 2587 | } | ||
| 2588 | if (s->s_state < CEPH_MDS_SESSION_OPEN) { | ||
| 2589 | /* this mds is failed or recovering, just wait */ | ||
| 2590 | ceph_put_mds_session(s); | ||
| 2591 | continue; | ||
| 2592 | } | ||
| 2593 | mutex_unlock(&mdsc->mutex); | ||
| 2594 | |||
| 2595 | mutex_lock(&s->s_mutex); | ||
| 2596 | if (renew_caps) | ||
| 2597 | send_renew_caps(mdsc, s); | ||
| 2598 | else | ||
| 2599 | ceph_con_keepalive(&s->s_con); | ||
| 2600 | add_cap_releases(mdsc, s, -1); | ||
| 2601 | send_cap_releases(mdsc, s); | ||
| 2602 | mutex_unlock(&s->s_mutex); | ||
| 2603 | ceph_put_mds_session(s); | ||
| 2604 | |||
| 2605 | mutex_lock(&mdsc->mutex); | ||
| 2606 | } | ||
| 2607 | mutex_unlock(&mdsc->mutex); | ||
| 2608 | |||
| 2609 | schedule_delayed(mdsc); | ||
| 2610 | } | ||
| 2611 | |||
| 2612 | |||
| 2613 | int ceph_mdsc_init(struct ceph_mds_client *mdsc, struct ceph_client *client) | ||
| 2614 | { | ||
| 2615 | mdsc->client = client; | ||
| 2616 | mutex_init(&mdsc->mutex); | ||
| 2617 | mdsc->mdsmap = kzalloc(sizeof(*mdsc->mdsmap), GFP_NOFS); | ||
| 2618 | init_completion(&mdsc->safe_umount_waiters); | ||
| 2619 | init_completion(&mdsc->session_close_waiters); | ||
| 2620 | INIT_LIST_HEAD(&mdsc->waiting_for_map); | ||
| 2621 | mdsc->sessions = NULL; | ||
| 2622 | mdsc->max_sessions = 0; | ||
| 2623 | mdsc->stopping = 0; | ||
| 2624 | init_rwsem(&mdsc->snap_rwsem); | ||
| 2625 | mdsc->snap_realms = RB_ROOT; | ||
| 2626 | INIT_LIST_HEAD(&mdsc->snap_empty); | ||
| 2627 | spin_lock_init(&mdsc->snap_empty_lock); | ||
| 2628 | mdsc->last_tid = 0; | ||
| 2629 | mdsc->request_tree = RB_ROOT; | ||
| 2630 | INIT_DELAYED_WORK(&mdsc->delayed_work, delayed_work); | ||
| 2631 | mdsc->last_renew_caps = jiffies; | ||
| 2632 | INIT_LIST_HEAD(&mdsc->cap_delay_list); | ||
| 2633 | spin_lock_init(&mdsc->cap_delay_lock); | ||
| 2634 | INIT_LIST_HEAD(&mdsc->snap_flush_list); | ||
| 2635 | spin_lock_init(&mdsc->snap_flush_lock); | ||
| 2636 | mdsc->cap_flush_seq = 0; | ||
| 2637 | INIT_LIST_HEAD(&mdsc->cap_dirty); | ||
| 2638 | mdsc->num_cap_flushing = 0; | ||
| 2639 | spin_lock_init(&mdsc->cap_dirty_lock); | ||
| 2640 | init_waitqueue_head(&mdsc->cap_flushing_wq); | ||
| 2641 | spin_lock_init(&mdsc->dentry_lru_lock); | ||
| 2642 | INIT_LIST_HEAD(&mdsc->dentry_lru); | ||
| 2643 | return 0; | ||
| 2644 | } | ||
| 2645 | |||
| 2646 | /* | ||
| 2647 | * Wait for safe replies on open mds requests. If we time out, drop | ||
| 2648 | * all requests from the tree to avoid dangling dentry refs. | ||
| 2649 | */ | ||
| 2650 | static void wait_requests(struct ceph_mds_client *mdsc) | ||
| 2651 | { | ||
| 2652 | struct ceph_mds_request *req; | ||
| 2653 | struct ceph_client *client = mdsc->client; | ||
| 2654 | |||
| 2655 | mutex_lock(&mdsc->mutex); | ||
| 2656 | if (__get_oldest_req(mdsc)) { | ||
| 2657 | mutex_unlock(&mdsc->mutex); | ||
| 2658 | |||
| 2659 | dout("wait_requests waiting for requests\n"); | ||
| 2660 | wait_for_completion_timeout(&mdsc->safe_umount_waiters, | ||
| 2661 | client->mount_args->mount_timeout * HZ); | ||
| 2662 | |||
| 2663 | /* tear down remaining requests */ | ||
| 2664 | mutex_lock(&mdsc->mutex); | ||
| 2665 | while ((req = __get_oldest_req(mdsc))) { | ||
| 2666 | dout("wait_requests timed out on tid %llu\n", | ||
| 2667 | req->r_tid); | ||
| 2668 | __unregister_request(mdsc, req); | ||
| 2669 | } | ||
| 2670 | } | ||
| 2671 | mutex_unlock(&mdsc->mutex); | ||
| 2672 | dout("wait_requests done\n"); | ||
| 2673 | } | ||
| 2674 | |||
| 2675 | /* | ||
| 2676 | * called before mount is ro, and before dentries are torn down. | ||
| 2677 | * (hmm, does this still race with new lookups?) | ||
| 2678 | */ | ||
| 2679 | void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc) | ||
| 2680 | { | ||
| 2681 | dout("pre_umount\n"); | ||
| 2682 | mdsc->stopping = 1; | ||
| 2683 | |||
| 2684 | drop_leases(mdsc); | ||
| 2685 | ceph_flush_dirty_caps(mdsc); | ||
| 2686 | wait_requests(mdsc); | ||
| 2687 | } | ||
| 2688 | |||
| 2689 | /* | ||
| 2690 | * wait for all write mds requests to flush. | ||
| 2691 | */ | ||
| 2692 | static void wait_unsafe_requests(struct ceph_mds_client *mdsc, u64 want_tid) | ||
| 2693 | { | ||
| 2694 | struct ceph_mds_request *req = NULL, *nextreq; | ||
| 2695 | struct rb_node *n; | ||
| 2696 | |||
| 2697 | mutex_lock(&mdsc->mutex); | ||
| 2698 | dout("wait_unsafe_requests want %lld\n", want_tid); | ||
| 2699 | restart: | ||
| 2700 | req = __get_oldest_req(mdsc); | ||
| 2701 | while (req && req->r_tid <= want_tid) { | ||
| 2702 | /* find next request */ | ||
| 2703 | n = rb_next(&req->r_node); | ||
| 2704 | if (n) | ||
| 2705 | nextreq = rb_entry(n, struct ceph_mds_request, r_node); | ||
| 2706 | else | ||
| 2707 | nextreq = NULL; | ||
| 2708 | if ((req->r_op & CEPH_MDS_OP_WRITE)) { | ||
| 2709 | /* write op */ | ||
| 2710 | ceph_mdsc_get_request(req); | ||
| 2711 | if (nextreq) | ||
| 2712 | ceph_mdsc_get_request(nextreq); | ||
| 2713 | mutex_unlock(&mdsc->mutex); | ||
| 2714 | dout("wait_unsafe_requests wait on %llu (want %llu)\n", | ||
| 2715 | req->r_tid, want_tid); | ||
| 2716 | wait_for_completion(&req->r_safe_completion); | ||
| 2717 | mutex_lock(&mdsc->mutex); | ||
| 2718 | ceph_mdsc_put_request(req); | ||
| 2719 | if (!nextreq) | ||
| 2720 | break; /* next dne before, so we're done! */ | ||
| 2721 | if (RB_EMPTY_NODE(&nextreq->r_node)) { | ||
| 2722 | /* next request was removed from tree */ | ||
| 2723 | ceph_mdsc_put_request(nextreq); | ||
| 2724 | goto restart; | ||
| 2725 | } | ||
| 2726 | ceph_mdsc_put_request(nextreq); /* won't go away */ | ||
| 2727 | } | ||
| 2728 | req = nextreq; | ||
| 2729 | } | ||
| 2730 | mutex_unlock(&mdsc->mutex); | ||
| 2731 | dout("wait_unsafe_requests done\n"); | ||
| 2732 | } | ||
| 2733 | |||
| 2734 | void ceph_mdsc_sync(struct ceph_mds_client *mdsc) | ||
| 2735 | { | ||
| 2736 | u64 want_tid, want_flush; | ||
| 2737 | |||
| 2738 | dout("sync\n"); | ||
| 2739 | mutex_lock(&mdsc->mutex); | ||
| 2740 | want_tid = mdsc->last_tid; | ||
| 2741 | want_flush = mdsc->cap_flush_seq; | ||
| 2742 | mutex_unlock(&mdsc->mutex); | ||
| 2743 | dout("sync want tid %lld flush_seq %lld\n", want_tid, want_flush); | ||
| 2744 | |||
| 2745 | ceph_flush_dirty_caps(mdsc); | ||
| 2746 | |||
| 2747 | wait_unsafe_requests(mdsc, want_tid); | ||
| 2748 | wait_event(mdsc->cap_flushing_wq, check_cap_flush(mdsc, want_flush)); | ||
| 2749 | } | ||
| 2750 | |||
| 2751 | |||
| 2752 | /* | ||
| 2753 | * called after sb is ro. | ||
| 2754 | */ | ||
| 2755 | void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc) | ||
| 2756 | { | ||
| 2757 | struct ceph_mds_session *session; | ||
| 2758 | int i; | ||
| 2759 | int n; | ||
| 2760 | struct ceph_client *client = mdsc->client; | ||
| 2761 | unsigned long started, timeout = client->mount_args->mount_timeout * HZ; | ||
| 2762 | |||
| 2763 | dout("close_sessions\n"); | ||
| 2764 | |||
| 2765 | mutex_lock(&mdsc->mutex); | ||
| 2766 | |||
| 2767 | /* close sessions */ | ||
| 2768 | started = jiffies; | ||
| 2769 | while (time_before(jiffies, started + timeout)) { | ||
| 2770 | dout("closing sessions\n"); | ||
| 2771 | n = 0; | ||
| 2772 | for (i = 0; i < mdsc->max_sessions; i++) { | ||
| 2773 | session = __ceph_lookup_mds_session(mdsc, i); | ||
| 2774 | if (!session) | ||
| 2775 | continue; | ||
| 2776 | mutex_unlock(&mdsc->mutex); | ||
| 2777 | mutex_lock(&session->s_mutex); | ||
| 2778 | __close_session(mdsc, session); | ||
| 2779 | mutex_unlock(&session->s_mutex); | ||
| 2780 | ceph_put_mds_session(session); | ||
| 2781 | mutex_lock(&mdsc->mutex); | ||
| 2782 | n++; | ||
| 2783 | } | ||
| 2784 | if (n == 0) | ||
| 2785 | break; | ||
| 2786 | |||
| 2787 | if (client->mount_state == CEPH_MOUNT_SHUTDOWN) | ||
| 2788 | break; | ||
| 2789 | |||
| 2790 | dout("waiting for sessions to close\n"); | ||
| 2791 | mutex_unlock(&mdsc->mutex); | ||
| 2792 | wait_for_completion_timeout(&mdsc->session_close_waiters, | ||
| 2793 | timeout); | ||
| 2794 | mutex_lock(&mdsc->mutex); | ||
| 2795 | } | ||
| 2796 | |||
| 2797 | /* tear down remaining sessions */ | ||
| 2798 | for (i = 0; i < mdsc->max_sessions; i++) { | ||
| 2799 | if (mdsc->sessions[i]) { | ||
| 2800 | session = get_session(mdsc->sessions[i]); | ||
| 2801 | __unregister_session(mdsc, session); | ||
| 2802 | mutex_unlock(&mdsc->mutex); | ||
| 2803 | mutex_lock(&session->s_mutex); | ||
| 2804 | remove_session_caps(session); | ||
| 2805 | mutex_unlock(&session->s_mutex); | ||
| 2806 | ceph_put_mds_session(session); | ||
| 2807 | mutex_lock(&mdsc->mutex); | ||
| 2808 | } | ||
| 2809 | } | ||
| 2810 | |||
| 2811 | WARN_ON(!list_empty(&mdsc->cap_delay_list)); | ||
| 2812 | |||
| 2813 | mutex_unlock(&mdsc->mutex); | ||
| 2814 | |||
| 2815 | ceph_cleanup_empty_realms(mdsc); | ||
| 2816 | |||
| 2817 | cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */ | ||
| 2818 | |||
| 2819 | dout("stopped\n"); | ||
| 2820 | } | ||
| 2821 | |||
| 2822 | void ceph_mdsc_stop(struct ceph_mds_client *mdsc) | ||
| 2823 | { | ||
| 2824 | dout("stop\n"); | ||
| 2825 | cancel_delayed_work_sync(&mdsc->delayed_work); /* cancel timer */ | ||
| 2826 | if (mdsc->mdsmap) | ||
| 2827 | ceph_mdsmap_destroy(mdsc->mdsmap); | ||
| 2828 | kfree(mdsc->sessions); | ||
| 2829 | } | ||
| 2830 | |||
| 2831 | |||
| 2832 | /* | ||
| 2833 | * handle mds map update. | ||
| 2834 | */ | ||
| 2835 | void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, struct ceph_msg *msg) | ||
| 2836 | { | ||
| 2837 | u32 epoch; | ||
| 2838 | u32 maplen; | ||
| 2839 | void *p = msg->front.iov_base; | ||
| 2840 | void *end = p + msg->front.iov_len; | ||
| 2841 | struct ceph_mdsmap *newmap, *oldmap; | ||
| 2842 | struct ceph_fsid fsid; | ||
| 2843 | int err = -EINVAL; | ||
| 2844 | |||
| 2845 | ceph_decode_need(&p, end, sizeof(fsid)+2*sizeof(u32), bad); | ||
| 2846 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); | ||
| 2847 | if (ceph_check_fsid(mdsc->client, &fsid) < 0) | ||
| 2848 | return; | ||
| 2849 | epoch = ceph_decode_32(&p); | ||
| 2850 | maplen = ceph_decode_32(&p); | ||
| 2851 | dout("handle_map epoch %u len %d\n", epoch, (int)maplen); | ||
| 2852 | |||
| 2853 | /* do we need it? */ | ||
| 2854 | ceph_monc_got_mdsmap(&mdsc->client->monc, epoch); | ||
| 2855 | mutex_lock(&mdsc->mutex); | ||
| 2856 | if (mdsc->mdsmap && epoch <= mdsc->mdsmap->m_epoch) { | ||
| 2857 | dout("handle_map epoch %u <= our %u\n", | ||
| 2858 | epoch, mdsc->mdsmap->m_epoch); | ||
| 2859 | mutex_unlock(&mdsc->mutex); | ||
| 2860 | return; | ||
| 2861 | } | ||
| 2862 | |||
| 2863 | newmap = ceph_mdsmap_decode(&p, end); | ||
| 2864 | if (IS_ERR(newmap)) { | ||
| 2865 | err = PTR_ERR(newmap); | ||
| 2866 | goto bad_unlock; | ||
| 2867 | } | ||
| 2868 | |||
| 2869 | /* swap into place */ | ||
| 2870 | if (mdsc->mdsmap) { | ||
| 2871 | oldmap = mdsc->mdsmap; | ||
| 2872 | mdsc->mdsmap = newmap; | ||
| 2873 | check_new_map(mdsc, newmap, oldmap); | ||
| 2874 | ceph_mdsmap_destroy(oldmap); | ||
| 2875 | } else { | ||
| 2876 | mdsc->mdsmap = newmap; /* first mds map */ | ||
| 2877 | } | ||
| 2878 | mdsc->client->sb->s_maxbytes = mdsc->mdsmap->m_max_file_size; | ||
| 2879 | |||
| 2880 | __wake_requests(mdsc, &mdsc->waiting_for_map); | ||
| 2881 | |||
| 2882 | mutex_unlock(&mdsc->mutex); | ||
| 2883 | schedule_delayed(mdsc); | ||
| 2884 | return; | ||
| 2885 | |||
| 2886 | bad_unlock: | ||
| 2887 | mutex_unlock(&mdsc->mutex); | ||
| 2888 | bad: | ||
| 2889 | pr_err("error decoding mdsmap %d\n", err); | ||
| 2890 | return; | ||
| 2891 | } | ||
| 2892 | |||
| 2893 | static struct ceph_connection *con_get(struct ceph_connection *con) | ||
| 2894 | { | ||
| 2895 | struct ceph_mds_session *s = con->private; | ||
| 2896 | |||
| 2897 | if (get_session(s)) { | ||
| 2898 | dout("mdsc con_get %p ok (%d)\n", s, atomic_read(&s->s_ref)); | ||
| 2899 | return con; | ||
| 2900 | } | ||
| 2901 | dout("mdsc con_get %p FAIL\n", s); | ||
| 2902 | return NULL; | ||
| 2903 | } | ||
| 2904 | |||
| 2905 | static void con_put(struct ceph_connection *con) | ||
| 2906 | { | ||
| 2907 | struct ceph_mds_session *s = con->private; | ||
| 2908 | |||
| 2909 | ceph_put_mds_session(s); | ||
| 2910 | dout("mdsc con_put %p (%d)\n", s, atomic_read(&s->s_ref)); | ||
| 2911 | } | ||
| 2912 | |||
| 2913 | /* | ||
| 2914 | * if the client is unresponsive for long enough, the mds will kill | ||
| 2915 | * the session entirely. | ||
| 2916 | */ | ||
| 2917 | static void peer_reset(struct ceph_connection *con) | ||
| 2918 | { | ||
| 2919 | struct ceph_mds_session *s = con->private; | ||
| 2920 | |||
| 2921 | pr_err("mds%d gave us the boot. IMPLEMENT RECONNECT.\n", | ||
| 2922 | s->s_mds); | ||
| 2923 | } | ||
| 2924 | |||
| 2925 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) | ||
| 2926 | { | ||
| 2927 | struct ceph_mds_session *s = con->private; | ||
| 2928 | struct ceph_mds_client *mdsc = s->s_mdsc; | ||
| 2929 | int type = le16_to_cpu(msg->hdr.type); | ||
| 2930 | |||
| 2931 | mutex_lock(&mdsc->mutex); | ||
| 2932 | if (__verify_registered_session(mdsc, s) < 0) { | ||
| 2933 | mutex_unlock(&mdsc->mutex); | ||
| 2934 | goto out; | ||
| 2935 | } | ||
| 2936 | mutex_unlock(&mdsc->mutex); | ||
| 2937 | |||
| 2938 | switch (type) { | ||
| 2939 | case CEPH_MSG_MDS_MAP: | ||
| 2940 | ceph_mdsc_handle_map(mdsc, msg); | ||
| 2941 | break; | ||
| 2942 | case CEPH_MSG_CLIENT_SESSION: | ||
| 2943 | handle_session(s, msg); | ||
| 2944 | break; | ||
| 2945 | case CEPH_MSG_CLIENT_REPLY: | ||
| 2946 | handle_reply(s, msg); | ||
| 2947 | break; | ||
| 2948 | case CEPH_MSG_CLIENT_REQUEST_FORWARD: | ||
| 2949 | handle_forward(mdsc, s, msg); | ||
| 2950 | break; | ||
| 2951 | case CEPH_MSG_CLIENT_CAPS: | ||
| 2952 | ceph_handle_caps(s, msg); | ||
| 2953 | break; | ||
| 2954 | case CEPH_MSG_CLIENT_SNAP: | ||
| 2955 | ceph_handle_snap(mdsc, s, msg); | ||
| 2956 | break; | ||
| 2957 | case CEPH_MSG_CLIENT_LEASE: | ||
| 2958 | handle_lease(mdsc, s, msg); | ||
| 2959 | break; | ||
| 2960 | |||
| 2961 | default: | ||
| 2962 | pr_err("received unknown message type %d %s\n", type, | ||
| 2963 | ceph_msg_type_name(type)); | ||
| 2964 | } | ||
| 2965 | out: | ||
| 2966 | ceph_msg_put(msg); | ||
| 2967 | } | ||
| 2968 | |||
| 2969 | /* | ||
| 2970 | * authentication | ||
| 2971 | */ | ||
| 2972 | static int get_authorizer(struct ceph_connection *con, | ||
| 2973 | void **buf, int *len, int *proto, | ||
| 2974 | void **reply_buf, int *reply_len, int force_new) | ||
| 2975 | { | ||
| 2976 | struct ceph_mds_session *s = con->private; | ||
| 2977 | struct ceph_mds_client *mdsc = s->s_mdsc; | ||
| 2978 | struct ceph_auth_client *ac = mdsc->client->monc.auth; | ||
| 2979 | int ret = 0; | ||
| 2980 | |||
| 2981 | if (force_new && s->s_authorizer) { | ||
| 2982 | ac->ops->destroy_authorizer(ac, s->s_authorizer); | ||
| 2983 | s->s_authorizer = NULL; | ||
| 2984 | } | ||
| 2985 | if (s->s_authorizer == NULL) { | ||
| 2986 | if (ac->ops->create_authorizer) { | ||
| 2987 | ret = ac->ops->create_authorizer( | ||
| 2988 | ac, CEPH_ENTITY_TYPE_MDS, | ||
| 2989 | &s->s_authorizer, | ||
| 2990 | &s->s_authorizer_buf, | ||
| 2991 | &s->s_authorizer_buf_len, | ||
| 2992 | &s->s_authorizer_reply_buf, | ||
| 2993 | &s->s_authorizer_reply_buf_len); | ||
| 2994 | if (ret) | ||
| 2995 | return ret; | ||
| 2996 | } | ||
| 2997 | } | ||
| 2998 | |||
| 2999 | *proto = ac->protocol; | ||
| 3000 | *buf = s->s_authorizer_buf; | ||
| 3001 | *len = s->s_authorizer_buf_len; | ||
| 3002 | *reply_buf = s->s_authorizer_reply_buf; | ||
| 3003 | *reply_len = s->s_authorizer_reply_buf_len; | ||
| 3004 | return 0; | ||
| 3005 | } | ||
| 3006 | |||
| 3007 | |||
| 3008 | static int verify_authorizer_reply(struct ceph_connection *con, int len) | ||
| 3009 | { | ||
| 3010 | struct ceph_mds_session *s = con->private; | ||
| 3011 | struct ceph_mds_client *mdsc = s->s_mdsc; | ||
| 3012 | struct ceph_auth_client *ac = mdsc->client->monc.auth; | ||
| 3013 | |||
| 3014 | return ac->ops->verify_authorizer_reply(ac, s->s_authorizer, len); | ||
| 3015 | } | ||
| 3016 | |||
| 3017 | static int invalidate_authorizer(struct ceph_connection *con) | ||
| 3018 | { | ||
| 3019 | struct ceph_mds_session *s = con->private; | ||
| 3020 | struct ceph_mds_client *mdsc = s->s_mdsc; | ||
| 3021 | struct ceph_auth_client *ac = mdsc->client->monc.auth; | ||
| 3022 | |||
| 3023 | if (ac->ops->invalidate_authorizer) | ||
| 3024 | ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_MDS); | ||
| 3025 | |||
| 3026 | return ceph_monc_validate_auth(&mdsc->client->monc); | ||
| 3027 | } | ||
| 3028 | |||
| 3029 | const static struct ceph_connection_operations mds_con_ops = { | ||
| 3030 | .get = con_get, | ||
| 3031 | .put = con_put, | ||
| 3032 | .dispatch = dispatch, | ||
| 3033 | .get_authorizer = get_authorizer, | ||
| 3034 | .verify_authorizer_reply = verify_authorizer_reply, | ||
| 3035 | .invalidate_authorizer = invalidate_authorizer, | ||
| 3036 | .peer_reset = peer_reset, | ||
| 3037 | }; | ||
| 3038 | |||
| 3039 | |||
| 3040 | |||
| 3041 | |||
| 3042 | /* eof */ | ||
diff --git a/fs/ceph/mds_client.h b/fs/ceph/mds_client.h new file mode 100644 index 000000000000..961cc6f65878 --- /dev/null +++ b/fs/ceph/mds_client.h | |||
| @@ -0,0 +1,335 @@ | |||
| 1 | #ifndef _FS_CEPH_MDS_CLIENT_H | ||
| 2 | #define _FS_CEPH_MDS_CLIENT_H | ||
| 3 | |||
| 4 | #include <linux/completion.h> | ||
| 5 | #include <linux/kref.h> | ||
| 6 | #include <linux/list.h> | ||
| 7 | #include <linux/mutex.h> | ||
| 8 | #include <linux/rbtree.h> | ||
| 9 | #include <linux/spinlock.h> | ||
| 10 | |||
| 11 | #include "types.h" | ||
| 12 | #include "messenger.h" | ||
| 13 | #include "mdsmap.h" | ||
| 14 | |||
| 15 | /* | ||
| 16 | * Some lock dependencies: | ||
| 17 | * | ||
| 18 | * session->s_mutex | ||
| 19 | * mdsc->mutex | ||
| 20 | * | ||
| 21 | * mdsc->snap_rwsem | ||
| 22 | * | ||
| 23 | * inode->i_lock | ||
| 24 | * mdsc->snap_flush_lock | ||
| 25 | * mdsc->cap_delay_lock | ||
| 26 | * | ||
| 27 | */ | ||
| 28 | |||
| 29 | struct ceph_client; | ||
| 30 | struct ceph_cap; | ||
| 31 | |||
| 32 | /* | ||
| 33 | * parsed info about a single inode. pointers are into the encoded | ||
| 34 | * on-wire structures within the mds reply message payload. | ||
| 35 | */ | ||
| 36 | struct ceph_mds_reply_info_in { | ||
| 37 | struct ceph_mds_reply_inode *in; | ||
| 38 | u32 symlink_len; | ||
| 39 | char *symlink; | ||
| 40 | u32 xattr_len; | ||
| 41 | char *xattr_data; | ||
| 42 | }; | ||
| 43 | |||
| 44 | /* | ||
| 45 | * parsed info about an mds reply, including information about the | ||
| 46 | * target inode and/or its parent directory and dentry, and directory | ||
| 47 | * contents (for readdir results). | ||
| 48 | */ | ||
| 49 | struct ceph_mds_reply_info_parsed { | ||
| 50 | struct ceph_mds_reply_head *head; | ||
| 51 | |||
| 52 | struct ceph_mds_reply_info_in diri, targeti; | ||
| 53 | struct ceph_mds_reply_dirfrag *dirfrag; | ||
| 54 | char *dname; | ||
| 55 | u32 dname_len; | ||
| 56 | struct ceph_mds_reply_lease *dlease; | ||
| 57 | |||
| 58 | struct ceph_mds_reply_dirfrag *dir_dir; | ||
| 59 | int dir_nr; | ||
| 60 | char **dir_dname; | ||
| 61 | u32 *dir_dname_len; | ||
| 62 | struct ceph_mds_reply_lease **dir_dlease; | ||
| 63 | struct ceph_mds_reply_info_in *dir_in; | ||
| 64 | u8 dir_complete, dir_end; | ||
| 65 | |||
| 66 | /* encoded blob describing snapshot contexts for certain | ||
| 67 | operations (e.g., open) */ | ||
| 68 | void *snapblob; | ||
| 69 | int snapblob_len; | ||
| 70 | }; | ||
| 71 | |||
| 72 | |||
| 73 | /* | ||
| 74 | * cap releases are batched and sent to the MDS en masse. | ||
| 75 | */ | ||
| 76 | #define CEPH_CAPS_PER_RELEASE ((PAGE_CACHE_SIZE - \ | ||
| 77 | sizeof(struct ceph_mds_cap_release)) / \ | ||
| 78 | sizeof(struct ceph_mds_cap_item)) | ||
| 79 | |||
| 80 | |||
| 81 | /* | ||
| 82 | * state associated with each MDS<->client session | ||
| 83 | */ | ||
| 84 | enum { | ||
| 85 | CEPH_MDS_SESSION_NEW = 1, | ||
| 86 | CEPH_MDS_SESSION_OPENING = 2, | ||
| 87 | CEPH_MDS_SESSION_OPEN = 3, | ||
| 88 | CEPH_MDS_SESSION_HUNG = 4, | ||
| 89 | CEPH_MDS_SESSION_CLOSING = 5, | ||
| 90 | CEPH_MDS_SESSION_RESTARTING = 6, | ||
| 91 | CEPH_MDS_SESSION_RECONNECTING = 7, | ||
| 92 | }; | ||
| 93 | |||
| 94 | struct ceph_mds_session { | ||
| 95 | struct ceph_mds_client *s_mdsc; | ||
| 96 | int s_mds; | ||
| 97 | int s_state; | ||
| 98 | unsigned long s_ttl; /* time until mds kills us */ | ||
| 99 | u64 s_seq; /* incoming msg seq # */ | ||
| 100 | struct mutex s_mutex; /* serialize session messages */ | ||
| 101 | |||
| 102 | struct ceph_connection s_con; | ||
| 103 | |||
| 104 | struct ceph_authorizer *s_authorizer; | ||
| 105 | void *s_authorizer_buf, *s_authorizer_reply_buf; | ||
| 106 | size_t s_authorizer_buf_len, s_authorizer_reply_buf_len; | ||
| 107 | |||
| 108 | /* protected by s_cap_lock */ | ||
| 109 | spinlock_t s_cap_lock; | ||
| 110 | u32 s_cap_gen; /* inc each time we get mds stale msg */ | ||
| 111 | unsigned long s_cap_ttl; /* when session caps expire */ | ||
| 112 | struct list_head s_caps; /* all caps issued by this session */ | ||
| 113 | int s_nr_caps, s_trim_caps; | ||
| 114 | int s_num_cap_releases; | ||
| 115 | struct list_head s_cap_releases; /* waiting cap_release messages */ | ||
| 116 | struct list_head s_cap_releases_done; /* ready to send */ | ||
| 117 | struct ceph_cap *s_cap_iterator; | ||
| 118 | |||
| 119 | /* protected by mutex */ | ||
| 120 | struct list_head s_cap_flushing; /* inodes w/ flushing caps */ | ||
| 121 | struct list_head s_cap_snaps_flushing; | ||
| 122 | unsigned long s_renew_requested; /* last time we sent a renew req */ | ||
| 123 | u64 s_renew_seq; | ||
| 124 | |||
| 125 | atomic_t s_ref; | ||
| 126 | struct list_head s_waiting; /* waiting requests */ | ||
| 127 | struct list_head s_unsafe; /* unsafe requests */ | ||
| 128 | }; | ||
| 129 | |||
| 130 | /* | ||
| 131 | * modes of choosing which MDS to send a request to | ||
| 132 | */ | ||
| 133 | enum { | ||
| 134 | USE_ANY_MDS, | ||
| 135 | USE_RANDOM_MDS, | ||
| 136 | USE_AUTH_MDS, /* prefer authoritative mds for this metadata item */ | ||
| 137 | }; | ||
| 138 | |||
| 139 | struct ceph_mds_request; | ||
| 140 | struct ceph_mds_client; | ||
| 141 | |||
| 142 | /* | ||
| 143 | * request completion callback | ||
| 144 | */ | ||
| 145 | typedef void (*ceph_mds_request_callback_t) (struct ceph_mds_client *mdsc, | ||
| 146 | struct ceph_mds_request *req); | ||
| 147 | |||
| 148 | /* | ||
| 149 | * an in-flight mds request | ||
| 150 | */ | ||
| 151 | struct ceph_mds_request { | ||
| 152 | u64 r_tid; /* transaction id */ | ||
| 153 | struct rb_node r_node; | ||
| 154 | |||
| 155 | int r_op; /* mds op code */ | ||
| 156 | int r_mds; | ||
| 157 | |||
| 158 | /* operation on what? */ | ||
| 159 | struct inode *r_inode; /* arg1 */ | ||
| 160 | struct dentry *r_dentry; /* arg1 */ | ||
| 161 | struct dentry *r_old_dentry; /* arg2: rename from or link from */ | ||
| 162 | char *r_path1, *r_path2; | ||
| 163 | struct ceph_vino r_ino1, r_ino2; | ||
| 164 | |||
| 165 | struct inode *r_locked_dir; /* dir (if any) i_mutex locked by vfs */ | ||
| 166 | struct inode *r_target_inode; /* resulting inode */ | ||
| 167 | |||
| 168 | union ceph_mds_request_args r_args; | ||
| 169 | int r_fmode; /* file mode, if expecting cap */ | ||
| 170 | |||
| 171 | /* for choosing which mds to send this request to */ | ||
| 172 | int r_direct_mode; | ||
| 173 | u32 r_direct_hash; /* choose dir frag based on this dentry hash */ | ||
| 174 | bool r_direct_is_hash; /* true if r_direct_hash is valid */ | ||
| 175 | |||
| 176 | /* data payload is used for xattr ops */ | ||
| 177 | struct page **r_pages; | ||
| 178 | int r_num_pages; | ||
| 179 | int r_data_len; | ||
| 180 | |||
| 181 | /* what caps shall we drop? */ | ||
| 182 | int r_inode_drop, r_inode_unless; | ||
| 183 | int r_dentry_drop, r_dentry_unless; | ||
| 184 | int r_old_dentry_drop, r_old_dentry_unless; | ||
| 185 | struct inode *r_old_inode; | ||
| 186 | int r_old_inode_drop, r_old_inode_unless; | ||
| 187 | |||
| 188 | struct ceph_msg *r_request; /* original request */ | ||
| 189 | struct ceph_msg *r_reply; | ||
| 190 | struct ceph_mds_reply_info_parsed r_reply_info; | ||
| 191 | int r_err; | ||
| 192 | bool r_aborted; | ||
| 193 | |||
| 194 | unsigned long r_timeout; /* optional. jiffies */ | ||
| 195 | unsigned long r_started; /* start time to measure timeout against */ | ||
| 196 | unsigned long r_request_started; /* start time for mds request only, | ||
| 197 | used to measure lease durations */ | ||
| 198 | |||
| 199 | /* link unsafe requests to parent directory, for fsync */ | ||
| 200 | struct inode *r_unsafe_dir; | ||
| 201 | struct list_head r_unsafe_dir_item; | ||
| 202 | |||
| 203 | struct ceph_mds_session *r_session; | ||
| 204 | |||
| 205 | int r_attempts; /* resend attempts */ | ||
| 206 | int r_num_fwd; /* number of forward attempts */ | ||
| 207 | int r_num_stale; | ||
| 208 | int r_resend_mds; /* mds to resend to next, if any*/ | ||
| 209 | |||
| 210 | struct kref r_kref; | ||
| 211 | struct list_head r_wait; | ||
| 212 | struct completion r_completion; | ||
| 213 | struct completion r_safe_completion; | ||
| 214 | ceph_mds_request_callback_t r_callback; | ||
| 215 | struct list_head r_unsafe_item; /* per-session unsafe list item */ | ||
| 216 | bool r_got_unsafe, r_got_safe; | ||
| 217 | |||
| 218 | bool r_did_prepopulate; | ||
| 219 | u32 r_readdir_offset; | ||
| 220 | |||
| 221 | struct ceph_cap_reservation r_caps_reservation; | ||
| 222 | int r_num_caps; | ||
| 223 | }; | ||
| 224 | |||
| 225 | /* | ||
| 226 | * mds client state | ||
| 227 | */ | ||
| 228 | struct ceph_mds_client { | ||
| 229 | struct ceph_client *client; | ||
| 230 | struct mutex mutex; /* all nested structures */ | ||
| 231 | |||
| 232 | struct ceph_mdsmap *mdsmap; | ||
| 233 | struct completion safe_umount_waiters, session_close_waiters; | ||
| 234 | struct list_head waiting_for_map; | ||
| 235 | |||
| 236 | struct ceph_mds_session **sessions; /* NULL for mds if no session */ | ||
| 237 | int max_sessions; /* len of s_mds_sessions */ | ||
| 238 | int stopping; /* true if shutting down */ | ||
| 239 | |||
| 240 | /* | ||
| 241 | * snap_rwsem will cover cap linkage into snaprealms, and | ||
| 242 | * realm snap contexts. (later, we can do per-realm snap | ||
| 243 | * contexts locks..) the empty list contains realms with no | ||
| 244 | * references (implying they contain no inodes with caps) that | ||
| 245 | * should be destroyed. | ||
| 246 | */ | ||
| 247 | struct rw_semaphore snap_rwsem; | ||
| 248 | struct rb_root snap_realms; | ||
| 249 | struct list_head snap_empty; | ||
| 250 | spinlock_t snap_empty_lock; /* protect snap_empty */ | ||
| 251 | |||
| 252 | u64 last_tid; /* most recent mds request */ | ||
| 253 | struct rb_root request_tree; /* pending mds requests */ | ||
| 254 | struct delayed_work delayed_work; /* delayed work */ | ||
| 255 | unsigned long last_renew_caps; /* last time we renewed our caps */ | ||
| 256 | struct list_head cap_delay_list; /* caps with delayed release */ | ||
| 257 | spinlock_t cap_delay_lock; /* protects cap_delay_list */ | ||
| 258 | struct list_head snap_flush_list; /* cap_snaps ready to flush */ | ||
| 259 | spinlock_t snap_flush_lock; | ||
| 260 | |||
| 261 | u64 cap_flush_seq; | ||
| 262 | struct list_head cap_dirty; /* inodes with dirty caps */ | ||
| 263 | int num_cap_flushing; /* # caps we are flushing */ | ||
| 264 | spinlock_t cap_dirty_lock; /* protects above items */ | ||
| 265 | wait_queue_head_t cap_flushing_wq; | ||
| 266 | |||
| 267 | #ifdef CONFIG_DEBUG_FS | ||
| 268 | struct dentry *debugfs_file; | ||
| 269 | #endif | ||
| 270 | |||
| 271 | spinlock_t dentry_lru_lock; | ||
| 272 | struct list_head dentry_lru; | ||
| 273 | int num_dentry; | ||
| 274 | }; | ||
| 275 | |||
| 276 | extern const char *ceph_mds_op_name(int op); | ||
| 277 | |||
| 278 | extern struct ceph_mds_session * | ||
| 279 | __ceph_lookup_mds_session(struct ceph_mds_client *, int mds); | ||
| 280 | |||
| 281 | static inline struct ceph_mds_session * | ||
| 282 | ceph_get_mds_session(struct ceph_mds_session *s) | ||
| 283 | { | ||
| 284 | atomic_inc(&s->s_ref); | ||
| 285 | return s; | ||
| 286 | } | ||
| 287 | |||
| 288 | extern void ceph_put_mds_session(struct ceph_mds_session *s); | ||
| 289 | |||
| 290 | extern int ceph_send_msg_mds(struct ceph_mds_client *mdsc, | ||
| 291 | struct ceph_msg *msg, int mds); | ||
| 292 | |||
| 293 | extern int ceph_mdsc_init(struct ceph_mds_client *mdsc, | ||
| 294 | struct ceph_client *client); | ||
| 295 | extern void ceph_mdsc_close_sessions(struct ceph_mds_client *mdsc); | ||
| 296 | extern void ceph_mdsc_stop(struct ceph_mds_client *mdsc); | ||
| 297 | |||
| 298 | extern void ceph_mdsc_sync(struct ceph_mds_client *mdsc); | ||
| 299 | |||
| 300 | extern void ceph_mdsc_lease_release(struct ceph_mds_client *mdsc, | ||
| 301 | struct inode *inode, | ||
| 302 | struct dentry *dn, int mask); | ||
| 303 | |||
| 304 | extern struct ceph_mds_request * | ||
| 305 | ceph_mdsc_create_request(struct ceph_mds_client *mdsc, int op, int mode); | ||
| 306 | extern void ceph_mdsc_submit_request(struct ceph_mds_client *mdsc, | ||
| 307 | struct ceph_mds_request *req); | ||
| 308 | extern int ceph_mdsc_do_request(struct ceph_mds_client *mdsc, | ||
| 309 | struct inode *dir, | ||
| 310 | struct ceph_mds_request *req); | ||
| 311 | static inline void ceph_mdsc_get_request(struct ceph_mds_request *req) | ||
| 312 | { | ||
| 313 | kref_get(&req->r_kref); | ||
| 314 | } | ||
| 315 | extern void ceph_mdsc_release_request(struct kref *kref); | ||
| 316 | static inline void ceph_mdsc_put_request(struct ceph_mds_request *req) | ||
| 317 | { | ||
| 318 | kref_put(&req->r_kref, ceph_mdsc_release_request); | ||
| 319 | } | ||
| 320 | |||
| 321 | extern void ceph_mdsc_pre_umount(struct ceph_mds_client *mdsc); | ||
| 322 | |||
| 323 | extern char *ceph_mdsc_build_path(struct dentry *dentry, int *plen, u64 *base, | ||
| 324 | int stop_on_nosnap); | ||
| 325 | |||
| 326 | extern void __ceph_mdsc_drop_dentry_lease(struct dentry *dentry); | ||
| 327 | extern void ceph_mdsc_lease_send_msg(struct ceph_mds_session *session, | ||
| 328 | struct inode *inode, | ||
| 329 | struct dentry *dentry, char action, | ||
| 330 | u32 seq); | ||
| 331 | |||
| 332 | extern void ceph_mdsc_handle_map(struct ceph_mds_client *mdsc, | ||
| 333 | struct ceph_msg *msg); | ||
| 334 | |||
| 335 | #endif | ||
diff --git a/fs/ceph/mdsmap.c b/fs/ceph/mdsmap.c new file mode 100644 index 000000000000..c4c498e6dfef --- /dev/null +++ b/fs/ceph/mdsmap.c | |||
| @@ -0,0 +1,174 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/bug.h> | ||
| 4 | #include <linux/err.h> | ||
| 5 | #include <linux/random.h> | ||
| 6 | #include <linux/slab.h> | ||
| 7 | #include <linux/types.h> | ||
| 8 | |||
| 9 | #include "mdsmap.h" | ||
| 10 | #include "messenger.h" | ||
| 11 | #include "decode.h" | ||
| 12 | |||
| 13 | #include "super.h" | ||
| 14 | |||
| 15 | |||
| 16 | /* | ||
| 17 | * choose a random mds that is "up" (i.e. has a state > 0), or -1. | ||
| 18 | */ | ||
| 19 | int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m) | ||
| 20 | { | ||
| 21 | int n = 0; | ||
| 22 | int i; | ||
| 23 | char r; | ||
| 24 | |||
| 25 | /* count */ | ||
| 26 | for (i = 0; i < m->m_max_mds; i++) | ||
| 27 | if (m->m_info[i].state > 0) | ||
| 28 | n++; | ||
| 29 | if (n == 0) | ||
| 30 | return -1; | ||
| 31 | |||
| 32 | /* pick */ | ||
| 33 | get_random_bytes(&r, 1); | ||
| 34 | n = r % n; | ||
| 35 | i = 0; | ||
| 36 | for (i = 0; n > 0; i++, n--) | ||
| 37 | while (m->m_info[i].state <= 0) | ||
| 38 | i++; | ||
| 39 | |||
| 40 | return i; | ||
| 41 | } | ||
| 42 | |||
| 43 | /* | ||
| 44 | * Decode an MDS map | ||
| 45 | * | ||
| 46 | * Ignore any fields we don't care about (there are quite a few of | ||
| 47 | * them). | ||
| 48 | */ | ||
| 49 | struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end) | ||
| 50 | { | ||
| 51 | struct ceph_mdsmap *m; | ||
| 52 | const void *start = *p; | ||
| 53 | int i, j, n; | ||
| 54 | int err = -EINVAL; | ||
| 55 | u16 version; | ||
| 56 | |||
| 57 | m = kzalloc(sizeof(*m), GFP_NOFS); | ||
| 58 | if (m == NULL) | ||
| 59 | return ERR_PTR(-ENOMEM); | ||
| 60 | |||
| 61 | ceph_decode_16_safe(p, end, version, bad); | ||
| 62 | |||
| 63 | ceph_decode_need(p, end, 8*sizeof(u32) + sizeof(u64), bad); | ||
| 64 | m->m_epoch = ceph_decode_32(p); | ||
| 65 | m->m_client_epoch = ceph_decode_32(p); | ||
| 66 | m->m_last_failure = ceph_decode_32(p); | ||
| 67 | m->m_root = ceph_decode_32(p); | ||
| 68 | m->m_session_timeout = ceph_decode_32(p); | ||
| 69 | m->m_session_autoclose = ceph_decode_32(p); | ||
| 70 | m->m_max_file_size = ceph_decode_64(p); | ||
| 71 | m->m_max_mds = ceph_decode_32(p); | ||
| 72 | |||
| 73 | m->m_info = kcalloc(m->m_max_mds, sizeof(*m->m_info), GFP_NOFS); | ||
| 74 | if (m->m_info == NULL) | ||
| 75 | goto badmem; | ||
| 76 | |||
| 77 | /* pick out active nodes from mds_info (state > 0) */ | ||
| 78 | n = ceph_decode_32(p); | ||
| 79 | for (i = 0; i < n; i++) { | ||
| 80 | u64 global_id; | ||
| 81 | u32 namelen; | ||
| 82 | s32 mds, inc, state; | ||
| 83 | u64 state_seq; | ||
| 84 | u8 infoversion; | ||
| 85 | struct ceph_entity_addr addr; | ||
| 86 | u32 num_export_targets; | ||
| 87 | void *pexport_targets = NULL; | ||
| 88 | |||
| 89 | ceph_decode_need(p, end, sizeof(u64)*2 + 1 + sizeof(u32), bad); | ||
| 90 | global_id = ceph_decode_64(p); | ||
| 91 | infoversion = ceph_decode_8(p); | ||
| 92 | *p += sizeof(u64); | ||
| 93 | namelen = ceph_decode_32(p); /* skip mds name */ | ||
| 94 | *p += namelen; | ||
| 95 | |||
| 96 | ceph_decode_need(p, end, | ||
| 97 | 4*sizeof(u32) + sizeof(u64) + | ||
| 98 | sizeof(addr) + sizeof(struct ceph_timespec), | ||
| 99 | bad); | ||
| 100 | mds = ceph_decode_32(p); | ||
| 101 | inc = ceph_decode_32(p); | ||
| 102 | state = ceph_decode_32(p); | ||
| 103 | state_seq = ceph_decode_64(p); | ||
| 104 | ceph_decode_copy(p, &addr, sizeof(addr)); | ||
| 105 | ceph_decode_addr(&addr); | ||
| 106 | *p += sizeof(struct ceph_timespec); | ||
| 107 | *p += sizeof(u32); | ||
| 108 | ceph_decode_32_safe(p, end, namelen, bad); | ||
| 109 | *p += namelen; | ||
| 110 | if (infoversion >= 2) { | ||
| 111 | ceph_decode_32_safe(p, end, num_export_targets, bad); | ||
| 112 | pexport_targets = *p; | ||
| 113 | *p += num_export_targets * sizeof(u32); | ||
| 114 | } else { | ||
| 115 | num_export_targets = 0; | ||
| 116 | } | ||
| 117 | |||
| 118 | dout("mdsmap_decode %d/%d %lld mds%d.%d %s %s\n", | ||
| 119 | i+1, n, global_id, mds, inc, pr_addr(&addr.in_addr), | ||
| 120 | ceph_mds_state_name(state)); | ||
| 121 | if (mds >= 0 && mds < m->m_max_mds && state > 0) { | ||
| 122 | m->m_info[mds].global_id = global_id; | ||
| 123 | m->m_info[mds].state = state; | ||
| 124 | m->m_info[mds].addr = addr; | ||
| 125 | m->m_info[mds].num_export_targets = num_export_targets; | ||
| 126 | if (num_export_targets) { | ||
| 127 | m->m_info[mds].export_targets = | ||
| 128 | kcalloc(num_export_targets, sizeof(u32), | ||
| 129 | GFP_NOFS); | ||
| 130 | for (j = 0; j < num_export_targets; j++) | ||
| 131 | m->m_info[mds].export_targets[j] = | ||
| 132 | ceph_decode_32(&pexport_targets); | ||
| 133 | } else { | ||
| 134 | m->m_info[mds].export_targets = NULL; | ||
| 135 | } | ||
| 136 | } | ||
| 137 | } | ||
| 138 | |||
| 139 | /* pg_pools */ | ||
| 140 | ceph_decode_32_safe(p, end, n, bad); | ||
| 141 | m->m_num_data_pg_pools = n; | ||
| 142 | m->m_data_pg_pools = kcalloc(n, sizeof(u32), GFP_NOFS); | ||
| 143 | if (!m->m_data_pg_pools) | ||
| 144 | goto badmem; | ||
| 145 | ceph_decode_need(p, end, sizeof(u32)*(n+1), bad); | ||
| 146 | for (i = 0; i < n; i++) | ||
| 147 | m->m_data_pg_pools[i] = ceph_decode_32(p); | ||
| 148 | m->m_cas_pg_pool = ceph_decode_32(p); | ||
| 149 | |||
| 150 | /* ok, we don't care about the rest. */ | ||
| 151 | dout("mdsmap_decode success epoch %u\n", m->m_epoch); | ||
| 152 | return m; | ||
| 153 | |||
| 154 | badmem: | ||
| 155 | err = -ENOMEM; | ||
| 156 | bad: | ||
| 157 | pr_err("corrupt mdsmap\n"); | ||
| 158 | print_hex_dump(KERN_DEBUG, "mdsmap: ", | ||
| 159 | DUMP_PREFIX_OFFSET, 16, 1, | ||
| 160 | start, end - start, true); | ||
| 161 | ceph_mdsmap_destroy(m); | ||
| 162 | return ERR_PTR(-EINVAL); | ||
| 163 | } | ||
| 164 | |||
| 165 | void ceph_mdsmap_destroy(struct ceph_mdsmap *m) | ||
| 166 | { | ||
| 167 | int i; | ||
| 168 | |||
| 169 | for (i = 0; i < m->m_max_mds; i++) | ||
| 170 | kfree(m->m_info[i].export_targets); | ||
| 171 | kfree(m->m_info); | ||
| 172 | kfree(m->m_data_pg_pools); | ||
| 173 | kfree(m); | ||
| 174 | } | ||
diff --git a/fs/ceph/mdsmap.h b/fs/ceph/mdsmap.h new file mode 100644 index 000000000000..eacc131aa5cb --- /dev/null +++ b/fs/ceph/mdsmap.h | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | #ifndef _FS_CEPH_MDSMAP_H | ||
| 2 | #define _FS_CEPH_MDSMAP_H | ||
| 3 | |||
| 4 | #include "types.h" | ||
| 5 | |||
| 6 | /* | ||
| 7 | * mds map - describe servers in the mds cluster. | ||
| 8 | * | ||
| 9 | * we limit fields to those the client actually xcares about | ||
| 10 | */ | ||
| 11 | struct ceph_mds_info { | ||
| 12 | u64 global_id; | ||
| 13 | struct ceph_entity_addr addr; | ||
| 14 | s32 state; | ||
| 15 | int num_export_targets; | ||
| 16 | u32 *export_targets; | ||
| 17 | }; | ||
| 18 | |||
| 19 | struct ceph_mdsmap { | ||
| 20 | u32 m_epoch, m_client_epoch, m_last_failure; | ||
| 21 | u32 m_root; | ||
| 22 | u32 m_session_timeout; /* seconds */ | ||
| 23 | u32 m_session_autoclose; /* seconds */ | ||
| 24 | u64 m_max_file_size; | ||
| 25 | u32 m_max_mds; /* size of m_addr, m_state arrays */ | ||
| 26 | struct ceph_mds_info *m_info; | ||
| 27 | |||
| 28 | /* which object pools file data can be stored in */ | ||
| 29 | int m_num_data_pg_pools; | ||
| 30 | u32 *m_data_pg_pools; | ||
| 31 | u32 m_cas_pg_pool; | ||
| 32 | }; | ||
| 33 | |||
| 34 | static inline struct ceph_entity_addr * | ||
| 35 | ceph_mdsmap_get_addr(struct ceph_mdsmap *m, int w) | ||
| 36 | { | ||
| 37 | if (w >= m->m_max_mds) | ||
| 38 | return NULL; | ||
| 39 | return &m->m_info[w].addr; | ||
| 40 | } | ||
| 41 | |||
| 42 | static inline int ceph_mdsmap_get_state(struct ceph_mdsmap *m, int w) | ||
| 43 | { | ||
| 44 | BUG_ON(w < 0); | ||
| 45 | if (w >= m->m_max_mds) | ||
| 46 | return CEPH_MDS_STATE_DNE; | ||
| 47 | return m->m_info[w].state; | ||
| 48 | } | ||
| 49 | |||
| 50 | extern int ceph_mdsmap_get_random_mds(struct ceph_mdsmap *m); | ||
| 51 | extern struct ceph_mdsmap *ceph_mdsmap_decode(void **p, void *end); | ||
| 52 | extern void ceph_mdsmap_destroy(struct ceph_mdsmap *m); | ||
| 53 | |||
| 54 | #endif | ||
diff --git a/fs/ceph/messenger.c b/fs/ceph/messenger.c new file mode 100644 index 000000000000..a32f0f896d9f --- /dev/null +++ b/fs/ceph/messenger.c | |||
| @@ -0,0 +1,2239 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/crc32c.h> | ||
| 4 | #include <linux/ctype.h> | ||
| 5 | #include <linux/highmem.h> | ||
| 6 | #include <linux/inet.h> | ||
| 7 | #include <linux/kthread.h> | ||
| 8 | #include <linux/net.h> | ||
| 9 | #include <linux/socket.h> | ||
| 10 | #include <linux/string.h> | ||
| 11 | #include <net/tcp.h> | ||
| 12 | |||
| 13 | #include "super.h" | ||
| 14 | #include "messenger.h" | ||
| 15 | #include "decode.h" | ||
| 16 | #include "pagelist.h" | ||
| 17 | |||
| 18 | /* | ||
| 19 | * Ceph uses the messenger to exchange ceph_msg messages with other | ||
| 20 | * hosts in the system. The messenger provides ordered and reliable | ||
| 21 | * delivery. We tolerate TCP disconnects by reconnecting (with | ||
| 22 | * exponential backoff) in the case of a fault (disconnection, bad | ||
| 23 | * crc, protocol error). Acks allow sent messages to be discarded by | ||
| 24 | * the sender. | ||
| 25 | */ | ||
| 26 | |||
| 27 | /* static tag bytes (protocol control messages) */ | ||
| 28 | static char tag_msg = CEPH_MSGR_TAG_MSG; | ||
| 29 | static char tag_ack = CEPH_MSGR_TAG_ACK; | ||
| 30 | static char tag_keepalive = CEPH_MSGR_TAG_KEEPALIVE; | ||
| 31 | |||
| 32 | |||
| 33 | static void queue_con(struct ceph_connection *con); | ||
| 34 | static void con_work(struct work_struct *); | ||
| 35 | static void ceph_fault(struct ceph_connection *con); | ||
| 36 | |||
| 37 | const char *ceph_name_type_str(int t) | ||
| 38 | { | ||
| 39 | switch (t) { | ||
| 40 | case CEPH_ENTITY_TYPE_MON: return "mon"; | ||
| 41 | case CEPH_ENTITY_TYPE_MDS: return "mds"; | ||
| 42 | case CEPH_ENTITY_TYPE_OSD: return "osd"; | ||
| 43 | case CEPH_ENTITY_TYPE_CLIENT: return "client"; | ||
| 44 | case CEPH_ENTITY_TYPE_ADMIN: return "admin"; | ||
| 45 | default: return "???"; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | /* | ||
| 50 | * nicely render a sockaddr as a string. | ||
| 51 | */ | ||
| 52 | #define MAX_ADDR_STR 20 | ||
| 53 | static char addr_str[MAX_ADDR_STR][40]; | ||
| 54 | static DEFINE_SPINLOCK(addr_str_lock); | ||
| 55 | static int last_addr_str; | ||
| 56 | |||
| 57 | const char *pr_addr(const struct sockaddr_storage *ss) | ||
| 58 | { | ||
| 59 | int i; | ||
| 60 | char *s; | ||
| 61 | struct sockaddr_in *in4 = (void *)ss; | ||
| 62 | unsigned char *quad = (void *)&in4->sin_addr.s_addr; | ||
| 63 | struct sockaddr_in6 *in6 = (void *)ss; | ||
| 64 | |||
| 65 | spin_lock(&addr_str_lock); | ||
| 66 | i = last_addr_str++; | ||
| 67 | if (last_addr_str == MAX_ADDR_STR) | ||
| 68 | last_addr_str = 0; | ||
| 69 | spin_unlock(&addr_str_lock); | ||
| 70 | s = addr_str[i]; | ||
| 71 | |||
| 72 | switch (ss->ss_family) { | ||
| 73 | case AF_INET: | ||
| 74 | sprintf(s, "%u.%u.%u.%u:%u", | ||
| 75 | (unsigned int)quad[0], | ||
| 76 | (unsigned int)quad[1], | ||
| 77 | (unsigned int)quad[2], | ||
| 78 | (unsigned int)quad[3], | ||
| 79 | (unsigned int)ntohs(in4->sin_port)); | ||
| 80 | break; | ||
| 81 | |||
| 82 | case AF_INET6: | ||
| 83 | sprintf(s, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%u", | ||
| 84 | in6->sin6_addr.s6_addr16[0], | ||
| 85 | in6->sin6_addr.s6_addr16[1], | ||
| 86 | in6->sin6_addr.s6_addr16[2], | ||
| 87 | in6->sin6_addr.s6_addr16[3], | ||
| 88 | in6->sin6_addr.s6_addr16[4], | ||
| 89 | in6->sin6_addr.s6_addr16[5], | ||
| 90 | in6->sin6_addr.s6_addr16[6], | ||
| 91 | in6->sin6_addr.s6_addr16[7], | ||
| 92 | (unsigned int)ntohs(in6->sin6_port)); | ||
| 93 | break; | ||
| 94 | |||
| 95 | default: | ||
| 96 | sprintf(s, "(unknown sockaddr family %d)", (int)ss->ss_family); | ||
| 97 | } | ||
| 98 | |||
| 99 | return s; | ||
| 100 | } | ||
| 101 | |||
| 102 | static void encode_my_addr(struct ceph_messenger *msgr) | ||
| 103 | { | ||
| 104 | memcpy(&msgr->my_enc_addr, &msgr->inst.addr, sizeof(msgr->my_enc_addr)); | ||
| 105 | ceph_encode_addr(&msgr->my_enc_addr); | ||
| 106 | } | ||
| 107 | |||
| 108 | /* | ||
| 109 | * work queue for all reading and writing to/from the socket. | ||
| 110 | */ | ||
| 111 | struct workqueue_struct *ceph_msgr_wq; | ||
| 112 | |||
| 113 | int __init ceph_msgr_init(void) | ||
| 114 | { | ||
| 115 | ceph_msgr_wq = create_workqueue("ceph-msgr"); | ||
| 116 | if (IS_ERR(ceph_msgr_wq)) { | ||
| 117 | int ret = PTR_ERR(ceph_msgr_wq); | ||
| 118 | pr_err("msgr_init failed to create workqueue: %d\n", ret); | ||
| 119 | ceph_msgr_wq = NULL; | ||
| 120 | return ret; | ||
| 121 | } | ||
| 122 | return 0; | ||
| 123 | } | ||
| 124 | |||
| 125 | void ceph_msgr_exit(void) | ||
| 126 | { | ||
| 127 | destroy_workqueue(ceph_msgr_wq); | ||
| 128 | } | ||
| 129 | |||
| 130 | /* | ||
| 131 | * socket callback functions | ||
| 132 | */ | ||
| 133 | |||
| 134 | /* data available on socket, or listen socket received a connect */ | ||
| 135 | static void ceph_data_ready(struct sock *sk, int count_unused) | ||
| 136 | { | ||
| 137 | struct ceph_connection *con = | ||
| 138 | (struct ceph_connection *)sk->sk_user_data; | ||
| 139 | if (sk->sk_state != TCP_CLOSE_WAIT) { | ||
| 140 | dout("ceph_data_ready on %p state = %lu, queueing work\n", | ||
| 141 | con, con->state); | ||
| 142 | queue_con(con); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | /* socket has buffer space for writing */ | ||
| 147 | static void ceph_write_space(struct sock *sk) | ||
| 148 | { | ||
| 149 | struct ceph_connection *con = | ||
| 150 | (struct ceph_connection *)sk->sk_user_data; | ||
| 151 | |||
| 152 | /* only queue to workqueue if there is data we want to write. */ | ||
| 153 | if (test_bit(WRITE_PENDING, &con->state)) { | ||
| 154 | dout("ceph_write_space %p queueing write work\n", con); | ||
| 155 | queue_con(con); | ||
| 156 | } else { | ||
| 157 | dout("ceph_write_space %p nothing to write\n", con); | ||
| 158 | } | ||
| 159 | |||
| 160 | /* since we have our own write_space, clear the SOCK_NOSPACE flag */ | ||
| 161 | clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags); | ||
| 162 | } | ||
| 163 | |||
| 164 | /* socket's state has changed */ | ||
| 165 | static void ceph_state_change(struct sock *sk) | ||
| 166 | { | ||
| 167 | struct ceph_connection *con = | ||
| 168 | (struct ceph_connection *)sk->sk_user_data; | ||
| 169 | |||
| 170 | dout("ceph_state_change %p state = %lu sk_state = %u\n", | ||
| 171 | con, con->state, sk->sk_state); | ||
| 172 | |||
| 173 | if (test_bit(CLOSED, &con->state)) | ||
| 174 | return; | ||
| 175 | |||
| 176 | switch (sk->sk_state) { | ||
| 177 | case TCP_CLOSE: | ||
| 178 | dout("ceph_state_change TCP_CLOSE\n"); | ||
| 179 | case TCP_CLOSE_WAIT: | ||
| 180 | dout("ceph_state_change TCP_CLOSE_WAIT\n"); | ||
| 181 | if (test_and_set_bit(SOCK_CLOSED, &con->state) == 0) { | ||
| 182 | if (test_bit(CONNECTING, &con->state)) | ||
| 183 | con->error_msg = "connection failed"; | ||
| 184 | else | ||
| 185 | con->error_msg = "socket closed"; | ||
| 186 | queue_con(con); | ||
| 187 | } | ||
| 188 | break; | ||
| 189 | case TCP_ESTABLISHED: | ||
| 190 | dout("ceph_state_change TCP_ESTABLISHED\n"); | ||
| 191 | queue_con(con); | ||
| 192 | break; | ||
| 193 | } | ||
| 194 | } | ||
| 195 | |||
| 196 | /* | ||
| 197 | * set up socket callbacks | ||
| 198 | */ | ||
| 199 | static void set_sock_callbacks(struct socket *sock, | ||
| 200 | struct ceph_connection *con) | ||
| 201 | { | ||
| 202 | struct sock *sk = sock->sk; | ||
| 203 | sk->sk_user_data = (void *)con; | ||
| 204 | sk->sk_data_ready = ceph_data_ready; | ||
| 205 | sk->sk_write_space = ceph_write_space; | ||
| 206 | sk->sk_state_change = ceph_state_change; | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | /* | ||
| 211 | * socket helpers | ||
| 212 | */ | ||
| 213 | |||
| 214 | /* | ||
| 215 | * initiate connection to a remote socket. | ||
| 216 | */ | ||
| 217 | static struct socket *ceph_tcp_connect(struct ceph_connection *con) | ||
| 218 | { | ||
| 219 | struct sockaddr *paddr = (struct sockaddr *)&con->peer_addr.in_addr; | ||
| 220 | struct socket *sock; | ||
| 221 | int ret; | ||
| 222 | |||
| 223 | BUG_ON(con->sock); | ||
| 224 | ret = sock_create_kern(AF_INET, SOCK_STREAM, IPPROTO_TCP, &sock); | ||
| 225 | if (ret) | ||
| 226 | return ERR_PTR(ret); | ||
| 227 | con->sock = sock; | ||
| 228 | sock->sk->sk_allocation = GFP_NOFS; | ||
| 229 | |||
| 230 | set_sock_callbacks(sock, con); | ||
| 231 | |||
| 232 | dout("connect %s\n", pr_addr(&con->peer_addr.in_addr)); | ||
| 233 | |||
| 234 | ret = sock->ops->connect(sock, paddr, sizeof(*paddr), O_NONBLOCK); | ||
| 235 | if (ret == -EINPROGRESS) { | ||
| 236 | dout("connect %s EINPROGRESS sk_state = %u\n", | ||
| 237 | pr_addr(&con->peer_addr.in_addr), | ||
| 238 | sock->sk->sk_state); | ||
| 239 | ret = 0; | ||
| 240 | } | ||
| 241 | if (ret < 0) { | ||
| 242 | pr_err("connect %s error %d\n", | ||
| 243 | pr_addr(&con->peer_addr.in_addr), ret); | ||
| 244 | sock_release(sock); | ||
| 245 | con->sock = NULL; | ||
| 246 | con->error_msg = "connect error"; | ||
| 247 | } | ||
| 248 | |||
| 249 | if (ret < 0) | ||
| 250 | return ERR_PTR(ret); | ||
| 251 | return sock; | ||
| 252 | } | ||
| 253 | |||
| 254 | static int ceph_tcp_recvmsg(struct socket *sock, void *buf, size_t len) | ||
| 255 | { | ||
| 256 | struct kvec iov = {buf, len}; | ||
| 257 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; | ||
| 258 | |||
| 259 | return kernel_recvmsg(sock, &msg, &iov, 1, len, msg.msg_flags); | ||
| 260 | } | ||
| 261 | |||
| 262 | /* | ||
| 263 | * write something. @more is true if caller will be sending more data | ||
| 264 | * shortly. | ||
| 265 | */ | ||
| 266 | static int ceph_tcp_sendmsg(struct socket *sock, struct kvec *iov, | ||
| 267 | size_t kvlen, size_t len, int more) | ||
| 268 | { | ||
| 269 | struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL }; | ||
| 270 | |||
| 271 | if (more) | ||
| 272 | msg.msg_flags |= MSG_MORE; | ||
| 273 | else | ||
| 274 | msg.msg_flags |= MSG_EOR; /* superfluous, but what the hell */ | ||
| 275 | |||
| 276 | return kernel_sendmsg(sock, &msg, iov, kvlen, len); | ||
| 277 | } | ||
| 278 | |||
| 279 | |||
| 280 | /* | ||
| 281 | * Shutdown/close the socket for the given connection. | ||
| 282 | */ | ||
| 283 | static int con_close_socket(struct ceph_connection *con) | ||
| 284 | { | ||
| 285 | int rc; | ||
| 286 | |||
| 287 | dout("con_close_socket on %p sock %p\n", con, con->sock); | ||
| 288 | if (!con->sock) | ||
| 289 | return 0; | ||
| 290 | set_bit(SOCK_CLOSED, &con->state); | ||
| 291 | rc = con->sock->ops->shutdown(con->sock, SHUT_RDWR); | ||
| 292 | sock_release(con->sock); | ||
| 293 | con->sock = NULL; | ||
| 294 | clear_bit(SOCK_CLOSED, &con->state); | ||
| 295 | return rc; | ||
| 296 | } | ||
| 297 | |||
| 298 | /* | ||
| 299 | * Reset a connection. Discard all incoming and outgoing messages | ||
| 300 | * and clear *_seq state. | ||
| 301 | */ | ||
| 302 | static void ceph_msg_remove(struct ceph_msg *msg) | ||
| 303 | { | ||
| 304 | list_del_init(&msg->list_head); | ||
| 305 | ceph_msg_put(msg); | ||
| 306 | } | ||
| 307 | static void ceph_msg_remove_list(struct list_head *head) | ||
| 308 | { | ||
| 309 | while (!list_empty(head)) { | ||
| 310 | struct ceph_msg *msg = list_first_entry(head, struct ceph_msg, | ||
| 311 | list_head); | ||
| 312 | ceph_msg_remove(msg); | ||
| 313 | } | ||
| 314 | } | ||
| 315 | |||
| 316 | static void reset_connection(struct ceph_connection *con) | ||
| 317 | { | ||
| 318 | /* reset connection, out_queue, msg_ and connect_seq */ | ||
| 319 | /* discard existing out_queue and msg_seq */ | ||
| 320 | ceph_msg_remove_list(&con->out_queue); | ||
| 321 | ceph_msg_remove_list(&con->out_sent); | ||
| 322 | |||
| 323 | if (con->in_msg) { | ||
| 324 | ceph_msg_put(con->in_msg); | ||
| 325 | con->in_msg = NULL; | ||
| 326 | } | ||
| 327 | |||
| 328 | con->connect_seq = 0; | ||
| 329 | con->out_seq = 0; | ||
| 330 | if (con->out_msg) { | ||
| 331 | ceph_msg_put(con->out_msg); | ||
| 332 | con->out_msg = NULL; | ||
| 333 | } | ||
| 334 | con->in_seq = 0; | ||
| 335 | } | ||
| 336 | |||
| 337 | /* | ||
| 338 | * mark a peer down. drop any open connections. | ||
| 339 | */ | ||
| 340 | void ceph_con_close(struct ceph_connection *con) | ||
| 341 | { | ||
| 342 | dout("con_close %p peer %s\n", con, pr_addr(&con->peer_addr.in_addr)); | ||
| 343 | set_bit(CLOSED, &con->state); /* in case there's queued work */ | ||
| 344 | clear_bit(STANDBY, &con->state); /* avoid connect_seq bump */ | ||
| 345 | clear_bit(LOSSYTX, &con->state); /* so we retry next connect */ | ||
| 346 | clear_bit(KEEPALIVE_PENDING, &con->state); | ||
| 347 | clear_bit(WRITE_PENDING, &con->state); | ||
| 348 | mutex_lock(&con->mutex); | ||
| 349 | reset_connection(con); | ||
| 350 | cancel_delayed_work(&con->work); | ||
| 351 | mutex_unlock(&con->mutex); | ||
| 352 | queue_con(con); | ||
| 353 | } | ||
| 354 | |||
| 355 | /* | ||
| 356 | * Reopen a closed connection, with a new peer address. | ||
| 357 | */ | ||
| 358 | void ceph_con_open(struct ceph_connection *con, struct ceph_entity_addr *addr) | ||
| 359 | { | ||
| 360 | dout("con_open %p %s\n", con, pr_addr(&addr->in_addr)); | ||
| 361 | set_bit(OPENING, &con->state); | ||
| 362 | clear_bit(CLOSED, &con->state); | ||
| 363 | memcpy(&con->peer_addr, addr, sizeof(*addr)); | ||
| 364 | con->delay = 0; /* reset backoff memory */ | ||
| 365 | queue_con(con); | ||
| 366 | } | ||
| 367 | |||
| 368 | /* | ||
| 369 | * return true if this connection ever successfully opened | ||
| 370 | */ | ||
| 371 | bool ceph_con_opened(struct ceph_connection *con) | ||
| 372 | { | ||
| 373 | return con->connect_seq > 0; | ||
| 374 | } | ||
| 375 | |||
| 376 | /* | ||
| 377 | * generic get/put | ||
| 378 | */ | ||
| 379 | struct ceph_connection *ceph_con_get(struct ceph_connection *con) | ||
| 380 | { | ||
| 381 | dout("con_get %p nref = %d -> %d\n", con, | ||
| 382 | atomic_read(&con->nref), atomic_read(&con->nref) + 1); | ||
| 383 | if (atomic_inc_not_zero(&con->nref)) | ||
| 384 | return con; | ||
| 385 | return NULL; | ||
| 386 | } | ||
| 387 | |||
| 388 | void ceph_con_put(struct ceph_connection *con) | ||
| 389 | { | ||
| 390 | dout("con_put %p nref = %d -> %d\n", con, | ||
| 391 | atomic_read(&con->nref), atomic_read(&con->nref) - 1); | ||
| 392 | BUG_ON(atomic_read(&con->nref) == 0); | ||
| 393 | if (atomic_dec_and_test(&con->nref)) { | ||
| 394 | BUG_ON(con->sock); | ||
| 395 | kfree(con); | ||
| 396 | } | ||
| 397 | } | ||
| 398 | |||
| 399 | /* | ||
| 400 | * initialize a new connection. | ||
| 401 | */ | ||
| 402 | void ceph_con_init(struct ceph_messenger *msgr, struct ceph_connection *con) | ||
| 403 | { | ||
| 404 | dout("con_init %p\n", con); | ||
| 405 | memset(con, 0, sizeof(*con)); | ||
| 406 | atomic_set(&con->nref, 1); | ||
| 407 | con->msgr = msgr; | ||
| 408 | mutex_init(&con->mutex); | ||
| 409 | INIT_LIST_HEAD(&con->out_queue); | ||
| 410 | INIT_LIST_HEAD(&con->out_sent); | ||
| 411 | INIT_DELAYED_WORK(&con->work, con_work); | ||
| 412 | } | ||
| 413 | |||
| 414 | |||
| 415 | /* | ||
| 416 | * We maintain a global counter to order connection attempts. Get | ||
| 417 | * a unique seq greater than @gt. | ||
| 418 | */ | ||
| 419 | static u32 get_global_seq(struct ceph_messenger *msgr, u32 gt) | ||
| 420 | { | ||
| 421 | u32 ret; | ||
| 422 | |||
| 423 | spin_lock(&msgr->global_seq_lock); | ||
| 424 | if (msgr->global_seq < gt) | ||
| 425 | msgr->global_seq = gt; | ||
| 426 | ret = ++msgr->global_seq; | ||
| 427 | spin_unlock(&msgr->global_seq_lock); | ||
| 428 | return ret; | ||
| 429 | } | ||
| 430 | |||
| 431 | |||
| 432 | /* | ||
| 433 | * Prepare footer for currently outgoing message, and finish things | ||
| 434 | * off. Assumes out_kvec* are already valid.. we just add on to the end. | ||
| 435 | */ | ||
| 436 | static void prepare_write_message_footer(struct ceph_connection *con, int v) | ||
| 437 | { | ||
| 438 | struct ceph_msg *m = con->out_msg; | ||
| 439 | |||
| 440 | dout("prepare_write_message_footer %p\n", con); | ||
| 441 | con->out_kvec_is_msg = true; | ||
| 442 | con->out_kvec[v].iov_base = &m->footer; | ||
| 443 | con->out_kvec[v].iov_len = sizeof(m->footer); | ||
| 444 | con->out_kvec_bytes += sizeof(m->footer); | ||
| 445 | con->out_kvec_left++; | ||
| 446 | con->out_more = m->more_to_follow; | ||
| 447 | con->out_msg_done = true; | ||
| 448 | } | ||
| 449 | |||
| 450 | /* | ||
| 451 | * Prepare headers for the next outgoing message. | ||
| 452 | */ | ||
| 453 | static void prepare_write_message(struct ceph_connection *con) | ||
| 454 | { | ||
| 455 | struct ceph_msg *m; | ||
| 456 | int v = 0; | ||
| 457 | |||
| 458 | con->out_kvec_bytes = 0; | ||
| 459 | con->out_kvec_is_msg = true; | ||
| 460 | con->out_msg_done = false; | ||
| 461 | |||
| 462 | /* Sneak an ack in there first? If we can get it into the same | ||
| 463 | * TCP packet that's a good thing. */ | ||
| 464 | if (con->in_seq > con->in_seq_acked) { | ||
| 465 | con->in_seq_acked = con->in_seq; | ||
| 466 | con->out_kvec[v].iov_base = &tag_ack; | ||
| 467 | con->out_kvec[v++].iov_len = 1; | ||
| 468 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); | ||
| 469 | con->out_kvec[v].iov_base = &con->out_temp_ack; | ||
| 470 | con->out_kvec[v++].iov_len = sizeof(con->out_temp_ack); | ||
| 471 | con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack); | ||
| 472 | } | ||
| 473 | |||
| 474 | m = list_first_entry(&con->out_queue, | ||
| 475 | struct ceph_msg, list_head); | ||
| 476 | con->out_msg = m; | ||
| 477 | if (test_bit(LOSSYTX, &con->state)) { | ||
| 478 | list_del_init(&m->list_head); | ||
| 479 | } else { | ||
| 480 | /* put message on sent list */ | ||
| 481 | ceph_msg_get(m); | ||
| 482 | list_move_tail(&m->list_head, &con->out_sent); | ||
| 483 | } | ||
| 484 | |||
| 485 | m->hdr.seq = cpu_to_le64(++con->out_seq); | ||
| 486 | |||
| 487 | dout("prepare_write_message %p seq %lld type %d len %d+%d+%d %d pgs\n", | ||
| 488 | m, con->out_seq, le16_to_cpu(m->hdr.type), | ||
| 489 | le32_to_cpu(m->hdr.front_len), le32_to_cpu(m->hdr.middle_len), | ||
| 490 | le32_to_cpu(m->hdr.data_len), | ||
| 491 | m->nr_pages); | ||
| 492 | BUG_ON(le32_to_cpu(m->hdr.front_len) != m->front.iov_len); | ||
| 493 | |||
| 494 | /* tag + hdr + front + middle */ | ||
| 495 | con->out_kvec[v].iov_base = &tag_msg; | ||
| 496 | con->out_kvec[v++].iov_len = 1; | ||
| 497 | con->out_kvec[v].iov_base = &m->hdr; | ||
| 498 | con->out_kvec[v++].iov_len = sizeof(m->hdr); | ||
| 499 | con->out_kvec[v++] = m->front; | ||
| 500 | if (m->middle) | ||
| 501 | con->out_kvec[v++] = m->middle->vec; | ||
| 502 | con->out_kvec_left = v; | ||
| 503 | con->out_kvec_bytes += 1 + sizeof(m->hdr) + m->front.iov_len + | ||
| 504 | (m->middle ? m->middle->vec.iov_len : 0); | ||
| 505 | con->out_kvec_cur = con->out_kvec; | ||
| 506 | |||
| 507 | /* fill in crc (except data pages), footer */ | ||
| 508 | con->out_msg->hdr.crc = | ||
| 509 | cpu_to_le32(crc32c(0, (void *)&m->hdr, | ||
| 510 | sizeof(m->hdr) - sizeof(m->hdr.crc))); | ||
| 511 | con->out_msg->footer.flags = CEPH_MSG_FOOTER_COMPLETE; | ||
| 512 | con->out_msg->footer.front_crc = | ||
| 513 | cpu_to_le32(crc32c(0, m->front.iov_base, m->front.iov_len)); | ||
| 514 | if (m->middle) | ||
| 515 | con->out_msg->footer.middle_crc = | ||
| 516 | cpu_to_le32(crc32c(0, m->middle->vec.iov_base, | ||
| 517 | m->middle->vec.iov_len)); | ||
| 518 | else | ||
| 519 | con->out_msg->footer.middle_crc = 0; | ||
| 520 | con->out_msg->footer.data_crc = 0; | ||
| 521 | dout("prepare_write_message front_crc %u data_crc %u\n", | ||
| 522 | le32_to_cpu(con->out_msg->footer.front_crc), | ||
| 523 | le32_to_cpu(con->out_msg->footer.middle_crc)); | ||
| 524 | |||
| 525 | /* is there a data payload? */ | ||
| 526 | if (le32_to_cpu(m->hdr.data_len) > 0) { | ||
| 527 | /* initialize page iterator */ | ||
| 528 | con->out_msg_pos.page = 0; | ||
| 529 | con->out_msg_pos.page_pos = | ||
| 530 | le16_to_cpu(m->hdr.data_off) & ~PAGE_MASK; | ||
| 531 | con->out_msg_pos.data_pos = 0; | ||
| 532 | con->out_msg_pos.did_page_crc = 0; | ||
| 533 | con->out_more = 1; /* data + footer will follow */ | ||
| 534 | } else { | ||
| 535 | /* no, queue up footer too and be done */ | ||
| 536 | prepare_write_message_footer(con, v); | ||
| 537 | } | ||
| 538 | |||
| 539 | set_bit(WRITE_PENDING, &con->state); | ||
| 540 | } | ||
| 541 | |||
| 542 | /* | ||
| 543 | * Prepare an ack. | ||
| 544 | */ | ||
| 545 | static void prepare_write_ack(struct ceph_connection *con) | ||
| 546 | { | ||
| 547 | dout("prepare_write_ack %p %llu -> %llu\n", con, | ||
| 548 | con->in_seq_acked, con->in_seq); | ||
| 549 | con->in_seq_acked = con->in_seq; | ||
| 550 | |||
| 551 | con->out_kvec[0].iov_base = &tag_ack; | ||
| 552 | con->out_kvec[0].iov_len = 1; | ||
| 553 | con->out_temp_ack = cpu_to_le64(con->in_seq_acked); | ||
| 554 | con->out_kvec[1].iov_base = &con->out_temp_ack; | ||
| 555 | con->out_kvec[1].iov_len = sizeof(con->out_temp_ack); | ||
| 556 | con->out_kvec_left = 2; | ||
| 557 | con->out_kvec_bytes = 1 + sizeof(con->out_temp_ack); | ||
| 558 | con->out_kvec_cur = con->out_kvec; | ||
| 559 | con->out_more = 1; /* more will follow.. eventually.. */ | ||
| 560 | set_bit(WRITE_PENDING, &con->state); | ||
| 561 | } | ||
| 562 | |||
| 563 | /* | ||
| 564 | * Prepare to write keepalive byte. | ||
| 565 | */ | ||
| 566 | static void prepare_write_keepalive(struct ceph_connection *con) | ||
| 567 | { | ||
| 568 | dout("prepare_write_keepalive %p\n", con); | ||
| 569 | con->out_kvec[0].iov_base = &tag_keepalive; | ||
| 570 | con->out_kvec[0].iov_len = 1; | ||
| 571 | con->out_kvec_left = 1; | ||
| 572 | con->out_kvec_bytes = 1; | ||
| 573 | con->out_kvec_cur = con->out_kvec; | ||
| 574 | set_bit(WRITE_PENDING, &con->state); | ||
| 575 | } | ||
| 576 | |||
| 577 | /* | ||
| 578 | * Connection negotiation. | ||
| 579 | */ | ||
| 580 | |||
| 581 | static void prepare_connect_authorizer(struct ceph_connection *con) | ||
| 582 | { | ||
| 583 | void *auth_buf; | ||
| 584 | int auth_len = 0; | ||
| 585 | int auth_protocol = 0; | ||
| 586 | |||
| 587 | mutex_unlock(&con->mutex); | ||
| 588 | if (con->ops->get_authorizer) | ||
| 589 | con->ops->get_authorizer(con, &auth_buf, &auth_len, | ||
| 590 | &auth_protocol, &con->auth_reply_buf, | ||
| 591 | &con->auth_reply_buf_len, | ||
| 592 | con->auth_retry); | ||
| 593 | mutex_lock(&con->mutex); | ||
| 594 | |||
| 595 | con->out_connect.authorizer_protocol = cpu_to_le32(auth_protocol); | ||
| 596 | con->out_connect.authorizer_len = cpu_to_le32(auth_len); | ||
| 597 | |||
| 598 | con->out_kvec[con->out_kvec_left].iov_base = auth_buf; | ||
| 599 | con->out_kvec[con->out_kvec_left].iov_len = auth_len; | ||
| 600 | con->out_kvec_left++; | ||
| 601 | con->out_kvec_bytes += auth_len; | ||
| 602 | } | ||
| 603 | |||
| 604 | /* | ||
| 605 | * We connected to a peer and are saying hello. | ||
| 606 | */ | ||
| 607 | static void prepare_write_banner(struct ceph_messenger *msgr, | ||
| 608 | struct ceph_connection *con) | ||
| 609 | { | ||
| 610 | int len = strlen(CEPH_BANNER); | ||
| 611 | |||
| 612 | con->out_kvec[0].iov_base = CEPH_BANNER; | ||
| 613 | con->out_kvec[0].iov_len = len; | ||
| 614 | con->out_kvec[1].iov_base = &msgr->my_enc_addr; | ||
| 615 | con->out_kvec[1].iov_len = sizeof(msgr->my_enc_addr); | ||
| 616 | con->out_kvec_left = 2; | ||
| 617 | con->out_kvec_bytes = len + sizeof(msgr->my_enc_addr); | ||
| 618 | con->out_kvec_cur = con->out_kvec; | ||
| 619 | con->out_more = 0; | ||
| 620 | set_bit(WRITE_PENDING, &con->state); | ||
| 621 | } | ||
| 622 | |||
| 623 | static void prepare_write_connect(struct ceph_messenger *msgr, | ||
| 624 | struct ceph_connection *con, | ||
| 625 | int after_banner) | ||
| 626 | { | ||
| 627 | unsigned global_seq = get_global_seq(con->msgr, 0); | ||
| 628 | int proto; | ||
| 629 | |||
| 630 | switch (con->peer_name.type) { | ||
| 631 | case CEPH_ENTITY_TYPE_MON: | ||
| 632 | proto = CEPH_MONC_PROTOCOL; | ||
| 633 | break; | ||
| 634 | case CEPH_ENTITY_TYPE_OSD: | ||
| 635 | proto = CEPH_OSDC_PROTOCOL; | ||
| 636 | break; | ||
| 637 | case CEPH_ENTITY_TYPE_MDS: | ||
| 638 | proto = CEPH_MDSC_PROTOCOL; | ||
| 639 | break; | ||
| 640 | default: | ||
| 641 | BUG(); | ||
| 642 | } | ||
| 643 | |||
| 644 | dout("prepare_write_connect %p cseq=%d gseq=%d proto=%d\n", con, | ||
| 645 | con->connect_seq, global_seq, proto); | ||
| 646 | |||
| 647 | con->out_connect.features = CEPH_FEATURE_SUPPORTED; | ||
| 648 | con->out_connect.host_type = cpu_to_le32(CEPH_ENTITY_TYPE_CLIENT); | ||
| 649 | con->out_connect.connect_seq = cpu_to_le32(con->connect_seq); | ||
| 650 | con->out_connect.global_seq = cpu_to_le32(global_seq); | ||
| 651 | con->out_connect.protocol_version = cpu_to_le32(proto); | ||
| 652 | con->out_connect.flags = 0; | ||
| 653 | |||
| 654 | if (!after_banner) { | ||
| 655 | con->out_kvec_left = 0; | ||
| 656 | con->out_kvec_bytes = 0; | ||
| 657 | } | ||
| 658 | con->out_kvec[con->out_kvec_left].iov_base = &con->out_connect; | ||
| 659 | con->out_kvec[con->out_kvec_left].iov_len = sizeof(con->out_connect); | ||
| 660 | con->out_kvec_left++; | ||
| 661 | con->out_kvec_bytes += sizeof(con->out_connect); | ||
| 662 | con->out_kvec_cur = con->out_kvec; | ||
| 663 | con->out_more = 0; | ||
| 664 | set_bit(WRITE_PENDING, &con->state); | ||
| 665 | |||
| 666 | prepare_connect_authorizer(con); | ||
| 667 | } | ||
| 668 | |||
| 669 | |||
| 670 | /* | ||
| 671 | * write as much of pending kvecs to the socket as we can. | ||
| 672 | * 1 -> done | ||
| 673 | * 0 -> socket full, but more to do | ||
| 674 | * <0 -> error | ||
| 675 | */ | ||
| 676 | static int write_partial_kvec(struct ceph_connection *con) | ||
| 677 | { | ||
| 678 | int ret; | ||
| 679 | |||
| 680 | dout("write_partial_kvec %p %d left\n", con, con->out_kvec_bytes); | ||
| 681 | while (con->out_kvec_bytes > 0) { | ||
| 682 | ret = ceph_tcp_sendmsg(con->sock, con->out_kvec_cur, | ||
| 683 | con->out_kvec_left, con->out_kvec_bytes, | ||
| 684 | con->out_more); | ||
| 685 | if (ret <= 0) | ||
| 686 | goto out; | ||
| 687 | con->out_kvec_bytes -= ret; | ||
| 688 | if (con->out_kvec_bytes == 0) | ||
| 689 | break; /* done */ | ||
| 690 | while (ret > 0) { | ||
| 691 | if (ret >= con->out_kvec_cur->iov_len) { | ||
| 692 | ret -= con->out_kvec_cur->iov_len; | ||
| 693 | con->out_kvec_cur++; | ||
| 694 | con->out_kvec_left--; | ||
| 695 | } else { | ||
| 696 | con->out_kvec_cur->iov_len -= ret; | ||
| 697 | con->out_kvec_cur->iov_base += ret; | ||
| 698 | ret = 0; | ||
| 699 | break; | ||
| 700 | } | ||
| 701 | } | ||
| 702 | } | ||
| 703 | con->out_kvec_left = 0; | ||
| 704 | con->out_kvec_is_msg = false; | ||
| 705 | ret = 1; | ||
| 706 | out: | ||
| 707 | dout("write_partial_kvec %p %d left in %d kvecs ret = %d\n", con, | ||
| 708 | con->out_kvec_bytes, con->out_kvec_left, ret); | ||
| 709 | return ret; /* done! */ | ||
| 710 | } | ||
| 711 | |||
| 712 | /* | ||
| 713 | * Write as much message data payload as we can. If we finish, queue | ||
| 714 | * up the footer. | ||
| 715 | * 1 -> done, footer is now queued in out_kvec[]. | ||
| 716 | * 0 -> socket full, but more to do | ||
| 717 | * <0 -> error | ||
| 718 | */ | ||
| 719 | static int write_partial_msg_pages(struct ceph_connection *con) | ||
| 720 | { | ||
| 721 | struct ceph_msg *msg = con->out_msg; | ||
| 722 | unsigned data_len = le32_to_cpu(msg->hdr.data_len); | ||
| 723 | size_t len; | ||
| 724 | int crc = con->msgr->nocrc; | ||
| 725 | int ret; | ||
| 726 | |||
| 727 | dout("write_partial_msg_pages %p msg %p page %d/%d offset %d\n", | ||
| 728 | con, con->out_msg, con->out_msg_pos.page, con->out_msg->nr_pages, | ||
| 729 | con->out_msg_pos.page_pos); | ||
| 730 | |||
| 731 | while (con->out_msg_pos.page < con->out_msg->nr_pages) { | ||
| 732 | struct page *page = NULL; | ||
| 733 | void *kaddr = NULL; | ||
| 734 | |||
| 735 | /* | ||
| 736 | * if we are calculating the data crc (the default), we need | ||
| 737 | * to map the page. if our pages[] has been revoked, use the | ||
| 738 | * zero page. | ||
| 739 | */ | ||
| 740 | if (msg->pages) { | ||
| 741 | page = msg->pages[con->out_msg_pos.page]; | ||
| 742 | if (crc) | ||
| 743 | kaddr = kmap(page); | ||
| 744 | } else if (msg->pagelist) { | ||
| 745 | page = list_first_entry(&msg->pagelist->head, | ||
| 746 | struct page, lru); | ||
| 747 | if (crc) | ||
| 748 | kaddr = kmap(page); | ||
| 749 | } else { | ||
| 750 | page = con->msgr->zero_page; | ||
| 751 | if (crc) | ||
| 752 | kaddr = page_address(con->msgr->zero_page); | ||
| 753 | } | ||
| 754 | len = min((int)(PAGE_SIZE - con->out_msg_pos.page_pos), | ||
| 755 | (int)(data_len - con->out_msg_pos.data_pos)); | ||
| 756 | if (crc && !con->out_msg_pos.did_page_crc) { | ||
| 757 | void *base = kaddr + con->out_msg_pos.page_pos; | ||
| 758 | u32 tmpcrc = le32_to_cpu(con->out_msg->footer.data_crc); | ||
| 759 | |||
| 760 | BUG_ON(kaddr == NULL); | ||
| 761 | con->out_msg->footer.data_crc = | ||
| 762 | cpu_to_le32(crc32c(tmpcrc, base, len)); | ||
| 763 | con->out_msg_pos.did_page_crc = 1; | ||
| 764 | } | ||
| 765 | |||
| 766 | ret = kernel_sendpage(con->sock, page, | ||
| 767 | con->out_msg_pos.page_pos, len, | ||
| 768 | MSG_DONTWAIT | MSG_NOSIGNAL | | ||
| 769 | MSG_MORE); | ||
| 770 | |||
| 771 | if (crc && (msg->pages || msg->pagelist)) | ||
| 772 | kunmap(page); | ||
| 773 | |||
| 774 | if (ret <= 0) | ||
| 775 | goto out; | ||
| 776 | |||
| 777 | con->out_msg_pos.data_pos += ret; | ||
| 778 | con->out_msg_pos.page_pos += ret; | ||
| 779 | if (ret == len) { | ||
| 780 | con->out_msg_pos.page_pos = 0; | ||
| 781 | con->out_msg_pos.page++; | ||
| 782 | con->out_msg_pos.did_page_crc = 0; | ||
| 783 | if (msg->pagelist) | ||
| 784 | list_move_tail(&page->lru, | ||
| 785 | &msg->pagelist->head); | ||
| 786 | } | ||
| 787 | } | ||
| 788 | |||
| 789 | dout("write_partial_msg_pages %p msg %p done\n", con, msg); | ||
| 790 | |||
| 791 | /* prepare and queue up footer, too */ | ||
| 792 | if (!crc) | ||
| 793 | con->out_msg->footer.flags |= CEPH_MSG_FOOTER_NOCRC; | ||
| 794 | con->out_kvec_bytes = 0; | ||
| 795 | con->out_kvec_left = 0; | ||
| 796 | con->out_kvec_cur = con->out_kvec; | ||
| 797 | prepare_write_message_footer(con, 0); | ||
| 798 | ret = 1; | ||
| 799 | out: | ||
| 800 | return ret; | ||
| 801 | } | ||
| 802 | |||
| 803 | /* | ||
| 804 | * write some zeros | ||
| 805 | */ | ||
| 806 | static int write_partial_skip(struct ceph_connection *con) | ||
| 807 | { | ||
| 808 | int ret; | ||
| 809 | |||
| 810 | while (con->out_skip > 0) { | ||
| 811 | struct kvec iov = { | ||
| 812 | .iov_base = page_address(con->msgr->zero_page), | ||
| 813 | .iov_len = min(con->out_skip, (int)PAGE_CACHE_SIZE) | ||
| 814 | }; | ||
| 815 | |||
| 816 | ret = ceph_tcp_sendmsg(con->sock, &iov, 1, iov.iov_len, 1); | ||
| 817 | if (ret <= 0) | ||
| 818 | goto out; | ||
| 819 | con->out_skip -= ret; | ||
| 820 | } | ||
| 821 | ret = 1; | ||
| 822 | out: | ||
| 823 | return ret; | ||
| 824 | } | ||
| 825 | |||
| 826 | /* | ||
| 827 | * Prepare to read connection handshake, or an ack. | ||
| 828 | */ | ||
| 829 | static void prepare_read_banner(struct ceph_connection *con) | ||
| 830 | { | ||
| 831 | dout("prepare_read_banner %p\n", con); | ||
| 832 | con->in_base_pos = 0; | ||
| 833 | } | ||
| 834 | |||
| 835 | static void prepare_read_connect(struct ceph_connection *con) | ||
| 836 | { | ||
| 837 | dout("prepare_read_connect %p\n", con); | ||
| 838 | con->in_base_pos = 0; | ||
| 839 | } | ||
| 840 | |||
| 841 | static void prepare_read_ack(struct ceph_connection *con) | ||
| 842 | { | ||
| 843 | dout("prepare_read_ack %p\n", con); | ||
| 844 | con->in_base_pos = 0; | ||
| 845 | } | ||
| 846 | |||
| 847 | static void prepare_read_tag(struct ceph_connection *con) | ||
| 848 | { | ||
| 849 | dout("prepare_read_tag %p\n", con); | ||
| 850 | con->in_base_pos = 0; | ||
| 851 | con->in_tag = CEPH_MSGR_TAG_READY; | ||
| 852 | } | ||
| 853 | |||
| 854 | /* | ||
| 855 | * Prepare to read a message. | ||
| 856 | */ | ||
| 857 | static int prepare_read_message(struct ceph_connection *con) | ||
| 858 | { | ||
| 859 | dout("prepare_read_message %p\n", con); | ||
| 860 | BUG_ON(con->in_msg != NULL); | ||
| 861 | con->in_base_pos = 0; | ||
| 862 | con->in_front_crc = con->in_middle_crc = con->in_data_crc = 0; | ||
| 863 | return 0; | ||
| 864 | } | ||
| 865 | |||
| 866 | |||
| 867 | static int read_partial(struct ceph_connection *con, | ||
| 868 | int *to, int size, void *object) | ||
| 869 | { | ||
| 870 | *to += size; | ||
| 871 | while (con->in_base_pos < *to) { | ||
| 872 | int left = *to - con->in_base_pos; | ||
| 873 | int have = size - left; | ||
| 874 | int ret = ceph_tcp_recvmsg(con->sock, object + have, left); | ||
| 875 | if (ret <= 0) | ||
| 876 | return ret; | ||
| 877 | con->in_base_pos += ret; | ||
| 878 | } | ||
| 879 | return 1; | ||
| 880 | } | ||
| 881 | |||
| 882 | |||
| 883 | /* | ||
| 884 | * Read all or part of the connect-side handshake on a new connection | ||
| 885 | */ | ||
| 886 | static int read_partial_banner(struct ceph_connection *con) | ||
| 887 | { | ||
| 888 | int ret, to = 0; | ||
| 889 | |||
| 890 | dout("read_partial_banner %p at %d\n", con, con->in_base_pos); | ||
| 891 | |||
| 892 | /* peer's banner */ | ||
| 893 | ret = read_partial(con, &to, strlen(CEPH_BANNER), con->in_banner); | ||
| 894 | if (ret <= 0) | ||
| 895 | goto out; | ||
| 896 | ret = read_partial(con, &to, sizeof(con->actual_peer_addr), | ||
| 897 | &con->actual_peer_addr); | ||
| 898 | if (ret <= 0) | ||
| 899 | goto out; | ||
| 900 | ret = read_partial(con, &to, sizeof(con->peer_addr_for_me), | ||
| 901 | &con->peer_addr_for_me); | ||
| 902 | if (ret <= 0) | ||
| 903 | goto out; | ||
| 904 | out: | ||
| 905 | return ret; | ||
| 906 | } | ||
| 907 | |||
| 908 | static int read_partial_connect(struct ceph_connection *con) | ||
| 909 | { | ||
| 910 | int ret, to = 0; | ||
| 911 | |||
| 912 | dout("read_partial_connect %p at %d\n", con, con->in_base_pos); | ||
| 913 | |||
| 914 | ret = read_partial(con, &to, sizeof(con->in_reply), &con->in_reply); | ||
| 915 | if (ret <= 0) | ||
| 916 | goto out; | ||
| 917 | ret = read_partial(con, &to, le32_to_cpu(con->in_reply.authorizer_len), | ||
| 918 | con->auth_reply_buf); | ||
| 919 | if (ret <= 0) | ||
| 920 | goto out; | ||
| 921 | |||
| 922 | dout("read_partial_connect %p tag %d, con_seq = %u, g_seq = %u\n", | ||
| 923 | con, (int)con->in_reply.tag, | ||
| 924 | le32_to_cpu(con->in_reply.connect_seq), | ||
| 925 | le32_to_cpu(con->in_reply.global_seq)); | ||
| 926 | out: | ||
| 927 | return ret; | ||
| 928 | |||
| 929 | } | ||
| 930 | |||
| 931 | /* | ||
| 932 | * Verify the hello banner looks okay. | ||
| 933 | */ | ||
| 934 | static int verify_hello(struct ceph_connection *con) | ||
| 935 | { | ||
| 936 | if (memcmp(con->in_banner, CEPH_BANNER, strlen(CEPH_BANNER))) { | ||
| 937 | pr_err("connect to %s got bad banner\n", | ||
| 938 | pr_addr(&con->peer_addr.in_addr)); | ||
| 939 | con->error_msg = "protocol error, bad banner"; | ||
| 940 | return -1; | ||
| 941 | } | ||
| 942 | return 0; | ||
| 943 | } | ||
| 944 | |||
| 945 | static bool addr_is_blank(struct sockaddr_storage *ss) | ||
| 946 | { | ||
| 947 | switch (ss->ss_family) { | ||
| 948 | case AF_INET: | ||
| 949 | return ((struct sockaddr_in *)ss)->sin_addr.s_addr == 0; | ||
| 950 | case AF_INET6: | ||
| 951 | return | ||
| 952 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[0] == 0 && | ||
| 953 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[1] == 0 && | ||
| 954 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[2] == 0 && | ||
| 955 | ((struct sockaddr_in6 *)ss)->sin6_addr.s6_addr32[3] == 0; | ||
| 956 | } | ||
| 957 | return false; | ||
| 958 | } | ||
| 959 | |||
| 960 | static int addr_port(struct sockaddr_storage *ss) | ||
| 961 | { | ||
| 962 | switch (ss->ss_family) { | ||
| 963 | case AF_INET: | ||
| 964 | return ntohs(((struct sockaddr_in *)ss)->sin_port); | ||
| 965 | case AF_INET6: | ||
| 966 | return ntohs(((struct sockaddr_in6 *)ss)->sin6_port); | ||
| 967 | } | ||
| 968 | return 0; | ||
| 969 | } | ||
| 970 | |||
| 971 | static void addr_set_port(struct sockaddr_storage *ss, int p) | ||
| 972 | { | ||
| 973 | switch (ss->ss_family) { | ||
| 974 | case AF_INET: | ||
| 975 | ((struct sockaddr_in *)ss)->sin_port = htons(p); | ||
| 976 | case AF_INET6: | ||
| 977 | ((struct sockaddr_in6 *)ss)->sin6_port = htons(p); | ||
| 978 | } | ||
| 979 | } | ||
| 980 | |||
| 981 | /* | ||
| 982 | * Parse an ip[:port] list into an addr array. Use the default | ||
| 983 | * monitor port if a port isn't specified. | ||
| 984 | */ | ||
| 985 | int ceph_parse_ips(const char *c, const char *end, | ||
| 986 | struct ceph_entity_addr *addr, | ||
| 987 | int max_count, int *count) | ||
| 988 | { | ||
| 989 | int i; | ||
| 990 | const char *p = c; | ||
| 991 | |||
| 992 | dout("parse_ips on '%.*s'\n", (int)(end-c), c); | ||
| 993 | for (i = 0; i < max_count; i++) { | ||
| 994 | const char *ipend; | ||
| 995 | struct sockaddr_storage *ss = &addr[i].in_addr; | ||
| 996 | struct sockaddr_in *in4 = (void *)ss; | ||
| 997 | struct sockaddr_in6 *in6 = (void *)ss; | ||
| 998 | int port; | ||
| 999 | |||
| 1000 | memset(ss, 0, sizeof(*ss)); | ||
| 1001 | if (in4_pton(p, end - p, (u8 *)&in4->sin_addr.s_addr, | ||
| 1002 | ',', &ipend)) { | ||
| 1003 | ss->ss_family = AF_INET; | ||
| 1004 | } else if (in6_pton(p, end - p, (u8 *)&in6->sin6_addr.s6_addr, | ||
| 1005 | ',', &ipend)) { | ||
| 1006 | ss->ss_family = AF_INET6; | ||
| 1007 | } else { | ||
| 1008 | goto bad; | ||
| 1009 | } | ||
| 1010 | p = ipend; | ||
| 1011 | |||
| 1012 | /* port? */ | ||
| 1013 | if (p < end && *p == ':') { | ||
| 1014 | port = 0; | ||
| 1015 | p++; | ||
| 1016 | while (p < end && *p >= '0' && *p <= '9') { | ||
| 1017 | port = (port * 10) + (*p - '0'); | ||
| 1018 | p++; | ||
| 1019 | } | ||
| 1020 | if (port > 65535 || port == 0) | ||
| 1021 | goto bad; | ||
| 1022 | } else { | ||
| 1023 | port = CEPH_MON_PORT; | ||
| 1024 | } | ||
| 1025 | |||
| 1026 | addr_set_port(ss, port); | ||
| 1027 | |||
| 1028 | dout("parse_ips got %s\n", pr_addr(ss)); | ||
| 1029 | |||
| 1030 | if (p == end) | ||
| 1031 | break; | ||
| 1032 | if (*p != ',') | ||
| 1033 | goto bad; | ||
| 1034 | p++; | ||
| 1035 | } | ||
| 1036 | |||
| 1037 | if (p != end) | ||
| 1038 | goto bad; | ||
| 1039 | |||
| 1040 | if (count) | ||
| 1041 | *count = i + 1; | ||
| 1042 | return 0; | ||
| 1043 | |||
| 1044 | bad: | ||
| 1045 | pr_err("parse_ips bad ip '%s'\n", c); | ||
| 1046 | return -EINVAL; | ||
| 1047 | } | ||
| 1048 | |||
| 1049 | static int process_banner(struct ceph_connection *con) | ||
| 1050 | { | ||
| 1051 | dout("process_banner on %p\n", con); | ||
| 1052 | |||
| 1053 | if (verify_hello(con) < 0) | ||
| 1054 | return -1; | ||
| 1055 | |||
| 1056 | ceph_decode_addr(&con->actual_peer_addr); | ||
| 1057 | ceph_decode_addr(&con->peer_addr_for_me); | ||
| 1058 | |||
| 1059 | /* | ||
| 1060 | * Make sure the other end is who we wanted. note that the other | ||
| 1061 | * end may not yet know their ip address, so if it's 0.0.0.0, give | ||
| 1062 | * them the benefit of the doubt. | ||
| 1063 | */ | ||
| 1064 | if (memcmp(&con->peer_addr, &con->actual_peer_addr, | ||
| 1065 | sizeof(con->peer_addr)) != 0 && | ||
| 1066 | !(addr_is_blank(&con->actual_peer_addr.in_addr) && | ||
| 1067 | con->actual_peer_addr.nonce == con->peer_addr.nonce)) { | ||
| 1068 | pr_warning("wrong peer, want %s/%lld, got %s/%lld\n", | ||
| 1069 | pr_addr(&con->peer_addr.in_addr), | ||
| 1070 | le64_to_cpu(con->peer_addr.nonce), | ||
| 1071 | pr_addr(&con->actual_peer_addr.in_addr), | ||
| 1072 | le64_to_cpu(con->actual_peer_addr.nonce)); | ||
| 1073 | con->error_msg = "wrong peer at address"; | ||
| 1074 | return -1; | ||
| 1075 | } | ||
| 1076 | |||
| 1077 | /* | ||
| 1078 | * did we learn our address? | ||
| 1079 | */ | ||
| 1080 | if (addr_is_blank(&con->msgr->inst.addr.in_addr)) { | ||
| 1081 | int port = addr_port(&con->msgr->inst.addr.in_addr); | ||
| 1082 | |||
| 1083 | memcpy(&con->msgr->inst.addr.in_addr, | ||
| 1084 | &con->peer_addr_for_me.in_addr, | ||
| 1085 | sizeof(con->peer_addr_for_me.in_addr)); | ||
| 1086 | addr_set_port(&con->msgr->inst.addr.in_addr, port); | ||
| 1087 | encode_my_addr(con->msgr); | ||
| 1088 | dout("process_banner learned my addr is %s\n", | ||
| 1089 | pr_addr(&con->msgr->inst.addr.in_addr)); | ||
| 1090 | } | ||
| 1091 | |||
| 1092 | set_bit(NEGOTIATING, &con->state); | ||
| 1093 | prepare_read_connect(con); | ||
| 1094 | return 0; | ||
| 1095 | } | ||
| 1096 | |||
| 1097 | static void fail_protocol(struct ceph_connection *con) | ||
| 1098 | { | ||
| 1099 | reset_connection(con); | ||
| 1100 | set_bit(CLOSED, &con->state); /* in case there's queued work */ | ||
| 1101 | |||
| 1102 | mutex_unlock(&con->mutex); | ||
| 1103 | if (con->ops->bad_proto) | ||
| 1104 | con->ops->bad_proto(con); | ||
| 1105 | mutex_lock(&con->mutex); | ||
| 1106 | } | ||
| 1107 | |||
| 1108 | static int process_connect(struct ceph_connection *con) | ||
| 1109 | { | ||
| 1110 | u64 sup_feat = CEPH_FEATURE_SUPPORTED; | ||
| 1111 | u64 req_feat = CEPH_FEATURE_REQUIRED; | ||
| 1112 | u64 server_feat = le64_to_cpu(con->in_reply.features); | ||
| 1113 | |||
| 1114 | dout("process_connect on %p tag %d\n", con, (int)con->in_tag); | ||
| 1115 | |||
| 1116 | switch (con->in_reply.tag) { | ||
| 1117 | case CEPH_MSGR_TAG_FEATURES: | ||
| 1118 | pr_err("%s%lld %s feature set mismatch," | ||
| 1119 | " my %llx < server's %llx, missing %llx\n", | ||
| 1120 | ENTITY_NAME(con->peer_name), | ||
| 1121 | pr_addr(&con->peer_addr.in_addr), | ||
| 1122 | sup_feat, server_feat, server_feat & ~sup_feat); | ||
| 1123 | con->error_msg = "missing required protocol features"; | ||
| 1124 | fail_protocol(con); | ||
| 1125 | return -1; | ||
| 1126 | |||
| 1127 | case CEPH_MSGR_TAG_BADPROTOVER: | ||
| 1128 | pr_err("%s%lld %s protocol version mismatch," | ||
| 1129 | " my %d != server's %d\n", | ||
| 1130 | ENTITY_NAME(con->peer_name), | ||
| 1131 | pr_addr(&con->peer_addr.in_addr), | ||
| 1132 | le32_to_cpu(con->out_connect.protocol_version), | ||
| 1133 | le32_to_cpu(con->in_reply.protocol_version)); | ||
| 1134 | con->error_msg = "protocol version mismatch"; | ||
| 1135 | fail_protocol(con); | ||
| 1136 | return -1; | ||
| 1137 | |||
| 1138 | case CEPH_MSGR_TAG_BADAUTHORIZER: | ||
| 1139 | con->auth_retry++; | ||
| 1140 | dout("process_connect %p got BADAUTHORIZER attempt %d\n", con, | ||
| 1141 | con->auth_retry); | ||
| 1142 | if (con->auth_retry == 2) { | ||
| 1143 | con->error_msg = "connect authorization failure"; | ||
| 1144 | reset_connection(con); | ||
| 1145 | set_bit(CLOSED, &con->state); | ||
| 1146 | return -1; | ||
| 1147 | } | ||
| 1148 | con->auth_retry = 1; | ||
| 1149 | prepare_write_connect(con->msgr, con, 0); | ||
| 1150 | prepare_read_connect(con); | ||
| 1151 | break; | ||
| 1152 | |||
| 1153 | case CEPH_MSGR_TAG_RESETSESSION: | ||
| 1154 | /* | ||
| 1155 | * If we connected with a large connect_seq but the peer | ||
| 1156 | * has no record of a session with us (no connection, or | ||
| 1157 | * connect_seq == 0), they will send RESETSESION to indicate | ||
| 1158 | * that they must have reset their session, and may have | ||
| 1159 | * dropped messages. | ||
| 1160 | */ | ||
| 1161 | dout("process_connect got RESET peer seq %u\n", | ||
| 1162 | le32_to_cpu(con->in_connect.connect_seq)); | ||
| 1163 | pr_err("%s%lld %s connection reset\n", | ||
| 1164 | ENTITY_NAME(con->peer_name), | ||
| 1165 | pr_addr(&con->peer_addr.in_addr)); | ||
| 1166 | reset_connection(con); | ||
| 1167 | prepare_write_connect(con->msgr, con, 0); | ||
| 1168 | prepare_read_connect(con); | ||
| 1169 | |||
| 1170 | /* Tell ceph about it. */ | ||
| 1171 | mutex_unlock(&con->mutex); | ||
| 1172 | pr_info("reset on %s%lld\n", ENTITY_NAME(con->peer_name)); | ||
| 1173 | if (con->ops->peer_reset) | ||
| 1174 | con->ops->peer_reset(con); | ||
| 1175 | mutex_lock(&con->mutex); | ||
| 1176 | break; | ||
| 1177 | |||
| 1178 | case CEPH_MSGR_TAG_RETRY_SESSION: | ||
| 1179 | /* | ||
| 1180 | * If we sent a smaller connect_seq than the peer has, try | ||
| 1181 | * again with a larger value. | ||
| 1182 | */ | ||
| 1183 | dout("process_connect got RETRY my seq = %u, peer_seq = %u\n", | ||
| 1184 | le32_to_cpu(con->out_connect.connect_seq), | ||
| 1185 | le32_to_cpu(con->in_connect.connect_seq)); | ||
| 1186 | con->connect_seq = le32_to_cpu(con->in_connect.connect_seq); | ||
| 1187 | prepare_write_connect(con->msgr, con, 0); | ||
| 1188 | prepare_read_connect(con); | ||
| 1189 | break; | ||
| 1190 | |||
| 1191 | case CEPH_MSGR_TAG_RETRY_GLOBAL: | ||
| 1192 | /* | ||
| 1193 | * If we sent a smaller global_seq than the peer has, try | ||
| 1194 | * again with a larger value. | ||
| 1195 | */ | ||
| 1196 | dout("process_connect got RETRY_GLOBAL my %u peer_gseq %u\n", | ||
| 1197 | con->peer_global_seq, | ||
| 1198 | le32_to_cpu(con->in_connect.global_seq)); | ||
| 1199 | get_global_seq(con->msgr, | ||
| 1200 | le32_to_cpu(con->in_connect.global_seq)); | ||
| 1201 | prepare_write_connect(con->msgr, con, 0); | ||
| 1202 | prepare_read_connect(con); | ||
| 1203 | break; | ||
| 1204 | |||
| 1205 | case CEPH_MSGR_TAG_READY: | ||
| 1206 | if (req_feat & ~server_feat) { | ||
| 1207 | pr_err("%s%lld %s protocol feature mismatch," | ||
| 1208 | " my required %llx > server's %llx, need %llx\n", | ||
| 1209 | ENTITY_NAME(con->peer_name), | ||
| 1210 | pr_addr(&con->peer_addr.in_addr), | ||
| 1211 | req_feat, server_feat, req_feat & ~server_feat); | ||
| 1212 | con->error_msg = "missing required protocol features"; | ||
| 1213 | fail_protocol(con); | ||
| 1214 | return -1; | ||
| 1215 | } | ||
| 1216 | clear_bit(CONNECTING, &con->state); | ||
| 1217 | con->peer_global_seq = le32_to_cpu(con->in_reply.global_seq); | ||
| 1218 | con->connect_seq++; | ||
| 1219 | dout("process_connect got READY gseq %d cseq %d (%d)\n", | ||
| 1220 | con->peer_global_seq, | ||
| 1221 | le32_to_cpu(con->in_reply.connect_seq), | ||
| 1222 | con->connect_seq); | ||
| 1223 | WARN_ON(con->connect_seq != | ||
| 1224 | le32_to_cpu(con->in_reply.connect_seq)); | ||
| 1225 | |||
| 1226 | if (con->in_reply.flags & CEPH_MSG_CONNECT_LOSSY) | ||
| 1227 | set_bit(LOSSYTX, &con->state); | ||
| 1228 | |||
| 1229 | prepare_read_tag(con); | ||
| 1230 | break; | ||
| 1231 | |||
| 1232 | case CEPH_MSGR_TAG_WAIT: | ||
| 1233 | /* | ||
| 1234 | * If there is a connection race (we are opening | ||
| 1235 | * connections to each other), one of us may just have | ||
| 1236 | * to WAIT. This shouldn't happen if we are the | ||
| 1237 | * client. | ||
| 1238 | */ | ||
| 1239 | pr_err("process_connect peer connecting WAIT\n"); | ||
| 1240 | |||
| 1241 | default: | ||
| 1242 | pr_err("connect protocol error, will retry\n"); | ||
| 1243 | con->error_msg = "protocol error, garbage tag during connect"; | ||
| 1244 | return -1; | ||
| 1245 | } | ||
| 1246 | return 0; | ||
| 1247 | } | ||
| 1248 | |||
| 1249 | |||
| 1250 | /* | ||
| 1251 | * read (part of) an ack | ||
| 1252 | */ | ||
| 1253 | static int read_partial_ack(struct ceph_connection *con) | ||
| 1254 | { | ||
| 1255 | int to = 0; | ||
| 1256 | |||
| 1257 | return read_partial(con, &to, sizeof(con->in_temp_ack), | ||
| 1258 | &con->in_temp_ack); | ||
| 1259 | } | ||
| 1260 | |||
| 1261 | |||
| 1262 | /* | ||
| 1263 | * We can finally discard anything that's been acked. | ||
| 1264 | */ | ||
| 1265 | static void process_ack(struct ceph_connection *con) | ||
| 1266 | { | ||
| 1267 | struct ceph_msg *m; | ||
| 1268 | u64 ack = le64_to_cpu(con->in_temp_ack); | ||
| 1269 | u64 seq; | ||
| 1270 | |||
| 1271 | while (!list_empty(&con->out_sent)) { | ||
| 1272 | m = list_first_entry(&con->out_sent, struct ceph_msg, | ||
| 1273 | list_head); | ||
| 1274 | seq = le64_to_cpu(m->hdr.seq); | ||
| 1275 | if (seq > ack) | ||
| 1276 | break; | ||
| 1277 | dout("got ack for seq %llu type %d at %p\n", seq, | ||
| 1278 | le16_to_cpu(m->hdr.type), m); | ||
| 1279 | ceph_msg_remove(m); | ||
| 1280 | } | ||
| 1281 | prepare_read_tag(con); | ||
| 1282 | } | ||
| 1283 | |||
| 1284 | |||
| 1285 | |||
| 1286 | |||
| 1287 | static int read_partial_message_section(struct ceph_connection *con, | ||
| 1288 | struct kvec *section, unsigned int sec_len, | ||
| 1289 | u32 *crc) | ||
| 1290 | { | ||
| 1291 | int left; | ||
| 1292 | int ret; | ||
| 1293 | |||
| 1294 | BUG_ON(!section); | ||
| 1295 | |||
| 1296 | while (section->iov_len < sec_len) { | ||
| 1297 | BUG_ON(section->iov_base == NULL); | ||
| 1298 | left = sec_len - section->iov_len; | ||
| 1299 | ret = ceph_tcp_recvmsg(con->sock, (char *)section->iov_base + | ||
| 1300 | section->iov_len, left); | ||
| 1301 | if (ret <= 0) | ||
| 1302 | return ret; | ||
| 1303 | section->iov_len += ret; | ||
| 1304 | if (section->iov_len == sec_len) | ||
| 1305 | *crc = crc32c(0, section->iov_base, | ||
| 1306 | section->iov_len); | ||
| 1307 | } | ||
| 1308 | |||
| 1309 | return 1; | ||
| 1310 | } | ||
| 1311 | |||
| 1312 | static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con, | ||
| 1313 | struct ceph_msg_header *hdr, | ||
| 1314 | int *skip); | ||
| 1315 | /* | ||
| 1316 | * read (part of) a message. | ||
| 1317 | */ | ||
| 1318 | static int read_partial_message(struct ceph_connection *con) | ||
| 1319 | { | ||
| 1320 | struct ceph_msg *m = con->in_msg; | ||
| 1321 | void *p; | ||
| 1322 | int ret; | ||
| 1323 | int to, left; | ||
| 1324 | unsigned front_len, middle_len, data_len, data_off; | ||
| 1325 | int datacrc = con->msgr->nocrc; | ||
| 1326 | int skip; | ||
| 1327 | |||
| 1328 | dout("read_partial_message con %p msg %p\n", con, m); | ||
| 1329 | |||
| 1330 | /* header */ | ||
| 1331 | while (con->in_base_pos < sizeof(con->in_hdr)) { | ||
| 1332 | left = sizeof(con->in_hdr) - con->in_base_pos; | ||
| 1333 | ret = ceph_tcp_recvmsg(con->sock, | ||
| 1334 | (char *)&con->in_hdr + con->in_base_pos, | ||
| 1335 | left); | ||
| 1336 | if (ret <= 0) | ||
| 1337 | return ret; | ||
| 1338 | con->in_base_pos += ret; | ||
| 1339 | if (con->in_base_pos == sizeof(con->in_hdr)) { | ||
| 1340 | u32 crc = crc32c(0, (void *)&con->in_hdr, | ||
| 1341 | sizeof(con->in_hdr) - sizeof(con->in_hdr.crc)); | ||
| 1342 | if (crc != le32_to_cpu(con->in_hdr.crc)) { | ||
| 1343 | pr_err("read_partial_message bad hdr " | ||
| 1344 | " crc %u != expected %u\n", | ||
| 1345 | crc, con->in_hdr.crc); | ||
| 1346 | return -EBADMSG; | ||
| 1347 | } | ||
| 1348 | } | ||
| 1349 | } | ||
| 1350 | front_len = le32_to_cpu(con->in_hdr.front_len); | ||
| 1351 | if (front_len > CEPH_MSG_MAX_FRONT_LEN) | ||
| 1352 | return -EIO; | ||
| 1353 | middle_len = le32_to_cpu(con->in_hdr.middle_len); | ||
| 1354 | if (middle_len > CEPH_MSG_MAX_DATA_LEN) | ||
| 1355 | return -EIO; | ||
| 1356 | data_len = le32_to_cpu(con->in_hdr.data_len); | ||
| 1357 | if (data_len > CEPH_MSG_MAX_DATA_LEN) | ||
| 1358 | return -EIO; | ||
| 1359 | data_off = le16_to_cpu(con->in_hdr.data_off); | ||
| 1360 | |||
| 1361 | /* allocate message? */ | ||
| 1362 | if (!con->in_msg) { | ||
| 1363 | dout("got hdr type %d front %d data %d\n", con->in_hdr.type, | ||
| 1364 | con->in_hdr.front_len, con->in_hdr.data_len); | ||
| 1365 | con->in_msg = ceph_alloc_msg(con, &con->in_hdr, &skip); | ||
| 1366 | if (skip) { | ||
| 1367 | /* skip this message */ | ||
| 1368 | dout("alloc_msg returned NULL, skipping message\n"); | ||
| 1369 | con->in_base_pos = -front_len - middle_len - data_len - | ||
| 1370 | sizeof(m->footer); | ||
| 1371 | con->in_tag = CEPH_MSGR_TAG_READY; | ||
| 1372 | return 0; | ||
| 1373 | } | ||
| 1374 | if (IS_ERR(con->in_msg)) { | ||
| 1375 | ret = PTR_ERR(con->in_msg); | ||
| 1376 | con->in_msg = NULL; | ||
| 1377 | con->error_msg = | ||
| 1378 | "error allocating memory for incoming message"; | ||
| 1379 | return ret; | ||
| 1380 | } | ||
| 1381 | m = con->in_msg; | ||
| 1382 | m->front.iov_len = 0; /* haven't read it yet */ | ||
| 1383 | if (m->middle) | ||
| 1384 | m->middle->vec.iov_len = 0; | ||
| 1385 | |||
| 1386 | con->in_msg_pos.page = 0; | ||
| 1387 | con->in_msg_pos.page_pos = data_off & ~PAGE_MASK; | ||
| 1388 | con->in_msg_pos.data_pos = 0; | ||
| 1389 | } | ||
| 1390 | |||
| 1391 | /* front */ | ||
| 1392 | ret = read_partial_message_section(con, &m->front, front_len, | ||
| 1393 | &con->in_front_crc); | ||
| 1394 | if (ret <= 0) | ||
| 1395 | return ret; | ||
| 1396 | |||
| 1397 | /* middle */ | ||
| 1398 | if (m->middle) { | ||
| 1399 | ret = read_partial_message_section(con, &m->middle->vec, middle_len, | ||
| 1400 | &con->in_middle_crc); | ||
| 1401 | if (ret <= 0) | ||
| 1402 | return ret; | ||
| 1403 | } | ||
| 1404 | |||
| 1405 | /* (page) data */ | ||
| 1406 | while (con->in_msg_pos.data_pos < data_len) { | ||
| 1407 | left = min((int)(data_len - con->in_msg_pos.data_pos), | ||
| 1408 | (int)(PAGE_SIZE - con->in_msg_pos.page_pos)); | ||
| 1409 | BUG_ON(m->pages == NULL); | ||
| 1410 | p = kmap(m->pages[con->in_msg_pos.page]); | ||
| 1411 | ret = ceph_tcp_recvmsg(con->sock, p + con->in_msg_pos.page_pos, | ||
| 1412 | left); | ||
| 1413 | if (ret > 0 && datacrc) | ||
| 1414 | con->in_data_crc = | ||
| 1415 | crc32c(con->in_data_crc, | ||
| 1416 | p + con->in_msg_pos.page_pos, ret); | ||
| 1417 | kunmap(m->pages[con->in_msg_pos.page]); | ||
| 1418 | if (ret <= 0) | ||
| 1419 | return ret; | ||
| 1420 | con->in_msg_pos.data_pos += ret; | ||
| 1421 | con->in_msg_pos.page_pos += ret; | ||
| 1422 | if (con->in_msg_pos.page_pos == PAGE_SIZE) { | ||
| 1423 | con->in_msg_pos.page_pos = 0; | ||
| 1424 | con->in_msg_pos.page++; | ||
| 1425 | } | ||
| 1426 | } | ||
| 1427 | |||
| 1428 | /* footer */ | ||
| 1429 | to = sizeof(m->hdr) + sizeof(m->footer); | ||
| 1430 | while (con->in_base_pos < to) { | ||
| 1431 | left = to - con->in_base_pos; | ||
| 1432 | ret = ceph_tcp_recvmsg(con->sock, (char *)&m->footer + | ||
| 1433 | (con->in_base_pos - sizeof(m->hdr)), | ||
| 1434 | left); | ||
| 1435 | if (ret <= 0) | ||
| 1436 | return ret; | ||
| 1437 | con->in_base_pos += ret; | ||
| 1438 | } | ||
| 1439 | dout("read_partial_message got msg %p %d (%u) + %d (%u) + %d (%u)\n", | ||
| 1440 | m, front_len, m->footer.front_crc, middle_len, | ||
| 1441 | m->footer.middle_crc, data_len, m->footer.data_crc); | ||
| 1442 | |||
| 1443 | /* crc ok? */ | ||
| 1444 | if (con->in_front_crc != le32_to_cpu(m->footer.front_crc)) { | ||
| 1445 | pr_err("read_partial_message %p front crc %u != exp. %u\n", | ||
| 1446 | m, con->in_front_crc, m->footer.front_crc); | ||
| 1447 | return -EBADMSG; | ||
| 1448 | } | ||
| 1449 | if (con->in_middle_crc != le32_to_cpu(m->footer.middle_crc)) { | ||
| 1450 | pr_err("read_partial_message %p middle crc %u != exp %u\n", | ||
| 1451 | m, con->in_middle_crc, m->footer.middle_crc); | ||
| 1452 | return -EBADMSG; | ||
| 1453 | } | ||
| 1454 | if (datacrc && | ||
| 1455 | (m->footer.flags & CEPH_MSG_FOOTER_NOCRC) == 0 && | ||
| 1456 | con->in_data_crc != le32_to_cpu(m->footer.data_crc)) { | ||
| 1457 | pr_err("read_partial_message %p data crc %u != exp. %u\n", m, | ||
| 1458 | con->in_data_crc, le32_to_cpu(m->footer.data_crc)); | ||
| 1459 | return -EBADMSG; | ||
| 1460 | } | ||
| 1461 | |||
| 1462 | return 1; /* done! */ | ||
| 1463 | } | ||
| 1464 | |||
| 1465 | /* | ||
| 1466 | * Process message. This happens in the worker thread. The callback should | ||
| 1467 | * be careful not to do anything that waits on other incoming messages or it | ||
| 1468 | * may deadlock. | ||
| 1469 | */ | ||
| 1470 | static void process_message(struct ceph_connection *con) | ||
| 1471 | { | ||
| 1472 | struct ceph_msg *msg; | ||
| 1473 | |||
| 1474 | msg = con->in_msg; | ||
| 1475 | con->in_msg = NULL; | ||
| 1476 | |||
| 1477 | /* if first message, set peer_name */ | ||
| 1478 | if (con->peer_name.type == 0) | ||
| 1479 | con->peer_name = msg->hdr.src.name; | ||
| 1480 | |||
| 1481 | con->in_seq++; | ||
| 1482 | mutex_unlock(&con->mutex); | ||
| 1483 | |||
| 1484 | dout("===== %p %llu from %s%lld %d=%s len %d+%d (%u %u %u) =====\n", | ||
| 1485 | msg, le64_to_cpu(msg->hdr.seq), | ||
| 1486 | ENTITY_NAME(msg->hdr.src.name), | ||
| 1487 | le16_to_cpu(msg->hdr.type), | ||
| 1488 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), | ||
| 1489 | le32_to_cpu(msg->hdr.front_len), | ||
| 1490 | le32_to_cpu(msg->hdr.data_len), | ||
| 1491 | con->in_front_crc, con->in_middle_crc, con->in_data_crc); | ||
| 1492 | con->ops->dispatch(con, msg); | ||
| 1493 | |||
| 1494 | mutex_lock(&con->mutex); | ||
| 1495 | prepare_read_tag(con); | ||
| 1496 | } | ||
| 1497 | |||
| 1498 | |||
| 1499 | /* | ||
| 1500 | * Write something to the socket. Called in a worker thread when the | ||
| 1501 | * socket appears to be writeable and we have something ready to send. | ||
| 1502 | */ | ||
| 1503 | static int try_write(struct ceph_connection *con) | ||
| 1504 | { | ||
| 1505 | struct ceph_messenger *msgr = con->msgr; | ||
| 1506 | int ret = 1; | ||
| 1507 | |||
| 1508 | dout("try_write start %p state %lu nref %d\n", con, con->state, | ||
| 1509 | atomic_read(&con->nref)); | ||
| 1510 | |||
| 1511 | mutex_lock(&con->mutex); | ||
| 1512 | more: | ||
| 1513 | dout("try_write out_kvec_bytes %d\n", con->out_kvec_bytes); | ||
| 1514 | |||
| 1515 | /* open the socket first? */ | ||
| 1516 | if (con->sock == NULL) { | ||
| 1517 | /* | ||
| 1518 | * if we were STANDBY and are reconnecting _this_ | ||
| 1519 | * connection, bump connect_seq now. Always bump | ||
| 1520 | * global_seq. | ||
| 1521 | */ | ||
| 1522 | if (test_and_clear_bit(STANDBY, &con->state)) | ||
| 1523 | con->connect_seq++; | ||
| 1524 | |||
| 1525 | prepare_write_banner(msgr, con); | ||
| 1526 | prepare_write_connect(msgr, con, 1); | ||
| 1527 | prepare_read_banner(con); | ||
| 1528 | set_bit(CONNECTING, &con->state); | ||
| 1529 | clear_bit(NEGOTIATING, &con->state); | ||
| 1530 | |||
| 1531 | BUG_ON(con->in_msg); | ||
| 1532 | con->in_tag = CEPH_MSGR_TAG_READY; | ||
| 1533 | dout("try_write initiating connect on %p new state %lu\n", | ||
| 1534 | con, con->state); | ||
| 1535 | con->sock = ceph_tcp_connect(con); | ||
| 1536 | if (IS_ERR(con->sock)) { | ||
| 1537 | con->sock = NULL; | ||
| 1538 | con->error_msg = "connect error"; | ||
| 1539 | ret = -1; | ||
| 1540 | goto out; | ||
| 1541 | } | ||
| 1542 | } | ||
| 1543 | |||
| 1544 | more_kvec: | ||
| 1545 | /* kvec data queued? */ | ||
| 1546 | if (con->out_skip) { | ||
| 1547 | ret = write_partial_skip(con); | ||
| 1548 | if (ret <= 0) | ||
| 1549 | goto done; | ||
| 1550 | if (ret < 0) { | ||
| 1551 | dout("try_write write_partial_skip err %d\n", ret); | ||
| 1552 | goto done; | ||
| 1553 | } | ||
| 1554 | } | ||
| 1555 | if (con->out_kvec_left) { | ||
| 1556 | ret = write_partial_kvec(con); | ||
| 1557 | if (ret <= 0) | ||
| 1558 | goto done; | ||
| 1559 | } | ||
| 1560 | |||
| 1561 | /* msg pages? */ | ||
| 1562 | if (con->out_msg) { | ||
| 1563 | if (con->out_msg_done) { | ||
| 1564 | ceph_msg_put(con->out_msg); | ||
| 1565 | con->out_msg = NULL; /* we're done with this one */ | ||
| 1566 | goto do_next; | ||
| 1567 | } | ||
| 1568 | |||
| 1569 | ret = write_partial_msg_pages(con); | ||
| 1570 | if (ret == 1) | ||
| 1571 | goto more_kvec; /* we need to send the footer, too! */ | ||
| 1572 | if (ret == 0) | ||
| 1573 | goto done; | ||
| 1574 | if (ret < 0) { | ||
| 1575 | dout("try_write write_partial_msg_pages err %d\n", | ||
| 1576 | ret); | ||
| 1577 | goto done; | ||
| 1578 | } | ||
| 1579 | } | ||
| 1580 | |||
| 1581 | do_next: | ||
| 1582 | if (!test_bit(CONNECTING, &con->state)) { | ||
| 1583 | /* is anything else pending? */ | ||
| 1584 | if (!list_empty(&con->out_queue)) { | ||
| 1585 | prepare_write_message(con); | ||
| 1586 | goto more; | ||
| 1587 | } | ||
| 1588 | if (con->in_seq > con->in_seq_acked) { | ||
| 1589 | prepare_write_ack(con); | ||
| 1590 | goto more; | ||
| 1591 | } | ||
| 1592 | if (test_and_clear_bit(KEEPALIVE_PENDING, &con->state)) { | ||
| 1593 | prepare_write_keepalive(con); | ||
| 1594 | goto more; | ||
| 1595 | } | ||
| 1596 | } | ||
| 1597 | |||
| 1598 | /* Nothing to do! */ | ||
| 1599 | clear_bit(WRITE_PENDING, &con->state); | ||
| 1600 | dout("try_write nothing else to write.\n"); | ||
| 1601 | done: | ||
| 1602 | ret = 0; | ||
| 1603 | out: | ||
| 1604 | mutex_unlock(&con->mutex); | ||
| 1605 | dout("try_write done on %p\n", con); | ||
| 1606 | return ret; | ||
| 1607 | } | ||
| 1608 | |||
| 1609 | |||
| 1610 | |||
| 1611 | /* | ||
| 1612 | * Read what we can from the socket. | ||
| 1613 | */ | ||
| 1614 | static int try_read(struct ceph_connection *con) | ||
| 1615 | { | ||
| 1616 | struct ceph_messenger *msgr; | ||
| 1617 | int ret = -1; | ||
| 1618 | |||
| 1619 | if (!con->sock) | ||
| 1620 | return 0; | ||
| 1621 | |||
| 1622 | if (test_bit(STANDBY, &con->state)) | ||
| 1623 | return 0; | ||
| 1624 | |||
| 1625 | dout("try_read start on %p\n", con); | ||
| 1626 | msgr = con->msgr; | ||
| 1627 | |||
| 1628 | mutex_lock(&con->mutex); | ||
| 1629 | |||
| 1630 | more: | ||
| 1631 | dout("try_read tag %d in_base_pos %d\n", (int)con->in_tag, | ||
| 1632 | con->in_base_pos); | ||
| 1633 | if (test_bit(CONNECTING, &con->state)) { | ||
| 1634 | if (!test_bit(NEGOTIATING, &con->state)) { | ||
| 1635 | dout("try_read connecting\n"); | ||
| 1636 | ret = read_partial_banner(con); | ||
| 1637 | if (ret <= 0) | ||
| 1638 | goto done; | ||
| 1639 | if (process_banner(con) < 0) { | ||
| 1640 | ret = -1; | ||
| 1641 | goto out; | ||
| 1642 | } | ||
| 1643 | } | ||
| 1644 | ret = read_partial_connect(con); | ||
| 1645 | if (ret <= 0) | ||
| 1646 | goto done; | ||
| 1647 | if (process_connect(con) < 0) { | ||
| 1648 | ret = -1; | ||
| 1649 | goto out; | ||
| 1650 | } | ||
| 1651 | goto more; | ||
| 1652 | } | ||
| 1653 | |||
| 1654 | if (con->in_base_pos < 0) { | ||
| 1655 | /* | ||
| 1656 | * skipping + discarding content. | ||
| 1657 | * | ||
| 1658 | * FIXME: there must be a better way to do this! | ||
| 1659 | */ | ||
| 1660 | static char buf[1024]; | ||
| 1661 | int skip = min(1024, -con->in_base_pos); | ||
| 1662 | dout("skipping %d / %d bytes\n", skip, -con->in_base_pos); | ||
| 1663 | ret = ceph_tcp_recvmsg(con->sock, buf, skip); | ||
| 1664 | if (ret <= 0) | ||
| 1665 | goto done; | ||
| 1666 | con->in_base_pos += ret; | ||
| 1667 | if (con->in_base_pos) | ||
| 1668 | goto more; | ||
| 1669 | } | ||
| 1670 | if (con->in_tag == CEPH_MSGR_TAG_READY) { | ||
| 1671 | /* | ||
| 1672 | * what's next? | ||
| 1673 | */ | ||
| 1674 | ret = ceph_tcp_recvmsg(con->sock, &con->in_tag, 1); | ||
| 1675 | if (ret <= 0) | ||
| 1676 | goto done; | ||
| 1677 | dout("try_read got tag %d\n", (int)con->in_tag); | ||
| 1678 | switch (con->in_tag) { | ||
| 1679 | case CEPH_MSGR_TAG_MSG: | ||
| 1680 | prepare_read_message(con); | ||
| 1681 | break; | ||
| 1682 | case CEPH_MSGR_TAG_ACK: | ||
| 1683 | prepare_read_ack(con); | ||
| 1684 | break; | ||
| 1685 | case CEPH_MSGR_TAG_CLOSE: | ||
| 1686 | set_bit(CLOSED, &con->state); /* fixme */ | ||
| 1687 | goto done; | ||
| 1688 | default: | ||
| 1689 | goto bad_tag; | ||
| 1690 | } | ||
| 1691 | } | ||
| 1692 | if (con->in_tag == CEPH_MSGR_TAG_MSG) { | ||
| 1693 | ret = read_partial_message(con); | ||
| 1694 | if (ret <= 0) { | ||
| 1695 | switch (ret) { | ||
| 1696 | case -EBADMSG: | ||
| 1697 | con->error_msg = "bad crc"; | ||
| 1698 | ret = -EIO; | ||
| 1699 | goto out; | ||
| 1700 | case -EIO: | ||
| 1701 | con->error_msg = "io error"; | ||
| 1702 | goto out; | ||
| 1703 | default: | ||
| 1704 | goto done; | ||
| 1705 | } | ||
| 1706 | } | ||
| 1707 | if (con->in_tag == CEPH_MSGR_TAG_READY) | ||
| 1708 | goto more; | ||
| 1709 | process_message(con); | ||
| 1710 | goto more; | ||
| 1711 | } | ||
| 1712 | if (con->in_tag == CEPH_MSGR_TAG_ACK) { | ||
| 1713 | ret = read_partial_ack(con); | ||
| 1714 | if (ret <= 0) | ||
| 1715 | goto done; | ||
| 1716 | process_ack(con); | ||
| 1717 | goto more; | ||
| 1718 | } | ||
| 1719 | |||
| 1720 | done: | ||
| 1721 | ret = 0; | ||
| 1722 | out: | ||
| 1723 | mutex_unlock(&con->mutex); | ||
| 1724 | dout("try_read done on %p\n", con); | ||
| 1725 | return ret; | ||
| 1726 | |||
| 1727 | bad_tag: | ||
| 1728 | pr_err("try_read bad con->in_tag = %d\n", (int)con->in_tag); | ||
| 1729 | con->error_msg = "protocol error, garbage tag"; | ||
| 1730 | ret = -1; | ||
| 1731 | goto out; | ||
| 1732 | } | ||
| 1733 | |||
| 1734 | |||
| 1735 | /* | ||
| 1736 | * Atomically queue work on a connection. Bump @con reference to | ||
| 1737 | * avoid races with connection teardown. | ||
| 1738 | * | ||
| 1739 | * There is some trickery going on with QUEUED and BUSY because we | ||
| 1740 | * only want a _single_ thread operating on each connection at any | ||
| 1741 | * point in time, but we want to use all available CPUs. | ||
| 1742 | * | ||
| 1743 | * The worker thread only proceeds if it can atomically set BUSY. It | ||
| 1744 | * clears QUEUED and does it's thing. When it thinks it's done, it | ||
| 1745 | * clears BUSY, then rechecks QUEUED.. if it's set again, it loops | ||
| 1746 | * (tries again to set BUSY). | ||
| 1747 | * | ||
| 1748 | * To queue work, we first set QUEUED, _then_ if BUSY isn't set, we | ||
| 1749 | * try to queue work. If that fails (work is already queued, or BUSY) | ||
| 1750 | * we give up (work also already being done or is queued) but leave QUEUED | ||
| 1751 | * set so that the worker thread will loop if necessary. | ||
| 1752 | */ | ||
| 1753 | static void queue_con(struct ceph_connection *con) | ||
| 1754 | { | ||
| 1755 | if (test_bit(DEAD, &con->state)) { | ||
| 1756 | dout("queue_con %p ignoring: DEAD\n", | ||
| 1757 | con); | ||
| 1758 | return; | ||
| 1759 | } | ||
| 1760 | |||
| 1761 | if (!con->ops->get(con)) { | ||
| 1762 | dout("queue_con %p ref count 0\n", con); | ||
| 1763 | return; | ||
| 1764 | } | ||
| 1765 | |||
| 1766 | set_bit(QUEUED, &con->state); | ||
| 1767 | if (test_bit(BUSY, &con->state)) { | ||
| 1768 | dout("queue_con %p - already BUSY\n", con); | ||
| 1769 | con->ops->put(con); | ||
| 1770 | } else if (!queue_work(ceph_msgr_wq, &con->work.work)) { | ||
| 1771 | dout("queue_con %p - already queued\n", con); | ||
| 1772 | con->ops->put(con); | ||
| 1773 | } else { | ||
| 1774 | dout("queue_con %p\n", con); | ||
| 1775 | } | ||
| 1776 | } | ||
| 1777 | |||
| 1778 | /* | ||
| 1779 | * Do some work on a connection. Drop a connection ref when we're done. | ||
| 1780 | */ | ||
| 1781 | static void con_work(struct work_struct *work) | ||
| 1782 | { | ||
| 1783 | struct ceph_connection *con = container_of(work, struct ceph_connection, | ||
| 1784 | work.work); | ||
| 1785 | int backoff = 0; | ||
| 1786 | |||
| 1787 | more: | ||
| 1788 | if (test_and_set_bit(BUSY, &con->state) != 0) { | ||
| 1789 | dout("con_work %p BUSY already set\n", con); | ||
| 1790 | goto out; | ||
| 1791 | } | ||
| 1792 | dout("con_work %p start, clearing QUEUED\n", con); | ||
| 1793 | clear_bit(QUEUED, &con->state); | ||
| 1794 | |||
| 1795 | if (test_bit(CLOSED, &con->state)) { /* e.g. if we are replaced */ | ||
| 1796 | dout("con_work CLOSED\n"); | ||
| 1797 | con_close_socket(con); | ||
| 1798 | goto done; | ||
| 1799 | } | ||
| 1800 | if (test_and_clear_bit(OPENING, &con->state)) { | ||
| 1801 | /* reopen w/ new peer */ | ||
| 1802 | dout("con_work OPENING\n"); | ||
| 1803 | con_close_socket(con); | ||
| 1804 | } | ||
| 1805 | |||
| 1806 | if (test_and_clear_bit(SOCK_CLOSED, &con->state) || | ||
| 1807 | try_read(con) < 0 || | ||
| 1808 | try_write(con) < 0) { | ||
| 1809 | backoff = 1; | ||
| 1810 | ceph_fault(con); /* error/fault path */ | ||
| 1811 | } | ||
| 1812 | |||
| 1813 | done: | ||
| 1814 | clear_bit(BUSY, &con->state); | ||
| 1815 | dout("con->state=%lu\n", con->state); | ||
| 1816 | if (test_bit(QUEUED, &con->state)) { | ||
| 1817 | if (!backoff || test_bit(OPENING, &con->state)) { | ||
| 1818 | dout("con_work %p QUEUED reset, looping\n", con); | ||
| 1819 | goto more; | ||
| 1820 | } | ||
| 1821 | dout("con_work %p QUEUED reset, but just faulted\n", con); | ||
| 1822 | clear_bit(QUEUED, &con->state); | ||
| 1823 | } | ||
| 1824 | dout("con_work %p done\n", con); | ||
| 1825 | |||
| 1826 | out: | ||
| 1827 | con->ops->put(con); | ||
| 1828 | } | ||
| 1829 | |||
| 1830 | |||
| 1831 | /* | ||
| 1832 | * Generic error/fault handler. A retry mechanism is used with | ||
| 1833 | * exponential backoff | ||
| 1834 | */ | ||
| 1835 | static void ceph_fault(struct ceph_connection *con) | ||
| 1836 | { | ||
| 1837 | pr_err("%s%lld %s %s\n", ENTITY_NAME(con->peer_name), | ||
| 1838 | pr_addr(&con->peer_addr.in_addr), con->error_msg); | ||
| 1839 | dout("fault %p state %lu to peer %s\n", | ||
| 1840 | con, con->state, pr_addr(&con->peer_addr.in_addr)); | ||
| 1841 | |||
| 1842 | if (test_bit(LOSSYTX, &con->state)) { | ||
| 1843 | dout("fault on LOSSYTX channel\n"); | ||
| 1844 | goto out; | ||
| 1845 | } | ||
| 1846 | |||
| 1847 | mutex_lock(&con->mutex); | ||
| 1848 | if (test_bit(CLOSED, &con->state)) | ||
| 1849 | goto out_unlock; | ||
| 1850 | |||
| 1851 | con_close_socket(con); | ||
| 1852 | |||
| 1853 | if (con->in_msg) { | ||
| 1854 | ceph_msg_put(con->in_msg); | ||
| 1855 | con->in_msg = NULL; | ||
| 1856 | } | ||
| 1857 | |||
| 1858 | /* Requeue anything that hasn't been acked */ | ||
| 1859 | list_splice_init(&con->out_sent, &con->out_queue); | ||
| 1860 | |||
| 1861 | /* If there are no messages in the queue, place the connection | ||
| 1862 | * in a STANDBY state (i.e., don't try to reconnect just yet). */ | ||
| 1863 | if (list_empty(&con->out_queue) && !con->out_keepalive_pending) { | ||
| 1864 | dout("fault setting STANDBY\n"); | ||
| 1865 | set_bit(STANDBY, &con->state); | ||
| 1866 | } else { | ||
| 1867 | /* retry after a delay. */ | ||
| 1868 | if (con->delay == 0) | ||
| 1869 | con->delay = BASE_DELAY_INTERVAL; | ||
| 1870 | else if (con->delay < MAX_DELAY_INTERVAL) | ||
| 1871 | con->delay *= 2; | ||
| 1872 | dout("fault queueing %p delay %lu\n", con, con->delay); | ||
| 1873 | con->ops->get(con); | ||
| 1874 | if (queue_delayed_work(ceph_msgr_wq, &con->work, | ||
| 1875 | round_jiffies_relative(con->delay)) == 0) | ||
| 1876 | con->ops->put(con); | ||
| 1877 | } | ||
| 1878 | |||
| 1879 | out_unlock: | ||
| 1880 | mutex_unlock(&con->mutex); | ||
| 1881 | out: | ||
| 1882 | /* | ||
| 1883 | * in case we faulted due to authentication, invalidate our | ||
| 1884 | * current tickets so that we can get new ones. | ||
| 1885 | */ | ||
| 1886 | if (con->auth_retry && con->ops->invalidate_authorizer) { | ||
| 1887 | dout("calling invalidate_authorizer()\n"); | ||
| 1888 | con->ops->invalidate_authorizer(con); | ||
| 1889 | } | ||
| 1890 | |||
| 1891 | if (con->ops->fault) | ||
| 1892 | con->ops->fault(con); | ||
| 1893 | } | ||
| 1894 | |||
| 1895 | |||
| 1896 | |||
| 1897 | /* | ||
| 1898 | * create a new messenger instance | ||
| 1899 | */ | ||
| 1900 | struct ceph_messenger *ceph_messenger_create(struct ceph_entity_addr *myaddr) | ||
| 1901 | { | ||
| 1902 | struct ceph_messenger *msgr; | ||
| 1903 | |||
| 1904 | msgr = kzalloc(sizeof(*msgr), GFP_KERNEL); | ||
| 1905 | if (msgr == NULL) | ||
| 1906 | return ERR_PTR(-ENOMEM); | ||
| 1907 | |||
| 1908 | spin_lock_init(&msgr->global_seq_lock); | ||
| 1909 | |||
| 1910 | /* the zero page is needed if a request is "canceled" while the message | ||
| 1911 | * is being written over the socket */ | ||
| 1912 | msgr->zero_page = alloc_page(GFP_KERNEL | __GFP_ZERO); | ||
| 1913 | if (!msgr->zero_page) { | ||
| 1914 | kfree(msgr); | ||
| 1915 | return ERR_PTR(-ENOMEM); | ||
| 1916 | } | ||
| 1917 | kmap(msgr->zero_page); | ||
| 1918 | |||
| 1919 | if (myaddr) | ||
| 1920 | msgr->inst.addr = *myaddr; | ||
| 1921 | |||
| 1922 | /* select a random nonce */ | ||
| 1923 | msgr->inst.addr.type = 0; | ||
| 1924 | get_random_bytes(&msgr->inst.addr.nonce, sizeof(msgr->inst.addr.nonce)); | ||
| 1925 | encode_my_addr(msgr); | ||
| 1926 | |||
| 1927 | dout("messenger_create %p\n", msgr); | ||
| 1928 | return msgr; | ||
| 1929 | } | ||
| 1930 | |||
| 1931 | void ceph_messenger_destroy(struct ceph_messenger *msgr) | ||
| 1932 | { | ||
| 1933 | dout("destroy %p\n", msgr); | ||
| 1934 | kunmap(msgr->zero_page); | ||
| 1935 | __free_page(msgr->zero_page); | ||
| 1936 | kfree(msgr); | ||
| 1937 | dout("destroyed messenger %p\n", msgr); | ||
| 1938 | } | ||
| 1939 | |||
| 1940 | /* | ||
| 1941 | * Queue up an outgoing message on the given connection. | ||
| 1942 | */ | ||
| 1943 | void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg) | ||
| 1944 | { | ||
| 1945 | if (test_bit(CLOSED, &con->state)) { | ||
| 1946 | dout("con_send %p closed, dropping %p\n", con, msg); | ||
| 1947 | ceph_msg_put(msg); | ||
| 1948 | return; | ||
| 1949 | } | ||
| 1950 | |||
| 1951 | /* set src+dst */ | ||
| 1952 | msg->hdr.src.name = con->msgr->inst.name; | ||
| 1953 | msg->hdr.src.addr = con->msgr->my_enc_addr; | ||
| 1954 | msg->hdr.orig_src = msg->hdr.src; | ||
| 1955 | |||
| 1956 | BUG_ON(msg->front.iov_len != le32_to_cpu(msg->hdr.front_len)); | ||
| 1957 | |||
| 1958 | /* queue */ | ||
| 1959 | mutex_lock(&con->mutex); | ||
| 1960 | BUG_ON(!list_empty(&msg->list_head)); | ||
| 1961 | list_add_tail(&msg->list_head, &con->out_queue); | ||
| 1962 | dout("----- %p to %s%lld %d=%s len %d+%d+%d -----\n", msg, | ||
| 1963 | ENTITY_NAME(con->peer_name), le16_to_cpu(msg->hdr.type), | ||
| 1964 | ceph_msg_type_name(le16_to_cpu(msg->hdr.type)), | ||
| 1965 | le32_to_cpu(msg->hdr.front_len), | ||
| 1966 | le32_to_cpu(msg->hdr.middle_len), | ||
| 1967 | le32_to_cpu(msg->hdr.data_len)); | ||
| 1968 | mutex_unlock(&con->mutex); | ||
| 1969 | |||
| 1970 | /* if there wasn't anything waiting to send before, queue | ||
| 1971 | * new work */ | ||
| 1972 | if (test_and_set_bit(WRITE_PENDING, &con->state) == 0) | ||
| 1973 | queue_con(con); | ||
| 1974 | } | ||
| 1975 | |||
| 1976 | /* | ||
| 1977 | * Revoke a message that was previously queued for send | ||
| 1978 | */ | ||
| 1979 | void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg) | ||
| 1980 | { | ||
| 1981 | mutex_lock(&con->mutex); | ||
| 1982 | if (!list_empty(&msg->list_head)) { | ||
| 1983 | dout("con_revoke %p msg %p\n", con, msg); | ||
| 1984 | list_del_init(&msg->list_head); | ||
| 1985 | ceph_msg_put(msg); | ||
| 1986 | msg->hdr.seq = 0; | ||
| 1987 | if (con->out_msg == msg) { | ||
| 1988 | ceph_msg_put(con->out_msg); | ||
| 1989 | con->out_msg = NULL; | ||
| 1990 | } | ||
| 1991 | if (con->out_kvec_is_msg) { | ||
| 1992 | con->out_skip = con->out_kvec_bytes; | ||
| 1993 | con->out_kvec_is_msg = false; | ||
| 1994 | } | ||
| 1995 | } else { | ||
| 1996 | dout("con_revoke %p msg %p - not queued (sent?)\n", con, msg); | ||
| 1997 | } | ||
| 1998 | mutex_unlock(&con->mutex); | ||
| 1999 | } | ||
| 2000 | |||
| 2001 | /* | ||
| 2002 | * Revoke a message that we may be reading data into | ||
| 2003 | */ | ||
| 2004 | void ceph_con_revoke_message(struct ceph_connection *con, struct ceph_msg *msg) | ||
| 2005 | { | ||
| 2006 | mutex_lock(&con->mutex); | ||
| 2007 | if (con->in_msg && con->in_msg == msg) { | ||
| 2008 | unsigned front_len = le32_to_cpu(con->in_hdr.front_len); | ||
| 2009 | unsigned middle_len = le32_to_cpu(con->in_hdr.middle_len); | ||
| 2010 | unsigned data_len = le32_to_cpu(con->in_hdr.data_len); | ||
| 2011 | |||
| 2012 | /* skip rest of message */ | ||
| 2013 | dout("con_revoke_pages %p msg %p revoked\n", con, msg); | ||
| 2014 | con->in_base_pos = con->in_base_pos - | ||
| 2015 | sizeof(struct ceph_msg_header) - | ||
| 2016 | front_len - | ||
| 2017 | middle_len - | ||
| 2018 | data_len - | ||
| 2019 | sizeof(struct ceph_msg_footer); | ||
| 2020 | ceph_msg_put(con->in_msg); | ||
| 2021 | con->in_msg = NULL; | ||
| 2022 | con->in_tag = CEPH_MSGR_TAG_READY; | ||
| 2023 | } else { | ||
| 2024 | dout("con_revoke_pages %p msg %p pages %p no-op\n", | ||
| 2025 | con, con->in_msg, msg); | ||
| 2026 | } | ||
| 2027 | mutex_unlock(&con->mutex); | ||
| 2028 | } | ||
| 2029 | |||
| 2030 | /* | ||
| 2031 | * Queue a keepalive byte to ensure the tcp connection is alive. | ||
| 2032 | */ | ||
| 2033 | void ceph_con_keepalive(struct ceph_connection *con) | ||
| 2034 | { | ||
| 2035 | if (test_and_set_bit(KEEPALIVE_PENDING, &con->state) == 0 && | ||
| 2036 | test_and_set_bit(WRITE_PENDING, &con->state) == 0) | ||
| 2037 | queue_con(con); | ||
| 2038 | } | ||
| 2039 | |||
| 2040 | |||
| 2041 | /* | ||
| 2042 | * construct a new message with given type, size | ||
| 2043 | * the new msg has a ref count of 1. | ||
| 2044 | */ | ||
| 2045 | struct ceph_msg *ceph_msg_new(int type, int front_len, | ||
| 2046 | int page_len, int page_off, struct page **pages) | ||
| 2047 | { | ||
| 2048 | struct ceph_msg *m; | ||
| 2049 | |||
| 2050 | m = kmalloc(sizeof(*m), GFP_NOFS); | ||
| 2051 | if (m == NULL) | ||
| 2052 | goto out; | ||
| 2053 | kref_init(&m->kref); | ||
| 2054 | INIT_LIST_HEAD(&m->list_head); | ||
| 2055 | |||
| 2056 | m->hdr.type = cpu_to_le16(type); | ||
| 2057 | m->hdr.front_len = cpu_to_le32(front_len); | ||
| 2058 | m->hdr.middle_len = 0; | ||
| 2059 | m->hdr.data_len = cpu_to_le32(page_len); | ||
| 2060 | m->hdr.data_off = cpu_to_le16(page_off); | ||
| 2061 | m->hdr.priority = cpu_to_le16(CEPH_MSG_PRIO_DEFAULT); | ||
| 2062 | m->footer.front_crc = 0; | ||
| 2063 | m->footer.middle_crc = 0; | ||
| 2064 | m->footer.data_crc = 0; | ||
| 2065 | m->front_max = front_len; | ||
| 2066 | m->front_is_vmalloc = false; | ||
| 2067 | m->more_to_follow = false; | ||
| 2068 | m->pool = NULL; | ||
| 2069 | |||
| 2070 | /* front */ | ||
| 2071 | if (front_len) { | ||
| 2072 | if (front_len > PAGE_CACHE_SIZE) { | ||
| 2073 | m->front.iov_base = __vmalloc(front_len, GFP_NOFS, | ||
| 2074 | PAGE_KERNEL); | ||
| 2075 | m->front_is_vmalloc = true; | ||
| 2076 | } else { | ||
| 2077 | m->front.iov_base = kmalloc(front_len, GFP_NOFS); | ||
| 2078 | } | ||
| 2079 | if (m->front.iov_base == NULL) { | ||
| 2080 | pr_err("msg_new can't allocate %d bytes\n", | ||
| 2081 | front_len); | ||
| 2082 | goto out2; | ||
| 2083 | } | ||
| 2084 | } else { | ||
| 2085 | m->front.iov_base = NULL; | ||
| 2086 | } | ||
| 2087 | m->front.iov_len = front_len; | ||
| 2088 | |||
| 2089 | /* middle */ | ||
| 2090 | m->middle = NULL; | ||
| 2091 | |||
| 2092 | /* data */ | ||
| 2093 | m->nr_pages = calc_pages_for(page_off, page_len); | ||
| 2094 | m->pages = pages; | ||
| 2095 | m->pagelist = NULL; | ||
| 2096 | |||
| 2097 | dout("ceph_msg_new %p page %d~%d -> %d\n", m, page_off, page_len, | ||
| 2098 | m->nr_pages); | ||
| 2099 | return m; | ||
| 2100 | |||
| 2101 | out2: | ||
| 2102 | ceph_msg_put(m); | ||
| 2103 | out: | ||
| 2104 | pr_err("msg_new can't create type %d len %d\n", type, front_len); | ||
| 2105 | return ERR_PTR(-ENOMEM); | ||
| 2106 | } | ||
| 2107 | |||
| 2108 | /* | ||
| 2109 | * Allocate "middle" portion of a message, if it is needed and wasn't | ||
| 2110 | * allocated by alloc_msg. This allows us to read a small fixed-size | ||
| 2111 | * per-type header in the front and then gracefully fail (i.e., | ||
| 2112 | * propagate the error to the caller based on info in the front) when | ||
| 2113 | * the middle is too large. | ||
| 2114 | */ | ||
| 2115 | static int ceph_alloc_middle(struct ceph_connection *con, struct ceph_msg *msg) | ||
| 2116 | { | ||
| 2117 | int type = le16_to_cpu(msg->hdr.type); | ||
| 2118 | int middle_len = le32_to_cpu(msg->hdr.middle_len); | ||
| 2119 | |||
| 2120 | dout("alloc_middle %p type %d %s middle_len %d\n", msg, type, | ||
| 2121 | ceph_msg_type_name(type), middle_len); | ||
| 2122 | BUG_ON(!middle_len); | ||
| 2123 | BUG_ON(msg->middle); | ||
| 2124 | |||
| 2125 | msg->middle = ceph_buffer_new(middle_len, GFP_NOFS); | ||
| 2126 | if (!msg->middle) | ||
| 2127 | return -ENOMEM; | ||
| 2128 | return 0; | ||
| 2129 | } | ||
| 2130 | |||
| 2131 | /* | ||
| 2132 | * Generic message allocator, for incoming messages. | ||
| 2133 | */ | ||
| 2134 | static struct ceph_msg *ceph_alloc_msg(struct ceph_connection *con, | ||
| 2135 | struct ceph_msg_header *hdr, | ||
| 2136 | int *skip) | ||
| 2137 | { | ||
| 2138 | int type = le16_to_cpu(hdr->type); | ||
| 2139 | int front_len = le32_to_cpu(hdr->front_len); | ||
| 2140 | int middle_len = le32_to_cpu(hdr->middle_len); | ||
| 2141 | struct ceph_msg *msg = NULL; | ||
| 2142 | int ret; | ||
| 2143 | |||
| 2144 | if (con->ops->alloc_msg) { | ||
| 2145 | mutex_unlock(&con->mutex); | ||
| 2146 | msg = con->ops->alloc_msg(con, hdr, skip); | ||
| 2147 | mutex_lock(&con->mutex); | ||
| 2148 | if (IS_ERR(msg)) | ||
| 2149 | return msg; | ||
| 2150 | |||
| 2151 | if (*skip) | ||
| 2152 | return NULL; | ||
| 2153 | } | ||
| 2154 | if (!msg) { | ||
| 2155 | *skip = 0; | ||
| 2156 | msg = ceph_msg_new(type, front_len, 0, 0, NULL); | ||
| 2157 | if (!msg) { | ||
| 2158 | pr_err("unable to allocate msg type %d len %d\n", | ||
| 2159 | type, front_len); | ||
| 2160 | return ERR_PTR(-ENOMEM); | ||
| 2161 | } | ||
| 2162 | } | ||
| 2163 | memcpy(&msg->hdr, &con->in_hdr, sizeof(con->in_hdr)); | ||
| 2164 | |||
| 2165 | if (middle_len) { | ||
| 2166 | ret = ceph_alloc_middle(con, msg); | ||
| 2167 | |||
| 2168 | if (ret < 0) { | ||
| 2169 | ceph_msg_put(msg); | ||
| 2170 | return msg; | ||
| 2171 | } | ||
| 2172 | } | ||
| 2173 | |||
| 2174 | return msg; | ||
| 2175 | } | ||
| 2176 | |||
| 2177 | |||
| 2178 | /* | ||
| 2179 | * Free a generically kmalloc'd message. | ||
| 2180 | */ | ||
| 2181 | void ceph_msg_kfree(struct ceph_msg *m) | ||
| 2182 | { | ||
| 2183 | dout("msg_kfree %p\n", m); | ||
| 2184 | if (m->front_is_vmalloc) | ||
| 2185 | vfree(m->front.iov_base); | ||
| 2186 | else | ||
| 2187 | kfree(m->front.iov_base); | ||
| 2188 | kfree(m); | ||
| 2189 | } | ||
| 2190 | |||
| 2191 | /* | ||
| 2192 | * Drop a msg ref. Destroy as needed. | ||
| 2193 | */ | ||
| 2194 | void ceph_msg_last_put(struct kref *kref) | ||
| 2195 | { | ||
| 2196 | struct ceph_msg *m = container_of(kref, struct ceph_msg, kref); | ||
| 2197 | |||
| 2198 | dout("ceph_msg_put last one on %p\n", m); | ||
| 2199 | WARN_ON(!list_empty(&m->list_head)); | ||
| 2200 | |||
| 2201 | /* drop middle, data, if any */ | ||
| 2202 | if (m->middle) { | ||
| 2203 | ceph_buffer_put(m->middle); | ||
| 2204 | m->middle = NULL; | ||
| 2205 | } | ||
| 2206 | m->nr_pages = 0; | ||
| 2207 | m->pages = NULL; | ||
| 2208 | |||
| 2209 | if (m->pagelist) { | ||
| 2210 | ceph_pagelist_release(m->pagelist); | ||
| 2211 | kfree(m->pagelist); | ||
| 2212 | m->pagelist = NULL; | ||
| 2213 | } | ||
| 2214 | |||
| 2215 | if (m->pool) | ||
| 2216 | ceph_msgpool_put(m->pool, m); | ||
| 2217 | else | ||
| 2218 | ceph_msg_kfree(m); | ||
| 2219 | } | ||
| 2220 | |||
| 2221 | void ceph_msg_dump(struct ceph_msg *msg) | ||
| 2222 | { | ||
| 2223 | pr_debug("msg_dump %p (front_max %d nr_pages %d)\n", msg, | ||
| 2224 | msg->front_max, msg->nr_pages); | ||
| 2225 | print_hex_dump(KERN_DEBUG, "header: ", | ||
| 2226 | DUMP_PREFIX_OFFSET, 16, 1, | ||
| 2227 | &msg->hdr, sizeof(msg->hdr), true); | ||
| 2228 | print_hex_dump(KERN_DEBUG, " front: ", | ||
| 2229 | DUMP_PREFIX_OFFSET, 16, 1, | ||
| 2230 | msg->front.iov_base, msg->front.iov_len, true); | ||
| 2231 | if (msg->middle) | ||
| 2232 | print_hex_dump(KERN_DEBUG, "middle: ", | ||
| 2233 | DUMP_PREFIX_OFFSET, 16, 1, | ||
| 2234 | msg->middle->vec.iov_base, | ||
| 2235 | msg->middle->vec.iov_len, true); | ||
| 2236 | print_hex_dump(KERN_DEBUG, "footer: ", | ||
| 2237 | DUMP_PREFIX_OFFSET, 16, 1, | ||
| 2238 | &msg->footer, sizeof(msg->footer), true); | ||
| 2239 | } | ||
diff --git a/fs/ceph/messenger.h b/fs/ceph/messenger.h new file mode 100644 index 000000000000..a343dae73cdc --- /dev/null +++ b/fs/ceph/messenger.h | |||
| @@ -0,0 +1,255 @@ | |||
| 1 | #ifndef __FS_CEPH_MESSENGER_H | ||
| 2 | #define __FS_CEPH_MESSENGER_H | ||
| 3 | |||
| 4 | #include <linux/kref.h> | ||
| 5 | #include <linux/mutex.h> | ||
| 6 | #include <linux/net.h> | ||
| 7 | #include <linux/radix-tree.h> | ||
| 8 | #include <linux/uio.h> | ||
| 9 | #include <linux/version.h> | ||
| 10 | #include <linux/workqueue.h> | ||
| 11 | |||
| 12 | #include "types.h" | ||
| 13 | #include "buffer.h" | ||
| 14 | |||
| 15 | struct ceph_msg; | ||
| 16 | struct ceph_connection; | ||
| 17 | |||
| 18 | extern struct workqueue_struct *ceph_msgr_wq; /* receive work queue */ | ||
| 19 | |||
| 20 | /* | ||
| 21 | * Ceph defines these callbacks for handling connection events. | ||
| 22 | */ | ||
| 23 | struct ceph_connection_operations { | ||
| 24 | struct ceph_connection *(*get)(struct ceph_connection *); | ||
| 25 | void (*put)(struct ceph_connection *); | ||
| 26 | |||
| 27 | /* handle an incoming message. */ | ||
| 28 | void (*dispatch) (struct ceph_connection *con, struct ceph_msg *m); | ||
| 29 | |||
| 30 | /* authorize an outgoing connection */ | ||
| 31 | int (*get_authorizer) (struct ceph_connection *con, | ||
| 32 | void **buf, int *len, int *proto, | ||
| 33 | void **reply_buf, int *reply_len, int force_new); | ||
| 34 | int (*verify_authorizer_reply) (struct ceph_connection *con, int len); | ||
| 35 | int (*invalidate_authorizer)(struct ceph_connection *con); | ||
| 36 | |||
| 37 | /* protocol version mismatch */ | ||
| 38 | void (*bad_proto) (struct ceph_connection *con); | ||
| 39 | |||
| 40 | /* there was some error on the socket (disconnect, whatever) */ | ||
| 41 | void (*fault) (struct ceph_connection *con); | ||
| 42 | |||
| 43 | /* a remote host as terminated a message exchange session, and messages | ||
| 44 | * we sent (or they tried to send us) may be lost. */ | ||
| 45 | void (*peer_reset) (struct ceph_connection *con); | ||
| 46 | |||
| 47 | struct ceph_msg * (*alloc_msg) (struct ceph_connection *con, | ||
| 48 | struct ceph_msg_header *hdr, | ||
| 49 | int *skip); | ||
| 50 | }; | ||
| 51 | |||
| 52 | extern const char *ceph_name_type_str(int t); | ||
| 53 | |||
| 54 | /* use format string %s%d */ | ||
| 55 | #define ENTITY_NAME(n) ceph_name_type_str((n).type), le64_to_cpu((n).num) | ||
| 56 | |||
| 57 | struct ceph_messenger { | ||
| 58 | struct ceph_entity_inst inst; /* my name+address */ | ||
| 59 | struct ceph_entity_addr my_enc_addr; | ||
| 60 | struct page *zero_page; /* used in certain error cases */ | ||
| 61 | |||
| 62 | bool nocrc; | ||
| 63 | |||
| 64 | /* | ||
| 65 | * the global_seq counts connections i (attempt to) initiate | ||
| 66 | * in order to disambiguate certain connect race conditions. | ||
| 67 | */ | ||
| 68 | u32 global_seq; | ||
| 69 | spinlock_t global_seq_lock; | ||
| 70 | }; | ||
| 71 | |||
| 72 | /* | ||
| 73 | * a single message. it contains a header (src, dest, message type, etc.), | ||
| 74 | * footer (crc values, mainly), a "front" message body, and possibly a | ||
| 75 | * data payload (stored in some number of pages). | ||
| 76 | */ | ||
| 77 | struct ceph_msg { | ||
| 78 | struct ceph_msg_header hdr; /* header */ | ||
| 79 | struct ceph_msg_footer footer; /* footer */ | ||
| 80 | struct kvec front; /* unaligned blobs of message */ | ||
| 81 | struct ceph_buffer *middle; | ||
| 82 | struct page **pages; /* data payload. NOT OWNER. */ | ||
| 83 | unsigned nr_pages; /* size of page array */ | ||
| 84 | struct ceph_pagelist *pagelist; /* instead of pages */ | ||
| 85 | struct list_head list_head; | ||
| 86 | struct kref kref; | ||
| 87 | bool front_is_vmalloc; | ||
| 88 | bool more_to_follow; | ||
| 89 | int front_max; | ||
| 90 | |||
| 91 | struct ceph_msgpool *pool; | ||
| 92 | }; | ||
| 93 | |||
| 94 | struct ceph_msg_pos { | ||
| 95 | int page, page_pos; /* which page; offset in page */ | ||
| 96 | int data_pos; /* offset in data payload */ | ||
| 97 | int did_page_crc; /* true if we've calculated crc for current page */ | ||
| 98 | }; | ||
| 99 | |||
| 100 | /* ceph connection fault delay defaults, for exponential backoff */ | ||
| 101 | #define BASE_DELAY_INTERVAL (HZ/2) | ||
| 102 | #define MAX_DELAY_INTERVAL (5 * 60 * HZ) | ||
| 103 | |||
| 104 | /* | ||
| 105 | * ceph_connection state bit flags | ||
| 106 | * | ||
| 107 | * QUEUED and BUSY are used together to ensure that only a single | ||
| 108 | * thread is currently opening, reading or writing data to the socket. | ||
| 109 | */ | ||
| 110 | #define LOSSYTX 0 /* we can close channel or drop messages on errors */ | ||
| 111 | #define CONNECTING 1 | ||
| 112 | #define NEGOTIATING 2 | ||
| 113 | #define KEEPALIVE_PENDING 3 | ||
| 114 | #define WRITE_PENDING 4 /* we have data ready to send */ | ||
| 115 | #define QUEUED 5 /* there is work queued on this connection */ | ||
| 116 | #define BUSY 6 /* work is being done */ | ||
| 117 | #define STANDBY 8 /* no outgoing messages, socket closed. we keep | ||
| 118 | * the ceph_connection around to maintain shared | ||
| 119 | * state with the peer. */ | ||
| 120 | #define CLOSED 10 /* we've closed the connection */ | ||
| 121 | #define SOCK_CLOSED 11 /* socket state changed to closed */ | ||
| 122 | #define OPENING 13 /* open connection w/ (possibly new) peer */ | ||
| 123 | #define DEAD 14 /* dead, about to kfree */ | ||
| 124 | |||
| 125 | /* | ||
| 126 | * A single connection with another host. | ||
| 127 | * | ||
| 128 | * We maintain a queue of outgoing messages, and some session state to | ||
| 129 | * ensure that we can preserve the lossless, ordered delivery of | ||
| 130 | * messages in the case of a TCP disconnect. | ||
| 131 | */ | ||
| 132 | struct ceph_connection { | ||
| 133 | void *private; | ||
| 134 | atomic_t nref; | ||
| 135 | |||
| 136 | const struct ceph_connection_operations *ops; | ||
| 137 | |||
| 138 | struct ceph_messenger *msgr; | ||
| 139 | struct socket *sock; | ||
| 140 | unsigned long state; /* connection state (see flags above) */ | ||
| 141 | const char *error_msg; /* error message, if any */ | ||
| 142 | |||
| 143 | struct ceph_entity_addr peer_addr; /* peer address */ | ||
| 144 | struct ceph_entity_name peer_name; /* peer name */ | ||
| 145 | struct ceph_entity_addr peer_addr_for_me; | ||
| 146 | u32 connect_seq; /* identify the most recent connection | ||
| 147 | attempt for this connection, client */ | ||
| 148 | u32 peer_global_seq; /* peer's global seq for this connection */ | ||
| 149 | |||
| 150 | int auth_retry; /* true if we need a newer authorizer */ | ||
| 151 | void *auth_reply_buf; /* where to put the authorizer reply */ | ||
| 152 | int auth_reply_buf_len; | ||
| 153 | |||
| 154 | struct mutex mutex; | ||
| 155 | |||
| 156 | /* out queue */ | ||
| 157 | struct list_head out_queue; | ||
| 158 | struct list_head out_sent; /* sending or sent but unacked */ | ||
| 159 | u64 out_seq; /* last message queued for send */ | ||
| 160 | u64 out_seq_sent; /* last message sent */ | ||
| 161 | bool out_keepalive_pending; | ||
| 162 | |||
| 163 | u64 in_seq, in_seq_acked; /* last message received, acked */ | ||
| 164 | |||
| 165 | /* connection negotiation temps */ | ||
| 166 | char in_banner[CEPH_BANNER_MAX_LEN]; | ||
| 167 | union { | ||
| 168 | struct { /* outgoing connection */ | ||
| 169 | struct ceph_msg_connect out_connect; | ||
| 170 | struct ceph_msg_connect_reply in_reply; | ||
| 171 | }; | ||
| 172 | struct { /* incoming */ | ||
| 173 | struct ceph_msg_connect in_connect; | ||
| 174 | struct ceph_msg_connect_reply out_reply; | ||
| 175 | }; | ||
| 176 | }; | ||
| 177 | struct ceph_entity_addr actual_peer_addr; | ||
| 178 | |||
| 179 | /* message out temps */ | ||
| 180 | struct ceph_msg *out_msg; /* sending message (== tail of | ||
| 181 | out_sent) */ | ||
| 182 | bool out_msg_done; | ||
| 183 | struct ceph_msg_pos out_msg_pos; | ||
| 184 | |||
| 185 | struct kvec out_kvec[8], /* sending header/footer data */ | ||
| 186 | *out_kvec_cur; | ||
| 187 | int out_kvec_left; /* kvec's left in out_kvec */ | ||
| 188 | int out_skip; /* skip this many bytes */ | ||
| 189 | int out_kvec_bytes; /* total bytes left */ | ||
| 190 | bool out_kvec_is_msg; /* kvec refers to out_msg */ | ||
| 191 | int out_more; /* there is more data after the kvecs */ | ||
| 192 | __le64 out_temp_ack; /* for writing an ack */ | ||
| 193 | |||
| 194 | /* message in temps */ | ||
| 195 | struct ceph_msg_header in_hdr; | ||
| 196 | struct ceph_msg *in_msg; | ||
| 197 | struct ceph_msg_pos in_msg_pos; | ||
| 198 | u32 in_front_crc, in_middle_crc, in_data_crc; /* calculated crc */ | ||
| 199 | |||
| 200 | char in_tag; /* protocol control byte */ | ||
| 201 | int in_base_pos; /* bytes read */ | ||
| 202 | __le64 in_temp_ack; /* for reading an ack */ | ||
| 203 | |||
| 204 | struct delayed_work work; /* send|recv work */ | ||
| 205 | unsigned long delay; /* current delay interval */ | ||
| 206 | }; | ||
| 207 | |||
| 208 | |||
| 209 | extern const char *pr_addr(const struct sockaddr_storage *ss); | ||
| 210 | extern int ceph_parse_ips(const char *c, const char *end, | ||
| 211 | struct ceph_entity_addr *addr, | ||
| 212 | int max_count, int *count); | ||
| 213 | |||
| 214 | |||
| 215 | extern int ceph_msgr_init(void); | ||
| 216 | extern void ceph_msgr_exit(void); | ||
| 217 | |||
| 218 | extern struct ceph_messenger *ceph_messenger_create( | ||
| 219 | struct ceph_entity_addr *myaddr); | ||
| 220 | extern void ceph_messenger_destroy(struct ceph_messenger *); | ||
| 221 | |||
| 222 | extern void ceph_con_init(struct ceph_messenger *msgr, | ||
| 223 | struct ceph_connection *con); | ||
| 224 | extern void ceph_con_open(struct ceph_connection *con, | ||
| 225 | struct ceph_entity_addr *addr); | ||
| 226 | extern bool ceph_con_opened(struct ceph_connection *con); | ||
| 227 | extern void ceph_con_close(struct ceph_connection *con); | ||
| 228 | extern void ceph_con_send(struct ceph_connection *con, struct ceph_msg *msg); | ||
| 229 | extern void ceph_con_revoke(struct ceph_connection *con, struct ceph_msg *msg); | ||
| 230 | extern void ceph_con_revoke_message(struct ceph_connection *con, | ||
| 231 | struct ceph_msg *msg); | ||
| 232 | extern void ceph_con_keepalive(struct ceph_connection *con); | ||
| 233 | extern struct ceph_connection *ceph_con_get(struct ceph_connection *con); | ||
| 234 | extern void ceph_con_put(struct ceph_connection *con); | ||
| 235 | |||
| 236 | extern struct ceph_msg *ceph_msg_new(int type, int front_len, | ||
| 237 | int page_len, int page_off, | ||
| 238 | struct page **pages); | ||
| 239 | extern void ceph_msg_kfree(struct ceph_msg *m); | ||
| 240 | |||
| 241 | |||
| 242 | static inline struct ceph_msg *ceph_msg_get(struct ceph_msg *msg) | ||
| 243 | { | ||
| 244 | kref_get(&msg->kref); | ||
| 245 | return msg; | ||
| 246 | } | ||
| 247 | extern void ceph_msg_last_put(struct kref *kref); | ||
| 248 | static inline void ceph_msg_put(struct ceph_msg *msg) | ||
| 249 | { | ||
| 250 | kref_put(&msg->kref, ceph_msg_last_put); | ||
| 251 | } | ||
| 252 | |||
| 253 | extern void ceph_msg_dump(struct ceph_msg *msg); | ||
| 254 | |||
| 255 | #endif | ||
diff --git a/fs/ceph/mon_client.c b/fs/ceph/mon_client.c new file mode 100644 index 000000000000..890597c09d43 --- /dev/null +++ b/fs/ceph/mon_client.c | |||
| @@ -0,0 +1,834 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/types.h> | ||
| 4 | #include <linux/random.h> | ||
| 5 | #include <linux/sched.h> | ||
| 6 | |||
| 7 | #include "mon_client.h" | ||
| 8 | #include "super.h" | ||
| 9 | #include "auth.h" | ||
| 10 | #include "decode.h" | ||
| 11 | |||
| 12 | /* | ||
| 13 | * Interact with Ceph monitor cluster. Handle requests for new map | ||
| 14 | * versions, and periodically resend as needed. Also implement | ||
| 15 | * statfs() and umount(). | ||
| 16 | * | ||
| 17 | * A small cluster of Ceph "monitors" are responsible for managing critical | ||
| 18 | * cluster configuration and state information. An odd number (e.g., 3, 5) | ||
| 19 | * of cmon daemons use a modified version of the Paxos part-time parliament | ||
| 20 | * algorithm to manage the MDS map (mds cluster membership), OSD map, and | ||
| 21 | * list of clients who have mounted the file system. | ||
| 22 | * | ||
| 23 | * We maintain an open, active session with a monitor at all times in order to | ||
| 24 | * receive timely MDSMap updates. We periodically send a keepalive byte on the | ||
| 25 | * TCP socket to ensure we detect a failure. If the connection does break, we | ||
| 26 | * randomly hunt for a new monitor. Once the connection is reestablished, we | ||
| 27 | * resend any outstanding requests. | ||
| 28 | */ | ||
| 29 | |||
| 30 | const static struct ceph_connection_operations mon_con_ops; | ||
| 31 | |||
| 32 | static int __validate_auth(struct ceph_mon_client *monc); | ||
| 33 | |||
| 34 | /* | ||
| 35 | * Decode a monmap blob (e.g., during mount). | ||
| 36 | */ | ||
| 37 | struct ceph_monmap *ceph_monmap_decode(void *p, void *end) | ||
| 38 | { | ||
| 39 | struct ceph_monmap *m = NULL; | ||
| 40 | int i, err = -EINVAL; | ||
| 41 | struct ceph_fsid fsid; | ||
| 42 | u32 epoch, num_mon; | ||
| 43 | u16 version; | ||
| 44 | u32 len; | ||
| 45 | |||
| 46 | ceph_decode_32_safe(&p, end, len, bad); | ||
| 47 | ceph_decode_need(&p, end, len, bad); | ||
| 48 | |||
| 49 | dout("monmap_decode %p %p len %d\n", p, end, (int)(end-p)); | ||
| 50 | |||
| 51 | ceph_decode_16_safe(&p, end, version, bad); | ||
| 52 | |||
| 53 | ceph_decode_need(&p, end, sizeof(fsid) + 2*sizeof(u32), bad); | ||
| 54 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); | ||
| 55 | epoch = ceph_decode_32(&p); | ||
| 56 | |||
| 57 | num_mon = ceph_decode_32(&p); | ||
| 58 | ceph_decode_need(&p, end, num_mon*sizeof(m->mon_inst[0]), bad); | ||
| 59 | |||
| 60 | if (num_mon >= CEPH_MAX_MON) | ||
| 61 | goto bad; | ||
| 62 | m = kmalloc(sizeof(*m) + sizeof(m->mon_inst[0])*num_mon, GFP_NOFS); | ||
| 63 | if (m == NULL) | ||
| 64 | return ERR_PTR(-ENOMEM); | ||
| 65 | m->fsid = fsid; | ||
| 66 | m->epoch = epoch; | ||
| 67 | m->num_mon = num_mon; | ||
| 68 | ceph_decode_copy(&p, m->mon_inst, num_mon*sizeof(m->mon_inst[0])); | ||
| 69 | for (i = 0; i < num_mon; i++) | ||
| 70 | ceph_decode_addr(&m->mon_inst[i].addr); | ||
| 71 | |||
| 72 | dout("monmap_decode epoch %d, num_mon %d\n", m->epoch, | ||
| 73 | m->num_mon); | ||
| 74 | for (i = 0; i < m->num_mon; i++) | ||
| 75 | dout("monmap_decode mon%d is %s\n", i, | ||
| 76 | pr_addr(&m->mon_inst[i].addr.in_addr)); | ||
| 77 | return m; | ||
| 78 | |||
| 79 | bad: | ||
| 80 | dout("monmap_decode failed with %d\n", err); | ||
| 81 | kfree(m); | ||
| 82 | return ERR_PTR(err); | ||
| 83 | } | ||
| 84 | |||
| 85 | /* | ||
| 86 | * return true if *addr is included in the monmap. | ||
| 87 | */ | ||
| 88 | int ceph_monmap_contains(struct ceph_monmap *m, struct ceph_entity_addr *addr) | ||
| 89 | { | ||
| 90 | int i; | ||
| 91 | |||
| 92 | for (i = 0; i < m->num_mon; i++) | ||
| 93 | if (memcmp(addr, &m->mon_inst[i].addr, sizeof(*addr)) == 0) | ||
| 94 | return 1; | ||
| 95 | return 0; | ||
| 96 | } | ||
| 97 | |||
| 98 | /* | ||
| 99 | * Send an auth request. | ||
| 100 | */ | ||
| 101 | static void __send_prepared_auth_request(struct ceph_mon_client *monc, int len) | ||
| 102 | { | ||
| 103 | monc->pending_auth = 1; | ||
| 104 | monc->m_auth->front.iov_len = len; | ||
| 105 | monc->m_auth->hdr.front_len = cpu_to_le32(len); | ||
| 106 | ceph_msg_get(monc->m_auth); /* keep our ref */ | ||
| 107 | ceph_con_send(monc->con, monc->m_auth); | ||
| 108 | } | ||
| 109 | |||
| 110 | /* | ||
| 111 | * Close monitor session, if any. | ||
| 112 | */ | ||
| 113 | static void __close_session(struct ceph_mon_client *monc) | ||
| 114 | { | ||
| 115 | if (monc->con) { | ||
| 116 | dout("__close_session closing mon%d\n", monc->cur_mon); | ||
| 117 | ceph_con_revoke(monc->con, monc->m_auth); | ||
| 118 | ceph_con_close(monc->con); | ||
| 119 | monc->cur_mon = -1; | ||
| 120 | monc->pending_auth = 0; | ||
| 121 | ceph_auth_reset(monc->auth); | ||
| 122 | } | ||
| 123 | } | ||
| 124 | |||
| 125 | /* | ||
| 126 | * Open a session with a (new) monitor. | ||
| 127 | */ | ||
| 128 | static int __open_session(struct ceph_mon_client *monc) | ||
| 129 | { | ||
| 130 | char r; | ||
| 131 | int ret; | ||
| 132 | |||
| 133 | if (monc->cur_mon < 0) { | ||
| 134 | get_random_bytes(&r, 1); | ||
| 135 | monc->cur_mon = r % monc->monmap->num_mon; | ||
| 136 | dout("open_session num=%d r=%d -> mon%d\n", | ||
| 137 | monc->monmap->num_mon, r, monc->cur_mon); | ||
| 138 | monc->sub_sent = 0; | ||
| 139 | monc->sub_renew_after = jiffies; /* i.e., expired */ | ||
| 140 | monc->want_next_osdmap = !!monc->want_next_osdmap; | ||
| 141 | |||
| 142 | dout("open_session mon%d opening\n", monc->cur_mon); | ||
| 143 | monc->con->peer_name.type = CEPH_ENTITY_TYPE_MON; | ||
| 144 | monc->con->peer_name.num = cpu_to_le64(monc->cur_mon); | ||
| 145 | ceph_con_open(monc->con, | ||
| 146 | &monc->monmap->mon_inst[monc->cur_mon].addr); | ||
| 147 | |||
| 148 | /* initiatiate authentication handshake */ | ||
| 149 | ret = ceph_auth_build_hello(monc->auth, | ||
| 150 | monc->m_auth->front.iov_base, | ||
| 151 | monc->m_auth->front_max); | ||
| 152 | __send_prepared_auth_request(monc, ret); | ||
| 153 | } else { | ||
| 154 | dout("open_session mon%d already open\n", monc->cur_mon); | ||
| 155 | } | ||
| 156 | return 0; | ||
| 157 | } | ||
| 158 | |||
| 159 | static bool __sub_expired(struct ceph_mon_client *monc) | ||
| 160 | { | ||
| 161 | return time_after_eq(jiffies, monc->sub_renew_after); | ||
| 162 | } | ||
| 163 | |||
| 164 | /* | ||
| 165 | * Reschedule delayed work timer. | ||
| 166 | */ | ||
| 167 | static void __schedule_delayed(struct ceph_mon_client *monc) | ||
| 168 | { | ||
| 169 | unsigned delay; | ||
| 170 | |||
| 171 | if (monc->cur_mon < 0 || __sub_expired(monc)) | ||
| 172 | delay = 10 * HZ; | ||
| 173 | else | ||
| 174 | delay = 20 * HZ; | ||
| 175 | dout("__schedule_delayed after %u\n", delay); | ||
| 176 | schedule_delayed_work(&monc->delayed_work, delay); | ||
| 177 | } | ||
| 178 | |||
| 179 | /* | ||
| 180 | * Send subscribe request for mdsmap and/or osdmap. | ||
| 181 | */ | ||
| 182 | static void __send_subscribe(struct ceph_mon_client *monc) | ||
| 183 | { | ||
| 184 | dout("__send_subscribe sub_sent=%u exp=%u want_osd=%d\n", | ||
| 185 | (unsigned)monc->sub_sent, __sub_expired(monc), | ||
| 186 | monc->want_next_osdmap); | ||
| 187 | if ((__sub_expired(monc) && !monc->sub_sent) || | ||
| 188 | monc->want_next_osdmap == 1) { | ||
| 189 | struct ceph_msg *msg; | ||
| 190 | struct ceph_mon_subscribe_item *i; | ||
| 191 | void *p, *end; | ||
| 192 | |||
| 193 | msg = ceph_msg_new(CEPH_MSG_MON_SUBSCRIBE, 96, 0, 0, NULL); | ||
| 194 | if (!msg) | ||
| 195 | return; | ||
| 196 | |||
| 197 | p = msg->front.iov_base; | ||
| 198 | end = p + msg->front.iov_len; | ||
| 199 | |||
| 200 | dout("__send_subscribe to 'mdsmap' %u+\n", | ||
| 201 | (unsigned)monc->have_mdsmap); | ||
| 202 | if (monc->want_next_osdmap) { | ||
| 203 | dout("__send_subscribe to 'osdmap' %u\n", | ||
| 204 | (unsigned)monc->have_osdmap); | ||
| 205 | ceph_encode_32(&p, 3); | ||
| 206 | ceph_encode_string(&p, end, "osdmap", 6); | ||
| 207 | i = p; | ||
| 208 | i->have = cpu_to_le64(monc->have_osdmap); | ||
| 209 | i->onetime = 1; | ||
| 210 | p += sizeof(*i); | ||
| 211 | monc->want_next_osdmap = 2; /* requested */ | ||
| 212 | } else { | ||
| 213 | ceph_encode_32(&p, 2); | ||
| 214 | } | ||
| 215 | ceph_encode_string(&p, end, "mdsmap", 6); | ||
| 216 | i = p; | ||
| 217 | i->have = cpu_to_le64(monc->have_mdsmap); | ||
| 218 | i->onetime = 0; | ||
| 219 | p += sizeof(*i); | ||
| 220 | ceph_encode_string(&p, end, "monmap", 6); | ||
| 221 | i = p; | ||
| 222 | i->have = 0; | ||
| 223 | i->onetime = 0; | ||
| 224 | p += sizeof(*i); | ||
| 225 | |||
| 226 | msg->front.iov_len = p - msg->front.iov_base; | ||
| 227 | msg->hdr.front_len = cpu_to_le32(msg->front.iov_len); | ||
| 228 | ceph_con_send(monc->con, msg); | ||
| 229 | |||
| 230 | monc->sub_sent = jiffies | 1; /* never 0 */ | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | static void handle_subscribe_ack(struct ceph_mon_client *monc, | ||
| 235 | struct ceph_msg *msg) | ||
| 236 | { | ||
| 237 | unsigned seconds; | ||
| 238 | struct ceph_mon_subscribe_ack *h = msg->front.iov_base; | ||
| 239 | |||
| 240 | if (msg->front.iov_len < sizeof(*h)) | ||
| 241 | goto bad; | ||
| 242 | seconds = le32_to_cpu(h->duration); | ||
| 243 | |||
| 244 | mutex_lock(&monc->mutex); | ||
| 245 | if (monc->hunting) { | ||
| 246 | pr_info("mon%d %s session established\n", | ||
| 247 | monc->cur_mon, pr_addr(&monc->con->peer_addr.in_addr)); | ||
| 248 | monc->hunting = false; | ||
| 249 | } | ||
| 250 | dout("handle_subscribe_ack after %d seconds\n", seconds); | ||
| 251 | monc->sub_renew_after = monc->sub_sent + (seconds >> 1)*HZ - 1; | ||
| 252 | monc->sub_sent = 0; | ||
| 253 | mutex_unlock(&monc->mutex); | ||
| 254 | return; | ||
| 255 | bad: | ||
| 256 | pr_err("got corrupt subscribe-ack msg\n"); | ||
| 257 | ceph_msg_dump(msg); | ||
| 258 | } | ||
| 259 | |||
| 260 | /* | ||
| 261 | * Keep track of which maps we have | ||
| 262 | */ | ||
| 263 | int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 got) | ||
| 264 | { | ||
| 265 | mutex_lock(&monc->mutex); | ||
| 266 | monc->have_mdsmap = got; | ||
| 267 | mutex_unlock(&monc->mutex); | ||
| 268 | return 0; | ||
| 269 | } | ||
| 270 | |||
| 271 | int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 got) | ||
| 272 | { | ||
| 273 | mutex_lock(&monc->mutex); | ||
| 274 | monc->have_osdmap = got; | ||
| 275 | monc->want_next_osdmap = 0; | ||
| 276 | mutex_unlock(&monc->mutex); | ||
| 277 | return 0; | ||
| 278 | } | ||
| 279 | |||
| 280 | /* | ||
| 281 | * Register interest in the next osdmap | ||
| 282 | */ | ||
| 283 | void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc) | ||
| 284 | { | ||
| 285 | dout("request_next_osdmap have %u\n", monc->have_osdmap); | ||
| 286 | mutex_lock(&monc->mutex); | ||
| 287 | if (!monc->want_next_osdmap) | ||
| 288 | monc->want_next_osdmap = 1; | ||
| 289 | if (monc->want_next_osdmap < 2) | ||
| 290 | __send_subscribe(monc); | ||
| 291 | mutex_unlock(&monc->mutex); | ||
| 292 | } | ||
| 293 | |||
| 294 | /* | ||
| 295 | * | ||
| 296 | */ | ||
| 297 | int ceph_monc_open_session(struct ceph_mon_client *monc) | ||
| 298 | { | ||
| 299 | if (!monc->con) { | ||
| 300 | monc->con = kmalloc(sizeof(*monc->con), GFP_KERNEL); | ||
| 301 | if (!monc->con) | ||
| 302 | return -ENOMEM; | ||
| 303 | ceph_con_init(monc->client->msgr, monc->con); | ||
| 304 | monc->con->private = monc; | ||
| 305 | monc->con->ops = &mon_con_ops; | ||
| 306 | } | ||
| 307 | |||
| 308 | mutex_lock(&monc->mutex); | ||
| 309 | __open_session(monc); | ||
| 310 | __schedule_delayed(monc); | ||
| 311 | mutex_unlock(&monc->mutex); | ||
| 312 | return 0; | ||
| 313 | } | ||
| 314 | |||
| 315 | /* | ||
| 316 | * The monitor responds with mount ack indicate mount success. The | ||
| 317 | * included client ticket allows the client to talk to MDSs and OSDs. | ||
| 318 | */ | ||
| 319 | static void ceph_monc_handle_map(struct ceph_mon_client *monc, | ||
| 320 | struct ceph_msg *msg) | ||
| 321 | { | ||
| 322 | struct ceph_client *client = monc->client; | ||
| 323 | struct ceph_monmap *monmap = NULL, *old = monc->monmap; | ||
| 324 | void *p, *end; | ||
| 325 | |||
| 326 | mutex_lock(&monc->mutex); | ||
| 327 | |||
| 328 | dout("handle_monmap\n"); | ||
| 329 | p = msg->front.iov_base; | ||
| 330 | end = p + msg->front.iov_len; | ||
| 331 | |||
| 332 | monmap = ceph_monmap_decode(p, end); | ||
| 333 | if (IS_ERR(monmap)) { | ||
| 334 | pr_err("problem decoding monmap, %d\n", | ||
| 335 | (int)PTR_ERR(monmap)); | ||
| 336 | goto out; | ||
| 337 | } | ||
| 338 | |||
| 339 | if (ceph_check_fsid(monc->client, &monmap->fsid) < 0) { | ||
| 340 | kfree(monmap); | ||
| 341 | goto out; | ||
| 342 | } | ||
| 343 | |||
| 344 | client->monc.monmap = monmap; | ||
| 345 | kfree(old); | ||
| 346 | |||
| 347 | out: | ||
| 348 | mutex_unlock(&monc->mutex); | ||
| 349 | wake_up(&client->auth_wq); | ||
| 350 | } | ||
| 351 | |||
| 352 | /* | ||
| 353 | * statfs | ||
| 354 | */ | ||
| 355 | static struct ceph_mon_statfs_request *__lookup_statfs( | ||
| 356 | struct ceph_mon_client *monc, u64 tid) | ||
| 357 | { | ||
| 358 | struct ceph_mon_statfs_request *req; | ||
| 359 | struct rb_node *n = monc->statfs_request_tree.rb_node; | ||
| 360 | |||
| 361 | while (n) { | ||
| 362 | req = rb_entry(n, struct ceph_mon_statfs_request, node); | ||
| 363 | if (tid < req->tid) | ||
| 364 | n = n->rb_left; | ||
| 365 | else if (tid > req->tid) | ||
| 366 | n = n->rb_right; | ||
| 367 | else | ||
| 368 | return req; | ||
| 369 | } | ||
| 370 | return NULL; | ||
| 371 | } | ||
| 372 | |||
| 373 | static void __insert_statfs(struct ceph_mon_client *monc, | ||
| 374 | struct ceph_mon_statfs_request *new) | ||
| 375 | { | ||
| 376 | struct rb_node **p = &monc->statfs_request_tree.rb_node; | ||
| 377 | struct rb_node *parent = NULL; | ||
| 378 | struct ceph_mon_statfs_request *req = NULL; | ||
| 379 | |||
| 380 | while (*p) { | ||
| 381 | parent = *p; | ||
| 382 | req = rb_entry(parent, struct ceph_mon_statfs_request, node); | ||
| 383 | if (new->tid < req->tid) | ||
| 384 | p = &(*p)->rb_left; | ||
| 385 | else if (new->tid > req->tid) | ||
| 386 | p = &(*p)->rb_right; | ||
| 387 | else | ||
| 388 | BUG(); | ||
| 389 | } | ||
| 390 | |||
| 391 | rb_link_node(&new->node, parent, p); | ||
| 392 | rb_insert_color(&new->node, &monc->statfs_request_tree); | ||
| 393 | } | ||
| 394 | |||
| 395 | static void handle_statfs_reply(struct ceph_mon_client *monc, | ||
| 396 | struct ceph_msg *msg) | ||
| 397 | { | ||
| 398 | struct ceph_mon_statfs_request *req; | ||
| 399 | struct ceph_mon_statfs_reply *reply = msg->front.iov_base; | ||
| 400 | u64 tid; | ||
| 401 | |||
| 402 | if (msg->front.iov_len != sizeof(*reply)) | ||
| 403 | goto bad; | ||
| 404 | tid = le64_to_cpu(msg->hdr.tid); | ||
| 405 | dout("handle_statfs_reply %p tid %llu\n", msg, tid); | ||
| 406 | |||
| 407 | mutex_lock(&monc->mutex); | ||
| 408 | req = __lookup_statfs(monc, tid); | ||
| 409 | if (req) { | ||
| 410 | *req->buf = reply->st; | ||
| 411 | req->result = 0; | ||
| 412 | } | ||
| 413 | mutex_unlock(&monc->mutex); | ||
| 414 | if (req) | ||
| 415 | complete(&req->completion); | ||
| 416 | return; | ||
| 417 | |||
| 418 | bad: | ||
| 419 | pr_err("corrupt statfs reply, no tid\n"); | ||
| 420 | ceph_msg_dump(msg); | ||
| 421 | } | ||
| 422 | |||
| 423 | /* | ||
| 424 | * (re)send a statfs request | ||
| 425 | */ | ||
| 426 | static int send_statfs(struct ceph_mon_client *monc, | ||
| 427 | struct ceph_mon_statfs_request *req) | ||
| 428 | { | ||
| 429 | struct ceph_msg *msg; | ||
| 430 | struct ceph_mon_statfs *h; | ||
| 431 | |||
| 432 | dout("send_statfs tid %llu\n", req->tid); | ||
| 433 | msg = ceph_msg_new(CEPH_MSG_STATFS, sizeof(*h), 0, 0, NULL); | ||
| 434 | if (IS_ERR(msg)) | ||
| 435 | return PTR_ERR(msg); | ||
| 436 | req->request = msg; | ||
| 437 | msg->hdr.tid = cpu_to_le64(req->tid); | ||
| 438 | h = msg->front.iov_base; | ||
| 439 | h->monhdr.have_version = 0; | ||
| 440 | h->monhdr.session_mon = cpu_to_le16(-1); | ||
| 441 | h->monhdr.session_mon_tid = 0; | ||
| 442 | h->fsid = monc->monmap->fsid; | ||
| 443 | ceph_con_send(monc->con, msg); | ||
| 444 | return 0; | ||
| 445 | } | ||
| 446 | |||
| 447 | /* | ||
| 448 | * Do a synchronous statfs(). | ||
| 449 | */ | ||
| 450 | int ceph_monc_do_statfs(struct ceph_mon_client *monc, struct ceph_statfs *buf) | ||
| 451 | { | ||
| 452 | struct ceph_mon_statfs_request req; | ||
| 453 | int err; | ||
| 454 | |||
| 455 | req.buf = buf; | ||
| 456 | init_completion(&req.completion); | ||
| 457 | |||
| 458 | /* allocate memory for reply */ | ||
| 459 | err = ceph_msgpool_resv(&monc->msgpool_statfs_reply, 1); | ||
| 460 | if (err) | ||
| 461 | return err; | ||
| 462 | |||
| 463 | /* register request */ | ||
| 464 | mutex_lock(&monc->mutex); | ||
| 465 | req.tid = ++monc->last_tid; | ||
| 466 | req.last_attempt = jiffies; | ||
| 467 | req.delay = BASE_DELAY_INTERVAL; | ||
| 468 | __insert_statfs(monc, &req); | ||
| 469 | monc->num_statfs_requests++; | ||
| 470 | mutex_unlock(&monc->mutex); | ||
| 471 | |||
| 472 | /* send request and wait */ | ||
| 473 | err = send_statfs(monc, &req); | ||
| 474 | if (!err) | ||
| 475 | err = wait_for_completion_interruptible(&req.completion); | ||
| 476 | |||
| 477 | mutex_lock(&monc->mutex); | ||
| 478 | rb_erase(&req.node, &monc->statfs_request_tree); | ||
| 479 | monc->num_statfs_requests--; | ||
| 480 | ceph_msgpool_resv(&monc->msgpool_statfs_reply, -1); | ||
| 481 | mutex_unlock(&monc->mutex); | ||
| 482 | |||
| 483 | if (!err) | ||
| 484 | err = req.result; | ||
| 485 | return err; | ||
| 486 | } | ||
| 487 | |||
| 488 | /* | ||
| 489 | * Resend pending statfs requests. | ||
| 490 | */ | ||
| 491 | static void __resend_statfs(struct ceph_mon_client *monc) | ||
| 492 | { | ||
| 493 | struct ceph_mon_statfs_request *req; | ||
| 494 | struct rb_node *p; | ||
| 495 | |||
| 496 | for (p = rb_first(&monc->statfs_request_tree); p; p = rb_next(p)) { | ||
| 497 | req = rb_entry(p, struct ceph_mon_statfs_request, node); | ||
| 498 | send_statfs(monc, req); | ||
| 499 | } | ||
| 500 | } | ||
| 501 | |||
| 502 | /* | ||
| 503 | * Delayed work. If we haven't mounted yet, retry. Otherwise, | ||
| 504 | * renew/retry subscription as needed (in case it is timing out, or we | ||
| 505 | * got an ENOMEM). And keep the monitor connection alive. | ||
| 506 | */ | ||
| 507 | static void delayed_work(struct work_struct *work) | ||
| 508 | { | ||
| 509 | struct ceph_mon_client *monc = | ||
| 510 | container_of(work, struct ceph_mon_client, delayed_work.work); | ||
| 511 | |||
| 512 | dout("monc delayed_work\n"); | ||
| 513 | mutex_lock(&monc->mutex); | ||
| 514 | if (monc->hunting) { | ||
| 515 | __close_session(monc); | ||
| 516 | __open_session(monc); /* continue hunting */ | ||
| 517 | } else { | ||
| 518 | ceph_con_keepalive(monc->con); | ||
| 519 | |||
| 520 | __validate_auth(monc); | ||
| 521 | |||
| 522 | if (monc->auth->ops->is_authenticated(monc->auth)) | ||
| 523 | __send_subscribe(monc); | ||
| 524 | } | ||
| 525 | __schedule_delayed(monc); | ||
| 526 | mutex_unlock(&monc->mutex); | ||
| 527 | } | ||
| 528 | |||
| 529 | /* | ||
| 530 | * On startup, we build a temporary monmap populated with the IPs | ||
| 531 | * provided by mount(2). | ||
| 532 | */ | ||
| 533 | static int build_initial_monmap(struct ceph_mon_client *monc) | ||
| 534 | { | ||
| 535 | struct ceph_mount_args *args = monc->client->mount_args; | ||
| 536 | struct ceph_entity_addr *mon_addr = args->mon_addr; | ||
| 537 | int num_mon = args->num_mon; | ||
| 538 | int i; | ||
| 539 | |||
| 540 | /* build initial monmap */ | ||
| 541 | monc->monmap = kzalloc(sizeof(*monc->monmap) + | ||
| 542 | num_mon*sizeof(monc->monmap->mon_inst[0]), | ||
| 543 | GFP_KERNEL); | ||
| 544 | if (!monc->monmap) | ||
| 545 | return -ENOMEM; | ||
| 546 | for (i = 0; i < num_mon; i++) { | ||
| 547 | monc->monmap->mon_inst[i].addr = mon_addr[i]; | ||
| 548 | monc->monmap->mon_inst[i].addr.nonce = 0; | ||
| 549 | monc->monmap->mon_inst[i].name.type = | ||
| 550 | CEPH_ENTITY_TYPE_MON; | ||
| 551 | monc->monmap->mon_inst[i].name.num = cpu_to_le64(i); | ||
| 552 | } | ||
| 553 | monc->monmap->num_mon = num_mon; | ||
| 554 | monc->have_fsid = false; | ||
| 555 | |||
| 556 | /* release addr memory */ | ||
| 557 | kfree(args->mon_addr); | ||
| 558 | args->mon_addr = NULL; | ||
| 559 | args->num_mon = 0; | ||
| 560 | return 0; | ||
| 561 | } | ||
| 562 | |||
| 563 | int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl) | ||
| 564 | { | ||
| 565 | int err = 0; | ||
| 566 | |||
| 567 | dout("init\n"); | ||
| 568 | memset(monc, 0, sizeof(*monc)); | ||
| 569 | monc->client = cl; | ||
| 570 | monc->monmap = NULL; | ||
| 571 | mutex_init(&monc->mutex); | ||
| 572 | |||
| 573 | err = build_initial_monmap(monc); | ||
| 574 | if (err) | ||
| 575 | goto out; | ||
| 576 | |||
| 577 | monc->con = NULL; | ||
| 578 | |||
| 579 | /* authentication */ | ||
| 580 | monc->auth = ceph_auth_init(cl->mount_args->name, | ||
| 581 | cl->mount_args->secret); | ||
| 582 | if (IS_ERR(monc->auth)) | ||
| 583 | return PTR_ERR(monc->auth); | ||
| 584 | monc->auth->want_keys = | ||
| 585 | CEPH_ENTITY_TYPE_AUTH | CEPH_ENTITY_TYPE_MON | | ||
| 586 | CEPH_ENTITY_TYPE_OSD | CEPH_ENTITY_TYPE_MDS; | ||
| 587 | |||
| 588 | /* msg pools */ | ||
| 589 | err = ceph_msgpool_init(&monc->msgpool_subscribe_ack, | ||
| 590 | sizeof(struct ceph_mon_subscribe_ack), 1, false); | ||
| 591 | if (err < 0) | ||
| 592 | goto out_monmap; | ||
| 593 | err = ceph_msgpool_init(&monc->msgpool_statfs_reply, | ||
| 594 | sizeof(struct ceph_mon_statfs_reply), 0, false); | ||
| 595 | if (err < 0) | ||
| 596 | goto out_pool1; | ||
| 597 | err = ceph_msgpool_init(&monc->msgpool_auth_reply, 4096, 1, false); | ||
| 598 | if (err < 0) | ||
| 599 | goto out_pool2; | ||
| 600 | |||
| 601 | monc->m_auth = ceph_msg_new(CEPH_MSG_AUTH, 4096, 0, 0, NULL); | ||
| 602 | monc->pending_auth = 0; | ||
| 603 | if (IS_ERR(monc->m_auth)) { | ||
| 604 | err = PTR_ERR(monc->m_auth); | ||
| 605 | monc->m_auth = NULL; | ||
| 606 | goto out_pool3; | ||
| 607 | } | ||
| 608 | |||
| 609 | monc->cur_mon = -1; | ||
| 610 | monc->hunting = true; | ||
| 611 | monc->sub_renew_after = jiffies; | ||
| 612 | monc->sub_sent = 0; | ||
| 613 | |||
| 614 | INIT_DELAYED_WORK(&monc->delayed_work, delayed_work); | ||
| 615 | monc->statfs_request_tree = RB_ROOT; | ||
| 616 | monc->num_statfs_requests = 0; | ||
| 617 | monc->last_tid = 0; | ||
| 618 | |||
| 619 | monc->have_mdsmap = 0; | ||
| 620 | monc->have_osdmap = 0; | ||
| 621 | monc->want_next_osdmap = 1; | ||
| 622 | return 0; | ||
| 623 | |||
| 624 | out_pool3: | ||
| 625 | ceph_msgpool_destroy(&monc->msgpool_auth_reply); | ||
| 626 | out_pool2: | ||
| 627 | ceph_msgpool_destroy(&monc->msgpool_subscribe_ack); | ||
| 628 | out_pool1: | ||
| 629 | ceph_msgpool_destroy(&monc->msgpool_statfs_reply); | ||
| 630 | out_monmap: | ||
| 631 | kfree(monc->monmap); | ||
| 632 | out: | ||
| 633 | return err; | ||
| 634 | } | ||
| 635 | |||
| 636 | void ceph_monc_stop(struct ceph_mon_client *monc) | ||
| 637 | { | ||
| 638 | dout("stop\n"); | ||
| 639 | cancel_delayed_work_sync(&monc->delayed_work); | ||
| 640 | |||
| 641 | mutex_lock(&monc->mutex); | ||
| 642 | __close_session(monc); | ||
| 643 | if (monc->con) { | ||
| 644 | monc->con->private = NULL; | ||
| 645 | monc->con->ops->put(monc->con); | ||
| 646 | monc->con = NULL; | ||
| 647 | } | ||
| 648 | mutex_unlock(&monc->mutex); | ||
| 649 | |||
| 650 | ceph_auth_destroy(monc->auth); | ||
| 651 | |||
| 652 | ceph_msg_put(monc->m_auth); | ||
| 653 | ceph_msgpool_destroy(&monc->msgpool_subscribe_ack); | ||
| 654 | ceph_msgpool_destroy(&monc->msgpool_statfs_reply); | ||
| 655 | ceph_msgpool_destroy(&monc->msgpool_auth_reply); | ||
| 656 | |||
| 657 | kfree(monc->monmap); | ||
| 658 | } | ||
| 659 | |||
| 660 | static void handle_auth_reply(struct ceph_mon_client *monc, | ||
| 661 | struct ceph_msg *msg) | ||
| 662 | { | ||
| 663 | int ret; | ||
| 664 | |||
| 665 | mutex_lock(&monc->mutex); | ||
| 666 | monc->pending_auth = 0; | ||
| 667 | ret = ceph_handle_auth_reply(monc->auth, msg->front.iov_base, | ||
| 668 | msg->front.iov_len, | ||
| 669 | monc->m_auth->front.iov_base, | ||
| 670 | monc->m_auth->front_max); | ||
| 671 | if (ret < 0) { | ||
| 672 | monc->client->auth_err = ret; | ||
| 673 | wake_up(&monc->client->auth_wq); | ||
| 674 | } else if (ret > 0) { | ||
| 675 | __send_prepared_auth_request(monc, ret); | ||
| 676 | } else if (monc->auth->ops->is_authenticated(monc->auth)) { | ||
| 677 | dout("authenticated, starting session\n"); | ||
| 678 | |||
| 679 | monc->client->msgr->inst.name.type = CEPH_ENTITY_TYPE_CLIENT; | ||
| 680 | monc->client->msgr->inst.name.num = monc->auth->global_id; | ||
| 681 | |||
| 682 | __send_subscribe(monc); | ||
| 683 | __resend_statfs(monc); | ||
| 684 | } | ||
| 685 | mutex_unlock(&monc->mutex); | ||
| 686 | } | ||
| 687 | |||
| 688 | static int __validate_auth(struct ceph_mon_client *monc) | ||
| 689 | { | ||
| 690 | int ret; | ||
| 691 | |||
| 692 | if (monc->pending_auth) | ||
| 693 | return 0; | ||
| 694 | |||
| 695 | ret = ceph_build_auth(monc->auth, monc->m_auth->front.iov_base, | ||
| 696 | monc->m_auth->front_max); | ||
| 697 | if (ret <= 0) | ||
| 698 | return ret; /* either an error, or no need to authenticate */ | ||
| 699 | __send_prepared_auth_request(monc, ret); | ||
| 700 | return 0; | ||
| 701 | } | ||
| 702 | |||
| 703 | int ceph_monc_validate_auth(struct ceph_mon_client *monc) | ||
| 704 | { | ||
| 705 | int ret; | ||
| 706 | |||
| 707 | mutex_lock(&monc->mutex); | ||
| 708 | ret = __validate_auth(monc); | ||
| 709 | mutex_unlock(&monc->mutex); | ||
| 710 | return ret; | ||
| 711 | } | ||
| 712 | |||
| 713 | /* | ||
| 714 | * handle incoming message | ||
| 715 | */ | ||
| 716 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) | ||
| 717 | { | ||
| 718 | struct ceph_mon_client *monc = con->private; | ||
| 719 | int type = le16_to_cpu(msg->hdr.type); | ||
| 720 | |||
| 721 | if (!monc) | ||
| 722 | return; | ||
| 723 | |||
| 724 | switch (type) { | ||
| 725 | case CEPH_MSG_AUTH_REPLY: | ||
| 726 | handle_auth_reply(monc, msg); | ||
| 727 | break; | ||
| 728 | |||
| 729 | case CEPH_MSG_MON_SUBSCRIBE_ACK: | ||
| 730 | handle_subscribe_ack(monc, msg); | ||
| 731 | break; | ||
| 732 | |||
| 733 | case CEPH_MSG_STATFS_REPLY: | ||
| 734 | handle_statfs_reply(monc, msg); | ||
| 735 | break; | ||
| 736 | |||
| 737 | case CEPH_MSG_MON_MAP: | ||
| 738 | ceph_monc_handle_map(monc, msg); | ||
| 739 | break; | ||
| 740 | |||
| 741 | case CEPH_MSG_MDS_MAP: | ||
| 742 | ceph_mdsc_handle_map(&monc->client->mdsc, msg); | ||
| 743 | break; | ||
| 744 | |||
| 745 | case CEPH_MSG_OSD_MAP: | ||
| 746 | ceph_osdc_handle_map(&monc->client->osdc, msg); | ||
| 747 | break; | ||
| 748 | |||
| 749 | default: | ||
| 750 | pr_err("received unknown message type %d %s\n", type, | ||
| 751 | ceph_msg_type_name(type)); | ||
| 752 | } | ||
| 753 | ceph_msg_put(msg); | ||
| 754 | } | ||
| 755 | |||
| 756 | /* | ||
| 757 | * Allocate memory for incoming message | ||
| 758 | */ | ||
| 759 | static struct ceph_msg *mon_alloc_msg(struct ceph_connection *con, | ||
| 760 | struct ceph_msg_header *hdr, | ||
| 761 | int *skip) | ||
| 762 | { | ||
| 763 | struct ceph_mon_client *monc = con->private; | ||
| 764 | int type = le16_to_cpu(hdr->type); | ||
| 765 | int front_len = le32_to_cpu(hdr->front_len); | ||
| 766 | struct ceph_msg *m = NULL; | ||
| 767 | |||
| 768 | *skip = 0; | ||
| 769 | |||
| 770 | switch (type) { | ||
| 771 | case CEPH_MSG_MON_SUBSCRIBE_ACK: | ||
| 772 | m = ceph_msgpool_get(&monc->msgpool_subscribe_ack, front_len); | ||
| 773 | break; | ||
| 774 | case CEPH_MSG_STATFS_REPLY: | ||
| 775 | m = ceph_msgpool_get(&monc->msgpool_statfs_reply, front_len); | ||
| 776 | break; | ||
| 777 | case CEPH_MSG_AUTH_REPLY: | ||
| 778 | m = ceph_msgpool_get(&monc->msgpool_auth_reply, front_len); | ||
| 779 | break; | ||
| 780 | case CEPH_MSG_MON_MAP: | ||
| 781 | case CEPH_MSG_MDS_MAP: | ||
| 782 | case CEPH_MSG_OSD_MAP: | ||
| 783 | m = ceph_msg_new(type, front_len, 0, 0, NULL); | ||
| 784 | break; | ||
| 785 | } | ||
| 786 | |||
| 787 | if (!m) { | ||
| 788 | pr_info("alloc_msg unknown type %d\n", type); | ||
| 789 | *skip = 1; | ||
| 790 | } | ||
| 791 | return m; | ||
| 792 | } | ||
| 793 | |||
| 794 | /* | ||
| 795 | * If the monitor connection resets, pick a new monitor and resubmit | ||
| 796 | * any pending requests. | ||
| 797 | */ | ||
| 798 | static void mon_fault(struct ceph_connection *con) | ||
| 799 | { | ||
| 800 | struct ceph_mon_client *monc = con->private; | ||
| 801 | |||
| 802 | if (!monc) | ||
| 803 | return; | ||
| 804 | |||
| 805 | dout("mon_fault\n"); | ||
| 806 | mutex_lock(&monc->mutex); | ||
| 807 | if (!con->private) | ||
| 808 | goto out; | ||
| 809 | |||
| 810 | if (monc->con && !monc->hunting) | ||
| 811 | pr_info("mon%d %s session lost, " | ||
| 812 | "hunting for new mon\n", monc->cur_mon, | ||
| 813 | pr_addr(&monc->con->peer_addr.in_addr)); | ||
| 814 | |||
| 815 | __close_session(monc); | ||
| 816 | if (!monc->hunting) { | ||
| 817 | /* start hunting */ | ||
| 818 | monc->hunting = true; | ||
| 819 | __open_session(monc); | ||
| 820 | } else { | ||
| 821 | /* already hunting, let's wait a bit */ | ||
| 822 | __schedule_delayed(monc); | ||
| 823 | } | ||
| 824 | out: | ||
| 825 | mutex_unlock(&monc->mutex); | ||
| 826 | } | ||
| 827 | |||
| 828 | const static struct ceph_connection_operations mon_con_ops = { | ||
| 829 | .get = ceph_con_get, | ||
| 830 | .put = ceph_con_put, | ||
| 831 | .dispatch = dispatch, | ||
| 832 | .fault = mon_fault, | ||
| 833 | .alloc_msg = mon_alloc_msg, | ||
| 834 | }; | ||
diff --git a/fs/ceph/mon_client.h b/fs/ceph/mon_client.h new file mode 100644 index 000000000000..b958ad5afa06 --- /dev/null +++ b/fs/ceph/mon_client.h | |||
| @@ -0,0 +1,119 @@ | |||
| 1 | #ifndef _FS_CEPH_MON_CLIENT_H | ||
| 2 | #define _FS_CEPH_MON_CLIENT_H | ||
| 3 | |||
| 4 | #include <linux/completion.h> | ||
| 5 | #include <linux/rbtree.h> | ||
| 6 | |||
| 7 | #include "messenger.h" | ||
| 8 | #include "msgpool.h" | ||
| 9 | |||
| 10 | struct ceph_client; | ||
| 11 | struct ceph_mount_args; | ||
| 12 | struct ceph_auth_client; | ||
| 13 | |||
| 14 | /* | ||
| 15 | * The monitor map enumerates the set of all monitors. | ||
| 16 | */ | ||
| 17 | struct ceph_monmap { | ||
| 18 | struct ceph_fsid fsid; | ||
| 19 | u32 epoch; | ||
| 20 | u32 num_mon; | ||
| 21 | struct ceph_entity_inst mon_inst[0]; | ||
| 22 | }; | ||
| 23 | |||
| 24 | struct ceph_mon_client; | ||
| 25 | struct ceph_mon_statfs_request; | ||
| 26 | |||
| 27 | |||
| 28 | /* | ||
| 29 | * Generic mechanism for resending monitor requests. | ||
| 30 | */ | ||
| 31 | typedef void (*ceph_monc_request_func_t)(struct ceph_mon_client *monc, | ||
| 32 | int newmon); | ||
| 33 | |||
| 34 | /* a pending monitor request */ | ||
| 35 | struct ceph_mon_request { | ||
| 36 | struct ceph_mon_client *monc; | ||
| 37 | struct delayed_work delayed_work; | ||
| 38 | unsigned long delay; | ||
| 39 | ceph_monc_request_func_t do_request; | ||
| 40 | }; | ||
| 41 | |||
| 42 | /* | ||
| 43 | * statfs() is done a bit differently because we need to get data back | ||
| 44 | * to the caller | ||
| 45 | */ | ||
| 46 | struct ceph_mon_statfs_request { | ||
| 47 | u64 tid; | ||
| 48 | struct rb_node node; | ||
| 49 | int result; | ||
| 50 | struct ceph_statfs *buf; | ||
| 51 | struct completion completion; | ||
| 52 | unsigned long last_attempt, delay; /* jiffies */ | ||
| 53 | struct ceph_msg *request; /* original request */ | ||
| 54 | }; | ||
| 55 | |||
| 56 | struct ceph_mon_client { | ||
| 57 | struct ceph_client *client; | ||
| 58 | struct ceph_monmap *monmap; | ||
| 59 | |||
| 60 | struct mutex mutex; | ||
| 61 | struct delayed_work delayed_work; | ||
| 62 | |||
| 63 | struct ceph_auth_client *auth; | ||
| 64 | struct ceph_msg *m_auth; | ||
| 65 | int pending_auth; | ||
| 66 | |||
| 67 | bool hunting; | ||
| 68 | int cur_mon; /* last monitor i contacted */ | ||
| 69 | unsigned long sub_sent, sub_renew_after; | ||
| 70 | struct ceph_connection *con; | ||
| 71 | bool have_fsid; | ||
| 72 | |||
| 73 | /* msg pools */ | ||
| 74 | struct ceph_msgpool msgpool_subscribe_ack; | ||
| 75 | struct ceph_msgpool msgpool_statfs_reply; | ||
| 76 | struct ceph_msgpool msgpool_auth_reply; | ||
| 77 | |||
| 78 | /* pending statfs requests */ | ||
| 79 | struct rb_root statfs_request_tree; | ||
| 80 | int num_statfs_requests; | ||
| 81 | u64 last_tid; | ||
| 82 | |||
| 83 | /* mds/osd map */ | ||
| 84 | int want_next_osdmap; /* 1 = want, 2 = want+asked */ | ||
| 85 | u32 have_osdmap, have_mdsmap; | ||
| 86 | |||
| 87 | #ifdef CONFIG_DEBUG_FS | ||
| 88 | struct dentry *debugfs_file; | ||
| 89 | #endif | ||
| 90 | }; | ||
| 91 | |||
| 92 | extern struct ceph_monmap *ceph_monmap_decode(void *p, void *end); | ||
| 93 | extern int ceph_monmap_contains(struct ceph_monmap *m, | ||
| 94 | struct ceph_entity_addr *addr); | ||
| 95 | |||
| 96 | extern int ceph_monc_init(struct ceph_mon_client *monc, struct ceph_client *cl); | ||
| 97 | extern void ceph_monc_stop(struct ceph_mon_client *monc); | ||
| 98 | |||
| 99 | /* | ||
| 100 | * The model here is to indicate that we need a new map of at least | ||
| 101 | * epoch @want, and also call in when we receive a map. We will | ||
| 102 | * periodically rerequest the map from the monitor cluster until we | ||
| 103 | * get what we want. | ||
| 104 | */ | ||
| 105 | extern int ceph_monc_got_mdsmap(struct ceph_mon_client *monc, u32 have); | ||
| 106 | extern int ceph_monc_got_osdmap(struct ceph_mon_client *monc, u32 have); | ||
| 107 | |||
| 108 | extern void ceph_monc_request_next_osdmap(struct ceph_mon_client *monc); | ||
| 109 | |||
| 110 | extern int ceph_monc_do_statfs(struct ceph_mon_client *monc, | ||
| 111 | struct ceph_statfs *buf); | ||
| 112 | |||
| 113 | extern int ceph_monc_open_session(struct ceph_mon_client *monc); | ||
| 114 | |||
| 115 | extern int ceph_monc_validate_auth(struct ceph_mon_client *monc); | ||
| 116 | |||
| 117 | |||
| 118 | |||
| 119 | #endif | ||
diff --git a/fs/ceph/msgpool.c b/fs/ceph/msgpool.c new file mode 100644 index 000000000000..ca3b44a89f2d --- /dev/null +++ b/fs/ceph/msgpool.c | |||
| @@ -0,0 +1,186 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/err.h> | ||
| 4 | #include <linux/sched.h> | ||
| 5 | #include <linux/types.h> | ||
| 6 | #include <linux/vmalloc.h> | ||
| 7 | |||
| 8 | #include "msgpool.h" | ||
| 9 | |||
| 10 | /* | ||
| 11 | * We use msg pools to preallocate memory for messages we expect to | ||
| 12 | * receive over the wire, to avoid getting ourselves into OOM | ||
| 13 | * conditions at unexpected times. We take use a few different | ||
| 14 | * strategies: | ||
| 15 | * | ||
| 16 | * - for request/response type interactions, we preallocate the | ||
| 17 | * memory needed for the response when we generate the request. | ||
| 18 | * | ||
| 19 | * - for messages we can receive at any time from the MDS, we preallocate | ||
| 20 | * a pool of messages we can re-use. | ||
| 21 | * | ||
| 22 | * - for writeback, we preallocate some number of messages to use for | ||
| 23 | * requests and their replies, so that we always make forward | ||
| 24 | * progress. | ||
| 25 | * | ||
| 26 | * The msgpool behaves like a mempool_t, but keeps preallocated | ||
| 27 | * ceph_msgs strung together on a list_head instead of using a pointer | ||
| 28 | * vector. This avoids vector reallocation when we adjust the number | ||
| 29 | * of preallocated items (which happens frequently). | ||
| 30 | */ | ||
| 31 | |||
| 32 | |||
| 33 | /* | ||
| 34 | * Allocate or release as necessary to meet our target pool size. | ||
| 35 | */ | ||
| 36 | static int __fill_msgpool(struct ceph_msgpool *pool) | ||
| 37 | { | ||
| 38 | struct ceph_msg *msg; | ||
| 39 | |||
| 40 | while (pool->num < pool->min) { | ||
| 41 | dout("fill_msgpool %p %d/%d allocating\n", pool, pool->num, | ||
| 42 | pool->min); | ||
| 43 | spin_unlock(&pool->lock); | ||
| 44 | msg = ceph_msg_new(0, pool->front_len, 0, 0, NULL); | ||
| 45 | spin_lock(&pool->lock); | ||
| 46 | if (IS_ERR(msg)) | ||
| 47 | return PTR_ERR(msg); | ||
| 48 | msg->pool = pool; | ||
| 49 | list_add(&msg->list_head, &pool->msgs); | ||
| 50 | pool->num++; | ||
| 51 | } | ||
| 52 | while (pool->num > pool->min) { | ||
| 53 | msg = list_first_entry(&pool->msgs, struct ceph_msg, list_head); | ||
| 54 | dout("fill_msgpool %p %d/%d releasing %p\n", pool, pool->num, | ||
| 55 | pool->min, msg); | ||
| 56 | list_del_init(&msg->list_head); | ||
| 57 | pool->num--; | ||
| 58 | ceph_msg_kfree(msg); | ||
| 59 | } | ||
| 60 | return 0; | ||
| 61 | } | ||
| 62 | |||
| 63 | int ceph_msgpool_init(struct ceph_msgpool *pool, | ||
| 64 | int front_len, int min, bool blocking) | ||
| 65 | { | ||
| 66 | int ret; | ||
| 67 | |||
| 68 | dout("msgpool_init %p front_len %d min %d\n", pool, front_len, min); | ||
| 69 | spin_lock_init(&pool->lock); | ||
| 70 | pool->front_len = front_len; | ||
| 71 | INIT_LIST_HEAD(&pool->msgs); | ||
| 72 | pool->num = 0; | ||
| 73 | pool->min = min; | ||
| 74 | pool->blocking = blocking; | ||
| 75 | init_waitqueue_head(&pool->wait); | ||
| 76 | |||
| 77 | spin_lock(&pool->lock); | ||
| 78 | ret = __fill_msgpool(pool); | ||
| 79 | spin_unlock(&pool->lock); | ||
| 80 | return ret; | ||
| 81 | } | ||
| 82 | |||
| 83 | void ceph_msgpool_destroy(struct ceph_msgpool *pool) | ||
| 84 | { | ||
| 85 | dout("msgpool_destroy %p\n", pool); | ||
| 86 | spin_lock(&pool->lock); | ||
| 87 | pool->min = 0; | ||
| 88 | __fill_msgpool(pool); | ||
| 89 | spin_unlock(&pool->lock); | ||
| 90 | } | ||
| 91 | |||
| 92 | int ceph_msgpool_resv(struct ceph_msgpool *pool, int delta) | ||
| 93 | { | ||
| 94 | int ret; | ||
| 95 | |||
| 96 | spin_lock(&pool->lock); | ||
| 97 | dout("msgpool_resv %p delta %d\n", pool, delta); | ||
| 98 | pool->min += delta; | ||
| 99 | ret = __fill_msgpool(pool); | ||
| 100 | spin_unlock(&pool->lock); | ||
| 101 | return ret; | ||
| 102 | } | ||
| 103 | |||
| 104 | struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *pool, int front_len) | ||
| 105 | { | ||
| 106 | wait_queue_t wait; | ||
| 107 | struct ceph_msg *msg; | ||
| 108 | |||
| 109 | if (front_len && front_len > pool->front_len) { | ||
| 110 | pr_err("msgpool_get pool %p need front %d, pool size is %d\n", | ||
| 111 | pool, front_len, pool->front_len); | ||
| 112 | WARN_ON(1); | ||
| 113 | |||
| 114 | /* try to alloc a fresh message */ | ||
| 115 | msg = ceph_msg_new(0, front_len, 0, 0, NULL); | ||
| 116 | if (!IS_ERR(msg)) | ||
| 117 | return msg; | ||
| 118 | } | ||
| 119 | |||
| 120 | if (!front_len) | ||
| 121 | front_len = pool->front_len; | ||
| 122 | |||
| 123 | if (pool->blocking) { | ||
| 124 | /* mempool_t behavior; first try to alloc */ | ||
| 125 | msg = ceph_msg_new(0, front_len, 0, 0, NULL); | ||
| 126 | if (!IS_ERR(msg)) | ||
| 127 | return msg; | ||
| 128 | } | ||
| 129 | |||
| 130 | while (1) { | ||
| 131 | spin_lock(&pool->lock); | ||
| 132 | if (likely(pool->num)) { | ||
| 133 | msg = list_entry(pool->msgs.next, struct ceph_msg, | ||
| 134 | list_head); | ||
| 135 | list_del_init(&msg->list_head); | ||
| 136 | pool->num--; | ||
| 137 | dout("msgpool_get %p got %p, now %d/%d\n", pool, msg, | ||
| 138 | pool->num, pool->min); | ||
| 139 | spin_unlock(&pool->lock); | ||
| 140 | return msg; | ||
| 141 | } | ||
| 142 | pr_err("msgpool_get %p now %d/%d, %s\n", pool, pool->num, | ||
| 143 | pool->min, pool->blocking ? "waiting" : "may fail"); | ||
| 144 | spin_unlock(&pool->lock); | ||
| 145 | |||
| 146 | if (!pool->blocking) { | ||
| 147 | WARN_ON(1); | ||
| 148 | |||
| 149 | /* maybe we can allocate it now? */ | ||
| 150 | msg = ceph_msg_new(0, front_len, 0, 0, NULL); | ||
| 151 | if (!IS_ERR(msg)) | ||
| 152 | return msg; | ||
| 153 | |||
| 154 | pr_err("msgpool_get %p empty + alloc failed\n", pool); | ||
| 155 | return ERR_PTR(-ENOMEM); | ||
| 156 | } | ||
| 157 | |||
| 158 | init_wait(&wait); | ||
| 159 | prepare_to_wait(&pool->wait, &wait, TASK_UNINTERRUPTIBLE); | ||
| 160 | schedule(); | ||
| 161 | finish_wait(&pool->wait, &wait); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | |||
| 165 | void ceph_msgpool_put(struct ceph_msgpool *pool, struct ceph_msg *msg) | ||
| 166 | { | ||
| 167 | spin_lock(&pool->lock); | ||
| 168 | if (pool->num < pool->min) { | ||
| 169 | /* reset msg front_len; user may have changed it */ | ||
| 170 | msg->front.iov_len = pool->front_len; | ||
| 171 | msg->hdr.front_len = cpu_to_le32(pool->front_len); | ||
| 172 | |||
| 173 | kref_set(&msg->kref, 1); /* retake a single ref */ | ||
| 174 | list_add(&msg->list_head, &pool->msgs); | ||
| 175 | pool->num++; | ||
| 176 | dout("msgpool_put %p reclaim %p, now %d/%d\n", pool, msg, | ||
| 177 | pool->num, pool->min); | ||
| 178 | spin_unlock(&pool->lock); | ||
| 179 | wake_up(&pool->wait); | ||
| 180 | } else { | ||
| 181 | dout("msgpool_put %p drop %p, at %d/%d\n", pool, msg, | ||
| 182 | pool->num, pool->min); | ||
| 183 | spin_unlock(&pool->lock); | ||
| 184 | ceph_msg_kfree(msg); | ||
| 185 | } | ||
| 186 | } | ||
diff --git a/fs/ceph/msgpool.h b/fs/ceph/msgpool.h new file mode 100644 index 000000000000..bc834bfcd720 --- /dev/null +++ b/fs/ceph/msgpool.h | |||
| @@ -0,0 +1,27 @@ | |||
| 1 | #ifndef _FS_CEPH_MSGPOOL | ||
| 2 | #define _FS_CEPH_MSGPOOL | ||
| 3 | |||
| 4 | #include "messenger.h" | ||
| 5 | |||
| 6 | /* | ||
| 7 | * we use memory pools for preallocating messages we may receive, to | ||
| 8 | * avoid unexpected OOM conditions. | ||
| 9 | */ | ||
| 10 | struct ceph_msgpool { | ||
| 11 | spinlock_t lock; | ||
| 12 | int front_len; /* preallocated payload size */ | ||
| 13 | struct list_head msgs; /* msgs in the pool; each has 1 ref */ | ||
| 14 | int num, min; /* cur, min # msgs in the pool */ | ||
| 15 | bool blocking; | ||
| 16 | wait_queue_head_t wait; | ||
| 17 | }; | ||
| 18 | |||
| 19 | extern int ceph_msgpool_init(struct ceph_msgpool *pool, | ||
| 20 | int front_len, int size, bool blocking); | ||
| 21 | extern void ceph_msgpool_destroy(struct ceph_msgpool *pool); | ||
| 22 | extern int ceph_msgpool_resv(struct ceph_msgpool *, int delta); | ||
| 23 | extern struct ceph_msg *ceph_msgpool_get(struct ceph_msgpool *, | ||
| 24 | int front_len); | ||
| 25 | extern void ceph_msgpool_put(struct ceph_msgpool *, struct ceph_msg *); | ||
| 26 | |||
| 27 | #endif | ||
diff --git a/fs/ceph/msgr.h b/fs/ceph/msgr.h new file mode 100644 index 000000000000..8aaab414f3f8 --- /dev/null +++ b/fs/ceph/msgr.h | |||
| @@ -0,0 +1,158 @@ | |||
| 1 | #ifndef __MSGR_H | ||
| 2 | #define __MSGR_H | ||
| 3 | |||
| 4 | /* | ||
| 5 | * Data types for message passing layer used by Ceph. | ||
| 6 | */ | ||
| 7 | |||
| 8 | #define CEPH_MON_PORT 6789 /* default monitor port */ | ||
| 9 | |||
| 10 | /* | ||
| 11 | * client-side processes will try to bind to ports in this | ||
| 12 | * range, simply for the benefit of tools like nmap or wireshark | ||
| 13 | * that would like to identify the protocol. | ||
| 14 | */ | ||
| 15 | #define CEPH_PORT_FIRST 6789 | ||
| 16 | #define CEPH_PORT_START 6800 /* non-monitors start here */ | ||
| 17 | #define CEPH_PORT_LAST 6900 | ||
| 18 | |||
| 19 | /* | ||
| 20 | * tcp connection banner. include a protocol version. and adjust | ||
| 21 | * whenever the wire protocol changes. try to keep this string length | ||
| 22 | * constant. | ||
| 23 | */ | ||
| 24 | #define CEPH_BANNER "ceph v027" | ||
| 25 | #define CEPH_BANNER_MAX_LEN 30 | ||
| 26 | |||
| 27 | |||
| 28 | /* | ||
| 29 | * Rollover-safe type and comparator for 32-bit sequence numbers. | ||
| 30 | * Comparator returns -1, 0, or 1. | ||
| 31 | */ | ||
| 32 | typedef __u32 ceph_seq_t; | ||
| 33 | |||
| 34 | static inline __s32 ceph_seq_cmp(__u32 a, __u32 b) | ||
| 35 | { | ||
| 36 | return (__s32)a - (__s32)b; | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | /* | ||
| 41 | * entity_name -- logical name for a process participating in the | ||
| 42 | * network, e.g. 'mds0' or 'osd3'. | ||
| 43 | */ | ||
| 44 | struct ceph_entity_name { | ||
| 45 | __u8 type; /* CEPH_ENTITY_TYPE_* */ | ||
| 46 | __le64 num; | ||
| 47 | } __attribute__ ((packed)); | ||
| 48 | |||
| 49 | #define CEPH_ENTITY_TYPE_MON 0x01 | ||
| 50 | #define CEPH_ENTITY_TYPE_MDS 0x02 | ||
| 51 | #define CEPH_ENTITY_TYPE_OSD 0x04 | ||
| 52 | #define CEPH_ENTITY_TYPE_CLIENT 0x08 | ||
| 53 | #define CEPH_ENTITY_TYPE_ADMIN 0x10 | ||
| 54 | #define CEPH_ENTITY_TYPE_AUTH 0x20 | ||
| 55 | |||
| 56 | #define CEPH_ENTITY_TYPE_ANY 0xFF | ||
| 57 | |||
| 58 | extern const char *ceph_entity_type_name(int type); | ||
| 59 | |||
| 60 | /* | ||
| 61 | * entity_addr -- network address | ||
| 62 | */ | ||
| 63 | struct ceph_entity_addr { | ||
| 64 | __le32 type; | ||
| 65 | __le32 nonce; /* unique id for process (e.g. pid) */ | ||
| 66 | struct sockaddr_storage in_addr; | ||
| 67 | } __attribute__ ((packed)); | ||
| 68 | |||
| 69 | struct ceph_entity_inst { | ||
| 70 | struct ceph_entity_name name; | ||
| 71 | struct ceph_entity_addr addr; | ||
| 72 | } __attribute__ ((packed)); | ||
| 73 | |||
| 74 | |||
| 75 | /* used by message exchange protocol */ | ||
| 76 | #define CEPH_MSGR_TAG_READY 1 /* server->client: ready for messages */ | ||
| 77 | #define CEPH_MSGR_TAG_RESETSESSION 2 /* server->client: reset, try again */ | ||
| 78 | #define CEPH_MSGR_TAG_WAIT 3 /* server->client: wait for racing | ||
| 79 | incoming connection */ | ||
| 80 | #define CEPH_MSGR_TAG_RETRY_SESSION 4 /* server->client + cseq: try again | ||
| 81 | with higher cseq */ | ||
| 82 | #define CEPH_MSGR_TAG_RETRY_GLOBAL 5 /* server->client + gseq: try again | ||
| 83 | with higher gseq */ | ||
| 84 | #define CEPH_MSGR_TAG_CLOSE 6 /* closing pipe */ | ||
| 85 | #define CEPH_MSGR_TAG_MSG 7 /* message */ | ||
| 86 | #define CEPH_MSGR_TAG_ACK 8 /* message ack */ | ||
| 87 | #define CEPH_MSGR_TAG_KEEPALIVE 9 /* just a keepalive byte! */ | ||
| 88 | #define CEPH_MSGR_TAG_BADPROTOVER 10 /* bad protocol version */ | ||
| 89 | #define CEPH_MSGR_TAG_BADAUTHORIZER 11 /* bad authorizer */ | ||
| 90 | #define CEPH_MSGR_TAG_FEATURES 12 /* insufficient features */ | ||
| 91 | |||
| 92 | |||
| 93 | /* | ||
| 94 | * connection negotiation | ||
| 95 | */ | ||
| 96 | struct ceph_msg_connect { | ||
| 97 | __le64 features; /* supported feature bits */ | ||
| 98 | __le32 host_type; /* CEPH_ENTITY_TYPE_* */ | ||
| 99 | __le32 global_seq; /* count connections initiated by this host */ | ||
| 100 | __le32 connect_seq; /* count connections initiated in this session */ | ||
| 101 | __le32 protocol_version; | ||
| 102 | __le32 authorizer_protocol; | ||
| 103 | __le32 authorizer_len; | ||
| 104 | __u8 flags; /* CEPH_MSG_CONNECT_* */ | ||
| 105 | } __attribute__ ((packed)); | ||
| 106 | |||
| 107 | struct ceph_msg_connect_reply { | ||
| 108 | __u8 tag; | ||
| 109 | __le64 features; /* feature bits for this session */ | ||
| 110 | __le32 global_seq; | ||
| 111 | __le32 connect_seq; | ||
| 112 | __le32 protocol_version; | ||
| 113 | __le32 authorizer_len; | ||
| 114 | __u8 flags; | ||
| 115 | } __attribute__ ((packed)); | ||
| 116 | |||
| 117 | #define CEPH_MSG_CONNECT_LOSSY 1 /* messages i send may be safely dropped */ | ||
| 118 | |||
| 119 | |||
| 120 | /* | ||
| 121 | * message header | ||
| 122 | */ | ||
| 123 | struct ceph_msg_header { | ||
| 124 | __le64 seq; /* message seq# for this session */ | ||
| 125 | __le64 tid; /* transaction id */ | ||
| 126 | __le16 type; /* message type */ | ||
| 127 | __le16 priority; /* priority. higher value == higher priority */ | ||
| 128 | __le16 version; /* version of message encoding */ | ||
| 129 | |||
| 130 | __le32 front_len; /* bytes in main payload */ | ||
| 131 | __le32 middle_len;/* bytes in middle payload */ | ||
| 132 | __le32 data_len; /* bytes of data payload */ | ||
| 133 | __le16 data_off; /* sender: include full offset; | ||
| 134 | receiver: mask against ~PAGE_MASK */ | ||
| 135 | |||
| 136 | struct ceph_entity_inst src, orig_src; | ||
| 137 | __le32 reserved; | ||
| 138 | __le32 crc; /* header crc32c */ | ||
| 139 | } __attribute__ ((packed)); | ||
| 140 | |||
| 141 | #define CEPH_MSG_PRIO_LOW 64 | ||
| 142 | #define CEPH_MSG_PRIO_DEFAULT 127 | ||
| 143 | #define CEPH_MSG_PRIO_HIGH 196 | ||
| 144 | #define CEPH_MSG_PRIO_HIGHEST 255 | ||
| 145 | |||
| 146 | /* | ||
| 147 | * follows data payload | ||
| 148 | */ | ||
| 149 | struct ceph_msg_footer { | ||
| 150 | __le32 front_crc, middle_crc, data_crc; | ||
| 151 | __u8 flags; | ||
| 152 | } __attribute__ ((packed)); | ||
| 153 | |||
| 154 | #define CEPH_MSG_FOOTER_COMPLETE (1<<0) /* msg wasn't aborted */ | ||
| 155 | #define CEPH_MSG_FOOTER_NOCRC (1<<1) /* no data crc */ | ||
| 156 | |||
| 157 | |||
| 158 | #endif | ||
diff --git a/fs/ceph/osd_client.c b/fs/ceph/osd_client.c new file mode 100644 index 000000000000..c7b4dedaace6 --- /dev/null +++ b/fs/ceph/osd_client.c | |||
| @@ -0,0 +1,1550 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/err.h> | ||
| 4 | #include <linux/highmem.h> | ||
| 5 | #include <linux/mm.h> | ||
| 6 | #include <linux/pagemap.h> | ||
| 7 | #include <linux/slab.h> | ||
| 8 | #include <linux/uaccess.h> | ||
| 9 | |||
| 10 | #include "super.h" | ||
| 11 | #include "osd_client.h" | ||
| 12 | #include "messenger.h" | ||
| 13 | #include "decode.h" | ||
| 14 | #include "auth.h" | ||
| 15 | |||
| 16 | #define OSD_OP_FRONT_LEN 4096 | ||
| 17 | #define OSD_OPREPLY_FRONT_LEN 512 | ||
| 18 | |||
| 19 | const static struct ceph_connection_operations osd_con_ops; | ||
| 20 | static int __kick_requests(struct ceph_osd_client *osdc, | ||
| 21 | struct ceph_osd *kickosd); | ||
| 22 | |||
| 23 | static void kick_requests(struct ceph_osd_client *osdc, struct ceph_osd *osd); | ||
| 24 | |||
| 25 | /* | ||
| 26 | * Implement client access to distributed object storage cluster. | ||
| 27 | * | ||
| 28 | * All data objects are stored within a cluster/cloud of OSDs, or | ||
| 29 | * "object storage devices." (Note that Ceph OSDs have _nothing_ to | ||
| 30 | * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply | ||
| 31 | * remote daemons serving up and coordinating consistent and safe | ||
| 32 | * access to storage. | ||
| 33 | * | ||
| 34 | * Cluster membership and the mapping of data objects onto storage devices | ||
| 35 | * are described by the osd map. | ||
| 36 | * | ||
| 37 | * We keep track of pending OSD requests (read, write), resubmit | ||
| 38 | * requests to different OSDs when the cluster topology/data layout | ||
| 39 | * change, or retry the affected requests when the communications | ||
| 40 | * channel with an OSD is reset. | ||
| 41 | */ | ||
| 42 | |||
| 43 | /* | ||
| 44 | * calculate the mapping of a file extent onto an object, and fill out the | ||
| 45 | * request accordingly. shorten extent as necessary if it crosses an | ||
| 46 | * object boundary. | ||
| 47 | * | ||
| 48 | * fill osd op in request message. | ||
| 49 | */ | ||
| 50 | static void calc_layout(struct ceph_osd_client *osdc, | ||
| 51 | struct ceph_vino vino, struct ceph_file_layout *layout, | ||
| 52 | u64 off, u64 *plen, | ||
| 53 | struct ceph_osd_request *req) | ||
| 54 | { | ||
| 55 | struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base; | ||
| 56 | struct ceph_osd_op *op = (void *)(reqhead + 1); | ||
| 57 | u64 orig_len = *plen; | ||
| 58 | u64 objoff, objlen; /* extent in object */ | ||
| 59 | u64 bno; | ||
| 60 | |||
| 61 | reqhead->snapid = cpu_to_le64(vino.snap); | ||
| 62 | |||
| 63 | /* object extent? */ | ||
| 64 | ceph_calc_file_object_mapping(layout, off, plen, &bno, | ||
| 65 | &objoff, &objlen); | ||
| 66 | if (*plen < orig_len) | ||
| 67 | dout(" skipping last %llu, final file extent %llu~%llu\n", | ||
| 68 | orig_len - *plen, off, *plen); | ||
| 69 | |||
| 70 | sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno); | ||
| 71 | req->r_oid_len = strlen(req->r_oid); | ||
| 72 | |||
| 73 | op->extent.offset = cpu_to_le64(objoff); | ||
| 74 | op->extent.length = cpu_to_le64(objlen); | ||
| 75 | req->r_num_pages = calc_pages_for(off, *plen); | ||
| 76 | |||
| 77 | dout("calc_layout %s (%d) %llu~%llu (%d pages)\n", | ||
| 78 | req->r_oid, req->r_oid_len, objoff, objlen, req->r_num_pages); | ||
| 79 | } | ||
| 80 | |||
| 81 | /* | ||
| 82 | * requests | ||
| 83 | */ | ||
| 84 | void ceph_osdc_release_request(struct kref *kref) | ||
| 85 | { | ||
| 86 | struct ceph_osd_request *req = container_of(kref, | ||
| 87 | struct ceph_osd_request, | ||
| 88 | r_kref); | ||
| 89 | |||
| 90 | if (req->r_request) | ||
| 91 | ceph_msg_put(req->r_request); | ||
| 92 | if (req->r_reply) | ||
| 93 | ceph_msg_put(req->r_reply); | ||
| 94 | if (req->r_con_filling_msg) { | ||
| 95 | dout("release_request revoking pages %p from con %p\n", | ||
| 96 | req->r_pages, req->r_con_filling_msg); | ||
| 97 | ceph_con_revoke_message(req->r_con_filling_msg, | ||
| 98 | req->r_reply); | ||
| 99 | ceph_con_put(req->r_con_filling_msg); | ||
| 100 | } | ||
| 101 | if (req->r_own_pages) | ||
| 102 | ceph_release_page_vector(req->r_pages, | ||
| 103 | req->r_num_pages); | ||
| 104 | ceph_put_snap_context(req->r_snapc); | ||
| 105 | if (req->r_mempool) | ||
| 106 | mempool_free(req, req->r_osdc->req_mempool); | ||
| 107 | else | ||
| 108 | kfree(req); | ||
| 109 | } | ||
| 110 | |||
| 111 | /* | ||
| 112 | * build new request AND message, calculate layout, and adjust file | ||
| 113 | * extent as needed. | ||
| 114 | * | ||
| 115 | * if the file was recently truncated, we include information about its | ||
| 116 | * old and new size so that the object can be updated appropriately. (we | ||
| 117 | * avoid synchronously deleting truncated objects because it's slow.) | ||
| 118 | * | ||
| 119 | * if @do_sync, include a 'startsync' command so that the osd will flush | ||
| 120 | * data quickly. | ||
| 121 | */ | ||
| 122 | struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc, | ||
| 123 | struct ceph_file_layout *layout, | ||
| 124 | struct ceph_vino vino, | ||
| 125 | u64 off, u64 *plen, | ||
| 126 | int opcode, int flags, | ||
| 127 | struct ceph_snap_context *snapc, | ||
| 128 | int do_sync, | ||
| 129 | u32 truncate_seq, | ||
| 130 | u64 truncate_size, | ||
| 131 | struct timespec *mtime, | ||
| 132 | bool use_mempool, int num_reply) | ||
| 133 | { | ||
| 134 | struct ceph_osd_request *req; | ||
| 135 | struct ceph_msg *msg; | ||
| 136 | struct ceph_osd_request_head *head; | ||
| 137 | struct ceph_osd_op *op; | ||
| 138 | void *p; | ||
| 139 | int num_op = 1 + do_sync; | ||
| 140 | size_t msg_size = sizeof(*head) + num_op*sizeof(*op); | ||
| 141 | int i; | ||
| 142 | |||
| 143 | if (use_mempool) { | ||
| 144 | req = mempool_alloc(osdc->req_mempool, GFP_NOFS); | ||
| 145 | memset(req, 0, sizeof(*req)); | ||
| 146 | } else { | ||
| 147 | req = kzalloc(sizeof(*req), GFP_NOFS); | ||
| 148 | } | ||
| 149 | if (req == NULL) | ||
| 150 | return ERR_PTR(-ENOMEM); | ||
| 151 | |||
| 152 | req->r_osdc = osdc; | ||
| 153 | req->r_mempool = use_mempool; | ||
| 154 | kref_init(&req->r_kref); | ||
| 155 | init_completion(&req->r_completion); | ||
| 156 | init_completion(&req->r_safe_completion); | ||
| 157 | INIT_LIST_HEAD(&req->r_unsafe_item); | ||
| 158 | req->r_flags = flags; | ||
| 159 | |||
| 160 | WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0); | ||
| 161 | |||
| 162 | /* create reply message */ | ||
| 163 | if (use_mempool) | ||
| 164 | msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0); | ||
| 165 | else | ||
| 166 | msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, | ||
| 167 | OSD_OPREPLY_FRONT_LEN, 0, 0, NULL); | ||
| 168 | if (IS_ERR(msg)) { | ||
| 169 | ceph_osdc_put_request(req); | ||
| 170 | return ERR_PTR(PTR_ERR(msg)); | ||
| 171 | } | ||
| 172 | req->r_reply = msg; | ||
| 173 | |||
| 174 | /* create request message; allow space for oid */ | ||
| 175 | msg_size += 40; | ||
| 176 | if (snapc) | ||
| 177 | msg_size += sizeof(u64) * snapc->num_snaps; | ||
| 178 | if (use_mempool) | ||
| 179 | msg = ceph_msgpool_get(&osdc->msgpool_op, 0); | ||
| 180 | else | ||
| 181 | msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, 0, 0, NULL); | ||
| 182 | if (IS_ERR(msg)) { | ||
| 183 | ceph_osdc_put_request(req); | ||
| 184 | return ERR_PTR(PTR_ERR(msg)); | ||
| 185 | } | ||
| 186 | msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP); | ||
| 187 | memset(msg->front.iov_base, 0, msg->front.iov_len); | ||
| 188 | head = msg->front.iov_base; | ||
| 189 | op = (void *)(head + 1); | ||
| 190 | p = (void *)(op + num_op); | ||
| 191 | |||
| 192 | req->r_request = msg; | ||
| 193 | req->r_snapc = ceph_get_snap_context(snapc); | ||
| 194 | |||
| 195 | head->client_inc = cpu_to_le32(1); /* always, for now. */ | ||
| 196 | head->flags = cpu_to_le32(flags); | ||
| 197 | if (flags & CEPH_OSD_FLAG_WRITE) | ||
| 198 | ceph_encode_timespec(&head->mtime, mtime); | ||
| 199 | head->num_ops = cpu_to_le16(num_op); | ||
| 200 | op->op = cpu_to_le16(opcode); | ||
| 201 | |||
| 202 | /* calculate max write size */ | ||
| 203 | calc_layout(osdc, vino, layout, off, plen, req); | ||
| 204 | req->r_file_layout = *layout; /* keep a copy */ | ||
| 205 | |||
| 206 | if (flags & CEPH_OSD_FLAG_WRITE) { | ||
| 207 | req->r_request->hdr.data_off = cpu_to_le16(off); | ||
| 208 | req->r_request->hdr.data_len = cpu_to_le32(*plen); | ||
| 209 | op->payload_len = cpu_to_le32(*plen); | ||
| 210 | } | ||
| 211 | op->extent.truncate_size = cpu_to_le64(truncate_size); | ||
| 212 | op->extent.truncate_seq = cpu_to_le32(truncate_seq); | ||
| 213 | |||
| 214 | /* fill in oid */ | ||
| 215 | head->object_len = cpu_to_le32(req->r_oid_len); | ||
| 216 | memcpy(p, req->r_oid, req->r_oid_len); | ||
| 217 | p += req->r_oid_len; | ||
| 218 | |||
| 219 | if (do_sync) { | ||
| 220 | op++; | ||
| 221 | op->op = cpu_to_le16(CEPH_OSD_OP_STARTSYNC); | ||
| 222 | } | ||
| 223 | if (snapc) { | ||
| 224 | head->snap_seq = cpu_to_le64(snapc->seq); | ||
| 225 | head->num_snaps = cpu_to_le32(snapc->num_snaps); | ||
| 226 | for (i = 0; i < snapc->num_snaps; i++) { | ||
| 227 | put_unaligned_le64(snapc->snaps[i], p); | ||
| 228 | p += sizeof(u64); | ||
| 229 | } | ||
| 230 | } | ||
| 231 | |||
| 232 | BUG_ON(p > msg->front.iov_base + msg->front.iov_len); | ||
| 233 | msg_size = p - msg->front.iov_base; | ||
| 234 | msg->front.iov_len = msg_size; | ||
| 235 | msg->hdr.front_len = cpu_to_le32(msg_size); | ||
| 236 | return req; | ||
| 237 | } | ||
| 238 | |||
| 239 | /* | ||
| 240 | * We keep osd requests in an rbtree, sorted by ->r_tid. | ||
| 241 | */ | ||
| 242 | static void __insert_request(struct ceph_osd_client *osdc, | ||
| 243 | struct ceph_osd_request *new) | ||
| 244 | { | ||
| 245 | struct rb_node **p = &osdc->requests.rb_node; | ||
| 246 | struct rb_node *parent = NULL; | ||
| 247 | struct ceph_osd_request *req = NULL; | ||
| 248 | |||
| 249 | while (*p) { | ||
| 250 | parent = *p; | ||
| 251 | req = rb_entry(parent, struct ceph_osd_request, r_node); | ||
| 252 | if (new->r_tid < req->r_tid) | ||
| 253 | p = &(*p)->rb_left; | ||
| 254 | else if (new->r_tid > req->r_tid) | ||
| 255 | p = &(*p)->rb_right; | ||
| 256 | else | ||
| 257 | BUG(); | ||
| 258 | } | ||
| 259 | |||
| 260 | rb_link_node(&new->r_node, parent, p); | ||
| 261 | rb_insert_color(&new->r_node, &osdc->requests); | ||
| 262 | } | ||
| 263 | |||
| 264 | static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc, | ||
| 265 | u64 tid) | ||
| 266 | { | ||
| 267 | struct ceph_osd_request *req; | ||
| 268 | struct rb_node *n = osdc->requests.rb_node; | ||
| 269 | |||
| 270 | while (n) { | ||
| 271 | req = rb_entry(n, struct ceph_osd_request, r_node); | ||
| 272 | if (tid < req->r_tid) | ||
| 273 | n = n->rb_left; | ||
| 274 | else if (tid > req->r_tid) | ||
| 275 | n = n->rb_right; | ||
| 276 | else | ||
| 277 | return req; | ||
| 278 | } | ||
| 279 | return NULL; | ||
| 280 | } | ||
| 281 | |||
| 282 | static struct ceph_osd_request * | ||
| 283 | __lookup_request_ge(struct ceph_osd_client *osdc, | ||
| 284 | u64 tid) | ||
| 285 | { | ||
| 286 | struct ceph_osd_request *req; | ||
| 287 | struct rb_node *n = osdc->requests.rb_node; | ||
| 288 | |||
| 289 | while (n) { | ||
| 290 | req = rb_entry(n, struct ceph_osd_request, r_node); | ||
| 291 | if (tid < req->r_tid) { | ||
| 292 | if (!n->rb_left) | ||
| 293 | return req; | ||
| 294 | n = n->rb_left; | ||
| 295 | } else if (tid > req->r_tid) { | ||
| 296 | n = n->rb_right; | ||
| 297 | } else { | ||
| 298 | return req; | ||
| 299 | } | ||
| 300 | } | ||
| 301 | return NULL; | ||
| 302 | } | ||
| 303 | |||
| 304 | |||
| 305 | /* | ||
| 306 | * If the osd connection drops, we need to resubmit all requests. | ||
| 307 | */ | ||
| 308 | static void osd_reset(struct ceph_connection *con) | ||
| 309 | { | ||
| 310 | struct ceph_osd *osd = con->private; | ||
| 311 | struct ceph_osd_client *osdc; | ||
| 312 | |||
| 313 | if (!osd) | ||
| 314 | return; | ||
| 315 | dout("osd_reset osd%d\n", osd->o_osd); | ||
| 316 | osdc = osd->o_osdc; | ||
| 317 | down_read(&osdc->map_sem); | ||
| 318 | kick_requests(osdc, osd); | ||
| 319 | up_read(&osdc->map_sem); | ||
| 320 | } | ||
| 321 | |||
| 322 | /* | ||
| 323 | * Track open sessions with osds. | ||
| 324 | */ | ||
| 325 | static struct ceph_osd *create_osd(struct ceph_osd_client *osdc) | ||
| 326 | { | ||
| 327 | struct ceph_osd *osd; | ||
| 328 | |||
| 329 | osd = kzalloc(sizeof(*osd), GFP_NOFS); | ||
| 330 | if (!osd) | ||
| 331 | return NULL; | ||
| 332 | |||
| 333 | atomic_set(&osd->o_ref, 1); | ||
| 334 | osd->o_osdc = osdc; | ||
| 335 | INIT_LIST_HEAD(&osd->o_requests); | ||
| 336 | INIT_LIST_HEAD(&osd->o_osd_lru); | ||
| 337 | osd->o_incarnation = 1; | ||
| 338 | |||
| 339 | ceph_con_init(osdc->client->msgr, &osd->o_con); | ||
| 340 | osd->o_con.private = osd; | ||
| 341 | osd->o_con.ops = &osd_con_ops; | ||
| 342 | osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD; | ||
| 343 | |||
| 344 | INIT_LIST_HEAD(&osd->o_keepalive_item); | ||
| 345 | return osd; | ||
| 346 | } | ||
| 347 | |||
| 348 | static struct ceph_osd *get_osd(struct ceph_osd *osd) | ||
| 349 | { | ||
| 350 | if (atomic_inc_not_zero(&osd->o_ref)) { | ||
| 351 | dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1, | ||
| 352 | atomic_read(&osd->o_ref)); | ||
| 353 | return osd; | ||
| 354 | } else { | ||
| 355 | dout("get_osd %p FAIL\n", osd); | ||
| 356 | return NULL; | ||
| 357 | } | ||
| 358 | } | ||
| 359 | |||
| 360 | static void put_osd(struct ceph_osd *osd) | ||
| 361 | { | ||
| 362 | dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref), | ||
| 363 | atomic_read(&osd->o_ref) - 1); | ||
| 364 | if (atomic_dec_and_test(&osd->o_ref)) | ||
| 365 | kfree(osd); | ||
| 366 | } | ||
| 367 | |||
| 368 | /* | ||
| 369 | * remove an osd from our map | ||
| 370 | */ | ||
| 371 | static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) | ||
| 372 | { | ||
| 373 | dout("__remove_osd %p\n", osd); | ||
| 374 | BUG_ON(!list_empty(&osd->o_requests)); | ||
| 375 | rb_erase(&osd->o_node, &osdc->osds); | ||
| 376 | list_del_init(&osd->o_osd_lru); | ||
| 377 | ceph_con_close(&osd->o_con); | ||
| 378 | put_osd(osd); | ||
| 379 | } | ||
| 380 | |||
| 381 | static void __move_osd_to_lru(struct ceph_osd_client *osdc, | ||
| 382 | struct ceph_osd *osd) | ||
| 383 | { | ||
| 384 | dout("__move_osd_to_lru %p\n", osd); | ||
| 385 | BUG_ON(!list_empty(&osd->o_osd_lru)); | ||
| 386 | list_add_tail(&osd->o_osd_lru, &osdc->osd_lru); | ||
| 387 | osd->lru_ttl = jiffies + osdc->client->mount_args->osd_idle_ttl * HZ; | ||
| 388 | } | ||
| 389 | |||
| 390 | static void __remove_osd_from_lru(struct ceph_osd *osd) | ||
| 391 | { | ||
| 392 | dout("__remove_osd_from_lru %p\n", osd); | ||
| 393 | if (!list_empty(&osd->o_osd_lru)) | ||
| 394 | list_del_init(&osd->o_osd_lru); | ||
| 395 | } | ||
| 396 | |||
| 397 | static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all) | ||
| 398 | { | ||
| 399 | struct ceph_osd *osd, *nosd; | ||
| 400 | |||
| 401 | dout("__remove_old_osds %p\n", osdc); | ||
| 402 | mutex_lock(&osdc->request_mutex); | ||
| 403 | list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) { | ||
| 404 | if (!remove_all && time_before(jiffies, osd->lru_ttl)) | ||
| 405 | break; | ||
| 406 | __remove_osd(osdc, osd); | ||
| 407 | } | ||
| 408 | mutex_unlock(&osdc->request_mutex); | ||
| 409 | } | ||
| 410 | |||
| 411 | /* | ||
| 412 | * reset osd connect | ||
| 413 | */ | ||
| 414 | static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd) | ||
| 415 | { | ||
| 416 | struct ceph_osd_request *req; | ||
| 417 | int ret = 0; | ||
| 418 | |||
| 419 | dout("__reset_osd %p osd%d\n", osd, osd->o_osd); | ||
| 420 | if (list_empty(&osd->o_requests)) { | ||
| 421 | __remove_osd(osdc, osd); | ||
| 422 | } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd], | ||
| 423 | &osd->o_con.peer_addr, | ||
| 424 | sizeof(osd->o_con.peer_addr)) == 0 && | ||
| 425 | !ceph_con_opened(&osd->o_con)) { | ||
| 426 | dout(" osd addr hasn't changed and connection never opened," | ||
| 427 | " letting msgr retry"); | ||
| 428 | /* touch each r_stamp for handle_timeout()'s benfit */ | ||
| 429 | list_for_each_entry(req, &osd->o_requests, r_osd_item) | ||
| 430 | req->r_stamp = jiffies; | ||
| 431 | ret = -EAGAIN; | ||
| 432 | } else { | ||
| 433 | ceph_con_close(&osd->o_con); | ||
| 434 | ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]); | ||
| 435 | osd->o_incarnation++; | ||
| 436 | } | ||
| 437 | return ret; | ||
| 438 | } | ||
| 439 | |||
| 440 | static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new) | ||
| 441 | { | ||
| 442 | struct rb_node **p = &osdc->osds.rb_node; | ||
| 443 | struct rb_node *parent = NULL; | ||
| 444 | struct ceph_osd *osd = NULL; | ||
| 445 | |||
| 446 | while (*p) { | ||
| 447 | parent = *p; | ||
| 448 | osd = rb_entry(parent, struct ceph_osd, o_node); | ||
| 449 | if (new->o_osd < osd->o_osd) | ||
| 450 | p = &(*p)->rb_left; | ||
| 451 | else if (new->o_osd > osd->o_osd) | ||
| 452 | p = &(*p)->rb_right; | ||
| 453 | else | ||
| 454 | BUG(); | ||
| 455 | } | ||
| 456 | |||
| 457 | rb_link_node(&new->o_node, parent, p); | ||
| 458 | rb_insert_color(&new->o_node, &osdc->osds); | ||
| 459 | } | ||
| 460 | |||
| 461 | static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o) | ||
| 462 | { | ||
| 463 | struct ceph_osd *osd; | ||
| 464 | struct rb_node *n = osdc->osds.rb_node; | ||
| 465 | |||
| 466 | while (n) { | ||
| 467 | osd = rb_entry(n, struct ceph_osd, o_node); | ||
| 468 | if (o < osd->o_osd) | ||
| 469 | n = n->rb_left; | ||
| 470 | else if (o > osd->o_osd) | ||
| 471 | n = n->rb_right; | ||
| 472 | else | ||
| 473 | return osd; | ||
| 474 | } | ||
| 475 | return NULL; | ||
| 476 | } | ||
| 477 | |||
| 478 | static void __schedule_osd_timeout(struct ceph_osd_client *osdc) | ||
| 479 | { | ||
| 480 | schedule_delayed_work(&osdc->timeout_work, | ||
| 481 | osdc->client->mount_args->osd_keepalive_timeout * HZ); | ||
| 482 | } | ||
| 483 | |||
| 484 | static void __cancel_osd_timeout(struct ceph_osd_client *osdc) | ||
| 485 | { | ||
| 486 | cancel_delayed_work(&osdc->timeout_work); | ||
| 487 | } | ||
| 488 | |||
| 489 | /* | ||
| 490 | * Register request, assign tid. If this is the first request, set up | ||
| 491 | * the timeout event. | ||
| 492 | */ | ||
| 493 | static void register_request(struct ceph_osd_client *osdc, | ||
| 494 | struct ceph_osd_request *req) | ||
| 495 | { | ||
| 496 | mutex_lock(&osdc->request_mutex); | ||
| 497 | req->r_tid = ++osdc->last_tid; | ||
| 498 | req->r_request->hdr.tid = cpu_to_le64(req->r_tid); | ||
| 499 | INIT_LIST_HEAD(&req->r_req_lru_item); | ||
| 500 | |||
| 501 | dout("register_request %p tid %lld\n", req, req->r_tid); | ||
| 502 | __insert_request(osdc, req); | ||
| 503 | ceph_osdc_get_request(req); | ||
| 504 | osdc->num_requests++; | ||
| 505 | |||
| 506 | if (osdc->num_requests == 1) { | ||
| 507 | dout(" first request, scheduling timeout\n"); | ||
| 508 | __schedule_osd_timeout(osdc); | ||
| 509 | } | ||
| 510 | mutex_unlock(&osdc->request_mutex); | ||
| 511 | } | ||
| 512 | |||
| 513 | /* | ||
| 514 | * called under osdc->request_mutex | ||
| 515 | */ | ||
| 516 | static void __unregister_request(struct ceph_osd_client *osdc, | ||
| 517 | struct ceph_osd_request *req) | ||
| 518 | { | ||
| 519 | dout("__unregister_request %p tid %lld\n", req, req->r_tid); | ||
| 520 | rb_erase(&req->r_node, &osdc->requests); | ||
| 521 | osdc->num_requests--; | ||
| 522 | |||
| 523 | if (req->r_osd) { | ||
| 524 | /* make sure the original request isn't in flight. */ | ||
| 525 | ceph_con_revoke(&req->r_osd->o_con, req->r_request); | ||
| 526 | |||
| 527 | list_del_init(&req->r_osd_item); | ||
| 528 | if (list_empty(&req->r_osd->o_requests)) | ||
| 529 | __move_osd_to_lru(osdc, req->r_osd); | ||
| 530 | req->r_osd = NULL; | ||
| 531 | } | ||
| 532 | |||
| 533 | ceph_osdc_put_request(req); | ||
| 534 | |||
| 535 | list_del_init(&req->r_req_lru_item); | ||
| 536 | if (osdc->num_requests == 0) { | ||
| 537 | dout(" no requests, canceling timeout\n"); | ||
| 538 | __cancel_osd_timeout(osdc); | ||
| 539 | } | ||
| 540 | } | ||
| 541 | |||
| 542 | /* | ||
| 543 | * Cancel a previously queued request message | ||
| 544 | */ | ||
| 545 | static void __cancel_request(struct ceph_osd_request *req) | ||
| 546 | { | ||
| 547 | if (req->r_sent) { | ||
| 548 | ceph_con_revoke(&req->r_osd->o_con, req->r_request); | ||
| 549 | req->r_sent = 0; | ||
| 550 | } | ||
| 551 | list_del_init(&req->r_req_lru_item); | ||
| 552 | } | ||
| 553 | |||
| 554 | /* | ||
| 555 | * Pick an osd (the first 'up' osd in the pg), allocate the osd struct | ||
| 556 | * (as needed), and set the request r_osd appropriately. If there is | ||
| 557 | * no up osd, set r_osd to NULL. | ||
| 558 | * | ||
| 559 | * Return 0 if unchanged, 1 if changed, or negative on error. | ||
| 560 | * | ||
| 561 | * Caller should hold map_sem for read and request_mutex. | ||
| 562 | */ | ||
| 563 | static int __map_osds(struct ceph_osd_client *osdc, | ||
| 564 | struct ceph_osd_request *req) | ||
| 565 | { | ||
| 566 | struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base; | ||
| 567 | struct ceph_pg pgid; | ||
| 568 | int o = -1; | ||
| 569 | int err; | ||
| 570 | |||
| 571 | dout("map_osds %p tid %lld\n", req, req->r_tid); | ||
| 572 | err = ceph_calc_object_layout(&reqhead->layout, req->r_oid, | ||
| 573 | &req->r_file_layout, osdc->osdmap); | ||
| 574 | if (err) | ||
| 575 | return err; | ||
| 576 | pgid = reqhead->layout.ol_pgid; | ||
| 577 | req->r_pgid = pgid; | ||
| 578 | |||
| 579 | o = ceph_calc_pg_primary(osdc->osdmap, pgid); | ||
| 580 | |||
| 581 | if ((req->r_osd && req->r_osd->o_osd == o && | ||
| 582 | req->r_sent >= req->r_osd->o_incarnation) || | ||
| 583 | (req->r_osd == NULL && o == -1)) | ||
| 584 | return 0; /* no change */ | ||
| 585 | |||
| 586 | dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n", | ||
| 587 | req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o, | ||
| 588 | req->r_osd ? req->r_osd->o_osd : -1); | ||
| 589 | |||
| 590 | if (req->r_osd) { | ||
| 591 | __cancel_request(req); | ||
| 592 | list_del_init(&req->r_osd_item); | ||
| 593 | req->r_osd = NULL; | ||
| 594 | } | ||
| 595 | |||
| 596 | req->r_osd = __lookup_osd(osdc, o); | ||
| 597 | if (!req->r_osd && o >= 0) { | ||
| 598 | err = -ENOMEM; | ||
| 599 | req->r_osd = create_osd(osdc); | ||
| 600 | if (!req->r_osd) | ||
| 601 | goto out; | ||
| 602 | |||
| 603 | dout("map_osds osd %p is osd%d\n", req->r_osd, o); | ||
| 604 | req->r_osd->o_osd = o; | ||
| 605 | req->r_osd->o_con.peer_name.num = cpu_to_le64(o); | ||
| 606 | __insert_osd(osdc, req->r_osd); | ||
| 607 | |||
| 608 | ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]); | ||
| 609 | } | ||
| 610 | |||
| 611 | if (req->r_osd) { | ||
| 612 | __remove_osd_from_lru(req->r_osd); | ||
| 613 | list_add(&req->r_osd_item, &req->r_osd->o_requests); | ||
| 614 | } | ||
| 615 | err = 1; /* osd changed */ | ||
| 616 | |||
| 617 | out: | ||
| 618 | return err; | ||
| 619 | } | ||
| 620 | |||
| 621 | /* | ||
| 622 | * caller should hold map_sem (for read) and request_mutex | ||
| 623 | */ | ||
| 624 | static int __send_request(struct ceph_osd_client *osdc, | ||
| 625 | struct ceph_osd_request *req) | ||
| 626 | { | ||
| 627 | struct ceph_osd_request_head *reqhead; | ||
| 628 | int err; | ||
| 629 | |||
| 630 | err = __map_osds(osdc, req); | ||
| 631 | if (err < 0) | ||
| 632 | return err; | ||
| 633 | if (req->r_osd == NULL) { | ||
| 634 | dout("send_request %p no up osds in pg\n", req); | ||
| 635 | ceph_monc_request_next_osdmap(&osdc->client->monc); | ||
| 636 | return 0; | ||
| 637 | } | ||
| 638 | |||
| 639 | dout("send_request %p tid %llu to osd%d flags %d\n", | ||
| 640 | req, req->r_tid, req->r_osd->o_osd, req->r_flags); | ||
| 641 | |||
| 642 | reqhead = req->r_request->front.iov_base; | ||
| 643 | reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch); | ||
| 644 | reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */ | ||
| 645 | reqhead->reassert_version = req->r_reassert_version; | ||
| 646 | |||
| 647 | req->r_stamp = jiffies; | ||
| 648 | list_move_tail(&osdc->req_lru, &req->r_req_lru_item); | ||
| 649 | |||
| 650 | ceph_msg_get(req->r_request); /* send consumes a ref */ | ||
| 651 | ceph_con_send(&req->r_osd->o_con, req->r_request); | ||
| 652 | req->r_sent = req->r_osd->o_incarnation; | ||
| 653 | return 0; | ||
| 654 | } | ||
| 655 | |||
| 656 | /* | ||
| 657 | * Timeout callback, called every N seconds when 1 or more osd | ||
| 658 | * requests has been active for more than N seconds. When this | ||
| 659 | * happens, we ping all OSDs with requests who have timed out to | ||
| 660 | * ensure any communications channel reset is detected. Reset the | ||
| 661 | * request timeouts another N seconds in the future as we go. | ||
| 662 | * Reschedule the timeout event another N seconds in future (unless | ||
| 663 | * there are no open requests). | ||
| 664 | */ | ||
| 665 | static void handle_timeout(struct work_struct *work) | ||
| 666 | { | ||
| 667 | struct ceph_osd_client *osdc = | ||
| 668 | container_of(work, struct ceph_osd_client, timeout_work.work); | ||
| 669 | struct ceph_osd_request *req, *last_req = NULL; | ||
| 670 | struct ceph_osd *osd; | ||
| 671 | unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ; | ||
| 672 | unsigned long keepalive = | ||
| 673 | osdc->client->mount_args->osd_keepalive_timeout * HZ; | ||
| 674 | unsigned long last_stamp = 0; | ||
| 675 | struct rb_node *p; | ||
| 676 | struct list_head slow_osds; | ||
| 677 | |||
| 678 | dout("timeout\n"); | ||
| 679 | down_read(&osdc->map_sem); | ||
| 680 | |||
| 681 | ceph_monc_request_next_osdmap(&osdc->client->monc); | ||
| 682 | |||
| 683 | mutex_lock(&osdc->request_mutex); | ||
| 684 | for (p = rb_first(&osdc->requests); p; p = rb_next(p)) { | ||
| 685 | req = rb_entry(p, struct ceph_osd_request, r_node); | ||
| 686 | |||
| 687 | if (req->r_resend) { | ||
| 688 | int err; | ||
| 689 | |||
| 690 | dout("osdc resending prev failed %lld\n", req->r_tid); | ||
| 691 | err = __send_request(osdc, req); | ||
| 692 | if (err) | ||
| 693 | dout("osdc failed again on %lld\n", req->r_tid); | ||
| 694 | else | ||
| 695 | req->r_resend = false; | ||
| 696 | continue; | ||
| 697 | } | ||
| 698 | } | ||
| 699 | |||
| 700 | /* | ||
| 701 | * reset osds that appear to be _really_ unresponsive. this | ||
| 702 | * is a failsafe measure.. we really shouldn't be getting to | ||
| 703 | * this point if the system is working properly. the monitors | ||
| 704 | * should mark the osd as failed and we should find out about | ||
| 705 | * it from an updated osd map. | ||
| 706 | */ | ||
| 707 | while (!list_empty(&osdc->req_lru)) { | ||
| 708 | req = list_entry(osdc->req_lru.next, struct ceph_osd_request, | ||
| 709 | r_req_lru_item); | ||
| 710 | |||
| 711 | if (time_before(jiffies, req->r_stamp + timeout)) | ||
| 712 | break; | ||
| 713 | |||
| 714 | BUG_ON(req == last_req && req->r_stamp == last_stamp); | ||
| 715 | last_req = req; | ||
| 716 | last_stamp = req->r_stamp; | ||
| 717 | |||
| 718 | osd = req->r_osd; | ||
| 719 | BUG_ON(!osd); | ||
| 720 | pr_warning(" tid %llu timed out on osd%d, will reset osd\n", | ||
| 721 | req->r_tid, osd->o_osd); | ||
| 722 | __kick_requests(osdc, osd); | ||
| 723 | } | ||
| 724 | |||
| 725 | /* | ||
| 726 | * ping osds that are a bit slow. this ensures that if there | ||
| 727 | * is a break in the TCP connection we will notice, and reopen | ||
| 728 | * a connection with that osd (from the fault callback). | ||
| 729 | */ | ||
| 730 | INIT_LIST_HEAD(&slow_osds); | ||
| 731 | list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) { | ||
| 732 | if (time_before(jiffies, req->r_stamp + keepalive)) | ||
| 733 | break; | ||
| 734 | |||
| 735 | osd = req->r_osd; | ||
| 736 | BUG_ON(!osd); | ||
| 737 | dout(" tid %llu is slow, will send keepalive on osd%d\n", | ||
| 738 | req->r_tid, osd->o_osd); | ||
| 739 | list_move_tail(&osd->o_keepalive_item, &slow_osds); | ||
| 740 | } | ||
| 741 | while (!list_empty(&slow_osds)) { | ||
| 742 | osd = list_entry(slow_osds.next, struct ceph_osd, | ||
| 743 | o_keepalive_item); | ||
| 744 | list_del_init(&osd->o_keepalive_item); | ||
| 745 | ceph_con_keepalive(&osd->o_con); | ||
| 746 | } | ||
| 747 | |||
| 748 | __schedule_osd_timeout(osdc); | ||
| 749 | mutex_unlock(&osdc->request_mutex); | ||
| 750 | |||
| 751 | up_read(&osdc->map_sem); | ||
| 752 | } | ||
| 753 | |||
| 754 | static void handle_osds_timeout(struct work_struct *work) | ||
| 755 | { | ||
| 756 | struct ceph_osd_client *osdc = | ||
| 757 | container_of(work, struct ceph_osd_client, | ||
| 758 | osds_timeout_work.work); | ||
| 759 | unsigned long delay = | ||
| 760 | osdc->client->mount_args->osd_idle_ttl * HZ >> 2; | ||
| 761 | |||
| 762 | dout("osds timeout\n"); | ||
| 763 | down_read(&osdc->map_sem); | ||
| 764 | remove_old_osds(osdc, 0); | ||
| 765 | up_read(&osdc->map_sem); | ||
| 766 | |||
| 767 | schedule_delayed_work(&osdc->osds_timeout_work, | ||
| 768 | round_jiffies_relative(delay)); | ||
| 769 | } | ||
| 770 | |||
| 771 | /* | ||
| 772 | * handle osd op reply. either call the callback if it is specified, | ||
| 773 | * or do the completion to wake up the waiting thread. | ||
| 774 | */ | ||
| 775 | static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg, | ||
| 776 | struct ceph_connection *con) | ||
| 777 | { | ||
| 778 | struct ceph_osd_reply_head *rhead = msg->front.iov_base; | ||
| 779 | struct ceph_osd_request *req; | ||
| 780 | u64 tid; | ||
| 781 | int numops, object_len, flags; | ||
| 782 | |||
| 783 | tid = le64_to_cpu(msg->hdr.tid); | ||
| 784 | if (msg->front.iov_len < sizeof(*rhead)) | ||
| 785 | goto bad; | ||
| 786 | numops = le32_to_cpu(rhead->num_ops); | ||
| 787 | object_len = le32_to_cpu(rhead->object_len); | ||
| 788 | if (msg->front.iov_len != sizeof(*rhead) + object_len + | ||
| 789 | numops * sizeof(struct ceph_osd_op)) | ||
| 790 | goto bad; | ||
| 791 | dout("handle_reply %p tid %llu\n", msg, tid); | ||
| 792 | |||
| 793 | /* lookup */ | ||
| 794 | mutex_lock(&osdc->request_mutex); | ||
| 795 | req = __lookup_request(osdc, tid); | ||
| 796 | if (req == NULL) { | ||
| 797 | dout("handle_reply tid %llu dne\n", tid); | ||
| 798 | mutex_unlock(&osdc->request_mutex); | ||
| 799 | return; | ||
| 800 | } | ||
| 801 | ceph_osdc_get_request(req); | ||
| 802 | flags = le32_to_cpu(rhead->flags); | ||
| 803 | |||
| 804 | /* | ||
| 805 | * if this connection filled our message, drop our reference now, to | ||
| 806 | * avoid a (safe but slower) revoke later. | ||
| 807 | */ | ||
| 808 | if (req->r_con_filling_msg == con && req->r_reply == msg) { | ||
| 809 | dout(" dropping con_filling_msg ref %p\n", con); | ||
| 810 | req->r_con_filling_msg = NULL; | ||
| 811 | ceph_con_put(con); | ||
| 812 | } | ||
| 813 | |||
| 814 | if (!req->r_got_reply) { | ||
| 815 | unsigned bytes; | ||
| 816 | |||
| 817 | req->r_result = le32_to_cpu(rhead->result); | ||
| 818 | bytes = le32_to_cpu(msg->hdr.data_len); | ||
| 819 | dout("handle_reply result %d bytes %d\n", req->r_result, | ||
| 820 | bytes); | ||
| 821 | if (req->r_result == 0) | ||
| 822 | req->r_result = bytes; | ||
| 823 | |||
| 824 | /* in case this is a write and we need to replay, */ | ||
| 825 | req->r_reassert_version = rhead->reassert_version; | ||
| 826 | |||
| 827 | req->r_got_reply = 1; | ||
| 828 | } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) { | ||
| 829 | dout("handle_reply tid %llu dup ack\n", tid); | ||
| 830 | mutex_unlock(&osdc->request_mutex); | ||
| 831 | goto done; | ||
| 832 | } | ||
| 833 | |||
| 834 | dout("handle_reply tid %llu flags %d\n", tid, flags); | ||
| 835 | |||
| 836 | /* either this is a read, or we got the safe response */ | ||
| 837 | if ((flags & CEPH_OSD_FLAG_ONDISK) || | ||
| 838 | ((flags & CEPH_OSD_FLAG_WRITE) == 0)) | ||
| 839 | __unregister_request(osdc, req); | ||
| 840 | |||
| 841 | mutex_unlock(&osdc->request_mutex); | ||
| 842 | |||
| 843 | if (req->r_callback) | ||
| 844 | req->r_callback(req, msg); | ||
| 845 | else | ||
| 846 | complete(&req->r_completion); | ||
| 847 | |||
| 848 | if (flags & CEPH_OSD_FLAG_ONDISK) { | ||
| 849 | if (req->r_safe_callback) | ||
| 850 | req->r_safe_callback(req, msg); | ||
| 851 | complete(&req->r_safe_completion); /* fsync waiter */ | ||
| 852 | } | ||
| 853 | |||
| 854 | done: | ||
| 855 | ceph_osdc_put_request(req); | ||
| 856 | return; | ||
| 857 | |||
| 858 | bad: | ||
| 859 | pr_err("corrupt osd_op_reply got %d %d expected %d\n", | ||
| 860 | (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len), | ||
| 861 | (int)sizeof(*rhead)); | ||
| 862 | ceph_msg_dump(msg); | ||
| 863 | } | ||
| 864 | |||
| 865 | |||
| 866 | static int __kick_requests(struct ceph_osd_client *osdc, | ||
| 867 | struct ceph_osd *kickosd) | ||
| 868 | { | ||
| 869 | struct ceph_osd_request *req; | ||
| 870 | struct rb_node *p, *n; | ||
| 871 | int needmap = 0; | ||
| 872 | int err; | ||
| 873 | |||
| 874 | dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1); | ||
| 875 | if (kickosd) { | ||
| 876 | err = __reset_osd(osdc, kickosd); | ||
| 877 | if (err == -EAGAIN) | ||
| 878 | return 1; | ||
| 879 | } else { | ||
| 880 | for (p = rb_first(&osdc->osds); p; p = n) { | ||
| 881 | struct ceph_osd *osd = | ||
| 882 | rb_entry(p, struct ceph_osd, o_node); | ||
| 883 | |||
| 884 | n = rb_next(p); | ||
| 885 | if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) || | ||
| 886 | memcmp(&osd->o_con.peer_addr, | ||
| 887 | ceph_osd_addr(osdc->osdmap, | ||
| 888 | osd->o_osd), | ||
| 889 | sizeof(struct ceph_entity_addr)) != 0) | ||
| 890 | __reset_osd(osdc, osd); | ||
| 891 | } | ||
| 892 | } | ||
| 893 | |||
| 894 | for (p = rb_first(&osdc->requests); p; p = rb_next(p)) { | ||
| 895 | req = rb_entry(p, struct ceph_osd_request, r_node); | ||
| 896 | |||
| 897 | if (req->r_resend) { | ||
| 898 | dout(" r_resend set on tid %llu\n", req->r_tid); | ||
| 899 | __cancel_request(req); | ||
| 900 | goto kick; | ||
| 901 | } | ||
| 902 | if (req->r_osd && kickosd == req->r_osd) { | ||
| 903 | __cancel_request(req); | ||
| 904 | goto kick; | ||
| 905 | } | ||
| 906 | |||
| 907 | err = __map_osds(osdc, req); | ||
| 908 | if (err == 0) | ||
| 909 | continue; /* no change */ | ||
| 910 | if (err < 0) { | ||
| 911 | /* | ||
| 912 | * FIXME: really, we should set the request | ||
| 913 | * error and fail if this isn't a 'nofail' | ||
| 914 | * request, but that's a fair bit more | ||
| 915 | * complicated to do. So retry! | ||
| 916 | */ | ||
| 917 | dout(" setting r_resend on %llu\n", req->r_tid); | ||
| 918 | req->r_resend = true; | ||
| 919 | continue; | ||
| 920 | } | ||
| 921 | if (req->r_osd == NULL) { | ||
| 922 | dout("tid %llu maps to no valid osd\n", req->r_tid); | ||
| 923 | needmap++; /* request a newer map */ | ||
| 924 | continue; | ||
| 925 | } | ||
| 926 | |||
| 927 | kick: | ||
| 928 | dout("kicking %p tid %llu osd%d\n", req, req->r_tid, | ||
| 929 | req->r_osd ? req->r_osd->o_osd : -1); | ||
| 930 | req->r_flags |= CEPH_OSD_FLAG_RETRY; | ||
| 931 | err = __send_request(osdc, req); | ||
| 932 | if (err) { | ||
| 933 | dout(" setting r_resend on %llu\n", req->r_tid); | ||
| 934 | req->r_resend = true; | ||
| 935 | } | ||
| 936 | } | ||
| 937 | |||
| 938 | return needmap; | ||
| 939 | } | ||
| 940 | |||
| 941 | /* | ||
| 942 | * Resubmit osd requests whose osd or osd address has changed. Request | ||
| 943 | * a new osd map if osds are down, or we are otherwise unable to determine | ||
| 944 | * how to direct a request. | ||
| 945 | * | ||
| 946 | * Close connections to down osds. | ||
| 947 | * | ||
| 948 | * If @who is specified, resubmit requests for that specific osd. | ||
| 949 | * | ||
| 950 | * Caller should hold map_sem for read and request_mutex. | ||
| 951 | */ | ||
| 952 | static void kick_requests(struct ceph_osd_client *osdc, | ||
| 953 | struct ceph_osd *kickosd) | ||
| 954 | { | ||
| 955 | int needmap; | ||
| 956 | |||
| 957 | mutex_lock(&osdc->request_mutex); | ||
| 958 | needmap = __kick_requests(osdc, kickosd); | ||
| 959 | mutex_unlock(&osdc->request_mutex); | ||
| 960 | |||
| 961 | if (needmap) { | ||
| 962 | dout("%d requests for down osds, need new map\n", needmap); | ||
| 963 | ceph_monc_request_next_osdmap(&osdc->client->monc); | ||
| 964 | } | ||
| 965 | |||
| 966 | } | ||
| 967 | /* | ||
| 968 | * Process updated osd map. | ||
| 969 | * | ||
| 970 | * The message contains any number of incremental and full maps, normally | ||
| 971 | * indicating some sort of topology change in the cluster. Kick requests | ||
| 972 | * off to different OSDs as needed. | ||
| 973 | */ | ||
| 974 | void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg) | ||
| 975 | { | ||
| 976 | void *p, *end, *next; | ||
| 977 | u32 nr_maps, maplen; | ||
| 978 | u32 epoch; | ||
| 979 | struct ceph_osdmap *newmap = NULL, *oldmap; | ||
| 980 | int err; | ||
| 981 | struct ceph_fsid fsid; | ||
| 982 | |||
| 983 | dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0); | ||
| 984 | p = msg->front.iov_base; | ||
| 985 | end = p + msg->front.iov_len; | ||
| 986 | |||
| 987 | /* verify fsid */ | ||
| 988 | ceph_decode_need(&p, end, sizeof(fsid), bad); | ||
| 989 | ceph_decode_copy(&p, &fsid, sizeof(fsid)); | ||
| 990 | if (ceph_check_fsid(osdc->client, &fsid) < 0) | ||
| 991 | return; | ||
| 992 | |||
| 993 | down_write(&osdc->map_sem); | ||
| 994 | |||
| 995 | /* incremental maps */ | ||
| 996 | ceph_decode_32_safe(&p, end, nr_maps, bad); | ||
| 997 | dout(" %d inc maps\n", nr_maps); | ||
| 998 | while (nr_maps > 0) { | ||
| 999 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); | ||
| 1000 | epoch = ceph_decode_32(&p); | ||
| 1001 | maplen = ceph_decode_32(&p); | ||
| 1002 | ceph_decode_need(&p, end, maplen, bad); | ||
| 1003 | next = p + maplen; | ||
| 1004 | if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) { | ||
| 1005 | dout("applying incremental map %u len %d\n", | ||
| 1006 | epoch, maplen); | ||
| 1007 | newmap = osdmap_apply_incremental(&p, next, | ||
| 1008 | osdc->osdmap, | ||
| 1009 | osdc->client->msgr); | ||
| 1010 | if (IS_ERR(newmap)) { | ||
| 1011 | err = PTR_ERR(newmap); | ||
| 1012 | goto bad; | ||
| 1013 | } | ||
| 1014 | BUG_ON(!newmap); | ||
| 1015 | if (newmap != osdc->osdmap) { | ||
| 1016 | ceph_osdmap_destroy(osdc->osdmap); | ||
| 1017 | osdc->osdmap = newmap; | ||
| 1018 | } | ||
| 1019 | } else { | ||
| 1020 | dout("ignoring incremental map %u len %d\n", | ||
| 1021 | epoch, maplen); | ||
| 1022 | } | ||
| 1023 | p = next; | ||
| 1024 | nr_maps--; | ||
| 1025 | } | ||
| 1026 | if (newmap) | ||
| 1027 | goto done; | ||
| 1028 | |||
| 1029 | /* full maps */ | ||
| 1030 | ceph_decode_32_safe(&p, end, nr_maps, bad); | ||
| 1031 | dout(" %d full maps\n", nr_maps); | ||
| 1032 | while (nr_maps) { | ||
| 1033 | ceph_decode_need(&p, end, 2*sizeof(u32), bad); | ||
| 1034 | epoch = ceph_decode_32(&p); | ||
| 1035 | maplen = ceph_decode_32(&p); | ||
| 1036 | ceph_decode_need(&p, end, maplen, bad); | ||
| 1037 | if (nr_maps > 1) { | ||
| 1038 | dout("skipping non-latest full map %u len %d\n", | ||
| 1039 | epoch, maplen); | ||
| 1040 | } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) { | ||
| 1041 | dout("skipping full map %u len %d, " | ||
| 1042 | "older than our %u\n", epoch, maplen, | ||
| 1043 | osdc->osdmap->epoch); | ||
| 1044 | } else { | ||
| 1045 | dout("taking full map %u len %d\n", epoch, maplen); | ||
| 1046 | newmap = osdmap_decode(&p, p+maplen); | ||
| 1047 | if (IS_ERR(newmap)) { | ||
| 1048 | err = PTR_ERR(newmap); | ||
| 1049 | goto bad; | ||
| 1050 | } | ||
| 1051 | BUG_ON(!newmap); | ||
| 1052 | oldmap = osdc->osdmap; | ||
| 1053 | osdc->osdmap = newmap; | ||
| 1054 | if (oldmap) | ||
| 1055 | ceph_osdmap_destroy(oldmap); | ||
| 1056 | } | ||
| 1057 | p += maplen; | ||
| 1058 | nr_maps--; | ||
| 1059 | } | ||
| 1060 | |||
| 1061 | done: | ||
| 1062 | downgrade_write(&osdc->map_sem); | ||
| 1063 | ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch); | ||
| 1064 | if (newmap) | ||
| 1065 | kick_requests(osdc, NULL); | ||
| 1066 | up_read(&osdc->map_sem); | ||
| 1067 | return; | ||
| 1068 | |||
| 1069 | bad: | ||
| 1070 | pr_err("osdc handle_map corrupt msg\n"); | ||
| 1071 | ceph_msg_dump(msg); | ||
| 1072 | up_write(&osdc->map_sem); | ||
| 1073 | return; | ||
| 1074 | } | ||
| 1075 | |||
| 1076 | |||
| 1077 | /* | ||
| 1078 | * A read request prepares specific pages that data is to be read into. | ||
| 1079 | * When a message is being read off the wire, we call prepare_pages to | ||
| 1080 | * find those pages. | ||
| 1081 | * 0 = success, -1 failure. | ||
| 1082 | */ | ||
| 1083 | static int __prepare_pages(struct ceph_connection *con, | ||
| 1084 | struct ceph_msg_header *hdr, | ||
| 1085 | struct ceph_osd_request *req, | ||
| 1086 | u64 tid, | ||
| 1087 | struct ceph_msg *m) | ||
| 1088 | { | ||
| 1089 | struct ceph_osd *osd = con->private; | ||
| 1090 | struct ceph_osd_client *osdc; | ||
| 1091 | int ret = -1; | ||
| 1092 | int data_len = le32_to_cpu(hdr->data_len); | ||
| 1093 | unsigned data_off = le16_to_cpu(hdr->data_off); | ||
| 1094 | |||
| 1095 | int want = calc_pages_for(data_off & ~PAGE_MASK, data_len); | ||
| 1096 | |||
| 1097 | if (!osd) | ||
| 1098 | return -1; | ||
| 1099 | |||
| 1100 | osdc = osd->o_osdc; | ||
| 1101 | |||
| 1102 | dout("__prepare_pages on msg %p tid %llu, has %d pages, want %d\n", m, | ||
| 1103 | tid, req->r_num_pages, want); | ||
| 1104 | if (unlikely(req->r_num_pages < want)) | ||
| 1105 | goto out; | ||
| 1106 | m->pages = req->r_pages; | ||
| 1107 | m->nr_pages = req->r_num_pages; | ||
| 1108 | ret = 0; /* success */ | ||
| 1109 | out: | ||
| 1110 | BUG_ON(ret < 0 || m->nr_pages < want); | ||
| 1111 | |||
| 1112 | return ret; | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | /* | ||
| 1116 | * Register request, send initial attempt. | ||
| 1117 | */ | ||
| 1118 | int ceph_osdc_start_request(struct ceph_osd_client *osdc, | ||
| 1119 | struct ceph_osd_request *req, | ||
| 1120 | bool nofail) | ||
| 1121 | { | ||
| 1122 | int rc = 0; | ||
| 1123 | |||
| 1124 | req->r_request->pages = req->r_pages; | ||
| 1125 | req->r_request->nr_pages = req->r_num_pages; | ||
| 1126 | |||
| 1127 | register_request(osdc, req); | ||
| 1128 | |||
| 1129 | down_read(&osdc->map_sem); | ||
| 1130 | mutex_lock(&osdc->request_mutex); | ||
| 1131 | /* | ||
| 1132 | * a racing kick_requests() may have sent the message for us | ||
| 1133 | * while we dropped request_mutex above, so only send now if | ||
| 1134 | * the request still han't been touched yet. | ||
| 1135 | */ | ||
| 1136 | if (req->r_sent == 0) { | ||
| 1137 | rc = __send_request(osdc, req); | ||
| 1138 | if (rc) { | ||
| 1139 | if (nofail) { | ||
| 1140 | dout("osdc_start_request failed send, " | ||
| 1141 | " marking %lld\n", req->r_tid); | ||
| 1142 | req->r_resend = true; | ||
| 1143 | rc = 0; | ||
| 1144 | } else { | ||
| 1145 | __unregister_request(osdc, req); | ||
| 1146 | } | ||
| 1147 | } | ||
| 1148 | } | ||
| 1149 | mutex_unlock(&osdc->request_mutex); | ||
| 1150 | up_read(&osdc->map_sem); | ||
| 1151 | return rc; | ||
| 1152 | } | ||
| 1153 | |||
| 1154 | /* | ||
| 1155 | * wait for a request to complete | ||
| 1156 | */ | ||
| 1157 | int ceph_osdc_wait_request(struct ceph_osd_client *osdc, | ||
| 1158 | struct ceph_osd_request *req) | ||
| 1159 | { | ||
| 1160 | int rc; | ||
| 1161 | |||
| 1162 | rc = wait_for_completion_interruptible(&req->r_completion); | ||
| 1163 | if (rc < 0) { | ||
| 1164 | mutex_lock(&osdc->request_mutex); | ||
| 1165 | __cancel_request(req); | ||
| 1166 | __unregister_request(osdc, req); | ||
| 1167 | mutex_unlock(&osdc->request_mutex); | ||
| 1168 | dout("wait_request tid %llu canceled/timed out\n", req->r_tid); | ||
| 1169 | return rc; | ||
| 1170 | } | ||
| 1171 | |||
| 1172 | dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result); | ||
| 1173 | return req->r_result; | ||
| 1174 | } | ||
| 1175 | |||
| 1176 | /* | ||
| 1177 | * sync - wait for all in-flight requests to flush. avoid starvation. | ||
| 1178 | */ | ||
| 1179 | void ceph_osdc_sync(struct ceph_osd_client *osdc) | ||
| 1180 | { | ||
| 1181 | struct ceph_osd_request *req; | ||
| 1182 | u64 last_tid, next_tid = 0; | ||
| 1183 | |||
| 1184 | mutex_lock(&osdc->request_mutex); | ||
| 1185 | last_tid = osdc->last_tid; | ||
| 1186 | while (1) { | ||
| 1187 | req = __lookup_request_ge(osdc, next_tid); | ||
| 1188 | if (!req) | ||
| 1189 | break; | ||
| 1190 | if (req->r_tid > last_tid) | ||
| 1191 | break; | ||
| 1192 | |||
| 1193 | next_tid = req->r_tid + 1; | ||
| 1194 | if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0) | ||
| 1195 | continue; | ||
| 1196 | |||
| 1197 | ceph_osdc_get_request(req); | ||
| 1198 | mutex_unlock(&osdc->request_mutex); | ||
| 1199 | dout("sync waiting on tid %llu (last is %llu)\n", | ||
| 1200 | req->r_tid, last_tid); | ||
| 1201 | wait_for_completion(&req->r_safe_completion); | ||
| 1202 | mutex_lock(&osdc->request_mutex); | ||
| 1203 | ceph_osdc_put_request(req); | ||
| 1204 | } | ||
| 1205 | mutex_unlock(&osdc->request_mutex); | ||
| 1206 | dout("sync done (thru tid %llu)\n", last_tid); | ||
| 1207 | } | ||
| 1208 | |||
| 1209 | /* | ||
| 1210 | * init, shutdown | ||
| 1211 | */ | ||
| 1212 | int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client) | ||
| 1213 | { | ||
| 1214 | int err; | ||
| 1215 | |||
| 1216 | dout("init\n"); | ||
| 1217 | osdc->client = client; | ||
| 1218 | osdc->osdmap = NULL; | ||
| 1219 | init_rwsem(&osdc->map_sem); | ||
| 1220 | init_completion(&osdc->map_waiters); | ||
| 1221 | osdc->last_requested_map = 0; | ||
| 1222 | mutex_init(&osdc->request_mutex); | ||
| 1223 | osdc->last_tid = 0; | ||
| 1224 | osdc->osds = RB_ROOT; | ||
| 1225 | INIT_LIST_HEAD(&osdc->osd_lru); | ||
| 1226 | osdc->requests = RB_ROOT; | ||
| 1227 | INIT_LIST_HEAD(&osdc->req_lru); | ||
| 1228 | osdc->num_requests = 0; | ||
| 1229 | INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout); | ||
| 1230 | INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout); | ||
| 1231 | |||
| 1232 | schedule_delayed_work(&osdc->osds_timeout_work, | ||
| 1233 | round_jiffies_relative(osdc->client->mount_args->osd_idle_ttl * HZ)); | ||
| 1234 | |||
| 1235 | err = -ENOMEM; | ||
| 1236 | osdc->req_mempool = mempool_create_kmalloc_pool(10, | ||
| 1237 | sizeof(struct ceph_osd_request)); | ||
| 1238 | if (!osdc->req_mempool) | ||
| 1239 | goto out; | ||
| 1240 | |||
| 1241 | err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true); | ||
| 1242 | if (err < 0) | ||
| 1243 | goto out_mempool; | ||
| 1244 | err = ceph_msgpool_init(&osdc->msgpool_op_reply, | ||
| 1245 | OSD_OPREPLY_FRONT_LEN, 10, true); | ||
| 1246 | if (err < 0) | ||
| 1247 | goto out_msgpool; | ||
| 1248 | return 0; | ||
| 1249 | |||
| 1250 | out_msgpool: | ||
| 1251 | ceph_msgpool_destroy(&osdc->msgpool_op); | ||
| 1252 | out_mempool: | ||
| 1253 | mempool_destroy(osdc->req_mempool); | ||
| 1254 | out: | ||
| 1255 | return err; | ||
| 1256 | } | ||
| 1257 | |||
| 1258 | void ceph_osdc_stop(struct ceph_osd_client *osdc) | ||
| 1259 | { | ||
| 1260 | cancel_delayed_work_sync(&osdc->timeout_work); | ||
| 1261 | cancel_delayed_work_sync(&osdc->osds_timeout_work); | ||
| 1262 | if (osdc->osdmap) { | ||
| 1263 | ceph_osdmap_destroy(osdc->osdmap); | ||
| 1264 | osdc->osdmap = NULL; | ||
| 1265 | } | ||
| 1266 | remove_old_osds(osdc, 1); | ||
| 1267 | mempool_destroy(osdc->req_mempool); | ||
| 1268 | ceph_msgpool_destroy(&osdc->msgpool_op); | ||
| 1269 | ceph_msgpool_destroy(&osdc->msgpool_op_reply); | ||
| 1270 | } | ||
| 1271 | |||
| 1272 | /* | ||
| 1273 | * Read some contiguous pages. If we cross a stripe boundary, shorten | ||
| 1274 | * *plen. Return number of bytes read, or error. | ||
| 1275 | */ | ||
| 1276 | int ceph_osdc_readpages(struct ceph_osd_client *osdc, | ||
| 1277 | struct ceph_vino vino, struct ceph_file_layout *layout, | ||
| 1278 | u64 off, u64 *plen, | ||
| 1279 | u32 truncate_seq, u64 truncate_size, | ||
| 1280 | struct page **pages, int num_pages) | ||
| 1281 | { | ||
| 1282 | struct ceph_osd_request *req; | ||
| 1283 | int rc = 0; | ||
| 1284 | |||
| 1285 | dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino, | ||
| 1286 | vino.snap, off, *plen); | ||
| 1287 | req = ceph_osdc_new_request(osdc, layout, vino, off, plen, | ||
| 1288 | CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ, | ||
| 1289 | NULL, 0, truncate_seq, truncate_size, NULL, | ||
| 1290 | false, 1); | ||
| 1291 | if (IS_ERR(req)) | ||
| 1292 | return PTR_ERR(req); | ||
| 1293 | |||
| 1294 | /* it may be a short read due to an object boundary */ | ||
| 1295 | req->r_pages = pages; | ||
| 1296 | num_pages = calc_pages_for(off, *plen); | ||
| 1297 | req->r_num_pages = num_pages; | ||
| 1298 | |||
| 1299 | dout("readpages final extent is %llu~%llu (%d pages)\n", | ||
| 1300 | off, *plen, req->r_num_pages); | ||
| 1301 | |||
| 1302 | rc = ceph_osdc_start_request(osdc, req, false); | ||
| 1303 | if (!rc) | ||
| 1304 | rc = ceph_osdc_wait_request(osdc, req); | ||
| 1305 | |||
| 1306 | ceph_osdc_put_request(req); | ||
| 1307 | dout("readpages result %d\n", rc); | ||
| 1308 | return rc; | ||
| 1309 | } | ||
| 1310 | |||
| 1311 | /* | ||
| 1312 | * do a synchronous write on N pages | ||
| 1313 | */ | ||
| 1314 | int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino, | ||
| 1315 | struct ceph_file_layout *layout, | ||
| 1316 | struct ceph_snap_context *snapc, | ||
| 1317 | u64 off, u64 len, | ||
| 1318 | u32 truncate_seq, u64 truncate_size, | ||
| 1319 | struct timespec *mtime, | ||
| 1320 | struct page **pages, int num_pages, | ||
| 1321 | int flags, int do_sync, bool nofail) | ||
| 1322 | { | ||
| 1323 | struct ceph_osd_request *req; | ||
| 1324 | int rc = 0; | ||
| 1325 | |||
| 1326 | BUG_ON(vino.snap != CEPH_NOSNAP); | ||
| 1327 | req = ceph_osdc_new_request(osdc, layout, vino, off, &len, | ||
| 1328 | CEPH_OSD_OP_WRITE, | ||
| 1329 | flags | CEPH_OSD_FLAG_ONDISK | | ||
| 1330 | CEPH_OSD_FLAG_WRITE, | ||
| 1331 | snapc, do_sync, | ||
| 1332 | truncate_seq, truncate_size, mtime, | ||
| 1333 | nofail, 1); | ||
| 1334 | if (IS_ERR(req)) | ||
| 1335 | return PTR_ERR(req); | ||
| 1336 | |||
| 1337 | /* it may be a short write due to an object boundary */ | ||
| 1338 | req->r_pages = pages; | ||
| 1339 | req->r_num_pages = calc_pages_for(off, len); | ||
| 1340 | dout("writepages %llu~%llu (%d pages)\n", off, len, | ||
| 1341 | req->r_num_pages); | ||
| 1342 | |||
| 1343 | rc = ceph_osdc_start_request(osdc, req, nofail); | ||
| 1344 | if (!rc) | ||
| 1345 | rc = ceph_osdc_wait_request(osdc, req); | ||
| 1346 | |||
| 1347 | ceph_osdc_put_request(req); | ||
| 1348 | if (rc == 0) | ||
| 1349 | rc = len; | ||
| 1350 | dout("writepages result %d\n", rc); | ||
| 1351 | return rc; | ||
| 1352 | } | ||
| 1353 | |||
| 1354 | /* | ||
| 1355 | * handle incoming message | ||
| 1356 | */ | ||
| 1357 | static void dispatch(struct ceph_connection *con, struct ceph_msg *msg) | ||
| 1358 | { | ||
| 1359 | struct ceph_osd *osd = con->private; | ||
| 1360 | struct ceph_osd_client *osdc; | ||
| 1361 | int type = le16_to_cpu(msg->hdr.type); | ||
| 1362 | |||
| 1363 | if (!osd) | ||
| 1364 | return; | ||
| 1365 | osdc = osd->o_osdc; | ||
| 1366 | |||
| 1367 | switch (type) { | ||
| 1368 | case CEPH_MSG_OSD_MAP: | ||
| 1369 | ceph_osdc_handle_map(osdc, msg); | ||
| 1370 | break; | ||
| 1371 | case CEPH_MSG_OSD_OPREPLY: | ||
| 1372 | handle_reply(osdc, msg, con); | ||
| 1373 | break; | ||
| 1374 | |||
| 1375 | default: | ||
| 1376 | pr_err("received unknown message type %d %s\n", type, | ||
| 1377 | ceph_msg_type_name(type)); | ||
| 1378 | } | ||
| 1379 | ceph_msg_put(msg); | ||
| 1380 | } | ||
| 1381 | |||
| 1382 | /* | ||
| 1383 | * lookup and return message for incoming reply | ||
| 1384 | */ | ||
| 1385 | static struct ceph_msg *get_reply(struct ceph_connection *con, | ||
| 1386 | struct ceph_msg_header *hdr, | ||
| 1387 | int *skip) | ||
| 1388 | { | ||
| 1389 | struct ceph_osd *osd = con->private; | ||
| 1390 | struct ceph_osd_client *osdc = osd->o_osdc; | ||
| 1391 | struct ceph_msg *m; | ||
| 1392 | struct ceph_osd_request *req; | ||
| 1393 | int front = le32_to_cpu(hdr->front_len); | ||
| 1394 | int data_len = le32_to_cpu(hdr->data_len); | ||
| 1395 | u64 tid; | ||
| 1396 | int err; | ||
| 1397 | |||
| 1398 | tid = le64_to_cpu(hdr->tid); | ||
| 1399 | mutex_lock(&osdc->request_mutex); | ||
| 1400 | req = __lookup_request(osdc, tid); | ||
| 1401 | if (!req) { | ||
| 1402 | *skip = 1; | ||
| 1403 | m = NULL; | ||
| 1404 | pr_info("get_reply unknown tid %llu from osd%d\n", tid, | ||
| 1405 | osd->o_osd); | ||
| 1406 | goto out; | ||
| 1407 | } | ||
| 1408 | |||
| 1409 | if (req->r_con_filling_msg) { | ||
| 1410 | dout("get_reply revoking msg %p from old con %p\n", | ||
| 1411 | req->r_reply, req->r_con_filling_msg); | ||
| 1412 | ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply); | ||
| 1413 | ceph_con_put(req->r_con_filling_msg); | ||
| 1414 | } | ||
| 1415 | |||
| 1416 | if (front > req->r_reply->front.iov_len) { | ||
| 1417 | pr_warning("get_reply front %d > preallocated %d\n", | ||
| 1418 | front, (int)req->r_reply->front.iov_len); | ||
| 1419 | m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, 0, 0, NULL); | ||
| 1420 | if (IS_ERR(m)) | ||
| 1421 | goto out; | ||
| 1422 | ceph_msg_put(req->r_reply); | ||
| 1423 | req->r_reply = m; | ||
| 1424 | } | ||
| 1425 | m = ceph_msg_get(req->r_reply); | ||
| 1426 | |||
| 1427 | if (data_len > 0) { | ||
| 1428 | err = __prepare_pages(con, hdr, req, tid, m); | ||
| 1429 | if (err < 0) { | ||
| 1430 | *skip = 1; | ||
| 1431 | ceph_msg_put(m); | ||
| 1432 | m = ERR_PTR(err); | ||
| 1433 | } | ||
| 1434 | } | ||
| 1435 | *skip = 0; | ||
| 1436 | req->r_con_filling_msg = ceph_con_get(con); | ||
| 1437 | dout("get_reply tid %lld %p\n", tid, m); | ||
| 1438 | |||
| 1439 | out: | ||
| 1440 | mutex_unlock(&osdc->request_mutex); | ||
| 1441 | return m; | ||
| 1442 | |||
| 1443 | } | ||
| 1444 | |||
| 1445 | static struct ceph_msg *alloc_msg(struct ceph_connection *con, | ||
| 1446 | struct ceph_msg_header *hdr, | ||
| 1447 | int *skip) | ||
| 1448 | { | ||
| 1449 | struct ceph_osd *osd = con->private; | ||
| 1450 | int type = le16_to_cpu(hdr->type); | ||
| 1451 | int front = le32_to_cpu(hdr->front_len); | ||
| 1452 | |||
| 1453 | switch (type) { | ||
| 1454 | case CEPH_MSG_OSD_MAP: | ||
| 1455 | return ceph_msg_new(type, front, 0, 0, NULL); | ||
| 1456 | case CEPH_MSG_OSD_OPREPLY: | ||
| 1457 | return get_reply(con, hdr, skip); | ||
| 1458 | default: | ||
| 1459 | pr_info("alloc_msg unexpected msg type %d from osd%d\n", type, | ||
| 1460 | osd->o_osd); | ||
| 1461 | *skip = 1; | ||
| 1462 | return NULL; | ||
| 1463 | } | ||
| 1464 | } | ||
| 1465 | |||
| 1466 | /* | ||
| 1467 | * Wrappers to refcount containing ceph_osd struct | ||
| 1468 | */ | ||
| 1469 | static struct ceph_connection *get_osd_con(struct ceph_connection *con) | ||
| 1470 | { | ||
| 1471 | struct ceph_osd *osd = con->private; | ||
| 1472 | if (get_osd(osd)) | ||
| 1473 | return con; | ||
| 1474 | return NULL; | ||
| 1475 | } | ||
| 1476 | |||
| 1477 | static void put_osd_con(struct ceph_connection *con) | ||
| 1478 | { | ||
| 1479 | struct ceph_osd *osd = con->private; | ||
| 1480 | put_osd(osd); | ||
| 1481 | } | ||
| 1482 | |||
| 1483 | /* | ||
| 1484 | * authentication | ||
| 1485 | */ | ||
| 1486 | static int get_authorizer(struct ceph_connection *con, | ||
| 1487 | void **buf, int *len, int *proto, | ||
| 1488 | void **reply_buf, int *reply_len, int force_new) | ||
| 1489 | { | ||
| 1490 | struct ceph_osd *o = con->private; | ||
| 1491 | struct ceph_osd_client *osdc = o->o_osdc; | ||
| 1492 | struct ceph_auth_client *ac = osdc->client->monc.auth; | ||
| 1493 | int ret = 0; | ||
| 1494 | |||
| 1495 | if (force_new && o->o_authorizer) { | ||
| 1496 | ac->ops->destroy_authorizer(ac, o->o_authorizer); | ||
| 1497 | o->o_authorizer = NULL; | ||
| 1498 | } | ||
| 1499 | if (o->o_authorizer == NULL) { | ||
| 1500 | ret = ac->ops->create_authorizer( | ||
| 1501 | ac, CEPH_ENTITY_TYPE_OSD, | ||
| 1502 | &o->o_authorizer, | ||
| 1503 | &o->o_authorizer_buf, | ||
| 1504 | &o->o_authorizer_buf_len, | ||
| 1505 | &o->o_authorizer_reply_buf, | ||
| 1506 | &o->o_authorizer_reply_buf_len); | ||
| 1507 | if (ret) | ||
| 1508 | return ret; | ||
| 1509 | } | ||
| 1510 | |||
| 1511 | *proto = ac->protocol; | ||
| 1512 | *buf = o->o_authorizer_buf; | ||
| 1513 | *len = o->o_authorizer_buf_len; | ||
| 1514 | *reply_buf = o->o_authorizer_reply_buf; | ||
| 1515 | *reply_len = o->o_authorizer_reply_buf_len; | ||
| 1516 | return 0; | ||
| 1517 | } | ||
| 1518 | |||
| 1519 | |||
| 1520 | static int verify_authorizer_reply(struct ceph_connection *con, int len) | ||
| 1521 | { | ||
| 1522 | struct ceph_osd *o = con->private; | ||
| 1523 | struct ceph_osd_client *osdc = o->o_osdc; | ||
| 1524 | struct ceph_auth_client *ac = osdc->client->monc.auth; | ||
| 1525 | |||
| 1526 | return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len); | ||
| 1527 | } | ||
| 1528 | |||
| 1529 | static int invalidate_authorizer(struct ceph_connection *con) | ||
| 1530 | { | ||
| 1531 | struct ceph_osd *o = con->private; | ||
| 1532 | struct ceph_osd_client *osdc = o->o_osdc; | ||
| 1533 | struct ceph_auth_client *ac = osdc->client->monc.auth; | ||
| 1534 | |||
| 1535 | if (ac->ops->invalidate_authorizer) | ||
| 1536 | ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD); | ||
| 1537 | |||
| 1538 | return ceph_monc_validate_auth(&osdc->client->monc); | ||
| 1539 | } | ||
| 1540 | |||
| 1541 | const static struct ceph_connection_operations osd_con_ops = { | ||
| 1542 | .get = get_osd_con, | ||
| 1543 | .put = put_osd_con, | ||
| 1544 | .dispatch = dispatch, | ||
| 1545 | .get_authorizer = get_authorizer, | ||
| 1546 | .verify_authorizer_reply = verify_authorizer_reply, | ||
| 1547 | .invalidate_authorizer = invalidate_authorizer, | ||
| 1548 | .alloc_msg = alloc_msg, | ||
| 1549 | .fault = osd_reset, | ||
| 1550 | }; | ||
diff --git a/fs/ceph/osd_client.h b/fs/ceph/osd_client.h new file mode 100644 index 000000000000..b0759911e7c3 --- /dev/null +++ b/fs/ceph/osd_client.h | |||
| @@ -0,0 +1,166 @@ | |||
| 1 | #ifndef _FS_CEPH_OSD_CLIENT_H | ||
| 2 | #define _FS_CEPH_OSD_CLIENT_H | ||
| 3 | |||
| 4 | #include <linux/completion.h> | ||
| 5 | #include <linux/kref.h> | ||
| 6 | #include <linux/mempool.h> | ||
| 7 | #include <linux/rbtree.h> | ||
| 8 | |||
| 9 | #include "types.h" | ||
| 10 | #include "osdmap.h" | ||
| 11 | #include "messenger.h" | ||
| 12 | |||
| 13 | struct ceph_msg; | ||
| 14 | struct ceph_snap_context; | ||
| 15 | struct ceph_osd_request; | ||
| 16 | struct ceph_osd_client; | ||
| 17 | struct ceph_authorizer; | ||
| 18 | |||
| 19 | /* | ||
| 20 | * completion callback for async writepages | ||
| 21 | */ | ||
| 22 | typedef void (*ceph_osdc_callback_t)(struct ceph_osd_request *, | ||
| 23 | struct ceph_msg *); | ||
| 24 | |||
| 25 | /* a given osd we're communicating with */ | ||
| 26 | struct ceph_osd { | ||
| 27 | atomic_t o_ref; | ||
| 28 | struct ceph_osd_client *o_osdc; | ||
| 29 | int o_osd; | ||
| 30 | int o_incarnation; | ||
| 31 | struct rb_node o_node; | ||
| 32 | struct ceph_connection o_con; | ||
| 33 | struct list_head o_requests; | ||
| 34 | struct list_head o_osd_lru; | ||
| 35 | struct ceph_authorizer *o_authorizer; | ||
| 36 | void *o_authorizer_buf, *o_authorizer_reply_buf; | ||
| 37 | size_t o_authorizer_buf_len, o_authorizer_reply_buf_len; | ||
| 38 | unsigned long lru_ttl; | ||
| 39 | int o_marked_for_keepalive; | ||
| 40 | struct list_head o_keepalive_item; | ||
| 41 | }; | ||
| 42 | |||
| 43 | /* an in-flight request */ | ||
| 44 | struct ceph_osd_request { | ||
| 45 | u64 r_tid; /* unique for this client */ | ||
| 46 | struct rb_node r_node; | ||
| 47 | struct list_head r_req_lru_item; | ||
| 48 | struct list_head r_osd_item; | ||
| 49 | struct ceph_osd *r_osd; | ||
| 50 | struct ceph_pg r_pgid; | ||
| 51 | |||
| 52 | struct ceph_connection *r_con_filling_msg; | ||
| 53 | |||
| 54 | struct ceph_msg *r_request, *r_reply; | ||
| 55 | int r_result; | ||
| 56 | int r_flags; /* any additional flags for the osd */ | ||
| 57 | u32 r_sent; /* >0 if r_request is sending/sent */ | ||
| 58 | int r_got_reply; | ||
| 59 | |||
| 60 | struct ceph_osd_client *r_osdc; | ||
| 61 | struct kref r_kref; | ||
| 62 | bool r_mempool; | ||
| 63 | struct completion r_completion, r_safe_completion; | ||
| 64 | ceph_osdc_callback_t r_callback, r_safe_callback; | ||
| 65 | struct ceph_eversion r_reassert_version; | ||
| 66 | struct list_head r_unsafe_item; | ||
| 67 | |||
| 68 | struct inode *r_inode; /* for use by callbacks */ | ||
| 69 | struct writeback_control *r_wbc; /* ditto */ | ||
| 70 | |||
| 71 | char r_oid[40]; /* object name */ | ||
| 72 | int r_oid_len; | ||
| 73 | unsigned long r_stamp; /* send OR check time */ | ||
| 74 | bool r_resend; /* msg send failed, needs retry */ | ||
| 75 | |||
| 76 | struct ceph_file_layout r_file_layout; | ||
| 77 | struct ceph_snap_context *r_snapc; /* snap context for writes */ | ||
| 78 | unsigned r_num_pages; /* size of page array (follows) */ | ||
| 79 | struct page **r_pages; /* pages for data payload */ | ||
| 80 | int r_pages_from_pool; | ||
| 81 | int r_own_pages; /* if true, i own page list */ | ||
| 82 | }; | ||
| 83 | |||
| 84 | struct ceph_osd_client { | ||
| 85 | struct ceph_client *client; | ||
| 86 | |||
| 87 | struct ceph_osdmap *osdmap; /* current map */ | ||
| 88 | struct rw_semaphore map_sem; | ||
| 89 | struct completion map_waiters; | ||
| 90 | u64 last_requested_map; | ||
| 91 | |||
| 92 | struct mutex request_mutex; | ||
| 93 | struct rb_root osds; /* osds */ | ||
| 94 | struct list_head osd_lru; /* idle osds */ | ||
| 95 | u64 timeout_tid; /* tid of timeout triggering rq */ | ||
| 96 | u64 last_tid; /* tid of last request */ | ||
| 97 | struct rb_root requests; /* pending requests */ | ||
| 98 | struct list_head req_lru; /* pending requests lru */ | ||
| 99 | int num_requests; | ||
| 100 | struct delayed_work timeout_work; | ||
| 101 | struct delayed_work osds_timeout_work; | ||
| 102 | #ifdef CONFIG_DEBUG_FS | ||
| 103 | struct dentry *debugfs_file; | ||
| 104 | #endif | ||
| 105 | |||
| 106 | mempool_t *req_mempool; | ||
| 107 | |||
| 108 | struct ceph_msgpool msgpool_op; | ||
| 109 | struct ceph_msgpool msgpool_op_reply; | ||
| 110 | }; | ||
| 111 | |||
| 112 | extern int ceph_osdc_init(struct ceph_osd_client *osdc, | ||
| 113 | struct ceph_client *client); | ||
| 114 | extern void ceph_osdc_stop(struct ceph_osd_client *osdc); | ||
| 115 | |||
| 116 | extern void ceph_osdc_handle_reply(struct ceph_osd_client *osdc, | ||
| 117 | struct ceph_msg *msg); | ||
| 118 | extern void ceph_osdc_handle_map(struct ceph_osd_client *osdc, | ||
| 119 | struct ceph_msg *msg); | ||
| 120 | |||
| 121 | extern struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *, | ||
| 122 | struct ceph_file_layout *layout, | ||
| 123 | struct ceph_vino vino, | ||
| 124 | u64 offset, u64 *len, int op, int flags, | ||
| 125 | struct ceph_snap_context *snapc, | ||
| 126 | int do_sync, u32 truncate_seq, | ||
| 127 | u64 truncate_size, | ||
| 128 | struct timespec *mtime, | ||
| 129 | bool use_mempool, int num_reply); | ||
| 130 | |||
| 131 | static inline void ceph_osdc_get_request(struct ceph_osd_request *req) | ||
| 132 | { | ||
| 133 | kref_get(&req->r_kref); | ||
| 134 | } | ||
| 135 | extern void ceph_osdc_release_request(struct kref *kref); | ||
| 136 | static inline void ceph_osdc_put_request(struct ceph_osd_request *req) | ||
| 137 | { | ||
| 138 | kref_put(&req->r_kref, ceph_osdc_release_request); | ||
| 139 | } | ||
| 140 | |||
| 141 | extern int ceph_osdc_start_request(struct ceph_osd_client *osdc, | ||
| 142 | struct ceph_osd_request *req, | ||
| 143 | bool nofail); | ||
| 144 | extern int ceph_osdc_wait_request(struct ceph_osd_client *osdc, | ||
| 145 | struct ceph_osd_request *req); | ||
| 146 | extern void ceph_osdc_sync(struct ceph_osd_client *osdc); | ||
| 147 | |||
| 148 | extern int ceph_osdc_readpages(struct ceph_osd_client *osdc, | ||
| 149 | struct ceph_vino vino, | ||
| 150 | struct ceph_file_layout *layout, | ||
| 151 | u64 off, u64 *plen, | ||
| 152 | u32 truncate_seq, u64 truncate_size, | ||
| 153 | struct page **pages, int nr_pages); | ||
| 154 | |||
| 155 | extern int ceph_osdc_writepages(struct ceph_osd_client *osdc, | ||
| 156 | struct ceph_vino vino, | ||
| 157 | struct ceph_file_layout *layout, | ||
| 158 | struct ceph_snap_context *sc, | ||
| 159 | u64 off, u64 len, | ||
| 160 | u32 truncate_seq, u64 truncate_size, | ||
| 161 | struct timespec *mtime, | ||
| 162 | struct page **pages, int nr_pages, | ||
| 163 | int flags, int do_sync, bool nofail); | ||
| 164 | |||
| 165 | #endif | ||
| 166 | |||
diff --git a/fs/ceph/osdmap.c b/fs/ceph/osdmap.c new file mode 100644 index 000000000000..d82fe87c2a6e --- /dev/null +++ b/fs/ceph/osdmap.c | |||
| @@ -0,0 +1,1022 @@ | |||
| 1 | |||
| 2 | #include <asm/div64.h> | ||
| 3 | |||
| 4 | #include "super.h" | ||
| 5 | #include "osdmap.h" | ||
| 6 | #include "crush/hash.h" | ||
| 7 | #include "crush/mapper.h" | ||
| 8 | #include "decode.h" | ||
| 9 | #include "ceph_debug.h" | ||
| 10 | |||
| 11 | char *ceph_osdmap_state_str(char *str, int len, int state) | ||
| 12 | { | ||
| 13 | int flag = 0; | ||
| 14 | |||
| 15 | if (!len) | ||
| 16 | goto done; | ||
| 17 | |||
| 18 | *str = '\0'; | ||
| 19 | if (state) { | ||
| 20 | if (state & CEPH_OSD_EXISTS) { | ||
| 21 | snprintf(str, len, "exists"); | ||
| 22 | flag = 1; | ||
| 23 | } | ||
| 24 | if (state & CEPH_OSD_UP) { | ||
| 25 | snprintf(str, len, "%s%s%s", str, (flag ? ", " : ""), | ||
| 26 | "up"); | ||
| 27 | flag = 1; | ||
| 28 | } | ||
| 29 | } else { | ||
| 30 | snprintf(str, len, "doesn't exist"); | ||
| 31 | } | ||
| 32 | done: | ||
| 33 | return str; | ||
| 34 | } | ||
| 35 | |||
| 36 | /* maps */ | ||
| 37 | |||
| 38 | static int calc_bits_of(unsigned t) | ||
| 39 | { | ||
| 40 | int b = 0; | ||
| 41 | while (t) { | ||
| 42 | t = t >> 1; | ||
| 43 | b++; | ||
| 44 | } | ||
| 45 | return b; | ||
| 46 | } | ||
| 47 | |||
| 48 | /* | ||
| 49 | * the foo_mask is the smallest value 2^n-1 that is >= foo. | ||
| 50 | */ | ||
| 51 | static void calc_pg_masks(struct ceph_pg_pool_info *pi) | ||
| 52 | { | ||
| 53 | pi->pg_num_mask = (1 << calc_bits_of(le32_to_cpu(pi->v.pg_num)-1)) - 1; | ||
| 54 | pi->pgp_num_mask = | ||
| 55 | (1 << calc_bits_of(le32_to_cpu(pi->v.pgp_num)-1)) - 1; | ||
| 56 | pi->lpg_num_mask = | ||
| 57 | (1 << calc_bits_of(le32_to_cpu(pi->v.lpg_num)-1)) - 1; | ||
| 58 | pi->lpgp_num_mask = | ||
| 59 | (1 << calc_bits_of(le32_to_cpu(pi->v.lpgp_num)-1)) - 1; | ||
| 60 | } | ||
| 61 | |||
| 62 | /* | ||
| 63 | * decode crush map | ||
| 64 | */ | ||
| 65 | static int crush_decode_uniform_bucket(void **p, void *end, | ||
| 66 | struct crush_bucket_uniform *b) | ||
| 67 | { | ||
| 68 | dout("crush_decode_uniform_bucket %p to %p\n", *p, end); | ||
| 69 | ceph_decode_need(p, end, (1+b->h.size) * sizeof(u32), bad); | ||
| 70 | b->item_weight = ceph_decode_32(p); | ||
| 71 | return 0; | ||
| 72 | bad: | ||
| 73 | return -EINVAL; | ||
| 74 | } | ||
| 75 | |||
| 76 | static int crush_decode_list_bucket(void **p, void *end, | ||
| 77 | struct crush_bucket_list *b) | ||
| 78 | { | ||
| 79 | int j; | ||
| 80 | dout("crush_decode_list_bucket %p to %p\n", *p, end); | ||
| 81 | b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); | ||
| 82 | if (b->item_weights == NULL) | ||
| 83 | return -ENOMEM; | ||
| 84 | b->sum_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); | ||
| 85 | if (b->sum_weights == NULL) | ||
| 86 | return -ENOMEM; | ||
| 87 | ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad); | ||
| 88 | for (j = 0; j < b->h.size; j++) { | ||
| 89 | b->item_weights[j] = ceph_decode_32(p); | ||
| 90 | b->sum_weights[j] = ceph_decode_32(p); | ||
| 91 | } | ||
| 92 | return 0; | ||
| 93 | bad: | ||
| 94 | return -EINVAL; | ||
| 95 | } | ||
| 96 | |||
| 97 | static int crush_decode_tree_bucket(void **p, void *end, | ||
| 98 | struct crush_bucket_tree *b) | ||
| 99 | { | ||
| 100 | int j; | ||
| 101 | dout("crush_decode_tree_bucket %p to %p\n", *p, end); | ||
| 102 | ceph_decode_32_safe(p, end, b->num_nodes, bad); | ||
| 103 | b->node_weights = kcalloc(b->num_nodes, sizeof(u32), GFP_NOFS); | ||
| 104 | if (b->node_weights == NULL) | ||
| 105 | return -ENOMEM; | ||
| 106 | ceph_decode_need(p, end, b->num_nodes * sizeof(u32), bad); | ||
| 107 | for (j = 0; j < b->num_nodes; j++) | ||
| 108 | b->node_weights[j] = ceph_decode_32(p); | ||
| 109 | return 0; | ||
| 110 | bad: | ||
| 111 | return -EINVAL; | ||
| 112 | } | ||
| 113 | |||
| 114 | static int crush_decode_straw_bucket(void **p, void *end, | ||
| 115 | struct crush_bucket_straw *b) | ||
| 116 | { | ||
| 117 | int j; | ||
| 118 | dout("crush_decode_straw_bucket %p to %p\n", *p, end); | ||
| 119 | b->item_weights = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); | ||
| 120 | if (b->item_weights == NULL) | ||
| 121 | return -ENOMEM; | ||
| 122 | b->straws = kcalloc(b->h.size, sizeof(u32), GFP_NOFS); | ||
| 123 | if (b->straws == NULL) | ||
| 124 | return -ENOMEM; | ||
| 125 | ceph_decode_need(p, end, 2 * b->h.size * sizeof(u32), bad); | ||
| 126 | for (j = 0; j < b->h.size; j++) { | ||
| 127 | b->item_weights[j] = ceph_decode_32(p); | ||
| 128 | b->straws[j] = ceph_decode_32(p); | ||
| 129 | } | ||
| 130 | return 0; | ||
| 131 | bad: | ||
| 132 | return -EINVAL; | ||
| 133 | } | ||
| 134 | |||
| 135 | static struct crush_map *crush_decode(void *pbyval, void *end) | ||
| 136 | { | ||
| 137 | struct crush_map *c; | ||
| 138 | int err = -EINVAL; | ||
| 139 | int i, j; | ||
| 140 | void **p = &pbyval; | ||
| 141 | void *start = pbyval; | ||
| 142 | u32 magic; | ||
| 143 | |||
| 144 | dout("crush_decode %p to %p len %d\n", *p, end, (int)(end - *p)); | ||
| 145 | |||
| 146 | c = kzalloc(sizeof(*c), GFP_NOFS); | ||
| 147 | if (c == NULL) | ||
| 148 | return ERR_PTR(-ENOMEM); | ||
| 149 | |||
| 150 | ceph_decode_need(p, end, 4*sizeof(u32), bad); | ||
| 151 | magic = ceph_decode_32(p); | ||
| 152 | if (magic != CRUSH_MAGIC) { | ||
| 153 | pr_err("crush_decode magic %x != current %x\n", | ||
| 154 | (unsigned)magic, (unsigned)CRUSH_MAGIC); | ||
| 155 | goto bad; | ||
| 156 | } | ||
| 157 | c->max_buckets = ceph_decode_32(p); | ||
| 158 | c->max_rules = ceph_decode_32(p); | ||
| 159 | c->max_devices = ceph_decode_32(p); | ||
| 160 | |||
| 161 | c->device_parents = kcalloc(c->max_devices, sizeof(u32), GFP_NOFS); | ||
| 162 | if (c->device_parents == NULL) | ||
| 163 | goto badmem; | ||
| 164 | c->bucket_parents = kcalloc(c->max_buckets, sizeof(u32), GFP_NOFS); | ||
| 165 | if (c->bucket_parents == NULL) | ||
| 166 | goto badmem; | ||
| 167 | |||
| 168 | c->buckets = kcalloc(c->max_buckets, sizeof(*c->buckets), GFP_NOFS); | ||
| 169 | if (c->buckets == NULL) | ||
| 170 | goto badmem; | ||
| 171 | c->rules = kcalloc(c->max_rules, sizeof(*c->rules), GFP_NOFS); | ||
| 172 | if (c->rules == NULL) | ||
| 173 | goto badmem; | ||
| 174 | |||
| 175 | /* buckets */ | ||
| 176 | for (i = 0; i < c->max_buckets; i++) { | ||
| 177 | int size = 0; | ||
| 178 | u32 alg; | ||
| 179 | struct crush_bucket *b; | ||
| 180 | |||
| 181 | ceph_decode_32_safe(p, end, alg, bad); | ||
| 182 | if (alg == 0) { | ||
| 183 | c->buckets[i] = NULL; | ||
| 184 | continue; | ||
| 185 | } | ||
| 186 | dout("crush_decode bucket %d off %x %p to %p\n", | ||
| 187 | i, (int)(*p-start), *p, end); | ||
| 188 | |||
| 189 | switch (alg) { | ||
| 190 | case CRUSH_BUCKET_UNIFORM: | ||
| 191 | size = sizeof(struct crush_bucket_uniform); | ||
| 192 | break; | ||
| 193 | case CRUSH_BUCKET_LIST: | ||
| 194 | size = sizeof(struct crush_bucket_list); | ||
| 195 | break; | ||
| 196 | case CRUSH_BUCKET_TREE: | ||
| 197 | size = sizeof(struct crush_bucket_tree); | ||
| 198 | break; | ||
| 199 | case CRUSH_BUCKET_STRAW: | ||
| 200 | size = sizeof(struct crush_bucket_straw); | ||
| 201 | break; | ||
| 202 | default: | ||
| 203 | err = -EINVAL; | ||
| 204 | goto bad; | ||
| 205 | } | ||
| 206 | BUG_ON(size == 0); | ||
| 207 | b = c->buckets[i] = kzalloc(size, GFP_NOFS); | ||
| 208 | if (b == NULL) | ||
| 209 | goto badmem; | ||
| 210 | |||
| 211 | ceph_decode_need(p, end, 4*sizeof(u32), bad); | ||
| 212 | b->id = ceph_decode_32(p); | ||
| 213 | b->type = ceph_decode_16(p); | ||
| 214 | b->alg = ceph_decode_8(p); | ||
| 215 | b->hash = ceph_decode_8(p); | ||
| 216 | b->weight = ceph_decode_32(p); | ||
| 217 | b->size = ceph_decode_32(p); | ||
| 218 | |||
| 219 | dout("crush_decode bucket size %d off %x %p to %p\n", | ||
| 220 | b->size, (int)(*p-start), *p, end); | ||
| 221 | |||
| 222 | b->items = kcalloc(b->size, sizeof(__s32), GFP_NOFS); | ||
| 223 | if (b->items == NULL) | ||
| 224 | goto badmem; | ||
| 225 | b->perm = kcalloc(b->size, sizeof(u32), GFP_NOFS); | ||
| 226 | if (b->perm == NULL) | ||
| 227 | goto badmem; | ||
| 228 | b->perm_n = 0; | ||
| 229 | |||
| 230 | ceph_decode_need(p, end, b->size*sizeof(u32), bad); | ||
| 231 | for (j = 0; j < b->size; j++) | ||
| 232 | b->items[j] = ceph_decode_32(p); | ||
| 233 | |||
| 234 | switch (b->alg) { | ||
| 235 | case CRUSH_BUCKET_UNIFORM: | ||
| 236 | err = crush_decode_uniform_bucket(p, end, | ||
| 237 | (struct crush_bucket_uniform *)b); | ||
| 238 | if (err < 0) | ||
| 239 | goto bad; | ||
| 240 | break; | ||
| 241 | case CRUSH_BUCKET_LIST: | ||
| 242 | err = crush_decode_list_bucket(p, end, | ||
| 243 | (struct crush_bucket_list *)b); | ||
| 244 | if (err < 0) | ||
| 245 | goto bad; | ||
| 246 | break; | ||
| 247 | case CRUSH_BUCKET_TREE: | ||
| 248 | err = crush_decode_tree_bucket(p, end, | ||
| 249 | (struct crush_bucket_tree *)b); | ||
| 250 | if (err < 0) | ||
| 251 | goto bad; | ||
| 252 | break; | ||
| 253 | case CRUSH_BUCKET_STRAW: | ||
| 254 | err = crush_decode_straw_bucket(p, end, | ||
| 255 | (struct crush_bucket_straw *)b); | ||
| 256 | if (err < 0) | ||
| 257 | goto bad; | ||
| 258 | break; | ||
| 259 | } | ||
| 260 | } | ||
| 261 | |||
| 262 | /* rules */ | ||
| 263 | dout("rule vec is %p\n", c->rules); | ||
| 264 | for (i = 0; i < c->max_rules; i++) { | ||
| 265 | u32 yes; | ||
| 266 | struct crush_rule *r; | ||
| 267 | |||
| 268 | ceph_decode_32_safe(p, end, yes, bad); | ||
| 269 | if (!yes) { | ||
| 270 | dout("crush_decode NO rule %d off %x %p to %p\n", | ||
| 271 | i, (int)(*p-start), *p, end); | ||
| 272 | c->rules[i] = NULL; | ||
| 273 | continue; | ||
| 274 | } | ||
| 275 | |||
| 276 | dout("crush_decode rule %d off %x %p to %p\n", | ||
| 277 | i, (int)(*p-start), *p, end); | ||
| 278 | |||
| 279 | /* len */ | ||
| 280 | ceph_decode_32_safe(p, end, yes, bad); | ||
| 281 | #if BITS_PER_LONG == 32 | ||
| 282 | err = -EINVAL; | ||
| 283 | if (yes > ULONG_MAX / sizeof(struct crush_rule_step)) | ||
| 284 | goto bad; | ||
| 285 | #endif | ||
| 286 | r = c->rules[i] = kmalloc(sizeof(*r) + | ||
| 287 | yes*sizeof(struct crush_rule_step), | ||
| 288 | GFP_NOFS); | ||
| 289 | if (r == NULL) | ||
| 290 | goto badmem; | ||
| 291 | dout(" rule %d is at %p\n", i, r); | ||
| 292 | r->len = yes; | ||
| 293 | ceph_decode_copy_safe(p, end, &r->mask, 4, bad); /* 4 u8's */ | ||
| 294 | ceph_decode_need(p, end, r->len*3*sizeof(u32), bad); | ||
| 295 | for (j = 0; j < r->len; j++) { | ||
| 296 | r->steps[j].op = ceph_decode_32(p); | ||
| 297 | r->steps[j].arg1 = ceph_decode_32(p); | ||
| 298 | r->steps[j].arg2 = ceph_decode_32(p); | ||
| 299 | } | ||
| 300 | } | ||
| 301 | |||
| 302 | /* ignore trailing name maps. */ | ||
| 303 | |||
| 304 | dout("crush_decode success\n"); | ||
| 305 | return c; | ||
| 306 | |||
| 307 | badmem: | ||
| 308 | err = -ENOMEM; | ||
| 309 | bad: | ||
| 310 | dout("crush_decode fail %d\n", err); | ||
| 311 | crush_destroy(c); | ||
| 312 | return ERR_PTR(err); | ||
| 313 | } | ||
| 314 | |||
| 315 | |||
| 316 | /* | ||
| 317 | * osd map | ||
| 318 | */ | ||
| 319 | void ceph_osdmap_destroy(struct ceph_osdmap *map) | ||
| 320 | { | ||
| 321 | dout("osdmap_destroy %p\n", map); | ||
| 322 | if (map->crush) | ||
| 323 | crush_destroy(map->crush); | ||
| 324 | while (!RB_EMPTY_ROOT(&map->pg_temp)) { | ||
| 325 | struct ceph_pg_mapping *pg = | ||
| 326 | rb_entry(rb_first(&map->pg_temp), | ||
| 327 | struct ceph_pg_mapping, node); | ||
| 328 | rb_erase(&pg->node, &map->pg_temp); | ||
| 329 | kfree(pg); | ||
| 330 | } | ||
| 331 | while (!RB_EMPTY_ROOT(&map->pg_pools)) { | ||
| 332 | struct ceph_pg_pool_info *pi = | ||
| 333 | rb_entry(rb_first(&map->pg_pools), | ||
| 334 | struct ceph_pg_pool_info, node); | ||
| 335 | rb_erase(&pi->node, &map->pg_pools); | ||
| 336 | kfree(pi); | ||
| 337 | } | ||
| 338 | kfree(map->osd_state); | ||
| 339 | kfree(map->osd_weight); | ||
| 340 | kfree(map->osd_addr); | ||
| 341 | kfree(map); | ||
| 342 | } | ||
| 343 | |||
| 344 | /* | ||
| 345 | * adjust max osd value. reallocate arrays. | ||
| 346 | */ | ||
| 347 | static int osdmap_set_max_osd(struct ceph_osdmap *map, int max) | ||
| 348 | { | ||
| 349 | u8 *state; | ||
| 350 | struct ceph_entity_addr *addr; | ||
| 351 | u32 *weight; | ||
| 352 | |||
| 353 | state = kcalloc(max, sizeof(*state), GFP_NOFS); | ||
| 354 | addr = kcalloc(max, sizeof(*addr), GFP_NOFS); | ||
| 355 | weight = kcalloc(max, sizeof(*weight), GFP_NOFS); | ||
| 356 | if (state == NULL || addr == NULL || weight == NULL) { | ||
| 357 | kfree(state); | ||
| 358 | kfree(addr); | ||
| 359 | kfree(weight); | ||
| 360 | return -ENOMEM; | ||
| 361 | } | ||
| 362 | |||
| 363 | /* copy old? */ | ||
| 364 | if (map->osd_state) { | ||
| 365 | memcpy(state, map->osd_state, map->max_osd*sizeof(*state)); | ||
| 366 | memcpy(addr, map->osd_addr, map->max_osd*sizeof(*addr)); | ||
| 367 | memcpy(weight, map->osd_weight, map->max_osd*sizeof(*weight)); | ||
| 368 | kfree(map->osd_state); | ||
| 369 | kfree(map->osd_addr); | ||
| 370 | kfree(map->osd_weight); | ||
| 371 | } | ||
| 372 | |||
| 373 | map->osd_state = state; | ||
| 374 | map->osd_weight = weight; | ||
| 375 | map->osd_addr = addr; | ||
| 376 | map->max_osd = max; | ||
| 377 | return 0; | ||
| 378 | } | ||
| 379 | |||
| 380 | /* | ||
| 381 | * rbtree of pg_mapping for handling pg_temp (explicit mapping of pgid | ||
| 382 | * to a set of osds) | ||
| 383 | */ | ||
| 384 | static int pgid_cmp(struct ceph_pg l, struct ceph_pg r) | ||
| 385 | { | ||
| 386 | u64 a = *(u64 *)&l; | ||
| 387 | u64 b = *(u64 *)&r; | ||
| 388 | |||
| 389 | if (a < b) | ||
| 390 | return -1; | ||
| 391 | if (a > b) | ||
| 392 | return 1; | ||
| 393 | return 0; | ||
| 394 | } | ||
| 395 | |||
| 396 | static int __insert_pg_mapping(struct ceph_pg_mapping *new, | ||
| 397 | struct rb_root *root) | ||
| 398 | { | ||
| 399 | struct rb_node **p = &root->rb_node; | ||
| 400 | struct rb_node *parent = NULL; | ||
| 401 | struct ceph_pg_mapping *pg = NULL; | ||
| 402 | int c; | ||
| 403 | |||
| 404 | while (*p) { | ||
| 405 | parent = *p; | ||
| 406 | pg = rb_entry(parent, struct ceph_pg_mapping, node); | ||
| 407 | c = pgid_cmp(new->pgid, pg->pgid); | ||
| 408 | if (c < 0) | ||
| 409 | p = &(*p)->rb_left; | ||
| 410 | else if (c > 0) | ||
| 411 | p = &(*p)->rb_right; | ||
| 412 | else | ||
| 413 | return -EEXIST; | ||
| 414 | } | ||
| 415 | |||
| 416 | rb_link_node(&new->node, parent, p); | ||
| 417 | rb_insert_color(&new->node, root); | ||
| 418 | return 0; | ||
| 419 | } | ||
| 420 | |||
| 421 | static struct ceph_pg_mapping *__lookup_pg_mapping(struct rb_root *root, | ||
| 422 | struct ceph_pg pgid) | ||
| 423 | { | ||
| 424 | struct rb_node *n = root->rb_node; | ||
| 425 | struct ceph_pg_mapping *pg; | ||
| 426 | int c; | ||
| 427 | |||
| 428 | while (n) { | ||
| 429 | pg = rb_entry(n, struct ceph_pg_mapping, node); | ||
| 430 | c = pgid_cmp(pgid, pg->pgid); | ||
| 431 | if (c < 0) | ||
| 432 | n = n->rb_left; | ||
| 433 | else if (c > 0) | ||
| 434 | n = n->rb_right; | ||
| 435 | else | ||
| 436 | return pg; | ||
| 437 | } | ||
| 438 | return NULL; | ||
| 439 | } | ||
| 440 | |||
| 441 | /* | ||
| 442 | * rbtree of pg pool info | ||
| 443 | */ | ||
| 444 | static int __insert_pg_pool(struct rb_root *root, struct ceph_pg_pool_info *new) | ||
| 445 | { | ||
| 446 | struct rb_node **p = &root->rb_node; | ||
| 447 | struct rb_node *parent = NULL; | ||
| 448 | struct ceph_pg_pool_info *pi = NULL; | ||
| 449 | |||
| 450 | while (*p) { | ||
| 451 | parent = *p; | ||
| 452 | pi = rb_entry(parent, struct ceph_pg_pool_info, node); | ||
| 453 | if (new->id < pi->id) | ||
| 454 | p = &(*p)->rb_left; | ||
| 455 | else if (new->id > pi->id) | ||
| 456 | p = &(*p)->rb_right; | ||
| 457 | else | ||
| 458 | return -EEXIST; | ||
| 459 | } | ||
| 460 | |||
| 461 | rb_link_node(&new->node, parent, p); | ||
| 462 | rb_insert_color(&new->node, root); | ||
| 463 | return 0; | ||
| 464 | } | ||
| 465 | |||
| 466 | static struct ceph_pg_pool_info *__lookup_pg_pool(struct rb_root *root, int id) | ||
| 467 | { | ||
| 468 | struct ceph_pg_pool_info *pi; | ||
| 469 | struct rb_node *n = root->rb_node; | ||
| 470 | |||
| 471 | while (n) { | ||
| 472 | pi = rb_entry(n, struct ceph_pg_pool_info, node); | ||
| 473 | if (id < pi->id) | ||
| 474 | n = n->rb_left; | ||
| 475 | else if (id > pi->id) | ||
| 476 | n = n->rb_right; | ||
| 477 | else | ||
| 478 | return pi; | ||
| 479 | } | ||
| 480 | return NULL; | ||
| 481 | } | ||
| 482 | |||
| 483 | void __decode_pool(void **p, struct ceph_pg_pool_info *pi) | ||
| 484 | { | ||
| 485 | ceph_decode_copy(p, &pi->v, sizeof(pi->v)); | ||
| 486 | calc_pg_masks(pi); | ||
| 487 | *p += le32_to_cpu(pi->v.num_snaps) * sizeof(u64); | ||
| 488 | *p += le32_to_cpu(pi->v.num_removed_snap_intervals) * sizeof(u64) * 2; | ||
| 489 | } | ||
| 490 | |||
| 491 | /* | ||
| 492 | * decode a full map. | ||
| 493 | */ | ||
| 494 | struct ceph_osdmap *osdmap_decode(void **p, void *end) | ||
| 495 | { | ||
| 496 | struct ceph_osdmap *map; | ||
| 497 | u16 version; | ||
| 498 | u32 len, max, i; | ||
| 499 | u8 ev; | ||
| 500 | int err = -EINVAL; | ||
| 501 | void *start = *p; | ||
| 502 | struct ceph_pg_pool_info *pi; | ||
| 503 | |||
| 504 | dout("osdmap_decode %p to %p len %d\n", *p, end, (int)(end - *p)); | ||
| 505 | |||
| 506 | map = kzalloc(sizeof(*map), GFP_NOFS); | ||
| 507 | if (map == NULL) | ||
| 508 | return ERR_PTR(-ENOMEM); | ||
| 509 | map->pg_temp = RB_ROOT; | ||
| 510 | |||
| 511 | ceph_decode_16_safe(p, end, version, bad); | ||
| 512 | if (version > CEPH_OSDMAP_VERSION) { | ||
| 513 | pr_warning("got unknown v %d > %d of osdmap\n", version, | ||
| 514 | CEPH_OSDMAP_VERSION); | ||
| 515 | goto bad; | ||
| 516 | } | ||
| 517 | |||
| 518 | ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad); | ||
| 519 | ceph_decode_copy(p, &map->fsid, sizeof(map->fsid)); | ||
| 520 | map->epoch = ceph_decode_32(p); | ||
| 521 | ceph_decode_copy(p, &map->created, sizeof(map->created)); | ||
| 522 | ceph_decode_copy(p, &map->modified, sizeof(map->modified)); | ||
| 523 | |||
| 524 | ceph_decode_32_safe(p, end, max, bad); | ||
| 525 | while (max--) { | ||
| 526 | ceph_decode_need(p, end, 4 + 1 + sizeof(pi->v), bad); | ||
| 527 | pi = kmalloc(sizeof(*pi), GFP_NOFS); | ||
| 528 | if (!pi) | ||
| 529 | goto bad; | ||
| 530 | pi->id = ceph_decode_32(p); | ||
| 531 | ev = ceph_decode_8(p); /* encoding version */ | ||
| 532 | if (ev > CEPH_PG_POOL_VERSION) { | ||
| 533 | pr_warning("got unknown v %d > %d of ceph_pg_pool\n", | ||
| 534 | ev, CEPH_PG_POOL_VERSION); | ||
| 535 | goto bad; | ||
| 536 | } | ||
| 537 | __decode_pool(p, pi); | ||
| 538 | __insert_pg_pool(&map->pg_pools, pi); | ||
| 539 | } | ||
| 540 | ceph_decode_32_safe(p, end, map->pool_max, bad); | ||
| 541 | |||
| 542 | ceph_decode_32_safe(p, end, map->flags, bad); | ||
| 543 | |||
| 544 | max = ceph_decode_32(p); | ||
| 545 | |||
| 546 | /* (re)alloc osd arrays */ | ||
| 547 | err = osdmap_set_max_osd(map, max); | ||
| 548 | if (err < 0) | ||
| 549 | goto bad; | ||
| 550 | dout("osdmap_decode max_osd = %d\n", map->max_osd); | ||
| 551 | |||
| 552 | /* osds */ | ||
| 553 | err = -EINVAL; | ||
| 554 | ceph_decode_need(p, end, 3*sizeof(u32) + | ||
| 555 | map->max_osd*(1 + sizeof(*map->osd_weight) + | ||
| 556 | sizeof(*map->osd_addr)), bad); | ||
| 557 | *p += 4; /* skip length field (should match max) */ | ||
| 558 | ceph_decode_copy(p, map->osd_state, map->max_osd); | ||
| 559 | |||
| 560 | *p += 4; /* skip length field (should match max) */ | ||
| 561 | for (i = 0; i < map->max_osd; i++) | ||
| 562 | map->osd_weight[i] = ceph_decode_32(p); | ||
| 563 | |||
| 564 | *p += 4; /* skip length field (should match max) */ | ||
| 565 | ceph_decode_copy(p, map->osd_addr, map->max_osd*sizeof(*map->osd_addr)); | ||
| 566 | for (i = 0; i < map->max_osd; i++) | ||
| 567 | ceph_decode_addr(&map->osd_addr[i]); | ||
| 568 | |||
| 569 | /* pg_temp */ | ||
| 570 | ceph_decode_32_safe(p, end, len, bad); | ||
| 571 | for (i = 0; i < len; i++) { | ||
| 572 | int n, j; | ||
| 573 | struct ceph_pg pgid; | ||
| 574 | struct ceph_pg_mapping *pg; | ||
| 575 | |||
| 576 | ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad); | ||
| 577 | ceph_decode_copy(p, &pgid, sizeof(pgid)); | ||
| 578 | n = ceph_decode_32(p); | ||
| 579 | ceph_decode_need(p, end, n * sizeof(u32), bad); | ||
| 580 | err = -ENOMEM; | ||
| 581 | pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS); | ||
| 582 | if (!pg) | ||
| 583 | goto bad; | ||
| 584 | pg->pgid = pgid; | ||
| 585 | pg->len = n; | ||
| 586 | for (j = 0; j < n; j++) | ||
| 587 | pg->osds[j] = ceph_decode_32(p); | ||
| 588 | |||
| 589 | err = __insert_pg_mapping(pg, &map->pg_temp); | ||
| 590 | if (err) | ||
| 591 | goto bad; | ||
| 592 | dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, len); | ||
| 593 | } | ||
| 594 | |||
| 595 | /* crush */ | ||
| 596 | ceph_decode_32_safe(p, end, len, bad); | ||
| 597 | dout("osdmap_decode crush len %d from off 0x%x\n", len, | ||
| 598 | (int)(*p - start)); | ||
| 599 | ceph_decode_need(p, end, len, bad); | ||
| 600 | map->crush = crush_decode(*p, end); | ||
| 601 | *p += len; | ||
| 602 | if (IS_ERR(map->crush)) { | ||
| 603 | err = PTR_ERR(map->crush); | ||
| 604 | map->crush = NULL; | ||
| 605 | goto bad; | ||
| 606 | } | ||
| 607 | |||
| 608 | /* ignore the rest of the map */ | ||
| 609 | *p = end; | ||
| 610 | |||
| 611 | dout("osdmap_decode done %p %p\n", *p, end); | ||
| 612 | return map; | ||
| 613 | |||
| 614 | bad: | ||
| 615 | dout("osdmap_decode fail\n"); | ||
| 616 | ceph_osdmap_destroy(map); | ||
| 617 | return ERR_PTR(err); | ||
| 618 | } | ||
| 619 | |||
| 620 | /* | ||
| 621 | * decode and apply an incremental map update. | ||
| 622 | */ | ||
| 623 | struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end, | ||
| 624 | struct ceph_osdmap *map, | ||
| 625 | struct ceph_messenger *msgr) | ||
| 626 | { | ||
| 627 | struct crush_map *newcrush = NULL; | ||
| 628 | struct ceph_fsid fsid; | ||
| 629 | u32 epoch = 0; | ||
| 630 | struct ceph_timespec modified; | ||
| 631 | u32 len, pool; | ||
| 632 | __s32 new_pool_max, new_flags, max; | ||
| 633 | void *start = *p; | ||
| 634 | int err = -EINVAL; | ||
| 635 | u16 version; | ||
| 636 | struct rb_node *rbp; | ||
| 637 | |||
| 638 | ceph_decode_16_safe(p, end, version, bad); | ||
| 639 | if (version > CEPH_OSDMAP_INC_VERSION) { | ||
| 640 | pr_warning("got unknown v %d > %d of inc osdmap\n", version, | ||
| 641 | CEPH_OSDMAP_INC_VERSION); | ||
| 642 | goto bad; | ||
| 643 | } | ||
| 644 | |||
| 645 | ceph_decode_need(p, end, sizeof(fsid)+sizeof(modified)+2*sizeof(u32), | ||
| 646 | bad); | ||
| 647 | ceph_decode_copy(p, &fsid, sizeof(fsid)); | ||
| 648 | epoch = ceph_decode_32(p); | ||
| 649 | BUG_ON(epoch != map->epoch+1); | ||
| 650 | ceph_decode_copy(p, &modified, sizeof(modified)); | ||
| 651 | new_pool_max = ceph_decode_32(p); | ||
| 652 | new_flags = ceph_decode_32(p); | ||
| 653 | |||
| 654 | /* full map? */ | ||
| 655 | ceph_decode_32_safe(p, end, len, bad); | ||
| 656 | if (len > 0) { | ||
| 657 | dout("apply_incremental full map len %d, %p to %p\n", | ||
| 658 | len, *p, end); | ||
| 659 | return osdmap_decode(p, min(*p+len, end)); | ||
| 660 | } | ||
| 661 | |||
| 662 | /* new crush? */ | ||
| 663 | ceph_decode_32_safe(p, end, len, bad); | ||
| 664 | if (len > 0) { | ||
| 665 | dout("apply_incremental new crush map len %d, %p to %p\n", | ||
| 666 | len, *p, end); | ||
| 667 | newcrush = crush_decode(*p, min(*p+len, end)); | ||
| 668 | if (IS_ERR(newcrush)) | ||
| 669 | return ERR_PTR(PTR_ERR(newcrush)); | ||
| 670 | } | ||
| 671 | |||
| 672 | /* new flags? */ | ||
| 673 | if (new_flags >= 0) | ||
| 674 | map->flags = new_flags; | ||
| 675 | if (new_pool_max >= 0) | ||
| 676 | map->pool_max = new_pool_max; | ||
| 677 | |||
| 678 | ceph_decode_need(p, end, 5*sizeof(u32), bad); | ||
| 679 | |||
| 680 | /* new max? */ | ||
| 681 | max = ceph_decode_32(p); | ||
| 682 | if (max >= 0) { | ||
| 683 | err = osdmap_set_max_osd(map, max); | ||
| 684 | if (err < 0) | ||
| 685 | goto bad; | ||
| 686 | } | ||
| 687 | |||
| 688 | map->epoch++; | ||
| 689 | map->modified = map->modified; | ||
| 690 | if (newcrush) { | ||
| 691 | if (map->crush) | ||
| 692 | crush_destroy(map->crush); | ||
| 693 | map->crush = newcrush; | ||
| 694 | newcrush = NULL; | ||
| 695 | } | ||
| 696 | |||
| 697 | /* new_pool */ | ||
| 698 | ceph_decode_32_safe(p, end, len, bad); | ||
| 699 | while (len--) { | ||
| 700 | __u8 ev; | ||
| 701 | struct ceph_pg_pool_info *pi; | ||
| 702 | |||
| 703 | ceph_decode_32_safe(p, end, pool, bad); | ||
| 704 | ceph_decode_need(p, end, 1 + sizeof(pi->v), bad); | ||
| 705 | ev = ceph_decode_8(p); /* encoding version */ | ||
| 706 | if (ev > CEPH_PG_POOL_VERSION) { | ||
| 707 | pr_warning("got unknown v %d > %d of ceph_pg_pool\n", | ||
| 708 | ev, CEPH_PG_POOL_VERSION); | ||
| 709 | goto bad; | ||
| 710 | } | ||
| 711 | pi = __lookup_pg_pool(&map->pg_pools, pool); | ||
| 712 | if (!pi) { | ||
| 713 | pi = kmalloc(sizeof(*pi), GFP_NOFS); | ||
| 714 | if (!pi) { | ||
| 715 | err = -ENOMEM; | ||
| 716 | goto bad; | ||
| 717 | } | ||
| 718 | pi->id = pool; | ||
| 719 | __insert_pg_pool(&map->pg_pools, pi); | ||
| 720 | } | ||
| 721 | __decode_pool(p, pi); | ||
| 722 | } | ||
| 723 | |||
| 724 | /* old_pool */ | ||
| 725 | ceph_decode_32_safe(p, end, len, bad); | ||
| 726 | while (len--) { | ||
| 727 | struct ceph_pg_pool_info *pi; | ||
| 728 | |||
| 729 | ceph_decode_32_safe(p, end, pool, bad); | ||
| 730 | pi = __lookup_pg_pool(&map->pg_pools, pool); | ||
| 731 | if (pi) { | ||
| 732 | rb_erase(&pi->node, &map->pg_pools); | ||
| 733 | kfree(pi); | ||
| 734 | } | ||
| 735 | } | ||
| 736 | |||
| 737 | /* new_up */ | ||
| 738 | err = -EINVAL; | ||
| 739 | ceph_decode_32_safe(p, end, len, bad); | ||
| 740 | while (len--) { | ||
| 741 | u32 osd; | ||
| 742 | struct ceph_entity_addr addr; | ||
| 743 | ceph_decode_32_safe(p, end, osd, bad); | ||
| 744 | ceph_decode_copy_safe(p, end, &addr, sizeof(addr), bad); | ||
| 745 | ceph_decode_addr(&addr); | ||
| 746 | pr_info("osd%d up\n", osd); | ||
| 747 | BUG_ON(osd >= map->max_osd); | ||
| 748 | map->osd_state[osd] |= CEPH_OSD_UP; | ||
| 749 | map->osd_addr[osd] = addr; | ||
| 750 | } | ||
| 751 | |||
| 752 | /* new_down */ | ||
| 753 | ceph_decode_32_safe(p, end, len, bad); | ||
| 754 | while (len--) { | ||
| 755 | u32 osd; | ||
| 756 | ceph_decode_32_safe(p, end, osd, bad); | ||
| 757 | (*p)++; /* clean flag */ | ||
| 758 | pr_info("osd%d down\n", osd); | ||
| 759 | if (osd < map->max_osd) | ||
| 760 | map->osd_state[osd] &= ~CEPH_OSD_UP; | ||
| 761 | } | ||
| 762 | |||
| 763 | /* new_weight */ | ||
| 764 | ceph_decode_32_safe(p, end, len, bad); | ||
| 765 | while (len--) { | ||
| 766 | u32 osd, off; | ||
| 767 | ceph_decode_need(p, end, sizeof(u32)*2, bad); | ||
| 768 | osd = ceph_decode_32(p); | ||
| 769 | off = ceph_decode_32(p); | ||
| 770 | pr_info("osd%d weight 0x%x %s\n", osd, off, | ||
| 771 | off == CEPH_OSD_IN ? "(in)" : | ||
| 772 | (off == CEPH_OSD_OUT ? "(out)" : "")); | ||
| 773 | if (osd < map->max_osd) | ||
| 774 | map->osd_weight[osd] = off; | ||
| 775 | } | ||
| 776 | |||
| 777 | /* new_pg_temp */ | ||
| 778 | rbp = rb_first(&map->pg_temp); | ||
| 779 | ceph_decode_32_safe(p, end, len, bad); | ||
| 780 | while (len--) { | ||
| 781 | struct ceph_pg_mapping *pg; | ||
| 782 | int j; | ||
| 783 | struct ceph_pg pgid; | ||
| 784 | u32 pglen; | ||
| 785 | ceph_decode_need(p, end, sizeof(u64) + sizeof(u32), bad); | ||
| 786 | ceph_decode_copy(p, &pgid, sizeof(pgid)); | ||
| 787 | pglen = ceph_decode_32(p); | ||
| 788 | |||
| 789 | /* remove any? */ | ||
| 790 | while (rbp && pgid_cmp(rb_entry(rbp, struct ceph_pg_mapping, | ||
| 791 | node)->pgid, pgid) <= 0) { | ||
| 792 | struct rb_node *cur = rbp; | ||
| 793 | rbp = rb_next(rbp); | ||
| 794 | dout(" removed pg_temp %llx\n", | ||
| 795 | *(u64 *)&rb_entry(cur, struct ceph_pg_mapping, | ||
| 796 | node)->pgid); | ||
| 797 | rb_erase(cur, &map->pg_temp); | ||
| 798 | } | ||
| 799 | |||
| 800 | if (pglen) { | ||
| 801 | /* insert */ | ||
| 802 | ceph_decode_need(p, end, pglen*sizeof(u32), bad); | ||
| 803 | pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS); | ||
| 804 | if (!pg) { | ||
| 805 | err = -ENOMEM; | ||
| 806 | goto bad; | ||
| 807 | } | ||
| 808 | pg->pgid = pgid; | ||
| 809 | pg->len = pglen; | ||
| 810 | for (j = 0; j < pglen; j++) | ||
| 811 | pg->osds[j] = ceph_decode_32(p); | ||
| 812 | err = __insert_pg_mapping(pg, &map->pg_temp); | ||
| 813 | if (err) | ||
| 814 | goto bad; | ||
| 815 | dout(" added pg_temp %llx len %d\n", *(u64 *)&pgid, | ||
| 816 | pglen); | ||
| 817 | } | ||
| 818 | } | ||
| 819 | while (rbp) { | ||
| 820 | struct rb_node *cur = rbp; | ||
| 821 | rbp = rb_next(rbp); | ||
| 822 | dout(" removed pg_temp %llx\n", | ||
| 823 | *(u64 *)&rb_entry(cur, struct ceph_pg_mapping, | ||
| 824 | node)->pgid); | ||
| 825 | rb_erase(cur, &map->pg_temp); | ||
| 826 | } | ||
| 827 | |||
| 828 | /* ignore the rest */ | ||
| 829 | *p = end; | ||
| 830 | return map; | ||
| 831 | |||
| 832 | bad: | ||
| 833 | pr_err("corrupt inc osdmap epoch %d off %d (%p of %p-%p)\n", | ||
| 834 | epoch, (int)(*p - start), *p, start, end); | ||
| 835 | print_hex_dump(KERN_DEBUG, "osdmap: ", | ||
| 836 | DUMP_PREFIX_OFFSET, 16, 1, | ||
| 837 | start, end - start, true); | ||
| 838 | if (newcrush) | ||
| 839 | crush_destroy(newcrush); | ||
| 840 | return ERR_PTR(err); | ||
| 841 | } | ||
| 842 | |||
| 843 | |||
| 844 | |||
| 845 | |||
| 846 | /* | ||
| 847 | * calculate file layout from given offset, length. | ||
| 848 | * fill in correct oid, logical length, and object extent | ||
| 849 | * offset, length. | ||
| 850 | * | ||
| 851 | * for now, we write only a single su, until we can | ||
| 852 | * pass a stride back to the caller. | ||
| 853 | */ | ||
| 854 | void ceph_calc_file_object_mapping(struct ceph_file_layout *layout, | ||
| 855 | u64 off, u64 *plen, | ||
| 856 | u64 *ono, | ||
| 857 | u64 *oxoff, u64 *oxlen) | ||
| 858 | { | ||
| 859 | u32 osize = le32_to_cpu(layout->fl_object_size); | ||
| 860 | u32 su = le32_to_cpu(layout->fl_stripe_unit); | ||
| 861 | u32 sc = le32_to_cpu(layout->fl_stripe_count); | ||
| 862 | u32 bl, stripeno, stripepos, objsetno; | ||
| 863 | u32 su_per_object; | ||
| 864 | u64 t, su_offset; | ||
| 865 | |||
| 866 | dout("mapping %llu~%llu osize %u fl_su %u\n", off, *plen, | ||
| 867 | osize, su); | ||
| 868 | su_per_object = osize / su; | ||
| 869 | dout("osize %u / su %u = su_per_object %u\n", osize, su, | ||
| 870 | su_per_object); | ||
| 871 | |||
| 872 | BUG_ON((su & ~PAGE_MASK) != 0); | ||
| 873 | /* bl = *off / su; */ | ||
| 874 | t = off; | ||
| 875 | do_div(t, su); | ||
| 876 | bl = t; | ||
| 877 | dout("off %llu / su %u = bl %u\n", off, su, bl); | ||
| 878 | |||
| 879 | stripeno = bl / sc; | ||
| 880 | stripepos = bl % sc; | ||
| 881 | objsetno = stripeno / su_per_object; | ||
| 882 | |||
| 883 | *ono = objsetno * sc + stripepos; | ||
| 884 | dout("objset %u * sc %u = ono %u\n", objsetno, sc, (unsigned)*ono); | ||
| 885 | |||
| 886 | /* *oxoff = *off % layout->fl_stripe_unit; # offset in su */ | ||
| 887 | t = off; | ||
| 888 | su_offset = do_div(t, su); | ||
| 889 | *oxoff = su_offset + (stripeno % su_per_object) * su; | ||
| 890 | |||
| 891 | /* | ||
| 892 | * Calculate the length of the extent being written to the selected | ||
| 893 | * object. This is the minimum of the full length requested (plen) or | ||
| 894 | * the remainder of the current stripe being written to. | ||
| 895 | */ | ||
| 896 | *oxlen = min_t(u64, *plen, su - su_offset); | ||
| 897 | *plen = *oxlen; | ||
| 898 | |||
| 899 | dout(" obj extent %llu~%llu\n", *oxoff, *oxlen); | ||
| 900 | } | ||
| 901 | |||
| 902 | /* | ||
| 903 | * calculate an object layout (i.e. pgid) from an oid, | ||
| 904 | * file_layout, and osdmap | ||
| 905 | */ | ||
| 906 | int ceph_calc_object_layout(struct ceph_object_layout *ol, | ||
| 907 | const char *oid, | ||
| 908 | struct ceph_file_layout *fl, | ||
| 909 | struct ceph_osdmap *osdmap) | ||
| 910 | { | ||
| 911 | unsigned num, num_mask; | ||
| 912 | struct ceph_pg pgid; | ||
| 913 | s32 preferred = (s32)le32_to_cpu(fl->fl_pg_preferred); | ||
| 914 | int poolid = le32_to_cpu(fl->fl_pg_pool); | ||
| 915 | struct ceph_pg_pool_info *pool; | ||
| 916 | unsigned ps; | ||
| 917 | |||
| 918 | BUG_ON(!osdmap); | ||
| 919 | |||
| 920 | pool = __lookup_pg_pool(&osdmap->pg_pools, poolid); | ||
| 921 | if (!pool) | ||
| 922 | return -EIO; | ||
| 923 | ps = ceph_str_hash(pool->v.object_hash, oid, strlen(oid)); | ||
| 924 | if (preferred >= 0) { | ||
| 925 | ps += preferred; | ||
| 926 | num = le32_to_cpu(pool->v.lpg_num); | ||
| 927 | num_mask = pool->lpg_num_mask; | ||
| 928 | } else { | ||
| 929 | num = le32_to_cpu(pool->v.pg_num); | ||
| 930 | num_mask = pool->pg_num_mask; | ||
| 931 | } | ||
| 932 | |||
| 933 | pgid.ps = cpu_to_le16(ps); | ||
| 934 | pgid.preferred = cpu_to_le16(preferred); | ||
| 935 | pgid.pool = fl->fl_pg_pool; | ||
| 936 | if (preferred >= 0) | ||
| 937 | dout("calc_object_layout '%s' pgid %d.%xp%d\n", oid, poolid, ps, | ||
| 938 | (int)preferred); | ||
| 939 | else | ||
| 940 | dout("calc_object_layout '%s' pgid %d.%x\n", oid, poolid, ps); | ||
| 941 | |||
| 942 | ol->ol_pgid = pgid; | ||
| 943 | ol->ol_stripe_unit = fl->fl_object_stripe_unit; | ||
| 944 | return 0; | ||
| 945 | } | ||
| 946 | |||
| 947 | /* | ||
| 948 | * Calculate raw osd vector for the given pgid. Return pointer to osd | ||
| 949 | * array, or NULL on failure. | ||
| 950 | */ | ||
| 951 | static int *calc_pg_raw(struct ceph_osdmap *osdmap, struct ceph_pg pgid, | ||
| 952 | int *osds, int *num) | ||
| 953 | { | ||
| 954 | struct ceph_pg_mapping *pg; | ||
| 955 | struct ceph_pg_pool_info *pool; | ||
| 956 | int ruleno; | ||
| 957 | unsigned poolid, ps, pps; | ||
| 958 | int preferred; | ||
| 959 | |||
| 960 | /* pg_temp? */ | ||
| 961 | pg = __lookup_pg_mapping(&osdmap->pg_temp, pgid); | ||
| 962 | if (pg) { | ||
| 963 | *num = pg->len; | ||
| 964 | return pg->osds; | ||
| 965 | } | ||
| 966 | |||
| 967 | /* crush */ | ||
| 968 | poolid = le32_to_cpu(pgid.pool); | ||
| 969 | ps = le16_to_cpu(pgid.ps); | ||
| 970 | preferred = (s16)le16_to_cpu(pgid.preferred); | ||
| 971 | |||
| 972 | /* don't forcefeed bad device ids to crush */ | ||
| 973 | if (preferred >= osdmap->max_osd || | ||
| 974 | preferred >= osdmap->crush->max_devices) | ||
| 975 | preferred = -1; | ||
| 976 | |||
| 977 | pool = __lookup_pg_pool(&osdmap->pg_pools, poolid); | ||
| 978 | if (!pool) | ||
| 979 | return NULL; | ||
| 980 | ruleno = crush_find_rule(osdmap->crush, pool->v.crush_ruleset, | ||
| 981 | pool->v.type, pool->v.size); | ||
| 982 | if (ruleno < 0) { | ||
| 983 | pr_err("no crush rule pool %d type %d size %d\n", | ||
| 984 | poolid, pool->v.type, pool->v.size); | ||
| 985 | return NULL; | ||
| 986 | } | ||
| 987 | |||
| 988 | if (preferred >= 0) | ||
| 989 | pps = ceph_stable_mod(ps, | ||
| 990 | le32_to_cpu(pool->v.lpgp_num), | ||
| 991 | pool->lpgp_num_mask); | ||
| 992 | else | ||
| 993 | pps = ceph_stable_mod(ps, | ||
| 994 | le32_to_cpu(pool->v.pgp_num), | ||
| 995 | pool->pgp_num_mask); | ||
| 996 | pps += poolid; | ||
| 997 | *num = crush_do_rule(osdmap->crush, ruleno, pps, osds, | ||
| 998 | min_t(int, pool->v.size, *num), | ||
| 999 | preferred, osdmap->osd_weight); | ||
| 1000 | return osds; | ||
| 1001 | } | ||
| 1002 | |||
| 1003 | /* | ||
| 1004 | * Return primary osd for given pgid, or -1 if none. | ||
| 1005 | */ | ||
| 1006 | int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, struct ceph_pg pgid) | ||
| 1007 | { | ||
| 1008 | int rawosds[10], *osds; | ||
| 1009 | int i, num = ARRAY_SIZE(rawosds); | ||
| 1010 | |||
| 1011 | osds = calc_pg_raw(osdmap, pgid, rawosds, &num); | ||
| 1012 | if (!osds) | ||
| 1013 | return -1; | ||
| 1014 | |||
| 1015 | /* primary is first up osd */ | ||
| 1016 | for (i = 0; i < num; i++) | ||
| 1017 | if (ceph_osd_is_up(osdmap, osds[i])) { | ||
| 1018 | return osds[i]; | ||
| 1019 | break; | ||
| 1020 | } | ||
| 1021 | return -1; | ||
| 1022 | } | ||
diff --git a/fs/ceph/osdmap.h b/fs/ceph/osdmap.h new file mode 100644 index 000000000000..1fb55afb2642 --- /dev/null +++ b/fs/ceph/osdmap.h | |||
| @@ -0,0 +1,125 @@ | |||
| 1 | #ifndef _FS_CEPH_OSDMAP_H | ||
| 2 | #define _FS_CEPH_OSDMAP_H | ||
| 3 | |||
| 4 | #include <linux/rbtree.h> | ||
| 5 | #include "types.h" | ||
| 6 | #include "ceph_fs.h" | ||
| 7 | #include "crush/crush.h" | ||
| 8 | |||
| 9 | /* | ||
| 10 | * The osd map describes the current membership of the osd cluster and | ||
| 11 | * specifies the mapping of objects to placement groups and placement | ||
| 12 | * groups to (sets of) osds. That is, it completely specifies the | ||
| 13 | * (desired) distribution of all data objects in the system at some | ||
| 14 | * point in time. | ||
| 15 | * | ||
| 16 | * Each map version is identified by an epoch, which increases monotonically. | ||
| 17 | * | ||
| 18 | * The map can be updated either via an incremental map (diff) describing | ||
| 19 | * the change between two successive epochs, or as a fully encoded map. | ||
| 20 | */ | ||
| 21 | struct ceph_pg_pool_info { | ||
| 22 | struct rb_node node; | ||
| 23 | int id; | ||
| 24 | struct ceph_pg_pool v; | ||
| 25 | int pg_num_mask, pgp_num_mask, lpg_num_mask, lpgp_num_mask; | ||
| 26 | }; | ||
| 27 | |||
| 28 | struct ceph_pg_mapping { | ||
| 29 | struct rb_node node; | ||
| 30 | struct ceph_pg pgid; | ||
| 31 | int len; | ||
| 32 | int osds[]; | ||
| 33 | }; | ||
| 34 | |||
| 35 | struct ceph_osdmap { | ||
| 36 | struct ceph_fsid fsid; | ||
| 37 | u32 epoch; | ||
| 38 | u32 mkfs_epoch; | ||
| 39 | struct ceph_timespec created, modified; | ||
| 40 | |||
| 41 | u32 flags; /* CEPH_OSDMAP_* */ | ||
| 42 | |||
| 43 | u32 max_osd; /* size of osd_state, _offload, _addr arrays */ | ||
| 44 | u8 *osd_state; /* CEPH_OSD_* */ | ||
| 45 | u32 *osd_weight; /* 0 = failed, 0x10000 = 100% normal */ | ||
| 46 | struct ceph_entity_addr *osd_addr; | ||
| 47 | |||
| 48 | struct rb_root pg_temp; | ||
| 49 | struct rb_root pg_pools; | ||
| 50 | u32 pool_max; | ||
| 51 | |||
| 52 | /* the CRUSH map specifies the mapping of placement groups to | ||
| 53 | * the list of osds that store+replicate them. */ | ||
| 54 | struct crush_map *crush; | ||
| 55 | }; | ||
| 56 | |||
| 57 | /* | ||
| 58 | * file layout helpers | ||
| 59 | */ | ||
| 60 | #define ceph_file_layout_su(l) ((__s32)le32_to_cpu((l).fl_stripe_unit)) | ||
| 61 | #define ceph_file_layout_stripe_count(l) \ | ||
| 62 | ((__s32)le32_to_cpu((l).fl_stripe_count)) | ||
| 63 | #define ceph_file_layout_object_size(l) ((__s32)le32_to_cpu((l).fl_object_size)) | ||
| 64 | #define ceph_file_layout_cas_hash(l) ((__s32)le32_to_cpu((l).fl_cas_hash)) | ||
| 65 | #define ceph_file_layout_object_su(l) \ | ||
| 66 | ((__s32)le32_to_cpu((l).fl_object_stripe_unit)) | ||
| 67 | #define ceph_file_layout_pg_preferred(l) \ | ||
| 68 | ((__s32)le32_to_cpu((l).fl_pg_preferred)) | ||
| 69 | #define ceph_file_layout_pg_pool(l) \ | ||
| 70 | ((__s32)le32_to_cpu((l).fl_pg_pool)) | ||
| 71 | |||
| 72 | static inline unsigned ceph_file_layout_stripe_width(struct ceph_file_layout *l) | ||
| 73 | { | ||
| 74 | return le32_to_cpu(l->fl_stripe_unit) * | ||
| 75 | le32_to_cpu(l->fl_stripe_count); | ||
| 76 | } | ||
| 77 | |||
| 78 | /* "period" == bytes before i start on a new set of objects */ | ||
| 79 | static inline unsigned ceph_file_layout_period(struct ceph_file_layout *l) | ||
| 80 | { | ||
| 81 | return le32_to_cpu(l->fl_object_size) * | ||
| 82 | le32_to_cpu(l->fl_stripe_count); | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | static inline int ceph_osd_is_up(struct ceph_osdmap *map, int osd) | ||
| 87 | { | ||
| 88 | return (osd < map->max_osd) && (map->osd_state[osd] & CEPH_OSD_UP); | ||
| 89 | } | ||
| 90 | |||
| 91 | static inline bool ceph_osdmap_flag(struct ceph_osdmap *map, int flag) | ||
| 92 | { | ||
| 93 | return map && (map->flags & flag); | ||
| 94 | } | ||
| 95 | |||
| 96 | extern char *ceph_osdmap_state_str(char *str, int len, int state); | ||
| 97 | |||
| 98 | static inline struct ceph_entity_addr *ceph_osd_addr(struct ceph_osdmap *map, | ||
| 99 | int osd) | ||
| 100 | { | ||
| 101 | if (osd >= map->max_osd) | ||
| 102 | return NULL; | ||
| 103 | return &map->osd_addr[osd]; | ||
| 104 | } | ||
| 105 | |||
| 106 | extern struct ceph_osdmap *osdmap_decode(void **p, void *end); | ||
| 107 | extern struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end, | ||
| 108 | struct ceph_osdmap *map, | ||
| 109 | struct ceph_messenger *msgr); | ||
| 110 | extern void ceph_osdmap_destroy(struct ceph_osdmap *map); | ||
| 111 | |||
| 112 | /* calculate mapping of a file extent to an object */ | ||
| 113 | extern void ceph_calc_file_object_mapping(struct ceph_file_layout *layout, | ||
| 114 | u64 off, u64 *plen, | ||
| 115 | u64 *bno, u64 *oxoff, u64 *oxlen); | ||
| 116 | |||
| 117 | /* calculate mapping of object to a placement group */ | ||
| 118 | extern int ceph_calc_object_layout(struct ceph_object_layout *ol, | ||
| 119 | const char *oid, | ||
| 120 | struct ceph_file_layout *fl, | ||
| 121 | struct ceph_osdmap *osdmap); | ||
| 122 | extern int ceph_calc_pg_primary(struct ceph_osdmap *osdmap, | ||
| 123 | struct ceph_pg pgid); | ||
| 124 | |||
| 125 | #endif | ||
diff --git a/fs/ceph/pagelist.c b/fs/ceph/pagelist.c new file mode 100644 index 000000000000..370e93695474 --- /dev/null +++ b/fs/ceph/pagelist.c | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | |||
| 2 | #include <linux/pagemap.h> | ||
| 3 | #include <linux/highmem.h> | ||
| 4 | |||
| 5 | #include "pagelist.h" | ||
| 6 | |||
| 7 | int ceph_pagelist_release(struct ceph_pagelist *pl) | ||
| 8 | { | ||
| 9 | if (pl->mapped_tail) | ||
| 10 | kunmap(pl->mapped_tail); | ||
| 11 | while (!list_empty(&pl->head)) { | ||
| 12 | struct page *page = list_first_entry(&pl->head, struct page, | ||
| 13 | lru); | ||
| 14 | list_del(&page->lru); | ||
| 15 | __free_page(page); | ||
| 16 | } | ||
| 17 | return 0; | ||
| 18 | } | ||
| 19 | |||
| 20 | static int ceph_pagelist_addpage(struct ceph_pagelist *pl) | ||
| 21 | { | ||
| 22 | struct page *page = alloc_page(GFP_NOFS); | ||
| 23 | if (!page) | ||
| 24 | return -ENOMEM; | ||
| 25 | pl->room += PAGE_SIZE; | ||
| 26 | list_add_tail(&page->lru, &pl->head); | ||
| 27 | if (pl->mapped_tail) | ||
| 28 | kunmap(pl->mapped_tail); | ||
| 29 | pl->mapped_tail = kmap(page); | ||
| 30 | return 0; | ||
| 31 | } | ||
| 32 | |||
| 33 | int ceph_pagelist_append(struct ceph_pagelist *pl, void *buf, size_t len) | ||
| 34 | { | ||
| 35 | while (pl->room < len) { | ||
| 36 | size_t bit = pl->room; | ||
| 37 | int ret; | ||
| 38 | |||
| 39 | memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK), | ||
| 40 | buf, bit); | ||
| 41 | pl->length += bit; | ||
| 42 | pl->room -= bit; | ||
| 43 | buf += bit; | ||
| 44 | len -= bit; | ||
| 45 | ret = ceph_pagelist_addpage(pl); | ||
| 46 | if (ret) | ||
| 47 | return ret; | ||
| 48 | } | ||
| 49 | |||
| 50 | memcpy(pl->mapped_tail + (pl->length & ~PAGE_CACHE_MASK), buf, len); | ||
| 51 | pl->length += len; | ||
| 52 | pl->room -= len; | ||
| 53 | return 0; | ||
| 54 | } | ||
diff --git a/fs/ceph/pagelist.h b/fs/ceph/pagelist.h new file mode 100644 index 000000000000..e8a4187e1087 --- /dev/null +++ b/fs/ceph/pagelist.h | |||
| @@ -0,0 +1,54 @@ | |||
| 1 | #ifndef __FS_CEPH_PAGELIST_H | ||
| 2 | #define __FS_CEPH_PAGELIST_H | ||
| 3 | |||
| 4 | #include <linux/list.h> | ||
| 5 | |||
| 6 | struct ceph_pagelist { | ||
| 7 | struct list_head head; | ||
| 8 | void *mapped_tail; | ||
| 9 | size_t length; | ||
| 10 | size_t room; | ||
| 11 | }; | ||
| 12 | |||
| 13 | static inline void ceph_pagelist_init(struct ceph_pagelist *pl) | ||
| 14 | { | ||
| 15 | INIT_LIST_HEAD(&pl->head); | ||
| 16 | pl->mapped_tail = NULL; | ||
| 17 | pl->length = 0; | ||
| 18 | pl->room = 0; | ||
| 19 | } | ||
| 20 | extern int ceph_pagelist_release(struct ceph_pagelist *pl); | ||
| 21 | |||
| 22 | extern int ceph_pagelist_append(struct ceph_pagelist *pl, void *d, size_t l); | ||
| 23 | |||
| 24 | static inline int ceph_pagelist_encode_64(struct ceph_pagelist *pl, u64 v) | ||
| 25 | { | ||
| 26 | __le64 ev = cpu_to_le64(v); | ||
| 27 | return ceph_pagelist_append(pl, &ev, sizeof(ev)); | ||
| 28 | } | ||
| 29 | static inline int ceph_pagelist_encode_32(struct ceph_pagelist *pl, u32 v) | ||
| 30 | { | ||
| 31 | __le32 ev = cpu_to_le32(v); | ||
| 32 | return ceph_pagelist_append(pl, &ev, sizeof(ev)); | ||
| 33 | } | ||
| 34 | static inline int ceph_pagelist_encode_16(struct ceph_pagelist *pl, u16 v) | ||
| 35 | { | ||
| 36 | __le16 ev = cpu_to_le16(v); | ||
| 37 | return ceph_pagelist_append(pl, &ev, sizeof(ev)); | ||
| 38 | } | ||
| 39 | static inline int ceph_pagelist_encode_8(struct ceph_pagelist *pl, u8 v) | ||
| 40 | { | ||
| 41 | return ceph_pagelist_append(pl, &v, 1); | ||
| 42 | } | ||
| 43 | static inline int ceph_pagelist_encode_string(struct ceph_pagelist *pl, | ||
| 44 | char *s, size_t len) | ||
| 45 | { | ||
| 46 | int ret = ceph_pagelist_encode_32(pl, len); | ||
| 47 | if (ret) | ||
| 48 | return ret; | ||
| 49 | if (len) | ||
| 50 | return ceph_pagelist_append(pl, s, len); | ||
| 51 | return 0; | ||
| 52 | } | ||
| 53 | |||
| 54 | #endif | ||
diff --git a/fs/ceph/rados.h b/fs/ceph/rados.h new file mode 100644 index 000000000000..26ac8b89a676 --- /dev/null +++ b/fs/ceph/rados.h | |||
| @@ -0,0 +1,374 @@ | |||
| 1 | #ifndef __RADOS_H | ||
| 2 | #define __RADOS_H | ||
| 3 | |||
| 4 | /* | ||
| 5 | * Data types for the Ceph distributed object storage layer RADOS | ||
| 6 | * (Reliable Autonomic Distributed Object Store). | ||
| 7 | */ | ||
| 8 | |||
| 9 | #include "msgr.h" | ||
| 10 | |||
| 11 | /* | ||
| 12 | * osdmap encoding versions | ||
| 13 | */ | ||
| 14 | #define CEPH_OSDMAP_INC_VERSION 4 | ||
| 15 | #define CEPH_OSDMAP_VERSION 4 | ||
| 16 | |||
| 17 | /* | ||
| 18 | * fs id | ||
| 19 | */ | ||
| 20 | struct ceph_fsid { | ||
| 21 | unsigned char fsid[16]; | ||
| 22 | }; | ||
| 23 | |||
| 24 | static inline int ceph_fsid_compare(const struct ceph_fsid *a, | ||
| 25 | const struct ceph_fsid *b) | ||
| 26 | { | ||
| 27 | return memcmp(a, b, sizeof(*a)); | ||
| 28 | } | ||
| 29 | |||
| 30 | /* | ||
| 31 | * ino, object, etc. | ||
| 32 | */ | ||
| 33 | typedef __le64 ceph_snapid_t; | ||
| 34 | #define CEPH_SNAPDIR ((__u64)(-1)) /* reserved for hidden .snap dir */ | ||
| 35 | #define CEPH_NOSNAP ((__u64)(-2)) /* "head", "live" revision */ | ||
| 36 | #define CEPH_MAXSNAP ((__u64)(-3)) /* largest valid snapid */ | ||
| 37 | |||
| 38 | struct ceph_timespec { | ||
| 39 | __le32 tv_sec; | ||
| 40 | __le32 tv_nsec; | ||
| 41 | } __attribute__ ((packed)); | ||
| 42 | |||
| 43 | |||
| 44 | /* | ||
| 45 | * object layout - how objects are mapped into PGs | ||
| 46 | */ | ||
| 47 | #define CEPH_OBJECT_LAYOUT_HASH 1 | ||
| 48 | #define CEPH_OBJECT_LAYOUT_LINEAR 2 | ||
| 49 | #define CEPH_OBJECT_LAYOUT_HASHINO 3 | ||
| 50 | |||
| 51 | /* | ||
| 52 | * pg layout -- how PGs are mapped onto (sets of) OSDs | ||
| 53 | */ | ||
| 54 | #define CEPH_PG_LAYOUT_CRUSH 0 | ||
| 55 | #define CEPH_PG_LAYOUT_HASH 1 | ||
| 56 | #define CEPH_PG_LAYOUT_LINEAR 2 | ||
| 57 | #define CEPH_PG_LAYOUT_HYBRID 3 | ||
| 58 | |||
| 59 | |||
| 60 | /* | ||
| 61 | * placement group. | ||
| 62 | * we encode this into one __le64. | ||
| 63 | */ | ||
| 64 | struct ceph_pg { | ||
| 65 | __le16 preferred; /* preferred primary osd */ | ||
| 66 | __le16 ps; /* placement seed */ | ||
| 67 | __le32 pool; /* object pool */ | ||
| 68 | } __attribute__ ((packed)); | ||
| 69 | |||
| 70 | /* | ||
| 71 | * pg_pool is a set of pgs storing a pool of objects | ||
| 72 | * | ||
| 73 | * pg_num -- base number of pseudorandomly placed pgs | ||
| 74 | * | ||
| 75 | * pgp_num -- effective number when calculating pg placement. this | ||
| 76 | * is used for pg_num increases. new pgs result in data being "split" | ||
| 77 | * into new pgs. for this to proceed smoothly, new pgs are intiially | ||
| 78 | * colocated with their parents; that is, pgp_num doesn't increase | ||
| 79 | * until the new pgs have successfully split. only _then_ are the new | ||
| 80 | * pgs placed independently. | ||
| 81 | * | ||
| 82 | * lpg_num -- localized pg count (per device). replicas are randomly | ||
| 83 | * selected. | ||
| 84 | * | ||
| 85 | * lpgp_num -- as above. | ||
| 86 | */ | ||
| 87 | #define CEPH_PG_TYPE_REP 1 | ||
| 88 | #define CEPH_PG_TYPE_RAID4 2 | ||
| 89 | #define CEPH_PG_POOL_VERSION 2 | ||
| 90 | struct ceph_pg_pool { | ||
| 91 | __u8 type; /* CEPH_PG_TYPE_* */ | ||
| 92 | __u8 size; /* number of osds in each pg */ | ||
| 93 | __u8 crush_ruleset; /* crush placement rule */ | ||
| 94 | __u8 object_hash; /* hash mapping object name to ps */ | ||
| 95 | __le32 pg_num, pgp_num; /* number of pg's */ | ||
| 96 | __le32 lpg_num, lpgp_num; /* number of localized pg's */ | ||
| 97 | __le32 last_change; /* most recent epoch changed */ | ||
| 98 | __le64 snap_seq; /* seq for per-pool snapshot */ | ||
| 99 | __le32 snap_epoch; /* epoch of last snap */ | ||
| 100 | __le32 num_snaps; | ||
| 101 | __le32 num_removed_snap_intervals; | ||
| 102 | __le64 uid; | ||
| 103 | } __attribute__ ((packed)); | ||
| 104 | |||
| 105 | /* | ||
| 106 | * stable_mod func is used to control number of placement groups. | ||
| 107 | * similar to straight-up modulo, but produces a stable mapping as b | ||
| 108 | * increases over time. b is the number of bins, and bmask is the | ||
| 109 | * containing power of 2 minus 1. | ||
| 110 | * | ||
| 111 | * b <= bmask and bmask=(2**n)-1 | ||
| 112 | * e.g., b=12 -> bmask=15, b=123 -> bmask=127 | ||
| 113 | */ | ||
| 114 | static inline int ceph_stable_mod(int x, int b, int bmask) | ||
| 115 | { | ||
| 116 | if ((x & bmask) < b) | ||
| 117 | return x & bmask; | ||
| 118 | else | ||
| 119 | return x & (bmask >> 1); | ||
| 120 | } | ||
| 121 | |||
| 122 | /* | ||
| 123 | * object layout - how a given object should be stored. | ||
| 124 | */ | ||
| 125 | struct ceph_object_layout { | ||
| 126 | struct ceph_pg ol_pgid; /* raw pg, with _full_ ps precision. */ | ||
| 127 | __le32 ol_stripe_unit; /* for per-object parity, if any */ | ||
| 128 | } __attribute__ ((packed)); | ||
| 129 | |||
| 130 | /* | ||
| 131 | * compound epoch+version, used by storage layer to serialize mutations | ||
| 132 | */ | ||
| 133 | struct ceph_eversion { | ||
| 134 | __le32 epoch; | ||
| 135 | __le64 version; | ||
| 136 | } __attribute__ ((packed)); | ||
| 137 | |||
| 138 | /* | ||
| 139 | * osd map bits | ||
| 140 | */ | ||
| 141 | |||
| 142 | /* status bits */ | ||
| 143 | #define CEPH_OSD_EXISTS 1 | ||
| 144 | #define CEPH_OSD_UP 2 | ||
| 145 | |||
| 146 | /* osd weights. fixed point value: 0x10000 == 1.0 ("in"), 0 == "out" */ | ||
| 147 | #define CEPH_OSD_IN 0x10000 | ||
| 148 | #define CEPH_OSD_OUT 0 | ||
| 149 | |||
| 150 | |||
| 151 | /* | ||
| 152 | * osd map flag bits | ||
| 153 | */ | ||
| 154 | #define CEPH_OSDMAP_NEARFULL (1<<0) /* sync writes (near ENOSPC) */ | ||
| 155 | #define CEPH_OSDMAP_FULL (1<<1) /* no data writes (ENOSPC) */ | ||
| 156 | #define CEPH_OSDMAP_PAUSERD (1<<2) /* pause all reads */ | ||
| 157 | #define CEPH_OSDMAP_PAUSEWR (1<<3) /* pause all writes */ | ||
| 158 | #define CEPH_OSDMAP_PAUSEREC (1<<4) /* pause recovery */ | ||
| 159 | |||
| 160 | /* | ||
| 161 | * osd ops | ||
| 162 | */ | ||
| 163 | #define CEPH_OSD_OP_MODE 0xf000 | ||
| 164 | #define CEPH_OSD_OP_MODE_RD 0x1000 | ||
| 165 | #define CEPH_OSD_OP_MODE_WR 0x2000 | ||
| 166 | #define CEPH_OSD_OP_MODE_RMW 0x3000 | ||
| 167 | #define CEPH_OSD_OP_MODE_SUB 0x4000 | ||
| 168 | |||
| 169 | #define CEPH_OSD_OP_TYPE 0x0f00 | ||
| 170 | #define CEPH_OSD_OP_TYPE_LOCK 0x0100 | ||
| 171 | #define CEPH_OSD_OP_TYPE_DATA 0x0200 | ||
| 172 | #define CEPH_OSD_OP_TYPE_ATTR 0x0300 | ||
| 173 | #define CEPH_OSD_OP_TYPE_EXEC 0x0400 | ||
| 174 | #define CEPH_OSD_OP_TYPE_PG 0x0500 | ||
| 175 | |||
| 176 | enum { | ||
| 177 | /** data **/ | ||
| 178 | /* read */ | ||
| 179 | CEPH_OSD_OP_READ = CEPH_OSD_OP_MODE_RD | CEPH_OSD_OP_TYPE_DATA | 1, | ||
| 180 | CEPH_OSD_OP_STAT = CEPH_OSD_OP_MODE_RD | CEPH_OSD_OP_TYPE_DATA | 2, | ||
| 181 | |||
| 182 | /* fancy read */ | ||
| 183 | CEPH_OSD_OP_MASKTRUNC = CEPH_OSD_OP_MODE_RD | CEPH_OSD_OP_TYPE_DATA | 4, | ||
| 184 | |||
| 185 | /* write */ | ||
| 186 | CEPH_OSD_OP_WRITE = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_DATA | 1, | ||
| 187 | CEPH_OSD_OP_WRITEFULL = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_DATA | 2, | ||
| 188 | CEPH_OSD_OP_TRUNCATE = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_DATA | 3, | ||
| 189 | CEPH_OSD_OP_ZERO = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_DATA | 4, | ||
| 190 | CEPH_OSD_OP_DELETE = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_DATA | 5, | ||
| 191 | |||
| 192 | /* fancy write */ | ||
| 193 | CEPH_OSD_OP_APPEND = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_DATA | 6, | ||
| 194 | CEPH_OSD_OP_STARTSYNC = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_DATA | 7, | ||
| 195 | CEPH_OSD_OP_SETTRUNC = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_DATA | 8, | ||
| 196 | CEPH_OSD_OP_TRIMTRUNC = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_DATA | 9, | ||
| 197 | |||
| 198 | CEPH_OSD_OP_TMAPUP = CEPH_OSD_OP_MODE_RMW | CEPH_OSD_OP_TYPE_DATA | 10, | ||
| 199 | CEPH_OSD_OP_TMAPPUT = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_DATA | 11, | ||
| 200 | CEPH_OSD_OP_TMAPGET = CEPH_OSD_OP_MODE_RD | CEPH_OSD_OP_TYPE_DATA | 12, | ||
| 201 | |||
| 202 | CEPH_OSD_OP_CREATE = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_DATA | 13, | ||
| 203 | |||
| 204 | /** attrs **/ | ||
| 205 | /* read */ | ||
| 206 | CEPH_OSD_OP_GETXATTR = CEPH_OSD_OP_MODE_RD | CEPH_OSD_OP_TYPE_ATTR | 1, | ||
| 207 | CEPH_OSD_OP_GETXATTRS = CEPH_OSD_OP_MODE_RD | CEPH_OSD_OP_TYPE_ATTR | 2, | ||
| 208 | |||
| 209 | /* write */ | ||
| 210 | CEPH_OSD_OP_SETXATTR = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_ATTR | 1, | ||
| 211 | CEPH_OSD_OP_SETXATTRS = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_ATTR | 2, | ||
| 212 | CEPH_OSD_OP_RESETXATTRS = CEPH_OSD_OP_MODE_WR|CEPH_OSD_OP_TYPE_ATTR | 3, | ||
| 213 | CEPH_OSD_OP_RMXATTR = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_ATTR | 4, | ||
| 214 | |||
| 215 | /** subop **/ | ||
| 216 | CEPH_OSD_OP_PULL = CEPH_OSD_OP_MODE_SUB | 1, | ||
| 217 | CEPH_OSD_OP_PUSH = CEPH_OSD_OP_MODE_SUB | 2, | ||
| 218 | CEPH_OSD_OP_BALANCEREADS = CEPH_OSD_OP_MODE_SUB | 3, | ||
| 219 | CEPH_OSD_OP_UNBALANCEREADS = CEPH_OSD_OP_MODE_SUB | 4, | ||
| 220 | CEPH_OSD_OP_SCRUB = CEPH_OSD_OP_MODE_SUB | 5, | ||
| 221 | |||
| 222 | /** lock **/ | ||
| 223 | CEPH_OSD_OP_WRLOCK = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_LOCK | 1, | ||
| 224 | CEPH_OSD_OP_WRUNLOCK = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_LOCK | 2, | ||
| 225 | CEPH_OSD_OP_RDLOCK = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_LOCK | 3, | ||
| 226 | CEPH_OSD_OP_RDUNLOCK = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_LOCK | 4, | ||
| 227 | CEPH_OSD_OP_UPLOCK = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_LOCK | 5, | ||
| 228 | CEPH_OSD_OP_DNLOCK = CEPH_OSD_OP_MODE_WR | CEPH_OSD_OP_TYPE_LOCK | 6, | ||
| 229 | |||
| 230 | /** exec **/ | ||
| 231 | CEPH_OSD_OP_CALL = CEPH_OSD_OP_MODE_RD | CEPH_OSD_OP_TYPE_EXEC | 1, | ||
| 232 | |||
| 233 | /** pg **/ | ||
| 234 | CEPH_OSD_OP_PGLS = CEPH_OSD_OP_MODE_RD | CEPH_OSD_OP_TYPE_PG | 1, | ||
| 235 | }; | ||
| 236 | |||
| 237 | static inline int ceph_osd_op_type_lock(int op) | ||
| 238 | { | ||
| 239 | return (op & CEPH_OSD_OP_TYPE) == CEPH_OSD_OP_TYPE_LOCK; | ||
| 240 | } | ||
| 241 | static inline int ceph_osd_op_type_data(int op) | ||
| 242 | { | ||
| 243 | return (op & CEPH_OSD_OP_TYPE) == CEPH_OSD_OP_TYPE_DATA; | ||
| 244 | } | ||
| 245 | static inline int ceph_osd_op_type_attr(int op) | ||
| 246 | { | ||
| 247 | return (op & CEPH_OSD_OP_TYPE) == CEPH_OSD_OP_TYPE_ATTR; | ||
| 248 | } | ||
| 249 | static inline int ceph_osd_op_type_exec(int op) | ||
| 250 | { | ||
| 251 | return (op & CEPH_OSD_OP_TYPE) == CEPH_OSD_OP_TYPE_EXEC; | ||
| 252 | } | ||
| 253 | static inline int ceph_osd_op_type_pg(int op) | ||
| 254 | { | ||
| 255 | return (op & CEPH_OSD_OP_TYPE) == CEPH_OSD_OP_TYPE_PG; | ||
| 256 | } | ||
| 257 | |||
| 258 | static inline int ceph_osd_op_mode_subop(int op) | ||
| 259 | { | ||
| 260 | return (op & CEPH_OSD_OP_MODE) == CEPH_OSD_OP_MODE_SUB; | ||
| 261 | } | ||
| 262 | static inline int ceph_osd_op_mode_read(int op) | ||
| 263 | { | ||
| 264 | return (op & CEPH_OSD_OP_MODE) == CEPH_OSD_OP_MODE_RD; | ||
| 265 | } | ||
| 266 | static inline int ceph_osd_op_mode_modify(int op) | ||
| 267 | { | ||
| 268 | return (op & CEPH_OSD_OP_MODE) == CEPH_OSD_OP_MODE_WR; | ||
| 269 | } | ||
| 270 | |||
| 271 | #define CEPH_OSD_TMAP_HDR 'h' | ||
| 272 | #define CEPH_OSD_TMAP_SET 's' | ||
| 273 | #define CEPH_OSD_TMAP_RM 'r' | ||
| 274 | |||
| 275 | extern const char *ceph_osd_op_name(int op); | ||
| 276 | |||
| 277 | |||
| 278 | /* | ||
| 279 | * osd op flags | ||
| 280 | * | ||
| 281 | * An op may be READ, WRITE, or READ|WRITE. | ||
| 282 | */ | ||
| 283 | enum { | ||
| 284 | CEPH_OSD_FLAG_ACK = 1, /* want (or is) "ack" ack */ | ||
| 285 | CEPH_OSD_FLAG_ONNVRAM = 2, /* want (or is) "onnvram" ack */ | ||
| 286 | CEPH_OSD_FLAG_ONDISK = 4, /* want (or is) "ondisk" ack */ | ||
| 287 | CEPH_OSD_FLAG_RETRY = 8, /* resend attempt */ | ||
| 288 | CEPH_OSD_FLAG_READ = 16, /* op may read */ | ||
| 289 | CEPH_OSD_FLAG_WRITE = 32, /* op may write */ | ||
| 290 | CEPH_OSD_FLAG_ORDERSNAP = 64, /* EOLDSNAP if snapc is out of order */ | ||
| 291 | CEPH_OSD_FLAG_PEERSTAT = 128, /* msg includes osd_peer_stat */ | ||
| 292 | CEPH_OSD_FLAG_BALANCE_READS = 256, | ||
| 293 | CEPH_OSD_FLAG_PARALLELEXEC = 512, /* execute op in parallel */ | ||
| 294 | CEPH_OSD_FLAG_PGOP = 1024, /* pg op, no object */ | ||
| 295 | CEPH_OSD_FLAG_EXEC = 2048, /* op may exec */ | ||
| 296 | }; | ||
| 297 | |||
| 298 | enum { | ||
| 299 | CEPH_OSD_OP_FLAG_EXCL = 1, /* EXCL object create */ | ||
| 300 | }; | ||
| 301 | |||
| 302 | #define EOLDSNAPC ERESTART /* ORDERSNAP flag set; writer has old snapc*/ | ||
| 303 | #define EBLACKLISTED ESHUTDOWN /* blacklisted */ | ||
| 304 | |||
| 305 | /* | ||
| 306 | * an individual object operation. each may be accompanied by some data | ||
| 307 | * payload | ||
| 308 | */ | ||
| 309 | struct ceph_osd_op { | ||
| 310 | __le16 op; /* CEPH_OSD_OP_* */ | ||
| 311 | __le32 flags; /* CEPH_OSD_FLAG_* */ | ||
| 312 | union { | ||
| 313 | struct { | ||
| 314 | __le64 offset, length; | ||
| 315 | __le64 truncate_size; | ||
| 316 | __le32 truncate_seq; | ||
| 317 | } __attribute__ ((packed)) extent; | ||
| 318 | struct { | ||
| 319 | __le32 name_len; | ||
| 320 | __le32 value_len; | ||
| 321 | } __attribute__ ((packed)) xattr; | ||
| 322 | struct { | ||
| 323 | __u8 class_len; | ||
| 324 | __u8 method_len; | ||
| 325 | __u8 argc; | ||
| 326 | __le32 indata_len; | ||
| 327 | } __attribute__ ((packed)) cls; | ||
| 328 | struct { | ||
| 329 | __le64 cookie, count; | ||
| 330 | } __attribute__ ((packed)) pgls; | ||
| 331 | }; | ||
| 332 | __le32 payload_len; | ||
| 333 | } __attribute__ ((packed)); | ||
| 334 | |||
| 335 | /* | ||
| 336 | * osd request message header. each request may include multiple | ||
| 337 | * ceph_osd_op object operations. | ||
| 338 | */ | ||
| 339 | struct ceph_osd_request_head { | ||
| 340 | __le32 client_inc; /* client incarnation */ | ||
| 341 | struct ceph_object_layout layout; /* pgid */ | ||
| 342 | __le32 osdmap_epoch; /* client's osdmap epoch */ | ||
| 343 | |||
| 344 | __le32 flags; | ||
| 345 | |||
| 346 | struct ceph_timespec mtime; /* for mutations only */ | ||
| 347 | struct ceph_eversion reassert_version; /* if we are replaying op */ | ||
| 348 | |||
| 349 | __le32 object_len; /* length of object name */ | ||
| 350 | |||
| 351 | __le64 snapid; /* snapid to read */ | ||
| 352 | __le64 snap_seq; /* writer's snap context */ | ||
| 353 | __le32 num_snaps; | ||
| 354 | |||
| 355 | __le16 num_ops; | ||
| 356 | struct ceph_osd_op ops[]; /* followed by ops[], obj, ticket, snaps */ | ||
| 357 | } __attribute__ ((packed)); | ||
| 358 | |||
| 359 | struct ceph_osd_reply_head { | ||
| 360 | __le32 client_inc; /* client incarnation */ | ||
| 361 | __le32 flags; | ||
| 362 | struct ceph_object_layout layout; | ||
| 363 | __le32 osdmap_epoch; | ||
| 364 | struct ceph_eversion reassert_version; /* for replaying uncommitted */ | ||
| 365 | |||
| 366 | __le32 result; /* result code */ | ||
| 367 | |||
| 368 | __le32 object_len; /* length of object name */ | ||
| 369 | __le32 num_ops; | ||
| 370 | struct ceph_osd_op ops[0]; /* ops[], object */ | ||
| 371 | } __attribute__ ((packed)); | ||
| 372 | |||
| 373 | |||
| 374 | #endif | ||
diff --git a/fs/ceph/snap.c b/fs/ceph/snap.c new file mode 100644 index 000000000000..df04e210a055 --- /dev/null +++ b/fs/ceph/snap.c | |||
| @@ -0,0 +1,906 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | |||
| 3 | #include <linux/sort.h> | ||
| 4 | |||
| 5 | #include "super.h" | ||
| 6 | #include "decode.h" | ||
| 7 | |||
| 8 | /* | ||
| 9 | * Snapshots in ceph are driven in large part by cooperation from the | ||
| 10 | * client. In contrast to local file systems or file servers that | ||
| 11 | * implement snapshots at a single point in the system, ceph's | ||
| 12 | * distributed access to storage requires clients to help decide | ||
| 13 | * whether a write logically occurs before or after a recently created | ||
| 14 | * snapshot. | ||
| 15 | * | ||
| 16 | * This provides a perfect instantanous client-wide snapshot. Between | ||
| 17 | * clients, however, snapshots may appear to be applied at slightly | ||
| 18 | * different points in time, depending on delays in delivering the | ||
| 19 | * snapshot notification. | ||
| 20 | * | ||
| 21 | * Snapshots are _not_ file system-wide. Instead, each snapshot | ||
| 22 | * applies to the subdirectory nested beneath some directory. This | ||
| 23 | * effectively divides the hierarchy into multiple "realms," where all | ||
| 24 | * of the files contained by each realm share the same set of | ||
| 25 | * snapshots. An individual realm's snap set contains snapshots | ||
| 26 | * explicitly created on that realm, as well as any snaps in its | ||
| 27 | * parent's snap set _after_ the point at which the parent became it's | ||
| 28 | * parent (due to, say, a rename). Similarly, snaps from prior parents | ||
| 29 | * during the time intervals during which they were the parent are included. | ||
| 30 | * | ||
| 31 | * The client is spared most of this detail, fortunately... it must only | ||
| 32 | * maintains a hierarchy of realms reflecting the current parent/child | ||
| 33 | * realm relationship, and for each realm has an explicit list of snaps | ||
| 34 | * inherited from prior parents. | ||
| 35 | * | ||
| 36 | * A snap_realm struct is maintained for realms containing every inode | ||
| 37 | * with an open cap in the system. (The needed snap realm information is | ||
| 38 | * provided by the MDS whenever a cap is issued, i.e., on open.) A 'seq' | ||
| 39 | * version number is used to ensure that as realm parameters change (new | ||
| 40 | * snapshot, new parent, etc.) the client's realm hierarchy is updated. | ||
| 41 | * | ||
| 42 | * The realm hierarchy drives the generation of a 'snap context' for each | ||
| 43 | * realm, which simply lists the resulting set of snaps for the realm. This | ||
| 44 | * is attached to any writes sent to OSDs. | ||
| 45 | */ | ||
| 46 | /* | ||
| 47 | * Unfortunately error handling is a bit mixed here. If we get a snap | ||
| 48 | * update, but don't have enough memory to update our realm hierarchy, | ||
| 49 | * it's not clear what we can do about it (besides complaining to the | ||
| 50 | * console). | ||
| 51 | */ | ||
| 52 | |||
| 53 | |||
| 54 | /* | ||
| 55 | * increase ref count for the realm | ||
| 56 | * | ||
| 57 | * caller must hold snap_rwsem for write. | ||
| 58 | */ | ||
| 59 | void ceph_get_snap_realm(struct ceph_mds_client *mdsc, | ||
| 60 | struct ceph_snap_realm *realm) | ||
| 61 | { | ||
| 62 | dout("get_realm %p %d -> %d\n", realm, | ||
| 63 | atomic_read(&realm->nref), atomic_read(&realm->nref)+1); | ||
| 64 | /* | ||
| 65 | * since we _only_ increment realm refs or empty the empty | ||
| 66 | * list with snap_rwsem held, adjusting the empty list here is | ||
| 67 | * safe. we do need to protect against concurrent empty list | ||
| 68 | * additions, however. | ||
| 69 | */ | ||
| 70 | if (atomic_read(&realm->nref) == 0) { | ||
| 71 | spin_lock(&mdsc->snap_empty_lock); | ||
| 72 | list_del_init(&realm->empty_item); | ||
| 73 | spin_unlock(&mdsc->snap_empty_lock); | ||
| 74 | } | ||
| 75 | |||
| 76 | atomic_inc(&realm->nref); | ||
| 77 | } | ||
| 78 | |||
| 79 | static void __insert_snap_realm(struct rb_root *root, | ||
| 80 | struct ceph_snap_realm *new) | ||
| 81 | { | ||
| 82 | struct rb_node **p = &root->rb_node; | ||
| 83 | struct rb_node *parent = NULL; | ||
| 84 | struct ceph_snap_realm *r = NULL; | ||
| 85 | |||
| 86 | while (*p) { | ||
| 87 | parent = *p; | ||
| 88 | r = rb_entry(parent, struct ceph_snap_realm, node); | ||
| 89 | if (new->ino < r->ino) | ||
| 90 | p = &(*p)->rb_left; | ||
| 91 | else if (new->ino > r->ino) | ||
| 92 | p = &(*p)->rb_right; | ||
| 93 | else | ||
| 94 | BUG(); | ||
| 95 | } | ||
| 96 | |||
| 97 | rb_link_node(&new->node, parent, p); | ||
| 98 | rb_insert_color(&new->node, root); | ||
| 99 | } | ||
| 100 | |||
| 101 | /* | ||
| 102 | * create and get the realm rooted at @ino and bump its ref count. | ||
| 103 | * | ||
| 104 | * caller must hold snap_rwsem for write. | ||
| 105 | */ | ||
| 106 | static struct ceph_snap_realm *ceph_create_snap_realm( | ||
| 107 | struct ceph_mds_client *mdsc, | ||
| 108 | u64 ino) | ||
| 109 | { | ||
| 110 | struct ceph_snap_realm *realm; | ||
| 111 | |||
| 112 | realm = kzalloc(sizeof(*realm), GFP_NOFS); | ||
| 113 | if (!realm) | ||
| 114 | return ERR_PTR(-ENOMEM); | ||
| 115 | |||
| 116 | atomic_set(&realm->nref, 0); /* tree does not take a ref */ | ||
| 117 | realm->ino = ino; | ||
| 118 | INIT_LIST_HEAD(&realm->children); | ||
| 119 | INIT_LIST_HEAD(&realm->child_item); | ||
| 120 | INIT_LIST_HEAD(&realm->empty_item); | ||
| 121 | INIT_LIST_HEAD(&realm->inodes_with_caps); | ||
| 122 | spin_lock_init(&realm->inodes_with_caps_lock); | ||
| 123 | __insert_snap_realm(&mdsc->snap_realms, realm); | ||
| 124 | dout("create_snap_realm %llx %p\n", realm->ino, realm); | ||
| 125 | return realm; | ||
| 126 | } | ||
| 127 | |||
| 128 | /* | ||
| 129 | * lookup the realm rooted at @ino. | ||
| 130 | * | ||
| 131 | * caller must hold snap_rwsem for write. | ||
| 132 | */ | ||
| 133 | struct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc, | ||
| 134 | u64 ino) | ||
| 135 | { | ||
| 136 | struct rb_node *n = mdsc->snap_realms.rb_node; | ||
| 137 | struct ceph_snap_realm *r; | ||
| 138 | |||
| 139 | while (n) { | ||
| 140 | r = rb_entry(n, struct ceph_snap_realm, node); | ||
| 141 | if (ino < r->ino) | ||
| 142 | n = n->rb_left; | ||
| 143 | else if (ino > r->ino) | ||
| 144 | n = n->rb_right; | ||
| 145 | else { | ||
| 146 | dout("lookup_snap_realm %llx %p\n", r->ino, r); | ||
| 147 | return r; | ||
| 148 | } | ||
| 149 | } | ||
| 150 | return NULL; | ||
| 151 | } | ||
| 152 | |||
| 153 | static void __put_snap_realm(struct ceph_mds_client *mdsc, | ||
| 154 | struct ceph_snap_realm *realm); | ||
| 155 | |||
| 156 | /* | ||
| 157 | * called with snap_rwsem (write) | ||
| 158 | */ | ||
| 159 | static void __destroy_snap_realm(struct ceph_mds_client *mdsc, | ||
| 160 | struct ceph_snap_realm *realm) | ||
| 161 | { | ||
| 162 | dout("__destroy_snap_realm %p %llx\n", realm, realm->ino); | ||
| 163 | |||
| 164 | rb_erase(&realm->node, &mdsc->snap_realms); | ||
| 165 | |||
| 166 | if (realm->parent) { | ||
| 167 | list_del_init(&realm->child_item); | ||
| 168 | __put_snap_realm(mdsc, realm->parent); | ||
| 169 | } | ||
| 170 | |||
| 171 | kfree(realm->prior_parent_snaps); | ||
| 172 | kfree(realm->snaps); | ||
| 173 | ceph_put_snap_context(realm->cached_context); | ||
| 174 | kfree(realm); | ||
| 175 | } | ||
| 176 | |||
| 177 | /* | ||
| 178 | * caller holds snap_rwsem (write) | ||
| 179 | */ | ||
| 180 | static void __put_snap_realm(struct ceph_mds_client *mdsc, | ||
| 181 | struct ceph_snap_realm *realm) | ||
| 182 | { | ||
| 183 | dout("__put_snap_realm %llx %p %d -> %d\n", realm->ino, realm, | ||
| 184 | atomic_read(&realm->nref), atomic_read(&realm->nref)-1); | ||
| 185 | if (atomic_dec_and_test(&realm->nref)) | ||
| 186 | __destroy_snap_realm(mdsc, realm); | ||
| 187 | } | ||
| 188 | |||
| 189 | /* | ||
| 190 | * caller needn't hold any locks | ||
| 191 | */ | ||
| 192 | void ceph_put_snap_realm(struct ceph_mds_client *mdsc, | ||
| 193 | struct ceph_snap_realm *realm) | ||
| 194 | { | ||
| 195 | dout("put_snap_realm %llx %p %d -> %d\n", realm->ino, realm, | ||
| 196 | atomic_read(&realm->nref), atomic_read(&realm->nref)-1); | ||
| 197 | if (!atomic_dec_and_test(&realm->nref)) | ||
| 198 | return; | ||
| 199 | |||
| 200 | if (down_write_trylock(&mdsc->snap_rwsem)) { | ||
| 201 | __destroy_snap_realm(mdsc, realm); | ||
| 202 | up_write(&mdsc->snap_rwsem); | ||
| 203 | } else { | ||
| 204 | spin_lock(&mdsc->snap_empty_lock); | ||
| 205 | list_add(&mdsc->snap_empty, &realm->empty_item); | ||
| 206 | spin_unlock(&mdsc->snap_empty_lock); | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | /* | ||
| 211 | * Clean up any realms whose ref counts have dropped to zero. Note | ||
| 212 | * that this does not include realms who were created but not yet | ||
| 213 | * used. | ||
| 214 | * | ||
| 215 | * Called under snap_rwsem (write) | ||
| 216 | */ | ||
| 217 | static void __cleanup_empty_realms(struct ceph_mds_client *mdsc) | ||
| 218 | { | ||
| 219 | struct ceph_snap_realm *realm; | ||
| 220 | |||
| 221 | spin_lock(&mdsc->snap_empty_lock); | ||
| 222 | while (!list_empty(&mdsc->snap_empty)) { | ||
| 223 | realm = list_first_entry(&mdsc->snap_empty, | ||
| 224 | struct ceph_snap_realm, empty_item); | ||
| 225 | list_del(&realm->empty_item); | ||
| 226 | spin_unlock(&mdsc->snap_empty_lock); | ||
| 227 | __destroy_snap_realm(mdsc, realm); | ||
| 228 | spin_lock(&mdsc->snap_empty_lock); | ||
| 229 | } | ||
| 230 | spin_unlock(&mdsc->snap_empty_lock); | ||
| 231 | } | ||
| 232 | |||
| 233 | void ceph_cleanup_empty_realms(struct ceph_mds_client *mdsc) | ||
| 234 | { | ||
| 235 | down_write(&mdsc->snap_rwsem); | ||
| 236 | __cleanup_empty_realms(mdsc); | ||
| 237 | up_write(&mdsc->snap_rwsem); | ||
| 238 | } | ||
| 239 | |||
| 240 | /* | ||
| 241 | * adjust the parent realm of a given @realm. adjust child list, and parent | ||
| 242 | * pointers, and ref counts appropriately. | ||
| 243 | * | ||
| 244 | * return true if parent was changed, 0 if unchanged, <0 on error. | ||
| 245 | * | ||
| 246 | * caller must hold snap_rwsem for write. | ||
| 247 | */ | ||
| 248 | static int adjust_snap_realm_parent(struct ceph_mds_client *mdsc, | ||
| 249 | struct ceph_snap_realm *realm, | ||
| 250 | u64 parentino) | ||
| 251 | { | ||
| 252 | struct ceph_snap_realm *parent; | ||
| 253 | |||
| 254 | if (realm->parent_ino == parentino) | ||
| 255 | return 0; | ||
| 256 | |||
| 257 | parent = ceph_lookup_snap_realm(mdsc, parentino); | ||
| 258 | if (!parent) { | ||
| 259 | parent = ceph_create_snap_realm(mdsc, parentino); | ||
| 260 | if (IS_ERR(parent)) | ||
| 261 | return PTR_ERR(parent); | ||
| 262 | } | ||
| 263 | dout("adjust_snap_realm_parent %llx %p: %llx %p -> %llx %p\n", | ||
| 264 | realm->ino, realm, realm->parent_ino, realm->parent, | ||
| 265 | parentino, parent); | ||
| 266 | if (realm->parent) { | ||
| 267 | list_del_init(&realm->child_item); | ||
| 268 | ceph_put_snap_realm(mdsc, realm->parent); | ||
| 269 | } | ||
| 270 | realm->parent_ino = parentino; | ||
| 271 | realm->parent = parent; | ||
| 272 | ceph_get_snap_realm(mdsc, parent); | ||
| 273 | list_add(&realm->child_item, &parent->children); | ||
| 274 | return 1; | ||
| 275 | } | ||
| 276 | |||
| 277 | |||
| 278 | static int cmpu64_rev(const void *a, const void *b) | ||
| 279 | { | ||
| 280 | if (*(u64 *)a < *(u64 *)b) | ||
| 281 | return 1; | ||
| 282 | if (*(u64 *)a > *(u64 *)b) | ||
| 283 | return -1; | ||
| 284 | return 0; | ||
| 285 | } | ||
| 286 | |||
| 287 | /* | ||
| 288 | * build the snap context for a given realm. | ||
| 289 | */ | ||
| 290 | static int build_snap_context(struct ceph_snap_realm *realm) | ||
| 291 | { | ||
| 292 | struct ceph_snap_realm *parent = realm->parent; | ||
| 293 | struct ceph_snap_context *snapc; | ||
| 294 | int err = 0; | ||
| 295 | int i; | ||
| 296 | int num = realm->num_prior_parent_snaps + realm->num_snaps; | ||
| 297 | |||
| 298 | /* | ||
| 299 | * build parent context, if it hasn't been built. | ||
| 300 | * conservatively estimate that all parent snaps might be | ||
| 301 | * included by us. | ||
| 302 | */ | ||
| 303 | if (parent) { | ||
| 304 | if (!parent->cached_context) { | ||
| 305 | err = build_snap_context(parent); | ||
| 306 | if (err) | ||
| 307 | goto fail; | ||
| 308 | } | ||
| 309 | num += parent->cached_context->num_snaps; | ||
| 310 | } | ||
| 311 | |||
| 312 | /* do i actually need to update? not if my context seq | ||
| 313 | matches realm seq, and my parents' does to. (this works | ||
| 314 | because we rebuild_snap_realms() works _downward_ in | ||
| 315 | hierarchy after each update.) */ | ||
| 316 | if (realm->cached_context && | ||
| 317 | realm->cached_context->seq == realm->seq && | ||
| 318 | (!parent || | ||
| 319 | realm->cached_context->seq >= parent->cached_context->seq)) { | ||
| 320 | dout("build_snap_context %llx %p: %p seq %lld (%d snaps)" | ||
| 321 | " (unchanged)\n", | ||
| 322 | realm->ino, realm, realm->cached_context, | ||
| 323 | realm->cached_context->seq, | ||
| 324 | realm->cached_context->num_snaps); | ||
| 325 | return 0; | ||
| 326 | } | ||
| 327 | |||
| 328 | /* alloc new snap context */ | ||
| 329 | err = -ENOMEM; | ||
| 330 | if (num > ULONG_MAX / sizeof(u64) - sizeof(*snapc)) | ||
| 331 | goto fail; | ||
| 332 | snapc = kzalloc(sizeof(*snapc) + num*sizeof(u64), GFP_NOFS); | ||
| 333 | if (!snapc) | ||
| 334 | goto fail; | ||
| 335 | atomic_set(&snapc->nref, 1); | ||
| 336 | |||
| 337 | /* build (reverse sorted) snap vector */ | ||
| 338 | num = 0; | ||
| 339 | snapc->seq = realm->seq; | ||
| 340 | if (parent) { | ||
| 341 | /* include any of parent's snaps occuring _after_ my | ||
| 342 | parent became my parent */ | ||
| 343 | for (i = 0; i < parent->cached_context->num_snaps; i++) | ||
| 344 | if (parent->cached_context->snaps[i] >= | ||
| 345 | realm->parent_since) | ||
| 346 | snapc->snaps[num++] = | ||
| 347 | parent->cached_context->snaps[i]; | ||
| 348 | if (parent->cached_context->seq > snapc->seq) | ||
| 349 | snapc->seq = parent->cached_context->seq; | ||
| 350 | } | ||
| 351 | memcpy(snapc->snaps + num, realm->snaps, | ||
| 352 | sizeof(u64)*realm->num_snaps); | ||
| 353 | num += realm->num_snaps; | ||
| 354 | memcpy(snapc->snaps + num, realm->prior_parent_snaps, | ||
| 355 | sizeof(u64)*realm->num_prior_parent_snaps); | ||
| 356 | num += realm->num_prior_parent_snaps; | ||
| 357 | |||
| 358 | sort(snapc->snaps, num, sizeof(u64), cmpu64_rev, NULL); | ||
| 359 | snapc->num_snaps = num; | ||
| 360 | dout("build_snap_context %llx %p: %p seq %lld (%d snaps)\n", | ||
| 361 | realm->ino, realm, snapc, snapc->seq, snapc->num_snaps); | ||
| 362 | |||
| 363 | if (realm->cached_context) | ||
| 364 | ceph_put_snap_context(realm->cached_context); | ||
| 365 | realm->cached_context = snapc; | ||
| 366 | return 0; | ||
| 367 | |||
| 368 | fail: | ||
| 369 | /* | ||
| 370 | * if we fail, clear old (incorrect) cached_context... hopefully | ||
| 371 | * we'll have better luck building it later | ||
| 372 | */ | ||
| 373 | if (realm->cached_context) { | ||
| 374 | ceph_put_snap_context(realm->cached_context); | ||
| 375 | realm->cached_context = NULL; | ||
| 376 | } | ||
| 377 | pr_err("build_snap_context %llx %p fail %d\n", realm->ino, | ||
| 378 | realm, err); | ||
| 379 | return err; | ||
| 380 | } | ||
| 381 | |||
| 382 | /* | ||
| 383 | * rebuild snap context for the given realm and all of its children. | ||
| 384 | */ | ||
| 385 | static void rebuild_snap_realms(struct ceph_snap_realm *realm) | ||
| 386 | { | ||
| 387 | struct ceph_snap_realm *child; | ||
| 388 | |||
| 389 | dout("rebuild_snap_realms %llx %p\n", realm->ino, realm); | ||
| 390 | build_snap_context(realm); | ||
| 391 | |||
| 392 | list_for_each_entry(child, &realm->children, child_item) | ||
| 393 | rebuild_snap_realms(child); | ||
| 394 | } | ||
| 395 | |||
| 396 | |||
| 397 | /* | ||
| 398 | * helper to allocate and decode an array of snapids. free prior | ||
| 399 | * instance, if any. | ||
| 400 | */ | ||
| 401 | static int dup_array(u64 **dst, __le64 *src, int num) | ||
| 402 | { | ||
| 403 | int i; | ||
| 404 | |||
| 405 | kfree(*dst); | ||
| 406 | if (num) { | ||
| 407 | *dst = kcalloc(num, sizeof(u64), GFP_NOFS); | ||
| 408 | if (!*dst) | ||
| 409 | return -ENOMEM; | ||
| 410 | for (i = 0; i < num; i++) | ||
| 411 | (*dst)[i] = get_unaligned_le64(src + i); | ||
| 412 | } else { | ||
| 413 | *dst = NULL; | ||
| 414 | } | ||
| 415 | return 0; | ||
| 416 | } | ||
| 417 | |||
| 418 | |||
| 419 | /* | ||
| 420 | * When a snapshot is applied, the size/mtime inode metadata is queued | ||
| 421 | * in a ceph_cap_snap (one for each snapshot) until writeback | ||
| 422 | * completes and the metadata can be flushed back to the MDS. | ||
| 423 | * | ||
| 424 | * However, if a (sync) write is currently in-progress when we apply | ||
| 425 | * the snapshot, we have to wait until the write succeeds or fails | ||
| 426 | * (and a final size/mtime is known). In this case the | ||
| 427 | * cap_snap->writing = 1, and is said to be "pending." When the write | ||
| 428 | * finishes, we __ceph_finish_cap_snap(). | ||
| 429 | * | ||
| 430 | * Caller must hold snap_rwsem for read (i.e., the realm topology won't | ||
| 431 | * change). | ||
| 432 | */ | ||
| 433 | void ceph_queue_cap_snap(struct ceph_inode_info *ci, | ||
| 434 | struct ceph_snap_context *snapc) | ||
| 435 | { | ||
| 436 | struct inode *inode = &ci->vfs_inode; | ||
| 437 | struct ceph_cap_snap *capsnap; | ||
| 438 | int used; | ||
| 439 | |||
| 440 | capsnap = kzalloc(sizeof(*capsnap), GFP_NOFS); | ||
| 441 | if (!capsnap) { | ||
| 442 | pr_err("ENOMEM allocating ceph_cap_snap on %p\n", inode); | ||
| 443 | return; | ||
| 444 | } | ||
| 445 | |||
| 446 | spin_lock(&inode->i_lock); | ||
| 447 | used = __ceph_caps_used(ci); | ||
| 448 | if (__ceph_have_pending_cap_snap(ci)) { | ||
| 449 | /* there is no point in queuing multiple "pending" cap_snaps, | ||
| 450 | as no new writes are allowed to start when pending, so any | ||
| 451 | writes in progress now were started before the previous | ||
| 452 | cap_snap. lucky us. */ | ||
| 453 | dout("queue_cap_snap %p snapc %p seq %llu used %d" | ||
| 454 | " already pending\n", inode, snapc, snapc->seq, used); | ||
| 455 | kfree(capsnap); | ||
| 456 | } else if (ci->i_wrbuffer_ref_head || (used & CEPH_CAP_FILE_WR)) { | ||
| 457 | igrab(inode); | ||
| 458 | |||
| 459 | atomic_set(&capsnap->nref, 1); | ||
| 460 | capsnap->ci = ci; | ||
| 461 | INIT_LIST_HEAD(&capsnap->ci_item); | ||
| 462 | INIT_LIST_HEAD(&capsnap->flushing_item); | ||
| 463 | |||
| 464 | capsnap->follows = snapc->seq - 1; | ||
| 465 | capsnap->context = ceph_get_snap_context(snapc); | ||
| 466 | capsnap->issued = __ceph_caps_issued(ci, NULL); | ||
| 467 | capsnap->dirty = __ceph_caps_dirty(ci); | ||
| 468 | |||
| 469 | capsnap->mode = inode->i_mode; | ||
| 470 | capsnap->uid = inode->i_uid; | ||
| 471 | capsnap->gid = inode->i_gid; | ||
| 472 | |||
| 473 | /* fixme? */ | ||
| 474 | capsnap->xattr_blob = NULL; | ||
| 475 | capsnap->xattr_len = 0; | ||
| 476 | |||
| 477 | /* dirty page count moved from _head to this cap_snap; | ||
| 478 | all subsequent writes page dirties occur _after_ this | ||
| 479 | snapshot. */ | ||
| 480 | capsnap->dirty_pages = ci->i_wrbuffer_ref_head; | ||
| 481 | ci->i_wrbuffer_ref_head = 0; | ||
| 482 | ceph_put_snap_context(ci->i_head_snapc); | ||
| 483 | ci->i_head_snapc = NULL; | ||
| 484 | list_add_tail(&capsnap->ci_item, &ci->i_cap_snaps); | ||
| 485 | |||
| 486 | if (used & CEPH_CAP_FILE_WR) { | ||
| 487 | dout("queue_cap_snap %p cap_snap %p snapc %p" | ||
| 488 | " seq %llu used WR, now pending\n", inode, | ||
| 489 | capsnap, snapc, snapc->seq); | ||
| 490 | capsnap->writing = 1; | ||
| 491 | } else { | ||
| 492 | /* note mtime, size NOW. */ | ||
| 493 | __ceph_finish_cap_snap(ci, capsnap); | ||
| 494 | } | ||
| 495 | } else { | ||
| 496 | dout("queue_cap_snap %p nothing dirty|writing\n", inode); | ||
| 497 | kfree(capsnap); | ||
| 498 | } | ||
| 499 | |||
| 500 | spin_unlock(&inode->i_lock); | ||
| 501 | } | ||
| 502 | |||
| 503 | /* | ||
| 504 | * Finalize the size, mtime for a cap_snap.. that is, settle on final values | ||
| 505 | * to be used for the snapshot, to be flushed back to the mds. | ||
| 506 | * | ||
| 507 | * If capsnap can now be flushed, add to snap_flush list, and return 1. | ||
| 508 | * | ||
| 509 | * Caller must hold i_lock. | ||
| 510 | */ | ||
| 511 | int __ceph_finish_cap_snap(struct ceph_inode_info *ci, | ||
| 512 | struct ceph_cap_snap *capsnap) | ||
| 513 | { | ||
| 514 | struct inode *inode = &ci->vfs_inode; | ||
| 515 | struct ceph_mds_client *mdsc = &ceph_client(inode->i_sb)->mdsc; | ||
| 516 | |||
| 517 | BUG_ON(capsnap->writing); | ||
| 518 | capsnap->size = inode->i_size; | ||
| 519 | capsnap->mtime = inode->i_mtime; | ||
| 520 | capsnap->atime = inode->i_atime; | ||
| 521 | capsnap->ctime = inode->i_ctime; | ||
| 522 | capsnap->time_warp_seq = ci->i_time_warp_seq; | ||
| 523 | if (capsnap->dirty_pages) { | ||
| 524 | dout("finish_cap_snap %p cap_snap %p snapc %p %llu s=%llu " | ||
| 525 | "still has %d dirty pages\n", inode, capsnap, | ||
| 526 | capsnap->context, capsnap->context->seq, | ||
| 527 | capsnap->size, capsnap->dirty_pages); | ||
| 528 | return 0; | ||
| 529 | } | ||
| 530 | dout("finish_cap_snap %p cap_snap %p snapc %p %llu s=%llu clean\n", | ||
| 531 | inode, capsnap, capsnap->context, | ||
| 532 | capsnap->context->seq, capsnap->size); | ||
| 533 | |||
| 534 | spin_lock(&mdsc->snap_flush_lock); | ||
| 535 | list_add_tail(&ci->i_snap_flush_item, &mdsc->snap_flush_list); | ||
| 536 | spin_unlock(&mdsc->snap_flush_lock); | ||
| 537 | return 1; /* caller may want to ceph_flush_snaps */ | ||
| 538 | } | ||
| 539 | |||
| 540 | |||
| 541 | /* | ||
| 542 | * Parse and apply a snapblob "snap trace" from the MDS. This specifies | ||
| 543 | * the snap realm parameters from a given realm and all of its ancestors, | ||
| 544 | * up to the root. | ||
| 545 | * | ||
| 546 | * Caller must hold snap_rwsem for write. | ||
| 547 | */ | ||
| 548 | int ceph_update_snap_trace(struct ceph_mds_client *mdsc, | ||
| 549 | void *p, void *e, bool deletion) | ||
| 550 | { | ||
| 551 | struct ceph_mds_snap_realm *ri; /* encoded */ | ||
| 552 | __le64 *snaps; /* encoded */ | ||
| 553 | __le64 *prior_parent_snaps; /* encoded */ | ||
| 554 | struct ceph_snap_realm *realm; | ||
| 555 | int invalidate = 0; | ||
| 556 | int err = -ENOMEM; | ||
| 557 | |||
| 558 | dout("update_snap_trace deletion=%d\n", deletion); | ||
| 559 | more: | ||
| 560 | ceph_decode_need(&p, e, sizeof(*ri), bad); | ||
| 561 | ri = p; | ||
| 562 | p += sizeof(*ri); | ||
| 563 | ceph_decode_need(&p, e, sizeof(u64)*(le32_to_cpu(ri->num_snaps) + | ||
| 564 | le32_to_cpu(ri->num_prior_parent_snaps)), bad); | ||
| 565 | snaps = p; | ||
| 566 | p += sizeof(u64) * le32_to_cpu(ri->num_snaps); | ||
| 567 | prior_parent_snaps = p; | ||
| 568 | p += sizeof(u64) * le32_to_cpu(ri->num_prior_parent_snaps); | ||
| 569 | |||
| 570 | realm = ceph_lookup_snap_realm(mdsc, le64_to_cpu(ri->ino)); | ||
| 571 | if (!realm) { | ||
| 572 | realm = ceph_create_snap_realm(mdsc, le64_to_cpu(ri->ino)); | ||
| 573 | if (IS_ERR(realm)) { | ||
| 574 | err = PTR_ERR(realm); | ||
| 575 | goto fail; | ||
| 576 | } | ||
| 577 | } | ||
| 578 | |||
| 579 | if (le64_to_cpu(ri->seq) > realm->seq) { | ||
| 580 | dout("update_snap_trace updating %llx %p %lld -> %lld\n", | ||
| 581 | realm->ino, realm, realm->seq, le64_to_cpu(ri->seq)); | ||
| 582 | /* | ||
| 583 | * if the realm seq has changed, queue a cap_snap for every | ||
| 584 | * inode with open caps. we do this _before_ we update | ||
| 585 | * the realm info so that we prepare for writeback under the | ||
| 586 | * _previous_ snap context. | ||
| 587 | * | ||
| 588 | * ...unless it's a snap deletion! | ||
| 589 | */ | ||
| 590 | if (!deletion) { | ||
| 591 | struct ceph_inode_info *ci; | ||
| 592 | struct inode *lastinode = NULL; | ||
| 593 | |||
| 594 | spin_lock(&realm->inodes_with_caps_lock); | ||
| 595 | list_for_each_entry(ci, &realm->inodes_with_caps, | ||
| 596 | i_snap_realm_item) { | ||
| 597 | struct inode *inode = igrab(&ci->vfs_inode); | ||
| 598 | if (!inode) | ||
| 599 | continue; | ||
| 600 | spin_unlock(&realm->inodes_with_caps_lock); | ||
| 601 | if (lastinode) | ||
| 602 | iput(lastinode); | ||
| 603 | lastinode = inode; | ||
| 604 | ceph_queue_cap_snap(ci, realm->cached_context); | ||
| 605 | spin_lock(&realm->inodes_with_caps_lock); | ||
| 606 | } | ||
| 607 | spin_unlock(&realm->inodes_with_caps_lock); | ||
| 608 | if (lastinode) | ||
| 609 | iput(lastinode); | ||
| 610 | dout("update_snap_trace cap_snaps queued\n"); | ||
| 611 | } | ||
| 612 | |||
| 613 | } else { | ||
| 614 | dout("update_snap_trace %llx %p seq %lld unchanged\n", | ||
| 615 | realm->ino, realm, realm->seq); | ||
| 616 | } | ||
| 617 | |||
| 618 | /* ensure the parent is correct */ | ||
| 619 | err = adjust_snap_realm_parent(mdsc, realm, le64_to_cpu(ri->parent)); | ||
| 620 | if (err < 0) | ||
| 621 | goto fail; | ||
| 622 | invalidate += err; | ||
| 623 | |||
| 624 | if (le64_to_cpu(ri->seq) > realm->seq) { | ||
| 625 | /* update realm parameters, snap lists */ | ||
| 626 | realm->seq = le64_to_cpu(ri->seq); | ||
| 627 | realm->created = le64_to_cpu(ri->created); | ||
| 628 | realm->parent_since = le64_to_cpu(ri->parent_since); | ||
| 629 | |||
| 630 | realm->num_snaps = le32_to_cpu(ri->num_snaps); | ||
| 631 | err = dup_array(&realm->snaps, snaps, realm->num_snaps); | ||
| 632 | if (err < 0) | ||
| 633 | goto fail; | ||
| 634 | |||
| 635 | realm->num_prior_parent_snaps = | ||
| 636 | le32_to_cpu(ri->num_prior_parent_snaps); | ||
| 637 | err = dup_array(&realm->prior_parent_snaps, prior_parent_snaps, | ||
| 638 | realm->num_prior_parent_snaps); | ||
| 639 | if (err < 0) | ||
| 640 | goto fail; | ||
| 641 | |||
| 642 | invalidate = 1; | ||
| 643 | } else if (!realm->cached_context) { | ||
| 644 | invalidate = 1; | ||
| 645 | } | ||
| 646 | |||
| 647 | dout("done with %llx %p, invalidated=%d, %p %p\n", realm->ino, | ||
| 648 | realm, invalidate, p, e); | ||
| 649 | |||
| 650 | if (p < e) | ||
| 651 | goto more; | ||
| 652 | |||
| 653 | /* invalidate when we reach the _end_ (root) of the trace */ | ||
| 654 | if (invalidate) | ||
| 655 | rebuild_snap_realms(realm); | ||
| 656 | |||
| 657 | __cleanup_empty_realms(mdsc); | ||
| 658 | return 0; | ||
| 659 | |||
| 660 | bad: | ||
| 661 | err = -EINVAL; | ||
| 662 | fail: | ||
| 663 | pr_err("update_snap_trace error %d\n", err); | ||
| 664 | return err; | ||
| 665 | } | ||
| 666 | |||
| 667 | |||
| 668 | /* | ||
| 669 | * Send any cap_snaps that are queued for flush. Try to carry | ||
| 670 | * s_mutex across multiple snap flushes to avoid locking overhead. | ||
| 671 | * | ||
| 672 | * Caller holds no locks. | ||
| 673 | */ | ||
| 674 | static void flush_snaps(struct ceph_mds_client *mdsc) | ||
| 675 | { | ||
| 676 | struct ceph_inode_info *ci; | ||
| 677 | struct inode *inode; | ||
| 678 | struct ceph_mds_session *session = NULL; | ||
| 679 | |||
| 680 | dout("flush_snaps\n"); | ||
| 681 | spin_lock(&mdsc->snap_flush_lock); | ||
| 682 | while (!list_empty(&mdsc->snap_flush_list)) { | ||
| 683 | ci = list_first_entry(&mdsc->snap_flush_list, | ||
| 684 | struct ceph_inode_info, i_snap_flush_item); | ||
| 685 | inode = &ci->vfs_inode; | ||
| 686 | igrab(inode); | ||
| 687 | spin_unlock(&mdsc->snap_flush_lock); | ||
| 688 | spin_lock(&inode->i_lock); | ||
| 689 | __ceph_flush_snaps(ci, &session); | ||
| 690 | spin_unlock(&inode->i_lock); | ||
| 691 | iput(inode); | ||
| 692 | spin_lock(&mdsc->snap_flush_lock); | ||
| 693 | } | ||
| 694 | spin_unlock(&mdsc->snap_flush_lock); | ||
| 695 | |||
| 696 | if (session) { | ||
| 697 | mutex_unlock(&session->s_mutex); | ||
| 698 | ceph_put_mds_session(session); | ||
| 699 | } | ||
| 700 | dout("flush_snaps done\n"); | ||
| 701 | } | ||
| 702 | |||
| 703 | |||
| 704 | /* | ||
| 705 | * Handle a snap notification from the MDS. | ||
| 706 | * | ||
| 707 | * This can take two basic forms: the simplest is just a snap creation | ||
| 708 | * or deletion notification on an existing realm. This should update the | ||
| 709 | * realm and its children. | ||
| 710 | * | ||
| 711 | * The more difficult case is realm creation, due to snap creation at a | ||
| 712 | * new point in the file hierarchy, or due to a rename that moves a file or | ||
| 713 | * directory into another realm. | ||
| 714 | */ | ||
| 715 | void ceph_handle_snap(struct ceph_mds_client *mdsc, | ||
| 716 | struct ceph_mds_session *session, | ||
| 717 | struct ceph_msg *msg) | ||
| 718 | { | ||
| 719 | struct super_block *sb = mdsc->client->sb; | ||
| 720 | int mds = session->s_mds; | ||
| 721 | u64 split; | ||
| 722 | int op; | ||
| 723 | int trace_len; | ||
| 724 | struct ceph_snap_realm *realm = NULL; | ||
| 725 | void *p = msg->front.iov_base; | ||
| 726 | void *e = p + msg->front.iov_len; | ||
| 727 | struct ceph_mds_snap_head *h; | ||
| 728 | int num_split_inos, num_split_realms; | ||
| 729 | __le64 *split_inos = NULL, *split_realms = NULL; | ||
| 730 | int i; | ||
| 731 | int locked_rwsem = 0; | ||
| 732 | |||
| 733 | /* decode */ | ||
| 734 | if (msg->front.iov_len < sizeof(*h)) | ||
| 735 | goto bad; | ||
| 736 | h = p; | ||
| 737 | op = le32_to_cpu(h->op); | ||
| 738 | split = le64_to_cpu(h->split); /* non-zero if we are splitting an | ||
| 739 | * existing realm */ | ||
| 740 | num_split_inos = le32_to_cpu(h->num_split_inos); | ||
| 741 | num_split_realms = le32_to_cpu(h->num_split_realms); | ||
| 742 | trace_len = le32_to_cpu(h->trace_len); | ||
| 743 | p += sizeof(*h); | ||
| 744 | |||
| 745 | dout("handle_snap from mds%d op %s split %llx tracelen %d\n", mds, | ||
| 746 | ceph_snap_op_name(op), split, trace_len); | ||
| 747 | |||
| 748 | mutex_lock(&session->s_mutex); | ||
| 749 | session->s_seq++; | ||
| 750 | mutex_unlock(&session->s_mutex); | ||
| 751 | |||
| 752 | down_write(&mdsc->snap_rwsem); | ||
| 753 | locked_rwsem = 1; | ||
| 754 | |||
| 755 | if (op == CEPH_SNAP_OP_SPLIT) { | ||
| 756 | struct ceph_mds_snap_realm *ri; | ||
| 757 | |||
| 758 | /* | ||
| 759 | * A "split" breaks part of an existing realm off into | ||
| 760 | * a new realm. The MDS provides a list of inodes | ||
| 761 | * (with caps) and child realms that belong to the new | ||
| 762 | * child. | ||
| 763 | */ | ||
| 764 | split_inos = p; | ||
| 765 | p += sizeof(u64) * num_split_inos; | ||
| 766 | split_realms = p; | ||
| 767 | p += sizeof(u64) * num_split_realms; | ||
| 768 | ceph_decode_need(&p, e, sizeof(*ri), bad); | ||
| 769 | /* we will peek at realm info here, but will _not_ | ||
| 770 | * advance p, as the realm update will occur below in | ||
| 771 | * ceph_update_snap_trace. */ | ||
| 772 | ri = p; | ||
| 773 | |||
| 774 | realm = ceph_lookup_snap_realm(mdsc, split); | ||
| 775 | if (!realm) { | ||
| 776 | realm = ceph_create_snap_realm(mdsc, split); | ||
| 777 | if (IS_ERR(realm)) | ||
| 778 | goto out; | ||
| 779 | } | ||
| 780 | ceph_get_snap_realm(mdsc, realm); | ||
| 781 | |||
| 782 | dout("splitting snap_realm %llx %p\n", realm->ino, realm); | ||
| 783 | for (i = 0; i < num_split_inos; i++) { | ||
| 784 | struct ceph_vino vino = { | ||
| 785 | .ino = le64_to_cpu(split_inos[i]), | ||
| 786 | .snap = CEPH_NOSNAP, | ||
| 787 | }; | ||
| 788 | struct inode *inode = ceph_find_inode(sb, vino); | ||
| 789 | struct ceph_inode_info *ci; | ||
| 790 | |||
| 791 | if (!inode) | ||
| 792 | continue; | ||
| 793 | ci = ceph_inode(inode); | ||
| 794 | |||
| 795 | spin_lock(&inode->i_lock); | ||
| 796 | if (!ci->i_snap_realm) | ||
| 797 | goto skip_inode; | ||
| 798 | /* | ||
| 799 | * If this inode belongs to a realm that was | ||
| 800 | * created after our new realm, we experienced | ||
| 801 | * a race (due to another split notifications | ||
| 802 | * arriving from a different MDS). So skip | ||
| 803 | * this inode. | ||
| 804 | */ | ||
| 805 | if (ci->i_snap_realm->created > | ||
| 806 | le64_to_cpu(ri->created)) { | ||
| 807 | dout(" leaving %p in newer realm %llx %p\n", | ||
| 808 | inode, ci->i_snap_realm->ino, | ||
| 809 | ci->i_snap_realm); | ||
| 810 | goto skip_inode; | ||
| 811 | } | ||
| 812 | dout(" will move %p to split realm %llx %p\n", | ||
| 813 | inode, realm->ino, realm); | ||
| 814 | /* | ||
| 815 | * Remove the inode from the realm's inode | ||
| 816 | * list, but don't add it to the new realm | ||
| 817 | * yet. We don't want the cap_snap to be | ||
| 818 | * queued (again) by ceph_update_snap_trace() | ||
| 819 | * below. Queue it _now_, under the old context. | ||
| 820 | */ | ||
| 821 | spin_lock(&realm->inodes_with_caps_lock); | ||
| 822 | list_del_init(&ci->i_snap_realm_item); | ||
| 823 | spin_unlock(&realm->inodes_with_caps_lock); | ||
| 824 | spin_unlock(&inode->i_lock); | ||
| 825 | |||
| 826 | ceph_queue_cap_snap(ci, | ||
| 827 | ci->i_snap_realm->cached_context); | ||
| 828 | |||
| 829 | iput(inode); | ||
| 830 | continue; | ||
| 831 | |||
| 832 | skip_inode: | ||
| 833 | spin_unlock(&inode->i_lock); | ||
| 834 | iput(inode); | ||
| 835 | } | ||
| 836 | |||
| 837 | /* we may have taken some of the old realm's children. */ | ||
| 838 | for (i = 0; i < num_split_realms; i++) { | ||
| 839 | struct ceph_snap_realm *child = | ||
| 840 | ceph_lookup_snap_realm(mdsc, | ||
| 841 | le64_to_cpu(split_realms[i])); | ||
| 842 | if (!child) | ||
| 843 | continue; | ||
| 844 | adjust_snap_realm_parent(mdsc, child, realm->ino); | ||
| 845 | } | ||
| 846 | } | ||
| 847 | |||
| 848 | /* | ||
| 849 | * update using the provided snap trace. if we are deleting a | ||
| 850 | * snap, we can avoid queueing cap_snaps. | ||
| 851 | */ | ||
| 852 | ceph_update_snap_trace(mdsc, p, e, | ||
| 853 | op == CEPH_SNAP_OP_DESTROY); | ||
| 854 | |||
| 855 | if (op == CEPH_SNAP_OP_SPLIT) { | ||
| 856 | /* | ||
| 857 | * ok, _now_ add the inodes into the new realm. | ||
| 858 | */ | ||
| 859 | for (i = 0; i < num_split_inos; i++) { | ||
| 860 | struct ceph_vino vino = { | ||
| 861 | .ino = le64_to_cpu(split_inos[i]), | ||
| 862 | .snap = CEPH_NOSNAP, | ||
| 863 | }; | ||
| 864 | struct inode *inode = ceph_find_inode(sb, vino); | ||
| 865 | struct ceph_inode_info *ci; | ||
| 866 | |||
| 867 | if (!inode) | ||
| 868 | continue; | ||
| 869 | ci = ceph_inode(inode); | ||
| 870 | spin_lock(&inode->i_lock); | ||
| 871 | if (!ci->i_snap_realm) | ||
| 872 | goto split_skip_inode; | ||
| 873 | ceph_put_snap_realm(mdsc, ci->i_snap_realm); | ||
| 874 | spin_lock(&realm->inodes_with_caps_lock); | ||
| 875 | list_add(&ci->i_snap_realm_item, | ||
| 876 | &realm->inodes_with_caps); | ||
| 877 | ci->i_snap_realm = realm; | ||
| 878 | spin_unlock(&realm->inodes_with_caps_lock); | ||
| 879 | ceph_get_snap_realm(mdsc, realm); | ||
| 880 | split_skip_inode: | ||
| 881 | spin_unlock(&inode->i_lock); | ||
| 882 | iput(inode); | ||
| 883 | } | ||
| 884 | |||
| 885 | /* we took a reference when we created the realm, above */ | ||
| 886 | ceph_put_snap_realm(mdsc, realm); | ||
| 887 | } | ||
| 888 | |||
| 889 | __cleanup_empty_realms(mdsc); | ||
| 890 | |||
| 891 | up_write(&mdsc->snap_rwsem); | ||
| 892 | |||
| 893 | flush_snaps(mdsc); | ||
| 894 | return; | ||
| 895 | |||
| 896 | bad: | ||
| 897 | pr_err("corrupt snap message from mds%d\n", mds); | ||
| 898 | ceph_msg_dump(msg); | ||
| 899 | out: | ||
| 900 | if (locked_rwsem) | ||
| 901 | up_write(&mdsc->snap_rwsem); | ||
| 902 | return; | ||
| 903 | } | ||
| 904 | |||
| 905 | |||
| 906 | |||
diff --git a/fs/ceph/super.c b/fs/ceph/super.c new file mode 100644 index 000000000000..4290a6e860b0 --- /dev/null +++ b/fs/ceph/super.c | |||
| @@ -0,0 +1,1030 @@ | |||
| 1 | |||
| 2 | #include "ceph_debug.h" | ||
| 3 | |||
| 4 | #include <linux/backing-dev.h> | ||
| 5 | #include <linux/fs.h> | ||
| 6 | #include <linux/inet.h> | ||
| 7 | #include <linux/in6.h> | ||
| 8 | #include <linux/module.h> | ||
| 9 | #include <linux/mount.h> | ||
| 10 | #include <linux/parser.h> | ||
| 11 | #include <linux/rwsem.h> | ||
| 12 | #include <linux/sched.h> | ||
| 13 | #include <linux/seq_file.h> | ||
| 14 | #include <linux/statfs.h> | ||
| 15 | #include <linux/string.h> | ||
| 16 | #include <linux/version.h> | ||
| 17 | #include <linux/vmalloc.h> | ||
| 18 | |||
| 19 | #include "decode.h" | ||
| 20 | #include "super.h" | ||
| 21 | #include "mon_client.h" | ||
| 22 | #include "auth.h" | ||
| 23 | |||
| 24 | /* | ||
| 25 | * Ceph superblock operations | ||
| 26 | * | ||
| 27 | * Handle the basics of mounting, unmounting. | ||
| 28 | */ | ||
| 29 | |||
| 30 | |||
| 31 | /* | ||
| 32 | * find filename portion of a path (/foo/bar/baz -> baz) | ||
| 33 | */ | ||
| 34 | const char *ceph_file_part(const char *s, int len) | ||
| 35 | { | ||
| 36 | const char *e = s + len; | ||
| 37 | |||
| 38 | while (e != s && *(e-1) != '/') | ||
| 39 | e--; | ||
| 40 | return e; | ||
| 41 | } | ||
| 42 | |||
| 43 | |||
| 44 | /* | ||
| 45 | * super ops | ||
| 46 | */ | ||
| 47 | static void ceph_put_super(struct super_block *s) | ||
| 48 | { | ||
| 49 | struct ceph_client *cl = ceph_client(s); | ||
| 50 | |||
| 51 | dout("put_super\n"); | ||
| 52 | ceph_mdsc_close_sessions(&cl->mdsc); | ||
| 53 | return; | ||
| 54 | } | ||
| 55 | |||
| 56 | static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf) | ||
| 57 | { | ||
| 58 | struct ceph_client *client = ceph_inode_to_client(dentry->d_inode); | ||
| 59 | struct ceph_monmap *monmap = client->monc.monmap; | ||
| 60 | struct ceph_statfs st; | ||
| 61 | u64 fsid; | ||
| 62 | int err; | ||
| 63 | |||
| 64 | dout("statfs\n"); | ||
| 65 | err = ceph_monc_do_statfs(&client->monc, &st); | ||
| 66 | if (err < 0) | ||
| 67 | return err; | ||
| 68 | |||
| 69 | /* fill in kstatfs */ | ||
| 70 | buf->f_type = CEPH_SUPER_MAGIC; /* ?? */ | ||
| 71 | |||
| 72 | /* | ||
| 73 | * express utilization in terms of large blocks to avoid | ||
| 74 | * overflow on 32-bit machines. | ||
| 75 | */ | ||
| 76 | buf->f_bsize = 1 << CEPH_BLOCK_SHIFT; | ||
| 77 | buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10); | ||
| 78 | buf->f_bfree = (le64_to_cpu(st.kb) - le64_to_cpu(st.kb_used)) >> | ||
| 79 | (CEPH_BLOCK_SHIFT-10); | ||
| 80 | buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10); | ||
| 81 | |||
| 82 | buf->f_files = le64_to_cpu(st.num_objects); | ||
| 83 | buf->f_ffree = -1; | ||
| 84 | buf->f_namelen = PATH_MAX; | ||
| 85 | buf->f_frsize = PAGE_CACHE_SIZE; | ||
| 86 | |||
| 87 | /* leave fsid little-endian, regardless of host endianness */ | ||
| 88 | fsid = *(u64 *)(&monmap->fsid) ^ *((u64 *)&monmap->fsid + 1); | ||
| 89 | buf->f_fsid.val[0] = fsid & 0xffffffff; | ||
| 90 | buf->f_fsid.val[1] = fsid >> 32; | ||
| 91 | |||
| 92 | return 0; | ||
| 93 | } | ||
| 94 | |||
| 95 | |||
| 96 | static int ceph_syncfs(struct super_block *sb, int wait) | ||
| 97 | { | ||
| 98 | dout("sync_fs %d\n", wait); | ||
| 99 | ceph_osdc_sync(&ceph_client(sb)->osdc); | ||
| 100 | ceph_mdsc_sync(&ceph_client(sb)->mdsc); | ||
| 101 | dout("sync_fs %d done\n", wait); | ||
| 102 | return 0; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | /** | ||
| 107 | * ceph_show_options - Show mount options in /proc/mounts | ||
| 108 | * @m: seq_file to write to | ||
| 109 | * @mnt: mount descriptor | ||
| 110 | */ | ||
| 111 | static int ceph_show_options(struct seq_file *m, struct vfsmount *mnt) | ||
| 112 | { | ||
| 113 | struct ceph_client *client = ceph_sb_to_client(mnt->mnt_sb); | ||
| 114 | struct ceph_mount_args *args = client->mount_args; | ||
| 115 | |||
| 116 | if (args->flags & CEPH_OPT_FSID) | ||
| 117 | seq_printf(m, ",fsidmajor=%llu,fsidminor%llu", | ||
| 118 | le64_to_cpu(*(__le64 *)&args->fsid.fsid[0]), | ||
| 119 | le64_to_cpu(*(__le64 *)&args->fsid.fsid[8])); | ||
| 120 | if (args->flags & CEPH_OPT_NOSHARE) | ||
| 121 | seq_puts(m, ",noshare"); | ||
| 122 | if (args->flags & CEPH_OPT_DIRSTAT) | ||
| 123 | seq_puts(m, ",dirstat"); | ||
| 124 | if ((args->flags & CEPH_OPT_RBYTES) == 0) | ||
| 125 | seq_puts(m, ",norbytes"); | ||
| 126 | if (args->flags & CEPH_OPT_NOCRC) | ||
| 127 | seq_puts(m, ",nocrc"); | ||
| 128 | if (args->flags & CEPH_OPT_NOASYNCREADDIR) | ||
| 129 | seq_puts(m, ",noasyncreaddir"); | ||
| 130 | if (strcmp(args->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT)) | ||
| 131 | seq_printf(m, ",snapdirname=%s", args->snapdir_name); | ||
| 132 | if (args->name) | ||
| 133 | seq_printf(m, ",name=%s", args->name); | ||
| 134 | if (args->secret) | ||
| 135 | seq_puts(m, ",secret=<hidden>"); | ||
| 136 | return 0; | ||
| 137 | } | ||
| 138 | |||
| 139 | /* | ||
| 140 | * caches | ||
| 141 | */ | ||
| 142 | struct kmem_cache *ceph_inode_cachep; | ||
| 143 | struct kmem_cache *ceph_cap_cachep; | ||
| 144 | struct kmem_cache *ceph_dentry_cachep; | ||
| 145 | struct kmem_cache *ceph_file_cachep; | ||
| 146 | |||
| 147 | static void ceph_inode_init_once(void *foo) | ||
| 148 | { | ||
| 149 | struct ceph_inode_info *ci = foo; | ||
| 150 | inode_init_once(&ci->vfs_inode); | ||
| 151 | } | ||
| 152 | |||
| 153 | static int default_congestion_kb(void) | ||
| 154 | { | ||
| 155 | int congestion_kb; | ||
| 156 | |||
| 157 | /* | ||
| 158 | * Copied from NFS | ||
| 159 | * | ||
| 160 | * congestion size, scale with available memory. | ||
| 161 | * | ||
| 162 | * 64MB: 8192k | ||
| 163 | * 128MB: 11585k | ||
| 164 | * 256MB: 16384k | ||
| 165 | * 512MB: 23170k | ||
| 166 | * 1GB: 32768k | ||
| 167 | * 2GB: 46340k | ||
| 168 | * 4GB: 65536k | ||
| 169 | * 8GB: 92681k | ||
| 170 | * 16GB: 131072k | ||
| 171 | * | ||
| 172 | * This allows larger machines to have larger/more transfers. | ||
| 173 | * Limit the default to 256M | ||
| 174 | */ | ||
| 175 | congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10); | ||
| 176 | if (congestion_kb > 256*1024) | ||
| 177 | congestion_kb = 256*1024; | ||
| 178 | |||
| 179 | return congestion_kb; | ||
| 180 | } | ||
| 181 | |||
| 182 | static int __init init_caches(void) | ||
| 183 | { | ||
| 184 | ceph_inode_cachep = kmem_cache_create("ceph_inode_info", | ||
| 185 | sizeof(struct ceph_inode_info), | ||
| 186 | __alignof__(struct ceph_inode_info), | ||
| 187 | (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD), | ||
| 188 | ceph_inode_init_once); | ||
| 189 | if (ceph_inode_cachep == NULL) | ||
| 190 | return -ENOMEM; | ||
| 191 | |||
| 192 | ceph_cap_cachep = KMEM_CACHE(ceph_cap, | ||
| 193 | SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD); | ||
| 194 | if (ceph_cap_cachep == NULL) | ||
| 195 | goto bad_cap; | ||
| 196 | |||
| 197 | ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info, | ||
| 198 | SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD); | ||
| 199 | if (ceph_dentry_cachep == NULL) | ||
| 200 | goto bad_dentry; | ||
| 201 | |||
| 202 | ceph_file_cachep = KMEM_CACHE(ceph_file_info, | ||
| 203 | SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD); | ||
| 204 | if (ceph_file_cachep == NULL) | ||
| 205 | goto bad_file; | ||
| 206 | |||
| 207 | return 0; | ||
| 208 | |||
| 209 | bad_file: | ||
| 210 | kmem_cache_destroy(ceph_dentry_cachep); | ||
| 211 | bad_dentry: | ||
| 212 | kmem_cache_destroy(ceph_cap_cachep); | ||
| 213 | bad_cap: | ||
| 214 | kmem_cache_destroy(ceph_inode_cachep); | ||
| 215 | return -ENOMEM; | ||
| 216 | } | ||
| 217 | |||
| 218 | static void destroy_caches(void) | ||
| 219 | { | ||
| 220 | kmem_cache_destroy(ceph_inode_cachep); | ||
| 221 | kmem_cache_destroy(ceph_cap_cachep); | ||
| 222 | kmem_cache_destroy(ceph_dentry_cachep); | ||
| 223 | kmem_cache_destroy(ceph_file_cachep); | ||
| 224 | } | ||
| 225 | |||
| 226 | |||
| 227 | /* | ||
| 228 | * ceph_umount_begin - initiate forced umount. Tear down down the | ||
| 229 | * mount, skipping steps that may hang while waiting for server(s). | ||
| 230 | */ | ||
| 231 | static void ceph_umount_begin(struct super_block *sb) | ||
| 232 | { | ||
| 233 | struct ceph_client *client = ceph_sb_to_client(sb); | ||
| 234 | |||
| 235 | dout("ceph_umount_begin - starting forced umount\n"); | ||
| 236 | if (!client) | ||
| 237 | return; | ||
| 238 | client->mount_state = CEPH_MOUNT_SHUTDOWN; | ||
| 239 | return; | ||
| 240 | } | ||
| 241 | |||
| 242 | static const struct super_operations ceph_super_ops = { | ||
| 243 | .alloc_inode = ceph_alloc_inode, | ||
| 244 | .destroy_inode = ceph_destroy_inode, | ||
| 245 | .write_inode = ceph_write_inode, | ||
| 246 | .sync_fs = ceph_syncfs, | ||
| 247 | .put_super = ceph_put_super, | ||
| 248 | .show_options = ceph_show_options, | ||
| 249 | .statfs = ceph_statfs, | ||
| 250 | .umount_begin = ceph_umount_begin, | ||
| 251 | }; | ||
| 252 | |||
| 253 | |||
| 254 | const char *ceph_msg_type_name(int type) | ||
| 255 | { | ||
| 256 | switch (type) { | ||
| 257 | case CEPH_MSG_SHUTDOWN: return "shutdown"; | ||
| 258 | case CEPH_MSG_PING: return "ping"; | ||
| 259 | case CEPH_MSG_AUTH: return "auth"; | ||
| 260 | case CEPH_MSG_AUTH_REPLY: return "auth_reply"; | ||
| 261 | case CEPH_MSG_MON_MAP: return "mon_map"; | ||
| 262 | case CEPH_MSG_MON_GET_MAP: return "mon_get_map"; | ||
| 263 | case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe"; | ||
| 264 | case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack"; | ||
| 265 | case CEPH_MSG_STATFS: return "statfs"; | ||
| 266 | case CEPH_MSG_STATFS_REPLY: return "statfs_reply"; | ||
| 267 | case CEPH_MSG_MDS_MAP: return "mds_map"; | ||
| 268 | case CEPH_MSG_CLIENT_SESSION: return "client_session"; | ||
| 269 | case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect"; | ||
| 270 | case CEPH_MSG_CLIENT_REQUEST: return "client_request"; | ||
| 271 | case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward"; | ||
| 272 | case CEPH_MSG_CLIENT_REPLY: return "client_reply"; | ||
| 273 | case CEPH_MSG_CLIENT_CAPS: return "client_caps"; | ||
| 274 | case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release"; | ||
| 275 | case CEPH_MSG_CLIENT_SNAP: return "client_snap"; | ||
| 276 | case CEPH_MSG_CLIENT_LEASE: return "client_lease"; | ||
| 277 | case CEPH_MSG_OSD_MAP: return "osd_map"; | ||
| 278 | case CEPH_MSG_OSD_OP: return "osd_op"; | ||
| 279 | case CEPH_MSG_OSD_OPREPLY: return "osd_opreply"; | ||
| 280 | default: return "unknown"; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | |||
| 284 | |||
| 285 | /* | ||
| 286 | * mount options | ||
| 287 | */ | ||
| 288 | enum { | ||
| 289 | Opt_fsidmajor, | ||
| 290 | Opt_fsidminor, | ||
| 291 | Opt_monport, | ||
| 292 | Opt_wsize, | ||
| 293 | Opt_rsize, | ||
| 294 | Opt_osdtimeout, | ||
| 295 | Opt_osdkeepalivetimeout, | ||
| 296 | Opt_mount_timeout, | ||
| 297 | Opt_osd_idle_ttl, | ||
| 298 | Opt_caps_wanted_delay_min, | ||
| 299 | Opt_caps_wanted_delay_max, | ||
| 300 | Opt_readdir_max_entries, | ||
| 301 | Opt_congestion_kb, | ||
| 302 | Opt_last_int, | ||
| 303 | /* int args above */ | ||
| 304 | Opt_snapdirname, | ||
| 305 | Opt_name, | ||
| 306 | Opt_secret, | ||
| 307 | Opt_last_string, | ||
| 308 | /* string args above */ | ||
| 309 | Opt_ip, | ||
| 310 | Opt_noshare, | ||
| 311 | Opt_dirstat, | ||
| 312 | Opt_nodirstat, | ||
| 313 | Opt_rbytes, | ||
| 314 | Opt_norbytes, | ||
| 315 | Opt_nocrc, | ||
| 316 | Opt_noasyncreaddir, | ||
| 317 | }; | ||
| 318 | |||
| 319 | static match_table_t arg_tokens = { | ||
| 320 | {Opt_fsidmajor, "fsidmajor=%ld"}, | ||
| 321 | {Opt_fsidminor, "fsidminor=%ld"}, | ||
| 322 | {Opt_monport, "monport=%d"}, | ||
| 323 | {Opt_wsize, "wsize=%d"}, | ||
| 324 | {Opt_rsize, "rsize=%d"}, | ||
| 325 | {Opt_osdtimeout, "osdtimeout=%d"}, | ||
| 326 | {Opt_osdkeepalivetimeout, "osdkeepalive=%d"}, | ||
| 327 | {Opt_mount_timeout, "mount_timeout=%d"}, | ||
| 328 | {Opt_osd_idle_ttl, "osd_idle_ttl=%d"}, | ||
| 329 | {Opt_caps_wanted_delay_min, "caps_wanted_delay_min=%d"}, | ||
| 330 | {Opt_caps_wanted_delay_max, "caps_wanted_delay_max=%d"}, | ||
| 331 | {Opt_readdir_max_entries, "readdir_max_entries=%d"}, | ||
| 332 | {Opt_congestion_kb, "write_congestion_kb=%d"}, | ||
| 333 | /* int args above */ | ||
| 334 | {Opt_snapdirname, "snapdirname=%s"}, | ||
| 335 | {Opt_name, "name=%s"}, | ||
| 336 | {Opt_secret, "secret=%s"}, | ||
| 337 | /* string args above */ | ||
| 338 | {Opt_ip, "ip=%s"}, | ||
| 339 | {Opt_noshare, "noshare"}, | ||
| 340 | {Opt_dirstat, "dirstat"}, | ||
| 341 | {Opt_nodirstat, "nodirstat"}, | ||
| 342 | {Opt_rbytes, "rbytes"}, | ||
| 343 | {Opt_norbytes, "norbytes"}, | ||
| 344 | {Opt_nocrc, "nocrc"}, | ||
| 345 | {Opt_noasyncreaddir, "noasyncreaddir"}, | ||
| 346 | {-1, NULL} | ||
| 347 | }; | ||
| 348 | |||
| 349 | |||
| 350 | static struct ceph_mount_args *parse_mount_args(int flags, char *options, | ||
| 351 | const char *dev_name, | ||
| 352 | const char **path) | ||
| 353 | { | ||
| 354 | struct ceph_mount_args *args; | ||
| 355 | const char *c; | ||
| 356 | int err = -ENOMEM; | ||
| 357 | substring_t argstr[MAX_OPT_ARGS]; | ||
| 358 | |||
| 359 | args = kzalloc(sizeof(*args), GFP_KERNEL); | ||
| 360 | if (!args) | ||
| 361 | return ERR_PTR(-ENOMEM); | ||
| 362 | args->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*args->mon_addr), | ||
| 363 | GFP_KERNEL); | ||
| 364 | if (!args->mon_addr) | ||
| 365 | goto out; | ||
| 366 | |||
| 367 | dout("parse_mount_args %p, dev_name '%s'\n", args, dev_name); | ||
| 368 | |||
| 369 | /* start with defaults */ | ||
| 370 | args->sb_flags = flags; | ||
| 371 | args->flags = CEPH_OPT_DEFAULT; | ||
| 372 | args->osd_timeout = CEPH_OSD_TIMEOUT_DEFAULT; | ||
| 373 | args->osd_keepalive_timeout = CEPH_OSD_KEEPALIVE_DEFAULT; | ||
| 374 | args->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT; /* seconds */ | ||
| 375 | args->osd_idle_ttl = CEPH_OSD_IDLE_TTL_DEFAULT; /* seconds */ | ||
| 376 | args->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT; | ||
| 377 | args->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT; | ||
| 378 | args->rsize = CEPH_MOUNT_RSIZE_DEFAULT; | ||
| 379 | args->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL); | ||
| 380 | args->cap_release_safety = CEPH_CAPS_PER_RELEASE * 4; | ||
| 381 | args->max_readdir = 1024; | ||
| 382 | args->congestion_kb = default_congestion_kb(); | ||
| 383 | |||
| 384 | /* ip1[:port1][,ip2[:port2]...]:/subdir/in/fs */ | ||
| 385 | err = -EINVAL; | ||
| 386 | if (!dev_name) | ||
| 387 | goto out; | ||
| 388 | *path = strstr(dev_name, ":/"); | ||
| 389 | if (*path == NULL) { | ||
| 390 | pr_err("device name is missing path (no :/ in %s)\n", | ||
| 391 | dev_name); | ||
| 392 | goto out; | ||
| 393 | } | ||
| 394 | |||
| 395 | /* get mon ip(s) */ | ||
| 396 | err = ceph_parse_ips(dev_name, *path, args->mon_addr, | ||
| 397 | CEPH_MAX_MON, &args->num_mon); | ||
| 398 | if (err < 0) | ||
| 399 | goto out; | ||
| 400 | |||
| 401 | /* path on server */ | ||
| 402 | *path += 2; | ||
| 403 | dout("server path '%s'\n", *path); | ||
| 404 | |||
| 405 | /* parse mount options */ | ||
| 406 | while ((c = strsep(&options, ",")) != NULL) { | ||
| 407 | int token, intval, ret; | ||
| 408 | if (!*c) | ||
| 409 | continue; | ||
| 410 | err = -EINVAL; | ||
| 411 | token = match_token((char *)c, arg_tokens, argstr); | ||
| 412 | if (token < 0) { | ||
| 413 | pr_err("bad mount option at '%s'\n", c); | ||
| 414 | goto out; | ||
| 415 | } | ||
| 416 | if (token < Opt_last_int) { | ||
| 417 | ret = match_int(&argstr[0], &intval); | ||
| 418 | if (ret < 0) { | ||
| 419 | pr_err("bad mount option arg (not int) " | ||
| 420 | "at '%s'\n", c); | ||
| 421 | continue; | ||
| 422 | } | ||
| 423 | dout("got int token %d val %d\n", token, intval); | ||
| 424 | } else if (token > Opt_last_int && token < Opt_last_string) { | ||
| 425 | dout("got string token %d val %s\n", token, | ||
| 426 | argstr[0].from); | ||
| 427 | } else { | ||
| 428 | dout("got token %d\n", token); | ||
| 429 | } | ||
| 430 | switch (token) { | ||
| 431 | case Opt_fsidmajor: | ||
| 432 | *(__le64 *)&args->fsid.fsid[0] = cpu_to_le64(intval); | ||
| 433 | break; | ||
| 434 | case Opt_fsidminor: | ||
| 435 | *(__le64 *)&args->fsid.fsid[8] = cpu_to_le64(intval); | ||
| 436 | break; | ||
| 437 | case Opt_ip: | ||
| 438 | err = ceph_parse_ips(argstr[0].from, | ||
| 439 | argstr[0].to, | ||
| 440 | &args->my_addr, | ||
| 441 | 1, NULL); | ||
| 442 | if (err < 0) | ||
| 443 | goto out; | ||
| 444 | args->flags |= CEPH_OPT_MYIP; | ||
| 445 | break; | ||
| 446 | |||
| 447 | case Opt_snapdirname: | ||
| 448 | kfree(args->snapdir_name); | ||
| 449 | args->snapdir_name = kstrndup(argstr[0].from, | ||
| 450 | argstr[0].to-argstr[0].from, | ||
| 451 | GFP_KERNEL); | ||
| 452 | break; | ||
| 453 | case Opt_name: | ||
| 454 | args->name = kstrndup(argstr[0].from, | ||
| 455 | argstr[0].to-argstr[0].from, | ||
| 456 | GFP_KERNEL); | ||
| 457 | break; | ||
| 458 | case Opt_secret: | ||
| 459 | args->secret = kstrndup(argstr[0].from, | ||
| 460 | argstr[0].to-argstr[0].from, | ||
| 461 | GFP_KERNEL); | ||
| 462 | break; | ||
| 463 | |||
| 464 | /* misc */ | ||
| 465 | case Opt_wsize: | ||
| 466 | args->wsize = intval; | ||
| 467 | break; | ||
| 468 | case Opt_rsize: | ||
| 469 | args->rsize = intval; | ||
| 470 | break; | ||
| 471 | case Opt_osdtimeout: | ||
| 472 | args->osd_timeout = intval; | ||
| 473 | break; | ||
| 474 | case Opt_osdkeepalivetimeout: | ||
| 475 | args->osd_keepalive_timeout = intval; | ||
| 476 | break; | ||
| 477 | case Opt_mount_timeout: | ||
| 478 | args->mount_timeout = intval; | ||
| 479 | break; | ||
| 480 | case Opt_caps_wanted_delay_min: | ||
| 481 | args->caps_wanted_delay_min = intval; | ||
| 482 | break; | ||
| 483 | case Opt_caps_wanted_delay_max: | ||
| 484 | args->caps_wanted_delay_max = intval; | ||
| 485 | break; | ||
| 486 | case Opt_readdir_max_entries: | ||
| 487 | args->max_readdir = intval; | ||
| 488 | break; | ||
| 489 | case Opt_congestion_kb: | ||
| 490 | args->congestion_kb = intval; | ||
| 491 | break; | ||
| 492 | |||
| 493 | case Opt_noshare: | ||
| 494 | args->flags |= CEPH_OPT_NOSHARE; | ||
| 495 | break; | ||
| 496 | |||
| 497 | case Opt_dirstat: | ||
| 498 | args->flags |= CEPH_OPT_DIRSTAT; | ||
| 499 | break; | ||
| 500 | case Opt_nodirstat: | ||
| 501 | args->flags &= ~CEPH_OPT_DIRSTAT; | ||
| 502 | break; | ||
| 503 | case Opt_rbytes: | ||
| 504 | args->flags |= CEPH_OPT_RBYTES; | ||
| 505 | break; | ||
| 506 | case Opt_norbytes: | ||
| 507 | args->flags &= ~CEPH_OPT_RBYTES; | ||
| 508 | break; | ||
| 509 | case Opt_nocrc: | ||
| 510 | args->flags |= CEPH_OPT_NOCRC; | ||
| 511 | break; | ||
| 512 | case Opt_noasyncreaddir: | ||
| 513 | args->flags |= CEPH_OPT_NOASYNCREADDIR; | ||
| 514 | break; | ||
| 515 | |||
| 516 | default: | ||
| 517 | BUG_ON(token); | ||
| 518 | } | ||
| 519 | } | ||
| 520 | return args; | ||
| 521 | |||
| 522 | out: | ||
| 523 | kfree(args->mon_addr); | ||
| 524 | kfree(args); | ||
| 525 | return ERR_PTR(err); | ||
| 526 | } | ||
| 527 | |||
| 528 | static void destroy_mount_args(struct ceph_mount_args *args) | ||
| 529 | { | ||
| 530 | dout("destroy_mount_args %p\n", args); | ||
| 531 | kfree(args->snapdir_name); | ||
| 532 | args->snapdir_name = NULL; | ||
| 533 | kfree(args->name); | ||
| 534 | args->name = NULL; | ||
| 535 | kfree(args->secret); | ||
| 536 | args->secret = NULL; | ||
| 537 | kfree(args); | ||
| 538 | } | ||
| 539 | |||
| 540 | /* | ||
| 541 | * create a fresh client instance | ||
| 542 | */ | ||
| 543 | static struct ceph_client *ceph_create_client(struct ceph_mount_args *args) | ||
| 544 | { | ||
| 545 | struct ceph_client *client; | ||
| 546 | int err = -ENOMEM; | ||
| 547 | |||
| 548 | client = kzalloc(sizeof(*client), GFP_KERNEL); | ||
| 549 | if (client == NULL) | ||
| 550 | return ERR_PTR(-ENOMEM); | ||
| 551 | |||
| 552 | mutex_init(&client->mount_mutex); | ||
| 553 | |||
| 554 | init_waitqueue_head(&client->auth_wq); | ||
| 555 | |||
| 556 | client->sb = NULL; | ||
| 557 | client->mount_state = CEPH_MOUNT_MOUNTING; | ||
| 558 | client->mount_args = args; | ||
| 559 | |||
| 560 | client->msgr = NULL; | ||
| 561 | |||
| 562 | client->auth_err = 0; | ||
| 563 | atomic_long_set(&client->writeback_count, 0); | ||
| 564 | |||
| 565 | err = bdi_init(&client->backing_dev_info); | ||
| 566 | if (err < 0) | ||
| 567 | goto fail; | ||
| 568 | |||
| 569 | err = -ENOMEM; | ||
| 570 | client->wb_wq = create_workqueue("ceph-writeback"); | ||
| 571 | if (client->wb_wq == NULL) | ||
| 572 | goto fail_bdi; | ||
| 573 | client->pg_inv_wq = create_singlethread_workqueue("ceph-pg-invalid"); | ||
| 574 | if (client->pg_inv_wq == NULL) | ||
| 575 | goto fail_wb_wq; | ||
| 576 | client->trunc_wq = create_singlethread_workqueue("ceph-trunc"); | ||
| 577 | if (client->trunc_wq == NULL) | ||
| 578 | goto fail_pg_inv_wq; | ||
| 579 | |||
| 580 | /* set up mempools */ | ||
| 581 | err = -ENOMEM; | ||
| 582 | client->wb_pagevec_pool = mempool_create_kmalloc_pool(10, | ||
| 583 | client->mount_args->wsize >> PAGE_CACHE_SHIFT); | ||
| 584 | if (!client->wb_pagevec_pool) | ||
| 585 | goto fail_trunc_wq; | ||
| 586 | |||
| 587 | /* caps */ | ||
| 588 | client->min_caps = args->max_readdir; | ||
| 589 | ceph_adjust_min_caps(client->min_caps); | ||
| 590 | |||
| 591 | /* subsystems */ | ||
| 592 | err = ceph_monc_init(&client->monc, client); | ||
| 593 | if (err < 0) | ||
| 594 | goto fail_mempool; | ||
| 595 | err = ceph_osdc_init(&client->osdc, client); | ||
| 596 | if (err < 0) | ||
| 597 | goto fail_monc; | ||
| 598 | err = ceph_mdsc_init(&client->mdsc, client); | ||
| 599 | if (err < 0) | ||
| 600 | goto fail_osdc; | ||
| 601 | return client; | ||
| 602 | |||
| 603 | fail_osdc: | ||
| 604 | ceph_osdc_stop(&client->osdc); | ||
| 605 | fail_monc: | ||
| 606 | ceph_monc_stop(&client->monc); | ||
| 607 | fail_mempool: | ||
| 608 | mempool_destroy(client->wb_pagevec_pool); | ||
| 609 | fail_trunc_wq: | ||
| 610 | destroy_workqueue(client->trunc_wq); | ||
| 611 | fail_pg_inv_wq: | ||
| 612 | destroy_workqueue(client->pg_inv_wq); | ||
| 613 | fail_wb_wq: | ||
| 614 | destroy_workqueue(client->wb_wq); | ||
| 615 | fail_bdi: | ||
| 616 | bdi_destroy(&client->backing_dev_info); | ||
| 617 | fail: | ||
| 618 | kfree(client); | ||
| 619 | return ERR_PTR(err); | ||
| 620 | } | ||
| 621 | |||
| 622 | static void ceph_destroy_client(struct ceph_client *client) | ||
| 623 | { | ||
| 624 | dout("destroy_client %p\n", client); | ||
| 625 | |||
| 626 | /* unmount */ | ||
| 627 | ceph_mdsc_stop(&client->mdsc); | ||
| 628 | ceph_monc_stop(&client->monc); | ||
| 629 | ceph_osdc_stop(&client->osdc); | ||
| 630 | |||
| 631 | ceph_adjust_min_caps(-client->min_caps); | ||
| 632 | |||
| 633 | ceph_debugfs_client_cleanup(client); | ||
| 634 | destroy_workqueue(client->wb_wq); | ||
| 635 | destroy_workqueue(client->pg_inv_wq); | ||
| 636 | destroy_workqueue(client->trunc_wq); | ||
| 637 | |||
| 638 | if (client->msgr) | ||
| 639 | ceph_messenger_destroy(client->msgr); | ||
| 640 | mempool_destroy(client->wb_pagevec_pool); | ||
| 641 | |||
| 642 | destroy_mount_args(client->mount_args); | ||
| 643 | |||
| 644 | kfree(client); | ||
| 645 | dout("destroy_client %p done\n", client); | ||
| 646 | } | ||
| 647 | |||
| 648 | /* | ||
| 649 | * Initially learn our fsid, or verify an fsid matches. | ||
| 650 | */ | ||
| 651 | int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid) | ||
| 652 | { | ||
| 653 | if (client->have_fsid) { | ||
| 654 | if (ceph_fsid_compare(&client->fsid, fsid)) { | ||
| 655 | pr_err("bad fsid, had " FSID_FORMAT " got " FSID_FORMAT, | ||
| 656 | PR_FSID(&client->fsid), PR_FSID(fsid)); | ||
| 657 | return -1; | ||
| 658 | } | ||
| 659 | } else { | ||
| 660 | pr_info("client%lld fsid " FSID_FORMAT "\n", | ||
| 661 | client->monc.auth->global_id, PR_FSID(fsid)); | ||
| 662 | memcpy(&client->fsid, fsid, sizeof(*fsid)); | ||
| 663 | ceph_debugfs_client_init(client); | ||
| 664 | client->have_fsid = true; | ||
| 665 | } | ||
| 666 | return 0; | ||
| 667 | } | ||
| 668 | |||
| 669 | /* | ||
| 670 | * true if we have the mon map (and have thus joined the cluster) | ||
| 671 | */ | ||
| 672 | static int have_mon_map(struct ceph_client *client) | ||
| 673 | { | ||
| 674 | return client->monc.monmap && client->monc.monmap->epoch; | ||
| 675 | } | ||
| 676 | |||
| 677 | /* | ||
| 678 | * Bootstrap mount by opening the root directory. Note the mount | ||
| 679 | * @started time from caller, and time out if this takes too long. | ||
| 680 | */ | ||
| 681 | static struct dentry *open_root_dentry(struct ceph_client *client, | ||
| 682 | const char *path, | ||
| 683 | unsigned long started) | ||
| 684 | { | ||
| 685 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 686 | struct ceph_mds_request *req = NULL; | ||
| 687 | int err; | ||
| 688 | struct dentry *root; | ||
| 689 | |||
| 690 | /* open dir */ | ||
| 691 | dout("open_root_inode opening '%s'\n", path); | ||
| 692 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS); | ||
| 693 | if (IS_ERR(req)) | ||
| 694 | return ERR_PTR(PTR_ERR(req)); | ||
| 695 | req->r_path1 = kstrdup(path, GFP_NOFS); | ||
| 696 | req->r_ino1.ino = CEPH_INO_ROOT; | ||
| 697 | req->r_ino1.snap = CEPH_NOSNAP; | ||
| 698 | req->r_started = started; | ||
| 699 | req->r_timeout = client->mount_args->mount_timeout * HZ; | ||
| 700 | req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE); | ||
| 701 | req->r_num_caps = 2; | ||
| 702 | err = ceph_mdsc_do_request(mdsc, NULL, req); | ||
| 703 | if (err == 0) { | ||
| 704 | dout("open_root_inode success\n"); | ||
| 705 | if (ceph_ino(req->r_target_inode) == CEPH_INO_ROOT && | ||
| 706 | client->sb->s_root == NULL) | ||
| 707 | root = d_alloc_root(req->r_target_inode); | ||
| 708 | else | ||
| 709 | root = d_obtain_alias(req->r_target_inode); | ||
| 710 | req->r_target_inode = NULL; | ||
| 711 | dout("open_root_inode success, root dentry is %p\n", root); | ||
| 712 | } else { | ||
| 713 | root = ERR_PTR(err); | ||
| 714 | } | ||
| 715 | ceph_mdsc_put_request(req); | ||
| 716 | return root; | ||
| 717 | } | ||
| 718 | |||
| 719 | /* | ||
| 720 | * mount: join the ceph cluster, and open root directory. | ||
| 721 | */ | ||
| 722 | static int ceph_mount(struct ceph_client *client, struct vfsmount *mnt, | ||
| 723 | const char *path) | ||
| 724 | { | ||
| 725 | struct ceph_entity_addr *myaddr = NULL; | ||
| 726 | int err; | ||
| 727 | unsigned long timeout = client->mount_args->mount_timeout * HZ; | ||
| 728 | unsigned long started = jiffies; /* note the start time */ | ||
| 729 | struct dentry *root; | ||
| 730 | |||
| 731 | dout("mount start\n"); | ||
| 732 | mutex_lock(&client->mount_mutex); | ||
| 733 | |||
| 734 | /* initialize the messenger */ | ||
| 735 | if (client->msgr == NULL) { | ||
| 736 | if (ceph_test_opt(client, MYIP)) | ||
| 737 | myaddr = &client->mount_args->my_addr; | ||
| 738 | client->msgr = ceph_messenger_create(myaddr); | ||
| 739 | if (IS_ERR(client->msgr)) { | ||
| 740 | err = PTR_ERR(client->msgr); | ||
| 741 | client->msgr = NULL; | ||
| 742 | goto out; | ||
| 743 | } | ||
| 744 | client->msgr->nocrc = ceph_test_opt(client, NOCRC); | ||
| 745 | } | ||
| 746 | |||
| 747 | /* open session, and wait for mon, mds, and osd maps */ | ||
| 748 | err = ceph_monc_open_session(&client->monc); | ||
| 749 | if (err < 0) | ||
| 750 | goto out; | ||
| 751 | |||
| 752 | while (!have_mon_map(client)) { | ||
| 753 | err = -EIO; | ||
| 754 | if (timeout && time_after_eq(jiffies, started + timeout)) | ||
| 755 | goto out; | ||
| 756 | |||
| 757 | /* wait */ | ||
| 758 | dout("mount waiting for mon_map\n"); | ||
| 759 | err = wait_event_interruptible_timeout(client->auth_wq, | ||
| 760 | have_mon_map(client) || (client->auth_err < 0), | ||
| 761 | timeout); | ||
| 762 | if (err == -EINTR || err == -ERESTARTSYS) | ||
| 763 | goto out; | ||
| 764 | if (client->auth_err < 0) { | ||
| 765 | err = client->auth_err; | ||
| 766 | goto out; | ||
| 767 | } | ||
| 768 | } | ||
| 769 | |||
| 770 | dout("mount opening root\n"); | ||
| 771 | root = open_root_dentry(client, "", started); | ||
| 772 | if (IS_ERR(root)) { | ||
| 773 | err = PTR_ERR(root); | ||
| 774 | goto out; | ||
| 775 | } | ||
| 776 | if (client->sb->s_root) | ||
| 777 | dput(root); | ||
| 778 | else | ||
| 779 | client->sb->s_root = root; | ||
| 780 | |||
| 781 | if (path[0] == 0) { | ||
| 782 | dget(root); | ||
| 783 | } else { | ||
| 784 | dout("mount opening base mountpoint\n"); | ||
| 785 | root = open_root_dentry(client, path, started); | ||
| 786 | if (IS_ERR(root)) { | ||
| 787 | err = PTR_ERR(root); | ||
| 788 | dput(client->sb->s_root); | ||
| 789 | client->sb->s_root = NULL; | ||
| 790 | goto out; | ||
| 791 | } | ||
| 792 | } | ||
| 793 | |||
| 794 | mnt->mnt_root = root; | ||
| 795 | mnt->mnt_sb = client->sb; | ||
| 796 | |||
| 797 | client->mount_state = CEPH_MOUNT_MOUNTED; | ||
| 798 | dout("mount success\n"); | ||
| 799 | err = 0; | ||
| 800 | |||
| 801 | out: | ||
| 802 | mutex_unlock(&client->mount_mutex); | ||
| 803 | return err; | ||
| 804 | } | ||
| 805 | |||
| 806 | static int ceph_set_super(struct super_block *s, void *data) | ||
| 807 | { | ||
| 808 | struct ceph_client *client = data; | ||
| 809 | int ret; | ||
| 810 | |||
| 811 | dout("set_super %p data %p\n", s, data); | ||
| 812 | |||
| 813 | s->s_flags = client->mount_args->sb_flags; | ||
| 814 | s->s_maxbytes = 1ULL << 40; /* temp value until we get mdsmap */ | ||
| 815 | |||
| 816 | s->s_fs_info = client; | ||
| 817 | client->sb = s; | ||
| 818 | |||
| 819 | s->s_op = &ceph_super_ops; | ||
| 820 | s->s_export_op = &ceph_export_ops; | ||
| 821 | |||
| 822 | s->s_time_gran = 1000; /* 1000 ns == 1 us */ | ||
| 823 | |||
| 824 | ret = set_anon_super(s, NULL); /* what is that second arg for? */ | ||
| 825 | if (ret != 0) | ||
| 826 | goto fail; | ||
| 827 | |||
| 828 | return ret; | ||
| 829 | |||
| 830 | fail: | ||
| 831 | s->s_fs_info = NULL; | ||
| 832 | client->sb = NULL; | ||
| 833 | return ret; | ||
| 834 | } | ||
| 835 | |||
| 836 | /* | ||
| 837 | * share superblock if same fs AND options | ||
| 838 | */ | ||
| 839 | static int ceph_compare_super(struct super_block *sb, void *data) | ||
| 840 | { | ||
| 841 | struct ceph_client *new = data; | ||
| 842 | struct ceph_mount_args *args = new->mount_args; | ||
| 843 | struct ceph_client *other = ceph_sb_to_client(sb); | ||
| 844 | int i; | ||
| 845 | |||
| 846 | dout("ceph_compare_super %p\n", sb); | ||
| 847 | if (args->flags & CEPH_OPT_FSID) { | ||
| 848 | if (ceph_fsid_compare(&args->fsid, &other->fsid)) { | ||
| 849 | dout("fsid doesn't match\n"); | ||
| 850 | return 0; | ||
| 851 | } | ||
| 852 | } else { | ||
| 853 | /* do we share (a) monitor? */ | ||
| 854 | for (i = 0; i < new->monc.monmap->num_mon; i++) | ||
| 855 | if (ceph_monmap_contains(other->monc.monmap, | ||
| 856 | &new->monc.monmap->mon_inst[i].addr)) | ||
| 857 | break; | ||
| 858 | if (i == new->monc.monmap->num_mon) { | ||
| 859 | dout("mon ip not part of monmap\n"); | ||
| 860 | return 0; | ||
| 861 | } | ||
| 862 | dout("mon ip matches existing sb %p\n", sb); | ||
| 863 | } | ||
| 864 | if (args->sb_flags != other->mount_args->sb_flags) { | ||
| 865 | dout("flags differ\n"); | ||
| 866 | return 0; | ||
| 867 | } | ||
| 868 | return 1; | ||
| 869 | } | ||
| 870 | |||
| 871 | /* | ||
| 872 | * construct our own bdi so we can control readahead, etc. | ||
| 873 | */ | ||
| 874 | static int ceph_register_bdi(struct super_block *sb, struct ceph_client *client) | ||
| 875 | { | ||
| 876 | int err; | ||
| 877 | |||
| 878 | sb->s_bdi = &client->backing_dev_info; | ||
| 879 | |||
| 880 | /* set ra_pages based on rsize mount option? */ | ||
| 881 | if (client->mount_args->rsize >= PAGE_CACHE_SIZE) | ||
| 882 | client->backing_dev_info.ra_pages = | ||
| 883 | (client->mount_args->rsize + PAGE_CACHE_SIZE - 1) | ||
| 884 | >> PAGE_SHIFT; | ||
| 885 | err = bdi_register_dev(&client->backing_dev_info, sb->s_dev); | ||
| 886 | return err; | ||
| 887 | } | ||
| 888 | |||
| 889 | static int ceph_get_sb(struct file_system_type *fs_type, | ||
| 890 | int flags, const char *dev_name, void *data, | ||
| 891 | struct vfsmount *mnt) | ||
| 892 | { | ||
| 893 | struct super_block *sb; | ||
| 894 | struct ceph_client *client; | ||
| 895 | int err; | ||
| 896 | int (*compare_super)(struct super_block *, void *) = ceph_compare_super; | ||
| 897 | const char *path = NULL; | ||
| 898 | struct ceph_mount_args *args; | ||
| 899 | |||
| 900 | dout("ceph_get_sb\n"); | ||
| 901 | args = parse_mount_args(flags, data, dev_name, &path); | ||
| 902 | if (IS_ERR(args)) { | ||
| 903 | err = PTR_ERR(args); | ||
| 904 | goto out_final; | ||
| 905 | } | ||
| 906 | |||
| 907 | /* create client (which we may/may not use) */ | ||
| 908 | client = ceph_create_client(args); | ||
| 909 | if (IS_ERR(client)) { | ||
| 910 | err = PTR_ERR(client); | ||
| 911 | goto out_final; | ||
| 912 | } | ||
| 913 | |||
| 914 | if (client->mount_args->flags & CEPH_OPT_NOSHARE) | ||
| 915 | compare_super = NULL; | ||
| 916 | sb = sget(fs_type, compare_super, ceph_set_super, client); | ||
| 917 | if (IS_ERR(sb)) { | ||
| 918 | err = PTR_ERR(sb); | ||
| 919 | goto out; | ||
| 920 | } | ||
| 921 | |||
| 922 | if (ceph_client(sb) != client) { | ||
| 923 | ceph_destroy_client(client); | ||
| 924 | client = ceph_client(sb); | ||
| 925 | dout("get_sb got existing client %p\n", client); | ||
| 926 | } else { | ||
| 927 | dout("get_sb using new client %p\n", client); | ||
| 928 | err = ceph_register_bdi(sb, client); | ||
| 929 | if (err < 0) | ||
| 930 | goto out_splat; | ||
| 931 | } | ||
| 932 | |||
| 933 | err = ceph_mount(client, mnt, path); | ||
| 934 | if (err < 0) | ||
| 935 | goto out_splat; | ||
| 936 | dout("root %p inode %p ino %llx.%llx\n", mnt->mnt_root, | ||
| 937 | mnt->mnt_root->d_inode, ceph_vinop(mnt->mnt_root->d_inode)); | ||
| 938 | return 0; | ||
| 939 | |||
| 940 | out_splat: | ||
| 941 | ceph_mdsc_close_sessions(&client->mdsc); | ||
| 942 | up_write(&sb->s_umount); | ||
| 943 | deactivate_super(sb); | ||
| 944 | goto out_final; | ||
| 945 | |||
| 946 | out: | ||
| 947 | ceph_destroy_client(client); | ||
| 948 | out_final: | ||
| 949 | dout("ceph_get_sb fail %d\n", err); | ||
| 950 | return err; | ||
| 951 | } | ||
| 952 | |||
| 953 | static void ceph_kill_sb(struct super_block *s) | ||
| 954 | { | ||
| 955 | struct ceph_client *client = ceph_sb_to_client(s); | ||
| 956 | dout("kill_sb %p\n", s); | ||
| 957 | ceph_mdsc_pre_umount(&client->mdsc); | ||
| 958 | kill_anon_super(s); /* will call put_super after sb is r/o */ | ||
| 959 | if (s->s_bdi == &client->backing_dev_info) | ||
| 960 | bdi_unregister(&client->backing_dev_info); | ||
| 961 | bdi_destroy(&client->backing_dev_info); | ||
| 962 | ceph_destroy_client(client); | ||
| 963 | } | ||
| 964 | |||
| 965 | static struct file_system_type ceph_fs_type = { | ||
| 966 | .owner = THIS_MODULE, | ||
| 967 | .name = "ceph", | ||
| 968 | .get_sb = ceph_get_sb, | ||
| 969 | .kill_sb = ceph_kill_sb, | ||
| 970 | .fs_flags = FS_RENAME_DOES_D_MOVE, | ||
| 971 | }; | ||
| 972 | |||
| 973 | #define _STRINGIFY(x) #x | ||
| 974 | #define STRINGIFY(x) _STRINGIFY(x) | ||
| 975 | |||
| 976 | static int __init init_ceph(void) | ||
| 977 | { | ||
| 978 | int ret = 0; | ||
| 979 | |||
| 980 | ret = ceph_debugfs_init(); | ||
| 981 | if (ret < 0) | ||
| 982 | goto out; | ||
| 983 | |||
| 984 | ret = ceph_msgr_init(); | ||
| 985 | if (ret < 0) | ||
| 986 | goto out_debugfs; | ||
| 987 | |||
| 988 | ret = init_caches(); | ||
| 989 | if (ret) | ||
| 990 | goto out_msgr; | ||
| 991 | |||
| 992 | ceph_caps_init(); | ||
| 993 | |||
| 994 | ret = register_filesystem(&ceph_fs_type); | ||
| 995 | if (ret) | ||
| 996 | goto out_icache; | ||
| 997 | |||
| 998 | pr_info("loaded %d.%d.%d (mon/mds/osd proto %d/%d/%d)\n", | ||
| 999 | CEPH_VERSION_MAJOR, CEPH_VERSION_MINOR, CEPH_VERSION_PATCH, | ||
| 1000 | CEPH_MONC_PROTOCOL, CEPH_MDSC_PROTOCOL, CEPH_OSDC_PROTOCOL); | ||
| 1001 | return 0; | ||
| 1002 | |||
| 1003 | out_icache: | ||
| 1004 | destroy_caches(); | ||
| 1005 | out_msgr: | ||
| 1006 | ceph_msgr_exit(); | ||
| 1007 | out_debugfs: | ||
| 1008 | ceph_debugfs_cleanup(); | ||
| 1009 | out: | ||
| 1010 | return ret; | ||
| 1011 | } | ||
| 1012 | |||
| 1013 | static void __exit exit_ceph(void) | ||
| 1014 | { | ||
| 1015 | dout("exit_ceph\n"); | ||
| 1016 | unregister_filesystem(&ceph_fs_type); | ||
| 1017 | ceph_caps_finalize(); | ||
| 1018 | destroy_caches(); | ||
| 1019 | ceph_msgr_exit(); | ||
| 1020 | ceph_debugfs_cleanup(); | ||
| 1021 | } | ||
| 1022 | |||
| 1023 | module_init(init_ceph); | ||
| 1024 | module_exit(exit_ceph); | ||
| 1025 | |||
| 1026 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); | ||
| 1027 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); | ||
| 1028 | MODULE_AUTHOR("Patience Warnick <patience@newdream.net>"); | ||
| 1029 | MODULE_DESCRIPTION("Ceph filesystem for Linux"); | ||
| 1030 | MODULE_LICENSE("GPL"); | ||
diff --git a/fs/ceph/super.h b/fs/ceph/super.h new file mode 100644 index 000000000000..65d12036b670 --- /dev/null +++ b/fs/ceph/super.h | |||
| @@ -0,0 +1,901 @@ | |||
| 1 | #ifndef _FS_CEPH_SUPER_H | ||
| 2 | #define _FS_CEPH_SUPER_H | ||
| 3 | |||
| 4 | #include "ceph_debug.h" | ||
| 5 | |||
| 6 | #include <asm/unaligned.h> | ||
| 7 | #include <linux/backing-dev.h> | ||
| 8 | #include <linux/completion.h> | ||
| 9 | #include <linux/exportfs.h> | ||
| 10 | #include <linux/fs.h> | ||
| 11 | #include <linux/mempool.h> | ||
| 12 | #include <linux/pagemap.h> | ||
| 13 | #include <linux/wait.h> | ||
| 14 | #include <linux/writeback.h> | ||
| 15 | |||
| 16 | #include "types.h" | ||
| 17 | #include "messenger.h" | ||
| 18 | #include "msgpool.h" | ||
| 19 | #include "mon_client.h" | ||
| 20 | #include "mds_client.h" | ||
| 21 | #include "osd_client.h" | ||
| 22 | #include "ceph_fs.h" | ||
| 23 | |||
| 24 | /* f_type in struct statfs */ | ||
| 25 | #define CEPH_SUPER_MAGIC 0x00c36400 | ||
| 26 | |||
| 27 | /* large granularity for statfs utilization stats to facilitate | ||
| 28 | * large volume sizes on 32-bit machines. */ | ||
| 29 | #define CEPH_BLOCK_SHIFT 20 /* 1 MB */ | ||
| 30 | #define CEPH_BLOCK (1 << CEPH_BLOCK_SHIFT) | ||
| 31 | |||
| 32 | /* | ||
| 33 | * mount options | ||
| 34 | */ | ||
| 35 | #define CEPH_OPT_FSID (1<<0) | ||
| 36 | #define CEPH_OPT_NOSHARE (1<<1) /* don't share client with other sbs */ | ||
| 37 | #define CEPH_OPT_MYIP (1<<2) /* specified my ip */ | ||
| 38 | #define CEPH_OPT_DIRSTAT (1<<4) /* funky `cat dirname` for stats */ | ||
| 39 | #define CEPH_OPT_RBYTES (1<<5) /* dir st_bytes = rbytes */ | ||
| 40 | #define CEPH_OPT_NOCRC (1<<6) /* no data crc on writes */ | ||
| 41 | #define CEPH_OPT_NOASYNCREADDIR (1<<7) /* no dcache readdir */ | ||
| 42 | |||
| 43 | #define CEPH_OPT_DEFAULT (CEPH_OPT_RBYTES) | ||
| 44 | |||
| 45 | #define ceph_set_opt(client, opt) \ | ||
| 46 | (client)->mount_args->flags |= CEPH_OPT_##opt; | ||
| 47 | #define ceph_test_opt(client, opt) \ | ||
| 48 | (!!((client)->mount_args->flags & CEPH_OPT_##opt)) | ||
| 49 | |||
| 50 | |||
| 51 | struct ceph_mount_args { | ||
| 52 | int sb_flags; | ||
| 53 | int num_mon; | ||
| 54 | struct ceph_entity_addr *mon_addr; | ||
| 55 | int flags; | ||
| 56 | int mount_timeout; | ||
| 57 | int osd_idle_ttl; | ||
| 58 | int caps_wanted_delay_min, caps_wanted_delay_max; | ||
| 59 | struct ceph_fsid fsid; | ||
| 60 | struct ceph_entity_addr my_addr; | ||
| 61 | int wsize; | ||
| 62 | int rsize; /* max readahead */ | ||
| 63 | int max_readdir; /* max readdir size */ | ||
| 64 | int congestion_kb; /* max readdir size */ | ||
| 65 | int osd_timeout; | ||
| 66 | int osd_keepalive_timeout; | ||
| 67 | char *snapdir_name; /* default ".snap" */ | ||
| 68 | char *name; | ||
| 69 | char *secret; | ||
| 70 | int cap_release_safety; | ||
| 71 | }; | ||
| 72 | |||
| 73 | /* | ||
| 74 | * defaults | ||
| 75 | */ | ||
| 76 | #define CEPH_MOUNT_TIMEOUT_DEFAULT 60 | ||
| 77 | #define CEPH_OSD_TIMEOUT_DEFAULT 60 /* seconds */ | ||
| 78 | #define CEPH_OSD_KEEPALIVE_DEFAULT 5 | ||
| 79 | #define CEPH_OSD_IDLE_TTL_DEFAULT 60 | ||
| 80 | #define CEPH_MOUNT_RSIZE_DEFAULT (512*1024) /* readahead */ | ||
| 81 | |||
| 82 | #define CEPH_MSG_MAX_FRONT_LEN (16*1024*1024) | ||
| 83 | #define CEPH_MSG_MAX_DATA_LEN (16*1024*1024) | ||
| 84 | |||
| 85 | #define CEPH_SNAPDIRNAME_DEFAULT ".snap" | ||
| 86 | #define CEPH_AUTH_NAME_DEFAULT "guest" | ||
| 87 | |||
| 88 | /* | ||
| 89 | * Delay telling the MDS we no longer want caps, in case we reopen | ||
| 90 | * the file. Delay a minimum amount of time, even if we send a cap | ||
| 91 | * message for some other reason. Otherwise, take the oppotunity to | ||
| 92 | * update the mds to avoid sending another message later. | ||
| 93 | */ | ||
| 94 | #define CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT 5 /* cap release delay */ | ||
| 95 | #define CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT 60 /* cap release delay */ | ||
| 96 | |||
| 97 | |||
| 98 | /* mount state */ | ||
| 99 | enum { | ||
| 100 | CEPH_MOUNT_MOUNTING, | ||
| 101 | CEPH_MOUNT_MOUNTED, | ||
| 102 | CEPH_MOUNT_UNMOUNTING, | ||
| 103 | CEPH_MOUNT_UNMOUNTED, | ||
| 104 | CEPH_MOUNT_SHUTDOWN, | ||
| 105 | }; | ||
| 106 | |||
| 107 | /* | ||
| 108 | * subtract jiffies | ||
| 109 | */ | ||
| 110 | static inline unsigned long time_sub(unsigned long a, unsigned long b) | ||
| 111 | { | ||
| 112 | BUG_ON(time_after(b, a)); | ||
| 113 | return (long)a - (long)b; | ||
| 114 | } | ||
| 115 | |||
| 116 | /* | ||
| 117 | * per-filesystem client state | ||
| 118 | * | ||
| 119 | * possibly shared by multiple mount points, if they are | ||
| 120 | * mounting the same ceph filesystem/cluster. | ||
| 121 | */ | ||
| 122 | struct ceph_client { | ||
| 123 | struct ceph_fsid fsid; | ||
| 124 | bool have_fsid; | ||
| 125 | |||
| 126 | struct mutex mount_mutex; /* serialize mount attempts */ | ||
| 127 | struct ceph_mount_args *mount_args; | ||
| 128 | |||
| 129 | struct super_block *sb; | ||
| 130 | |||
| 131 | unsigned long mount_state; | ||
| 132 | wait_queue_head_t auth_wq; | ||
| 133 | |||
| 134 | int auth_err; | ||
| 135 | |||
| 136 | int min_caps; /* min caps i added */ | ||
| 137 | |||
| 138 | struct ceph_messenger *msgr; /* messenger instance */ | ||
| 139 | struct ceph_mon_client monc; | ||
| 140 | struct ceph_mds_client mdsc; | ||
| 141 | struct ceph_osd_client osdc; | ||
| 142 | |||
| 143 | /* writeback */ | ||
| 144 | mempool_t *wb_pagevec_pool; | ||
| 145 | struct workqueue_struct *wb_wq; | ||
| 146 | struct workqueue_struct *pg_inv_wq; | ||
| 147 | struct workqueue_struct *trunc_wq; | ||
| 148 | atomic_long_t writeback_count; | ||
| 149 | |||
| 150 | struct backing_dev_info backing_dev_info; | ||
| 151 | |||
| 152 | #ifdef CONFIG_DEBUG_FS | ||
| 153 | struct dentry *debugfs_monmap; | ||
| 154 | struct dentry *debugfs_mdsmap, *debugfs_osdmap; | ||
| 155 | struct dentry *debugfs_dir, *debugfs_dentry_lru, *debugfs_caps; | ||
| 156 | struct dentry *debugfs_congestion_kb; | ||
| 157 | struct dentry *debugfs_bdi; | ||
| 158 | #endif | ||
| 159 | }; | ||
| 160 | |||
| 161 | static inline struct ceph_client *ceph_client(struct super_block *sb) | ||
| 162 | { | ||
| 163 | return sb->s_fs_info; | ||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | /* | ||
| 168 | * File i/o capability. This tracks shared state with the metadata | ||
| 169 | * server that allows us to cache or writeback attributes or to read | ||
| 170 | * and write data. For any given inode, we should have one or more | ||
| 171 | * capabilities, one issued by each metadata server, and our | ||
| 172 | * cumulative access is the OR of all issued capabilities. | ||
| 173 | * | ||
| 174 | * Each cap is referenced by the inode's i_caps rbtree and by per-mds | ||
| 175 | * session capability lists. | ||
| 176 | */ | ||
| 177 | struct ceph_cap { | ||
| 178 | struct ceph_inode_info *ci; | ||
| 179 | struct rb_node ci_node; /* per-ci cap tree */ | ||
| 180 | struct ceph_mds_session *session; | ||
| 181 | struct list_head session_caps; /* per-session caplist */ | ||
| 182 | int mds; | ||
| 183 | u64 cap_id; /* unique cap id (mds provided) */ | ||
| 184 | int issued; /* latest, from the mds */ | ||
| 185 | int implemented; /* implemented superset of issued (for revocation) */ | ||
| 186 | int mds_wanted; | ||
| 187 | u32 seq, issue_seq, mseq; | ||
| 188 | u32 cap_gen; /* active/stale cycle */ | ||
| 189 | unsigned long last_used; | ||
| 190 | struct list_head caps_item; | ||
| 191 | }; | ||
| 192 | |||
| 193 | #define CHECK_CAPS_NODELAY 1 /* do not delay any further */ | ||
| 194 | #define CHECK_CAPS_AUTHONLY 2 /* only check auth cap */ | ||
| 195 | #define CHECK_CAPS_FLUSH 4 /* flush any dirty caps */ | ||
| 196 | |||
| 197 | /* | ||
| 198 | * Snapped cap state that is pending flush to mds. When a snapshot occurs, | ||
| 199 | * we first complete any in-process sync writes and writeback any dirty | ||
| 200 | * data before flushing the snapped state (tracked here) back to the MDS. | ||
| 201 | */ | ||
| 202 | struct ceph_cap_snap { | ||
| 203 | atomic_t nref; | ||
| 204 | struct ceph_inode_info *ci; | ||
| 205 | struct list_head ci_item, flushing_item; | ||
| 206 | |||
| 207 | u64 follows, flush_tid; | ||
| 208 | int issued, dirty; | ||
| 209 | struct ceph_snap_context *context; | ||
| 210 | |||
| 211 | mode_t mode; | ||
| 212 | uid_t uid; | ||
| 213 | gid_t gid; | ||
| 214 | |||
| 215 | void *xattr_blob; | ||
| 216 | int xattr_len; | ||
| 217 | u64 xattr_version; | ||
| 218 | |||
| 219 | u64 size; | ||
| 220 | struct timespec mtime, atime, ctime; | ||
| 221 | u64 time_warp_seq; | ||
| 222 | int writing; /* a sync write is still in progress */ | ||
| 223 | int dirty_pages; /* dirty pages awaiting writeback */ | ||
| 224 | }; | ||
| 225 | |||
| 226 | static inline void ceph_put_cap_snap(struct ceph_cap_snap *capsnap) | ||
| 227 | { | ||
| 228 | if (atomic_dec_and_test(&capsnap->nref)) | ||
| 229 | kfree(capsnap); | ||
| 230 | } | ||
| 231 | |||
| 232 | /* | ||
| 233 | * The frag tree describes how a directory is fragmented, potentially across | ||
| 234 | * multiple metadata servers. It is also used to indicate points where | ||
| 235 | * metadata authority is delegated, and whether/where metadata is replicated. | ||
| 236 | * | ||
| 237 | * A _leaf_ frag will be present in the i_fragtree IFF there is | ||
| 238 | * delegation info. That is, if mds >= 0 || ndist > 0. | ||
| 239 | */ | ||
| 240 | #define CEPH_MAX_DIRFRAG_REP 4 | ||
| 241 | |||
| 242 | struct ceph_inode_frag { | ||
| 243 | struct rb_node node; | ||
| 244 | |||
| 245 | /* fragtree state */ | ||
| 246 | u32 frag; | ||
| 247 | int split_by; /* i.e. 2^(split_by) children */ | ||
| 248 | |||
| 249 | /* delegation and replication info */ | ||
| 250 | int mds; /* -1 if same authority as parent */ | ||
| 251 | int ndist; /* >0 if replicated */ | ||
| 252 | int dist[CEPH_MAX_DIRFRAG_REP]; | ||
| 253 | }; | ||
| 254 | |||
| 255 | /* | ||
| 256 | * We cache inode xattrs as an encoded blob until they are first used, | ||
| 257 | * at which point we parse them into an rbtree. | ||
| 258 | */ | ||
| 259 | struct ceph_inode_xattr { | ||
| 260 | struct rb_node node; | ||
| 261 | |||
| 262 | const char *name; | ||
| 263 | int name_len; | ||
| 264 | const char *val; | ||
| 265 | int val_len; | ||
| 266 | int dirty; | ||
| 267 | |||
| 268 | int should_free_name; | ||
| 269 | int should_free_val; | ||
| 270 | }; | ||
| 271 | |||
| 272 | struct ceph_inode_xattrs_info { | ||
| 273 | /* | ||
| 274 | * (still encoded) xattr blob. we avoid the overhead of parsing | ||
| 275 | * this until someone actually calls getxattr, etc. | ||
| 276 | * | ||
| 277 | * blob->vec.iov_len == 4 implies there are no xattrs; blob == | ||
| 278 | * NULL means we don't know. | ||
| 279 | */ | ||
| 280 | struct ceph_buffer *blob, *prealloc_blob; | ||
| 281 | |||
| 282 | struct rb_root index; | ||
| 283 | bool dirty; | ||
| 284 | int count; | ||
| 285 | int names_size; | ||
| 286 | int vals_size; | ||
| 287 | u64 version, index_version; | ||
| 288 | }; | ||
| 289 | |||
| 290 | /* | ||
| 291 | * Ceph inode. | ||
| 292 | */ | ||
| 293 | #define CEPH_I_COMPLETE 1 /* we have complete directory cached */ | ||
| 294 | #define CEPH_I_NODELAY 4 /* do not delay cap release */ | ||
| 295 | #define CEPH_I_FLUSH 8 /* do not delay flush of dirty metadata */ | ||
| 296 | #define CEPH_I_NOFLUSH 16 /* do not flush dirty caps */ | ||
| 297 | |||
| 298 | struct ceph_inode_info { | ||
| 299 | struct ceph_vino i_vino; /* ceph ino + snap */ | ||
| 300 | |||
| 301 | u64 i_version; | ||
| 302 | u32 i_time_warp_seq; | ||
| 303 | |||
| 304 | unsigned i_ceph_flags; | ||
| 305 | unsigned long i_release_count; | ||
| 306 | |||
| 307 | struct ceph_file_layout i_layout; | ||
| 308 | char *i_symlink; | ||
| 309 | |||
| 310 | /* for dirs */ | ||
| 311 | struct timespec i_rctime; | ||
| 312 | u64 i_rbytes, i_rfiles, i_rsubdirs; | ||
| 313 | u64 i_files, i_subdirs; | ||
| 314 | u64 i_max_offset; /* largest readdir offset, set with I_COMPLETE */ | ||
| 315 | |||
| 316 | struct rb_root i_fragtree; | ||
| 317 | struct mutex i_fragtree_mutex; | ||
| 318 | |||
| 319 | struct ceph_inode_xattrs_info i_xattrs; | ||
| 320 | |||
| 321 | /* capabilities. protected _both_ by i_lock and cap->session's | ||
| 322 | * s_mutex. */ | ||
| 323 | struct rb_root i_caps; /* cap list */ | ||
| 324 | struct ceph_cap *i_auth_cap; /* authoritative cap, if any */ | ||
| 325 | unsigned i_dirty_caps, i_flushing_caps; /* mask of dirtied fields */ | ||
| 326 | struct list_head i_dirty_item, i_flushing_item; | ||
| 327 | u64 i_cap_flush_seq; | ||
| 328 | /* we need to track cap writeback on a per-cap-bit basis, to allow | ||
| 329 | * overlapping, pipelined cap flushes to the mds. we can probably | ||
| 330 | * reduce the tid to 8 bits if we're concerned about inode size. */ | ||
| 331 | u16 i_cap_flush_last_tid, i_cap_flush_tid[CEPH_CAP_BITS]; | ||
| 332 | wait_queue_head_t i_cap_wq; /* threads waiting on a capability */ | ||
| 333 | unsigned long i_hold_caps_min; /* jiffies */ | ||
| 334 | unsigned long i_hold_caps_max; /* jiffies */ | ||
| 335 | struct list_head i_cap_delay_list; /* for delayed cap release to mds */ | ||
| 336 | int i_cap_exporting_mds; /* to handle cap migration between */ | ||
| 337 | unsigned i_cap_exporting_mseq; /* mds's. */ | ||
| 338 | unsigned i_cap_exporting_issued; | ||
| 339 | struct ceph_cap_reservation i_cap_migration_resv; | ||
| 340 | struct list_head i_cap_snaps; /* snapped state pending flush to mds */ | ||
| 341 | struct ceph_snap_context *i_head_snapc; /* set if wr_buffer_head > 0 */ | ||
| 342 | unsigned i_snap_caps; /* cap bits for snapped files */ | ||
| 343 | |||
| 344 | int i_nr_by_mode[CEPH_FILE_MODE_NUM]; /* open file counts */ | ||
| 345 | |||
| 346 | u32 i_truncate_seq; /* last truncate to smaller size */ | ||
| 347 | u64 i_truncate_size; /* and the size we last truncated down to */ | ||
| 348 | int i_truncate_pending; /* still need to call vmtruncate */ | ||
| 349 | |||
| 350 | u64 i_max_size; /* max file size authorized by mds */ | ||
| 351 | u64 i_reported_size; /* (max_)size reported to or requested of mds */ | ||
| 352 | u64 i_wanted_max_size; /* offset we'd like to write too */ | ||
| 353 | u64 i_requested_max_size; /* max_size we've requested */ | ||
| 354 | |||
| 355 | /* held references to caps */ | ||
| 356 | int i_pin_ref; | ||
| 357 | int i_rd_ref, i_rdcache_ref, i_wr_ref; | ||
| 358 | int i_wrbuffer_ref, i_wrbuffer_ref_head; | ||
| 359 | u32 i_shared_gen; /* increment each time we get FILE_SHARED */ | ||
| 360 | u32 i_rdcache_gen; /* we increment this each time we get | ||
| 361 | FILE_CACHE. If it's non-zero, we | ||
| 362 | _may_ have cached pages. */ | ||
| 363 | u32 i_rdcache_revoking; /* RDCACHE gen to async invalidate, if any */ | ||
| 364 | |||
| 365 | struct list_head i_unsafe_writes; /* uncommitted sync writes */ | ||
| 366 | struct list_head i_unsafe_dirops; /* uncommitted mds dir ops */ | ||
| 367 | spinlock_t i_unsafe_lock; | ||
| 368 | |||
| 369 | struct ceph_snap_realm *i_snap_realm; /* snap realm (if caps) */ | ||
| 370 | int i_snap_realm_counter; /* snap realm (if caps) */ | ||
| 371 | struct list_head i_snap_realm_item; | ||
| 372 | struct list_head i_snap_flush_item; | ||
| 373 | |||
| 374 | struct work_struct i_wb_work; /* writeback work */ | ||
| 375 | struct work_struct i_pg_inv_work; /* page invalidation work */ | ||
| 376 | |||
| 377 | struct work_struct i_vmtruncate_work; | ||
| 378 | |||
| 379 | struct inode vfs_inode; /* at end */ | ||
| 380 | }; | ||
| 381 | |||
| 382 | static inline struct ceph_inode_info *ceph_inode(struct inode *inode) | ||
| 383 | { | ||
| 384 | return container_of(inode, struct ceph_inode_info, vfs_inode); | ||
| 385 | } | ||
| 386 | |||
| 387 | static inline void ceph_i_clear(struct inode *inode, unsigned mask) | ||
| 388 | { | ||
| 389 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 390 | |||
| 391 | spin_lock(&inode->i_lock); | ||
| 392 | ci->i_ceph_flags &= ~mask; | ||
| 393 | spin_unlock(&inode->i_lock); | ||
| 394 | } | ||
| 395 | |||
| 396 | static inline void ceph_i_set(struct inode *inode, unsigned mask) | ||
| 397 | { | ||
| 398 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 399 | |||
| 400 | spin_lock(&inode->i_lock); | ||
| 401 | ci->i_ceph_flags |= mask; | ||
| 402 | spin_unlock(&inode->i_lock); | ||
| 403 | } | ||
| 404 | |||
| 405 | static inline bool ceph_i_test(struct inode *inode, unsigned mask) | ||
| 406 | { | ||
| 407 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 408 | bool r; | ||
| 409 | |||
| 410 | smp_mb(); | ||
| 411 | r = (ci->i_ceph_flags & mask) == mask; | ||
| 412 | return r; | ||
| 413 | } | ||
| 414 | |||
| 415 | |||
| 416 | /* find a specific frag @f */ | ||
| 417 | extern struct ceph_inode_frag *__ceph_find_frag(struct ceph_inode_info *ci, | ||
| 418 | u32 f); | ||
| 419 | |||
| 420 | /* | ||
| 421 | * choose fragment for value @v. copy frag content to pfrag, if leaf | ||
| 422 | * exists | ||
| 423 | */ | ||
| 424 | extern u32 ceph_choose_frag(struct ceph_inode_info *ci, u32 v, | ||
| 425 | struct ceph_inode_frag *pfrag, | ||
| 426 | int *found); | ||
| 427 | |||
| 428 | /* | ||
| 429 | * Ceph dentry state | ||
| 430 | */ | ||
| 431 | struct ceph_dentry_info { | ||
| 432 | struct ceph_mds_session *lease_session; | ||
| 433 | u32 lease_gen, lease_shared_gen; | ||
| 434 | u32 lease_seq; | ||
| 435 | unsigned long lease_renew_after, lease_renew_from; | ||
| 436 | struct list_head lru; | ||
| 437 | struct dentry *dentry; | ||
| 438 | u64 time; | ||
| 439 | u64 offset; | ||
| 440 | }; | ||
| 441 | |||
| 442 | static inline struct ceph_dentry_info *ceph_dentry(struct dentry *dentry) | ||
| 443 | { | ||
| 444 | return (struct ceph_dentry_info *)dentry->d_fsdata; | ||
| 445 | } | ||
| 446 | |||
| 447 | static inline loff_t ceph_make_fpos(unsigned frag, unsigned off) | ||
| 448 | { | ||
| 449 | return ((loff_t)frag << 32) | (loff_t)off; | ||
| 450 | } | ||
| 451 | |||
| 452 | /* | ||
| 453 | * ino_t is <64 bits on many architectures, blech. | ||
| 454 | * | ||
| 455 | * don't include snap in ino hash, at least for now. | ||
| 456 | */ | ||
| 457 | static inline ino_t ceph_vino_to_ino(struct ceph_vino vino) | ||
| 458 | { | ||
| 459 | ino_t ino = (ino_t)vino.ino; /* ^ (vino.snap << 20); */ | ||
| 460 | #if BITS_PER_LONG == 32 | ||
| 461 | ino ^= vino.ino >> (sizeof(u64)-sizeof(ino_t)) * 8; | ||
| 462 | if (!ino) | ||
| 463 | ino = 1; | ||
| 464 | #endif | ||
| 465 | return ino; | ||
| 466 | } | ||
| 467 | |||
| 468 | static inline int ceph_set_ino_cb(struct inode *inode, void *data) | ||
| 469 | { | ||
| 470 | ceph_inode(inode)->i_vino = *(struct ceph_vino *)data; | ||
| 471 | inode->i_ino = ceph_vino_to_ino(*(struct ceph_vino *)data); | ||
| 472 | return 0; | ||
| 473 | } | ||
| 474 | |||
| 475 | static inline struct ceph_vino ceph_vino(struct inode *inode) | ||
| 476 | { | ||
| 477 | return ceph_inode(inode)->i_vino; | ||
| 478 | } | ||
| 479 | |||
| 480 | /* for printf-style formatting */ | ||
| 481 | #define ceph_vinop(i) ceph_inode(i)->i_vino.ino, ceph_inode(i)->i_vino.snap | ||
| 482 | |||
| 483 | static inline u64 ceph_ino(struct inode *inode) | ||
| 484 | { | ||
| 485 | return ceph_inode(inode)->i_vino.ino; | ||
| 486 | } | ||
| 487 | static inline u64 ceph_snap(struct inode *inode) | ||
| 488 | { | ||
| 489 | return ceph_inode(inode)->i_vino.snap; | ||
| 490 | } | ||
| 491 | |||
| 492 | static inline int ceph_ino_compare(struct inode *inode, void *data) | ||
| 493 | { | ||
| 494 | struct ceph_vino *pvino = (struct ceph_vino *)data; | ||
| 495 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 496 | return ci->i_vino.ino == pvino->ino && | ||
| 497 | ci->i_vino.snap == pvino->snap; | ||
| 498 | } | ||
| 499 | |||
| 500 | static inline struct inode *ceph_find_inode(struct super_block *sb, | ||
| 501 | struct ceph_vino vino) | ||
| 502 | { | ||
| 503 | ino_t t = ceph_vino_to_ino(vino); | ||
| 504 | return ilookup5(sb, t, ceph_ino_compare, &vino); | ||
| 505 | } | ||
| 506 | |||
| 507 | |||
| 508 | /* | ||
| 509 | * caps helpers | ||
| 510 | */ | ||
| 511 | static inline bool __ceph_is_any_real_caps(struct ceph_inode_info *ci) | ||
| 512 | { | ||
| 513 | return !RB_EMPTY_ROOT(&ci->i_caps); | ||
| 514 | } | ||
| 515 | |||
| 516 | extern int __ceph_caps_issued(struct ceph_inode_info *ci, int *implemented); | ||
| 517 | extern int __ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, int t); | ||
| 518 | extern int __ceph_caps_issued_other(struct ceph_inode_info *ci, | ||
| 519 | struct ceph_cap *cap); | ||
| 520 | |||
| 521 | static inline int ceph_caps_issued(struct ceph_inode_info *ci) | ||
| 522 | { | ||
| 523 | int issued; | ||
| 524 | spin_lock(&ci->vfs_inode.i_lock); | ||
| 525 | issued = __ceph_caps_issued(ci, NULL); | ||
| 526 | spin_unlock(&ci->vfs_inode.i_lock); | ||
| 527 | return issued; | ||
| 528 | } | ||
| 529 | |||
| 530 | static inline int ceph_caps_issued_mask(struct ceph_inode_info *ci, int mask, | ||
| 531 | int touch) | ||
| 532 | { | ||
| 533 | int r; | ||
| 534 | spin_lock(&ci->vfs_inode.i_lock); | ||
| 535 | r = __ceph_caps_issued_mask(ci, mask, touch); | ||
| 536 | spin_unlock(&ci->vfs_inode.i_lock); | ||
| 537 | return r; | ||
| 538 | } | ||
| 539 | |||
| 540 | static inline int __ceph_caps_dirty(struct ceph_inode_info *ci) | ||
| 541 | { | ||
| 542 | return ci->i_dirty_caps | ci->i_flushing_caps; | ||
| 543 | } | ||
| 544 | extern void __ceph_mark_dirty_caps(struct ceph_inode_info *ci, int mask); | ||
| 545 | |||
| 546 | extern int ceph_caps_revoking(struct ceph_inode_info *ci, int mask); | ||
| 547 | extern int __ceph_caps_used(struct ceph_inode_info *ci); | ||
| 548 | |||
| 549 | extern int __ceph_caps_file_wanted(struct ceph_inode_info *ci); | ||
| 550 | |||
| 551 | /* | ||
| 552 | * wanted, by virtue of open file modes AND cap refs (buffered/cached data) | ||
| 553 | */ | ||
| 554 | static inline int __ceph_caps_wanted(struct ceph_inode_info *ci) | ||
| 555 | { | ||
| 556 | int w = __ceph_caps_file_wanted(ci) | __ceph_caps_used(ci); | ||
| 557 | if (w & CEPH_CAP_FILE_BUFFER) | ||
| 558 | w |= CEPH_CAP_FILE_EXCL; /* we want EXCL if dirty data */ | ||
| 559 | return w; | ||
| 560 | } | ||
| 561 | |||
| 562 | /* what the mds thinks we want */ | ||
| 563 | extern int __ceph_caps_mds_wanted(struct ceph_inode_info *ci); | ||
| 564 | |||
| 565 | extern void ceph_caps_init(void); | ||
| 566 | extern void ceph_caps_finalize(void); | ||
| 567 | extern void ceph_adjust_min_caps(int delta); | ||
| 568 | extern int ceph_reserve_caps(struct ceph_cap_reservation *ctx, int need); | ||
| 569 | extern int ceph_unreserve_caps(struct ceph_cap_reservation *ctx); | ||
| 570 | extern void ceph_reservation_status(struct ceph_client *client, | ||
| 571 | int *total, int *avail, int *used, | ||
| 572 | int *reserved, int *min); | ||
| 573 | |||
| 574 | static inline struct ceph_client *ceph_inode_to_client(struct inode *inode) | ||
| 575 | { | ||
| 576 | return (struct ceph_client *)inode->i_sb->s_fs_info; | ||
| 577 | } | ||
| 578 | |||
| 579 | static inline struct ceph_client *ceph_sb_to_client(struct super_block *sb) | ||
| 580 | { | ||
| 581 | return (struct ceph_client *)sb->s_fs_info; | ||
| 582 | } | ||
| 583 | |||
| 584 | |||
| 585 | /* | ||
| 586 | * we keep buffered readdir results attached to file->private_data | ||
| 587 | */ | ||
| 588 | struct ceph_file_info { | ||
| 589 | int fmode; /* initialized on open */ | ||
| 590 | |||
| 591 | /* readdir: position within the dir */ | ||
| 592 | u32 frag; | ||
| 593 | struct ceph_mds_request *last_readdir; | ||
| 594 | int at_end; | ||
| 595 | |||
| 596 | /* readdir: position within a frag */ | ||
| 597 | unsigned offset; /* offset of last chunk, adjusted for . and .. */ | ||
| 598 | u64 next_offset; /* offset of next chunk (last_name's + 1) */ | ||
| 599 | char *last_name; /* last entry in previous chunk */ | ||
| 600 | struct dentry *dentry; /* next dentry (for dcache readdir) */ | ||
| 601 | unsigned long dir_release_count; | ||
| 602 | |||
| 603 | /* used for -o dirstat read() on directory thing */ | ||
| 604 | char *dir_info; | ||
| 605 | int dir_info_len; | ||
| 606 | }; | ||
| 607 | |||
| 608 | |||
| 609 | |||
| 610 | /* | ||
| 611 | * snapshots | ||
| 612 | */ | ||
| 613 | |||
| 614 | /* | ||
| 615 | * A "snap context" is the set of existing snapshots when we | ||
| 616 | * write data. It is used by the OSD to guide its COW behavior. | ||
| 617 | * | ||
| 618 | * The ceph_snap_context is refcounted, and attached to each dirty | ||
| 619 | * page, indicating which context the dirty data belonged when it was | ||
| 620 | * dirtied. | ||
| 621 | */ | ||
| 622 | struct ceph_snap_context { | ||
| 623 | atomic_t nref; | ||
| 624 | u64 seq; | ||
| 625 | int num_snaps; | ||
| 626 | u64 snaps[]; | ||
| 627 | }; | ||
| 628 | |||
| 629 | static inline struct ceph_snap_context * | ||
| 630 | ceph_get_snap_context(struct ceph_snap_context *sc) | ||
| 631 | { | ||
| 632 | /* | ||
| 633 | printk("get_snap_context %p %d -> %d\n", sc, atomic_read(&sc->nref), | ||
| 634 | atomic_read(&sc->nref)+1); | ||
| 635 | */ | ||
| 636 | if (sc) | ||
| 637 | atomic_inc(&sc->nref); | ||
| 638 | return sc; | ||
| 639 | } | ||
| 640 | |||
| 641 | static inline void ceph_put_snap_context(struct ceph_snap_context *sc) | ||
| 642 | { | ||
| 643 | if (!sc) | ||
| 644 | return; | ||
| 645 | /* | ||
| 646 | printk("put_snap_context %p %d -> %d\n", sc, atomic_read(&sc->nref), | ||
| 647 | atomic_read(&sc->nref)-1); | ||
| 648 | */ | ||
| 649 | if (atomic_dec_and_test(&sc->nref)) { | ||
| 650 | /*printk(" deleting snap_context %p\n", sc);*/ | ||
| 651 | kfree(sc); | ||
| 652 | } | ||
| 653 | } | ||
| 654 | |||
| 655 | /* | ||
| 656 | * A "snap realm" describes a subset of the file hierarchy sharing | ||
| 657 | * the same set of snapshots that apply to it. The realms themselves | ||
| 658 | * are organized into a hierarchy, such that children inherit (some of) | ||
| 659 | * the snapshots of their parents. | ||
| 660 | * | ||
| 661 | * All inodes within the realm that have capabilities are linked into a | ||
| 662 | * per-realm list. | ||
| 663 | */ | ||
| 664 | struct ceph_snap_realm { | ||
| 665 | u64 ino; | ||
| 666 | atomic_t nref; | ||
| 667 | struct rb_node node; | ||
| 668 | |||
| 669 | u64 created, seq; | ||
| 670 | u64 parent_ino; | ||
| 671 | u64 parent_since; /* snapid when our current parent became so */ | ||
| 672 | |||
| 673 | u64 *prior_parent_snaps; /* snaps inherited from any parents we */ | ||
| 674 | int num_prior_parent_snaps; /* had prior to parent_since */ | ||
| 675 | u64 *snaps; /* snaps specific to this realm */ | ||
| 676 | int num_snaps; | ||
| 677 | |||
| 678 | struct ceph_snap_realm *parent; | ||
| 679 | struct list_head children; /* list of child realms */ | ||
| 680 | struct list_head child_item; | ||
| 681 | |||
| 682 | struct list_head empty_item; /* if i have ref==0 */ | ||
| 683 | |||
| 684 | /* the current set of snaps for this realm */ | ||
| 685 | struct ceph_snap_context *cached_context; | ||
| 686 | |||
| 687 | struct list_head inodes_with_caps; | ||
| 688 | spinlock_t inodes_with_caps_lock; | ||
| 689 | }; | ||
| 690 | |||
| 691 | |||
| 692 | |||
| 693 | /* | ||
| 694 | * calculate the number of pages a given length and offset map onto, | ||
| 695 | * if we align the data. | ||
| 696 | */ | ||
| 697 | static inline int calc_pages_for(u64 off, u64 len) | ||
| 698 | { | ||
| 699 | return ((off+len+PAGE_CACHE_SIZE-1) >> PAGE_CACHE_SHIFT) - | ||
| 700 | (off >> PAGE_CACHE_SHIFT); | ||
| 701 | } | ||
| 702 | |||
| 703 | |||
| 704 | |||
| 705 | /* snap.c */ | ||
| 706 | struct ceph_snap_realm *ceph_lookup_snap_realm(struct ceph_mds_client *mdsc, | ||
| 707 | u64 ino); | ||
| 708 | extern void ceph_get_snap_realm(struct ceph_mds_client *mdsc, | ||
| 709 | struct ceph_snap_realm *realm); | ||
| 710 | extern void ceph_put_snap_realm(struct ceph_mds_client *mdsc, | ||
| 711 | struct ceph_snap_realm *realm); | ||
| 712 | extern int ceph_update_snap_trace(struct ceph_mds_client *m, | ||
| 713 | void *p, void *e, bool deletion); | ||
| 714 | extern void ceph_handle_snap(struct ceph_mds_client *mdsc, | ||
| 715 | struct ceph_mds_session *session, | ||
| 716 | struct ceph_msg *msg); | ||
| 717 | extern void ceph_queue_cap_snap(struct ceph_inode_info *ci, | ||
| 718 | struct ceph_snap_context *snapc); | ||
| 719 | extern int __ceph_finish_cap_snap(struct ceph_inode_info *ci, | ||
| 720 | struct ceph_cap_snap *capsnap); | ||
| 721 | extern void ceph_cleanup_empty_realms(struct ceph_mds_client *mdsc); | ||
| 722 | |||
| 723 | /* | ||
| 724 | * a cap_snap is "pending" if it is still awaiting an in-progress | ||
| 725 | * sync write (that may/may not still update size, mtime, etc.). | ||
| 726 | */ | ||
| 727 | static inline bool __ceph_have_pending_cap_snap(struct ceph_inode_info *ci) | ||
| 728 | { | ||
| 729 | return !list_empty(&ci->i_cap_snaps) && | ||
| 730 | list_entry(ci->i_cap_snaps.prev, struct ceph_cap_snap, | ||
| 731 | ci_item)->writing; | ||
| 732 | } | ||
| 733 | |||
| 734 | |||
| 735 | /* super.c */ | ||
| 736 | extern struct kmem_cache *ceph_inode_cachep; | ||
| 737 | extern struct kmem_cache *ceph_cap_cachep; | ||
| 738 | extern struct kmem_cache *ceph_dentry_cachep; | ||
| 739 | extern struct kmem_cache *ceph_file_cachep; | ||
| 740 | |||
| 741 | extern const char *ceph_msg_type_name(int type); | ||
| 742 | extern int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid); | ||
| 743 | |||
| 744 | #define FSID_FORMAT "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-" \ | ||
| 745 | "%02x%02x%02x%02x%02x%02x" | ||
| 746 | #define PR_FSID(f) (f)->fsid[0], (f)->fsid[1], (f)->fsid[2], (f)->fsid[3], \ | ||
| 747 | (f)->fsid[4], (f)->fsid[5], (f)->fsid[6], (f)->fsid[7], \ | ||
| 748 | (f)->fsid[8], (f)->fsid[9], (f)->fsid[10], (f)->fsid[11], \ | ||
| 749 | (f)->fsid[12], (f)->fsid[13], (f)->fsid[14], (f)->fsid[15] | ||
| 750 | |||
| 751 | /* inode.c */ | ||
| 752 | extern const struct inode_operations ceph_file_iops; | ||
| 753 | |||
| 754 | extern struct inode *ceph_alloc_inode(struct super_block *sb); | ||
| 755 | extern void ceph_destroy_inode(struct inode *inode); | ||
| 756 | |||
| 757 | extern struct inode *ceph_get_inode(struct super_block *sb, | ||
| 758 | struct ceph_vino vino); | ||
| 759 | extern struct inode *ceph_get_snapdir(struct inode *parent); | ||
| 760 | extern int ceph_fill_file_size(struct inode *inode, int issued, | ||
| 761 | u32 truncate_seq, u64 truncate_size, u64 size); | ||
| 762 | extern void ceph_fill_file_time(struct inode *inode, int issued, | ||
| 763 | u64 time_warp_seq, struct timespec *ctime, | ||
| 764 | struct timespec *mtime, struct timespec *atime); | ||
| 765 | extern int ceph_fill_trace(struct super_block *sb, | ||
| 766 | struct ceph_mds_request *req, | ||
| 767 | struct ceph_mds_session *session); | ||
| 768 | extern int ceph_readdir_prepopulate(struct ceph_mds_request *req, | ||
| 769 | struct ceph_mds_session *session); | ||
| 770 | |||
| 771 | extern int ceph_inode_holds_cap(struct inode *inode, int mask); | ||
| 772 | |||
| 773 | extern int ceph_inode_set_size(struct inode *inode, loff_t size); | ||
| 774 | extern void __ceph_do_pending_vmtruncate(struct inode *inode); | ||
| 775 | extern void ceph_queue_vmtruncate(struct inode *inode); | ||
| 776 | |||
| 777 | extern void ceph_queue_invalidate(struct inode *inode); | ||
| 778 | extern void ceph_queue_writeback(struct inode *inode); | ||
| 779 | |||
| 780 | extern int ceph_do_getattr(struct inode *inode, int mask); | ||
| 781 | extern int ceph_permission(struct inode *inode, int mask); | ||
| 782 | extern int ceph_setattr(struct dentry *dentry, struct iattr *attr); | ||
| 783 | extern int ceph_getattr(struct vfsmount *mnt, struct dentry *dentry, | ||
| 784 | struct kstat *stat); | ||
| 785 | |||
| 786 | /* xattr.c */ | ||
| 787 | extern int ceph_setxattr(struct dentry *, const char *, const void *, | ||
| 788 | size_t, int); | ||
| 789 | extern ssize_t ceph_getxattr(struct dentry *, const char *, void *, size_t); | ||
| 790 | extern ssize_t ceph_listxattr(struct dentry *, char *, size_t); | ||
| 791 | extern int ceph_removexattr(struct dentry *, const char *); | ||
| 792 | extern void __ceph_build_xattrs_blob(struct ceph_inode_info *ci); | ||
| 793 | extern void __ceph_destroy_xattrs(struct ceph_inode_info *ci); | ||
| 794 | |||
| 795 | /* caps.c */ | ||
| 796 | extern const char *ceph_cap_string(int c); | ||
| 797 | extern void ceph_handle_caps(struct ceph_mds_session *session, | ||
| 798 | struct ceph_msg *msg); | ||
| 799 | extern int ceph_add_cap(struct inode *inode, | ||
| 800 | struct ceph_mds_session *session, u64 cap_id, | ||
| 801 | int fmode, unsigned issued, unsigned wanted, | ||
| 802 | unsigned cap, unsigned seq, u64 realmino, int flags, | ||
| 803 | struct ceph_cap_reservation *caps_reservation); | ||
| 804 | extern void __ceph_remove_cap(struct ceph_cap *cap); | ||
| 805 | static inline void ceph_remove_cap(struct ceph_cap *cap) | ||
| 806 | { | ||
| 807 | struct inode *inode = &cap->ci->vfs_inode; | ||
| 808 | spin_lock(&inode->i_lock); | ||
| 809 | __ceph_remove_cap(cap); | ||
| 810 | spin_unlock(&inode->i_lock); | ||
| 811 | } | ||
| 812 | extern void ceph_put_cap(struct ceph_cap *cap); | ||
| 813 | |||
| 814 | extern void ceph_queue_caps_release(struct inode *inode); | ||
| 815 | extern int ceph_write_inode(struct inode *inode, struct writeback_control *wbc); | ||
| 816 | extern int ceph_fsync(struct file *file, struct dentry *dentry, int datasync); | ||
| 817 | extern void ceph_kick_flushing_caps(struct ceph_mds_client *mdsc, | ||
| 818 | struct ceph_mds_session *session); | ||
| 819 | extern int ceph_get_cap_mds(struct inode *inode); | ||
| 820 | extern void ceph_get_cap_refs(struct ceph_inode_info *ci, int caps); | ||
| 821 | extern void ceph_put_cap_refs(struct ceph_inode_info *ci, int had); | ||
| 822 | extern void ceph_put_wrbuffer_cap_refs(struct ceph_inode_info *ci, int nr, | ||
| 823 | struct ceph_snap_context *snapc); | ||
| 824 | extern void __ceph_flush_snaps(struct ceph_inode_info *ci, | ||
| 825 | struct ceph_mds_session **psession); | ||
| 826 | extern void ceph_check_caps(struct ceph_inode_info *ci, int flags, | ||
| 827 | struct ceph_mds_session *session); | ||
| 828 | extern void ceph_check_delayed_caps(struct ceph_mds_client *mdsc); | ||
| 829 | extern void ceph_flush_dirty_caps(struct ceph_mds_client *mdsc); | ||
| 830 | |||
| 831 | extern int ceph_encode_inode_release(void **p, struct inode *inode, | ||
| 832 | int mds, int drop, int unless, int force); | ||
| 833 | extern int ceph_encode_dentry_release(void **p, struct dentry *dn, | ||
| 834 | int mds, int drop, int unless); | ||
| 835 | |||
| 836 | extern int ceph_get_caps(struct ceph_inode_info *ci, int need, int want, | ||
| 837 | int *got, loff_t endoff); | ||
| 838 | |||
| 839 | /* for counting open files by mode */ | ||
| 840 | static inline void __ceph_get_fmode(struct ceph_inode_info *ci, int mode) | ||
| 841 | { | ||
| 842 | ci->i_nr_by_mode[mode]++; | ||
| 843 | } | ||
| 844 | extern void ceph_put_fmode(struct ceph_inode_info *ci, int mode); | ||
| 845 | |||
| 846 | /* addr.c */ | ||
| 847 | extern const struct address_space_operations ceph_aops; | ||
| 848 | extern int ceph_mmap(struct file *file, struct vm_area_struct *vma); | ||
| 849 | |||
| 850 | /* file.c */ | ||
| 851 | extern const struct file_operations ceph_file_fops; | ||
| 852 | extern const struct address_space_operations ceph_aops; | ||
| 853 | extern int ceph_open(struct inode *inode, struct file *file); | ||
| 854 | extern struct dentry *ceph_lookup_open(struct inode *dir, struct dentry *dentry, | ||
| 855 | struct nameidata *nd, int mode, | ||
| 856 | int locked_dir); | ||
| 857 | extern int ceph_release(struct inode *inode, struct file *filp); | ||
| 858 | extern void ceph_release_page_vector(struct page **pages, int num_pages); | ||
| 859 | |||
| 860 | /* dir.c */ | ||
| 861 | extern const struct file_operations ceph_dir_fops; | ||
| 862 | extern const struct inode_operations ceph_dir_iops; | ||
| 863 | extern struct dentry_operations ceph_dentry_ops, ceph_snap_dentry_ops, | ||
| 864 | ceph_snapdir_dentry_ops; | ||
| 865 | |||
| 866 | extern int ceph_handle_notrace_create(struct inode *dir, struct dentry *dentry); | ||
| 867 | extern struct dentry *ceph_finish_lookup(struct ceph_mds_request *req, | ||
| 868 | struct dentry *dentry, int err); | ||
| 869 | |||
| 870 | extern void ceph_dentry_lru_add(struct dentry *dn); | ||
| 871 | extern void ceph_dentry_lru_touch(struct dentry *dn); | ||
| 872 | extern void ceph_dentry_lru_del(struct dentry *dn); | ||
| 873 | |||
| 874 | /* | ||
| 875 | * our d_ops vary depending on whether the inode is live, | ||
| 876 | * snapshotted (read-only), or a virtual ".snap" directory. | ||
| 877 | */ | ||
| 878 | int ceph_init_dentry(struct dentry *dentry); | ||
| 879 | |||
| 880 | |||
| 881 | /* ioctl.c */ | ||
| 882 | extern long ceph_ioctl(struct file *file, unsigned int cmd, unsigned long arg); | ||
| 883 | |||
| 884 | /* export.c */ | ||
| 885 | extern const struct export_operations ceph_export_ops; | ||
| 886 | |||
| 887 | /* debugfs.c */ | ||
| 888 | extern int ceph_debugfs_init(void); | ||
| 889 | extern void ceph_debugfs_cleanup(void); | ||
| 890 | extern int ceph_debugfs_client_init(struct ceph_client *client); | ||
| 891 | extern void ceph_debugfs_client_cleanup(struct ceph_client *client); | ||
| 892 | |||
| 893 | static inline struct inode *get_dentry_parent_inode(struct dentry *dentry) | ||
| 894 | { | ||
| 895 | if (dentry && dentry->d_parent) | ||
| 896 | return dentry->d_parent->d_inode; | ||
| 897 | |||
| 898 | return NULL; | ||
| 899 | } | ||
| 900 | |||
| 901 | #endif /* _FS_CEPH_SUPER_H */ | ||
diff --git a/fs/ceph/types.h b/fs/ceph/types.h new file mode 100644 index 000000000000..28b35a005ec2 --- /dev/null +++ b/fs/ceph/types.h | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | #ifndef _FS_CEPH_TYPES_H | ||
| 2 | #define _FS_CEPH_TYPES_H | ||
| 3 | |||
| 4 | /* needed before including ceph_fs.h */ | ||
| 5 | #include <linux/in.h> | ||
| 6 | #include <linux/types.h> | ||
| 7 | #include <linux/fcntl.h> | ||
| 8 | #include <linux/string.h> | ||
| 9 | |||
| 10 | #include "ceph_fs.h" | ||
| 11 | #include "ceph_frag.h" | ||
| 12 | #include "ceph_hash.h" | ||
| 13 | |||
| 14 | /* | ||
| 15 | * Identify inodes by both their ino AND snapshot id (a u64). | ||
| 16 | */ | ||
| 17 | struct ceph_vino { | ||
| 18 | u64 ino; | ||
| 19 | u64 snap; | ||
| 20 | }; | ||
| 21 | |||
| 22 | |||
| 23 | /* context for the caps reservation mechanism */ | ||
| 24 | struct ceph_cap_reservation { | ||
| 25 | int count; | ||
| 26 | }; | ||
| 27 | |||
| 28 | |||
| 29 | #endif | ||
diff --git a/fs/ceph/xattr.c b/fs/ceph/xattr.c new file mode 100644 index 000000000000..37d6ce645691 --- /dev/null +++ b/fs/ceph/xattr.c | |||
| @@ -0,0 +1,844 @@ | |||
| 1 | #include "ceph_debug.h" | ||
| 2 | #include "super.h" | ||
| 3 | #include "decode.h" | ||
| 4 | |||
| 5 | #include <linux/xattr.h> | ||
| 6 | |||
| 7 | static bool ceph_is_valid_xattr(const char *name) | ||
| 8 | { | ||
| 9 | return !strncmp(name, XATTR_SECURITY_PREFIX, | ||
| 10 | XATTR_SECURITY_PREFIX_LEN) || | ||
| 11 | !strncmp(name, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) || | ||
| 12 | !strncmp(name, XATTR_USER_PREFIX, XATTR_USER_PREFIX_LEN); | ||
| 13 | } | ||
| 14 | |||
| 15 | /* | ||
| 16 | * These define virtual xattrs exposing the recursive directory | ||
| 17 | * statistics and layout metadata. | ||
| 18 | */ | ||
| 19 | struct ceph_vxattr_cb { | ||
| 20 | bool readonly; | ||
| 21 | char *name; | ||
| 22 | size_t (*getxattr_cb)(struct ceph_inode_info *ci, char *val, | ||
| 23 | size_t size); | ||
| 24 | }; | ||
| 25 | |||
| 26 | /* directories */ | ||
| 27 | |||
| 28 | static size_t ceph_vxattrcb_entries(struct ceph_inode_info *ci, char *val, | ||
| 29 | size_t size) | ||
| 30 | { | ||
| 31 | return snprintf(val, size, "%lld", ci->i_files + ci->i_subdirs); | ||
| 32 | } | ||
| 33 | |||
| 34 | static size_t ceph_vxattrcb_files(struct ceph_inode_info *ci, char *val, | ||
| 35 | size_t size) | ||
| 36 | { | ||
| 37 | return snprintf(val, size, "%lld", ci->i_files); | ||
| 38 | } | ||
| 39 | |||
| 40 | static size_t ceph_vxattrcb_subdirs(struct ceph_inode_info *ci, char *val, | ||
| 41 | size_t size) | ||
| 42 | { | ||
| 43 | return snprintf(val, size, "%lld", ci->i_subdirs); | ||
| 44 | } | ||
| 45 | |||
| 46 | static size_t ceph_vxattrcb_rentries(struct ceph_inode_info *ci, char *val, | ||
| 47 | size_t size) | ||
| 48 | { | ||
| 49 | return snprintf(val, size, "%lld", ci->i_rfiles + ci->i_rsubdirs); | ||
| 50 | } | ||
| 51 | |||
| 52 | static size_t ceph_vxattrcb_rfiles(struct ceph_inode_info *ci, char *val, | ||
| 53 | size_t size) | ||
| 54 | { | ||
| 55 | return snprintf(val, size, "%lld", ci->i_rfiles); | ||
| 56 | } | ||
| 57 | |||
| 58 | static size_t ceph_vxattrcb_rsubdirs(struct ceph_inode_info *ci, char *val, | ||
| 59 | size_t size) | ||
| 60 | { | ||
| 61 | return snprintf(val, size, "%lld", ci->i_rsubdirs); | ||
| 62 | } | ||
| 63 | |||
| 64 | static size_t ceph_vxattrcb_rbytes(struct ceph_inode_info *ci, char *val, | ||
| 65 | size_t size) | ||
| 66 | { | ||
| 67 | return snprintf(val, size, "%lld", ci->i_rbytes); | ||
| 68 | } | ||
| 69 | |||
| 70 | static size_t ceph_vxattrcb_rctime(struct ceph_inode_info *ci, char *val, | ||
| 71 | size_t size) | ||
| 72 | { | ||
| 73 | return snprintf(val, size, "%ld.%ld", (long)ci->i_rctime.tv_sec, | ||
| 74 | (long)ci->i_rctime.tv_nsec); | ||
| 75 | } | ||
| 76 | |||
| 77 | static struct ceph_vxattr_cb ceph_dir_vxattrs[] = { | ||
| 78 | { true, "user.ceph.dir.entries", ceph_vxattrcb_entries}, | ||
| 79 | { true, "user.ceph.dir.files", ceph_vxattrcb_files}, | ||
| 80 | { true, "user.ceph.dir.subdirs", ceph_vxattrcb_subdirs}, | ||
| 81 | { true, "user.ceph.dir.rentries", ceph_vxattrcb_rentries}, | ||
| 82 | { true, "user.ceph.dir.rfiles", ceph_vxattrcb_rfiles}, | ||
| 83 | { true, "user.ceph.dir.rsubdirs", ceph_vxattrcb_rsubdirs}, | ||
| 84 | { true, "user.ceph.dir.rbytes", ceph_vxattrcb_rbytes}, | ||
| 85 | { true, "user.ceph.dir.rctime", ceph_vxattrcb_rctime}, | ||
| 86 | { true, NULL, NULL } | ||
| 87 | }; | ||
| 88 | |||
| 89 | /* files */ | ||
| 90 | |||
| 91 | static size_t ceph_vxattrcb_layout(struct ceph_inode_info *ci, char *val, | ||
| 92 | size_t size) | ||
| 93 | { | ||
| 94 | int ret; | ||
| 95 | |||
| 96 | ret = snprintf(val, size, | ||
| 97 | "chunk_bytes=%lld\nstripe_count=%lld\nobject_size=%lld\n", | ||
| 98 | (unsigned long long)ceph_file_layout_su(ci->i_layout), | ||
| 99 | (unsigned long long)ceph_file_layout_stripe_count(ci->i_layout), | ||
| 100 | (unsigned long long)ceph_file_layout_object_size(ci->i_layout)); | ||
| 101 | if (ceph_file_layout_pg_preferred(ci->i_layout)) | ||
| 102 | ret += snprintf(val + ret, size, "preferred_osd=%lld\n", | ||
| 103 | (unsigned long long)ceph_file_layout_pg_preferred( | ||
| 104 | ci->i_layout)); | ||
| 105 | return ret; | ||
| 106 | } | ||
| 107 | |||
| 108 | static struct ceph_vxattr_cb ceph_file_vxattrs[] = { | ||
| 109 | { true, "user.ceph.layout", ceph_vxattrcb_layout}, | ||
| 110 | { NULL, NULL } | ||
| 111 | }; | ||
| 112 | |||
| 113 | static struct ceph_vxattr_cb *ceph_inode_vxattrs(struct inode *inode) | ||
| 114 | { | ||
| 115 | if (S_ISDIR(inode->i_mode)) | ||
| 116 | return ceph_dir_vxattrs; | ||
| 117 | else if (S_ISREG(inode->i_mode)) | ||
| 118 | return ceph_file_vxattrs; | ||
| 119 | return NULL; | ||
| 120 | } | ||
| 121 | |||
| 122 | static struct ceph_vxattr_cb *ceph_match_vxattr(struct ceph_vxattr_cb *vxattr, | ||
| 123 | const char *name) | ||
| 124 | { | ||
| 125 | do { | ||
| 126 | if (strcmp(vxattr->name, name) == 0) | ||
| 127 | return vxattr; | ||
| 128 | vxattr++; | ||
| 129 | } while (vxattr->name); | ||
| 130 | return NULL; | ||
| 131 | } | ||
| 132 | |||
| 133 | static int __set_xattr(struct ceph_inode_info *ci, | ||
| 134 | const char *name, int name_len, | ||
| 135 | const char *val, int val_len, | ||
| 136 | int dirty, | ||
| 137 | int should_free_name, int should_free_val, | ||
| 138 | struct ceph_inode_xattr **newxattr) | ||
| 139 | { | ||
| 140 | struct rb_node **p; | ||
| 141 | struct rb_node *parent = NULL; | ||
| 142 | struct ceph_inode_xattr *xattr = NULL; | ||
| 143 | int c; | ||
| 144 | int new = 0; | ||
| 145 | |||
| 146 | p = &ci->i_xattrs.index.rb_node; | ||
| 147 | while (*p) { | ||
| 148 | parent = *p; | ||
| 149 | xattr = rb_entry(parent, struct ceph_inode_xattr, node); | ||
| 150 | c = strncmp(name, xattr->name, min(name_len, xattr->name_len)); | ||
| 151 | if (c < 0) | ||
| 152 | p = &(*p)->rb_left; | ||
| 153 | else if (c > 0) | ||
| 154 | p = &(*p)->rb_right; | ||
| 155 | else { | ||
| 156 | if (name_len == xattr->name_len) | ||
| 157 | break; | ||
| 158 | else if (name_len < xattr->name_len) | ||
| 159 | p = &(*p)->rb_left; | ||
| 160 | else | ||
| 161 | p = &(*p)->rb_right; | ||
| 162 | } | ||
| 163 | xattr = NULL; | ||
| 164 | } | ||
| 165 | |||
| 166 | if (!xattr) { | ||
| 167 | new = 1; | ||
| 168 | xattr = *newxattr; | ||
| 169 | xattr->name = name; | ||
| 170 | xattr->name_len = name_len; | ||
| 171 | xattr->should_free_name = should_free_name; | ||
| 172 | |||
| 173 | ci->i_xattrs.count++; | ||
| 174 | dout("__set_xattr count=%d\n", ci->i_xattrs.count); | ||
| 175 | } else { | ||
| 176 | kfree(*newxattr); | ||
| 177 | *newxattr = NULL; | ||
| 178 | if (xattr->should_free_val) | ||
| 179 | kfree((void *)xattr->val); | ||
| 180 | |||
| 181 | if (should_free_name) { | ||
| 182 | kfree((void *)name); | ||
| 183 | name = xattr->name; | ||
| 184 | } | ||
| 185 | ci->i_xattrs.names_size -= xattr->name_len; | ||
| 186 | ci->i_xattrs.vals_size -= xattr->val_len; | ||
| 187 | } | ||
| 188 | if (!xattr) { | ||
| 189 | pr_err("__set_xattr ENOMEM on %p %llx.%llx xattr %s=%s\n", | ||
| 190 | &ci->vfs_inode, ceph_vinop(&ci->vfs_inode), name, | ||
| 191 | xattr->val); | ||
| 192 | return -ENOMEM; | ||
| 193 | } | ||
| 194 | ci->i_xattrs.names_size += name_len; | ||
| 195 | ci->i_xattrs.vals_size += val_len; | ||
| 196 | if (val) | ||
| 197 | xattr->val = val; | ||
| 198 | else | ||
| 199 | xattr->val = ""; | ||
| 200 | |||
| 201 | xattr->val_len = val_len; | ||
| 202 | xattr->dirty = dirty; | ||
| 203 | xattr->should_free_val = (val && should_free_val); | ||
| 204 | |||
| 205 | if (new) { | ||
| 206 | rb_link_node(&xattr->node, parent, p); | ||
| 207 | rb_insert_color(&xattr->node, &ci->i_xattrs.index); | ||
| 208 | dout("__set_xattr_val p=%p\n", p); | ||
| 209 | } | ||
| 210 | |||
| 211 | dout("__set_xattr_val added %llx.%llx xattr %p %s=%.*s\n", | ||
| 212 | ceph_vinop(&ci->vfs_inode), xattr, name, val_len, val); | ||
| 213 | |||
| 214 | return 0; | ||
| 215 | } | ||
| 216 | |||
| 217 | static struct ceph_inode_xattr *__get_xattr(struct ceph_inode_info *ci, | ||
| 218 | const char *name) | ||
| 219 | { | ||
| 220 | struct rb_node **p; | ||
| 221 | struct rb_node *parent = NULL; | ||
| 222 | struct ceph_inode_xattr *xattr = NULL; | ||
| 223 | int c; | ||
| 224 | |||
| 225 | p = &ci->i_xattrs.index.rb_node; | ||
| 226 | while (*p) { | ||
| 227 | parent = *p; | ||
| 228 | xattr = rb_entry(parent, struct ceph_inode_xattr, node); | ||
| 229 | c = strncmp(name, xattr->name, xattr->name_len); | ||
| 230 | if (c < 0) | ||
| 231 | p = &(*p)->rb_left; | ||
| 232 | else if (c > 0) | ||
| 233 | p = &(*p)->rb_right; | ||
| 234 | else { | ||
| 235 | dout("__get_xattr %s: found %.*s\n", name, | ||
| 236 | xattr->val_len, xattr->val); | ||
| 237 | return xattr; | ||
| 238 | } | ||
| 239 | } | ||
| 240 | |||
| 241 | dout("__get_xattr %s: not found\n", name); | ||
| 242 | |||
| 243 | return NULL; | ||
| 244 | } | ||
| 245 | |||
| 246 | static void __free_xattr(struct ceph_inode_xattr *xattr) | ||
| 247 | { | ||
| 248 | BUG_ON(!xattr); | ||
| 249 | |||
| 250 | if (xattr->should_free_name) | ||
| 251 | kfree((void *)xattr->name); | ||
| 252 | if (xattr->should_free_val) | ||
| 253 | kfree((void *)xattr->val); | ||
| 254 | |||
| 255 | kfree(xattr); | ||
| 256 | } | ||
| 257 | |||
| 258 | static int __remove_xattr(struct ceph_inode_info *ci, | ||
| 259 | struct ceph_inode_xattr *xattr) | ||
| 260 | { | ||
| 261 | if (!xattr) | ||
| 262 | return -EOPNOTSUPP; | ||
| 263 | |||
| 264 | rb_erase(&xattr->node, &ci->i_xattrs.index); | ||
| 265 | |||
| 266 | if (xattr->should_free_name) | ||
| 267 | kfree((void *)xattr->name); | ||
| 268 | if (xattr->should_free_val) | ||
| 269 | kfree((void *)xattr->val); | ||
| 270 | |||
| 271 | ci->i_xattrs.names_size -= xattr->name_len; | ||
| 272 | ci->i_xattrs.vals_size -= xattr->val_len; | ||
| 273 | ci->i_xattrs.count--; | ||
| 274 | kfree(xattr); | ||
| 275 | |||
| 276 | return 0; | ||
| 277 | } | ||
| 278 | |||
| 279 | static int __remove_xattr_by_name(struct ceph_inode_info *ci, | ||
| 280 | const char *name) | ||
| 281 | { | ||
| 282 | struct rb_node **p; | ||
| 283 | struct ceph_inode_xattr *xattr; | ||
| 284 | int err; | ||
| 285 | |||
| 286 | p = &ci->i_xattrs.index.rb_node; | ||
| 287 | xattr = __get_xattr(ci, name); | ||
| 288 | err = __remove_xattr(ci, xattr); | ||
| 289 | return err; | ||
| 290 | } | ||
| 291 | |||
| 292 | static char *__copy_xattr_names(struct ceph_inode_info *ci, | ||
| 293 | char *dest) | ||
| 294 | { | ||
| 295 | struct rb_node *p; | ||
| 296 | struct ceph_inode_xattr *xattr = NULL; | ||
| 297 | |||
| 298 | p = rb_first(&ci->i_xattrs.index); | ||
| 299 | dout("__copy_xattr_names count=%d\n", ci->i_xattrs.count); | ||
| 300 | |||
| 301 | while (p) { | ||
| 302 | xattr = rb_entry(p, struct ceph_inode_xattr, node); | ||
| 303 | memcpy(dest, xattr->name, xattr->name_len); | ||
| 304 | dest[xattr->name_len] = '\0'; | ||
| 305 | |||
| 306 | dout("dest=%s %p (%s) (%d/%d)\n", dest, xattr, xattr->name, | ||
| 307 | xattr->name_len, ci->i_xattrs.names_size); | ||
| 308 | |||
| 309 | dest += xattr->name_len + 1; | ||
| 310 | p = rb_next(p); | ||
| 311 | } | ||
| 312 | |||
| 313 | return dest; | ||
| 314 | } | ||
| 315 | |||
| 316 | void __ceph_destroy_xattrs(struct ceph_inode_info *ci) | ||
| 317 | { | ||
| 318 | struct rb_node *p, *tmp; | ||
| 319 | struct ceph_inode_xattr *xattr = NULL; | ||
| 320 | |||
| 321 | p = rb_first(&ci->i_xattrs.index); | ||
| 322 | |||
| 323 | dout("__ceph_destroy_xattrs p=%p\n", p); | ||
| 324 | |||
| 325 | while (p) { | ||
| 326 | xattr = rb_entry(p, struct ceph_inode_xattr, node); | ||
| 327 | tmp = p; | ||
| 328 | p = rb_next(tmp); | ||
| 329 | dout("__ceph_destroy_xattrs next p=%p (%.*s)\n", p, | ||
| 330 | xattr->name_len, xattr->name); | ||
| 331 | rb_erase(tmp, &ci->i_xattrs.index); | ||
| 332 | |||
| 333 | __free_xattr(xattr); | ||
| 334 | } | ||
| 335 | |||
| 336 | ci->i_xattrs.names_size = 0; | ||
| 337 | ci->i_xattrs.vals_size = 0; | ||
| 338 | ci->i_xattrs.index_version = 0; | ||
| 339 | ci->i_xattrs.count = 0; | ||
| 340 | ci->i_xattrs.index = RB_ROOT; | ||
| 341 | } | ||
| 342 | |||
| 343 | static int __build_xattrs(struct inode *inode) | ||
| 344 | { | ||
| 345 | u32 namelen; | ||
| 346 | u32 numattr = 0; | ||
| 347 | void *p, *end; | ||
| 348 | u32 len; | ||
| 349 | const char *name, *val; | ||
| 350 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 351 | int xattr_version; | ||
| 352 | struct ceph_inode_xattr **xattrs = NULL; | ||
| 353 | int err = 0; | ||
| 354 | int i; | ||
| 355 | |||
| 356 | dout("__build_xattrs() len=%d\n", | ||
| 357 | ci->i_xattrs.blob ? (int)ci->i_xattrs.blob->vec.iov_len : 0); | ||
| 358 | |||
| 359 | if (ci->i_xattrs.index_version >= ci->i_xattrs.version) | ||
| 360 | return 0; /* already built */ | ||
| 361 | |||
| 362 | __ceph_destroy_xattrs(ci); | ||
| 363 | |||
| 364 | start: | ||
| 365 | /* updated internal xattr rb tree */ | ||
| 366 | if (ci->i_xattrs.blob && ci->i_xattrs.blob->vec.iov_len > 4) { | ||
| 367 | p = ci->i_xattrs.blob->vec.iov_base; | ||
| 368 | end = p + ci->i_xattrs.blob->vec.iov_len; | ||
| 369 | ceph_decode_32_safe(&p, end, numattr, bad); | ||
| 370 | xattr_version = ci->i_xattrs.version; | ||
| 371 | spin_unlock(&inode->i_lock); | ||
| 372 | |||
| 373 | xattrs = kcalloc(numattr, sizeof(struct ceph_xattr *), | ||
| 374 | GFP_NOFS); | ||
| 375 | err = -ENOMEM; | ||
| 376 | if (!xattrs) | ||
| 377 | goto bad_lock; | ||
| 378 | memset(xattrs, 0, numattr*sizeof(struct ceph_xattr *)); | ||
| 379 | for (i = 0; i < numattr; i++) { | ||
| 380 | xattrs[i] = kmalloc(sizeof(struct ceph_inode_xattr), | ||
| 381 | GFP_NOFS); | ||
| 382 | if (!xattrs[i]) | ||
| 383 | goto bad_lock; | ||
| 384 | } | ||
| 385 | |||
| 386 | spin_lock(&inode->i_lock); | ||
| 387 | if (ci->i_xattrs.version != xattr_version) { | ||
| 388 | /* lost a race, retry */ | ||
| 389 | for (i = 0; i < numattr; i++) | ||
| 390 | kfree(xattrs[i]); | ||
| 391 | kfree(xattrs); | ||
| 392 | goto start; | ||
| 393 | } | ||
| 394 | err = -EIO; | ||
| 395 | while (numattr--) { | ||
| 396 | ceph_decode_32_safe(&p, end, len, bad); | ||
| 397 | namelen = len; | ||
| 398 | name = p; | ||
| 399 | p += len; | ||
| 400 | ceph_decode_32_safe(&p, end, len, bad); | ||
| 401 | val = p; | ||
| 402 | p += len; | ||
| 403 | |||
| 404 | err = __set_xattr(ci, name, namelen, val, len, | ||
| 405 | 0, 0, 0, &xattrs[numattr]); | ||
| 406 | |||
| 407 | if (err < 0) | ||
| 408 | goto bad; | ||
| 409 | } | ||
| 410 | kfree(xattrs); | ||
| 411 | } | ||
| 412 | ci->i_xattrs.index_version = ci->i_xattrs.version; | ||
| 413 | ci->i_xattrs.dirty = false; | ||
| 414 | |||
| 415 | return err; | ||
| 416 | bad_lock: | ||
| 417 | spin_lock(&inode->i_lock); | ||
| 418 | bad: | ||
| 419 | if (xattrs) { | ||
| 420 | for (i = 0; i < numattr; i++) | ||
| 421 | kfree(xattrs[i]); | ||
| 422 | kfree(xattrs); | ||
| 423 | } | ||
| 424 | ci->i_xattrs.names_size = 0; | ||
| 425 | return err; | ||
| 426 | } | ||
| 427 | |||
| 428 | static int __get_required_blob_size(struct ceph_inode_info *ci, int name_size, | ||
| 429 | int val_size) | ||
| 430 | { | ||
| 431 | /* | ||
| 432 | * 4 bytes for the length, and additional 4 bytes per each xattr name, | ||
| 433 | * 4 bytes per each value | ||
| 434 | */ | ||
| 435 | int size = 4 + ci->i_xattrs.count*(4 + 4) + | ||
| 436 | ci->i_xattrs.names_size + | ||
| 437 | ci->i_xattrs.vals_size; | ||
| 438 | dout("__get_required_blob_size c=%d names.size=%d vals.size=%d\n", | ||
| 439 | ci->i_xattrs.count, ci->i_xattrs.names_size, | ||
| 440 | ci->i_xattrs.vals_size); | ||
| 441 | |||
| 442 | if (name_size) | ||
| 443 | size += 4 + 4 + name_size + val_size; | ||
| 444 | |||
| 445 | return size; | ||
| 446 | } | ||
| 447 | |||
| 448 | /* | ||
| 449 | * If there are dirty xattrs, reencode xattrs into the prealloc_blob | ||
| 450 | * and swap into place. | ||
| 451 | */ | ||
| 452 | void __ceph_build_xattrs_blob(struct ceph_inode_info *ci) | ||
| 453 | { | ||
| 454 | struct rb_node *p; | ||
| 455 | struct ceph_inode_xattr *xattr = NULL; | ||
| 456 | void *dest; | ||
| 457 | |||
| 458 | dout("__build_xattrs_blob %p\n", &ci->vfs_inode); | ||
| 459 | if (ci->i_xattrs.dirty) { | ||
| 460 | int need = __get_required_blob_size(ci, 0, 0); | ||
| 461 | |||
| 462 | BUG_ON(need > ci->i_xattrs.prealloc_blob->alloc_len); | ||
| 463 | |||
| 464 | p = rb_first(&ci->i_xattrs.index); | ||
| 465 | dest = ci->i_xattrs.prealloc_blob->vec.iov_base; | ||
| 466 | |||
| 467 | ceph_encode_32(&dest, ci->i_xattrs.count); | ||
| 468 | while (p) { | ||
| 469 | xattr = rb_entry(p, struct ceph_inode_xattr, node); | ||
| 470 | |||
| 471 | ceph_encode_32(&dest, xattr->name_len); | ||
| 472 | memcpy(dest, xattr->name, xattr->name_len); | ||
| 473 | dest += xattr->name_len; | ||
| 474 | ceph_encode_32(&dest, xattr->val_len); | ||
| 475 | memcpy(dest, xattr->val, xattr->val_len); | ||
| 476 | dest += xattr->val_len; | ||
| 477 | |||
| 478 | p = rb_next(p); | ||
| 479 | } | ||
| 480 | |||
| 481 | /* adjust buffer len; it may be larger than we need */ | ||
| 482 | ci->i_xattrs.prealloc_blob->vec.iov_len = | ||
| 483 | dest - ci->i_xattrs.prealloc_blob->vec.iov_base; | ||
| 484 | |||
| 485 | if (ci->i_xattrs.blob) | ||
| 486 | ceph_buffer_put(ci->i_xattrs.blob); | ||
| 487 | ci->i_xattrs.blob = ci->i_xattrs.prealloc_blob; | ||
| 488 | ci->i_xattrs.prealloc_blob = NULL; | ||
| 489 | ci->i_xattrs.dirty = false; | ||
| 490 | } | ||
| 491 | } | ||
| 492 | |||
| 493 | ssize_t ceph_getxattr(struct dentry *dentry, const char *name, void *value, | ||
| 494 | size_t size) | ||
| 495 | { | ||
| 496 | struct inode *inode = dentry->d_inode; | ||
| 497 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 498 | struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode); | ||
| 499 | int err; | ||
| 500 | struct ceph_inode_xattr *xattr; | ||
| 501 | struct ceph_vxattr_cb *vxattr = NULL; | ||
| 502 | |||
| 503 | if (!ceph_is_valid_xattr(name)) | ||
| 504 | return -ENODATA; | ||
| 505 | |||
| 506 | /* let's see if a virtual xattr was requested */ | ||
| 507 | if (vxattrs) | ||
| 508 | vxattr = ceph_match_vxattr(vxattrs, name); | ||
| 509 | |||
| 510 | spin_lock(&inode->i_lock); | ||
| 511 | dout("getxattr %p ver=%lld index_ver=%lld\n", inode, | ||
| 512 | ci->i_xattrs.version, ci->i_xattrs.index_version); | ||
| 513 | |||
| 514 | if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) && | ||
| 515 | (ci->i_xattrs.index_version >= ci->i_xattrs.version)) { | ||
| 516 | goto get_xattr; | ||
| 517 | } else { | ||
| 518 | spin_unlock(&inode->i_lock); | ||
| 519 | /* get xattrs from mds (if we don't already have them) */ | ||
| 520 | err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR); | ||
| 521 | if (err) | ||
| 522 | return err; | ||
| 523 | } | ||
| 524 | |||
| 525 | spin_lock(&inode->i_lock); | ||
| 526 | |||
| 527 | if (vxattr && vxattr->readonly) { | ||
| 528 | err = vxattr->getxattr_cb(ci, value, size); | ||
| 529 | goto out; | ||
| 530 | } | ||
| 531 | |||
| 532 | err = __build_xattrs(inode); | ||
| 533 | if (err < 0) | ||
| 534 | goto out; | ||
| 535 | |||
| 536 | get_xattr: | ||
| 537 | err = -ENODATA; /* == ENOATTR */ | ||
| 538 | xattr = __get_xattr(ci, name); | ||
| 539 | if (!xattr) { | ||
| 540 | if (vxattr) | ||
| 541 | err = vxattr->getxattr_cb(ci, value, size); | ||
| 542 | goto out; | ||
| 543 | } | ||
| 544 | |||
| 545 | err = -ERANGE; | ||
| 546 | if (size && size < xattr->val_len) | ||
| 547 | goto out; | ||
| 548 | |||
| 549 | err = xattr->val_len; | ||
| 550 | if (size == 0) | ||
| 551 | goto out; | ||
| 552 | |||
| 553 | memcpy(value, xattr->val, xattr->val_len); | ||
| 554 | |||
| 555 | out: | ||
| 556 | spin_unlock(&inode->i_lock); | ||
| 557 | return err; | ||
| 558 | } | ||
| 559 | |||
| 560 | ssize_t ceph_listxattr(struct dentry *dentry, char *names, size_t size) | ||
| 561 | { | ||
| 562 | struct inode *inode = dentry->d_inode; | ||
| 563 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 564 | struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode); | ||
| 565 | u32 vir_namelen = 0; | ||
| 566 | u32 namelen; | ||
| 567 | int err; | ||
| 568 | u32 len; | ||
| 569 | int i; | ||
| 570 | |||
| 571 | spin_lock(&inode->i_lock); | ||
| 572 | dout("listxattr %p ver=%lld index_ver=%lld\n", inode, | ||
| 573 | ci->i_xattrs.version, ci->i_xattrs.index_version); | ||
| 574 | |||
| 575 | if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 1) && | ||
| 576 | (ci->i_xattrs.index_version > ci->i_xattrs.version)) { | ||
| 577 | goto list_xattr; | ||
| 578 | } else { | ||
| 579 | spin_unlock(&inode->i_lock); | ||
| 580 | err = ceph_do_getattr(inode, CEPH_STAT_CAP_XATTR); | ||
| 581 | if (err) | ||
| 582 | return err; | ||
| 583 | } | ||
| 584 | |||
| 585 | spin_lock(&inode->i_lock); | ||
| 586 | |||
| 587 | err = __build_xattrs(inode); | ||
| 588 | if (err < 0) | ||
| 589 | goto out; | ||
| 590 | |||
| 591 | list_xattr: | ||
| 592 | vir_namelen = 0; | ||
| 593 | /* include virtual dir xattrs */ | ||
| 594 | if (vxattrs) | ||
| 595 | for (i = 0; vxattrs[i].name; i++) | ||
| 596 | vir_namelen += strlen(vxattrs[i].name) + 1; | ||
| 597 | /* adding 1 byte per each variable due to the null termination */ | ||
| 598 | namelen = vir_namelen + ci->i_xattrs.names_size + ci->i_xattrs.count; | ||
| 599 | err = -ERANGE; | ||
| 600 | if (size && namelen > size) | ||
| 601 | goto out; | ||
| 602 | |||
| 603 | err = namelen; | ||
| 604 | if (size == 0) | ||
| 605 | goto out; | ||
| 606 | |||
| 607 | names = __copy_xattr_names(ci, names); | ||
| 608 | |||
| 609 | /* virtual xattr names, too */ | ||
| 610 | if (vxattrs) | ||
| 611 | for (i = 0; vxattrs[i].name; i++) { | ||
| 612 | len = sprintf(names, "%s", vxattrs[i].name); | ||
| 613 | names += len + 1; | ||
| 614 | } | ||
| 615 | |||
| 616 | out: | ||
| 617 | spin_unlock(&inode->i_lock); | ||
| 618 | return err; | ||
| 619 | } | ||
| 620 | |||
| 621 | static int ceph_sync_setxattr(struct dentry *dentry, const char *name, | ||
| 622 | const char *value, size_t size, int flags) | ||
| 623 | { | ||
| 624 | struct ceph_client *client = ceph_client(dentry->d_sb); | ||
| 625 | struct inode *inode = dentry->d_inode; | ||
| 626 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 627 | struct inode *parent_inode = dentry->d_parent->d_inode; | ||
| 628 | struct ceph_mds_request *req; | ||
| 629 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 630 | int err; | ||
| 631 | int i, nr_pages; | ||
| 632 | struct page **pages = NULL; | ||
| 633 | void *kaddr; | ||
| 634 | |||
| 635 | /* copy value into some pages */ | ||
| 636 | nr_pages = calc_pages_for(0, size); | ||
| 637 | if (nr_pages) { | ||
| 638 | pages = kmalloc(sizeof(pages[0])*nr_pages, GFP_NOFS); | ||
| 639 | if (!pages) | ||
| 640 | return -ENOMEM; | ||
| 641 | err = -ENOMEM; | ||
| 642 | for (i = 0; i < nr_pages; i++) { | ||
| 643 | pages[i] = alloc_page(GFP_NOFS); | ||
| 644 | if (!pages[i]) { | ||
| 645 | nr_pages = i; | ||
| 646 | goto out; | ||
| 647 | } | ||
| 648 | kaddr = kmap(pages[i]); | ||
| 649 | memcpy(kaddr, value + i*PAGE_CACHE_SIZE, | ||
| 650 | min(PAGE_CACHE_SIZE, size-i*PAGE_CACHE_SIZE)); | ||
| 651 | } | ||
| 652 | } | ||
| 653 | |||
| 654 | dout("setxattr value=%.*s\n", (int)size, value); | ||
| 655 | |||
| 656 | /* do request */ | ||
| 657 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_SETXATTR, | ||
| 658 | USE_AUTH_MDS); | ||
| 659 | if (IS_ERR(req)) { | ||
| 660 | err = PTR_ERR(req); | ||
| 661 | goto out; | ||
| 662 | } | ||
| 663 | req->r_inode = igrab(inode); | ||
| 664 | req->r_inode_drop = CEPH_CAP_XATTR_SHARED; | ||
| 665 | req->r_num_caps = 1; | ||
| 666 | req->r_args.setxattr.flags = cpu_to_le32(flags); | ||
| 667 | req->r_path2 = kstrdup(name, GFP_NOFS); | ||
| 668 | |||
| 669 | req->r_pages = pages; | ||
| 670 | req->r_num_pages = nr_pages; | ||
| 671 | req->r_data_len = size; | ||
| 672 | |||
| 673 | dout("xattr.ver (before): %lld\n", ci->i_xattrs.version); | ||
| 674 | err = ceph_mdsc_do_request(mdsc, parent_inode, req); | ||
| 675 | ceph_mdsc_put_request(req); | ||
| 676 | dout("xattr.ver (after): %lld\n", ci->i_xattrs.version); | ||
| 677 | |||
| 678 | out: | ||
| 679 | if (pages) { | ||
| 680 | for (i = 0; i < nr_pages; i++) | ||
| 681 | __free_page(pages[i]); | ||
| 682 | kfree(pages); | ||
| 683 | } | ||
| 684 | return err; | ||
| 685 | } | ||
| 686 | |||
| 687 | int ceph_setxattr(struct dentry *dentry, const char *name, | ||
| 688 | const void *value, size_t size, int flags) | ||
| 689 | { | ||
| 690 | struct inode *inode = dentry->d_inode; | ||
| 691 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 692 | struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode); | ||
| 693 | int err; | ||
| 694 | int name_len = strlen(name); | ||
| 695 | int val_len = size; | ||
| 696 | char *newname = NULL; | ||
| 697 | char *newval = NULL; | ||
| 698 | struct ceph_inode_xattr *xattr = NULL; | ||
| 699 | int issued; | ||
| 700 | int required_blob_size; | ||
| 701 | |||
| 702 | if (ceph_snap(inode) != CEPH_NOSNAP) | ||
| 703 | return -EROFS; | ||
| 704 | |||
| 705 | if (!ceph_is_valid_xattr(name)) | ||
| 706 | return -EOPNOTSUPP; | ||
| 707 | |||
| 708 | if (vxattrs) { | ||
| 709 | struct ceph_vxattr_cb *vxattr = | ||
| 710 | ceph_match_vxattr(vxattrs, name); | ||
| 711 | if (vxattr && vxattr->readonly) | ||
| 712 | return -EOPNOTSUPP; | ||
| 713 | } | ||
| 714 | |||
| 715 | /* preallocate memory for xattr name, value, index node */ | ||
| 716 | err = -ENOMEM; | ||
| 717 | newname = kmalloc(name_len + 1, GFP_NOFS); | ||
| 718 | if (!newname) | ||
| 719 | goto out; | ||
| 720 | memcpy(newname, name, name_len + 1); | ||
| 721 | |||
| 722 | if (val_len) { | ||
| 723 | newval = kmalloc(val_len + 1, GFP_NOFS); | ||
| 724 | if (!newval) | ||
| 725 | goto out; | ||
| 726 | memcpy(newval, value, val_len); | ||
| 727 | newval[val_len] = '\0'; | ||
| 728 | } | ||
| 729 | |||
| 730 | xattr = kmalloc(sizeof(struct ceph_inode_xattr), GFP_NOFS); | ||
| 731 | if (!xattr) | ||
| 732 | goto out; | ||
| 733 | |||
| 734 | spin_lock(&inode->i_lock); | ||
| 735 | retry: | ||
| 736 | issued = __ceph_caps_issued(ci, NULL); | ||
| 737 | if (!(issued & CEPH_CAP_XATTR_EXCL)) | ||
| 738 | goto do_sync; | ||
| 739 | __build_xattrs(inode); | ||
| 740 | |||
| 741 | required_blob_size = __get_required_blob_size(ci, name_len, val_len); | ||
| 742 | |||
| 743 | if (!ci->i_xattrs.prealloc_blob || | ||
| 744 | required_blob_size > ci->i_xattrs.prealloc_blob->alloc_len) { | ||
| 745 | struct ceph_buffer *blob = NULL; | ||
| 746 | |||
| 747 | spin_unlock(&inode->i_lock); | ||
| 748 | dout(" preaallocating new blob size=%d\n", required_blob_size); | ||
| 749 | blob = ceph_buffer_new(required_blob_size, GFP_NOFS); | ||
| 750 | if (!blob) | ||
| 751 | goto out; | ||
| 752 | spin_lock(&inode->i_lock); | ||
| 753 | if (ci->i_xattrs.prealloc_blob) | ||
| 754 | ceph_buffer_put(ci->i_xattrs.prealloc_blob); | ||
| 755 | ci->i_xattrs.prealloc_blob = blob; | ||
| 756 | goto retry; | ||
| 757 | } | ||
| 758 | |||
| 759 | dout("setxattr %p issued %s\n", inode, ceph_cap_string(issued)); | ||
| 760 | err = __set_xattr(ci, newname, name_len, newval, | ||
| 761 | val_len, 1, 1, 1, &xattr); | ||
| 762 | __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL); | ||
| 763 | ci->i_xattrs.dirty = true; | ||
| 764 | inode->i_ctime = CURRENT_TIME; | ||
| 765 | spin_unlock(&inode->i_lock); | ||
| 766 | |||
| 767 | return err; | ||
| 768 | |||
| 769 | do_sync: | ||
| 770 | spin_unlock(&inode->i_lock); | ||
| 771 | err = ceph_sync_setxattr(dentry, name, value, size, flags); | ||
| 772 | out: | ||
| 773 | kfree(newname); | ||
| 774 | kfree(newval); | ||
| 775 | kfree(xattr); | ||
| 776 | return err; | ||
| 777 | } | ||
| 778 | |||
| 779 | static int ceph_send_removexattr(struct dentry *dentry, const char *name) | ||
| 780 | { | ||
| 781 | struct ceph_client *client = ceph_client(dentry->d_sb); | ||
| 782 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
| 783 | struct inode *inode = dentry->d_inode; | ||
| 784 | struct inode *parent_inode = dentry->d_parent->d_inode; | ||
| 785 | struct ceph_mds_request *req; | ||
| 786 | int err; | ||
| 787 | |||
| 788 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_RMXATTR, | ||
| 789 | USE_AUTH_MDS); | ||
| 790 | if (IS_ERR(req)) | ||
| 791 | return PTR_ERR(req); | ||
| 792 | req->r_inode = igrab(inode); | ||
| 793 | req->r_inode_drop = CEPH_CAP_XATTR_SHARED; | ||
| 794 | req->r_num_caps = 1; | ||
| 795 | req->r_path2 = kstrdup(name, GFP_NOFS); | ||
| 796 | |||
| 797 | err = ceph_mdsc_do_request(mdsc, parent_inode, req); | ||
| 798 | ceph_mdsc_put_request(req); | ||
| 799 | return err; | ||
| 800 | } | ||
| 801 | |||
| 802 | int ceph_removexattr(struct dentry *dentry, const char *name) | ||
| 803 | { | ||
| 804 | struct inode *inode = dentry->d_inode; | ||
| 805 | struct ceph_inode_info *ci = ceph_inode(inode); | ||
| 806 | struct ceph_vxattr_cb *vxattrs = ceph_inode_vxattrs(inode); | ||
| 807 | int issued; | ||
| 808 | int err; | ||
| 809 | |||
| 810 | if (ceph_snap(inode) != CEPH_NOSNAP) | ||
| 811 | return -EROFS; | ||
| 812 | |||
| 813 | if (!ceph_is_valid_xattr(name)) | ||
| 814 | return -EOPNOTSUPP; | ||
| 815 | |||
| 816 | if (vxattrs) { | ||
| 817 | struct ceph_vxattr_cb *vxattr = | ||
| 818 | ceph_match_vxattr(vxattrs, name); | ||
| 819 | if (vxattr && vxattr->readonly) | ||
| 820 | return -EOPNOTSUPP; | ||
| 821 | } | ||
| 822 | |||
| 823 | spin_lock(&inode->i_lock); | ||
| 824 | __build_xattrs(inode); | ||
| 825 | issued = __ceph_caps_issued(ci, NULL); | ||
| 826 | dout("removexattr %p issued %s\n", inode, ceph_cap_string(issued)); | ||
| 827 | |||
| 828 | if (!(issued & CEPH_CAP_XATTR_EXCL)) | ||
| 829 | goto do_sync; | ||
| 830 | |||
| 831 | err = __remove_xattr_by_name(ceph_inode(inode), name); | ||
| 832 | __ceph_mark_dirty_caps(ci, CEPH_CAP_XATTR_EXCL); | ||
| 833 | ci->i_xattrs.dirty = true; | ||
| 834 | inode->i_ctime = CURRENT_TIME; | ||
| 835 | |||
| 836 | spin_unlock(&inode->i_lock); | ||
| 837 | |||
| 838 | return err; | ||
| 839 | do_sync: | ||
| 840 | spin_unlock(&inode->i_lock); | ||
| 841 | err = ceph_send_removexattr(dentry, name); | ||
| 842 | return err; | ||
| 843 | } | ||
| 844 | |||
