1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ceph/ceph_debug.h>
3
4 #include <linux/backing-dev.h>
5 #include <linux/fs.h>
6 #include <linux/mm.h>
7 #include <linux/pagemap.h>
8 #include <linux/writeback.h> /* generic_writepages */
9 #include <linux/slab.h>
10 #include <linux/pagevec.h>
11 #include <linux/task_io_accounting_ops.h>
12 #include <linux/signal.h>
13 #include <linux/iversion.h>
14 #include <linux/ktime.h>
15
16 #include "super.h"
17 #include "mds_client.h"
18 #include "cache.h"
19 #include "metric.h"
20 #include <linux/ceph/osd_client.h>
21 #include <linux/ceph/striper.h>
22
23 /*
24 * Ceph address space ops.
25 *
26 * There are a few funny things going on here.
27 *
28 * The page->private field is used to reference a struct
29 * ceph_snap_context for _every_ dirty page. This indicates which
30 * snapshot the page was logically dirtied in, and thus which snap
31 * context needs to be associated with the osd write during writeback.
32 *
33 * Similarly, struct ceph_inode_info maintains a set of counters to
34 * count dirty pages on the inode. In the absence of snapshots,
35 * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
36 *
37 * When a snapshot is taken (that is, when the client receives
38 * notification that a snapshot was taken), each inode with caps and
39 * with dirty pages (dirty pages implies there is a cap) gets a new
40 * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
41 * order, new snaps go to the tail). The i_wrbuffer_ref_head count is
42 * moved to capsnap->dirty. (Unless a sync write is currently in
43 * progress. In that case, the capsnap is said to be "pending", new
44 * writes cannot start, and the capsnap isn't "finalized" until the
45 * write completes (or fails) and a final size/mtime for the inode for
46 * that snap can be settled upon.) i_wrbuffer_ref_head is reset to 0.
47 *
48 * On writeback, we must submit writes to the osd IN SNAP ORDER. So,
49 * we look for the first capsnap in i_cap_snaps and write out pages in
50 * that snap context _only_. Then we move on to the next capsnap,
51 * eventually reaching the "live" or "head" context (i.e., pages that
52 * are not yet snapped) and are writing the most recently dirtied
53 * pages.
54 *
55 * Invalidate and so forth must take care to ensure the dirty page
56 * accounting is preserved.
57 */
58
59 #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
60 #define CONGESTION_OFF_THRESH(congestion_kb) \
61 (CONGESTION_ON_THRESH(congestion_kb) - \
62 (CONGESTION_ON_THRESH(congestion_kb) >> 2))
63
page_snap_context(struct page * page)64 static inline struct ceph_snap_context *page_snap_context(struct page *page)
65 {
66 if (PagePrivate(page))
67 return (void *)page->private;
68 return NULL;
69 }
70
71 /*
72 * Dirty a page. Optimistically adjust accounting, on the assumption
73 * that we won't race with invalidate. If we do, readjust.
74 */
ceph_set_page_dirty(struct page * page)75 static int ceph_set_page_dirty(struct page *page)
76 {
77 struct address_space *mapping = page->mapping;
78 struct inode *inode;
79 struct ceph_inode_info *ci;
80 struct ceph_snap_context *snapc;
81
82 if (PageDirty(page)) {
83 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
84 mapping->host, page, page->index);
85 BUG_ON(!PagePrivate(page));
86 return 0;
87 }
88
89 inode = mapping->host;
90 ci = ceph_inode(inode);
91
92 /* dirty the head */
93 spin_lock(&ci->i_ceph_lock);
94 BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference
95 if (__ceph_have_pending_cap_snap(ci)) {
96 struct ceph_cap_snap *capsnap =
97 list_last_entry(&ci->i_cap_snaps,
98 struct ceph_cap_snap,
99 ci_item);
100 snapc = ceph_get_snap_context(capsnap->context);
101 capsnap->dirty_pages++;
102 } else {
103 BUG_ON(!ci->i_head_snapc);
104 snapc = ceph_get_snap_context(ci->i_head_snapc);
105 ++ci->i_wrbuffer_ref_head;
106 }
107 if (ci->i_wrbuffer_ref == 0)
108 ihold(inode);
109 ++ci->i_wrbuffer_ref;
110 dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
111 "snapc %p seq %lld (%d snaps)\n",
112 mapping->host, page, page->index,
113 ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
114 ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
115 snapc, snapc->seq, snapc->num_snaps);
116 spin_unlock(&ci->i_ceph_lock);
117
118 /*
119 * Reference snap context in page->private. Also set
120 * PagePrivate so that we get invalidatepage callback.
121 */
122 BUG_ON(PagePrivate(page));
123 page->private = (unsigned long)snapc;
124 SetPagePrivate(page);
125
126 return __set_page_dirty_nobuffers(page);
127 }
128
129 /*
130 * If we are truncating the full page (i.e. offset == 0), adjust the
131 * dirty page counters appropriately. Only called if there is private
132 * data on the page.
133 */
ceph_invalidatepage(struct page * page,unsigned int offset,unsigned int length)134 static void ceph_invalidatepage(struct page *page, unsigned int offset,
135 unsigned int length)
136 {
137 struct inode *inode;
138 struct ceph_inode_info *ci;
139 struct ceph_snap_context *snapc = page_snap_context(page);
140
141 inode = page->mapping->host;
142 ci = ceph_inode(inode);
143
144 if (offset != 0 || length != PAGE_SIZE) {
145 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
146 inode, page, page->index, offset, length);
147 return;
148 }
149
150 ceph_invalidate_fscache_page(inode, page);
151
152 WARN_ON(!PageLocked(page));
153 if (!PagePrivate(page))
154 return;
155
156 dout("%p invalidatepage %p idx %lu full dirty page\n",
157 inode, page, page->index);
158
159 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
160 ceph_put_snap_context(snapc);
161 page->private = 0;
162 ClearPagePrivate(page);
163 }
164
ceph_releasepage(struct page * page,gfp_t g)165 static int ceph_releasepage(struct page *page, gfp_t g)
166 {
167 dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host,
168 page, page->index, PageDirty(page) ? "" : "not ");
169
170 /* Can we release the page from the cache? */
171 if (!ceph_release_fscache_page(page, g))
172 return 0;
173
174 return !PagePrivate(page);
175 }
176
177 /* read a single page, without unlocking it. */
ceph_do_readpage(struct file * filp,struct page * page)178 static int ceph_do_readpage(struct file *filp, struct page *page)
179 {
180 struct inode *inode = file_inode(filp);
181 struct ceph_inode_info *ci = ceph_inode(inode);
182 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
183 struct ceph_osd_client *osdc = &fsc->client->osdc;
184 struct ceph_osd_request *req;
185 struct ceph_vino vino = ceph_vino(inode);
186 int err = 0;
187 u64 off = page_offset(page);
188 u64 len = PAGE_SIZE;
189
190 if (off >= i_size_read(inode)) {
191 zero_user_segment(page, 0, PAGE_SIZE);
192 SetPageUptodate(page);
193 return 0;
194 }
195
196 if (ci->i_inline_version != CEPH_INLINE_NONE) {
197 /*
198 * Uptodate inline data should have been added
199 * into page cache while getting Fcr caps.
200 */
201 if (off == 0)
202 return -EINVAL;
203 zero_user_segment(page, 0, PAGE_SIZE);
204 SetPageUptodate(page);
205 return 0;
206 }
207
208 err = ceph_readpage_from_fscache(inode, page);
209 if (err == 0)
210 return -EINPROGRESS;
211
212 dout("readpage ino %llx.%llx file %p off %llu len %llu page %p index %lu\n",
213 vino.ino, vino.snap, filp, off, len, page, page->index);
214 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len, 0, 1,
215 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ, NULL,
216 ci->i_truncate_seq, ci->i_truncate_size,
217 false);
218 if (IS_ERR(req))
219 return PTR_ERR(req);
220
221 osd_req_op_extent_osd_data_pages(req, 0, &page, len, 0, false, false);
222
223 err = ceph_osdc_start_request(osdc, req, false);
224 if (!err)
225 err = ceph_osdc_wait_request(osdc, req);
226
227 ceph_update_read_latency(&fsc->mdsc->metric, req->r_start_latency,
228 req->r_end_latency, err);
229
230 ceph_osdc_put_request(req);
231 dout("readpage result %d\n", err);
232
233 if (err == -ENOENT)
234 err = 0;
235 if (err < 0) {
236 ceph_fscache_readpage_cancel(inode, page);
237 if (err == -EBLOCKLISTED)
238 fsc->blocklisted = true;
239 goto out;
240 }
241 if (err < PAGE_SIZE)
242 /* zero fill remainder of page */
243 zero_user_segment(page, err, PAGE_SIZE);
244 else
245 flush_dcache_page(page);
246
247 SetPageUptodate(page);
248 ceph_readpage_to_fscache(inode, page);
249
250 out:
251 return err < 0 ? err : 0;
252 }
253
ceph_readpage(struct file * filp,struct page * page)254 static int ceph_readpage(struct file *filp, struct page *page)
255 {
256 int r = ceph_do_readpage(filp, page);
257 if (r != -EINPROGRESS)
258 unlock_page(page);
259 else
260 r = 0;
261 return r;
262 }
263
264 /*
265 * Finish an async read(ahead) op.
266 */
finish_read(struct ceph_osd_request * req)267 static void finish_read(struct ceph_osd_request *req)
268 {
269 struct inode *inode = req->r_inode;
270 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
271 struct ceph_osd_data *osd_data;
272 int rc = req->r_result <= 0 ? req->r_result : 0;
273 int bytes = req->r_result >= 0 ? req->r_result : 0;
274 int num_pages;
275 int i;
276
277 dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
278 if (rc == -EBLOCKLISTED)
279 ceph_inode_to_client(inode)->blocklisted = true;
280
281 /* unlock all pages, zeroing any data we didn't read */
282 osd_data = osd_req_op_extent_osd_data(req, 0);
283 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
284 num_pages = calc_pages_for((u64)osd_data->alignment,
285 (u64)osd_data->length);
286 for (i = 0; i < num_pages; i++) {
287 struct page *page = osd_data->pages[i];
288
289 if (rc < 0 && rc != -ENOENT) {
290 ceph_fscache_readpage_cancel(inode, page);
291 goto unlock;
292 }
293 if (bytes < (int)PAGE_SIZE) {
294 /* zero (remainder of) page */
295 int s = bytes < 0 ? 0 : bytes;
296 zero_user_segment(page, s, PAGE_SIZE);
297 }
298 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
299 page->index);
300 flush_dcache_page(page);
301 SetPageUptodate(page);
302 ceph_readpage_to_fscache(inode, page);
303 unlock:
304 unlock_page(page);
305 put_page(page);
306 bytes -= PAGE_SIZE;
307 }
308
309 ceph_update_read_latency(&fsc->mdsc->metric, req->r_start_latency,
310 req->r_end_latency, rc);
311
312 kfree(osd_data->pages);
313 }
314
315 /*
316 * start an async read(ahead) operation. return nr_pages we submitted
317 * a read for on success, or negative error code.
318 */
start_read(struct inode * inode,struct ceph_rw_context * rw_ctx,struct list_head * page_list,int max)319 static int start_read(struct inode *inode, struct ceph_rw_context *rw_ctx,
320 struct list_head *page_list, int max)
321 {
322 struct ceph_osd_client *osdc =
323 &ceph_inode_to_client(inode)->client->osdc;
324 struct ceph_inode_info *ci = ceph_inode(inode);
325 struct page *page = lru_to_page(page_list);
326 struct ceph_vino vino;
327 struct ceph_osd_request *req;
328 u64 off;
329 u64 len;
330 int i;
331 struct page **pages;
332 pgoff_t next_index;
333 int nr_pages = 0;
334 int got = 0;
335 int ret = 0;
336
337 if (!rw_ctx) {
338 /* caller of readpages does not hold buffer and read caps
339 * (fadvise, madvise and readahead cases) */
340 int want = CEPH_CAP_FILE_CACHE;
341 ret = ceph_try_get_caps(inode, CEPH_CAP_FILE_RD, want,
342 true, &got);
343 if (ret < 0) {
344 dout("start_read %p, error getting cap\n", inode);
345 } else if (!(got & want)) {
346 dout("start_read %p, no cache cap\n", inode);
347 ret = 0;
348 }
349 if (ret <= 0) {
350 if (got)
351 ceph_put_cap_refs(ci, got);
352 while (!list_empty(page_list)) {
353 page = lru_to_page(page_list);
354 list_del(&page->lru);
355 put_page(page);
356 }
357 return ret;
358 }
359 }
360
361 off = (u64) page_offset(page);
362
363 /* count pages */
364 next_index = page->index;
365 list_for_each_entry_reverse(page, page_list, lru) {
366 if (page->index != next_index)
367 break;
368 nr_pages++;
369 next_index++;
370 if (max && nr_pages == max)
371 break;
372 }
373 len = nr_pages << PAGE_SHIFT;
374 dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
375 off, len);
376 vino = ceph_vino(inode);
377 req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
378 0, 1, CEPH_OSD_OP_READ,
379 CEPH_OSD_FLAG_READ, NULL,
380 ci->i_truncate_seq, ci->i_truncate_size,
381 false);
382 if (IS_ERR(req)) {
383 ret = PTR_ERR(req);
384 goto out;
385 }
386
387 /* build page vector */
388 nr_pages = calc_pages_for(0, len);
389 pages = kmalloc_array(nr_pages, sizeof(*pages), GFP_KERNEL);
390 if (!pages) {
391 ret = -ENOMEM;
392 goto out_put;
393 }
394 for (i = 0; i < nr_pages; ++i) {
395 page = list_entry(page_list->prev, struct page, lru);
396 BUG_ON(PageLocked(page));
397 list_del(&page->lru);
398
399 dout("start_read %p adding %p idx %lu\n", inode, page,
400 page->index);
401 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
402 GFP_KERNEL)) {
403 ceph_fscache_uncache_page(inode, page);
404 put_page(page);
405 dout("start_read %p add_to_page_cache failed %p\n",
406 inode, page);
407 nr_pages = i;
408 if (nr_pages > 0) {
409 len = nr_pages << PAGE_SHIFT;
410 osd_req_op_extent_update(req, 0, len);
411 break;
412 }
413 goto out_pages;
414 }
415 pages[i] = page;
416 }
417 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
418 req->r_callback = finish_read;
419 req->r_inode = inode;
420
421 dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
422 ret = ceph_osdc_start_request(osdc, req, false);
423 if (ret < 0)
424 goto out_pages;
425 ceph_osdc_put_request(req);
426
427 /* After adding locked pages to page cache, the inode holds cache cap.
428 * So we can drop our cap refs. */
429 if (got)
430 ceph_put_cap_refs(ci, got);
431
432 return nr_pages;
433
434 out_pages:
435 for (i = 0; i < nr_pages; ++i) {
436 ceph_fscache_readpage_cancel(inode, pages[i]);
437 unlock_page(pages[i]);
438 }
439 ceph_put_page_vector(pages, nr_pages, false);
440 out_put:
441 ceph_osdc_put_request(req);
442 out:
443 if (got)
444 ceph_put_cap_refs(ci, got);
445 return ret;
446 }
447
448
449 /*
450 * Read multiple pages. Leave pages we don't read + unlock in page_list;
451 * the caller (VM) cleans them up.
452 */
ceph_readpages(struct file * file,struct address_space * mapping,struct list_head * page_list,unsigned nr_pages)453 static int ceph_readpages(struct file *file, struct address_space *mapping,
454 struct list_head *page_list, unsigned nr_pages)
455 {
456 struct inode *inode = file_inode(file);
457 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
458 struct ceph_file_info *fi = file->private_data;
459 struct ceph_rw_context *rw_ctx;
460 int rc = 0;
461 int max = 0;
462
463 if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
464 return -EINVAL;
465
466 rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
467 &nr_pages);
468
469 if (rc == 0)
470 goto out;
471
472 rw_ctx = ceph_find_rw_context(fi);
473 max = fsc->mount_options->rsize >> PAGE_SHIFT;
474 dout("readpages %p file %p ctx %p nr_pages %d max %d\n",
475 inode, file, rw_ctx, nr_pages, max);
476 while (!list_empty(page_list)) {
477 rc = start_read(inode, rw_ctx, page_list, max);
478 if (rc < 0)
479 goto out;
480 }
481 out:
482 ceph_fscache_readpages_cancel(inode, page_list);
483
484 dout("readpages %p file %p ret %d\n", inode, file, rc);
485 return rc;
486 }
487
488 struct ceph_writeback_ctl
489 {
490 loff_t i_size;
491 u64 truncate_size;
492 u32 truncate_seq;
493 bool size_stable;
494 bool head_snapc;
495 };
496
497 /*
498 * Get ref for the oldest snapc for an inode with dirty data... that is, the
499 * only snap context we are allowed to write back.
500 */
501 static struct ceph_snap_context *
get_oldest_context(struct inode * inode,struct ceph_writeback_ctl * ctl,struct ceph_snap_context * page_snapc)502 get_oldest_context(struct inode *inode, struct ceph_writeback_ctl *ctl,
503 struct ceph_snap_context *page_snapc)
504 {
505 struct ceph_inode_info *ci = ceph_inode(inode);
506 struct ceph_snap_context *snapc = NULL;
507 struct ceph_cap_snap *capsnap = NULL;
508
509 spin_lock(&ci->i_ceph_lock);
510 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
511 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
512 capsnap->context, capsnap->dirty_pages);
513 if (!capsnap->dirty_pages)
514 continue;
515
516 /* get i_size, truncate_{seq,size} for page_snapc? */
517 if (snapc && capsnap->context != page_snapc)
518 continue;
519
520 if (ctl) {
521 if (capsnap->writing) {
522 ctl->i_size = i_size_read(inode);
523 ctl->size_stable = false;
524 } else {
525 ctl->i_size = capsnap->size;
526 ctl->size_stable = true;
527 }
528 ctl->truncate_size = capsnap->truncate_size;
529 ctl->truncate_seq = capsnap->truncate_seq;
530 ctl->head_snapc = false;
531 }
532
533 if (snapc)
534 break;
535
536 snapc = ceph_get_snap_context(capsnap->context);
537 if (!page_snapc ||
538 page_snapc == snapc ||
539 page_snapc->seq > snapc->seq)
540 break;
541 }
542 if (!snapc && ci->i_wrbuffer_ref_head) {
543 snapc = ceph_get_snap_context(ci->i_head_snapc);
544 dout(" head snapc %p has %d dirty pages\n",
545 snapc, ci->i_wrbuffer_ref_head);
546 if (ctl) {
547 ctl->i_size = i_size_read(inode);
548 ctl->truncate_size = ci->i_truncate_size;
549 ctl->truncate_seq = ci->i_truncate_seq;
550 ctl->size_stable = false;
551 ctl->head_snapc = true;
552 }
553 }
554 spin_unlock(&ci->i_ceph_lock);
555 return snapc;
556 }
557
get_writepages_data_length(struct inode * inode,struct page * page,u64 start)558 static u64 get_writepages_data_length(struct inode *inode,
559 struct page *page, u64 start)
560 {
561 struct ceph_inode_info *ci = ceph_inode(inode);
562 struct ceph_snap_context *snapc = page_snap_context(page);
563 struct ceph_cap_snap *capsnap = NULL;
564 u64 end = i_size_read(inode);
565
566 if (snapc != ci->i_head_snapc) {
567 bool found = false;
568 spin_lock(&ci->i_ceph_lock);
569 list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
570 if (capsnap->context == snapc) {
571 if (!capsnap->writing)
572 end = capsnap->size;
573 found = true;
574 break;
575 }
576 }
577 spin_unlock(&ci->i_ceph_lock);
578 WARN_ON(!found);
579 }
580 if (end > page_offset(page) + PAGE_SIZE)
581 end = page_offset(page) + PAGE_SIZE;
582 return end > start ? end - start : 0;
583 }
584
585 /*
586 * Write a single page, but leave the page locked.
587 *
588 * If we get a write error, mark the mapping for error, but still adjust the
589 * dirty page accounting (i.e., page is no longer dirty).
590 */
writepage_nounlock(struct page * page,struct writeback_control * wbc)591 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
592 {
593 struct inode *inode = page->mapping->host;
594 struct ceph_inode_info *ci = ceph_inode(inode);
595 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
596 struct ceph_snap_context *snapc, *oldest;
597 loff_t page_off = page_offset(page);
598 int err;
599 loff_t len = PAGE_SIZE;
600 struct ceph_writeback_ctl ceph_wbc;
601 struct ceph_osd_client *osdc = &fsc->client->osdc;
602 struct ceph_osd_request *req;
603
604 dout("writepage %p idx %lu\n", page, page->index);
605
606 /* verify this is a writeable snap context */
607 snapc = page_snap_context(page);
608 if (!snapc) {
609 dout("writepage %p page %p not dirty?\n", inode, page);
610 return 0;
611 }
612 oldest = get_oldest_context(inode, &ceph_wbc, snapc);
613 if (snapc->seq > oldest->seq) {
614 dout("writepage %p page %p snapc %p not writeable - noop\n",
615 inode, page, snapc);
616 /* we should only noop if called by kswapd */
617 WARN_ON(!(current->flags & PF_MEMALLOC));
618 ceph_put_snap_context(oldest);
619 redirty_page_for_writepage(wbc, page);
620 return 0;
621 }
622 ceph_put_snap_context(oldest);
623
624 /* is this a partial page at end of file? */
625 if (page_off >= ceph_wbc.i_size) {
626 dout("%p page eof %llu\n", page, ceph_wbc.i_size);
627 page->mapping->a_ops->invalidatepage(page, 0, PAGE_SIZE);
628 return 0;
629 }
630
631 if (ceph_wbc.i_size < page_off + len)
632 len = ceph_wbc.i_size - page_off;
633
634 dout("writepage %p page %p index %lu on %llu~%llu snapc %p seq %lld\n",
635 inode, page, page->index, page_off, len, snapc, snapc->seq);
636
637 if (atomic_long_inc_return(&fsc->writeback_count) >
638 CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
639 set_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
640
641 set_page_writeback(page);
642 req = ceph_osdc_new_request(osdc, &ci->i_layout, ceph_vino(inode), page_off, &len, 0, 1,
643 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE, snapc,
644 ceph_wbc.truncate_seq, ceph_wbc.truncate_size,
645 true);
646 if (IS_ERR(req)) {
647 redirty_page_for_writepage(wbc, page);
648 end_page_writeback(page);
649 return PTR_ERR(req);
650 }
651
652 /* it may be a short write due to an object boundary */
653 WARN_ON_ONCE(len > PAGE_SIZE);
654 osd_req_op_extent_osd_data_pages(req, 0, &page, len, 0, false, false);
655 dout("writepage %llu~%llu (%llu bytes)\n", page_off, len, len);
656
657 req->r_mtime = inode->i_mtime;
658 err = ceph_osdc_start_request(osdc, req, true);
659 if (!err)
660 err = ceph_osdc_wait_request(osdc, req);
661
662 ceph_update_write_latency(&fsc->mdsc->metric, req->r_start_latency,
663 req->r_end_latency, err);
664
665 ceph_osdc_put_request(req);
666 if (err == 0)
667 err = len;
668
669 if (err < 0) {
670 struct writeback_control tmp_wbc;
671 if (!wbc)
672 wbc = &tmp_wbc;
673 if (err == -ERESTARTSYS) {
674 /* killed by SIGKILL */
675 dout("writepage interrupted page %p\n", page);
676 redirty_page_for_writepage(wbc, page);
677 end_page_writeback(page);
678 return err;
679 }
680 if (err == -EBLOCKLISTED)
681 fsc->blocklisted = true;
682 dout("writepage setting page/mapping error %d %p\n",
683 err, page);
684 mapping_set_error(&inode->i_data, err);
685 wbc->pages_skipped++;
686 } else {
687 dout("writepage cleaned page %p\n", page);
688 err = 0; /* vfs expects us to return 0 */
689 }
690 page->private = 0;
691 ClearPagePrivate(page);
692 end_page_writeback(page);
693 ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
694 ceph_put_snap_context(snapc); /* page's reference */
695
696 if (atomic_long_dec_return(&fsc->writeback_count) <
697 CONGESTION_OFF_THRESH(fsc->mount_options->congestion_kb))
698 clear_bdi_congested(inode_to_bdi(inode), BLK_RW_ASYNC);
699
700 return err;
701 }
702
ceph_writepage(struct page * page,struct writeback_control * wbc)703 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
704 {
705 int err;
706 struct inode *inode = page->mapping->host;
707 BUG_ON(!inode);
708 ihold(inode);
709 err = writepage_nounlock(page, wbc);
710 if (err == -ERESTARTSYS) {
711 /* direct memory reclaimer was killed by SIGKILL. return 0
712 * to prevent caller from setting mapping/page error */
713 err = 0;
714 }
715 unlock_page(page);
716 iput(inode);
717 return err;
718 }
719
720 /*
721 * async writeback completion handler.
722 *
723 * If we get an error, set the mapping error bit, but not the individual
724 * page error bits.
725 */
writepages_finish(struct ceph_osd_request * req)726 static void writepages_finish(struct ceph_osd_request *req)
727 {
728 struct inode *inode = req->r_inode;
729 struct ceph_inode_info *ci = ceph_inode(inode);
730 struct ceph_osd_data *osd_data;
731 struct page *page;
732 int num_pages, total_pages = 0;
733 int i, j;
734 int rc = req->r_result;
735 struct ceph_snap_context *snapc = req->r_snapc;
736 struct address_space *mapping = inode->i_mapping;
737 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
738 bool remove_page;
739
740 dout("writepages_finish %p rc %d\n", inode, rc);
741 if (rc < 0) {
742 mapping_set_error(mapping, rc);
743 ceph_set_error_write(ci);
744 if (rc == -EBLOCKLISTED)
745 fsc->blocklisted = true;
746 } else {
747 ceph_clear_error_write(ci);
748 }
749
750 ceph_update_write_latency(&fsc->mdsc->metric, req->r_start_latency,
751 req->r_end_latency, rc);
752
753 /*
754 * We lost the cache cap, need to truncate the page before
755 * it is unlocked, otherwise we'd truncate it later in the
756 * page truncation thread, possibly losing some data that
757 * raced its way in
758 */
759 remove_page = !(ceph_caps_issued(ci) &
760 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
761
762 /* clean all pages */
763 for (i = 0; i < req->r_num_ops; i++) {
764 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
765 break;
766
767 osd_data = osd_req_op_extent_osd_data(req, i);
768 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
769 num_pages = calc_pages_for((u64)osd_data->alignment,
770 (u64)osd_data->length);
771 total_pages += num_pages;
772 for (j = 0; j < num_pages; j++) {
773 page = osd_data->pages[j];
774 BUG_ON(!page);
775 WARN_ON(!PageUptodate(page));
776
777 if (atomic_long_dec_return(&fsc->writeback_count) <
778 CONGESTION_OFF_THRESH(
779 fsc->mount_options->congestion_kb))
780 clear_bdi_congested(inode_to_bdi(inode),
781 BLK_RW_ASYNC);
782
783 ceph_put_snap_context(page_snap_context(page));
784 page->private = 0;
785 ClearPagePrivate(page);
786 dout("unlocking %p\n", page);
787 end_page_writeback(page);
788
789 if (remove_page)
790 generic_error_remove_page(inode->i_mapping,
791 page);
792
793 unlock_page(page);
794 }
795 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
796 inode, osd_data->length, rc >= 0 ? num_pages : 0);
797
798 release_pages(osd_data->pages, num_pages);
799 }
800
801 ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
802
803 osd_data = osd_req_op_extent_osd_data(req, 0);
804 if (osd_data->pages_from_pool)
805 mempool_free(osd_data->pages, ceph_wb_pagevec_pool);
806 else
807 kfree(osd_data->pages);
808 ceph_osdc_put_request(req);
809 }
810
811 /*
812 * initiate async writeback
813 */
ceph_writepages_start(struct address_space * mapping,struct writeback_control * wbc)814 static int ceph_writepages_start(struct address_space *mapping,
815 struct writeback_control *wbc)
816 {
817 struct inode *inode = mapping->host;
818 struct ceph_inode_info *ci = ceph_inode(inode);
819 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
820 struct ceph_vino vino = ceph_vino(inode);
821 pgoff_t index, start_index, end = -1;
822 struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
823 struct pagevec pvec;
824 int rc = 0;
825 unsigned int wsize = i_blocksize(inode);
826 struct ceph_osd_request *req = NULL;
827 struct ceph_writeback_ctl ceph_wbc;
828 bool should_loop, range_whole = false;
829 bool done = false;
830
831 dout("writepages_start %p (mode=%s)\n", inode,
832 wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
833 (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
834
835 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
836 if (ci->i_wrbuffer_ref > 0) {
837 pr_warn_ratelimited(
838 "writepage_start %p %lld forced umount\n",
839 inode, ceph_ino(inode));
840 }
841 mapping_set_error(mapping, -EIO);
842 return -EIO; /* we're in a forced umount, don't write! */
843 }
844 if (fsc->mount_options->wsize < wsize)
845 wsize = fsc->mount_options->wsize;
846
847 pagevec_init(&pvec);
848
849 start_index = wbc->range_cyclic ? mapping->writeback_index : 0;
850 index = start_index;
851
852 retry:
853 /* find oldest snap context with dirty data */
854 snapc = get_oldest_context(inode, &ceph_wbc, NULL);
855 if (!snapc) {
856 /* hmm, why does writepages get called when there
857 is no dirty data? */
858 dout(" no snap context with dirty data?\n");
859 goto out;
860 }
861 dout(" oldest snapc is %p seq %lld (%d snaps)\n",
862 snapc, snapc->seq, snapc->num_snaps);
863
864 should_loop = false;
865 if (ceph_wbc.head_snapc && snapc != last_snapc) {
866 /* where to start/end? */
867 if (wbc->range_cyclic) {
868 index = start_index;
869 end = -1;
870 if (index > 0)
871 should_loop = true;
872 dout(" cyclic, start at %lu\n", index);
873 } else {
874 index = wbc->range_start >> PAGE_SHIFT;
875 end = wbc->range_end >> PAGE_SHIFT;
876 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
877 range_whole = true;
878 dout(" not cyclic, %lu to %lu\n", index, end);
879 }
880 } else if (!ceph_wbc.head_snapc) {
881 /* Do not respect wbc->range_{start,end}. Dirty pages
882 * in that range can be associated with newer snapc.
883 * They are not writeable until we write all dirty pages
884 * associated with 'snapc' get written */
885 if (index > 0)
886 should_loop = true;
887 dout(" non-head snapc, range whole\n");
888 }
889
890 ceph_put_snap_context(last_snapc);
891 last_snapc = snapc;
892
893 while (!done && index <= end) {
894 int num_ops = 0, op_idx;
895 unsigned i, pvec_pages, max_pages, locked_pages = 0;
896 struct page **pages = NULL, **data_pages;
897 struct page *page;
898 pgoff_t strip_unit_end = 0;
899 u64 offset = 0, len = 0;
900 bool from_pool = false;
901
902 max_pages = wsize >> PAGE_SHIFT;
903
904 get_more_pages:
905 pvec_pages = pagevec_lookup_range_tag(&pvec, mapping, &index,
906 end, PAGECACHE_TAG_DIRTY);
907 dout("pagevec_lookup_range_tag got %d\n", pvec_pages);
908 if (!pvec_pages && !locked_pages)
909 break;
910 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
911 page = pvec.pages[i];
912 dout("? %p idx %lu\n", page, page->index);
913 if (locked_pages == 0)
914 lock_page(page); /* first page */
915 else if (!trylock_page(page))
916 break;
917
918 /* only dirty pages, or our accounting breaks */
919 if (unlikely(!PageDirty(page)) ||
920 unlikely(page->mapping != mapping)) {
921 dout("!dirty or !mapping %p\n", page);
922 unlock_page(page);
923 continue;
924 }
925 /* only if matching snap context */
926 pgsnapc = page_snap_context(page);
927 if (pgsnapc != snapc) {
928 dout("page snapc %p %lld != oldest %p %lld\n",
929 pgsnapc, pgsnapc->seq, snapc, snapc->seq);
930 if (!should_loop &&
931 !ceph_wbc.head_snapc &&
932 wbc->sync_mode != WB_SYNC_NONE)
933 should_loop = true;
934 unlock_page(page);
935 continue;
936 }
937 if (page_offset(page) >= ceph_wbc.i_size) {
938 dout("%p page eof %llu\n",
939 page, ceph_wbc.i_size);
940 if ((ceph_wbc.size_stable ||
941 page_offset(page) >= i_size_read(inode)) &&
942 clear_page_dirty_for_io(page))
943 mapping->a_ops->invalidatepage(page,
944 0, PAGE_SIZE);
945 unlock_page(page);
946 continue;
947 }
948 if (strip_unit_end && (page->index > strip_unit_end)) {
949 dout("end of strip unit %p\n", page);
950 unlock_page(page);
951 break;
952 }
953 if (PageWriteback(page)) {
954 if (wbc->sync_mode == WB_SYNC_NONE) {
955 dout("%p under writeback\n", page);
956 unlock_page(page);
957 continue;
958 }
959 dout("waiting on writeback %p\n", page);
960 wait_on_page_writeback(page);
961 }
962
963 if (!clear_page_dirty_for_io(page)) {
964 dout("%p !clear_page_dirty_for_io\n", page);
965 unlock_page(page);
966 continue;
967 }
968
969 /*
970 * We have something to write. If this is
971 * the first locked page this time through,
972 * calculate max possinle write size and
973 * allocate a page array
974 */
975 if (locked_pages == 0) {
976 u64 objnum;
977 u64 objoff;
978 u32 xlen;
979
980 /* prepare async write request */
981 offset = (u64)page_offset(page);
982 ceph_calc_file_object_mapping(&ci->i_layout,
983 offset, wsize,
984 &objnum, &objoff,
985 &xlen);
986 len = xlen;
987
988 num_ops = 1;
989 strip_unit_end = page->index +
990 ((len - 1) >> PAGE_SHIFT);
991
992 BUG_ON(pages);
993 max_pages = calc_pages_for(0, (u64)len);
994 pages = kmalloc_array(max_pages,
995 sizeof(*pages),
996 GFP_NOFS);
997 if (!pages) {
998 from_pool = true;
999 pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
1000 BUG_ON(!pages);
1001 }
1002
1003 len = 0;
1004 } else if (page->index !=
1005 (offset + len) >> PAGE_SHIFT) {
1006 if (num_ops >= (from_pool ? CEPH_OSD_SLAB_OPS :
1007 CEPH_OSD_MAX_OPS)) {
1008 redirty_page_for_writepage(wbc, page);
1009 unlock_page(page);
1010 break;
1011 }
1012
1013 num_ops++;
1014 offset = (u64)page_offset(page);
1015 len = 0;
1016 }
1017
1018 /* note position of first page in pvec */
1019 dout("%p will write page %p idx %lu\n",
1020 inode, page, page->index);
1021
1022 if (atomic_long_inc_return(&fsc->writeback_count) >
1023 CONGESTION_ON_THRESH(
1024 fsc->mount_options->congestion_kb)) {
1025 set_bdi_congested(inode_to_bdi(inode),
1026 BLK_RW_ASYNC);
1027 }
1028
1029
1030 pages[locked_pages++] = page;
1031 pvec.pages[i] = NULL;
1032
1033 len += PAGE_SIZE;
1034 }
1035
1036 /* did we get anything? */
1037 if (!locked_pages)
1038 goto release_pvec_pages;
1039 if (i) {
1040 unsigned j, n = 0;
1041 /* shift unused page to beginning of pvec */
1042 for (j = 0; j < pvec_pages; j++) {
1043 if (!pvec.pages[j])
1044 continue;
1045 if (n < j)
1046 pvec.pages[n] = pvec.pages[j];
1047 n++;
1048 }
1049 pvec.nr = n;
1050
1051 if (pvec_pages && i == pvec_pages &&
1052 locked_pages < max_pages) {
1053 dout("reached end pvec, trying for more\n");
1054 pagevec_release(&pvec);
1055 goto get_more_pages;
1056 }
1057 }
1058
1059 new_request:
1060 offset = page_offset(pages[0]);
1061 len = wsize;
1062
1063 req = ceph_osdc_new_request(&fsc->client->osdc,
1064 &ci->i_layout, vino,
1065 offset, &len, 0, num_ops,
1066 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1067 snapc, ceph_wbc.truncate_seq,
1068 ceph_wbc.truncate_size, false);
1069 if (IS_ERR(req)) {
1070 req = ceph_osdc_new_request(&fsc->client->osdc,
1071 &ci->i_layout, vino,
1072 offset, &len, 0,
1073 min(num_ops,
1074 CEPH_OSD_SLAB_OPS),
1075 CEPH_OSD_OP_WRITE,
1076 CEPH_OSD_FLAG_WRITE,
1077 snapc, ceph_wbc.truncate_seq,
1078 ceph_wbc.truncate_size, true);
1079 BUG_ON(IS_ERR(req));
1080 }
1081 BUG_ON(len < page_offset(pages[locked_pages - 1]) +
1082 PAGE_SIZE - offset);
1083
1084 req->r_callback = writepages_finish;
1085 req->r_inode = inode;
1086
1087 /* Format the osd request message and submit the write */
1088 len = 0;
1089 data_pages = pages;
1090 op_idx = 0;
1091 for (i = 0; i < locked_pages; i++) {
1092 u64 cur_offset = page_offset(pages[i]);
1093 if (offset + len != cur_offset) {
1094 if (op_idx + 1 == req->r_num_ops)
1095 break;
1096 osd_req_op_extent_dup_last(req, op_idx,
1097 cur_offset - offset);
1098 dout("writepages got pages at %llu~%llu\n",
1099 offset, len);
1100 osd_req_op_extent_osd_data_pages(req, op_idx,
1101 data_pages, len, 0,
1102 from_pool, false);
1103 osd_req_op_extent_update(req, op_idx, len);
1104
1105 len = 0;
1106 offset = cur_offset;
1107 data_pages = pages + i;
1108 op_idx++;
1109 }
1110
1111 set_page_writeback(pages[i]);
1112 len += PAGE_SIZE;
1113 }
1114
1115 if (ceph_wbc.size_stable) {
1116 len = min(len, ceph_wbc.i_size - offset);
1117 } else if (i == locked_pages) {
1118 /* writepages_finish() clears writeback pages
1119 * according to the data length, so make sure
1120 * data length covers all locked pages */
1121 u64 min_len = len + 1 - PAGE_SIZE;
1122 len = get_writepages_data_length(inode, pages[i - 1],
1123 offset);
1124 len = max(len, min_len);
1125 }
1126 dout("writepages got pages at %llu~%llu\n", offset, len);
1127
1128 osd_req_op_extent_osd_data_pages(req, op_idx, data_pages, len,
1129 0, from_pool, false);
1130 osd_req_op_extent_update(req, op_idx, len);
1131
1132 BUG_ON(op_idx + 1 != req->r_num_ops);
1133
1134 from_pool = false;
1135 if (i < locked_pages) {
1136 BUG_ON(num_ops <= req->r_num_ops);
1137 num_ops -= req->r_num_ops;
1138 locked_pages -= i;
1139
1140 /* allocate new pages array for next request */
1141 data_pages = pages;
1142 pages = kmalloc_array(locked_pages, sizeof(*pages),
1143 GFP_NOFS);
1144 if (!pages) {
1145 from_pool = true;
1146 pages = mempool_alloc(ceph_wb_pagevec_pool, GFP_NOFS);
1147 BUG_ON(!pages);
1148 }
1149 memcpy(pages, data_pages + i,
1150 locked_pages * sizeof(*pages));
1151 memset(data_pages + i, 0,
1152 locked_pages * sizeof(*pages));
1153 } else {
1154 BUG_ON(num_ops != req->r_num_ops);
1155 index = pages[i - 1]->index + 1;
1156 /* request message now owns the pages array */
1157 pages = NULL;
1158 }
1159
1160 req->r_mtime = inode->i_mtime;
1161 rc = ceph_osdc_start_request(&fsc->client->osdc, req, true);
1162 BUG_ON(rc);
1163 req = NULL;
1164
1165 wbc->nr_to_write -= i;
1166 if (pages)
1167 goto new_request;
1168
1169 /*
1170 * We stop writing back only if we are not doing
1171 * integrity sync. In case of integrity sync we have to
1172 * keep going until we have written all the pages
1173 * we tagged for writeback prior to entering this loop.
1174 */
1175 if (wbc->nr_to_write <= 0 && wbc->sync_mode == WB_SYNC_NONE)
1176 done = true;
1177
1178 release_pvec_pages:
1179 dout("pagevec_release on %d pages (%p)\n", (int)pvec.nr,
1180 pvec.nr ? pvec.pages[0] : NULL);
1181 pagevec_release(&pvec);
1182 }
1183
1184 if (should_loop && !done) {
1185 /* more to do; loop back to beginning of file */
1186 dout("writepages looping back to beginning of file\n");
1187 end = start_index - 1; /* OK even when start_index == 0 */
1188
1189 /* to write dirty pages associated with next snapc,
1190 * we need to wait until current writes complete */
1191 if (wbc->sync_mode != WB_SYNC_NONE &&
1192 start_index == 0 && /* all dirty pages were checked */
1193 !ceph_wbc.head_snapc) {
1194 struct page *page;
1195 unsigned i, nr;
1196 index = 0;
1197 while ((index <= end) &&
1198 (nr = pagevec_lookup_tag(&pvec, mapping, &index,
1199 PAGECACHE_TAG_WRITEBACK))) {
1200 for (i = 0; i < nr; i++) {
1201 page = pvec.pages[i];
1202 if (page_snap_context(page) != snapc)
1203 continue;
1204 wait_on_page_writeback(page);
1205 }
1206 pagevec_release(&pvec);
1207 cond_resched();
1208 }
1209 }
1210
1211 start_index = 0;
1212 index = 0;
1213 goto retry;
1214 }
1215
1216 if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0))
1217 mapping->writeback_index = index;
1218
1219 out:
1220 ceph_osdc_put_request(req);
1221 ceph_put_snap_context(last_snapc);
1222 dout("writepages dend - startone, rc = %d\n", rc);
1223 return rc;
1224 }
1225
1226
1227
1228 /*
1229 * See if a given @snapc is either writeable, or already written.
1230 */
context_is_writeable_or_written(struct inode * inode,struct ceph_snap_context * snapc)1231 static int context_is_writeable_or_written(struct inode *inode,
1232 struct ceph_snap_context *snapc)
1233 {
1234 struct ceph_snap_context *oldest = get_oldest_context(inode, NULL, NULL);
1235 int ret = !oldest || snapc->seq <= oldest->seq;
1236
1237 ceph_put_snap_context(oldest);
1238 return ret;
1239 }
1240
1241 /**
1242 * ceph_find_incompatible - find an incompatible context and return it
1243 * @page: page being dirtied
1244 *
1245 * We are only allowed to write into/dirty a page if the page is
1246 * clean, or already dirty within the same snap context. Returns a
1247 * conflicting context if there is one, NULL if there isn't, or a
1248 * negative error code on other errors.
1249 *
1250 * Must be called with page lock held.
1251 */
1252 static struct ceph_snap_context *
ceph_find_incompatible(struct page * page)1253 ceph_find_incompatible(struct page *page)
1254 {
1255 struct inode *inode = page->mapping->host;
1256 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1257 struct ceph_inode_info *ci = ceph_inode(inode);
1258
1259 if (READ_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
1260 dout(" page %p forced umount\n", page);
1261 return ERR_PTR(-EIO);
1262 }
1263
1264 for (;;) {
1265 struct ceph_snap_context *snapc, *oldest;
1266
1267 wait_on_page_writeback(page);
1268
1269 snapc = page_snap_context(page);
1270 if (!snapc || snapc == ci->i_head_snapc)
1271 break;
1272
1273 /*
1274 * this page is already dirty in another (older) snap
1275 * context! is it writeable now?
1276 */
1277 oldest = get_oldest_context(inode, NULL, NULL);
1278 if (snapc->seq > oldest->seq) {
1279 /* not writeable -- return it for the caller to deal with */
1280 ceph_put_snap_context(oldest);
1281 dout(" page %p snapc %p not current or oldest\n", page, snapc);
1282 return ceph_get_snap_context(snapc);
1283 }
1284 ceph_put_snap_context(oldest);
1285
1286 /* yay, writeable, do it now (without dropping page lock) */
1287 dout(" page %p snapc %p not current, but oldest\n", page, snapc);
1288 if (clear_page_dirty_for_io(page)) {
1289 int r = writepage_nounlock(page, NULL);
1290 if (r < 0)
1291 return ERR_PTR(r);
1292 }
1293 }
1294 return NULL;
1295 }
1296
1297 /**
1298 * prep_noread_page - prep a page for writing without reading first
1299 * @page: page being prepared
1300 * @pos: starting position for the write
1301 * @len: length of write
1302 *
1303 * In some cases, write_begin doesn't need to read at all:
1304 * - full page write
1305 * - file is currently zero-length
1306 * - write that lies in a page that is completely beyond EOF
1307 * - write that covers the the page from start to EOF or beyond it
1308 *
1309 * If any of these criteria are met, then zero out the unwritten parts
1310 * of the page and return true. Otherwise, return false.
1311 */
skip_page_read(struct page * page,loff_t pos,size_t len)1312 static bool skip_page_read(struct page *page, loff_t pos, size_t len)
1313 {
1314 struct inode *inode = page->mapping->host;
1315 loff_t i_size = i_size_read(inode);
1316 size_t offset = offset_in_page(pos);
1317
1318 /* Full page write */
1319 if (offset == 0 && len >= PAGE_SIZE)
1320 return true;
1321
1322 /* pos beyond last page in the file */
1323 if (pos - offset >= i_size)
1324 goto zero_out;
1325
1326 /* write that covers the whole page from start to EOF or beyond it */
1327 if (offset == 0 && (pos + len) >= i_size)
1328 goto zero_out;
1329
1330 return false;
1331 zero_out:
1332 zero_user_segments(page, 0, offset, offset + len, PAGE_SIZE);
1333 return true;
1334 }
1335
1336 /*
1337 * We are only allowed to write into/dirty the page if the page is
1338 * clean, or already dirty within the same snap context.
1339 */
ceph_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned flags,struct page ** pagep,void ** fsdata)1340 static int ceph_write_begin(struct file *file, struct address_space *mapping,
1341 loff_t pos, unsigned len, unsigned flags,
1342 struct page **pagep, void **fsdata)
1343 {
1344 struct inode *inode = file_inode(file);
1345 struct ceph_inode_info *ci = ceph_inode(inode);
1346 struct ceph_snap_context *snapc;
1347 struct page *page = NULL;
1348 pgoff_t index = pos >> PAGE_SHIFT;
1349 int r = 0;
1350
1351 dout("write_begin file %p inode %p page %p %d~%d\n", file, inode, page, (int)pos, (int)len);
1352
1353 for (;;) {
1354 page = grab_cache_page_write_begin(mapping, index, 0);
1355 if (!page) {
1356 r = -ENOMEM;
1357 break;
1358 }
1359
1360 snapc = ceph_find_incompatible(page);
1361 if (snapc) {
1362 if (IS_ERR(snapc)) {
1363 r = PTR_ERR(snapc);
1364 break;
1365 }
1366 unlock_page(page);
1367 put_page(page);
1368 page = NULL;
1369 ceph_queue_writeback(inode);
1370 r = wait_event_killable(ci->i_cap_wq,
1371 context_is_writeable_or_written(inode, snapc));
1372 ceph_put_snap_context(snapc);
1373 if (r != 0)
1374 break;
1375 continue;
1376 }
1377
1378 if (PageUptodate(page)) {
1379 dout(" page %p already uptodate\n", page);
1380 break;
1381 }
1382
1383 /* No need to read in some cases */
1384 if (skip_page_read(page, pos, len))
1385 break;
1386
1387 /*
1388 * We need to read it. If we get back -EINPROGRESS, then the page was
1389 * handed off to fscache and it will be unlocked when the read completes.
1390 * Refind the page in that case so we can reacquire the page lock. Otherwise
1391 * we got a hard error or the read was completed synchronously.
1392 */
1393 r = ceph_do_readpage(file, page);
1394 if (r != -EINPROGRESS)
1395 break;
1396 }
1397
1398 if (r < 0) {
1399 if (page) {
1400 unlock_page(page);
1401 put_page(page);
1402 }
1403 } else {
1404 *pagep = page;
1405 }
1406 return r;
1407 }
1408
1409 /*
1410 * we don't do anything in here that simple_write_end doesn't do
1411 * except adjust dirty page accounting
1412 */
ceph_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)1413 static int ceph_write_end(struct file *file, struct address_space *mapping,
1414 loff_t pos, unsigned len, unsigned copied,
1415 struct page *page, void *fsdata)
1416 {
1417 struct inode *inode = file_inode(file);
1418 bool check_cap = false;
1419
1420 dout("write_end file %p inode %p page %p %d~%d (%d)\n", file,
1421 inode, page, (int)pos, (int)copied, (int)len);
1422
1423 /* zero the stale part of the page if we did a short copy */
1424 if (!PageUptodate(page)) {
1425 if (copied < len) {
1426 copied = 0;
1427 goto out;
1428 }
1429 SetPageUptodate(page);
1430 }
1431
1432 /* did file size increase? */
1433 if (pos+copied > i_size_read(inode))
1434 check_cap = ceph_inode_set_size(inode, pos+copied);
1435
1436 set_page_dirty(page);
1437
1438 out:
1439 unlock_page(page);
1440 put_page(page);
1441
1442 if (check_cap)
1443 ceph_check_caps(ceph_inode(inode), CHECK_CAPS_AUTHONLY, NULL);
1444
1445 return copied;
1446 }
1447
1448 /*
1449 * we set .direct_IO to indicate direct io is supported, but since we
1450 * intercept O_DIRECT reads and writes early, this function should
1451 * never get called.
1452 */
ceph_direct_io(struct kiocb * iocb,struct iov_iter * iter)1453 static ssize_t ceph_direct_io(struct kiocb *iocb, struct iov_iter *iter)
1454 {
1455 WARN_ON(1);
1456 return -EINVAL;
1457 }
1458
1459 const struct address_space_operations ceph_aops = {
1460 .readpage = ceph_readpage,
1461 .readpages = ceph_readpages,
1462 .writepage = ceph_writepage,
1463 .writepages = ceph_writepages_start,
1464 .write_begin = ceph_write_begin,
1465 .write_end = ceph_write_end,
1466 .set_page_dirty = ceph_set_page_dirty,
1467 .invalidatepage = ceph_invalidatepage,
1468 .releasepage = ceph_releasepage,
1469 .direct_IO = ceph_direct_io,
1470 };
1471
ceph_block_sigs(sigset_t * oldset)1472 static void ceph_block_sigs(sigset_t *oldset)
1473 {
1474 sigset_t mask;
1475 siginitsetinv(&mask, sigmask(SIGKILL));
1476 sigprocmask(SIG_BLOCK, &mask, oldset);
1477 }
1478
ceph_restore_sigs(sigset_t * oldset)1479 static void ceph_restore_sigs(sigset_t *oldset)
1480 {
1481 sigprocmask(SIG_SETMASK, oldset, NULL);
1482 }
1483
1484 /*
1485 * vm ops
1486 */
ceph_filemap_fault(struct vm_fault * vmf)1487 static vm_fault_t ceph_filemap_fault(struct vm_fault *vmf)
1488 {
1489 struct vm_area_struct *vma = vmf->vma;
1490 struct inode *inode = file_inode(vma->vm_file);
1491 struct ceph_inode_info *ci = ceph_inode(inode);
1492 struct ceph_file_info *fi = vma->vm_file->private_data;
1493 struct page *pinned_page = NULL;
1494 loff_t off = (loff_t)vmf->pgoff << PAGE_SHIFT;
1495 int want, got, err;
1496 sigset_t oldset;
1497 vm_fault_t ret = VM_FAULT_SIGBUS;
1498
1499 ceph_block_sigs(&oldset);
1500
1501 dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1502 inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
1503 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1504 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1505 else
1506 want = CEPH_CAP_FILE_CACHE;
1507
1508 got = 0;
1509 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_RD, want, -1,
1510 &got, &pinned_page);
1511 if (err < 0)
1512 goto out_restore;
1513
1514 dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1515 inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
1516
1517 if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1518 ci->i_inline_version == CEPH_INLINE_NONE) {
1519 CEPH_DEFINE_RW_CONTEXT(rw_ctx, got);
1520 ceph_add_rw_context(fi, &rw_ctx);
1521 ret = filemap_fault(vmf);
1522 ceph_del_rw_context(fi, &rw_ctx);
1523 dout("filemap_fault %p %llu~%zd drop cap refs %s ret %x\n",
1524 inode, off, (size_t)PAGE_SIZE,
1525 ceph_cap_string(got), ret);
1526 } else
1527 err = -EAGAIN;
1528
1529 if (pinned_page)
1530 put_page(pinned_page);
1531 ceph_put_cap_refs(ci, got);
1532
1533 if (err != -EAGAIN)
1534 goto out_restore;
1535
1536 /* read inline data */
1537 if (off >= PAGE_SIZE) {
1538 /* does not support inline data > PAGE_SIZE */
1539 ret = VM_FAULT_SIGBUS;
1540 } else {
1541 struct address_space *mapping = inode->i_mapping;
1542 struct page *page = find_or_create_page(mapping, 0,
1543 mapping_gfp_constraint(mapping,
1544 ~__GFP_FS));
1545 if (!page) {
1546 ret = VM_FAULT_OOM;
1547 goto out_inline;
1548 }
1549 err = __ceph_do_getattr(inode, page,
1550 CEPH_STAT_CAP_INLINE_DATA, true);
1551 if (err < 0 || off >= i_size_read(inode)) {
1552 unlock_page(page);
1553 put_page(page);
1554 ret = vmf_error(err);
1555 goto out_inline;
1556 }
1557 if (err < PAGE_SIZE)
1558 zero_user_segment(page, err, PAGE_SIZE);
1559 else
1560 flush_dcache_page(page);
1561 SetPageUptodate(page);
1562 vmf->page = page;
1563 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1564 out_inline:
1565 dout("filemap_fault %p %llu~%zd read inline data ret %x\n",
1566 inode, off, (size_t)PAGE_SIZE, ret);
1567 }
1568 out_restore:
1569 ceph_restore_sigs(&oldset);
1570 if (err < 0)
1571 ret = vmf_error(err);
1572
1573 return ret;
1574 }
1575
1576 /*
1577 * Reuse write_begin here for simplicity.
1578 */
ceph_page_mkwrite(struct vm_fault * vmf)1579 static vm_fault_t ceph_page_mkwrite(struct vm_fault *vmf)
1580 {
1581 struct vm_area_struct *vma = vmf->vma;
1582 struct inode *inode = file_inode(vma->vm_file);
1583 struct ceph_inode_info *ci = ceph_inode(inode);
1584 struct ceph_file_info *fi = vma->vm_file->private_data;
1585 struct ceph_cap_flush *prealloc_cf;
1586 struct page *page = vmf->page;
1587 loff_t off = page_offset(page);
1588 loff_t size = i_size_read(inode);
1589 size_t len;
1590 int want, got, err;
1591 sigset_t oldset;
1592 vm_fault_t ret = VM_FAULT_SIGBUS;
1593
1594 prealloc_cf = ceph_alloc_cap_flush();
1595 if (!prealloc_cf)
1596 return VM_FAULT_OOM;
1597
1598 sb_start_pagefault(inode->i_sb);
1599 ceph_block_sigs(&oldset);
1600
1601 if (ci->i_inline_version != CEPH_INLINE_NONE) {
1602 struct page *locked_page = NULL;
1603 if (off == 0) {
1604 lock_page(page);
1605 locked_page = page;
1606 }
1607 err = ceph_uninline_data(vma->vm_file, locked_page);
1608 if (locked_page)
1609 unlock_page(locked_page);
1610 if (err < 0)
1611 goto out_free;
1612 }
1613
1614 if (off + PAGE_SIZE <= size)
1615 len = PAGE_SIZE;
1616 else
1617 len = size & ~PAGE_MASK;
1618
1619 dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1620 inode, ceph_vinop(inode), off, len, size);
1621 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1622 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1623 else
1624 want = CEPH_CAP_FILE_BUFFER;
1625
1626 got = 0;
1627 err = ceph_get_caps(vma->vm_file, CEPH_CAP_FILE_WR, want, off + len,
1628 &got, NULL);
1629 if (err < 0)
1630 goto out_free;
1631
1632 dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1633 inode, off, len, ceph_cap_string(got));
1634
1635 /* Update time before taking page lock */
1636 file_update_time(vma->vm_file);
1637 inode_inc_iversion_raw(inode);
1638
1639 do {
1640 struct ceph_snap_context *snapc;
1641
1642 lock_page(page);
1643
1644 if (page_mkwrite_check_truncate(page, inode) < 0) {
1645 unlock_page(page);
1646 ret = VM_FAULT_NOPAGE;
1647 break;
1648 }
1649
1650 snapc = ceph_find_incompatible(page);
1651 if (!snapc) {
1652 /* success. we'll keep the page locked. */
1653 set_page_dirty(page);
1654 ret = VM_FAULT_LOCKED;
1655 break;
1656 }
1657
1658 unlock_page(page);
1659
1660 if (IS_ERR(snapc)) {
1661 ret = VM_FAULT_SIGBUS;
1662 break;
1663 }
1664
1665 ceph_queue_writeback(inode);
1666 err = wait_event_killable(ci->i_cap_wq,
1667 context_is_writeable_or_written(inode, snapc));
1668 ceph_put_snap_context(snapc);
1669 } while (err == 0);
1670
1671 if (ret == VM_FAULT_LOCKED ||
1672 ci->i_inline_version != CEPH_INLINE_NONE) {
1673 int dirty;
1674 spin_lock(&ci->i_ceph_lock);
1675 ci->i_inline_version = CEPH_INLINE_NONE;
1676 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1677 &prealloc_cf);
1678 spin_unlock(&ci->i_ceph_lock);
1679 if (dirty)
1680 __mark_inode_dirty(inode, dirty);
1681 }
1682
1683 dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %x\n",
1684 inode, off, len, ceph_cap_string(got), ret);
1685 ceph_put_cap_refs(ci, got);
1686 out_free:
1687 ceph_restore_sigs(&oldset);
1688 sb_end_pagefault(inode->i_sb);
1689 ceph_free_cap_flush(prealloc_cf);
1690 if (err < 0)
1691 ret = vmf_error(err);
1692 return ret;
1693 }
1694
ceph_fill_inline_data(struct inode * inode,struct page * locked_page,char * data,size_t len)1695 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1696 char *data, size_t len)
1697 {
1698 struct address_space *mapping = inode->i_mapping;
1699 struct page *page;
1700
1701 if (locked_page) {
1702 page = locked_page;
1703 } else {
1704 if (i_size_read(inode) == 0)
1705 return;
1706 page = find_or_create_page(mapping, 0,
1707 mapping_gfp_constraint(mapping,
1708 ~__GFP_FS));
1709 if (!page)
1710 return;
1711 if (PageUptodate(page)) {
1712 unlock_page(page);
1713 put_page(page);
1714 return;
1715 }
1716 }
1717
1718 dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1719 inode, ceph_vinop(inode), len, locked_page);
1720
1721 if (len > 0) {
1722 void *kaddr = kmap_atomic(page);
1723 memcpy(kaddr, data, len);
1724 kunmap_atomic(kaddr);
1725 }
1726
1727 if (page != locked_page) {
1728 if (len < PAGE_SIZE)
1729 zero_user_segment(page, len, PAGE_SIZE);
1730 else
1731 flush_dcache_page(page);
1732
1733 SetPageUptodate(page);
1734 unlock_page(page);
1735 put_page(page);
1736 }
1737 }
1738
ceph_uninline_data(struct file * filp,struct page * locked_page)1739 int ceph_uninline_data(struct file *filp, struct page *locked_page)
1740 {
1741 struct inode *inode = file_inode(filp);
1742 struct ceph_inode_info *ci = ceph_inode(inode);
1743 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1744 struct ceph_osd_request *req;
1745 struct page *page = NULL;
1746 u64 len, inline_version;
1747 int err = 0;
1748 bool from_pagecache = false;
1749
1750 spin_lock(&ci->i_ceph_lock);
1751 inline_version = ci->i_inline_version;
1752 spin_unlock(&ci->i_ceph_lock);
1753
1754 dout("uninline_data %p %llx.%llx inline_version %llu\n",
1755 inode, ceph_vinop(inode), inline_version);
1756
1757 if (inline_version == 1 || /* initial version, no data */
1758 inline_version == CEPH_INLINE_NONE)
1759 goto out;
1760
1761 if (locked_page) {
1762 page = locked_page;
1763 WARN_ON(!PageUptodate(page));
1764 } else if (ceph_caps_issued(ci) &
1765 (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1766 page = find_get_page(inode->i_mapping, 0);
1767 if (page) {
1768 if (PageUptodate(page)) {
1769 from_pagecache = true;
1770 lock_page(page);
1771 } else {
1772 put_page(page);
1773 page = NULL;
1774 }
1775 }
1776 }
1777
1778 if (page) {
1779 len = i_size_read(inode);
1780 if (len > PAGE_SIZE)
1781 len = PAGE_SIZE;
1782 } else {
1783 page = __page_cache_alloc(GFP_NOFS);
1784 if (!page) {
1785 err = -ENOMEM;
1786 goto out;
1787 }
1788 err = __ceph_do_getattr(inode, page,
1789 CEPH_STAT_CAP_INLINE_DATA, true);
1790 if (err < 0) {
1791 /* no inline data */
1792 if (err == -ENODATA)
1793 err = 0;
1794 goto out;
1795 }
1796 len = err;
1797 }
1798
1799 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1800 ceph_vino(inode), 0, &len, 0, 1,
1801 CEPH_OSD_OP_CREATE, CEPH_OSD_FLAG_WRITE,
1802 NULL, 0, 0, false);
1803 if (IS_ERR(req)) {
1804 err = PTR_ERR(req);
1805 goto out;
1806 }
1807
1808 req->r_mtime = inode->i_mtime;
1809 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1810 if (!err)
1811 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1812 ceph_osdc_put_request(req);
1813 if (err < 0)
1814 goto out;
1815
1816 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1817 ceph_vino(inode), 0, &len, 1, 3,
1818 CEPH_OSD_OP_WRITE, CEPH_OSD_FLAG_WRITE,
1819 NULL, ci->i_truncate_seq,
1820 ci->i_truncate_size, false);
1821 if (IS_ERR(req)) {
1822 err = PTR_ERR(req);
1823 goto out;
1824 }
1825
1826 osd_req_op_extent_osd_data_pages(req, 1, &page, len, 0, false, false);
1827
1828 {
1829 __le64 xattr_buf = cpu_to_le64(inline_version);
1830 err = osd_req_op_xattr_init(req, 0, CEPH_OSD_OP_CMPXATTR,
1831 "inline_version", &xattr_buf,
1832 sizeof(xattr_buf),
1833 CEPH_OSD_CMPXATTR_OP_GT,
1834 CEPH_OSD_CMPXATTR_MODE_U64);
1835 if (err)
1836 goto out_put;
1837 }
1838
1839 {
1840 char xattr_buf[32];
1841 int xattr_len = snprintf(xattr_buf, sizeof(xattr_buf),
1842 "%llu", inline_version);
1843 err = osd_req_op_xattr_init(req, 2, CEPH_OSD_OP_SETXATTR,
1844 "inline_version",
1845 xattr_buf, xattr_len, 0, 0);
1846 if (err)
1847 goto out_put;
1848 }
1849
1850 req->r_mtime = inode->i_mtime;
1851 err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1852 if (!err)
1853 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1854
1855 ceph_update_write_latency(&fsc->mdsc->metric, req->r_start_latency,
1856 req->r_end_latency, err);
1857
1858 out_put:
1859 ceph_osdc_put_request(req);
1860 if (err == -ECANCELED)
1861 err = 0;
1862 out:
1863 if (page && page != locked_page) {
1864 if (from_pagecache) {
1865 unlock_page(page);
1866 put_page(page);
1867 } else
1868 __free_pages(page, 0);
1869 }
1870
1871 dout("uninline_data %p %llx.%llx inline_version %llu = %d\n",
1872 inode, ceph_vinop(inode), inline_version, err);
1873 return err;
1874 }
1875
1876 static const struct vm_operations_struct ceph_vmops = {
1877 .fault = ceph_filemap_fault,
1878 .page_mkwrite = ceph_page_mkwrite,
1879 };
1880
ceph_mmap(struct file * file,struct vm_area_struct * vma)1881 int ceph_mmap(struct file *file, struct vm_area_struct *vma)
1882 {
1883 struct address_space *mapping = file->f_mapping;
1884
1885 if (!mapping->a_ops->readpage)
1886 return -ENOEXEC;
1887 file_accessed(file);
1888 vma->vm_ops = &ceph_vmops;
1889 return 0;
1890 }
1891
1892 enum {
1893 POOL_READ = 1,
1894 POOL_WRITE = 2,
1895 };
1896
__ceph_pool_perm_get(struct ceph_inode_info * ci,s64 pool,struct ceph_string * pool_ns)1897 static int __ceph_pool_perm_get(struct ceph_inode_info *ci,
1898 s64 pool, struct ceph_string *pool_ns)
1899 {
1900 struct ceph_fs_client *fsc = ceph_inode_to_client(&ci->vfs_inode);
1901 struct ceph_mds_client *mdsc = fsc->mdsc;
1902 struct ceph_osd_request *rd_req = NULL, *wr_req = NULL;
1903 struct rb_node **p, *parent;
1904 struct ceph_pool_perm *perm;
1905 struct page **pages;
1906 size_t pool_ns_len;
1907 int err = 0, err2 = 0, have = 0;
1908
1909 down_read(&mdsc->pool_perm_rwsem);
1910 p = &mdsc->pool_perm_tree.rb_node;
1911 while (*p) {
1912 perm = rb_entry(*p, struct ceph_pool_perm, node);
1913 if (pool < perm->pool)
1914 p = &(*p)->rb_left;
1915 else if (pool > perm->pool)
1916 p = &(*p)->rb_right;
1917 else {
1918 int ret = ceph_compare_string(pool_ns,
1919 perm->pool_ns,
1920 perm->pool_ns_len);
1921 if (ret < 0)
1922 p = &(*p)->rb_left;
1923 else if (ret > 0)
1924 p = &(*p)->rb_right;
1925 else {
1926 have = perm->perm;
1927 break;
1928 }
1929 }
1930 }
1931 up_read(&mdsc->pool_perm_rwsem);
1932 if (*p)
1933 goto out;
1934
1935 if (pool_ns)
1936 dout("__ceph_pool_perm_get pool %lld ns %.*s no perm cached\n",
1937 pool, (int)pool_ns->len, pool_ns->str);
1938 else
1939 dout("__ceph_pool_perm_get pool %lld no perm cached\n", pool);
1940
1941 down_write(&mdsc->pool_perm_rwsem);
1942 p = &mdsc->pool_perm_tree.rb_node;
1943 parent = NULL;
1944 while (*p) {
1945 parent = *p;
1946 perm = rb_entry(parent, struct ceph_pool_perm, node);
1947 if (pool < perm->pool)
1948 p = &(*p)->rb_left;
1949 else if (pool > perm->pool)
1950 p = &(*p)->rb_right;
1951 else {
1952 int ret = ceph_compare_string(pool_ns,
1953 perm->pool_ns,
1954 perm->pool_ns_len);
1955 if (ret < 0)
1956 p = &(*p)->rb_left;
1957 else if (ret > 0)
1958 p = &(*p)->rb_right;
1959 else {
1960 have = perm->perm;
1961 break;
1962 }
1963 }
1964 }
1965 if (*p) {
1966 up_write(&mdsc->pool_perm_rwsem);
1967 goto out;
1968 }
1969
1970 rd_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1971 1, false, GFP_NOFS);
1972 if (!rd_req) {
1973 err = -ENOMEM;
1974 goto out_unlock;
1975 }
1976
1977 rd_req->r_flags = CEPH_OSD_FLAG_READ;
1978 osd_req_op_init(rd_req, 0, CEPH_OSD_OP_STAT, 0);
1979 rd_req->r_base_oloc.pool = pool;
1980 if (pool_ns)
1981 rd_req->r_base_oloc.pool_ns = ceph_get_string(pool_ns);
1982 ceph_oid_printf(&rd_req->r_base_oid, "%llx.00000000", ci->i_vino.ino);
1983
1984 err = ceph_osdc_alloc_messages(rd_req, GFP_NOFS);
1985 if (err)
1986 goto out_unlock;
1987
1988 wr_req = ceph_osdc_alloc_request(&fsc->client->osdc, NULL,
1989 1, false, GFP_NOFS);
1990 if (!wr_req) {
1991 err = -ENOMEM;
1992 goto out_unlock;
1993 }
1994
1995 wr_req->r_flags = CEPH_OSD_FLAG_WRITE;
1996 osd_req_op_init(wr_req, 0, CEPH_OSD_OP_CREATE, CEPH_OSD_OP_FLAG_EXCL);
1997 ceph_oloc_copy(&wr_req->r_base_oloc, &rd_req->r_base_oloc);
1998 ceph_oid_copy(&wr_req->r_base_oid, &rd_req->r_base_oid);
1999
2000 err = ceph_osdc_alloc_messages(wr_req, GFP_NOFS);
2001 if (err)
2002 goto out_unlock;
2003
2004 /* one page should be large enough for STAT data */
2005 pages = ceph_alloc_page_vector(1, GFP_KERNEL);
2006 if (IS_ERR(pages)) {
2007 err = PTR_ERR(pages);
2008 goto out_unlock;
2009 }
2010
2011 osd_req_op_raw_data_in_pages(rd_req, 0, pages, PAGE_SIZE,
2012 0, false, true);
2013 err = ceph_osdc_start_request(&fsc->client->osdc, rd_req, false);
2014
2015 wr_req->r_mtime = ci->vfs_inode.i_mtime;
2016 err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
2017
2018 if (!err)
2019 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
2020 if (!err2)
2021 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
2022
2023 if (err >= 0 || err == -ENOENT)
2024 have |= POOL_READ;
2025 else if (err != -EPERM) {
2026 if (err == -EBLOCKLISTED)
2027 fsc->blocklisted = true;
2028 goto out_unlock;
2029 }
2030
2031 if (err2 == 0 || err2 == -EEXIST)
2032 have |= POOL_WRITE;
2033 else if (err2 != -EPERM) {
2034 if (err2 == -EBLOCKLISTED)
2035 fsc->blocklisted = true;
2036 err = err2;
2037 goto out_unlock;
2038 }
2039
2040 pool_ns_len = pool_ns ? pool_ns->len : 0;
2041 perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
2042 if (!perm) {
2043 err = -ENOMEM;
2044 goto out_unlock;
2045 }
2046
2047 perm->pool = pool;
2048 perm->perm = have;
2049 perm->pool_ns_len = pool_ns_len;
2050 if (pool_ns_len > 0)
2051 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
2052 perm->pool_ns[pool_ns_len] = 0;
2053
2054 rb_link_node(&perm->node, parent, p);
2055 rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
2056 err = 0;
2057 out_unlock:
2058 up_write(&mdsc->pool_perm_rwsem);
2059
2060 ceph_osdc_put_request(rd_req);
2061 ceph_osdc_put_request(wr_req);
2062 out:
2063 if (!err)
2064 err = have;
2065 if (pool_ns)
2066 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
2067 pool, (int)pool_ns->len, pool_ns->str, err);
2068 else
2069 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
2070 return err;
2071 }
2072
ceph_pool_perm_check(struct inode * inode,int need)2073 int ceph_pool_perm_check(struct inode *inode, int need)
2074 {
2075 struct ceph_inode_info *ci = ceph_inode(inode);
2076 struct ceph_string *pool_ns;
2077 s64 pool;
2078 int ret, flags;
2079
2080 if (ci->i_vino.snap != CEPH_NOSNAP) {
2081 /*
2082 * Pool permission check needs to write to the first object.
2083 * But for snapshot, head of the first object may have alread
2084 * been deleted. Skip check to avoid creating orphan object.
2085 */
2086 return 0;
2087 }
2088
2089 if (ceph_test_mount_opt(ceph_inode_to_client(inode),
2090 NOPOOLPERM))
2091 return 0;
2092
2093 spin_lock(&ci->i_ceph_lock);
2094 flags = ci->i_ceph_flags;
2095 pool = ci->i_layout.pool_id;
2096 spin_unlock(&ci->i_ceph_lock);
2097 check:
2098 if (flags & CEPH_I_POOL_PERM) {
2099 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
2100 dout("ceph_pool_perm_check pool %lld no read perm\n",
2101 pool);
2102 return -EPERM;
2103 }
2104 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
2105 dout("ceph_pool_perm_check pool %lld no write perm\n",
2106 pool);
2107 return -EPERM;
2108 }
2109 return 0;
2110 }
2111
2112 pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
2113 ret = __ceph_pool_perm_get(ci, pool, pool_ns);
2114 ceph_put_string(pool_ns);
2115 if (ret < 0)
2116 return ret;
2117
2118 flags = CEPH_I_POOL_PERM;
2119 if (ret & POOL_READ)
2120 flags |= CEPH_I_POOL_RD;
2121 if (ret & POOL_WRITE)
2122 flags |= CEPH_I_POOL_WR;
2123
2124 spin_lock(&ci->i_ceph_lock);
2125 if (pool == ci->i_layout.pool_id &&
2126 pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2127 ci->i_ceph_flags |= flags;
2128 } else {
2129 pool = ci->i_layout.pool_id;
2130 flags = ci->i_ceph_flags;
2131 }
2132 spin_unlock(&ci->i_ceph_lock);
2133 goto check;
2134 }
2135
ceph_pool_perm_destroy(struct ceph_mds_client * mdsc)2136 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2137 {
2138 struct ceph_pool_perm *perm;
2139 struct rb_node *n;
2140
2141 while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2142 n = rb_first(&mdsc->pool_perm_tree);
2143 perm = rb_entry(n, struct ceph_pool_perm, node);
2144 rb_erase(n, &mdsc->pool_perm_tree);
2145 kfree(perm);
2146 }
2147 }
2148