blob: 5cf034e915bb1ba5053b8d49786a2956148d3b72 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weil124e68e2009-10-06 11:31:08 -07002
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003#include <linux/module.h>
Sage Weil124e68e2009-10-06 11:31:08 -07004#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +09005#include <linux/slab.h>
Sage Weil124e68e2009-10-06 11:31:08 -07006#include <linux/file.h>
Sage Weil5ef50c32012-07-31 11:27:36 -07007#include <linux/mount.h>
Sage Weil124e68e2009-10-06 11:31:08 -07008#include <linux/namei.h>
9#include <linux/writeback.h>
Kent Overstreeta27bb332013-05-07 16:19:08 -070010#include <linux/aio.h>
Li Wangad7a60d2013-08-15 11:51:44 +080011#include <linux/falloc.h>
Sage Weil124e68e2009-10-06 11:31:08 -070012
13#include "super.h"
14#include "mds_client.h"
Milosz Tanski99ccbd22013-08-21 17:29:54 -040015#include "cache.h"
Sage Weil124e68e2009-10-06 11:31:08 -070016
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 */
43static struct ceph_mds_request *
44prepare_open_request(struct super_block *sb, int flags, int create_mode)
45{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070046 struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
47 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil124e68e2009-10-06 11:31:08 -070048 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);
Sage Weil124e68e2009-10-06 11:31:08 -070061out:
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 */
69static int ceph_init_file(struct inode *inode, struct file *file, int fmode)
70{
71 struct ceph_file_info *cf;
72 int ret = 0;
Milosz Tanski99ccbd22013-08-21 17:29:54 -040073 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;
Sage Weil124e68e2009-10-06 11:31:08 -070076
77 switch (inode->i_mode & S_IFMT) {
78 case S_IFREG:
Milosz Tanski99ccbd22013-08-21 17:29:54 -040079 /* 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);
Sage Weil124e68e2009-10-06 11:31:08 -070090 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/*
Sage Weil124e68e2009-10-06 11:31:08 -0700127 * 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 */
132int ceph_open(struct inode *inode, struct file *file)
133{
134 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700135 struct ceph_fs_client *fsc = ceph_sb_to_client(inode->i_sb);
136 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil124e68e2009-10-06 11:31:08 -0700137 struct ceph_mds_request *req;
138 struct ceph_file_info *cf = file->private_data;
Sage Weil5f21c962011-07-26 11:30:29 -0700139 struct inode *parent_inode = NULL;
Sage Weil124e68e2009-10-06 11:31:08 -0700140 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) {
Sage Weilbe655592011-11-30 09:47:09 -0800164 spin_lock(&ci->i_ceph_lock);
Sage Weil124e68e2009-10-06 11:31:08 -0700165 __ceph_get_fmode(ci, fmode);
Sage Weilbe655592011-11-30 09:47:09 -0800166 spin_unlock(&ci->i_ceph_lock);
Sage Weil124e68e2009-10-06 11:31:08 -0700167 return ceph_init_file(inode, file, fmode);
168 }
169
170 /*
Sage Weil7421ab82010-11-07 09:07:15 -0800171 * No need to block if we have caps on the auth MDS (for
172 * write) or any MDS (for read). Update wanted set
Sage Weil124e68e2009-10-06 11:31:08 -0700173 * asynchronously.
174 */
Sage Weilbe655592011-11-30 09:47:09 -0800175 spin_lock(&ci->i_ceph_lock);
Sage Weil7421ab82010-11-07 09:07:15 -0800176 if (__ceph_is_any_real_caps(ci) &&
177 (((fmode & CEPH_FILE_MODE_WR) == 0) || ci->i_auth_cap)) {
Sage Weil124e68e2009-10-06 11:31:08 -0700178 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);
Sage Weilbe655592011-11-30 09:47:09 -0800185 spin_unlock(&ci->i_ceph_lock);
Sage Weil124e68e2009-10-06 11:31:08 -0700186
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);
Sage Weilbe655592011-11-30 09:47:09 -0800197 spin_unlock(&ci->i_ceph_lock);
Sage Weil124e68e2009-10-06 11:31:08 -0700198 return ceph_init_file(inode, file, fmode);
199 }
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400200
Sage Weilbe655592011-11-30 09:47:09 -0800201 spin_unlock(&ci->i_ceph_lock);
Sage Weil124e68e2009-10-06 11:31:08 -0700202
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 }
Sage Weil70b666c2011-05-27 09:24:26 -0700209 req->r_inode = inode;
210 ihold(inode);
Milosz Tanski99ccbd22013-08-21 17:29:54 -0400211
Sage Weil124e68e2009-10-06 11:31:08 -0700212 req->r_num_caps = 1;
Sage Weil5f21c962011-07-26 11:30:29 -0700213 if (flags & (O_CREAT|O_TRUNC))
214 parent_inode = ceph_get_dentry_parent_inode(file->f_dentry);
Sage Weil124e68e2009-10-06 11:31:08 -0700215 err = ceph_mdsc_do_request(mdsc, parent_inode, req);
Sage Weil5f21c962011-07-26 11:30:29 -0700216 iput(parent_inode);
Sage Weil124e68e2009-10-06 11:31:08 -0700217 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));
221out:
222 return err;
223}
224
225
226/*
Sage Weil5ef50c32012-07-31 11:27:36 -0700227 * 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.
Sage Weil124e68e2009-10-06 11:31:08 -0700229 */
Sage Weil5ef50c32012-07-31 11:27:36 -0700230int ceph_atomic_open(struct inode *dir, struct dentry *dentry,
Al Viro30d90492012-06-22 12:40:19 +0400231 struct file *file, unsigned flags, umode_t mode,
Al Virod9585272012-06-22 12:39:14 +0400232 int *opened)
Sage Weil124e68e2009-10-06 11:31:08 -0700233{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700234 struct ceph_fs_client *fsc = ceph_sb_to_client(dir->i_sb);
235 struct ceph_mds_client *mdsc = fsc->mdsc;
Sage Weil124e68e2009-10-06 11:31:08 -0700236 struct ceph_mds_request *req;
Sage Weil5ef50c32012-07-31 11:27:36 -0700237 struct dentry *dn;
Sage Weil124e68e2009-10-06 11:31:08 -0700238 int err;
Sage Weil124e68e2009-10-06 11:31:08 -0700239
Sage Weil5ef50c32012-07-31 11:27:36 -0700240 dout("atomic_open %p dentry %p '%.*s' %s flags %d mode 0%o\n",
241 dir, dentry, dentry->d_name.len, dentry->d_name.name,
242 d_unhashed(dentry) ? "unhashed" : "hashed", flags, mode);
243
244 if (dentry->d_name.len > NAME_MAX)
245 return -ENAMETOOLONG;
246
247 err = ceph_init_dentry(dentry);
248 if (err < 0)
249 return err;
Sage Weil124e68e2009-10-06 11:31:08 -0700250
251 /* do the open */
252 req = prepare_open_request(dir->i_sb, flags, mode);
253 if (IS_ERR(req))
Al Virod9585272012-06-22 12:39:14 +0400254 return PTR_ERR(req);
Sage Weil124e68e2009-10-06 11:31:08 -0700255 req->r_dentry = dget(dentry);
256 req->r_num_caps = 2;
257 if (flags & O_CREAT) {
258 req->r_dentry_drop = CEPH_CAP_FILE_SHARED;
259 req->r_dentry_unless = CEPH_CAP_FILE_EXCL;
260 }
261 req->r_locked_dir = dir; /* caller holds dir->i_mutex */
Sage Weilacda7652011-07-26 11:27:48 -0700262 err = ceph_mdsc_do_request(mdsc,
263 (flags & (O_CREAT|O_TRUNC)) ? dir : NULL,
264 req);
Sam Lang79aec9842012-12-19 09:44:23 -1000265 if (err)
266 goto out_err;
267
Sage Weil468640e2011-07-26 11:28:11 -0700268 err = ceph_handle_snapdir(req, dentry, err);
Sage Weil5ef50c32012-07-31 11:27:36 -0700269 if (err == 0 && (flags & O_CREAT) && !req->r_reply_info.head->is_dentry)
Sage Weil124e68e2009-10-06 11:31:08 -0700270 err = ceph_handle_notrace_create(dir, dentry);
Sage Weil5ef50c32012-07-31 11:27:36 -0700271
272 if (d_unhashed(dentry)) {
273 dn = ceph_finish_lookup(req, dentry, err);
274 if (IS_ERR(dn))
275 err = PTR_ERR(dn);
276 } else {
277 /* we were given a hashed negative dentry */
278 dn = NULL;
279 }
Sage Weil468640e2011-07-26 11:28:11 -0700280 if (err)
Sage Weil5ef50c32012-07-31 11:27:36 -0700281 goto out_err;
282 if (dn || dentry->d_inode == NULL || S_ISLNK(dentry->d_inode->i_mode)) {
283 /* make vfs retry on splice, ENOENT, or symlink */
284 dout("atomic_open finish_no_open on dn %p\n", dn);
285 err = finish_no_open(file, dn);
286 } else {
287 dout("atomic_open finish_open on dn %p\n", dn);
Sam Lang6e8575f2012-12-28 09:56:46 -0800288 if (req->r_op == CEPH_MDS_OP_CREATE && req->r_reply_info.has_create_ino) {
289 *opened |= FILE_CREATED;
290 }
Sage Weil5ef50c32012-07-31 11:27:36 -0700291 err = finish_open(file, dentry, ceph_open, opened);
292 }
293
294out_err:
Sage Weil124e68e2009-10-06 11:31:08 -0700295 ceph_mdsc_put_request(req);
Sage Weil5ef50c32012-07-31 11:27:36 -0700296 dout("atomic_open result=%d\n", err);
Al Virod9585272012-06-22 12:39:14 +0400297 return err;
Sage Weil124e68e2009-10-06 11:31:08 -0700298}
299
300int ceph_release(struct inode *inode, struct file *file)
301{
302 struct ceph_inode_info *ci = ceph_inode(inode);
303 struct ceph_file_info *cf = file->private_data;
304
305 dout("release inode %p file %p\n", inode, file);
306 ceph_put_fmode(ci, cf->fmode);
307 if (cf->last_readdir)
308 ceph_mdsc_put_request(cf->last_readdir);
309 kfree(cf->last_name);
310 kfree(cf->dir_info);
311 dput(cf->dentry);
312 kmem_cache_free(ceph_file_cachep, cf);
Sage Weil195d3ce2010-03-01 09:57:54 -0800313
314 /* wake up anyone waiting for caps on this inode */
Yehuda Sadeh03066f22010-07-27 13:11:08 -0700315 wake_up_all(&ci->i_cap_wq);
Sage Weil124e68e2009-10-06 11:31:08 -0700316 return 0;
317}
318
319/*
Sage Weil124e68e2009-10-06 11:31:08 -0700320 * Read a range of bytes striped over one or more objects. Iterate over
321 * objects we stripe over. (That's not atomic, but good enough for now.)
322 *
323 * If we get a short result from the OSD, check against i_size; we need to
324 * only return a short read to the caller if we hit EOF.
325 */
326static int striped_read(struct inode *inode,
327 u64 off, u64 len,
Sage Weil6a026582010-02-09 14:04:02 -0800328 struct page **pages, int num_pages,
Sage Weilc3cd6282011-06-01 16:08:44 -0700329 int *checkeof, bool o_direct,
Henry C Changab226e22010-12-15 20:41:54 -0800330 unsigned long buf_align)
Sage Weil124e68e2009-10-06 11:31:08 -0700331{
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700332 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Sage Weil124e68e2009-10-06 11:31:08 -0700333 struct ceph_inode_info *ci = ceph_inode(inode);
Dan Carpenter688bac42013-07-23 16:48:01 +0300334 u64 pos, this_len, left;
Sage Weilb7495fc2010-11-09 12:43:12 -0800335 int io_align, page_align;
Dan Carpenter688bac42013-07-23 16:48:01 +0300336 int pages_left;
Sage Weil124e68e2009-10-06 11:31:08 -0700337 int read;
338 struct page **page_pos;
339 int ret;
340 bool hit_stripe, was_short;
341
342 /*
343 * we may need to do multiple reads. not atomic, unfortunately.
344 */
345 pos = off;
346 left = len;
347 page_pos = pages;
348 pages_left = num_pages;
349 read = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -0800350 io_align = off & ~PAGE_MASK;
Sage Weil124e68e2009-10-06 11:31:08 -0700351
352more:
Sage Weilc3cd6282011-06-01 16:08:44 -0700353 if (o_direct)
Henry C Changab226e22010-12-15 20:41:54 -0800354 page_align = (pos - io_align + buf_align) & ~PAGE_MASK;
Sage Weilb7495fc2010-11-09 12:43:12 -0800355 else
356 page_align = pos & ~PAGE_MASK;
Sage Weil124e68e2009-10-06 11:31:08 -0700357 this_len = left;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700358 ret = ceph_osdc_readpages(&fsc->client->osdc, ceph_vino(inode),
Sage Weil124e68e2009-10-06 11:31:08 -0700359 &ci->i_layout, pos, &this_len,
360 ci->i_truncate_seq,
361 ci->i_truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -0800362 page_pos, pages_left, page_align);
Sage Weil124e68e2009-10-06 11:31:08 -0700363 if (ret == -ENOENT)
364 ret = 0;
Sage Weil0e987282011-06-07 20:40:35 -0700365 hit_stripe = this_len < left;
366 was_short = ret >= 0 && ret < this_len;
Dan Carpenter688bac42013-07-23 16:48:01 +0300367 dout("striped_read %llu~%llu (read %u) got %d%s%s\n", pos, left, read,
Sage Weil124e68e2009-10-06 11:31:08 -0700368 ret, hit_stripe ? " HITSTRIPE" : "", was_short ? " SHORT" : "");
369
majianpeng02ae66d2013-08-06 16:20:38 +0800370 if (ret >= 0) {
371 int didpages;
372 if (was_short && (pos + ret < inode->i_size)) {
373 u64 tmp = min(this_len - ret,
374 inode->i_size - pos - ret);
375 dout(" zero gap %llu to %llu\n",
376 pos + ret, pos + ret + tmp);
377 ceph_zero_page_vector_range(page_align + read + ret,
378 tmp, pages);
379 ret += tmp;
Sage Weil124e68e2009-10-06 11:31:08 -0700380 }
majianpeng02ae66d2013-08-06 16:20:38 +0800381
382 didpages = (page_align + ret) >> PAGE_CACHE_SHIFT;
Sage Weil124e68e2009-10-06 11:31:08 -0700383 pos += ret;
384 read = pos - off;
385 left -= ret;
386 page_pos += didpages;
387 pages_left -= didpages;
388
majianpeng02ae66d2013-08-06 16:20:38 +0800389 /* hit stripe and need continue*/
390 if (left && hit_stripe && pos < inode->i_size)
Sage Weil124e68e2009-10-06 11:31:08 -0700391 goto more;
392 }
393
majianpengee7289b2013-08-21 15:02:51 +0800394 if (read > 0) {
majianpeng02ae66d2013-08-06 16:20:38 +0800395 ret = read;
Sage Weilc3cd6282011-06-01 16:08:44 -0700396 /* did we bounce off eof? */
397 if (pos + left > inode->i_size)
398 *checkeof = 1;
Sage Weil124e68e2009-10-06 11:31:08 -0700399 }
400
Sage Weil124e68e2009-10-06 11:31:08 -0700401 dout("striped_read returns %d\n", ret);
402 return ret;
403}
404
405/*
406 * Completely synchronous read and write methods. Direct from __user
407 * buffer to osd, or directly to user pages (if O_DIRECT).
408 *
409 * If the read spans object boundary, just do multiple reads.
410 */
411static ssize_t ceph_sync_read(struct file *file, char __user *data,
Sage Weil6a026582010-02-09 14:04:02 -0800412 unsigned len, loff_t *poff, int *checkeof)
Sage Weil124e68e2009-10-06 11:31:08 -0700413{
Al Viro496ad9a2013-01-23 17:07:38 -0500414 struct inode *inode = file_inode(file);
Sage Weil124e68e2009-10-06 11:31:08 -0700415 struct page **pages;
416 u64 off = *poff;
Henry C Changab226e22010-12-15 20:41:54 -0800417 int num_pages, ret;
Sage Weil124e68e2009-10-06 11:31:08 -0700418
419 dout("sync_read on file %p %llu~%u %s\n", file, off, len,
420 (file->f_flags & O_DIRECT) ? "O_DIRECT" : "");
421
Henry C Changab226e22010-12-15 20:41:54 -0800422 if (file->f_flags & O_DIRECT) {
423 num_pages = calc_pages_for((unsigned long)data, len);
Henry C Changb6aa5902010-12-15 20:45:41 -0800424 pages = ceph_get_direct_page_vector(data, num_pages, true);
Henry C Changab226e22010-12-15 20:41:54 -0800425 } else {
426 num_pages = calc_pages_for(off, len);
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700427 pages = ceph_alloc_page_vector(num_pages, GFP_NOFS);
Henry C Changab226e22010-12-15 20:41:54 -0800428 }
Sage Weil124e68e2009-10-06 11:31:08 -0700429 if (IS_ERR(pages))
430 return PTR_ERR(pages);
431
Sage Weile98b6fe2010-11-09 12:24:53 -0800432 /*
433 * flush any page cache pages in this range. this
434 * will make concurrent normal and sync io slow,
435 * but it will at least behave sensibly when they are
436 * in sequence.
437 */
Yehuda Sadeh29065a52010-02-09 11:14:41 -0800438 ret = filemap_write_and_wait(inode->i_mapping);
439 if (ret < 0)
440 goto done;
441
Sage Weilb7495fc2010-11-09 12:43:12 -0800442 ret = striped_read(inode, off, len, pages, num_pages, checkeof,
Henry C Changab226e22010-12-15 20:41:54 -0800443 file->f_flags & O_DIRECT,
444 (unsigned long)data & ~PAGE_MASK);
Sage Weil124e68e2009-10-06 11:31:08 -0700445
446 if (ret >= 0 && (file->f_flags & O_DIRECT) == 0)
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700447 ret = ceph_copy_page_vector_to_user(pages, data, off, ret);
Sage Weil124e68e2009-10-06 11:31:08 -0700448 if (ret >= 0)
449 *poff = off + ret;
450
Yehuda Sadeh29065a52010-02-09 11:14:41 -0800451done:
Sage Weil124e68e2009-10-06 11:31:08 -0700452 if (file->f_flags & O_DIRECT)
Henry C Changb6aa5902010-12-15 20:45:41 -0800453 ceph_put_page_vector(pages, num_pages, true);
Sage Weil124e68e2009-10-06 11:31:08 -0700454 else
455 ceph_release_page_vector(pages, num_pages);
456 dout("sync_read result %d\n", ret);
457 return ret;
458}
459
460/*
Alex Elder26be8802013-04-15 11:20:42 -0500461 * Write commit request unsafe callback, called to tell us when a
462 * request is unsafe (that is, in flight--has been handed to the
463 * messenger to send to its target osd). It is called again when
464 * we've received a response message indicating the request is
465 * "safe" (its CEPH_OSD_FLAG_ONDISK flag is set), or when a request
466 * is completed early (and unsuccessfully) due to a timeout or
467 * interrupt.
468 *
469 * This is used if we requested both an ACK and ONDISK commit reply
470 * from the OSD.
Sage Weil124e68e2009-10-06 11:31:08 -0700471 */
Alex Elder26be8802013-04-15 11:20:42 -0500472static void ceph_sync_write_unsafe(struct ceph_osd_request *req, bool unsafe)
Sage Weil124e68e2009-10-06 11:31:08 -0700473{
474 struct ceph_inode_info *ci = ceph_inode(req->r_inode);
475
Alex Elder26be8802013-04-15 11:20:42 -0500476 dout("%s %p tid %llu %ssafe\n", __func__, req, req->r_tid,
477 unsafe ? "un" : "");
478 if (unsafe) {
479 ceph_get_cap_refs(ci, CEPH_CAP_FILE_WR);
480 spin_lock(&ci->i_unsafe_lock);
481 list_add_tail(&req->r_unsafe_item,
482 &ci->i_unsafe_writes);
483 spin_unlock(&ci->i_unsafe_lock);
484 } else {
485 spin_lock(&ci->i_unsafe_lock);
486 list_del_init(&req->r_unsafe_item);
487 spin_unlock(&ci->i_unsafe_lock);
488 ceph_put_cap_refs(ci, CEPH_CAP_FILE_WR);
489 }
Sage Weil124e68e2009-10-06 11:31:08 -0700490}
491
majianpenge8344e62013-09-12 13:54:26 +0800492
Sage Weil124e68e2009-10-06 11:31:08 -0700493/*
majianpenge8344e62013-09-12 13:54:26 +0800494 * Synchronous write, straight from __user pointer or user pages.
Sage Weil124e68e2009-10-06 11:31:08 -0700495 *
496 * If write spans object boundary, just do multiple writes. (For a
497 * correct atomic write, we should e.g. take write locks on all
498 * objects, rollback on failure, etc.)
499 */
majianpenge8344e62013-09-12 13:54:26 +0800500static ssize_t
501ceph_sync_direct_write(struct kiocb *iocb, const struct iovec *iov,
502 unsigned long nr_segs, size_t count)
Sage Weil124e68e2009-10-06 11:31:08 -0700503{
majianpenge8344e62013-09-12 13:54:26 +0800504 struct file *file = iocb->ki_filp;
Al Viro496ad9a2013-01-23 17:07:38 -0500505 struct inode *inode = file_inode(file);
Sage Weil124e68e2009-10-06 11:31:08 -0700506 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700507 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
Alex Elderacead002013-03-14 14:09:05 -0500508 struct ceph_snap_context *snapc;
509 struct ceph_vino vino;
Sage Weil124e68e2009-10-06 11:31:08 -0700510 struct ceph_osd_request *req;
511 struct page **pages;
512 int num_pages;
Sage Weil124e68e2009-10-06 11:31:08 -0700513 int written = 0;
514 int flags;
Sage Weil124e68e2009-10-06 11:31:08 -0700515 int check_caps = 0;
majianpenge8344e62013-09-12 13:54:26 +0800516 int page_align;
Sage Weil124e68e2009-10-06 11:31:08 -0700517 int ret;
518 struct timespec mtime = CURRENT_TIME;
majianpenge8344e62013-09-12 13:54:26 +0800519 loff_t pos = iocb->ki_pos;
520 struct iov_iter i;
Sage Weil124e68e2009-10-06 11:31:08 -0700521
Al Viro496ad9a2013-01-23 17:07:38 -0500522 if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
Sage Weil124e68e2009-10-06 11:31:08 -0700523 return -EROFS;
524
majianpenge8344e62013-09-12 13:54:26 +0800525 dout("sync_direct_write on file %p %lld~%u\n", file, pos,
526 (unsigned)count);
Sage Weil124e68e2009-10-06 11:31:08 -0700527
majianpenge8344e62013-09-12 13:54:26 +0800528 ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
Yehuda Sadeh29065a52010-02-09 11:14:41 -0800529 if (ret < 0)
530 return ret;
531
532 ret = invalidate_inode_pages2_range(inode->i_mapping,
533 pos >> PAGE_CACHE_SHIFT,
majianpenge8344e62013-09-12 13:54:26 +0800534 (pos + count) >> PAGE_CACHE_SHIFT);
Yehuda Sadeh29065a52010-02-09 11:14:41 -0800535 if (ret < 0)
536 dout("invalidate_inode_pages2_range returned %d\n", ret);
537
Sage Weil124e68e2009-10-06 11:31:08 -0700538 flags = CEPH_OSD_FLAG_ORDERSNAP |
539 CEPH_OSD_FLAG_ONDISK |
540 CEPH_OSD_FLAG_WRITE;
Sage Weil124e68e2009-10-06 11:31:08 -0700541
majianpenge8344e62013-09-12 13:54:26 +0800542 iov_iter_init(&i, iov, nr_segs, count, 0);
Alex Elder3a42b6c2013-02-16 17:07:32 +0000543
majianpenge8344e62013-09-12 13:54:26 +0800544 while (iov_iter_count(&i) > 0) {
545 void __user *data = i.iov->iov_base + i.iov_offset;
546 u64 len = i.iov->iov_len - i.iov_offset;
Sage Weil124e68e2009-10-06 11:31:08 -0700547
majianpenge8344e62013-09-12 13:54:26 +0800548 page_align = (unsigned long)data & ~PAGE_MASK;
549
550 snapc = ci->i_snap_realm->cached_context;
551 vino = ceph_vino(inode);
552 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
553 vino, pos, &len,
554 2,/*include a 'startsync' command*/
555 CEPH_OSD_OP_WRITE, flags, snapc,
556 ci->i_truncate_seq,
557 ci->i_truncate_size,
558 false);
559 if (IS_ERR(req)) {
560 ret = PTR_ERR(req);
561 goto out;
562 }
563
564 num_pages = calc_pages_for(page_align, len);
Henry C Changb6aa5902010-12-15 20:45:41 -0800565 pages = ceph_get_direct_page_vector(data, num_pages, false);
Sage Weil124e68e2009-10-06 11:31:08 -0700566 if (IS_ERR(pages)) {
567 ret = PTR_ERR(pages);
568 goto out;
569 }
570
571 /*
572 * throw out any page cache pages in this range. this
573 * may block.
574 */
Sage Weil213c99e2010-08-03 10:25:11 -0700575 truncate_inode_pages_range(inode->i_mapping, pos,
majianpenge8344e62013-09-12 13:54:26 +0800576 (pos+len) | (PAGE_CACHE_SIZE-1));
577 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
578 false, false);
579
580 /* BUG_ON(vino.snap != CEPH_NOSNAP); */
581 ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime);
582
583 ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
584 if (!ret)
585 ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
586
587 ceph_put_page_vector(pages, num_pages, false);
588
589out:
590 ceph_osdc_put_request(req);
591 if (ret == 0) {
592 pos += len;
593 written += len;
594 iov_iter_advance(&i, (size_t)len);
595
596 if (pos > i_size_read(inode)) {
597 check_caps = ceph_inode_set_size(inode, pos);
598 if (check_caps)
599 ceph_check_caps(ceph_inode(inode),
600 CHECK_CAPS_AUTHONLY,
601 NULL);
602 }
603 } else
604 break;
605 }
606
607 if (ret != -EOLDSNAPC && written > 0) {
608 iocb->ki_pos = pos;
609 ret = written;
610 }
611 return ret;
612}
613
614
615/*
616 * Synchronous write, straight from __user pointer or user pages.
617 *
618 * If write spans object boundary, just do multiple writes. (For a
619 * correct atomic write, we should e.g. take write locks on all
620 * objects, rollback on failure, etc.)
621 */
622static ssize_t ceph_sync_write(struct kiocb *iocb, const struct iovec *iov,
623 unsigned long nr_segs, size_t count)
624{
625 struct file *file = iocb->ki_filp;
626 struct inode *inode = file_inode(file);
627 struct ceph_inode_info *ci = ceph_inode(inode);
628 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
629 struct ceph_snap_context *snapc;
630 struct ceph_vino vino;
631 struct ceph_osd_request *req;
632 struct page **pages;
633 u64 len;
634 int num_pages;
635 int written = 0;
636 int flags;
637 int check_caps = 0;
638 int ret;
639 struct timespec mtime = CURRENT_TIME;
640 loff_t pos = iocb->ki_pos;
641 struct iov_iter i;
642
643 if (ceph_snap(file_inode(file)) != CEPH_NOSNAP)
644 return -EROFS;
645
646 dout("sync_write on file %p %lld~%u\n", file, pos, (unsigned)count);
647
648 ret = filemap_write_and_wait_range(inode->i_mapping, pos, pos + count);
649 if (ret < 0)
650 return ret;
651
652 ret = invalidate_inode_pages2_range(inode->i_mapping,
653 pos >> PAGE_CACHE_SHIFT,
654 (pos + count) >> PAGE_CACHE_SHIFT);
655 if (ret < 0)
656 dout("invalidate_inode_pages2_range returned %d\n", ret);
657
658 flags = CEPH_OSD_FLAG_ORDERSNAP |
659 CEPH_OSD_FLAG_ONDISK |
660 CEPH_OSD_FLAG_WRITE |
661 CEPH_OSD_FLAG_ACK;
662
663 iov_iter_init(&i, iov, nr_segs, count, 0);
664
665 while ((len = iov_iter_count(&i)) > 0) {
666 size_t left;
667 int n;
668
669 snapc = ci->i_snap_realm->cached_context;
670 vino = ceph_vino(inode);
671 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
672 vino, pos, &len, 1,
673 CEPH_OSD_OP_WRITE, flags, snapc,
674 ci->i_truncate_seq,
675 ci->i_truncate_size,
676 false);
677 if (IS_ERR(req)) {
678 ret = PTR_ERR(req);
679 goto out;
680 }
681
682 /*
683 * write from beginning of first page,
684 * regardless of io alignment
685 */
686 num_pages = (len + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
687
Yehuda Sadeh34d23762010-04-06 14:33:58 -0700688 pages = ceph_alloc_page_vector(num_pages, GFP_NOFS);
Sage Weil124e68e2009-10-06 11:31:08 -0700689 if (IS_ERR(pages)) {
690 ret = PTR_ERR(pages);
691 goto out;
692 }
majianpenge8344e62013-09-12 13:54:26 +0800693
694 left = len;
695 for (n = 0; n < num_pages; n++) {
696 size_t plen = min(left, PAGE_SIZE);
697 ret = iov_iter_copy_from_user(pages[n], &i, 0, plen);
698 if (ret != plen) {
699 ret = -EFAULT;
700 break;
701 }
702 left -= ret;
703 iov_iter_advance(&i, ret);
704 }
705
Sage Weil124e68e2009-10-06 11:31:08 -0700706 if (ret < 0) {
707 ceph_release_page_vector(pages, num_pages);
708 goto out;
709 }
710
majianpenge8344e62013-09-12 13:54:26 +0800711 /* get a second commit callback */
712 req->r_unsafe_callback = ceph_sync_write_unsafe;
713 req->r_inode = inode;
Sage Weil124e68e2009-10-06 11:31:08 -0700714
majianpenge8344e62013-09-12 13:54:26 +0800715 osd_req_op_extent_osd_data_pages(req, 0, pages, len, 0,
716 false, true);
Alex Elder02ee07d2013-03-14 14:09:06 -0500717
majianpenge8344e62013-09-12 13:54:26 +0800718 /* BUG_ON(vino.snap != CEPH_NOSNAP); */
719 ceph_osdc_build_request(req, pos, snapc, vino.snap, &mtime);
Sage Weil124e68e2009-10-06 11:31:08 -0700720
majianpenge8344e62013-09-12 13:54:26 +0800721 ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
722 if (!ret)
723 ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
Sage Weil124e68e2009-10-06 11:31:08 -0700724
725out:
majianpenge8344e62013-09-12 13:54:26 +0800726 ceph_osdc_put_request(req);
727 if (ret == 0) {
728 pos += len;
729 written += len;
Sage Weil124e68e2009-10-06 11:31:08 -0700730
majianpenge8344e62013-09-12 13:54:26 +0800731 if (pos > i_size_read(inode)) {
732 check_caps = ceph_inode_set_size(inode, pos);
733 if (check_caps)
734 ceph_check_caps(ceph_inode(inode),
735 CHECK_CAPS_AUTHONLY,
736 NULL);
737 }
738 } else
739 break;
740 }
741
742 if (ret != -EOLDSNAPC && written > 0) {
Sage Weil124e68e2009-10-06 11:31:08 -0700743 ret = written;
majianpenge8344e62013-09-12 13:54:26 +0800744 iocb->ki_pos = pos;
Sage Weil124e68e2009-10-06 11:31:08 -0700745 }
746 return ret;
747}
748
749/*
750 * Wrap generic_file_aio_read with checks for cap bits on the inode.
751 * Atomically grab references, so that those bits are not released
752 * back to the MDS mid-read.
753 *
754 * Hmm, the sync read case isn't actually async... should it be?
755 */
756static ssize_t ceph_aio_read(struct kiocb *iocb, const struct iovec *iov,
757 unsigned long nr_segs, loff_t pos)
758{
759 struct file *filp = iocb->ki_filp;
Sage Weil29625072010-05-27 10:40:43 -0700760 struct ceph_file_info *fi = filp->private_data;
Sage Weil124e68e2009-10-06 11:31:08 -0700761 loff_t *ppos = &iocb->ki_pos;
762 size_t len = iov->iov_len;
Al Viro496ad9a2013-01-23 17:07:38 -0500763 struct inode *inode = file_inode(filp);
Sage Weil124e68e2009-10-06 11:31:08 -0700764 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadehcd84db62010-06-11 16:58:48 -0700765 void __user *base = iov->iov_base;
Sage Weil124e68e2009-10-06 11:31:08 -0700766 ssize_t ret;
Sage Weil29625072010-05-27 10:40:43 -0700767 int want, got = 0;
Sage Weil6a026582010-02-09 14:04:02 -0800768 int checkeof = 0, read = 0;
Sage Weil124e68e2009-10-06 11:31:08 -0700769
770 dout("aio_read %p %llx.%llx %llu~%u trying to get caps on %p\n",
771 inode, ceph_vinop(inode), pos, (unsigned)len, inode);
Sage Weil6a026582010-02-09 14:04:02 -0800772again:
Sage Weil29625072010-05-27 10:40:43 -0700773 if (fi->fmode & CEPH_FILE_MODE_LAZY)
774 want = CEPH_CAP_FILE_CACHE | CEPH_CAP_FILE_LAZYIO;
775 else
776 want = CEPH_CAP_FILE_CACHE;
777 ret = ceph_get_caps(ci, CEPH_CAP_FILE_RD, want, &got, -1);
Sage Weil124e68e2009-10-06 11:31:08 -0700778 if (ret < 0)
779 goto out;
780 dout("aio_read %p %llx.%llx %llu~%u got cap refs on %s\n",
781 inode, ceph_vinop(inode), pos, (unsigned)len,
782 ceph_cap_string(got));
783
Sage Weil29625072010-05-27 10:40:43 -0700784 if ((got & (CEPH_CAP_FILE_CACHE|CEPH_CAP_FILE_LAZYIO)) == 0 ||
Sage Weil124e68e2009-10-06 11:31:08 -0700785 (iocb->ki_filp->f_flags & O_DIRECT) ||
Sage Weil4918b6d2011-07-26 11:26:07 -0700786 (fi->flags & CEPH_F_SYNC))
Sage Weil124e68e2009-10-06 11:31:08 -0700787 /* hmm, this isn't really async... */
Sage Weil6a026582010-02-09 14:04:02 -0800788 ret = ceph_sync_read(filp, base, len, ppos, &checkeof);
Sage Weil124e68e2009-10-06 11:31:08 -0700789 else
790 ret = generic_file_aio_read(iocb, iov, nr_segs, pos);
791
792out:
793 dout("aio_read %p %llx.%llx dropping cap refs on %s = %d\n",
794 inode, ceph_vinop(inode), ceph_cap_string(got), (int)ret);
795 ceph_put_cap_refs(ci, got);
Sage Weil6a026582010-02-09 14:04:02 -0800796
797 if (checkeof && ret >= 0) {
798 int statret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
799
800 /* hit EOF or hole? */
801 if (statret == 0 && *ppos < inode->i_size) {
Sage Weilc3cd6282011-06-01 16:08:44 -0700802 dout("aio_read sync_read hit hole, ppos %lld < size %lld, reading more\n", *ppos, inode->i_size);
Sage Weil6a026582010-02-09 14:04:02 -0800803 read += ret;
804 base += ret;
805 len -= ret;
806 checkeof = 0;
807 goto again;
808 }
809 }
810 if (ret >= 0)
811 ret += read;
812
Sage Weil124e68e2009-10-06 11:31:08 -0700813 return ret;
814}
815
816/*
817 * Take cap references to avoid releasing caps to MDS mid-write.
818 *
819 * If we are synchronous, and write with an old snap context, the OSD
820 * may return EOLDSNAPC. In that case, retry the write.. _after_
821 * dropping our cap refs and allowing the pending snap to logically
822 * complete _before_ this write occurs.
823 *
824 * If we are near ENOSPC, write synchronously.
825 */
826static ssize_t ceph_aio_write(struct kiocb *iocb, const struct iovec *iov,
827 unsigned long nr_segs, loff_t pos)
828{
829 struct file *file = iocb->ki_filp;
Sage Weil33caad32010-05-26 14:31:27 -0700830 struct ceph_file_info *fi = file->private_data;
Al Viro496ad9a2013-01-23 17:07:38 -0500831 struct inode *inode = file_inode(file);
Sage Weil124e68e2009-10-06 11:31:08 -0700832 struct ceph_inode_info *ci = ceph_inode(inode);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700833 struct ceph_osd_client *osdc =
834 &ceph_sb_to_client(inode->i_sb)->client->osdc;
Yan, Zheng03d254e2013-04-12 16:11:13 +0800835 ssize_t count, written = 0;
836 int err, want, got;
Sage Weil124e68e2009-10-06 11:31:08 -0700837
838 if (ceph_snap(inode) != CEPH_NOSNAP)
839 return -EROFS;
840
Yan, Zheng03d254e2013-04-12 16:11:13 +0800841 mutex_lock(&inode->i_mutex);
Yan, Zheng03d254e2013-04-12 16:11:13 +0800842
843 err = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
844 if (err)
845 goto out;
846
847 /* We can write back this queue in page reclaim */
848 current->backing_dev_info = file->f_mapping->backing_dev_info;
849
850 err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode));
851 if (err)
852 goto out;
853
854 if (count == 0)
855 goto out;
856
857 err = file_remove_suid(file);
858 if (err)
859 goto out;
860
861 err = file_update_time(file);
862 if (err)
863 goto out;
864
Sage Weil124e68e2009-10-06 11:31:08 -0700865retry_snap:
Yan, Zheng6070e0c2013-03-01 10:55:39 +0800866 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL)) {
Yan, Zheng03d254e2013-04-12 16:11:13 +0800867 err = -ENOSPC;
Yan, Zheng6070e0c2013-03-01 10:55:39 +0800868 goto out;
869 }
Yan, Zheng03d254e2013-04-12 16:11:13 +0800870
Randy Dunlapac7f29b2013-04-19 14:20:07 -0700871 dout("aio_write %p %llx.%llx %llu~%zd getting caps. i_size %llu\n",
Yan, Zheng03d254e2013-04-12 16:11:13 +0800872 inode, ceph_vinop(inode), pos, count, inode->i_size);
Sage Weil7971bd92013-05-01 21:15:58 -0700873 if (fi->fmode & CEPH_FILE_MODE_LAZY)
874 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
875 else
876 want = CEPH_CAP_FILE_BUFFER;
Yan, Zheng03d254e2013-04-12 16:11:13 +0800877 got = 0;
878 err = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, &got, pos + count);
879 if (err < 0)
Yan, Zheng37505d52013-04-12 16:11:10 +0800880 goto out;
Sage Weil124e68e2009-10-06 11:31:08 -0700881
Randy Dunlapac7f29b2013-04-19 14:20:07 -0700882 dout("aio_write %p %llx.%llx %llu~%zd got cap refs on %s\n",
Yan, Zheng03d254e2013-04-12 16:11:13 +0800883 inode, ceph_vinop(inode), pos, count, ceph_cap_string(got));
Sage Weil7971bd92013-05-01 21:15:58 -0700884
885 if ((got & (CEPH_CAP_FILE_BUFFER|CEPH_CAP_FILE_LAZYIO)) == 0 ||
majianpenge8344e62013-09-12 13:54:26 +0800886 (file->f_flags & O_DIRECT) || (fi->flags & CEPH_F_SYNC)) {
Yan, Zheng37505d52013-04-12 16:11:10 +0800887 mutex_unlock(&inode->i_mutex);
majianpenge8344e62013-09-12 13:54:26 +0800888 if (file->f_flags & O_DIRECT)
889 written = ceph_sync_direct_write(iocb, iov,
890 nr_segs, count);
891 else
892 written = ceph_sync_write(iocb, iov, nr_segs, count);
majianpeng0e5dd452013-08-08 15:32:19 +0800893 if (written == -EOLDSNAPC) {
894 dout("aio_write %p %llx.%llx %llu~%u"
895 "got EOLDSNAPC, retrying\n",
896 inode, ceph_vinop(inode),
897 pos, (unsigned)iov->iov_len);
898 mutex_lock(&inode->i_mutex);
majianpeng0e5dd452013-08-08 15:32:19 +0800899 goto retry_snap;
900 }
Sage Weil7971bd92013-05-01 21:15:58 -0700901 } else {
Yan, Zhengb0d7c222013-08-12 21:42:15 -0700902 /*
903 * No need to acquire the i_truncate_mutex. Because
904 * the MDS revokes Fwb caps before sending truncate
905 * message to us. We can't get Fwb cap while there
906 * are pending vmtruncate. So write and vmtruncate
907 * can not run at the same time
908 */
Yan, Zheng03d254e2013-04-12 16:11:13 +0800909 written = generic_file_buffered_write(iocb, iov, nr_segs,
910 pos, &iocb->ki_pos,
911 count, 0);
Yan, Zheng6070e0c2013-03-01 10:55:39 +0800912 mutex_unlock(&inode->i_mutex);
Sage Weil124e68e2009-10-06 11:31:08 -0700913 }
Sage Weild8de9ab2011-07-26 11:27:34 -0700914
Yan, Zheng03d254e2013-04-12 16:11:13 +0800915 if (written >= 0) {
Sage Weilfca65b42011-05-04 11:33:47 -0700916 int dirty;
Sage Weilbe655592011-11-30 09:47:09 -0800917 spin_lock(&ci->i_ceph_lock);
Sage Weilfca65b42011-05-04 11:33:47 -0700918 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
Sage Weilbe655592011-11-30 09:47:09 -0800919 spin_unlock(&ci->i_ceph_lock);
Sage Weilfca65b42011-05-04 11:33:47 -0700920 if (dirty)
921 __mark_inode_dirty(inode, dirty);
Sage Weil124e68e2009-10-06 11:31:08 -0700922 }
Sage Weil7971bd92013-05-01 21:15:58 -0700923
Sage Weil124e68e2009-10-06 11:31:08 -0700924 dout("aio_write %p %llx.%llx %llu~%u dropping cap refs on %s\n",
Sage Weil7971bd92013-05-01 21:15:58 -0700925 inode, ceph_vinop(inode), pos, (unsigned)iov->iov_len,
926 ceph_cap_string(got));
Sage Weil124e68e2009-10-06 11:31:08 -0700927 ceph_put_cap_refs(ci, got);
Sage Weil7971bd92013-05-01 21:15:58 -0700928
Yan, Zheng03d254e2013-04-12 16:11:13 +0800929 if (written >= 0 &&
Yan, Zheng6070e0c2013-03-01 10:55:39 +0800930 ((file->f_flags & O_SYNC) || IS_SYNC(file->f_mapping->host) ||
931 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_NEARFULL))) {
Yan, Zheng03d254e2013-04-12 16:11:13 +0800932 err = vfs_fsync_range(file, pos, pos + written - 1, 1);
Yan, Zheng6070e0c2013-03-01 10:55:39 +0800933 if (err < 0)
Yan, Zheng03d254e2013-04-12 16:11:13 +0800934 written = err;
Yan, Zheng6070e0c2013-03-01 10:55:39 +0800935 }
Yan, Zheng03d254e2013-04-12 16:11:13 +0800936
Sage Weil2f75e9e2013-08-09 09:57:58 -0700937 goto out_unlocked;
Sage Weil124e68e2009-10-06 11:31:08 -0700938
Sage Weil2f75e9e2013-08-09 09:57:58 -0700939out:
940 mutex_unlock(&inode->i_mutex);
941out_unlocked:
942 current->backing_dev_info = NULL;
Yan, Zheng03d254e2013-04-12 16:11:13 +0800943 return written ? written : err;
Sage Weil124e68e2009-10-06 11:31:08 -0700944}
945
946/*
947 * llseek. be sure to verify file size on SEEK_END.
948 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800949static loff_t ceph_llseek(struct file *file, loff_t offset, int whence)
Sage Weil124e68e2009-10-06 11:31:08 -0700950{
951 struct inode *inode = file->f_mapping->host;
952 int ret;
953
954 mutex_lock(&inode->i_mutex);
Sage Weil6a82c472011-12-13 09:19:26 -0800955
Andrew Morton965c8e52012-12-17 15:59:39 -0800956 if (whence == SEEK_END || whence == SEEK_DATA || whence == SEEK_HOLE) {
Sage Weil124e68e2009-10-06 11:31:08 -0700957 ret = ceph_do_getattr(inode, CEPH_STAT_CAP_SIZE);
958 if (ret < 0) {
959 offset = ret;
960 goto out;
961 }
Josef Bacik06222e42011-07-18 13:21:38 -0400962 }
963
Andrew Morton965c8e52012-12-17 15:59:39 -0800964 switch (whence) {
Josef Bacik06222e42011-07-18 13:21:38 -0400965 case SEEK_END:
Sage Weil124e68e2009-10-06 11:31:08 -0700966 offset += inode->i_size;
967 break;
968 case SEEK_CUR:
969 /*
970 * Here we special-case the lseek(fd, 0, SEEK_CUR)
971 * position-querying operation. Avoid rewriting the "same"
972 * f_pos value back to the file because a concurrent read(),
973 * write() or lseek() might have altered it
974 */
975 if (offset == 0) {
976 offset = file->f_pos;
977 goto out;
978 }
979 offset += file->f_pos;
980 break;
Josef Bacik06222e42011-07-18 13:21:38 -0400981 case SEEK_DATA:
982 if (offset >= inode->i_size) {
983 ret = -ENXIO;
984 goto out;
985 }
986 break;
987 case SEEK_HOLE:
988 if (offset >= inode->i_size) {
989 ret = -ENXIO;
990 goto out;
991 }
992 offset = inode->i_size;
993 break;
Sage Weil124e68e2009-10-06 11:31:08 -0700994 }
995
Jie Liu46a1c2c2013-06-25 12:02:13 +0800996 offset = vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
Sage Weil124e68e2009-10-06 11:31:08 -0700997
998out:
999 mutex_unlock(&inode->i_mutex);
1000 return offset;
1001}
1002
Li Wangad7a60d2013-08-15 11:51:44 +08001003static inline void ceph_zero_partial_page(
1004 struct inode *inode, loff_t offset, unsigned size)
1005{
1006 struct page *page;
1007 pgoff_t index = offset >> PAGE_CACHE_SHIFT;
1008
1009 page = find_lock_page(inode->i_mapping, index);
1010 if (page) {
1011 wait_on_page_writeback(page);
1012 zero_user(page, offset & (PAGE_CACHE_SIZE - 1), size);
1013 unlock_page(page);
1014 page_cache_release(page);
1015 }
1016}
1017
1018static void ceph_zero_pagecache_range(struct inode *inode, loff_t offset,
1019 loff_t length)
1020{
1021 loff_t nearly = round_up(offset, PAGE_CACHE_SIZE);
1022 if (offset < nearly) {
1023 loff_t size = nearly - offset;
1024 if (length < size)
1025 size = length;
1026 ceph_zero_partial_page(inode, offset, size);
1027 offset += size;
1028 length -= size;
1029 }
1030 if (length >= PAGE_CACHE_SIZE) {
1031 loff_t size = round_down(length, PAGE_CACHE_SIZE);
1032 truncate_pagecache_range(inode, offset, offset + size - 1);
1033 offset += size;
1034 length -= size;
1035 }
1036 if (length)
1037 ceph_zero_partial_page(inode, offset, length);
1038}
1039
1040static int ceph_zero_partial_object(struct inode *inode,
1041 loff_t offset, loff_t *length)
1042{
1043 struct ceph_inode_info *ci = ceph_inode(inode);
1044 struct ceph_fs_client *fsc = ceph_inode_to_client(inode);
1045 struct ceph_osd_request *req;
1046 int ret = 0;
1047 loff_t zero = 0;
1048 int op;
1049
1050 if (!length) {
1051 op = offset ? CEPH_OSD_OP_DELETE : CEPH_OSD_OP_TRUNCATE;
1052 length = &zero;
1053 } else {
1054 op = CEPH_OSD_OP_ZERO;
1055 }
1056
1057 req = ceph_osdc_new_request(&fsc->client->osdc, &ci->i_layout,
1058 ceph_vino(inode),
1059 offset, length,
1060 1, op,
1061 CEPH_OSD_FLAG_WRITE |
1062 CEPH_OSD_FLAG_ONDISK,
1063 NULL, 0, 0, false);
1064 if (IS_ERR(req)) {
1065 ret = PTR_ERR(req);
1066 goto out;
1067 }
1068
1069 ceph_osdc_build_request(req, offset, NULL, ceph_vino(inode).snap,
1070 &inode->i_mtime);
1071
1072 ret = ceph_osdc_start_request(&fsc->client->osdc, req, false);
1073 if (!ret) {
1074 ret = ceph_osdc_wait_request(&fsc->client->osdc, req);
1075 if (ret == -ENOENT)
1076 ret = 0;
1077 }
1078 ceph_osdc_put_request(req);
1079
1080out:
1081 return ret;
1082}
1083
1084static int ceph_zero_objects(struct inode *inode, loff_t offset, loff_t length)
1085{
1086 int ret = 0;
1087 struct ceph_inode_info *ci = ceph_inode(inode);
Sage Weilb314a902013-08-27 12:15:16 -07001088 s32 stripe_unit = ceph_file_layout_su(ci->i_layout);
1089 s32 stripe_count = ceph_file_layout_stripe_count(ci->i_layout);
1090 s32 object_size = ceph_file_layout_object_size(ci->i_layout);
1091 u64 object_set_size = object_size * stripe_count;
1092 u64 nearly, t;
Li Wangad7a60d2013-08-15 11:51:44 +08001093
Sage Weilb314a902013-08-27 12:15:16 -07001094 /* round offset up to next period boundary */
1095 nearly = offset + object_set_size - 1;
1096 t = nearly;
1097 nearly -= do_div(t, object_set_size);
1098
Li Wangad7a60d2013-08-15 11:51:44 +08001099 while (length && offset < nearly) {
1100 loff_t size = length;
1101 ret = ceph_zero_partial_object(inode, offset, &size);
1102 if (ret < 0)
1103 return ret;
1104 offset += size;
1105 length -= size;
1106 }
1107 while (length >= object_set_size) {
1108 int i;
1109 loff_t pos = offset;
1110 for (i = 0; i < stripe_count; ++i) {
1111 ret = ceph_zero_partial_object(inode, pos, NULL);
1112 if (ret < 0)
1113 return ret;
1114 pos += stripe_unit;
1115 }
1116 offset += object_set_size;
1117 length -= object_set_size;
1118 }
1119 while (length) {
1120 loff_t size = length;
1121 ret = ceph_zero_partial_object(inode, offset, &size);
1122 if (ret < 0)
1123 return ret;
1124 offset += size;
1125 length -= size;
1126 }
1127 return ret;
1128}
1129
1130static long ceph_fallocate(struct file *file, int mode,
1131 loff_t offset, loff_t length)
1132{
1133 struct ceph_file_info *fi = file->private_data;
1134 struct inode *inode = file->f_dentry->d_inode;
1135 struct ceph_inode_info *ci = ceph_inode(inode);
1136 struct ceph_osd_client *osdc =
1137 &ceph_inode_to_client(inode)->client->osdc;
1138 int want, got = 0;
1139 int dirty;
1140 int ret = 0;
1141 loff_t endoff = 0;
1142 loff_t size;
1143
1144 if (!S_ISREG(inode->i_mode))
1145 return -EOPNOTSUPP;
1146
1147 if (IS_SWAPFILE(inode))
1148 return -ETXTBSY;
1149
1150 mutex_lock(&inode->i_mutex);
1151
1152 if (ceph_snap(inode) != CEPH_NOSNAP) {
1153 ret = -EROFS;
1154 goto unlock;
1155 }
1156
1157 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) &&
1158 !(mode & FALLOC_FL_PUNCH_HOLE)) {
1159 ret = -ENOSPC;
1160 goto unlock;
1161 }
1162
1163 size = i_size_read(inode);
1164 if (!(mode & FALLOC_FL_KEEP_SIZE))
1165 endoff = offset + length;
1166
1167 if (fi->fmode & CEPH_FILE_MODE_LAZY)
1168 want = CEPH_CAP_FILE_BUFFER | CEPH_CAP_FILE_LAZYIO;
1169 else
1170 want = CEPH_CAP_FILE_BUFFER;
1171
1172 ret = ceph_get_caps(ci, CEPH_CAP_FILE_WR, want, &got, endoff);
1173 if (ret < 0)
1174 goto unlock;
1175
1176 if (mode & FALLOC_FL_PUNCH_HOLE) {
1177 if (offset < size)
1178 ceph_zero_pagecache_range(inode, offset, length);
1179 ret = ceph_zero_objects(inode, offset, length);
1180 } else if (endoff > size) {
1181 truncate_pagecache_range(inode, size, -1);
1182 if (ceph_inode_set_size(inode, endoff))
1183 ceph_check_caps(ceph_inode(inode),
1184 CHECK_CAPS_AUTHONLY, NULL);
1185 }
1186
1187 if (!ret) {
1188 spin_lock(&ci->i_ceph_lock);
1189 dirty = __ceph_mark_dirty_caps(ci, CEPH_CAP_FILE_WR);
1190 spin_unlock(&ci->i_ceph_lock);
1191 if (dirty)
1192 __mark_inode_dirty(inode, dirty);
1193 }
1194
1195 ceph_put_cap_refs(ci, got);
1196unlock:
1197 mutex_unlock(&inode->i_mutex);
1198 return ret;
1199}
1200
Sage Weil124e68e2009-10-06 11:31:08 -07001201const struct file_operations ceph_file_fops = {
1202 .open = ceph_open,
1203 .release = ceph_release,
1204 .llseek = ceph_llseek,
1205 .read = do_sync_read,
1206 .write = do_sync_write,
1207 .aio_read = ceph_aio_read,
1208 .aio_write = ceph_aio_write,
1209 .mmap = ceph_mmap,
1210 .fsync = ceph_fsync,
Greg Farnum40819f62010-08-02 15:34:23 -07001211 .lock = ceph_lock,
1212 .flock = ceph_flock,
Sage Weil124e68e2009-10-06 11:31:08 -07001213 .splice_read = generic_file_splice_read,
1214 .splice_write = generic_file_splice_write,
1215 .unlocked_ioctl = ceph_ioctl,
1216 .compat_ioctl = ceph_ioctl,
Li Wangad7a60d2013-08-15 11:51:44 +08001217 .fallocate = ceph_fallocate,
Sage Weil124e68e2009-10-06 11:31:08 -07001218};
1219