]>
Commit | Line | Data |
---|---|---|
3d14c5d2 | 1 | #include <linux/ceph/ceph_debug.h> |
124e68e7 | 2 | |
3d14c5d2 | 3 | #include <linux/module.h> |
124e68e7 | 4 | #include <linux/sched.h> |
5a0e3ad6 | 5 | #include <linux/slab.h> |
124e68e7 | 6 | #include <linux/file.h> |
5ef50c3b | 7 | #include <linux/mount.h> |
124e68e7 SW |
8 | #include <linux/namei.h> |
9 | #include <linux/writeback.h> | |
a27bb332 | 10 | #include <linux/aio.h> |
ad7a60de | 11 | #include <linux/falloc.h> |
124e68e7 SW |
12 | |
13 | #include "super.h" | |
14 | #include "mds_client.h" | |
99ccbd22 | 15 | #include "cache.h" |
124e68e7 SW |
16 | |
17 | /* | |
18 | * Ceph file operations | |
19 | * | |
20 | * Implement basic open/close functionality, and implement | |
21 | * read/write. | |
22 | * | |
23 | * We implement three modes of file I/O: | |
24 | * - buffered uses the generic_file_aio_{read,write} helpers | |
25 | * | |
26 | * - synchronous is used when there is multi-client read/write | |
27 | * sharing, avoids the page cache, and synchronously waits for an | |
28 | * ack from the OSD. | |
29 | * | |
30 | * - direct io takes the variant of the sync path that references | |
31 | * user pages directly. | |
32 | * | |
33 | * fsync() flushes and waits on dirty pages, but just queues metadata | |
34 | * for writeback: since the MDS can recover size and mtime there is no | |
35 | * need to wait for MDS acknowledgement. | |
36 | */ | |
37 | ||
38 | ||
39 | /* | |
40 | * Prepare an open request. Preallocate ceph_cap to avoid an | |
41 | * inopportune ENOMEM later. | |
42 | */ | |
43 | static struct ceph_mds_request * | |
44 | prepare_open_request(struct super_block *sb, int flags, int create_mode) | |
45 | { | |
3d14c5d2 YS |
46 | struct ceph_fs_client *fsc = ceph_sb_to_client(sb); |
47 | struct ceph_mds_client *mdsc = fsc->mdsc; | |
124e68e7 SW |
48 | struct ceph_mds_request *req; |
49 | int want_auth = USE_ANY_MDS; | |
50 | int op = (flags & O_CREAT) ? CEPH_MDS_OP_CREATE : CEPH_MDS_OP_OPEN; | |
51 | ||
52 | if (flags & (O_WRONLY|O_RDWR|O_CREAT|O_TRUNC)) | |
53 | want_auth = USE_AUTH_MDS; | |
54 | ||
55 | req = ceph_mdsc_create_request(mdsc, op, want_auth); | |
56 | if (IS_ERR(req)) | |
57 | goto out; | |
58 | req->r_fmode = ceph_flags_to_mode(flags); | |
59 | req->r_args.open.flags = cpu_to_le32(flags); | |
60 | req->r_args.open.mode = cpu_to_le32(create_mode); | |
124e68e7 SW |
61 | out: |
62 | return req; | |
63 | } | |
64 | ||
65 | /* | |
66 | * initialize private struct file data. | |
67 | * if we fail, clean up by dropping fmode reference on the ceph_inode | |
68 | */ | |
69 | static int ceph_init_file(struct inode *inode, struct file *file, int fmode) | |
70 | { | |
71 | struct ceph_file_info *cf; | |
72 | int ret = 0; | |
99ccbd22 MT |
73 | struct ceph_inode_info *ci = ceph_inode(inode); |
74 | struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); | |
75 | struct ceph_mds_client *mdsc = fsc->mdsc; | |
124e68e7 SW |
76 | |
77 | switch (inode->i_mode & S_IFMT) { | |
78 | case S_IFREG: | |
99ccbd22 MT |
79 | /* First file open request creates the cookie, we want to keep |
80 | * this cookie around for the filetime of the inode as not to | |
81 | * have to worry about fscache register / revoke / operation | |
82 | * races. | |
83 | * | |
84 | * Also, if we know the operation is going to invalidate data | |
85 | * (non readonly) just nuke the cache right away. | |
86 | */ | |
87 | ceph_fscache_register_inode_cookie(mdsc->fsc, ci); | |
88 | if ((fmode & CEPH_FILE_MODE_WR)) | |
89 | ceph_fscache_invalidate(inode); | |
124e68e7 SW |
90 | case S_IFDIR: |
91 | dout("init_file %p %p 0%o (regular)\n", inode, file, | |
92 | inode->i_mode); | |
93 | cf = kmem_cache_alloc(ceph_file_cachep, GFP_NOFS | __GFP_ZERO); | |
94 | if (cf == NULL) { | |
95 | ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */ | |
96 | return -ENOMEM; | |
97 | } | |
98 | cf->fmode = fmode; | |
99 | cf->next_offset = 2; | |
100 | file->private_data = cf; | |
101 | BUG_ON(inode->i_fop->release != ceph_release); | |
102 | break; | |
103 | ||
104 | case S_IFLNK: | |
105 | dout("init_file %p %p 0%o (symlink)\n", inode, file, | |
106 | inode->i_mode); | |
107 | ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */ | |
108 | break; | |
109 | ||
110 | default: | |
111 | dout("init_file %p %p 0%o (special)\n", inode, file, | |
112 | inode->i_mode); | |
113 | /* | |
114 | * we need to drop the open ref now, since we don't | |
115 | * have .release set to ceph_release. | |
116 | */ | |
117 | ceph_put_fmode(ceph_inode(inode), fmode); /* clean up */ | |
118 | BUG_ON(inode->i_fop->release == ceph_release); | |
119 | ||
120 | /* call the proper open fop */ | |
121 | ret = inode->i_fop->open(inode, file); | |
122 | } | |
123 | return ret; | |
124 | } | |
125 | ||
126 | /* | |
124e68e7 SW |
127 | * If we already have the requisite capabilities, we can satisfy |
128 | * the open request locally (no need to request new caps from the | |
129 | * MDS). We do, however, need to inform the MDS (asynchronously) | |
130 | * if our wanted caps set expands. | |
131 | */ | |
132 | int ceph_open(struct inode *inode, struct file *file) | |
133 | { | |
134 | struct ceph_inode_info *ci = ceph_inode(inode); | |
3d14c5d2 YS |
135 | struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb); |
136 | struct ceph_mds_client *mdsc = fsc->mdsc; | |
124e68e7 SW |
137 | struct ceph_mds_request *req; |
138 | struct ceph_file_info *cf = file->private_data; | |
5f21c96d | 139 | struct inode *parent_inode = NULL; |
124e68e7 SW |
140 | int err; |
141 | int flags, fmode, wanted; | |
142 | ||
143 | if (cf) { | |
144 | dout("open file %p is already opened\n", file); | |
145 | return 0; | |
146 | } | |
147 | ||
148 | /* filter out O_CREAT|O_EXCL; vfs did that already. yuck. */ | |
149 | flags = file->f_flags & ~(O_CREAT|O_EXCL); | |
150 | if (S_ISDIR(inode->i_mode)) | |
151 | flags = O_DIRECTORY; /* mds likes to know */ | |
152 | ||
153 | dout("open inode %p ino %llx.%llx file %p flags %d (%d)\n", inode, | |
154 | ceph_vinop(inode), file, flags, file->f_flags); | |
155 | fmode = ceph_flags_to_mode(flags); | |
156 | wanted = ceph_caps_for_mode(fmode); | |
157 | ||
158 | /* snapped files are read-only */ | |
159 | if (ceph_snap(inode) != CEPH_NOSNAP && (file->f_mode & FMODE_WRITE)) | |
160 | return -EROFS; | |
161 | ||
162 | /* trivially open snapdir */ | |
163 | if (ceph_snap(inode) == CEPH_SNAPDIR) { | |
be655596 | 164 | spin_lock(&ci->i_ceph_lock); |
124e68e7 | 165 | __ceph_get_fmode(ci, fmode); |
be655596 | 166 | spin_unlock(&ci->i_ceph_lock); |
124e68e7 SW |
167 | return ceph_init_file(inode, file, fmode); |
168 | } | |
169 | ||
170 | /* | |
7421ab80 SW |
171 | * No need to block if we have caps on the auth MDS (for |
172 | * write) or any MDS (for read). Update wanted set | |
124e68e7 SW |
173 | * asynchronously. |
174 | */ | |
be655596 | 175 | spin_lock(&ci->i_ceph_lock); |
7421ab80 SW |
176 | if (__ceph_is_any_real_caps(ci) && |
177 | (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) { | |
124e68e7 SW |
178 | int mds_wanted = __ceph_caps_mds_wanted(ci); |
179 | int issued = __ceph_caps_issued(ci, NULL); | |
180 | ||
181 | dout("open %p fmode %d want %s issued %s using existing\n", | |
182 | inode, fmode, ceph_cap_string(wanted), | |
183 | ceph_cap_string(issued)); | |
184 | __ceph_get_fmode(ci, fmode); | |
be655596 | 185 | spin_unlock(&ci->i_ceph_lock); |
124e68e7 SW |
186 | |
187 | /* adjust wanted? */ | |
188 | if ((issued & wanted) != wanted && | |
189 | (mds_wanted & wanted) != wanted && | |
190 | ceph_snap(inode) != CEPH_SNAPDIR) | |
191 | ceph_check_caps(ci, 0, NULL); | |
192 | ||
193 | return ceph_init_file(inode, file, fmode); | |
194 | } else if (ceph_snap(inode) != CEPH_NOSNAP && | |
195 | (ci->i_snap_caps & wanted) == wanted) { | |
196 | __ceph_get_fmode(ci, fmode); | |
be655596 | 197 | spin_unlock(&ci->i_ceph_lock); |
124e68e7 SW |
198 | return ceph_init_file(inode, file, fmode); |
199 | } | |
99ccbd22 | 200 | |
be655596 | 201 | spin_unlock(&ci->i_ceph_lock); |
124e68e7 SW |
202 | |
203 | dout("open fmode %d wants %s\n", fmode, ceph_cap_string(wanted)); | |
204 | req = prepare_open_request(inode->i_sb, flags, 0); | |
205 | if (IS_ERR(req)) { | |
206 | err = PTR_ERR(req); | |
207 | goto out; | |
208 | } | |
70b666c3 SW |
209 | req->r_inode = inode; |
210 | ihold(inode); | |
99ccbd22 | 211 | |
124e68e7 | 212 | req->r_num_caps = 1; |
752c8bdc | 213 | if (flags & O_CREAT) |
5f21c96d | 214 | parent_inode = ceph_get_dentry_parent_inode(file->f_dentry); |
124e68e7 | 215 | err = ceph_mdsc_do_request(mdsc, parent_inode, req); |
5f21c96d | 216 | iput(parent_inode); |
124e68e7 SW |
217 | if (!err) |
218 | err = ceph_init_file(inode, file, req->r_fmode); | |
219 | ceph_mdsc_put_request(req); | |
220 | dout("open result=%d on %llx.%llx\n", err, ceph_vinop(inode)); | |
221 | out: | |
222 | return err; | |
223 | } | |
224 | ||
225 | ||
226 | /* | |
5ef50c3b SW |
227 | * Do a lookup + open with a single request. If we get a non-existent |
228 | * file or symlink, return 1 so the VFS can retry. | |
124e68e7 | 229 | */ |
5ef50c3b | 230 | int ceph_atomic_open(struct inode *dir, struct dentry *dentry, |
30d90494 | 231 | struct file *file, unsigned flags, umode_t mode, |
d9585277 | 232 | int *opened) |
124e68e7 | 233 | { |
3d14c5d2 YS |
234 | struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb); |
235 | struct ceph_mds_client *mdsc = fsc->mdsc; | |
124e68e7 | 236 | struct ceph_mds_request *req; |
5ef50c3b | 237 | struct dentry *dn; |
b1ee94aa | 238 | struct ceph_acls_info acls = {}; |
124e68e7 | 239 | int err; |
124e68e7 | 240 | |
5ef50c3b SW |
241 | dout("atomic_open %p dentry %p '%.*s' %s flags %d mode 0%o\n", |
242 | dir, dentry, dentry->d_name.len, dentry->d_name.name, | |
243 | d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode); | |
244 | ||
245 | if (dentry->d_name.len > NAME_MAX) | |
246 | return -ENAMETOOLONG; | |
247 | ||
248 | err = ceph_init_dentry(dentry); | |
249 | if (err < 0) | |
250 | return err; | |
124e68e7 | 251 | |
b1ee94aa YZ |
252 | if (flags & O_CREAT) { |
253 | err = ceph_pre_init_acls(dir, &mode, &acls); | |
254 | if (err < 0) | |
255 | return err; | |
256 | } | |
257 | ||
124e68e7 SW |
258 | /* do the open */ |
259 | req = prepare_open_request(dir->i_sb, flags, mode); | |
b1ee94aa YZ |
260 | if (IS_ERR(req)) { |
261 | err = PTR_ERR(req); | |
262 | goto out_acl; | |
263 | } | |
124e68e7 SW |
264 | req->r_dentry = dget(dentry); |
265 | req->r_num_caps = 2; | |
266 | if (flags & O_CREAT) { | |
267 | req->r_dentry_drop = CEPH_CAP_FILE_SHARED; | |
268 | req->r_dentry_unless = CEPH_CAP_FILE_EXCL; | |
b1ee94aa YZ |
269 | if (acls.pagelist) { |
270 | req->r_pagelist = acls.pagelist; | |
271 | acls.pagelist = NULL; | |
272 | } | |
124e68e7 SW |
273 | } |
274 | req->r_locked_dir = dir; /* caller holds dir->i_mutex */ | |
acda7657 SW |
275 | err = ceph_mdsc_do_request(mdsc, |
276 | (flags & (O_CREAT|O_TRUNC)) ? dir : NULL, | |
277 | req); | |
79aec984 | 278 | if (err) |
b1ee94aa | 279 | goto out_req; |
79aec984 | 280 | |
468640e3 | 281 | err = ceph_handle_snapdir(req, dentry, err); |
5ef50c3b | 282 | if (err == 0 && (flags & O_CREAT) && !req->r_reply_info.head->is_dentry) |
124e68e7 | 283 | err = ceph_handle_notrace_create(dir, dentry); |
2d83bde9 | 284 | |
5ef50c3b SW |
285 | if (d_unhashed(dentry)) { |
286 | dn = ceph_finish_lookup(req, dentry, err); | |
287 | if (IS_ERR(dn)) | |
288 | err = PTR_ERR(dn); | |
289 | } else { | |
290 | /* we were given a hashed negative dentry */ | |
291 | dn = NULL; | |
292 | } | |
293 | if (err) | |
b1ee94aa | 294 | goto out_req; |
5ef50c3b SW |
295 | if (dn || dentry->d_inode == NULL || S_ISLNK(dentry->d_inode->i_mode)) { |
296 | /* make vfs retry on splice, ENOENT, or symlink */ | |
297 | dout("atomic_open finish_no_open on dn %p\n", dn); | |
298 | err = finish_no_open(file, dn); | |
299 | } else { | |
300 | dout("atomic_open finish_open on dn %p\n", dn); | |
6e8575fa | 301 | if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) { |
b1ee94aa | 302 | ceph_init_inode_acls(dentry->d_inode, &acls); |
6e8575fa SL |
303 | *opened |= FILE_CREATED; |
304 | } | |
5ef50c3b SW |
305 | err = finish_open(file, dentry, ceph_open, opened); |
306 | } | |
b1ee94aa | 307 | out_req: |
ab866549 YZ |
308 | if (!req->r_err && req->r_target_inode) |
309 | ceph_put_fmode(ceph_inode(req->r_target_inode), req->r_fmode); | |
5ef50c3b | 310 | ceph_mdsc_put_request(req); |
b1ee94aa YZ |
311 | out_acl: |
312 | ceph_release_acls_info(&acls); | |
5ef50c3b | 313 | dout("atomic_open result=%d\n", err); |
d9585277 | 314 | return err; |
124e68e7 SW |
315 | } |
316 | ||
317 | int ceph_release(struct inode *inode, struct file *file) | |
318 | { | |
319 | struct ceph_inode_info *ci = ceph_inode(inode); | |
320 | struct ceph_file_info *cf = file->private_data; | |
321 | ||
322 | dout("release inode %p file %p\n", inode, file); | |
323 | ceph_put_fmode(ci, cf->fmode); | |
324 | if (cf->last_readdir) | |
325 | ceph_mdsc_put_request(cf->last_readdir); | |
326 | kfree(cf->last_name); | |
327 | kfree(cf->dir_info); | |
328 | dput(cf->dentry); | |
329 | kmem_cache_free(ceph_file_cachep, cf); | |
195d3ce2 SW |
330 | |
331 | /* wake up anyone waiting for caps on this inode */ | |
03066f23 | 332 | wake_up_all(&ci->i_cap_wq); |
124e68e7 SW |
333 | return 0; |
334 | } | |
335 | ||
124e68e7 SW |
336 | /* |
337 | * Read a range of bytes striped over one or more objects. Iterate over | |
338 | * objects we stripe over. (That's not atomic, but good enough for now.) | |
339 | * | |
340 | * If we get a short result from the OSD, check against i_size; we need to | |
341 | * only return a short read to the caller if we hit EOF. | |
342 | */ | |
343 | static int striped_read(struct inode *inode, | |
344 | u64 off, u64 len, | |
6a026589 | 345 | struct page **pages, int num_pages, |
c3cd6283 | 346 | int *checkeof, bool o_direct, |
ab226e21 | 347 | unsigned long buf_align) |
124e68e7 | 348 | { |
3d14c5d2 | 349 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
124e68e7 | 350 | struct ceph_inode_info *ci = ceph_inode(inode); |
688bac46 | 351 | u64 pos, this_len, left; |
b7495fc2 | 352 | int io_align, page_align; |
688bac46 | 353 | int pages_left; |
124e68e7 SW |
354 | int read; |
355 | struct page **page_pos; | |
356 | int ret; | |
357 | bool hit_stripe, was_short; | |
358 | ||
359 | /* | |
360 | * we may need to do multiple reads. not atomic, unfortunately. | |
361 | */ | |
362 | pos = off; | |
363 | left = len; | |
364 | page_pos = pages; | |
365 | pages_left = num_pages; | |
366 | read = 0; | |
b7495fc2 | 367 | io_align = off & ~PAGE_MASK; |
124e68e7 SW |
368 | |
369 | more: | |
c3cd6283 | 370 | if (o_direct) |
ab226e21 | 371 | page_align = (pos - io_align + buf_align) & ~PAGE_MASK; |
b7495fc2 SW |
372 | else |
373 | page_align = pos & ~PAGE_MASK; | |
124e68e7 | 374 | this_len = left; |
3d14c5d2 | 375 | ret = ceph_osdc_readpages(&fsc->client->osdc, ceph_vino(inode), |
124e68e7 SW |
376 | &ci->i_layout, pos, &this_len, |
377 | ci->i_truncate_seq, | |
378 | ci->i_truncate_size, | |
b7495fc2 | 379 | page_pos, pages_left, page_align); |
124e68e7 SW |
380 | if (ret == -ENOENT) |
381 | ret = 0; | |
0e98728f SW |
382 | hit_stripe = this_len < left; |
383 | was_short = ret >= 0 && ret < this_len; | |
688bac46 | 384 | dout("striped_read %llu~%llu (read %u) got %d%s%s\n", pos, left, read, |
124e68e7 SW |
385 | ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : ""); |
386 | ||
02ae66d8 | 387 | if (ret >= 0) { |
388 | int didpages; | |
389 | if (was_short && (pos + ret < inode->i_size)) { | |
390 | u64 tmp = min(this_len - ret, | |
391 | inode->i_size - pos - ret); | |
392 | dout(" zero gap %llu to %llu\n", | |
393 | pos + ret, pos + ret + tmp); | |
394 | ceph_zero_page_vector_range(page_align + read + ret, | |
395 | tmp, pages); | |
396 | ret += tmp; | |
124e68e7 | 397 | } |
02ae66d8 | 398 | |
399 | didpages = (page_align + ret) >> PAGE_CACHE_SHIFT; | |
124e68e7 SW |
400 | pos += ret; |
401 | read = pos - off; | |
402 | left -= ret; | |
403 | page_pos += didpages; | |
404 | pages_left -= didpages; | |
405 | ||
02ae66d8 | 406 | /* hit stripe and need continue*/ |
407 | if (left && hit_stripe && pos < inode->i_size) | |
124e68e7 SW |
408 | goto more; |
409 | } | |
410 | ||
ee7289bf | 411 | if (read > 0) { |
02ae66d8 | 412 | ret = read; |
c3cd6283 SW |
413 | /* did we bounce off eof? */ |
414 | if (pos + left > inode->i_size) | |
415 | *checkeof = 1; | |
124e68e7 SW |
416 | } |
417 | ||
124e68e7 SW |
418 | dout("striped_read returns %d\n", ret); |
419 | return ret; | |
420 | } | |
421 | ||
422 | /* | |
423 | * Completely synchronous read and write methods. Direct from __user | |
424 | * buffer to osd, or directly to user pages (if O_DIRECT). | |
425 | * | |
426 | * If the read spans object boundary, just do multiple reads. | |
427 | */ | |
8eb4efb0 | 428 | static ssize_t ceph_sync_read(struct kiocb *iocb, struct iov_iter *i, |
429 | int *checkeof) | |
124e68e7 | 430 | { |
8eb4efb0 | 431 | struct file *file = iocb->ki_filp; |
496ad9aa | 432 | struct inode *inode = file_inode(file); |
124e68e7 | 433 | struct page **pages; |
8eb4efb0 | 434 | u64 off = iocb->ki_pos; |
ab226e21 | 435 | int num_pages, ret; |
2b777c9d | 436 | size_t len = iov_iter_count(i); |
124e68e7 | 437 | |
8eb4efb0 | 438 | dout("sync_read on file %p %llu~%u %s\n", file, off, |
439 | (unsigned)len, | |
124e68e7 | 440 | (file->f_flags & O_DIRECT) ? "O_DIRECT" : ""); |
d0d0db22 YZ |
441 | |
442 | if (!len) | |
443 | return 0; | |
e98b6fed SW |
444 | /* |
445 | * flush any page cache pages in this range. this | |
446 | * will make concurrent normal and sync io slow, | |
447 | * but it will at least behave sensibly when they are | |
448 | * in sequence. | |
449 | */ | |
8eb4efb0 | 450 | ret = filemap_write_and_wait_range(inode->i_mapping, off, |
451 | off + len); | |
29065a51 | 452 | if (ret < 0) |
8eb4efb0 | 453 | return ret; |
29065a51 | 454 | |
8eb4efb0 | 455 | if (file->f_flags & O_DIRECT) { |
456 | while (iov_iter_count(i)) { | |
2b777c9d AV |
457 | size_t start; |
458 | ssize_t n; | |
8eb4efb0 | 459 | |
2b777c9d AV |
460 | n = iov_iter_get_pages_alloc(i, &pages, INT_MAX, &start); |
461 | if (n < 0) | |
462 | return n; | |
8eb4efb0 | 463 | |
2b777c9d AV |
464 | num_pages = (n + start + PAGE_SIZE - 1) / PAGE_SIZE; |
465 | ||
466 | ret = striped_read(inode, off, n, | |
8eb4efb0 | 467 | pages, num_pages, checkeof, |
2b777c9d AV |
468 | 1, start); |
469 | ||
8eb4efb0 | 470 | ceph_put_page_vector(pages, num_pages, true); |
471 | ||
472 | if (ret <= 0) | |
473 | break; | |
474 | off += ret; | |
475 | iov_iter_advance(i, ret); | |
2b777c9d | 476 | if (ret < n) |
8eb4efb0 | 477 | break; |
478 | } | |
479 | } else { | |
480 | num_pages = calc_pages_for(off, len); | |
481 | pages = ceph_alloc_page_vector(num_pages, GFP_NOFS); | |
482 | if (IS_ERR(pages)) | |
483 | return PTR_ERR(pages); | |
484 | ret = striped_read(inode, off, len, pages, | |
485 | num_pages, checkeof, 0, 0); | |
486 | if (ret > 0) { | |
487 | int l, k = 0; | |
2b777c9d | 488 | size_t left = ret; |
8eb4efb0 | 489 | |
490 | while (left) { | |
5aaa432a YZ |
491 | size_t page_off = off & ~PAGE_MASK; |
492 | size_t copy = min_t(size_t, | |
493 | PAGE_SIZE - page_off, left); | |
494 | l = copy_page_to_iter(pages[k++], page_off, | |
495 | copy, i); | |
2b777c9d AV |
496 | off += l; |
497 | left -= l; | |
498 | if (l < copy) | |
8eb4efb0 | 499 | break; |
500 | } | |
501 | } | |
502 | ceph_release_page_vector(pages, num_pages); | |
503 | } | |
124e68e7 | 504 | |
8eb4efb0 | 505 | if (off > iocb->ki_pos) { |
506 | ret = off - iocb->ki_pos; | |
507 | iocb->ki_pos = off; | |
508 | } | |
124e68e7 | 509 | |
124e68e7 SW |
510 | dout("sync_read result %d\n", ret); |
511 | return ret; | |
512 | } | |
513 | ||
514 | /* | |
26be8808 AE |
515 | * Write commit request unsafe callback, called to tell us when a |
516 | * request is unsafe (that is, in flight--has been handed to the | |
517 | * messenger to send to its target osd). It is called again when | |
518 | * we've received a response message indicating the request is | |
519 | * "safe" (its CEPH_OSD_FLAG_ONDISK flag is set), or when a request | |
520 | * is completed early (and unsuccessfully) due to a timeout or | |
521 | * interrupt. | |
522 | * | |
523 | * This is used if we requested both an ACK and ONDISK commit reply | |
524 | * from the OSD. | |
124e68e7 | 525 | */ |
26be8808 | 526 | static void ceph_sync_write_unsafe(struct ceph_osd_request *req, bool unsafe) |
124e68e7 SW |
527 | { |
528 | struct ceph_inode_info *ci = ceph_inode(req->r_inode); | |
529 | ||
26be8808 AE |
530 | dout("%s %p tid %llu %ssafe\n", __func__, req, req->r_tid, |
531 | unsafe ? "un" : ""); | |
532 | if (unsafe) { | |
533 | ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR); | |
534 | spin_lock(&ci->i_unsafe_lock); | |
535 | list_add_tail(&req->r_unsafe_item, | |
536 | &ci->i_unsafe_writes); | |
537 | spin_unlock(&ci->i_unsafe_lock); | |
538 | } else { | |
539 | spin_lock(&ci->i_unsafe_lock); | |
540 | list_del_init(&req->r_unsafe_item); | |
541 | spin_unlock(&ci->i_unsafe_lock); | |
542 | ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR); | |
543 | } | |
124e68e7 SW |
544 | } |
545 | ||
e8344e66 | 546 | |
124e68e7 | 547 | /* |
e8344e66 | 548 | * Synchronous write, straight from __user pointer or user pages. |
124e68e7 SW |
549 | * |
550 | * If write spans object boundary, just do multiple writes. (For a | |
551 | * correct atomic write, we should e.g. take write locks on all | |
552 | * objects, rollback on failure, etc.) | |
553 | */ | |
e8344e66 | 554 | static ssize_t |
06fee30f | 555 | ceph_sync_direct_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos) |
124e68e7 | 556 | { |
e8344e66 | 557 | struct file *file = iocb->ki_filp; |
496ad9aa | 558 | struct inode *inode = file_inode(file); |
124e68e7 | 559 | struct ceph_inode_info *ci = ceph_inode(inode); |
3d14c5d2 | 560 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); |
acead002 AE |
561 | struct ceph_snap_context *snapc; |
562 | struct ceph_vino vino; | |
124e68e7 SW |
563 | struct ceph_osd_request *req; |
564 | struct page **pages; | |
565 | int num_pages; | |
124e68e7 SW |
566 | int written = 0; |
567 | int flags; | |
124e68e7 SW |
568 | int check_caps = 0; |
569 | int ret; | |
570 | struct timespec mtime = CURRENT_TIME; | |
4908b822 | 571 | size_t count = iov_iter_count(from); |
124e68e7 | 572 | |
496ad9aa | 573 | if (ceph_snap(file_inode(file)) != CEPH_NOSNAP) |
124e68e7 SW |
574 | return -EROFS; |
575 | ||
e8344e66 | 576 | dout("sync_direct_write on file %p %lld~%u\n", file, pos, |
577 | (unsigned)count); | |
124e68e7 | 578 | |
e8344e66 | 579 | ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count); |
29065a51 YS |
580 | if (ret < 0) |
581 | return ret; | |
582 | ||
583 | ret = invalidate_inode_pages2_range(inode->i_mapping, | |
584 | pos >> PAGE_CACHE_SHIFT, | |
e8344e66 | 585 | (pos + count) >> PAGE_CACHE_SHIFT); |
29065a51 YS |
586 | if (ret < 0) |
587 | dout("invalidate_inode_pages2_range returned %d\n", ret); | |
588 | ||
124e68e7 SW |
589 | flags = CEPH_OSD_FLAG_ORDERSNAP | |
590 | CEPH_OSD_FLAG_ONDISK | | |
591 | CEPH_OSD_FLAG_WRITE; | |
124e68e7 | 592 | |
4908b822 AV |
593 | while (iov_iter_count(from) > 0) { |
594 | u64 len = iov_iter_single_seg_count(from); | |
64c31311 AV |
595 | size_t start; |
596 | ssize_t n; | |
e8344e66 | 597 | |
598 | snapc = ci->i_snap_realm->cached_context; | |
599 | vino = ceph_vino(inode); | |
600 | req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, | |
715e4cd4 | 601 | vino, pos, &len, 0, |
e8344e66 | 602 | 2,/*include a 'startsync' command*/ |
603 | CEPH_OSD_OP_WRITE, flags, snapc, | |
604 | ci->i_truncate_seq, | |
605 | ci->i_truncate_size, | |
606 | false); | |
607 | if (IS_ERR(req)) { | |
608 | ret = PTR_ERR(req); | |
eab87235 | 609 | break; |
e8344e66 | 610 | } |
124e68e7 | 611 | |
715e4cd4 YZ |
612 | osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC); |
613 | ||
4908b822 | 614 | n = iov_iter_get_pages_alloc(from, &pages, len, &start); |
64c31311 AV |
615 | if (unlikely(n < 0)) { |
616 | ret = n; | |
617 | ceph_osdc_put_request(req); | |
618 | break; | |
124e68e7 SW |
619 | } |
620 | ||
64c31311 | 621 | num_pages = (n + start + PAGE_SIZE - 1) / PAGE_SIZE; |
124e68e7 SW |
622 | /* |
623 | * throw out any page cache pages in this range. this | |
624 | * may block. | |
625 | */ | |
213c99ee | 626 | truncate_inode_pages_range(inode->i_mapping, pos, |
64c31311 AV |
627 | (pos+n) | (PAGE_CACHE_SIZE-1)); |
628 | osd_req_op_extent_osd_data_pages(req, 0, pages, n, start, | |
e8344e66 | 629 | false, false); |
630 | ||
631 | /* BUG_ON(vino.snap != CEPH_NOSNAP); */ | |
632 | ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime); | |
633 | ||
634 | ret = ceph_osdc_start_request(&fsc->client->osdc, req, false); | |
635 | if (!ret) | |
636 | ret = ceph_osdc_wait_request(&fsc->client->osdc, req); | |
637 | ||
638 | ceph_put_page_vector(pages, num_pages, false); | |
639 | ||
e8344e66 | 640 | ceph_osdc_put_request(req); |
64c31311 | 641 | if (ret) |
e8344e66 | 642 | break; |
64c31311 AV |
643 | pos += n; |
644 | written += n; | |
4908b822 | 645 | iov_iter_advance(from, n); |
64c31311 AV |
646 | |
647 | if (pos > i_size_read(inode)) { | |
648 | check_caps = ceph_inode_set_size(inode, pos); | |
649 | if (check_caps) | |
650 | ceph_check_caps(ceph_inode(inode), | |
651 | CHECK_CAPS_AUTHONLY, | |
652 | NULL); | |
653 | } | |
e8344e66 | 654 | } |
655 | ||
656 | if (ret != -EOLDSNAPC && written > 0) { | |
657 | iocb->ki_pos = pos; | |
658 | ret = written; | |
659 | } | |
660 | return ret; | |
661 | } | |
662 | ||
663 | ||
664 | /* | |
665 | * Synchronous write, straight from __user pointer or user pages. | |
666 | * | |
667 | * If write spans object boundary, just do multiple writes. (For a | |
668 | * correct atomic write, we should e.g. take write locks on all | |
669 | * objects, rollback on failure, etc.) | |
670 | */ | |
06fee30f YZ |
671 | static ssize_t |
672 | ceph_sync_write(struct kiocb *iocb, struct iov_iter *from, loff_t pos) | |
e8344e66 | 673 | { |
674 | struct file *file = iocb->ki_filp; | |
675 | struct inode *inode = file_inode(file); | |
676 | struct ceph_inode_info *ci = ceph_inode(inode); | |
677 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); | |
678 | struct ceph_snap_context *snapc; | |
679 | struct ceph_vino vino; | |
680 | struct ceph_osd_request *req; | |
681 | struct page **pages; | |
682 | u64 len; | |
683 | int num_pages; | |
684 | int written = 0; | |
685 | int flags; | |
686 | int check_caps = 0; | |
687 | int ret; | |
688 | struct timespec mtime = CURRENT_TIME; | |
4908b822 | 689 | size_t count = iov_iter_count(from); |
e8344e66 | 690 | |
691 | if (ceph_snap(file_inode(file)) != CEPH_NOSNAP) | |
692 | return -EROFS; | |
693 | ||
694 | dout("sync_write on file %p %lld~%u\n", file, pos, (unsigned)count); | |
695 | ||
696 | ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count); | |
697 | if (ret < 0) | |
698 | return ret; | |
699 | ||
700 | ret = invalidate_inode_pages2_range(inode->i_mapping, | |
701 | pos >> PAGE_CACHE_SHIFT, | |
702 | (pos + count) >> PAGE_CACHE_SHIFT); | |
703 | if (ret < 0) | |
704 | dout("invalidate_inode_pages2_range returned %d\n", ret); | |
705 | ||
706 | flags = CEPH_OSD_FLAG_ORDERSNAP | | |
707 | CEPH_OSD_FLAG_ONDISK | | |
708 | CEPH_OSD_FLAG_WRITE | | |
709 | CEPH_OSD_FLAG_ACK; | |
710 | ||
4908b822 | 711 | while ((len = iov_iter_count(from)) > 0) { |
e8344e66 | 712 | size_t left; |
713 | int n; | |
714 | ||
715 | snapc = ci->i_snap_realm->cached_context; | |
716 | vino = ceph_vino(inode); | |
717 | req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, | |
715e4cd4 | 718 | vino, pos, &len, 0, 1, |
e8344e66 | 719 | CEPH_OSD_OP_WRITE, flags, snapc, |
720 | ci->i_truncate_seq, | |
721 | ci->i_truncate_size, | |
722 | false); | |
723 | if (IS_ERR(req)) { | |
724 | ret = PTR_ERR(req); | |
eab87235 | 725 | break; |
e8344e66 | 726 | } |
727 | ||
728 | /* | |
729 | * write from beginning of first page, | |
730 | * regardless of io alignment | |
731 | */ | |
732 | num_pages = (len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT; | |
733 | ||
34d23762 | 734 | pages = ceph_alloc_page_vector(num_pages, GFP_NOFS); |
124e68e7 SW |
735 | if (IS_ERR(pages)) { |
736 | ret = PTR_ERR(pages); | |
737 | goto out; | |
738 | } | |
e8344e66 | 739 | |
740 | left = len; | |
741 | for (n = 0; n < num_pages; n++) { | |
125d725c | 742 | size_t plen = min_t(size_t, left, PAGE_SIZE); |
4908b822 | 743 | ret = copy_page_from_iter(pages[n], 0, plen, from); |
e8344e66 | 744 | if (ret != plen) { |
745 | ret = -EFAULT; | |
746 | break; | |
747 | } | |
748 | left -= ret; | |
e8344e66 | 749 | } |
750 | ||
124e68e7 SW |
751 | if (ret < 0) { |
752 | ceph_release_page_vector(pages, num_pages); | |
753 | goto out; | |
754 | } | |
755 | ||
e8344e66 | 756 | /* get a second commit callback */ |
757 | req->r_unsafe_callback = ceph_sync_write_unsafe; | |
758 | req->r_inode = inode; | |
124e68e7 | 759 | |
e8344e66 | 760 | osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0, |
761 | false, true); | |
02ee07d3 | 762 | |
e8344e66 | 763 | /* BUG_ON(vino.snap != CEPH_NOSNAP); */ |
764 | ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime); | |
124e68e7 | 765 | |
e8344e66 | 766 | ret = ceph_osdc_start_request(&fsc->client->osdc, req, false); |
767 | if (!ret) | |
768 | ret = ceph_osdc_wait_request(&fsc->client->osdc, req); | |
124e68e7 SW |
769 | |
770 | out: | |
e8344e66 | 771 | ceph_osdc_put_request(req); |
772 | if (ret == 0) { | |
773 | pos += len; | |
774 | written += len; | |
775 | ||
776 | if (pos > i_size_read(inode)) { | |
777 | check_caps = ceph_inode_set_size(inode, pos); | |
778 | if (check_caps) | |
779 | ceph_check_caps(ceph_inode(inode), | |
780 | CHECK_CAPS_AUTHONLY, | |
781 | NULL); | |
782 | } | |
783 | } else | |
784 | break; | |
785 | } | |
124e68e7 | 786 | |
e8344e66 | 787 | if (ret != -EOLDSNAPC && written > 0) { |
124e68e7 | 788 | ret = written; |
e8344e66 | 789 | iocb->ki_pos = pos; |
124e68e7 SW |
790 | } |
791 | return ret; | |
792 | } | |
793 | ||
794 | /* | |
795 | * Wrap generic_file_aio_read with checks for cap bits on the inode. | |
796 | * Atomically grab references, so that those bits are not released | |
797 | * back to the MDS mid-read. | |
798 | * | |
799 | * Hmm, the sync read case isn't actually async... should it be? | |
800 | */ | |
3644424d | 801 | static ssize_t ceph_read_iter(struct kiocb *iocb, struct iov_iter *to) |
124e68e7 SW |
802 | { |
803 | struct file *filp = iocb->ki_filp; | |
2962507c | 804 | struct ceph_file_info *fi = filp->private_data; |
8eb4efb0 | 805 | size_t len = iocb->ki_nbytes; |
496ad9aa | 806 | struct inode *inode = file_inode(filp); |
124e68e7 | 807 | struct ceph_inode_info *ci = ceph_inode(inode); |
3738daa6 | 808 | struct page *pinned_page = NULL; |
124e68e7 | 809 | ssize_t ret; |
2962507c | 810 | int want, got = 0; |
6a026589 | 811 | int checkeof = 0, read = 0; |
124e68e7 | 812 | |
6a026589 | 813 | again: |
8eb4efb0 | 814 | dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n", |
815 | inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, inode); | |
816 | ||
2962507c SW |
817 | if (fi->fmode & CEPH_FILE_MODE_LAZY) |
818 | want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO; | |
819 | else | |
820 | want = CEPH_CAP_FILE_CACHE; | |
3738daa6 | 821 | ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, -1, &got, &pinned_page); |
124e68e7 | 822 | if (ret < 0) |
8eb4efb0 | 823 | return ret; |
124e68e7 | 824 | |
2962507c | 825 | if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 || |
124e68e7 | 826 | (iocb->ki_filp->f_flags & O_DIRECT) || |
8eb4efb0 | 827 | (fi->flags & CEPH_F_SYNC)) { |
8eb4efb0 | 828 | |
829 | dout("aio_sync_read %p %llx.%llx %llu~%u got cap refs on %s\n", | |
830 | inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, | |
831 | ceph_cap_string(got)); | |
832 | ||
124e68e7 | 833 | /* hmm, this isn't really async... */ |
3644424d | 834 | ret = ceph_sync_read(iocb, to, &checkeof); |
8eb4efb0 | 835 | } else { |
8eb4efb0 | 836 | dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n", |
3644424d | 837 | inode, ceph_vinop(inode), iocb->ki_pos, (unsigned)len, |
8eb4efb0 | 838 | ceph_cap_string(got)); |
124e68e7 | 839 | |
3644424d | 840 | ret = generic_file_read_iter(iocb, to); |
8eb4efb0 | 841 | } |
124e68e7 SW |
842 | dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n", |
843 | inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret); | |
3738daa6 YZ |
844 | if (pinned_page) { |
845 | page_cache_release(pinned_page); | |
846 | pinned_page = NULL; | |
847 | } | |
124e68e7 | 848 | ceph_put_cap_refs(ci, got); |
6a026589 SW |
849 | |
850 | if (checkeof && ret >= 0) { | |
508b32d8 | 851 | int statret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false); |
6a026589 SW |
852 | |
853 | /* hit EOF or hole? */ | |
8eb4efb0 | 854 | if (statret == 0 && iocb->ki_pos < inode->i_size && |
855 | ret < len) { | |
856 | dout("sync_read hit hole, ppos %lld < size %lld" | |
857 | ", reading more\n", iocb->ki_pos, | |
858 | inode->i_size); | |
859 | ||
6a026589 | 860 | read += ret; |
6a026589 SW |
861 | len -= ret; |
862 | checkeof = 0; | |
863 | goto again; | |
864 | } | |
865 | } | |
8eb4efb0 | 866 | |
6a026589 SW |
867 | if (ret >= 0) |
868 | ret += read; | |
869 | ||
124e68e7 SW |
870 | return ret; |
871 | } | |
872 | ||
873 | /* | |
874 | * Take cap references to avoid releasing caps to MDS mid-write. | |
875 | * | |
876 | * If we are synchronous, and write with an old snap context, the OSD | |
877 | * may return EOLDSNAPC. In that case, retry the write.. _after_ | |
878 | * dropping our cap refs and allowing the pending snap to logically | |
879 | * complete _before_ this write occurs. | |
880 | * | |
881 | * If we are near ENOSPC, write synchronously. | |
882 | */ | |
4908b822 | 883 | static ssize_t ceph_write_iter(struct kiocb *iocb, struct iov_iter *from) |
124e68e7 SW |
884 | { |
885 | struct file *file = iocb->ki_filp; | |
33caad32 | 886 | struct ceph_file_info *fi = file->private_data; |
496ad9aa | 887 | struct inode *inode = file_inode(file); |
124e68e7 | 888 | struct ceph_inode_info *ci = ceph_inode(inode); |
3d14c5d2 YS |
889 | struct ceph_osd_client *osdc = |
890 | &ceph_sb_to_client(inode->i_sb)->client->osdc; | |
4908b822 | 891 | ssize_t count = iov_iter_count(from), written = 0; |
03d254ed | 892 | int err, want, got; |
4908b822 | 893 | loff_t pos = iocb->ki_pos; |
124e68e7 SW |
894 | |
895 | if (ceph_snap(inode) != CEPH_NOSNAP) | |
896 | return -EROFS; | |
897 | ||
03d254ed | 898 | mutex_lock(&inode->i_mutex); |
03d254ed | 899 | |
03d254ed YZ |
900 | /* We can write back this queue in page reclaim */ |
901 | current->backing_dev_info = file->f_mapping->backing_dev_info; | |
902 | ||
903 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); | |
904 | if (err) | |
905 | goto out; | |
906 | ||
907 | if (count == 0) | |
908 | goto out; | |
4908b822 | 909 | iov_iter_truncate(from, count); |
03d254ed YZ |
910 | |
911 | err = file_remove_suid(file); | |
912 | if (err) | |
913 | goto out; | |
914 | ||
915 | err = file_update_time(file); | |
916 | if (err) | |
917 | goto out; | |
918 | ||
124e68e7 | 919 | retry_snap: |
6070e0c1 | 920 | if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) { |
03d254ed | 921 | err = -ENOSPC; |
6070e0c1 YZ |
922 | goto out; |
923 | } | |
03d254ed | 924 | |
ac7f29bf | 925 | dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n", |
03d254ed | 926 | inode, ceph_vinop(inode), pos, count, inode->i_size); |
7971bd92 SW |
927 | if (fi->fmode & CEPH_FILE_MODE_LAZY) |
928 | want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO; | |
929 | else | |
930 | want = CEPH_CAP_FILE_BUFFER; | |
03d254ed | 931 | got = 0; |
3738daa6 YZ |
932 | err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, pos + count, |
933 | &got, NULL); | |
03d254ed | 934 | if (err < 0) |
37505d57 | 935 | goto out; |
124e68e7 | 936 | |
ac7f29bf | 937 | dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n", |
03d254ed | 938 | inode, ceph_vinop(inode), pos, count, ceph_cap_string(got)); |
7971bd92 SW |
939 | |
940 | if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 || | |
e8344e66 | 941 | (file->f_flags & O_DIRECT) || (fi->flags & CEPH_F_SYNC)) { |
4908b822 | 942 | struct iov_iter data; |
37505d57 | 943 | mutex_unlock(&inode->i_mutex); |
4908b822 AV |
944 | /* we might need to revert back to that point */ |
945 | data = *from; | |
e8344e66 | 946 | if (file->f_flags & O_DIRECT) |
06fee30f | 947 | written = ceph_sync_direct_write(iocb, &data, pos); |
e8344e66 | 948 | else |
06fee30f | 949 | written = ceph_sync_write(iocb, &data, pos); |
0e5dd45c | 950 | if (written == -EOLDSNAPC) { |
951 | dout("aio_write %p %llx.%llx %llu~%u" | |
952 | "got EOLDSNAPC, retrying\n", | |
953 | inode, ceph_vinop(inode), | |
4908b822 | 954 | pos, (unsigned)count); |
0e5dd45c | 955 | mutex_lock(&inode->i_mutex); |
0e5dd45c | 956 | goto retry_snap; |
957 | } | |
4908b822 AV |
958 | if (written > 0) |
959 | iov_iter_advance(from, written); | |
7971bd92 | 960 | } else { |
32d3e148 | 961 | loff_t old_size = inode->i_size; |
b0d7c223 YZ |
962 | /* |
963 | * No need to acquire the i_truncate_mutex. Because | |
964 | * the MDS revokes Fwb caps before sending truncate | |
965 | * message to us. We can't get Fwb cap while there | |
966 | * are pending vmtruncate. So write and vmtruncate | |
967 | * can not run at the same time | |
968 | */ | |
4908b822 | 969 | written = generic_perform_write(file, from, pos); |
aec605f4 AV |
970 | if (likely(written >= 0)) |
971 | iocb->ki_pos = pos + written; | |
32d3e148 YW |
972 | if (inode->i_size > old_size) |
973 | ceph_fscache_update_objectsize(inode); | |
6070e0c1 | 974 | mutex_unlock(&inode->i_mutex); |
7971bd92 | 975 | } |
d8de9ab6 | 976 | |
03d254ed | 977 | if (written >= 0) { |
fca65b4a | 978 | int dirty; |
be655596 | 979 | spin_lock(&ci->i_ceph_lock); |
fca65b4a | 980 | dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR); |
be655596 | 981 | spin_unlock(&ci->i_ceph_lock); |
fca65b4a SW |
982 | if (dirty) |
983 | __mark_inode_dirty(inode, dirty); | |
124e68e7 | 984 | } |
7971bd92 | 985 | |
124e68e7 | 986 | dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n", |
4908b822 | 987 | inode, ceph_vinop(inode), pos, (unsigned)count, |
7971bd92 | 988 | ceph_cap_string(got)); |
124e68e7 | 989 | ceph_put_cap_refs(ci, got); |
7971bd92 | 990 | |
03d254ed | 991 | if (written >= 0 && |
6070e0c1 YZ |
992 | ((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host) || |
993 | ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL))) { | |
03d254ed | 994 | err = vfs_fsync_range(file, pos, pos + written - 1, 1); |
6070e0c1 | 995 | if (err < 0) |
03d254ed | 996 | written = err; |
6070e0c1 | 997 | } |
03d254ed | 998 | |
2f75e9e1 SW |
999 | goto out_unlocked; |
1000 | ||
03d254ed | 1001 | out: |
2f75e9e1 SW |
1002 | mutex_unlock(&inode->i_mutex); |
1003 | out_unlocked: | |
03d254ed | 1004 | current->backing_dev_info = NULL; |
03d254ed | 1005 | return written ? written : err; |
124e68e7 SW |
1006 | } |
1007 | ||
1008 | /* | |
1009 | * llseek. be sure to verify file size on SEEK_END. | |
1010 | */ | |
965c8e59 | 1011 | static loff_t ceph_llseek(struct file *file, loff_t offset, int whence) |
124e68e7 SW |
1012 | { |
1013 | struct inode *inode = file->f_mapping->host; | |
1014 | int ret; | |
1015 | ||
1016 | mutex_lock(&inode->i_mutex); | |
6a82c47a | 1017 | |
965c8e59 | 1018 | if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) { |
508b32d8 | 1019 | ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE, false); |
124e68e7 SW |
1020 | if (ret < 0) { |
1021 | offset = ret; | |
1022 | goto out; | |
1023 | } | |
06222e49 JB |
1024 | } |
1025 | ||
965c8e59 | 1026 | switch (whence) { |
06222e49 | 1027 | case SEEK_END: |
124e68e7 SW |
1028 | offset += inode->i_size; |
1029 | break; | |
1030 | case SEEK_CUR: | |
1031 | /* | |
1032 | * Here we special-case the lseek(fd, 0, SEEK_CUR) | |
1033 | * position-querying operation. Avoid rewriting the "same" | |
1034 | * f_pos value back to the file because a concurrent read(), | |
1035 | * write() or lseek() might have altered it | |
1036 | */ | |
1037 | if (offset == 0) { | |
1038 | offset = file->f_pos; | |
1039 | goto out; | |
1040 | } | |
1041 | offset += file->f_pos; | |
1042 | break; | |
06222e49 JB |
1043 | case SEEK_DATA: |
1044 | if (offset >= inode->i_size) { | |
1045 | ret = -ENXIO; | |
1046 | goto out; | |
1047 | } | |
1048 | break; | |
1049 | case SEEK_HOLE: | |
1050 | if (offset >= inode->i_size) { | |
1051 | ret = -ENXIO; | |
1052 | goto out; | |
1053 | } | |
1054 | offset = inode->i_size; | |
1055 | break; | |
124e68e7 SW |
1056 | } |
1057 | ||
46a1c2c7 | 1058 | offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes); |
124e68e7 SW |
1059 | |
1060 | out: | |
1061 | mutex_unlock(&inode->i_mutex); | |
1062 | return offset; | |
1063 | } | |
1064 | ||
ad7a60de LW |
1065 | static inline void ceph_zero_partial_page( |
1066 | struct inode *inode, loff_t offset, unsigned size) | |
1067 | { | |
1068 | struct page *page; | |
1069 | pgoff_t index = offset >> PAGE_CACHE_SHIFT; | |
1070 | ||
1071 | page = find_lock_page(inode->i_mapping, index); | |
1072 | if (page) { | |
1073 | wait_on_page_writeback(page); | |
1074 | zero_user(page, offset & (PAGE_CACHE_SIZE - 1), size); | |
1075 | unlock_page(page); | |
1076 | page_cache_release(page); | |
1077 | } | |
1078 | } | |
1079 | ||
1080 | static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset, | |
1081 | loff_t length) | |
1082 | { | |
1083 | loff_t nearly = round_up(offset, PAGE_CACHE_SIZE); | |
1084 | if (offset < nearly) { | |
1085 | loff_t size = nearly - offset; | |
1086 | if (length < size) | |
1087 | size = length; | |
1088 | ceph_zero_partial_page(inode, offset, size); | |
1089 | offset += size; | |
1090 | length -= size; | |
1091 | } | |
1092 | if (length >= PAGE_CACHE_SIZE) { | |
1093 | loff_t size = round_down(length, PAGE_CACHE_SIZE); | |
1094 | truncate_pagecache_range(inode, offset, offset + size - 1); | |
1095 | offset += size; | |
1096 | length -= size; | |
1097 | } | |
1098 | if (length) | |
1099 | ceph_zero_partial_page(inode, offset, length); | |
1100 | } | |
1101 | ||
1102 | static int ceph_zero_partial_object(struct inode *inode, | |
1103 | loff_t offset, loff_t *length) | |
1104 | { | |
1105 | struct ceph_inode_info *ci = ceph_inode(inode); | |
1106 | struct ceph_fs_client *fsc = ceph_inode_to_client(inode); | |
1107 | struct ceph_osd_request *req; | |
1108 | int ret = 0; | |
1109 | loff_t zero = 0; | |
1110 | int op; | |
1111 | ||
1112 | if (!length) { | |
1113 | op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE; | |
1114 | length = &zero; | |
1115 | } else { | |
1116 | op = CEPH_OSD_OP_ZERO; | |
1117 | } | |
1118 | ||
1119 | req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout, | |
1120 | ceph_vino(inode), | |
1121 | offset, length, | |
715e4cd4 | 1122 | 0, 1, op, |
ad7a60de LW |
1123 | CEPH_OSD_FLAG_WRITE | |
1124 | CEPH_OSD_FLAG_ONDISK, | |
1125 | NULL, 0, 0, false); | |
1126 | if (IS_ERR(req)) { | |
1127 | ret = PTR_ERR(req); | |
1128 | goto out; | |
1129 | } | |
1130 | ||
1131 | ceph_osdc_build_request(req, offset, NULL, ceph_vino(inode).snap, | |
1132 | &inode->i_mtime); | |
1133 | ||
1134 | ret = ceph_osdc_start_request(&fsc->client->osdc, req, false); | |
1135 | if (!ret) { | |
1136 | ret = ceph_osdc_wait_request(&fsc->client->osdc, req); | |
1137 | if (ret == -ENOENT) | |
1138 | ret = 0; | |
1139 | } | |
1140 | ceph_osdc_put_request(req); | |
1141 | ||
1142 | out: | |
1143 | return ret; | |
1144 | } | |
1145 | ||
1146 | static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length) | |
1147 | { | |
1148 | int ret = 0; | |
1149 | struct ceph_inode_info *ci = ceph_inode(inode); | |
b314a90d SW |
1150 | s32 stripe_unit = ceph_file_layout_su(ci->i_layout); |
1151 | s32 stripe_count = ceph_file_layout_stripe_count(ci->i_layout); | |
1152 | s32 object_size = ceph_file_layout_object_size(ci->i_layout); | |
1153 | u64 object_set_size = object_size * stripe_count; | |
1154 | u64 nearly, t; | |
1155 | ||
1156 | /* round offset up to next period boundary */ | |
1157 | nearly = offset + object_set_size - 1; | |
1158 | t = nearly; | |
1159 | nearly -= do_div(t, object_set_size); | |
ad7a60de | 1160 | |
ad7a60de LW |
1161 | while (length && offset < nearly) { |
1162 | loff_t size = length; | |
1163 | ret = ceph_zero_partial_object(inode, offset, &size); | |
1164 | if (ret < 0) | |
1165 | return ret; | |
1166 | offset += size; | |
1167 | length -= size; | |
1168 | } | |
1169 | while (length >= object_set_size) { | |
1170 | int i; | |
1171 | loff_t pos = offset; | |
1172 | for (i = 0; i < stripe_count; ++i) { | |
1173 | ret = ceph_zero_partial_object(inode, pos, NULL); | |
1174 | if (ret < 0) | |
1175 | return ret; | |
1176 | pos += stripe_unit; | |
1177 | } | |
1178 | offset += object_set_size; | |
1179 | length -= object_set_size; | |
1180 | } | |
1181 | while (length) { | |
1182 | loff_t size = length; | |
1183 | ret = ceph_zero_partial_object(inode, offset, &size); | |
1184 | if (ret < 0) | |
1185 | return ret; | |
1186 | offset += size; | |
1187 | length -= size; | |
1188 | } | |
1189 | return ret; | |
1190 | } | |
1191 | ||
1192 | static long ceph_fallocate(struct file *file, int mode, | |
1193 | loff_t offset, loff_t length) | |
1194 | { | |
1195 | struct ceph_file_info *fi = file->private_data; | |
aa8b60e0 | 1196 | struct inode *inode = file_inode(file); |
ad7a60de LW |
1197 | struct ceph_inode_info *ci = ceph_inode(inode); |
1198 | struct ceph_osd_client *osdc = | |
1199 | &ceph_inode_to_client(inode)->client->osdc; | |
1200 | int want, got = 0; | |
1201 | int dirty; | |
1202 | int ret = 0; | |
1203 | loff_t endoff = 0; | |
1204 | loff_t size; | |
1205 | ||
494d77bf YZ |
1206 | if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) |
1207 | return -EOPNOTSUPP; | |
1208 | ||
ad7a60de LW |
1209 | if (!S_ISREG(inode->i_mode)) |
1210 | return -EOPNOTSUPP; | |
1211 | ||
ad7a60de LW |
1212 | mutex_lock(&inode->i_mutex); |
1213 | ||
1214 | if (ceph_snap(inode) != CEPH_NOSNAP) { | |
1215 | ret = -EROFS; | |
1216 | goto unlock; | |
1217 | } | |
1218 | ||
1219 | if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) && | |
1220 | !(mode & FALLOC_FL_PUNCH_HOLE)) { | |
1221 | ret = -ENOSPC; | |
1222 | goto unlock; | |
1223 | } | |
1224 | ||
1225 | size = i_size_read(inode); | |
1226 | if (!(mode & FALLOC_FL_KEEP_SIZE)) | |
1227 | endoff = offset + length; | |
1228 | ||
1229 | if (fi->fmode & CEPH_FILE_MODE_LAZY) | |
1230 | want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO; | |
1231 | else | |
1232 | want = CEPH_CAP_FILE_BUFFER; | |
1233 | ||
3738daa6 | 1234 | ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, endoff, &got, NULL); |
ad7a60de LW |
1235 | if (ret < 0) |
1236 | goto unlock; | |
1237 | ||
1238 | if (mode & FALLOC_FL_PUNCH_HOLE) { | |
1239 | if (offset < size) | |
1240 | ceph_zero_pagecache_range(inode, offset, length); | |
1241 | ret = ceph_zero_objects(inode, offset, length); | |
1242 | } else if (endoff > size) { | |
1243 | truncate_pagecache_range(inode, size, -1); | |
1244 | if (ceph_inode_set_size(inode, endoff)) | |
1245 | ceph_check_caps(ceph_inode(inode), | |
1246 | CHECK_CAPS_AUTHONLY, NULL); | |
1247 | } | |
1248 | ||
1249 | if (!ret) { | |
1250 | spin_lock(&ci->i_ceph_lock); | |
1251 | dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR); | |
1252 | spin_unlock(&ci->i_ceph_lock); | |
1253 | if (dirty) | |
1254 | __mark_inode_dirty(inode, dirty); | |
1255 | } | |
1256 | ||
1257 | ceph_put_cap_refs(ci, got); | |
1258 | unlock: | |
1259 | mutex_unlock(&inode->i_mutex); | |
1260 | return ret; | |
1261 | } | |
1262 | ||
124e68e7 SW |
1263 | const struct file_operations ceph_file_fops = { |
1264 | .open = ceph_open, | |
1265 | .release = ceph_release, | |
1266 | .llseek = ceph_llseek, | |
3644424d | 1267 | .read = new_sync_read, |
4908b822 | 1268 | .write = new_sync_write, |
3644424d | 1269 | .read_iter = ceph_read_iter, |
4908b822 | 1270 | .write_iter = ceph_write_iter, |
124e68e7 SW |
1271 | .mmap = ceph_mmap, |
1272 | .fsync = ceph_fsync, | |
40819f6f GF |
1273 | .lock = ceph_lock, |
1274 | .flock = ceph_flock, | |
124e68e7 | 1275 | .splice_read = generic_file_splice_read, |
3551dd79 | 1276 | .splice_write = iter_file_splice_write, |
124e68e7 SW |
1277 | .unlocked_ioctl = ceph_ioctl, |
1278 | .compat_ioctl = ceph_ioctl, | |
ad7a60de | 1279 | .fallocate = ceph_fallocate, |
124e68e7 SW |
1280 | }; |
1281 |