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