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