]>
Commit | Line | Data |
---|---|---|
1efb473f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
22 | * Copyright (c) 2011, Lawrence Livermore National Security, LLC. | |
5475aada | 23 | * Copyright (c) 2015 by Chunwei Chen. All rights reserved. |
1efb473f BB |
24 | */ |
25 | ||
26 | ||
f7b939bd CIK |
27 | #ifdef CONFIG_COMPAT |
28 | #include <linux/compat.h> | |
29 | #endif | |
119a394a | 30 | #include <sys/dmu_objset.h> |
1efb473f BB |
31 | #include <sys/zfs_vfsops.h> |
32 | #include <sys/zfs_vnops.h> | |
33 | #include <sys/zfs_znode.h> | |
34 | #include <sys/zpl.h> | |
35 | ||
36 | ||
126400a1 BB |
37 | static int |
38 | zpl_open(struct inode *ip, struct file *filp) | |
39 | { | |
81e97e21 | 40 | cred_t *cr = CRED(); |
126400a1 | 41 | int error; |
40d06e3c | 42 | fstrans_cookie_t cookie; |
126400a1 | 43 | |
7dc71949 CC |
44 | error = generic_file_open(ip, filp); |
45 | if (error) | |
46 | return (error); | |
47 | ||
81e97e21 | 48 | crhold(cr); |
40d06e3c | 49 | cookie = spl_fstrans_mark(); |
126400a1 | 50 | error = -zfs_open(ip, filp->f_mode, filp->f_flags, cr); |
40d06e3c | 51 | spl_fstrans_unmark(cookie); |
81e97e21 | 52 | crfree(cr); |
126400a1 BB |
53 | ASSERT3S(error, <=, 0); |
54 | ||
7dc71949 | 55 | return (error); |
126400a1 BB |
56 | } |
57 | ||
58 | static int | |
59 | zpl_release(struct inode *ip, struct file *filp) | |
60 | { | |
81e97e21 | 61 | cred_t *cr = CRED(); |
126400a1 | 62 | int error; |
40d06e3c | 63 | fstrans_cookie_t cookie; |
126400a1 | 64 | |
40d06e3c | 65 | cookie = spl_fstrans_mark(); |
78d7a5d7 | 66 | if (ITOZ(ip)->z_atime_dirty) |
1e8db771 | 67 | zfs_mark_inode_dirty(ip); |
78d7a5d7 | 68 | |
81e97e21 | 69 | crhold(cr); |
126400a1 | 70 | error = -zfs_close(ip, filp->f_flags, cr); |
40d06e3c | 71 | spl_fstrans_unmark(cookie); |
81e97e21 | 72 | crfree(cr); |
126400a1 BB |
73 | ASSERT3S(error, <=, 0); |
74 | ||
75 | return (error); | |
76 | } | |
77 | ||
1efb473f | 78 | static int |
0ee12919 | 79 | zpl_iterate(struct file *filp, zpl_dir_context_t *ctx) |
1efb473f | 80 | { |
81e97e21 | 81 | cred_t *cr = CRED(); |
1efb473f | 82 | int error; |
40d06e3c | 83 | fstrans_cookie_t cookie; |
1efb473f | 84 | |
81e97e21 | 85 | crhold(cr); |
40d06e3c | 86 | cookie = spl_fstrans_mark(); |
d9c97ec0 | 87 | error = -zfs_readdir(file_inode(filp), ctx, cr); |
40d06e3c | 88 | spl_fstrans_unmark(cookie); |
81e97e21 | 89 | crfree(cr); |
1efb473f BB |
90 | ASSERT3S(error, <=, 0); |
91 | ||
92 | return (error); | |
93 | } | |
94 | ||
9baaa7de | 95 | #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED) |
0f37d0c8 RY |
96 | static int |
97 | zpl_readdir(struct file *filp, void *dirent, filldir_t filldir) | |
98 | { | |
0ee12919 BB |
99 | zpl_dir_context_t ctx = |
100 | ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos); | |
0f37d0c8 RY |
101 | int error; |
102 | ||
103 | error = zpl_iterate(filp, &ctx); | |
104 | filp->f_pos = ctx.pos; | |
105 | ||
106 | return (error); | |
107 | } | |
0ee12919 | 108 | #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */ |
0f37d0c8 | 109 | |
adcd70bd | 110 | #if defined(HAVE_FSYNC_WITH_DENTRY) |
3117dd0b | 111 | /* |
adcd70bd BB |
112 | * Linux 2.6.x - 2.6.34 API, |
113 | * Through 2.6.34 the nfsd kernel server would pass a NULL 'file struct *' | |
114 | * to the fops->fsync() hook. For this reason, we must be careful not to | |
115 | * use filp unconditionally. | |
116 | */ | |
117 | static int | |
118 | zpl_fsync(struct file *filp, struct dentry *dentry, int datasync) | |
119 | { | |
120 | cred_t *cr = CRED(); | |
121 | int error; | |
40d06e3c | 122 | fstrans_cookie_t cookie; |
adcd70bd BB |
123 | |
124 | crhold(cr); | |
40d06e3c | 125 | cookie = spl_fstrans_mark(); |
adcd70bd | 126 | error = -zfs_fsync(dentry->d_inode, datasync, cr); |
40d06e3c | 127 | spl_fstrans_unmark(cookie); |
adcd70bd BB |
128 | crfree(cr); |
129 | ASSERT3S(error, <=, 0); | |
130 | ||
131 | return (error); | |
132 | } | |
133 | ||
7ca25051 | 134 | #ifdef HAVE_FILE_AIO_FSYNC |
cd3939c5 RY |
135 | static int |
136 | zpl_aio_fsync(struct kiocb *kiocb, int datasync) | |
137 | { | |
138 | struct file *filp = kiocb->ki_filp; | |
d9c97ec0 | 139 | return (zpl_fsync(filp, file_dentry(filp), datasync)); |
cd3939c5 | 140 | } |
7ca25051 D |
141 | #endif |
142 | ||
adcd70bd BB |
143 | #elif defined(HAVE_FSYNC_WITHOUT_DENTRY) |
144 | /* | |
145 | * Linux 2.6.35 - 3.0 API, | |
146 | * As of 2.6.35 the dentry argument to the fops->fsync() hook was deemed | |
3117dd0b BB |
147 | * redundant. The dentry is still accessible via filp->f_path.dentry, |
148 | * and we are guaranteed that filp will never be NULL. | |
3117dd0b | 149 | */ |
3117dd0b BB |
150 | static int |
151 | zpl_fsync(struct file *filp, int datasync) | |
152 | { | |
adcd70bd BB |
153 | struct inode *inode = filp->f_mapping->host; |
154 | cred_t *cr = CRED(); | |
155 | int error; | |
40d06e3c | 156 | fstrans_cookie_t cookie; |
adcd70bd BB |
157 | |
158 | crhold(cr); | |
40d06e3c | 159 | cookie = spl_fstrans_mark(); |
adcd70bd | 160 | error = -zfs_fsync(inode, datasync, cr); |
40d06e3c | 161 | spl_fstrans_unmark(cookie); |
adcd70bd BB |
162 | crfree(cr); |
163 | ASSERT3S(error, <=, 0); | |
164 | ||
165 | return (error); | |
166 | } | |
167 | ||
7ca25051 | 168 | #ifdef HAVE_FILE_AIO_FSYNC |
cd3939c5 RY |
169 | static int |
170 | zpl_aio_fsync(struct kiocb *kiocb, int datasync) | |
171 | { | |
172 | return (zpl_fsync(kiocb->ki_filp, datasync)); | |
173 | } | |
7ca25051 D |
174 | #endif |
175 | ||
adcd70bd BB |
176 | #elif defined(HAVE_FSYNC_RANGE) |
177 | /* | |
178 | * Linux 3.1 - 3.x API, | |
179 | * As of 3.1 the responsibility to call filemap_write_and_wait_range() has | |
180 | * been pushed down in to the .fsync() vfs hook. Additionally, the i_mutex | |
181 | * lock is no longer held by the caller, for zfs we don't require the lock | |
182 | * to be held so we don't acquire it. | |
183 | */ | |
3117dd0b | 184 | static int |
adcd70bd | 185 | zpl_fsync(struct file *filp, loff_t start, loff_t end, int datasync) |
1efb473f | 186 | { |
adcd70bd | 187 | struct inode *inode = filp->f_mapping->host; |
81e97e21 | 188 | cred_t *cr = CRED(); |
1efb473f | 189 | int error; |
40d06e3c | 190 | fstrans_cookie_t cookie; |
1efb473f | 191 | |
adcd70bd BB |
192 | error = filemap_write_and_wait_range(inode->i_mapping, start, end); |
193 | if (error) | |
194 | return (error); | |
195 | ||
81e97e21 | 196 | crhold(cr); |
40d06e3c | 197 | cookie = spl_fstrans_mark(); |
adcd70bd | 198 | error = -zfs_fsync(inode, datasync, cr); |
40d06e3c | 199 | spl_fstrans_unmark(cookie); |
81e97e21 | 200 | crfree(cr); |
1efb473f BB |
201 | ASSERT3S(error, <=, 0); |
202 | ||
203 | return (error); | |
204 | } | |
cd3939c5 | 205 | |
7ca25051 | 206 | #ifdef HAVE_FILE_AIO_FSYNC |
cd3939c5 RY |
207 | static int |
208 | zpl_aio_fsync(struct kiocb *kiocb, int datasync) | |
209 | { | |
57ae8400 | 210 | return (zpl_fsync(kiocb->ki_filp, kiocb->ki_pos, -1, datasync)); |
cd3939c5 | 211 | } |
7ca25051 D |
212 | #endif |
213 | ||
adcd70bd BB |
214 | #else |
215 | #error "Unsupported fops->fsync() implementation" | |
216 | #endif | |
1efb473f | 217 | |
5475aada | 218 | static ssize_t |
cd3939c5 | 219 | zpl_read_common_iovec(struct inode *ip, const struct iovec *iovp, size_t count, |
5475aada CC |
220 | unsigned long nr_segs, loff_t *ppos, uio_seg_t segment, int flags, |
221 | cred_t *cr, size_t skip) | |
1efb473f | 222 | { |
e3dc14b8 | 223 | ssize_t read; |
1efb473f | 224 | uio_t uio; |
cd3939c5 | 225 | int error; |
40d06e3c | 226 | fstrans_cookie_t cookie; |
1efb473f | 227 | |
5475aada CC |
228 | uio.uio_iov = iovp; |
229 | uio.uio_skip = skip; | |
cd3939c5 RY |
230 | uio.uio_resid = count; |
231 | uio.uio_iovcnt = nr_segs; | |
232 | uio.uio_loffset = *ppos; | |
1efb473f BB |
233 | uio.uio_limit = MAXOFFSET_T; |
234 | uio.uio_segflg = segment; | |
235 | ||
40d06e3c | 236 | cookie = spl_fstrans_mark(); |
1efb473f | 237 | error = -zfs_read(ip, &uio, flags, cr); |
40d06e3c | 238 | spl_fstrans_unmark(cookie); |
1efb473f BB |
239 | if (error < 0) |
240 | return (error); | |
241 | ||
cd3939c5 RY |
242 | read = count - uio.uio_resid; |
243 | *ppos += read; | |
e3dc14b8 BB |
244 | task_io_account_read(read); |
245 | ||
246 | return (read); | |
1efb473f BB |
247 | } |
248 | ||
cd3939c5 RY |
249 | inline ssize_t |
250 | zpl_read_common(struct inode *ip, const char *buf, size_t len, loff_t *ppos, | |
251 | uio_seg_t segment, int flags, cred_t *cr) | |
252 | { | |
253 | struct iovec iov; | |
254 | ||
255 | iov.iov_base = (void *)buf; | |
256 | iov.iov_len = len; | |
257 | ||
258 | return (zpl_read_common_iovec(ip, &iov, len, 1, ppos, segment, | |
5475aada | 259 | flags, cr, 0)); |
cd3939c5 RY |
260 | } |
261 | ||
cd3939c5 | 262 | static ssize_t |
57ae8400 | 263 | zpl_iter_read_common(struct kiocb *kiocb, const struct iovec *iovp, |
5475aada | 264 | unsigned long nr_segs, size_t count, uio_seg_t seg, size_t skip) |
cd3939c5 RY |
265 | { |
266 | cred_t *cr = CRED(); | |
267 | struct file *filp = kiocb->ki_filp; | |
cd3939c5 | 268 | ssize_t read; |
cd3939c5 RY |
269 | |
270 | crhold(cr); | |
5475aada CC |
271 | read = zpl_read_common_iovec(filp->f_mapping->host, iovp, count, |
272 | nr_segs, &kiocb->ki_pos, seg, filp->f_flags, cr, skip); | |
cd3939c5 RY |
273 | crfree(cr); |
274 | ||
0df9673f | 275 | file_accessed(filp); |
1efb473f BB |
276 | return (read); |
277 | } | |
278 | ||
57ae8400 MK |
279 | #if defined(HAVE_VFS_RW_ITERATE) |
280 | static ssize_t | |
281 | zpl_iter_read(struct kiocb *kiocb, struct iov_iter *to) | |
282 | { | |
5475aada CC |
283 | ssize_t ret; |
284 | uio_seg_t seg = UIO_USERSPACE; | |
285 | if (to->type & ITER_KVEC) | |
286 | seg = UIO_SYSSPACE; | |
287 | if (to->type & ITER_BVEC) | |
288 | seg = UIO_BVEC; | |
289 | ret = zpl_iter_read_common(kiocb, to->iov, to->nr_segs, | |
290 | iov_iter_count(to), seg, to->iov_offset); | |
291 | if (ret > 0) | |
292 | iov_iter_advance(to, ret); | |
293 | return (ret); | |
57ae8400 MK |
294 | } |
295 | #else | |
296 | static ssize_t | |
297 | zpl_aio_read(struct kiocb *kiocb, const struct iovec *iovp, | |
298 | unsigned long nr_segs, loff_t pos) | |
299 | { | |
933ec999 CC |
300 | ssize_t ret; |
301 | size_t count; | |
302 | ||
303 | ret = generic_segment_checks(iovp, &nr_segs, &count, VERIFY_WRITE); | |
304 | if (ret) | |
305 | return (ret); | |
306 | ||
307 | return (zpl_iter_read_common(kiocb, iovp, nr_segs, count, | |
5475aada | 308 | UIO_USERSPACE, 0)); |
57ae8400 MK |
309 | } |
310 | #endif /* HAVE_VFS_RW_ITERATE */ | |
311 | ||
5475aada | 312 | static ssize_t |
cd3939c5 | 313 | zpl_write_common_iovec(struct inode *ip, const struct iovec *iovp, size_t count, |
5475aada CC |
314 | unsigned long nr_segs, loff_t *ppos, uio_seg_t segment, int flags, |
315 | cred_t *cr, size_t skip) | |
1efb473f | 316 | { |
e3dc14b8 | 317 | ssize_t wrote; |
1efb473f | 318 | uio_t uio; |
cd3939c5 | 319 | int error; |
40d06e3c | 320 | fstrans_cookie_t cookie; |
1efb473f | 321 | |
1efdc45e BB |
322 | if (flags & O_APPEND) |
323 | *ppos = i_size_read(ip); | |
324 | ||
5475aada CC |
325 | uio.uio_iov = iovp; |
326 | uio.uio_skip = skip; | |
cd3939c5 RY |
327 | uio.uio_resid = count; |
328 | uio.uio_iovcnt = nr_segs; | |
329 | uio.uio_loffset = *ppos; | |
1efb473f BB |
330 | uio.uio_limit = MAXOFFSET_T; |
331 | uio.uio_segflg = segment; | |
332 | ||
40d06e3c | 333 | cookie = spl_fstrans_mark(); |
1efb473f | 334 | error = -zfs_write(ip, &uio, flags, cr); |
40d06e3c | 335 | spl_fstrans_unmark(cookie); |
1efb473f BB |
336 | if (error < 0) |
337 | return (error); | |
338 | ||
cd3939c5 RY |
339 | wrote = count - uio.uio_resid; |
340 | *ppos += wrote; | |
e3dc14b8 BB |
341 | task_io_account_write(wrote); |
342 | ||
343 | return (wrote); | |
1efb473f | 344 | } |
933ec999 | 345 | |
cd3939c5 RY |
346 | inline ssize_t |
347 | zpl_write_common(struct inode *ip, const char *buf, size_t len, loff_t *ppos, | |
348 | uio_seg_t segment, int flags, cred_t *cr) | |
349 | { | |
350 | struct iovec iov; | |
351 | ||
352 | iov.iov_base = (void *)buf; | |
353 | iov.iov_len = len; | |
354 | ||
355 | return (zpl_write_common_iovec(ip, &iov, len, 1, ppos, segment, | |
5475aada | 356 | flags, cr, 0)); |
cd3939c5 | 357 | } |
1efb473f | 358 | |
cd3939c5 | 359 | static ssize_t |
57ae8400 | 360 | zpl_iter_write_common(struct kiocb *kiocb, const struct iovec *iovp, |
5475aada | 361 | unsigned long nr_segs, size_t count, uio_seg_t seg, size_t skip) |
cd3939c5 RY |
362 | { |
363 | cred_t *cr = CRED(); | |
364 | struct file *filp = kiocb->ki_filp; | |
cd3939c5 | 365 | ssize_t wrote; |
cd3939c5 RY |
366 | |
367 | crhold(cr); | |
5475aada CC |
368 | wrote = zpl_write_common_iovec(filp->f_mapping->host, iovp, count, |
369 | nr_segs, &kiocb->ki_pos, seg, filp->f_flags, cr, skip); | |
cd3939c5 RY |
370 | crfree(cr); |
371 | ||
1efb473f BB |
372 | return (wrote); |
373 | } | |
374 | ||
57ae8400 MK |
375 | #if defined(HAVE_VFS_RW_ITERATE) |
376 | static ssize_t | |
377 | zpl_iter_write(struct kiocb *kiocb, struct iov_iter *from) | |
378 | { | |
933ec999 | 379 | size_t count; |
5475aada CC |
380 | ssize_t ret; |
381 | uio_seg_t seg = UIO_USERSPACE; | |
933ec999 CC |
382 | |
383 | #ifndef HAVE_GENERIC_WRITE_CHECKS_KIOCB | |
384 | struct file *file = kiocb->ki_filp; | |
385 | struct address_space *mapping = file->f_mapping; | |
386 | struct inode *ip = mapping->host; | |
387 | int isblk = S_ISBLK(ip->i_mode); | |
388 | ||
389 | count = iov_iter_count(from); | |
390 | ret = generic_write_checks(file, &kiocb->ki_pos, &count, isblk); | |
c7af63d6 CC |
391 | if (ret) |
392 | return (ret); | |
933ec999 CC |
393 | #else |
394 | /* | |
395 | * XXX - ideally this check should be in the same lock region with | |
396 | * write operations, so that there's no TOCTTOU race when doing | |
397 | * append and someone else grow the file. | |
398 | */ | |
399 | ret = generic_write_checks(kiocb, from); | |
933ec999 CC |
400 | if (ret <= 0) |
401 | return (ret); | |
c7af63d6 CC |
402 | count = ret; |
403 | #endif | |
933ec999 | 404 | |
5475aada CC |
405 | if (from->type & ITER_KVEC) |
406 | seg = UIO_SYSSPACE; | |
407 | if (from->type & ITER_BVEC) | |
408 | seg = UIO_BVEC; | |
933ec999 | 409 | |
5475aada | 410 | ret = zpl_iter_write_common(kiocb, from->iov, from->nr_segs, |
933ec999 | 411 | count, seg, from->iov_offset); |
5475aada CC |
412 | if (ret > 0) |
413 | iov_iter_advance(from, ret); | |
933ec999 | 414 | |
5475aada | 415 | return (ret); |
57ae8400 MK |
416 | } |
417 | #else | |
418 | static ssize_t | |
419 | zpl_aio_write(struct kiocb *kiocb, const struct iovec *iovp, | |
420 | unsigned long nr_segs, loff_t pos) | |
421 | { | |
933ec999 CC |
422 | struct file *file = kiocb->ki_filp; |
423 | struct address_space *mapping = file->f_mapping; | |
424 | struct inode *ip = mapping->host; | |
425 | int isblk = S_ISBLK(ip->i_mode); | |
426 | size_t count; | |
427 | ssize_t ret; | |
428 | ||
429 | ret = generic_segment_checks(iovp, &nr_segs, &count, VERIFY_READ); | |
430 | if (ret) | |
431 | return (ret); | |
432 | ||
433 | ret = generic_write_checks(file, &pos, &count, isblk); | |
434 | if (ret) | |
435 | return (ret); | |
436 | ||
437 | return (zpl_iter_write_common(kiocb, iovp, nr_segs, count, | |
5475aada | 438 | UIO_USERSPACE, 0)); |
57ae8400 MK |
439 | } |
440 | #endif /* HAVE_VFS_RW_ITERATE */ | |
441 | ||
802e7b5f LD |
442 | static loff_t |
443 | zpl_llseek(struct file *filp, loff_t offset, int whence) | |
444 | { | |
445 | #if defined(SEEK_HOLE) && defined(SEEK_DATA) | |
40d06e3c TC |
446 | fstrans_cookie_t cookie; |
447 | ||
802e7b5f LD |
448 | if (whence == SEEK_DATA || whence == SEEK_HOLE) { |
449 | struct inode *ip = filp->f_mapping->host; | |
450 | loff_t maxbytes = ip->i_sb->s_maxbytes; | |
451 | loff_t error; | |
452 | ||
9baaa7de | 453 | spl_inode_lock_shared(ip); |
40d06e3c | 454 | cookie = spl_fstrans_mark(); |
802e7b5f | 455 | error = -zfs_holey(ip, whence, &offset); |
40d06e3c | 456 | spl_fstrans_unmark(cookie); |
802e7b5f LD |
457 | if (error == 0) |
458 | error = lseek_execute(filp, ip, offset, maxbytes); | |
9baaa7de | 459 | spl_inode_unlock_shared(ip); |
802e7b5f LD |
460 | |
461 | return (error); | |
462 | } | |
463 | #endif /* SEEK_HOLE && SEEK_DATA */ | |
464 | ||
d1d7e268 | 465 | return (generic_file_llseek(filp, offset, whence)); |
802e7b5f LD |
466 | } |
467 | ||
c0d35759 BB |
468 | /* |
469 | * It's worth taking a moment to describe how mmap is implemented | |
470 | * for zfs because it differs considerably from other Linux filesystems. | |
471 | * However, this issue is handled the same way under OpenSolaris. | |
472 | * | |
473 | * The issue is that by design zfs bypasses the Linux page cache and | |
474 | * leaves all caching up to the ARC. This has been shown to work | |
475 | * well for the common read(2)/write(2) case. However, mmap(2) | |
476 | * is problem because it relies on being tightly integrated with the | |
477 | * page cache. To handle this we cache mmap'ed files twice, once in | |
478 | * the ARC and a second time in the page cache. The code is careful | |
479 | * to keep both copies synchronized. | |
480 | * | |
481 | * When a file with an mmap'ed region is written to using write(2) | |
482 | * both the data in the ARC and existing pages in the page cache | |
483 | * are updated. For a read(2) data will be read first from the page | |
484 | * cache then the ARC if needed. Neither a write(2) or read(2) will | |
485 | * will ever result in new pages being added to the page cache. | |
486 | * | |
487 | * New pages are added to the page cache only via .readpage() which | |
488 | * is called when the vfs needs to read a page off disk to back the | |
489 | * virtual memory region. These pages may be modified without | |
490 | * notifying the ARC and will be written out periodically via | |
491 | * .writepage(). This will occur due to either a sync or the usual | |
492 | * page aging behavior. Note because a read(2) of a mmap'ed file | |
493 | * will always check the page cache first even when the ARC is out | |
494 | * of date correct data will still be returned. | |
495 | * | |
496 | * While this implementation ensures correct behavior it does have | |
497 | * have some drawbacks. The most obvious of which is that it | |
498 | * increases the required memory footprint when access mmap'ed | |
499 | * files. It also adds additional complexity to the code keeping | |
500 | * both caches synchronized. | |
501 | * | |
502 | * Longer term it may be possible to cleanly resolve this wart by | |
503 | * mapping page cache pages directly on to the ARC buffers. The | |
504 | * Linux address space operations are flexible enough to allow | |
505 | * selection of which pages back a particular index. The trick | |
506 | * would be working out the details of which subsystem is in | |
507 | * charge, the ARC, the page cache, or both. It may also prove | |
508 | * helpful to move the ARC buffers to a scatter-gather lists | |
509 | * rather than a vmalloc'ed region. | |
510 | */ | |
511 | static int | |
512 | zpl_mmap(struct file *filp, struct vm_area_struct *vma) | |
513 | { | |
e2e7aa2d BB |
514 | struct inode *ip = filp->f_mapping->host; |
515 | znode_t *zp = ITOZ(ip); | |
c0d35759 | 516 | int error; |
40d06e3c | 517 | fstrans_cookie_t cookie; |
c0d35759 | 518 | |
40d06e3c | 519 | cookie = spl_fstrans_mark(); |
e2e7aa2d BB |
520 | error = -zfs_map(ip, vma->vm_pgoff, (caddr_t *)vma->vm_start, |
521 | (size_t)(vma->vm_end - vma->vm_start), vma->vm_flags); | |
40d06e3c | 522 | spl_fstrans_unmark(cookie); |
e2e7aa2d BB |
523 | if (error) |
524 | return (error); | |
525 | ||
c0d35759 BB |
526 | error = generic_file_mmap(filp, vma); |
527 | if (error) | |
528 | return (error); | |
529 | ||
530 | mutex_enter(&zp->z_lock); | |
531 | zp->z_is_mapped = 1; | |
532 | mutex_exit(&zp->z_lock); | |
533 | ||
534 | return (error); | |
535 | } | |
536 | ||
537 | /* | |
538 | * Populate a page with data for the Linux page cache. This function is | |
539 | * only used to support mmap(2). There will be an identical copy of the | |
540 | * data in the ARC which is kept up to date via .write() and .writepage(). | |
541 | * | |
542 | * Current this function relies on zpl_read_common() and the O_DIRECT | |
543 | * flag to read in a page. This works but the more correct way is to | |
544 | * update zfs_fillpage() to be Linux friendly and use that interface. | |
545 | */ | |
546 | static int | |
547 | zpl_readpage(struct file *filp, struct page *pp) | |
548 | { | |
549 | struct inode *ip; | |
dde471ef | 550 | struct page *pl[1]; |
c0d35759 | 551 | int error = 0; |
40d06e3c | 552 | fstrans_cookie_t cookie; |
c0d35759 BB |
553 | |
554 | ASSERT(PageLocked(pp)); | |
555 | ip = pp->mapping->host; | |
dde471ef | 556 | pl[0] = pp; |
c0d35759 | 557 | |
40d06e3c | 558 | cookie = spl_fstrans_mark(); |
dde471ef | 559 | error = -zfs_getpage(ip, pl, 1); |
40d06e3c | 560 | spl_fstrans_unmark(cookie); |
c0d35759 | 561 | |
dde471ef PJ |
562 | if (error) { |
563 | SetPageError(pp); | |
564 | ClearPageUptodate(pp); | |
565 | } else { | |
566 | ClearPageError(pp); | |
567 | SetPageUptodate(pp); | |
568 | flush_dcache_page(pp); | |
569 | } | |
c0d35759 | 570 | |
dde471ef | 571 | unlock_page(pp); |
d1d7e268 | 572 | return (error); |
dde471ef | 573 | } |
c0d35759 | 574 | |
f3ab88d6 BB |
575 | /* |
576 | * Populate a set of pages with data for the Linux page cache. This | |
577 | * function will only be called for read ahead and never for demand | |
578 | * paging. For simplicity, the code relies on read_cache_pages() to | |
579 | * correctly lock each page for IO and call zpl_readpage(). | |
580 | */ | |
581 | static int | |
582 | zpl_readpages(struct file *filp, struct address_space *mapping, | |
4ea3f864 | 583 | struct list_head *pages, unsigned nr_pages) |
f3ab88d6 | 584 | { |
95d9fd02 BB |
585 | return (read_cache_pages(mapping, pages, |
586 | (filler_t *)zpl_readpage, filp)); | |
f3ab88d6 BB |
587 | } |
588 | ||
dde471ef PJ |
589 | int |
590 | zpl_putpage(struct page *pp, struct writeback_control *wbc, void *data) | |
591 | { | |
3c0e5c0f | 592 | struct address_space *mapping = data; |
92119cc2 | 593 | fstrans_cookie_t cookie; |
3c0e5c0f BB |
594 | |
595 | ASSERT(PageLocked(pp)); | |
596 | ASSERT(!PageWriteback(pp)); | |
8630650a | 597 | |
92119cc2 | 598 | cookie = spl_fstrans_mark(); |
62c4165a | 599 | (void) zfs_putpage(mapping->host, pp, wbc); |
92119cc2 | 600 | spl_fstrans_unmark(cookie); |
c0d35759 | 601 | |
3c0e5c0f | 602 | return (0); |
dde471ef | 603 | } |
c0d35759 | 604 | |
dde471ef PJ |
605 | static int |
606 | zpl_writepages(struct address_space *mapping, struct writeback_control *wbc) | |
607 | { | |
119a394a | 608 | znode_t *zp = ITOZ(mapping->host); |
0037b49e | 609 | zfsvfs_t *zfsvfs = ITOZSB(mapping->host); |
119a394a ED |
610 | enum writeback_sync_modes sync_mode; |
611 | int result; | |
612 | ||
0037b49e BB |
613 | ZFS_ENTER(zfsvfs); |
614 | if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) | |
119a394a | 615 | wbc->sync_mode = WB_SYNC_ALL; |
0037b49e | 616 | ZFS_EXIT(zfsvfs); |
119a394a ED |
617 | sync_mode = wbc->sync_mode; |
618 | ||
619 | /* | |
620 | * We don't want to run write_cache_pages() in SYNC mode here, because | |
621 | * that would make putpage() wait for a single page to be committed to | |
622 | * disk every single time, resulting in atrocious performance. Instead | |
623 | * we run it once in non-SYNC mode so that the ZIL gets all the data, | |
624 | * and then we commit it all in one go. | |
625 | */ | |
626 | wbc->sync_mode = WB_SYNC_NONE; | |
627 | result = write_cache_pages(mapping, wbc, zpl_putpage, mapping); | |
628 | if (sync_mode != wbc->sync_mode) { | |
0037b49e | 629 | ZFS_ENTER(zfsvfs); |
119a394a | 630 | ZFS_VERIFY_ZP(zp); |
0037b49e BB |
631 | if (zfsvfs->z_log != NULL) |
632 | zil_commit(zfsvfs->z_log, zp->z_id); | |
633 | ZFS_EXIT(zfsvfs); | |
119a394a ED |
634 | |
635 | /* | |
636 | * We need to call write_cache_pages() again (we can't just | |
637 | * return after the commit) because the previous call in | |
638 | * non-SYNC mode does not guarantee that we got all the dirty | |
639 | * pages (see the implementation of write_cache_pages() for | |
640 | * details). That being said, this is a no-op in most cases. | |
641 | */ | |
642 | wbc->sync_mode = sync_mode; | |
643 | result = write_cache_pages(mapping, wbc, zpl_putpage, mapping); | |
644 | } | |
645 | return (result); | |
c0d35759 BB |
646 | } |
647 | ||
648 | /* | |
649 | * Write out dirty pages to the ARC, this function is only required to | |
650 | * support mmap(2). Mapped pages may be dirtied by memory operations | |
651 | * which never call .write(). These dirty pages are kept in sync with | |
652 | * the ARC buffers via this hook. | |
c0d35759 BB |
653 | */ |
654 | static int | |
655 | zpl_writepage(struct page *pp, struct writeback_control *wbc) | |
656 | { | |
119a394a ED |
657 | if (ITOZSB(pp->mapping->host)->z_os->os_sync == ZFS_SYNC_ALWAYS) |
658 | wbc->sync_mode = WB_SYNC_ALL; | |
659 | ||
660 | return (zpl_putpage(pp, wbc, pp->mapping)); | |
c0d35759 BB |
661 | } |
662 | ||
cb2d1901 ED |
663 | /* |
664 | * The only flag combination which matches the behavior of zfs_space() | |
223df016 TC |
665 | * is FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE. The FALLOC_FL_PUNCH_HOLE |
666 | * flag was introduced in the 2.6.38 kernel. | |
cb2d1901 | 667 | */ |
223df016 | 668 | #if defined(HAVE_FILE_FALLOCATE) || defined(HAVE_INODE_FALLOCATE) |
cb2d1901 ED |
669 | long |
670 | zpl_fallocate_common(struct inode *ip, int mode, loff_t offset, loff_t len) | |
671 | { | |
cb2d1901 ED |
672 | int error = -EOPNOTSUPP; |
673 | ||
223df016 TC |
674 | #if defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE) |
675 | cred_t *cr = CRED(); | |
676 | flock64_t bf; | |
677 | loff_t olen; | |
40d06e3c | 678 | fstrans_cookie_t cookie; |
223df016 TC |
679 | |
680 | if (mode != (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) | |
681 | return (error); | |
cb2d1901 | 682 | |
223df016 TC |
683 | if (offset < 0 || len <= 0) |
684 | return (-EINVAL); | |
cb2d1901 | 685 | |
223df016 TC |
686 | spl_inode_lock(ip); |
687 | olen = i_size_read(ip); | |
cb2d1901 | 688 | |
223df016 TC |
689 | if (offset > olen) { |
690 | spl_inode_unlock(ip); | |
691 | return (0); | |
cb2d1901 | 692 | } |
223df016 TC |
693 | if (offset + len > olen) |
694 | len = olen - offset; | |
695 | bf.l_type = F_WRLCK; | |
696 | bf.l_whence = 0; | |
697 | bf.l_start = offset; | |
698 | bf.l_len = len; | |
699 | bf.l_pid = 0; | |
700 | ||
9fa4db44 | 701 | crhold(cr); |
40d06e3c | 702 | cookie = spl_fstrans_mark(); |
223df016 | 703 | error = -zfs_space(ip, F_FREESP, &bf, FWRITE, offset, cr); |
40d06e3c | 704 | spl_fstrans_unmark(cookie); |
223df016 | 705 | spl_inode_unlock(ip); |
cb2d1901 ED |
706 | |
707 | crfree(cr); | |
223df016 | 708 | #endif /* defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE) */ |
cb2d1901 ED |
709 | |
710 | ASSERT3S(error, <=, 0); | |
711 | return (error); | |
712 | } | |
223df016 | 713 | #endif /* defined(HAVE_FILE_FALLOCATE) || defined(HAVE_INODE_FALLOCATE) */ |
cb2d1901 ED |
714 | |
715 | #ifdef HAVE_FILE_FALLOCATE | |
716 | static long | |
717 | zpl_fallocate(struct file *filp, int mode, loff_t offset, loff_t len) | |
718 | { | |
d9c97ec0 | 719 | return zpl_fallocate_common(file_inode(filp), |
cb2d1901 ED |
720 | mode, offset, len); |
721 | } | |
722 | #endif /* HAVE_FILE_FALLOCATE */ | |
723 | ||
9d317793 RY |
724 | /* |
725 | * Map zfs file z_pflags (xvattr_t) to linux file attributes. Only file | |
726 | * attributes common to both Linux and Solaris are mapped. | |
727 | */ | |
728 | static int | |
729 | zpl_ioctl_getflags(struct file *filp, void __user *arg) | |
730 | { | |
534759fa | 731 | struct inode *ip = file_inode(filp); |
9d317793 RY |
732 | unsigned int ioctl_flags = 0; |
733 | uint64_t zfs_flags = ITOZ(ip)->z_pflags; | |
734 | int error; | |
735 | ||
736 | if (zfs_flags & ZFS_IMMUTABLE) | |
737 | ioctl_flags |= FS_IMMUTABLE_FL; | |
738 | ||
739 | if (zfs_flags & ZFS_APPENDONLY) | |
740 | ioctl_flags |= FS_APPEND_FL; | |
741 | ||
742 | if (zfs_flags & ZFS_NODUMP) | |
743 | ioctl_flags |= FS_NODUMP_FL; | |
744 | ||
745 | ioctl_flags &= FS_FL_USER_VISIBLE; | |
746 | ||
747 | error = copy_to_user(arg, &ioctl_flags, sizeof (ioctl_flags)); | |
748 | ||
749 | return (error); | |
750 | } | |
751 | ||
752 | /* | |
753 | * fchange() is a helper macro to detect if we have been asked to change a | |
754 | * flag. This is ugly, but the requirement that we do this is a consequence of | |
755 | * how the Linux file attribute interface was designed. Another consequence is | |
756 | * that concurrent modification of files suffers from a TOCTOU race. Neither | |
757 | * are things we can fix without modifying the kernel-userland interface, which | |
758 | * is outside of our jurisdiction. | |
759 | */ | |
760 | ||
c360af54 | 761 | #define fchange(f0, f1, b0, b1) (!((f0) & (b0)) != !((f1) & (b1))) |
9d317793 RY |
762 | |
763 | static int | |
764 | zpl_ioctl_setflags(struct file *filp, void __user *arg) | |
765 | { | |
534759fa | 766 | struct inode *ip = file_inode(filp); |
9d317793 RY |
767 | uint64_t zfs_flags = ITOZ(ip)->z_pflags; |
768 | unsigned int ioctl_flags; | |
769 | cred_t *cr = CRED(); | |
770 | xvattr_t xva; | |
771 | xoptattr_t *xoap; | |
772 | int error; | |
40d06e3c | 773 | fstrans_cookie_t cookie; |
9d317793 RY |
774 | |
775 | if (copy_from_user(&ioctl_flags, arg, sizeof (ioctl_flags))) | |
776 | return (-EFAULT); | |
777 | ||
778 | if ((ioctl_flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL))) | |
779 | return (-EOPNOTSUPP); | |
780 | ||
781 | if ((ioctl_flags & ~(FS_FL_USER_MODIFIABLE))) | |
782 | return (-EACCES); | |
783 | ||
784 | if ((fchange(ioctl_flags, zfs_flags, FS_IMMUTABLE_FL, ZFS_IMMUTABLE) || | |
785 | fchange(ioctl_flags, zfs_flags, FS_APPEND_FL, ZFS_APPENDONLY)) && | |
786 | !capable(CAP_LINUX_IMMUTABLE)) | |
787 | return (-EACCES); | |
788 | ||
789 | if (!zpl_inode_owner_or_capable(ip)) | |
790 | return (-EACCES); | |
791 | ||
792 | xva_init(&xva); | |
793 | xoap = xva_getxoptattr(&xva); | |
794 | ||
795 | XVA_SET_REQ(&xva, XAT_IMMUTABLE); | |
796 | if (ioctl_flags & FS_IMMUTABLE_FL) | |
797 | xoap->xoa_immutable = B_TRUE; | |
798 | ||
799 | XVA_SET_REQ(&xva, XAT_APPENDONLY); | |
800 | if (ioctl_flags & FS_APPEND_FL) | |
801 | xoap->xoa_appendonly = B_TRUE; | |
802 | ||
803 | XVA_SET_REQ(&xva, XAT_NODUMP); | |
804 | if (ioctl_flags & FS_NODUMP_FL) | |
805 | xoap->xoa_nodump = B_TRUE; | |
806 | ||
807 | crhold(cr); | |
40d06e3c | 808 | cookie = spl_fstrans_mark(); |
9d317793 | 809 | error = -zfs_setattr(ip, (vattr_t *)&xva, 0, cr); |
40d06e3c | 810 | spl_fstrans_unmark(cookie); |
9d317793 RY |
811 | crfree(cr); |
812 | ||
813 | return (error); | |
814 | } | |
815 | ||
88c28395 BB |
816 | static long |
817 | zpl_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) | |
818 | { | |
819 | switch (cmd) { | |
9d317793 RY |
820 | case FS_IOC_GETFLAGS: |
821 | return (zpl_ioctl_getflags(filp, (void *)arg)); | |
822 | case FS_IOC_SETFLAGS: | |
823 | return (zpl_ioctl_setflags(filp, (void *)arg)); | |
88c28395 BB |
824 | default: |
825 | return (-ENOTTY); | |
826 | } | |
827 | } | |
828 | ||
829 | #ifdef CONFIG_COMPAT | |
830 | static long | |
831 | zpl_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) | |
832 | { | |
f7b939bd CIK |
833 | switch (cmd) { |
834 | case FS_IOC32_GETFLAGS: | |
835 | cmd = FS_IOC_GETFLAGS; | |
836 | break; | |
837 | case FS_IOC32_SETFLAGS: | |
838 | cmd = FS_IOC_SETFLAGS; | |
839 | break; | |
840 | default: | |
841 | return (-ENOTTY); | |
842 | } | |
843 | return (zpl_ioctl(filp, cmd, (unsigned long)compat_ptr(arg))); | |
88c28395 BB |
844 | } |
845 | #endif /* CONFIG_COMPAT */ | |
846 | ||
847 | ||
1efb473f | 848 | const struct address_space_operations zpl_address_space_operations = { |
dde471ef | 849 | .readpages = zpl_readpages, |
1efb473f BB |
850 | .readpage = zpl_readpage, |
851 | .writepage = zpl_writepage, | |
d1d7e268 | 852 | .writepages = zpl_writepages, |
1efb473f BB |
853 | }; |
854 | ||
855 | const struct file_operations zpl_file_operations = { | |
126400a1 BB |
856 | .open = zpl_open, |
857 | .release = zpl_release, | |
802e7b5f | 858 | .llseek = zpl_llseek, |
57ae8400 | 859 | #ifdef HAVE_VFS_RW_ITERATE |
7a789346 CC |
860 | #ifdef HAVE_NEW_SYNC_READ |
861 | .read = new_sync_read, | |
862 | .write = new_sync_write, | |
863 | #endif | |
57ae8400 MK |
864 | .read_iter = zpl_iter_read, |
865 | .write_iter = zpl_iter_write, | |
866 | #else | |
7a789346 CC |
867 | .read = do_sync_read, |
868 | .write = do_sync_write, | |
cd3939c5 RY |
869 | .aio_read = zpl_aio_read, |
870 | .aio_write = zpl_aio_write, | |
57ae8400 | 871 | #endif |
c0d35759 | 872 | .mmap = zpl_mmap, |
1efb473f | 873 | .fsync = zpl_fsync, |
7ca25051 | 874 | #ifdef HAVE_FILE_AIO_FSYNC |
cd3939c5 | 875 | .aio_fsync = zpl_aio_fsync, |
7ca25051 | 876 | #endif |
cb2d1901 | 877 | #ifdef HAVE_FILE_FALLOCATE |
d1d7e268 | 878 | .fallocate = zpl_fallocate, |
cb2d1901 | 879 | #endif /* HAVE_FILE_FALLOCATE */ |
d1d7e268 | 880 | .unlocked_ioctl = zpl_ioctl, |
88c28395 | 881 | #ifdef CONFIG_COMPAT |
d1d7e268 | 882 | .compat_ioctl = zpl_compat_ioctl, |
88c28395 | 883 | #endif |
1efb473f BB |
884 | }; |
885 | ||
886 | const struct file_operations zpl_dir_file_operations = { | |
887 | .llseek = generic_file_llseek, | |
888 | .read = generic_read_dir, | |
0ee12919 | 889 | #if defined(HAVE_VFS_ITERATE_SHARED) |
9baaa7de CC |
890 | .iterate_shared = zpl_iterate, |
891 | #elif defined(HAVE_VFS_ITERATE) | |
0f37d0c8 RY |
892 | .iterate = zpl_iterate, |
893 | #else | |
1efb473f | 894 | .readdir = zpl_readdir, |
0f37d0c8 | 895 | #endif |
1efb473f | 896 | .fsync = zpl_fsync, |
88c28395 BB |
897 | .unlocked_ioctl = zpl_ioctl, |
898 | #ifdef CONFIG_COMPAT | |
899 | .compat_ioctl = zpl_compat_ioctl, | |
900 | #endif | |
1efb473f | 901 | }; |