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