~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/ceph/addr.c

Version: ~ [ linux-5.4-rc3 ] ~ [ linux-5.3.6 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.79 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.149 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.196 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.196 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.75 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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 
 12 #include "super.h"
 13 #include "mds_client.h"
 14 #include "cache.h"
 15 #include <linux/ceph/osd_client.h>
 16 
 17 /*
 18  * Ceph address space ops.
 19  *
 20  * There are a few funny things going on here.
 21  *
 22  * The page->private field is used to reference a struct
 23  * ceph_snap_context for _every_ dirty page.  This indicates which
 24  * snapshot the page was logically dirtied in, and thus which snap
 25  * context needs to be associated with the osd write during writeback.
 26  *
 27  * Similarly, struct ceph_inode_info maintains a set of counters to
 28  * count dirty pages on the inode.  In the absence of snapshots,
 29  * i_wrbuffer_ref == i_wrbuffer_ref_head == the dirty page count.
 30  *
 31  * When a snapshot is taken (that is, when the client receives
 32  * notification that a snapshot was taken), each inode with caps and
 33  * with dirty pages (dirty pages implies there is a cap) gets a new
 34  * ceph_cap_snap in the i_cap_snaps list (which is sorted in ascending
 35  * order, new snaps go to the tail).  The i_wrbuffer_ref_head count is
 36  * moved to capsnap->dirty. (Unless a sync write is currently in
 37  * progress.  In that case, the capsnap is said to be "pending", new
 38  * writes cannot start, and the capsnap isn't "finalized" until the
 39  * write completes (or fails) and a final size/mtime for the inode for
 40  * that snap can be settled upon.)  i_wrbuffer_ref_head is reset to 0.
 41  *
 42  * On writeback, we must submit writes to the osd IN SNAP ORDER.  So,
 43  * we look for the first capsnap in i_cap_snaps and write out pages in
 44  * that snap context _only_.  Then we move on to the next capsnap,
 45  * eventually reaching the "live" or "head" context (i.e., pages that
 46  * are not yet snapped) and are writing the most recently dirtied
 47  * pages.
 48  *
 49  * Invalidate and so forth must take care to ensure the dirty page
 50  * accounting is preserved.
 51  */
 52 
 53 #define CONGESTION_ON_THRESH(congestion_kb) (congestion_kb >> (PAGE_SHIFT-10))
 54 #define CONGESTION_OFF_THRESH(congestion_kb)                            \
 55         (CONGESTION_ON_THRESH(congestion_kb) -                          \
 56          (CONGESTION_ON_THRESH(congestion_kb) >> 2))
 57 
 58 static inline struct ceph_snap_context *page_snap_context(struct page *page)
 59 {
 60         if (PagePrivate(page))
 61                 return (void *)page->private;
 62         return NULL;
 63 }
 64 
 65 /*
 66  * Dirty a page.  Optimistically adjust accounting, on the assumption
 67  * that we won't race with invalidate.  If we do, readjust.
 68  */
 69 static int ceph_set_page_dirty(struct page *page)
 70 {
 71         struct address_space *mapping = page->mapping;
 72         struct inode *inode;
 73         struct ceph_inode_info *ci;
 74         struct ceph_snap_context *snapc;
 75         int ret;
 76 
 77         if (unlikely(!mapping))
 78                 return !TestSetPageDirty(page);
 79 
 80         if (PageDirty(page)) {
 81                 dout("%p set_page_dirty %p idx %lu -- already dirty\n",
 82                      mapping->host, page, page->index);
 83                 BUG_ON(!PagePrivate(page));
 84                 return 0;
 85         }
 86 
 87         inode = mapping->host;
 88         ci = ceph_inode(inode);
 89 
 90         /* dirty the head */
 91         spin_lock(&ci->i_ceph_lock);
 92         BUG_ON(ci->i_wr_ref == 0); // caller should hold Fw reference
 93         if (__ceph_have_pending_cap_snap(ci)) {
 94                 struct ceph_cap_snap *capsnap =
 95                                 list_last_entry(&ci->i_cap_snaps,
 96                                                 struct ceph_cap_snap,
 97                                                 ci_item);
 98                 snapc = ceph_get_snap_context(capsnap->context);
 99                 capsnap->dirty_pages++;
100         } else {
101                 BUG_ON(!ci->i_head_snapc);
102                 snapc = ceph_get_snap_context(ci->i_head_snapc);
103                 ++ci->i_wrbuffer_ref_head;
104         }
105         if (ci->i_wrbuffer_ref == 0)
106                 ihold(inode);
107         ++ci->i_wrbuffer_ref;
108         dout("%p set_page_dirty %p idx %lu head %d/%d -> %d/%d "
109              "snapc %p seq %lld (%d snaps)\n",
110              mapping->host, page, page->index,
111              ci->i_wrbuffer_ref-1, ci->i_wrbuffer_ref_head-1,
112              ci->i_wrbuffer_ref, ci->i_wrbuffer_ref_head,
113              snapc, snapc->seq, snapc->num_snaps);
114         spin_unlock(&ci->i_ceph_lock);
115 
116         /*
117          * Reference snap context in page->private.  Also set
118          * PagePrivate so that we get invalidatepage callback.
119          */
120         BUG_ON(PagePrivate(page));
121         page->private = (unsigned long)snapc;
122         SetPagePrivate(page);
123 
124         ret = __set_page_dirty_nobuffers(page);
125         WARN_ON(!PageLocked(page));
126         WARN_ON(!page->mapping);
127 
128         return ret;
129 }
130 
131 /*
132  * If we are truncating the full page (i.e. offset == 0), adjust the
133  * dirty page counters appropriately.  Only called if there is private
134  * data on the page.
135  */
136 static void ceph_invalidatepage(struct page *page, unsigned int offset,
137                                 unsigned int length)
138 {
139         struct inode *inode;
140         struct ceph_inode_info *ci;
141         struct ceph_snap_context *snapc = page_snap_context(page);
142 
143         inode = page->mapping->host;
144         ci = ceph_inode(inode);
145 
146         if (offset != 0 || length != PAGE_SIZE) {
147                 dout("%p invalidatepage %p idx %lu partial dirty page %u~%u\n",
148                      inode, page, page->index, offset, length);
149                 return;
150         }
151 
152         ceph_invalidate_fscache_page(inode, page);
153 
154         if (!PagePrivate(page))
155                 return;
156 
157         /*
158          * We can get non-dirty pages here due to races between
159          * set_page_dirty and truncate_complete_page; just spit out a
160          * warning, in case we end up with accounting problems later.
161          */
162         if (!PageDirty(page))
163                 pr_err("%p invalidatepage %p page not dirty\n", inode, page);
164 
165         ClearPageChecked(page);
166 
167         dout("%p invalidatepage %p idx %lu full dirty page\n",
168              inode, page, page->index);
169 
170         ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
171         ceph_put_snap_context(snapc);
172         page->private = 0;
173         ClearPagePrivate(page);
174 }
175 
176 static int ceph_releasepage(struct page *page, gfp_t g)
177 {
178         dout("%p releasepage %p idx %lu (%sdirty)\n", page->mapping->host,
179              page, page->index, PageDirty(page) ? "" : "not ");
180 
181         /* Can we release the page from the cache? */
182         if (!ceph_release_fscache_page(page, g))
183                 return 0;
184 
185         return !PagePrivate(page);
186 }
187 
188 /*
189  * read a single page, without unlocking it.
190  */
191 static int readpage_nounlock(struct file *filp, struct page *page)
192 {
193         struct inode *inode = file_inode(filp);
194         struct ceph_inode_info *ci = ceph_inode(inode);
195         struct ceph_osd_client *osdc =
196                 &ceph_inode_to_client(inode)->client->osdc;
197         int err = 0;
198         u64 off = page_offset(page);
199         u64 len = PAGE_SIZE;
200 
201         if (off >= i_size_read(inode)) {
202                 zero_user_segment(page, 0, PAGE_SIZE);
203                 SetPageUptodate(page);
204                 return 0;
205         }
206 
207         if (ci->i_inline_version != CEPH_INLINE_NONE) {
208                 /*
209                  * Uptodate inline data should have been added
210                  * into page cache while getting Fcr caps.
211                  */
212                 if (off == 0)
213                         return -EINVAL;
214                 zero_user_segment(page, 0, PAGE_SIZE);
215                 SetPageUptodate(page);
216                 return 0;
217         }
218 
219         err = ceph_readpage_from_fscache(inode, page);
220         if (err == 0)
221                 goto out;
222 
223         dout("readpage inode %p file %p page %p index %lu\n",
224              inode, filp, page, page->index);
225         err = ceph_osdc_readpages(osdc, ceph_vino(inode), &ci->i_layout,
226                                   off, &len,
227                                   ci->i_truncate_seq, ci->i_truncate_size,
228                                   &page, 1, 0);
229         if (err == -ENOENT)
230                 err = 0;
231         if (err < 0) {
232                 SetPageError(page);
233                 ceph_fscache_readpage_cancel(inode, page);
234                 goto out;
235         }
236         if (err < PAGE_SIZE)
237                 /* zero fill remainder of page */
238                 zero_user_segment(page, err, PAGE_SIZE);
239         else
240                 flush_dcache_page(page);
241 
242         SetPageUptodate(page);
243         ceph_readpage_to_fscache(inode, page);
244 
245 out:
246         return err < 0 ? err : 0;
247 }
248 
249 static int ceph_readpage(struct file *filp, struct page *page)
250 {
251         int r = readpage_nounlock(filp, page);
252         unlock_page(page);
253         return r;
254 }
255 
256 /*
257  * Finish an async read(ahead) op.
258  */
259 static void finish_read(struct ceph_osd_request *req)
260 {
261         struct inode *inode = req->r_inode;
262         struct ceph_osd_data *osd_data;
263         int rc = req->r_result <= 0 ? req->r_result : 0;
264         int bytes = req->r_result >= 0 ? req->r_result : 0;
265         int num_pages;
266         int i;
267 
268         dout("finish_read %p req %p rc %d bytes %d\n", inode, req, rc, bytes);
269 
270         /* unlock all pages, zeroing any data we didn't read */
271         osd_data = osd_req_op_extent_osd_data(req, 0);
272         BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
273         num_pages = calc_pages_for((u64)osd_data->alignment,
274                                         (u64)osd_data->length);
275         for (i = 0; i < num_pages; i++) {
276                 struct page *page = osd_data->pages[i];
277 
278                 if (rc < 0 && rc != -ENOENT) {
279                         ceph_fscache_readpage_cancel(inode, page);
280                         goto unlock;
281                 }
282                 if (bytes < (int)PAGE_SIZE) {
283                         /* zero (remainder of) page */
284                         int s = bytes < 0 ? 0 : bytes;
285                         zero_user_segment(page, s, PAGE_SIZE);
286                 }
287                 dout("finish_read %p uptodate %p idx %lu\n", inode, page,
288                      page->index);
289                 flush_dcache_page(page);
290                 SetPageUptodate(page);
291                 ceph_readpage_to_fscache(inode, page);
292 unlock:
293                 unlock_page(page);
294                 put_page(page);
295                 bytes -= PAGE_SIZE;
296         }
297         kfree(osd_data->pages);
298 }
299 
300 /*
301  * start an async read(ahead) operation.  return nr_pages we submitted
302  * a read for on success, or negative error code.
303  */
304 static int start_read(struct inode *inode, struct list_head *page_list, int max)
305 {
306         struct ceph_osd_client *osdc =
307                 &ceph_inode_to_client(inode)->client->osdc;
308         struct ceph_inode_info *ci = ceph_inode(inode);
309         struct page *page = list_entry(page_list->prev, struct page, lru);
310         struct ceph_vino vino;
311         struct ceph_osd_request *req;
312         u64 off;
313         u64 len;
314         int i;
315         struct page **pages;
316         pgoff_t next_index;
317         int nr_pages = 0;
318         int got = 0;
319         int ret = 0;
320 
321         if (!current->journal_info) {
322                 /* caller of readpages does not hold buffer and read caps
323                  * (fadvise, madvise and readahead cases) */
324                 int want = CEPH_CAP_FILE_CACHE;
325                 ret = ceph_try_get_caps(ci, CEPH_CAP_FILE_RD, want, &got);
326                 if (ret < 0) {
327                         dout("start_read %p, error getting cap\n", inode);
328                 } else if (!(got & want)) {
329                         dout("start_read %p, no cache cap\n", inode);
330                         ret = 0;
331                 }
332                 if (ret <= 0) {
333                         if (got)
334                                 ceph_put_cap_refs(ci, got);
335                         while (!list_empty(page_list)) {
336                                 page = list_entry(page_list->prev,
337                                                   struct page, lru);
338                                 list_del(&page->lru);
339                                 put_page(page);
340                         }
341                         return ret;
342                 }
343         }
344 
345         off = (u64) page_offset(page);
346 
347         /* count pages */
348         next_index = page->index;
349         list_for_each_entry_reverse(page, page_list, lru) {
350                 if (page->index != next_index)
351                         break;
352                 nr_pages++;
353                 next_index++;
354                 if (max && nr_pages == max)
355                         break;
356         }
357         len = nr_pages << PAGE_SHIFT;
358         dout("start_read %p nr_pages %d is %lld~%lld\n", inode, nr_pages,
359              off, len);
360         vino = ceph_vino(inode);
361         req = ceph_osdc_new_request(osdc, &ci->i_layout, vino, off, &len,
362                                     0, 1, CEPH_OSD_OP_READ,
363                                     CEPH_OSD_FLAG_READ, NULL,
364                                     ci->i_truncate_seq, ci->i_truncate_size,
365                                     false);
366         if (IS_ERR(req)) {
367                 ret = PTR_ERR(req);
368                 goto out;
369         }
370 
371         /* build page vector */
372         nr_pages = calc_pages_for(0, len);
373         pages = kmalloc(sizeof(*pages) * nr_pages, GFP_KERNEL);
374         if (!pages) {
375                 ret = -ENOMEM;
376                 goto out_put;
377         }
378         for (i = 0; i < nr_pages; ++i) {
379                 page = list_entry(page_list->prev, struct page, lru);
380                 BUG_ON(PageLocked(page));
381                 list_del(&page->lru);
382 
383                 dout("start_read %p adding %p idx %lu\n", inode, page,
384                      page->index);
385                 if (add_to_page_cache_lru(page, &inode->i_data, page->index,
386                                           GFP_KERNEL)) {
387                         ceph_fscache_uncache_page(inode, page);
388                         put_page(page);
389                         dout("start_read %p add_to_page_cache failed %p\n",
390                              inode, page);
391                         nr_pages = i;
392                         if (nr_pages > 0) {
393                                 len = nr_pages << PAGE_SHIFT;
394                                 osd_req_op_extent_update(req, 0, len);
395                                 break;
396                         }
397                         goto out_pages;
398                 }
399                 pages[i] = page;
400         }
401         osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, false, false);
402         req->r_callback = finish_read;
403         req->r_inode = inode;
404 
405         dout("start_read %p starting %p %lld~%lld\n", inode, req, off, len);
406         ret = ceph_osdc_start_request(osdc, req, false);
407         if (ret < 0)
408                 goto out_pages;
409         ceph_osdc_put_request(req);
410 
411         /* After adding locked pages to page cache, the inode holds cache cap.
412          * So we can drop our cap refs. */
413         if (got)
414                 ceph_put_cap_refs(ci, got);
415 
416         return nr_pages;
417 
418 out_pages:
419         for (i = 0; i < nr_pages; ++i) {
420                 ceph_fscache_readpage_cancel(inode, pages[i]);
421                 unlock_page(pages[i]);
422         }
423         ceph_put_page_vector(pages, nr_pages, false);
424 out_put:
425         ceph_osdc_put_request(req);
426 out:
427         if (got)
428                 ceph_put_cap_refs(ci, got);
429         return ret;
430 }
431 
432 
433 /*
434  * Read multiple pages.  Leave pages we don't read + unlock in page_list;
435  * the caller (VM) cleans them up.
436  */
437 static int ceph_readpages(struct file *file, struct address_space *mapping,
438                           struct list_head *page_list, unsigned nr_pages)
439 {
440         struct inode *inode = file_inode(file);
441         struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
442         int rc = 0;
443         int max = 0;
444 
445         if (ceph_inode(inode)->i_inline_version != CEPH_INLINE_NONE)
446                 return -EINVAL;
447 
448         rc = ceph_readpages_from_fscache(mapping->host, mapping, page_list,
449                                          &nr_pages);
450 
451         if (rc == 0)
452                 goto out;
453 
454         if (fsc->mount_options->rsize >= PAGE_SIZE)
455                 max = (fsc->mount_options->rsize + PAGE_SIZE - 1)
456                         >> PAGE_SHIFT;
457 
458         dout("readpages %p file %p nr_pages %d max %d\n", inode,
459                 file, nr_pages,
460              max);
461         while (!list_empty(page_list)) {
462                 rc = start_read(inode, page_list, max);
463                 if (rc < 0)
464                         goto out;
465         }
466 out:
467         ceph_fscache_readpages_cancel(inode, page_list);
468 
469         dout("readpages %p file %p ret %d\n", inode, file, rc);
470         return rc;
471 }
472 
473 /*
474  * Get ref for the oldest snapc for an inode with dirty data... that is, the
475  * only snap context we are allowed to write back.
476  */
477 static struct ceph_snap_context *get_oldest_context(struct inode *inode,
478                                                     loff_t *snap_size,
479                                                     u64 *truncate_size,
480                                                     u32 *truncate_seq)
481 {
482         struct ceph_inode_info *ci = ceph_inode(inode);
483         struct ceph_snap_context *snapc = NULL;
484         struct ceph_cap_snap *capsnap = NULL;
485 
486         spin_lock(&ci->i_ceph_lock);
487         list_for_each_entry(capsnap, &ci->i_cap_snaps, ci_item) {
488                 dout(" cap_snap %p snapc %p has %d dirty pages\n", capsnap,
489                      capsnap->context, capsnap->dirty_pages);
490                 if (capsnap->dirty_pages) {
491                         snapc = ceph_get_snap_context(capsnap->context);
492                         if (snap_size)
493                                 *snap_size = capsnap->size;
494                         if (truncate_size)
495                                 *truncate_size = capsnap->truncate_size;
496                         if (truncate_seq)
497                                 *truncate_seq = capsnap->truncate_seq;
498                         break;
499                 }
500         }
501         if (!snapc && ci->i_wrbuffer_ref_head) {
502                 snapc = ceph_get_snap_context(ci->i_head_snapc);
503                 dout(" head snapc %p has %d dirty pages\n",
504                      snapc, ci->i_wrbuffer_ref_head);
505                 if (truncate_size)
506                         *truncate_size = ci->i_truncate_size;
507                 if (truncate_seq)
508                         *truncate_seq = ci->i_truncate_seq;
509         }
510         spin_unlock(&ci->i_ceph_lock);
511         return snapc;
512 }
513 
514 /*
515  * Write a single page, but leave the page locked.
516  *
517  * If we get a write error, set the page error bit, but still adjust the
518  * dirty page accounting (i.e., page is no longer dirty).
519  */
520 static int writepage_nounlock(struct page *page, struct writeback_control *wbc)
521 {
522         struct inode *inode;
523         struct ceph_inode_info *ci;
524         struct ceph_fs_client *fsc;
525         struct ceph_osd_client *osdc;
526         struct ceph_snap_context *snapc, *oldest;
527         loff_t page_off = page_offset(page);
528         loff_t snap_size = -1;
529         long writeback_stat;
530         u64 truncate_size;
531         u32 truncate_seq;
532         int err = 0, len = PAGE_SIZE;
533 
534         dout("writepage %p idx %lu\n", page, page->index);
535 
536         if (!page->mapping || !page->mapping->host) {
537                 dout("writepage %p - no mapping\n", page);
538                 return -EFAULT;
539         }
540         inode = page->mapping->host;
541         ci = ceph_inode(inode);
542         fsc = ceph_inode_to_client(inode);
543         osdc = &fsc->client->osdc;
544 
545         /* verify this is a writeable snap context */
546         snapc = page_snap_context(page);
547         if (snapc == NULL) {
548                 dout("writepage %p page %p not dirty?\n", inode, page);
549                 goto out;
550         }
551         oldest = get_oldest_context(inode, &snap_size,
552                                     &truncate_size, &truncate_seq);
553         if (snapc->seq > oldest->seq) {
554                 dout("writepage %p page %p snapc %p not writeable - noop\n",
555                      inode, page, snapc);
556                 /* we should only noop if called by kswapd */
557                 WARN_ON((current->flags & PF_MEMALLOC) == 0);
558                 ceph_put_snap_context(oldest);
559                 goto out;
560         }
561         ceph_put_snap_context(oldest);
562 
563         if (snap_size == -1)
564                 snap_size = i_size_read(inode);
565 
566         /* is this a partial page at end of file? */
567         if (page_off >= snap_size) {
568                 dout("%p page eof %llu\n", page, snap_size);
569                 goto out;
570         }
571         if (snap_size < page_off + len)
572                 len = snap_size - page_off;
573 
574         dout("writepage %p page %p index %lu on %llu~%u snapc %p\n",
575              inode, page, page->index, page_off, len, snapc);
576 
577         writeback_stat = atomic_long_inc_return(&fsc->writeback_count);
578         if (writeback_stat >
579             CONGESTION_ON_THRESH(fsc->mount_options->congestion_kb))
580                 set_bdi_congested(&fsc->backing_dev_info, BLK_RW_ASYNC);
581 
582         set_page_writeback(page);
583         err = ceph_osdc_writepages(osdc, ceph_vino(inode),
584                                    &ci->i_layout, snapc,
585                                    page_off, len,
586                                    truncate_seq, truncate_size,
587                                    &inode->i_mtime, &page, 1);
588         if (err < 0) {
589                 struct writeback_control tmp_wbc;
590                 if (!wbc)
591                         wbc = &tmp_wbc;
592                 if (err == -ERESTARTSYS) {
593                         /* killed by SIGKILL */
594                         dout("writepage interrupted page %p\n", page);
595                         redirty_page_for_writepage(wbc, page);
596                         end_page_writeback(page);
597                         goto out;
598                 }
599                 dout("writepage setting page/mapping error %d %p\n",
600                      err, page);
601                 SetPageError(page);
602                 mapping_set_error(&inode->i_data, err);
603                 wbc->pages_skipped++;
604         } else {
605                 dout("writepage cleaned page %p\n", page);
606                 err = 0;  /* vfs expects us to return 0 */
607         }
608         page->private = 0;
609         ClearPagePrivate(page);
610         end_page_writeback(page);
611         ceph_put_wrbuffer_cap_refs(ci, 1, snapc);
612         ceph_put_snap_context(snapc);  /* page's reference */
613 out:
614         return err;
615 }
616 
617 static int ceph_writepage(struct page *page, struct writeback_control *wbc)
618 {
619         int err;
620         struct inode *inode = page->mapping->host;
621         BUG_ON(!inode);
622         ihold(inode);
623         err = writepage_nounlock(page, wbc);
624         if (err == -ERESTARTSYS) {
625                 /* direct memory reclaimer was killed by SIGKILL. return 0
626                  * to prevent caller from setting mapping/page error */
627                 err = 0;
628         }
629         unlock_page(page);
630         iput(inode);
631         return err;
632 }
633 
634 /*
635  * lame release_pages helper.  release_pages() isn't exported to
636  * modules.
637  */
638 static void ceph_release_pages(struct page **pages, int num)
639 {
640         struct pagevec pvec;
641         int i;
642 
643         pagevec_init(&pvec, 0);
644         for (i = 0; i < num; i++) {
645                 if (pagevec_add(&pvec, pages[i]) == 0)
646                         pagevec_release(&pvec);
647         }
648         pagevec_release(&pvec);
649 }
650 
651 /*
652  * async writeback completion handler.
653  *
654  * If we get an error, set the mapping error bit, but not the individual
655  * page error bits.
656  */
657 static void writepages_finish(struct ceph_osd_request *req)
658 {
659         struct inode *inode = req->r_inode;
660         struct ceph_inode_info *ci = ceph_inode(inode);
661         struct ceph_osd_data *osd_data;
662         struct page *page;
663         int num_pages, total_pages = 0;
664         int i, j;
665         int rc = req->r_result;
666         struct ceph_snap_context *snapc = req->r_snapc;
667         struct address_space *mapping = inode->i_mapping;
668         struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
669         bool remove_page;
670 
671         dout("writepages_finish %p rc %d\n", inode, rc);
672         if (rc < 0)
673                 mapping_set_error(mapping, rc);
674 
675         /*
676          * We lost the cache cap, need to truncate the page before
677          * it is unlocked, otherwise we'd truncate it later in the
678          * page truncation thread, possibly losing some data that
679          * raced its way in
680          */
681         remove_page = !(ceph_caps_issued(ci) &
682                         (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO));
683 
684         /* clean all pages */
685         for (i = 0; i < req->r_num_ops; i++) {
686                 if (req->r_ops[i].op != CEPH_OSD_OP_WRITE)
687                         break;
688 
689                 osd_data = osd_req_op_extent_osd_data(req, i);
690                 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGES);
691                 num_pages = calc_pages_for((u64)osd_data->alignment,
692                                            (u64)osd_data->length);
693                 total_pages += num_pages;
694                 for (j = 0; j < num_pages; j++) {
695                         page = osd_data->pages[j];
696                         BUG_ON(!page);
697                         WARN_ON(!PageUptodate(page));
698 
699                         if (atomic_long_dec_return(&fsc->writeback_count) <
700                              CONGESTION_OFF_THRESH(
701                                         fsc->mount_options->congestion_kb))
702                                 clear_bdi_congested(&fsc->backing_dev_info,
703                                                     BLK_RW_ASYNC);
704 
705                         if (rc < 0)
706                                 SetPageError(page);
707 
708                         ceph_put_snap_context(page_snap_context(page));
709                         page->private = 0;
710                         ClearPagePrivate(page);
711                         dout("unlocking %p\n", page);
712                         end_page_writeback(page);
713 
714                         if (remove_page)
715                                 generic_error_remove_page(inode->i_mapping,
716                                                           page);
717 
718                         unlock_page(page);
719                 }
720                 dout("writepages_finish %p wrote %llu bytes cleaned %d pages\n",
721                      inode, osd_data->length, rc >= 0 ? num_pages : 0);
722 
723                 ceph_release_pages(osd_data->pages, num_pages);
724         }
725 
726         ceph_put_wrbuffer_cap_refs(ci, total_pages, snapc);
727 
728         osd_data = osd_req_op_extent_osd_data(req, 0);
729         if (osd_data->pages_from_pool)
730                 mempool_free(osd_data->pages,
731                              ceph_sb_to_client(inode->i_sb)->wb_pagevec_pool);
732         else
733                 kfree(osd_data->pages);
734         ceph_osdc_put_request(req);
735 }
736 
737 /*
738  * initiate async writeback
739  */
740 static int ceph_writepages_start(struct address_space *mapping,
741                                  struct writeback_control *wbc)
742 {
743         struct inode *inode = mapping->host;
744         struct ceph_inode_info *ci = ceph_inode(inode);
745         struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
746         struct ceph_vino vino = ceph_vino(inode);
747         pgoff_t index, start, end;
748         int range_whole = 0;
749         int should_loop = 1;
750         pgoff_t max_pages = 0, max_pages_ever = 0;
751         struct ceph_snap_context *snapc = NULL, *last_snapc = NULL, *pgsnapc;
752         struct pagevec pvec;
753         int done = 0;
754         int rc = 0;
755         unsigned wsize = 1 << inode->i_blkbits;
756         struct ceph_osd_request *req = NULL;
757         int do_sync = 0;
758         loff_t snap_size, i_size;
759         u64 truncate_size;
760         u32 truncate_seq;
761 
762         /*
763          * Include a 'sync' in the OSD request if this is a data
764          * integrity write (e.g., O_SYNC write or fsync()), or if our
765          * cap is being revoked.
766          */
767         if ((wbc->sync_mode == WB_SYNC_ALL) ||
768                 ceph_caps_revoking(ci, CEPH_CAP_FILE_BUFFER))
769                 do_sync = 1;
770         dout("writepages_start %p dosync=%d (mode=%s)\n",
771              inode, do_sync,
772              wbc->sync_mode == WB_SYNC_NONE ? "NONE" :
773              (wbc->sync_mode == WB_SYNC_ALL ? "ALL" : "HOLD"));
774 
775         if (ACCESS_ONCE(fsc->mount_state) == CEPH_MOUNT_SHUTDOWN) {
776                 if (ci->i_wrbuffer_ref > 0) {
777                         pr_warn_ratelimited(
778                                 "writepage_start %p %lld forced umount\n",
779                                 inode, ceph_ino(inode));
780                 }
781                 mapping_set_error(mapping, -EIO);
782                 return -EIO; /* we're in a forced umount, don't write! */
783         }
784         if (fsc->mount_options->wsize && fsc->mount_options->wsize < wsize)
785                 wsize = fsc->mount_options->wsize;
786         if (wsize < PAGE_SIZE)
787                 wsize = PAGE_SIZE;
788         max_pages_ever = wsize >> PAGE_SHIFT;
789 
790         pagevec_init(&pvec, 0);
791 
792         /* where to start/end? */
793         if (wbc->range_cyclic) {
794                 start = mapping->writeback_index; /* Start from prev offset */
795                 end = -1;
796                 dout(" cyclic, start at %lu\n", start);
797         } else {
798                 start = wbc->range_start >> PAGE_SHIFT;
799                 end = wbc->range_end >> PAGE_SHIFT;
800                 if (wbc->range_start == 0 && wbc->range_end == LLONG_MAX)
801                         range_whole = 1;
802                 should_loop = 0;
803                 dout(" not cyclic, %lu to %lu\n", start, end);
804         }
805         index = start;
806 
807 retry:
808         /* find oldest snap context with dirty data */
809         ceph_put_snap_context(snapc);
810         snap_size = -1;
811         snapc = get_oldest_context(inode, &snap_size,
812                                    &truncate_size, &truncate_seq);
813         if (!snapc) {
814                 /* hmm, why does writepages get called when there
815                    is no dirty data? */
816                 dout(" no snap context with dirty data?\n");
817                 goto out;
818         }
819         dout(" oldest snapc is %p seq %lld (%d snaps)\n",
820              snapc, snapc->seq, snapc->num_snaps);
821 
822         i_size = i_size_read(inode);
823 
824         if (last_snapc && snapc != last_snapc) {
825                 /* if we switched to a newer snapc, restart our scan at the
826                  * start of the original file range. */
827                 dout("  snapc differs from last pass, restarting at %lu\n",
828                      index);
829                 index = start;
830         }
831         last_snapc = snapc;
832 
833         while (!done && index <= end) {
834                 unsigned i;
835                 int first;
836                 pgoff_t strip_unit_end = 0;
837                 int num_ops = 0, op_idx;
838                 int pvec_pages, locked_pages = 0;
839                 struct page **pages = NULL, **data_pages;
840                 mempool_t *pool = NULL; /* Becomes non-null if mempool used */
841                 struct page *page;
842                 int want;
843                 u64 offset = 0, len = 0;
844 
845                 max_pages = max_pages_ever;
846 
847 get_more_pages:
848                 first = -1;
849                 want = min(end - index,
850                            min((pgoff_t)PAGEVEC_SIZE,
851                                max_pages - (pgoff_t)locked_pages) - 1)
852                         + 1;
853                 pvec_pages = pagevec_lookup_tag(&pvec, mapping, &index,
854                                                 PAGECACHE_TAG_DIRTY,
855                                                 want);
856                 dout("pagevec_lookup_tag got %d\n", pvec_pages);
857                 if (!pvec_pages && !locked_pages)
858                         break;
859                 for (i = 0; i < pvec_pages && locked_pages < max_pages; i++) {
860                         page = pvec.pages[i];
861                         dout("? %p idx %lu\n", page, page->index);
862                         if (locked_pages == 0)
863                                 lock_page(page);  /* first page */
864                         else if (!trylock_page(page))
865                                 break;
866 
867                         /* only dirty pages, or our accounting breaks */
868                         if (unlikely(!PageDirty(page)) ||
869                             unlikely(page->mapping != mapping)) {
870                                 dout("!dirty or !mapping %p\n", page);
871                                 unlock_page(page);
872                                 break;
873                         }
874                         if (!wbc->range_cyclic && page->index > end) {
875                                 dout("end of range %p\n", page);
876                                 done = 1;
877                                 unlock_page(page);
878                                 break;
879                         }
880                         if (strip_unit_end && (page->index > strip_unit_end)) {
881                                 dout("end of strip unit %p\n", page);
882                                 unlock_page(page);
883                                 break;
884                         }
885                         if (wbc->sync_mode != WB_SYNC_NONE) {
886                                 dout("waiting on writeback %p\n", page);
887                                 wait_on_page_writeback(page);
888                         }
889                         if (page_offset(page) >=
890                             (snap_size == -1 ? i_size : snap_size)) {
891                                 dout("%p page eof %llu\n", page,
892                                      (snap_size == -1 ? i_size : snap_size));
893                                 done = 1;
894                                 unlock_page(page);
895                                 break;
896                         }
897                         if (PageWriteback(page)) {
898                                 dout("%p under writeback\n", page);
899                                 unlock_page(page);
900                                 break;
901                         }
902 
903                         /* only if matching snap context */
904                         pgsnapc = page_snap_context(page);
905                         if (pgsnapc->seq > snapc->seq) {
906                                 dout("page snapc %p %lld > oldest %p %lld\n",
907                                      pgsnapc, pgsnapc->seq, snapc, snapc->seq);
908                                 unlock_page(page);
909                                 if (!locked_pages)
910                                         continue; /* keep looking for snap */
911                                 break;
912                         }
913 
914                         if (!clear_page_dirty_for_io(page)) {
915                                 dout("%p !clear_page_dirty_for_io\n", page);
916                                 unlock_page(page);
917                                 break;
918                         }
919 
920                         /*
921                          * We have something to write.  If this is
922                          * the first locked page this time through,
923                          * calculate max possinle write size and
924                          * allocate a page array
925                          */
926                         if (locked_pages == 0) {
927                                 u64 objnum;
928                                 u64 objoff;
929 
930                                 /* prepare async write request */
931                                 offset = (u64)page_offset(page);
932                                 len = wsize;
933 
934                                 rc = ceph_calc_file_object_mapping(&ci->i_layout,
935                                                                 offset, len,
936                                                                 &objnum, &objoff,
937                                                                 &len);
938                                 if (rc < 0) {
939                                         unlock_page(page);
940                                         break;
941                                 }
942 
943                                 num_ops = 1 + do_sync;
944                                 strip_unit_end = page->index +
945                                         ((len - 1) >> PAGE_SHIFT);
946 
947                                 BUG_ON(pages);
948                                 max_pages = calc_pages_for(0, (u64)len);
949                                 pages = kmalloc(max_pages * sizeof (*pages),
950                                                 GFP_NOFS);
951                                 if (!pages) {
952                                         pool = fsc->wb_pagevec_pool;
953                                         pages = mempool_alloc(pool, GFP_NOFS);
954                                         BUG_ON(!pages);
955                                 }
956 
957                                 len = 0;
958                         } else if (page->index !=
959                                    (offset + len) >> PAGE_SHIFT) {
960                                 if (num_ops >= (pool ?  CEPH_OSD_SLAB_OPS :
961                                                         CEPH_OSD_MAX_OPS)) {
962                                         redirty_page_for_writepage(wbc, page);
963                                         unlock_page(page);
964                                         break;
965                                 }
966 
967                                 num_ops++;
968                                 offset = (u64)page_offset(page);
969                                 len = 0;
970                         }
971 
972                         /* note position of first page in pvec */
973                         if (first < 0)
974                                 first = i;
975                         dout("%p will write page %p idx %lu\n",
976                              inode, page, page->index);
977 
978                         if (atomic_long_inc_return(&fsc->writeback_count) >
979                             CONGESTION_ON_THRESH(
980                                     fsc->mount_options->congestion_kb)) {
981                                 set_bdi_congested(&fsc->backing_dev_info,
982                                                   BLK_RW_ASYNC);
983                         }
984 
985                         pages[locked_pages] = page;
986                         locked_pages++;
987                         len += PAGE_SIZE;
988                 }
989 
990                 /* did we get anything? */
991                 if (!locked_pages)
992                         goto release_pvec_pages;
993                 if (i) {
994                         int j;
995                         BUG_ON(!locked_pages || first < 0);
996 
997                         if (pvec_pages && i == pvec_pages &&
998                             locked_pages < max_pages) {
999                                 dout("reached end pvec, trying for more\n");
1000                                 pagevec_reinit(&pvec);
1001                                 goto get_more_pages;
1002                         }
1003 
1004                         /* shift unused pages over in the pvec...  we
1005                          * will need to release them below. */
1006                         for (j = i; j < pvec_pages; j++) {
1007                                 dout(" pvec leftover page %p\n", pvec.pages[j]);
1008                                 pvec.pages[j-i+first] = pvec.pages[j];
1009                         }
1010                         pvec.nr -= i-first;
1011                 }
1012 
1013 new_request:
1014                 offset = page_offset(pages[0]);
1015                 len = wsize;
1016 
1017                 req = ceph_osdc_new_request(&fsc->client->osdc,
1018                                         &ci->i_layout, vino,
1019                                         offset, &len, 0, num_ops,
1020                                         CEPH_OSD_OP_WRITE,
1021                                         CEPH_OSD_FLAG_WRITE |
1022                                         CEPH_OSD_FLAG_ONDISK,
1023                                         snapc, truncate_seq,
1024                                         truncate_size, false);
1025                 if (IS_ERR(req)) {
1026                         req = ceph_osdc_new_request(&fsc->client->osdc,
1027                                                 &ci->i_layout, vino,
1028                                                 offset, &len, 0,
1029                                                 min(num_ops,
1030                                                     CEPH_OSD_SLAB_OPS),
1031                                                 CEPH_OSD_OP_WRITE,
1032                                                 CEPH_OSD_FLAG_WRITE |
1033                                                 CEPH_OSD_FLAG_ONDISK,
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 (ACCESS_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_area_struct *vma, struct vm_fault *vmf)
1391 {
1392         struct inode *inode = file_inode(vma->vm_file);
1393         struct ceph_inode_info *ci = ceph_inode(inode);
1394         struct ceph_file_info *fi = vma->vm_file->private_data;
1395         struct page *pinned_page = NULL;
1396         loff_t off = vmf->pgoff << PAGE_SHIFT;
1397         int want, got, ret;
1398         sigset_t oldset;
1399 
1400         ceph_block_sigs(&oldset);
1401 
1402         dout("filemap_fault %p %llx.%llx %llu~%zd trying to get caps\n",
1403              inode, ceph_vinop(inode), off, (size_t)PAGE_SIZE);
1404         if (fi->fmode & CEPH_FILE_MODE_LAZY)
1405                 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
1406         else
1407                 want = CEPH_CAP_FILE_CACHE;
1408 
1409         got = 0;
1410         ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page);
1411         if (ret < 0)
1412                 goto out_restore;
1413 
1414         dout("filemap_fault %p %llu~%zd got cap refs on %s\n",
1415              inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got));
1416 
1417         if ((got & (CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO)) ||
1418             ci->i_inline_version == CEPH_INLINE_NONE) {
1419                 current->journal_info = vma->vm_file;
1420                 ret = filemap_fault(vma, vmf);
1421                 current->journal_info = NULL;
1422         } else
1423                 ret = -EAGAIN;
1424 
1425         dout("filemap_fault %p %llu~%zd dropping cap refs on %s ret %d\n",
1426              inode, off, (size_t)PAGE_SIZE, ceph_cap_string(got), ret);
1427         if (pinned_page)
1428                 put_page(pinned_page);
1429         ceph_put_cap_refs(ci, got);
1430 
1431         if (ret != -EAGAIN)
1432                 goto out_restore;
1433 
1434         /* read inline data */
1435         if (off >= PAGE_SIZE) {
1436                 /* does not support inline data > PAGE_SIZE */
1437                 ret = VM_FAULT_SIGBUS;
1438         } else {
1439                 int ret1;
1440                 struct address_space *mapping = inode->i_mapping;
1441                 struct page *page = find_or_create_page(mapping, 0,
1442                                                 mapping_gfp_constraint(mapping,
1443                                                 ~__GFP_FS));
1444                 if (!page) {
1445                         ret = VM_FAULT_OOM;
1446                         goto out_inline;
1447                 }
1448                 ret1 = __ceph_do_getattr(inode, page,
1449                                          CEPH_STAT_CAP_INLINE_DATA, true);
1450                 if (ret1 < 0 || off >= i_size_read(inode)) {
1451                         unlock_page(page);
1452                         put_page(page);
1453                         if (ret1 < 0)
1454                                 ret = ret1;
1455                         else
1456                                 ret = VM_FAULT_SIGBUS;
1457                         goto out_inline;
1458                 }
1459                 if (ret1 < PAGE_SIZE)
1460                         zero_user_segment(page, ret1, PAGE_SIZE);
1461                 else
1462                         flush_dcache_page(page);
1463                 SetPageUptodate(page);
1464                 vmf->page = page;
1465                 ret = VM_FAULT_MAJOR | VM_FAULT_LOCKED;
1466 out_inline:
1467                 dout("filemap_fault %p %llu~%zd read inline data ret %d\n",
1468                      inode, off, (size_t)PAGE_SIZE, ret);
1469         }
1470 out_restore:
1471         ceph_restore_sigs(&oldset);
1472         if (ret < 0)
1473                 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1474 
1475         return ret;
1476 }
1477 
1478 /*
1479  * Reuse write_begin here for simplicity.
1480  */
1481 static int ceph_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
1482 {
1483         struct inode *inode = file_inode(vma->vm_file);
1484         struct ceph_inode_info *ci = ceph_inode(inode);
1485         struct ceph_file_info *fi = vma->vm_file->private_data;
1486         struct ceph_cap_flush *prealloc_cf;
1487         struct page *page = vmf->page;
1488         loff_t off = page_offset(page);
1489         loff_t size = i_size_read(inode);
1490         size_t len;
1491         int want, got, ret;
1492         sigset_t oldset;
1493 
1494         prealloc_cf = ceph_alloc_cap_flush();
1495         if (!prealloc_cf)
1496                 return VM_FAULT_OOM;
1497 
1498         ceph_block_sigs(&oldset);
1499 
1500         if (ci->i_inline_version != CEPH_INLINE_NONE) {
1501                 struct page *locked_page = NULL;
1502                 if (off == 0) {
1503                         lock_page(page);
1504                         locked_page = page;
1505                 }
1506                 ret = ceph_uninline_data(vma->vm_file, locked_page);
1507                 if (locked_page)
1508                         unlock_page(locked_page);
1509                 if (ret < 0)
1510                         goto out_free;
1511         }
1512 
1513         if (off + PAGE_SIZE <= size)
1514                 len = PAGE_SIZE;
1515         else
1516                 len = size & ~PAGE_MASK;
1517 
1518         dout("page_mkwrite %p %llx.%llx %llu~%zd getting caps i_size %llu\n",
1519              inode, ceph_vinop(inode), off, len, size);
1520         if (fi->fmode & CEPH_FILE_MODE_LAZY)
1521                 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1522         else
1523                 want = CEPH_CAP_FILE_BUFFER;
1524 
1525         got = 0;
1526         ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, off + len,
1527                             &got, NULL);
1528         if (ret < 0)
1529                 goto out_free;
1530 
1531         dout("page_mkwrite %p %llu~%zd got cap refs on %s\n",
1532              inode, off, len, ceph_cap_string(got));
1533 
1534         /* Update time before taking page lock */
1535         file_update_time(vma->vm_file);
1536 
1537         do {
1538                 lock_page(page);
1539 
1540                 if ((off > size) || (page->mapping != inode->i_mapping)) {
1541                         unlock_page(page);
1542                         ret = VM_FAULT_NOPAGE;
1543                         break;
1544                 }
1545 
1546                 ret = ceph_update_writeable_page(vma->vm_file, off, len, page);
1547                 if (ret >= 0) {
1548                         /* success.  we'll keep the page locked. */
1549                         set_page_dirty(page);
1550                         ret = VM_FAULT_LOCKED;
1551                 }
1552         } while (ret == -EAGAIN);
1553 
1554         if (ret == VM_FAULT_LOCKED ||
1555             ci->i_inline_version != CEPH_INLINE_NONE) {
1556                 int dirty;
1557                 spin_lock(&ci->i_ceph_lock);
1558                 ci->i_inline_version = CEPH_INLINE_NONE;
1559                 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR,
1560                                                &prealloc_cf);
1561                 spin_unlock(&ci->i_ceph_lock);
1562                 if (dirty)
1563                         __mark_inode_dirty(inode, dirty);
1564         }
1565 
1566         dout("page_mkwrite %p %llu~%zd dropping cap refs on %s ret %d\n",
1567              inode, off, len, ceph_cap_string(got), ret);
1568         ceph_put_cap_refs(ci, got);
1569 out_free:
1570         ceph_restore_sigs(&oldset);
1571         ceph_free_cap_flush(prealloc_cf);
1572         if (ret < 0)
1573                 ret = (ret == -ENOMEM) ? VM_FAULT_OOM : VM_FAULT_SIGBUS;
1574         return ret;
1575 }
1576 
1577 void ceph_fill_inline_data(struct inode *inode, struct page *locked_page,
1578                            char *data, size_t len)
1579 {
1580         struct address_space *mapping = inode->i_mapping;
1581         struct page *page;
1582 
1583         if (locked_page) {
1584                 page = locked_page;
1585         } else {
1586                 if (i_size_read(inode) == 0)
1587                         return;
1588                 page = find_or_create_page(mapping, 0,
1589                                            mapping_gfp_constraint(mapping,
1590                                            ~__GFP_FS));
1591                 if (!page)
1592                         return;
1593                 if (PageUptodate(page)) {
1594                         unlock_page(page);
1595                         put_page(page);
1596                         return;
1597                 }
1598         }
1599 
1600         dout("fill_inline_data %p %llx.%llx len %zu locked_page %p\n",
1601              inode, ceph_vinop(inode), len, locked_page);
1602 
1603         if (len > 0) {
1604                 void *kaddr = kmap_atomic(page);
1605                 memcpy(kaddr, data, len);
1606                 kunmap_atomic(kaddr);
1607         }
1608 
1609         if (page != locked_page) {
1610                 if (len < PAGE_SIZE)
1611                         zero_user_segment(page, len, PAGE_SIZE);
1612                 else
1613                         flush_dcache_page(page);
1614 
1615                 SetPageUptodate(page);
1616                 unlock_page(page);
1617                 put_page(page);
1618         }
1619 }
1620 
1621 int ceph_uninline_data(struct file *filp, struct page *locked_page)
1622 {
1623         struct inode *inode = file_inode(filp);
1624         struct ceph_inode_info *ci = ceph_inode(inode);
1625         struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1626         struct ceph_osd_request *req;
1627         struct page *page = NULL;
1628         u64 len, inline_version;
1629         int err = 0;
1630         bool from_pagecache = false;
1631 
1632         spin_lock(&ci->i_ceph_lock);
1633         inline_version = ci->i_inline_version;
1634         spin_unlock(&ci->i_ceph_lock);
1635 
1636         dout("uninline_data %p %llx.%llx inline_version %llu\n",
1637              inode, ceph_vinop(inode), inline_version);
1638 
1639         if (inline_version == 1 || /* initial version, no data */
1640             inline_version == CEPH_INLINE_NONE)
1641                 goto out;
1642 
1643         if (locked_page) {
1644                 page = locked_page;
1645                 WARN_ON(!PageUptodate(page));
1646         } else if (ceph_caps_issued(ci) &
1647                    (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) {
1648                 page = find_get_page(inode->i_mapping, 0);
1649                 if (page) {
1650                         if (PageUptodate(page)) {
1651                                 from_pagecache = true;
1652                                 lock_page(page);
1653                         } else {
1654                                 put_page(page);
1655                                 page = NULL;
1656                         }
1657                 }
1658         }
1659 
1660         if (page) {
1661                 len = i_size_read(inode);
1662                 if (len > PAGE_SIZE)
1663                         len = PAGE_SIZE;
1664         } else {
1665                 page = __page_cache_alloc(GFP_NOFS);
1666                 if (!page) {
1667                         err = -ENOMEM;
1668                         goto out;
1669                 }
1670                 err = __ceph_do_getattr(inode, page,
1671                                         CEPH_STAT_CAP_INLINE_DATA, true);
1672                 if (err < 0) {
1673                         /* no inline data */
1674                         if (err == -ENODATA)
1675                                 err = 0;
1676                         goto out;
1677                 }
1678                 len = err;
1679         }
1680 
1681         req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1682                                     ceph_vino(inode), 0, &len, 0, 1,
1683                                     CEPH_OSD_OP_CREATE,
1684                                     CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
1685                                     NULL, 0, 0, false);
1686         if (IS_ERR(req)) {
1687                 err = PTR_ERR(req);
1688                 goto out;
1689         }
1690 
1691         req->r_mtime = inode->i_mtime;
1692         err = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1693         if (!err)
1694                 err = ceph_osdc_wait_request(&fsc->client->osdc, req);
1695         ceph_osdc_put_request(req);
1696         if (err < 0)
1697                 goto out;
1698 
1699         req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1700                                     ceph_vino(inode), 0, &len, 1, 3,
1701                                     CEPH_OSD_OP_WRITE,
1702                                     CEPH_OSD_FLAG_ONDISK | 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 | CEPH_OSD_FLAG_ACK;
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         err2 = ceph_osdc_start_request(&fsc->client->osdc, wr_req, false);
1897 
1898         if (!err)
1899                 err = ceph_osdc_wait_request(&fsc->client->osdc, rd_req);
1900         if (!err2)
1901                 err2 = ceph_osdc_wait_request(&fsc->client->osdc, wr_req);
1902 
1903         if (err >= 0 || err == -ENOENT)
1904                 have |= POOL_READ;
1905         else if (err != -EPERM)
1906                 goto out_unlock;
1907 
1908         if (err2 == 0 || err2 == -EEXIST)
1909                 have |= POOL_WRITE;
1910         else if (err2 != -EPERM) {
1911                 err = err2;
1912                 goto out_unlock;
1913         }
1914 
1915         pool_ns_len = pool_ns ? pool_ns->len : 0;
1916         perm = kmalloc(sizeof(*perm) + pool_ns_len + 1, GFP_NOFS);
1917         if (!perm) {
1918                 err = -ENOMEM;
1919                 goto out_unlock;
1920         }
1921 
1922         perm->pool = pool;
1923         perm->perm = have;
1924         perm->pool_ns_len = pool_ns_len;
1925         if (pool_ns_len > 0)
1926                 memcpy(perm->pool_ns, pool_ns->str, pool_ns_len);
1927         perm->pool_ns[pool_ns_len] = 0;
1928 
1929         rb_link_node(&perm->node, parent, p);
1930         rb_insert_color(&perm->node, &mdsc->pool_perm_tree);
1931         err = 0;
1932 out_unlock:
1933         up_write(&mdsc->pool_perm_rwsem);
1934 
1935         ceph_osdc_put_request(rd_req);
1936         ceph_osdc_put_request(wr_req);
1937 out:
1938         if (!err)
1939                 err = have;
1940         if (pool_ns)
1941                 dout("__ceph_pool_perm_get pool %lld ns %.*s result = %d\n",
1942                      pool, (int)pool_ns->len, pool_ns->str, err);
1943         else
1944                 dout("__ceph_pool_perm_get pool %lld result = %d\n", pool, err);
1945         return err;
1946 }
1947 
1948 int ceph_pool_perm_check(struct ceph_inode_info *ci, int need)
1949 {
1950         s64 pool;
1951         struct ceph_string *pool_ns;
1952         int ret, flags;
1953 
1954         if (ci->i_vino.snap != CEPH_NOSNAP) {
1955                 /*
1956                  * Pool permission check needs to write to the first object.
1957                  * But for snapshot, head of the first object may have alread
1958                  * been deleted. Skip check to avoid creating orphan object.
1959                  */
1960                 return 0;
1961         }
1962 
1963         if (ceph_test_mount_opt(ceph_inode_to_client(&ci->vfs_inode),
1964                                 NOPOOLPERM))
1965                 return 0;
1966 
1967         spin_lock(&ci->i_ceph_lock);
1968         flags = ci->i_ceph_flags;
1969         pool = ci->i_layout.pool_id;
1970         spin_unlock(&ci->i_ceph_lock);
1971 check:
1972         if (flags & CEPH_I_POOL_PERM) {
1973                 if ((need & CEPH_CAP_FILE_RD) && !(flags & CEPH_I_POOL_RD)) {
1974                         dout("ceph_pool_perm_check pool %lld no read perm\n",
1975                              pool);
1976                         return -EPERM;
1977                 }
1978                 if ((need & CEPH_CAP_FILE_WR) && !(flags & CEPH_I_POOL_WR)) {
1979                         dout("ceph_pool_perm_check pool %lld no write perm\n",
1980                              pool);
1981                         return -EPERM;
1982                 }
1983                 return 0;
1984         }
1985 
1986         pool_ns = ceph_try_get_string(ci->i_layout.pool_ns);
1987         ret = __ceph_pool_perm_get(ci, pool, pool_ns);
1988         ceph_put_string(pool_ns);
1989         if (ret < 0)
1990                 return ret;
1991 
1992         flags = CEPH_I_POOL_PERM;
1993         if (ret & POOL_READ)
1994                 flags |= CEPH_I_POOL_RD;
1995         if (ret & POOL_WRITE)
1996                 flags |= CEPH_I_POOL_WR;
1997 
1998         spin_lock(&ci->i_ceph_lock);
1999         if (pool == ci->i_layout.pool_id &&
2000             pool_ns == rcu_dereference_raw(ci->i_layout.pool_ns)) {
2001                 ci->i_ceph_flags |= flags;
2002         } else {
2003                 pool = ci->i_layout.pool_id;
2004                 flags = ci->i_ceph_flags;
2005         }
2006         spin_unlock(&ci->i_ceph_lock);
2007         goto check;
2008 }
2009 
2010 void ceph_pool_perm_destroy(struct ceph_mds_client *mdsc)
2011 {
2012         struct ceph_pool_perm *perm;
2013         struct rb_node *n;
2014 
2015         while (!RB_EMPTY_ROOT(&mdsc->pool_perm_tree)) {
2016                 n = rb_first(&mdsc->pool_perm_tree);
2017                 perm = rb_entry(n, struct ceph_pool_perm, node);
2018                 rb_erase(n, &mdsc->pool_perm_tree);
2019                 kfree(perm);
2020         }
2021 }
2022 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp