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