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