]>
Commit | Line | Data |
---|---|---|
ee154f01 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. |
ee154f01 BB |
24 | */ |
25 | ||
26 | ||
278bee93 | 27 | #include <sys/zfs_ctldir.h> |
ee154f01 BB |
28 | #include <sys/zfs_vfsops.h> |
29 | #include <sys/zfs_vnops.h> | |
ebe7e575 | 30 | #include <sys/zfs_znode.h> |
24ef51f6 | 31 | #include <sys/dmu_objset.h> |
ee154f01 BB |
32 | #include <sys/vfs.h> |
33 | #include <sys/zpl.h> | |
34 | ||
35 | ||
36 | static struct dentry * | |
8f195a90 | 37 | #ifdef HAVE_LOOKUP_NAMEIDATA |
ee154f01 | 38 | zpl_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd) |
8f195a90 YS |
39 | #else |
40 | zpl_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) | |
41 | #endif | |
ee154f01 | 42 | { |
81e97e21 | 43 | cred_t *cr = CRED(); |
ee154f01 | 44 | struct inode *ip; |
ee154f01 | 45 | int error; |
40d06e3c | 46 | fstrans_cookie_t cookie; |
c5d02870 RS |
47 | pathname_t *ppn = NULL; |
48 | pathname_t pn; | |
49 | zfs_sb_t *zsb = dentry->d_sb->s_fs_info; | |
ee154f01 | 50 | |
9878a89d | 51 | if (dlen(dentry) > ZFS_MAXNAMELEN) |
d1d7e268 | 52 | return (ERR_PTR(-ENAMETOOLONG)); |
9878a89d | 53 | |
81e97e21 | 54 | crhold(cr); |
40d06e3c | 55 | cookie = spl_fstrans_mark(); |
c5d02870 RS |
56 | |
57 | /* If we are a case insensitive fs, we need the real name */ | |
58 | if (zsb->z_case == ZFS_CASE_INSENSITIVE) { | |
59 | pn.pn_bufsize = ZFS_MAXNAMELEN; | |
60 | pn.pn_buf = kmem_zalloc(ZFS_MAXNAMELEN, KM_SLEEP); | |
61 | ppn = &pn; | |
62 | } | |
63 | ||
64 | error = -zfs_lookup(dir, dname(dentry), &ip, 0, cr, NULL, ppn); | |
40d06e3c | 65 | spl_fstrans_unmark(cookie); |
ee154f01 | 66 | ASSERT3S(error, <=, 0); |
81e97e21 | 67 | crfree(cr); |
ee154f01 | 68 | |
7b3e34ba BB |
69 | spin_lock(&dentry->d_lock); |
70 | dentry->d_time = jiffies; | |
ee930353 BB |
71 | #ifndef HAVE_S_D_OP |
72 | d_set_d_op(dentry, &zpl_dentry_operations); | |
73 | #endif /* HAVE_S_D_OP */ | |
7b3e34ba BB |
74 | spin_unlock(&dentry->d_lock); |
75 | ||
ee154f01 | 76 | if (error) { |
c5d02870 RS |
77 | if (ppn) |
78 | kmem_free(pn.pn_buf, ZFS_MAXNAMELEN); | |
ee154f01 | 79 | if (error == -ENOENT) |
d1d7e268 | 80 | return (d_splice_alias(NULL, dentry)); |
ee154f01 | 81 | else |
d1d7e268 | 82 | return (ERR_PTR(error)); |
ee154f01 BB |
83 | } |
84 | ||
c5d02870 RS |
85 | /* |
86 | * If we are case insensitive, call the correct function | |
87 | * to install the name. | |
88 | */ | |
89 | if (ppn) { | |
90 | struct dentry *new_dentry; | |
91 | struct qstr ci_name; | |
92 | ||
93 | ci_name.name = pn.pn_buf; | |
94 | ci_name.len = strlen(pn.pn_buf); | |
95 | new_dentry = d_add_ci(dentry, ip, &ci_name); | |
96 | kmem_free(pn.pn_buf, ZFS_MAXNAMELEN); | |
97 | return (new_dentry); | |
98 | } else { | |
99 | return (d_splice_alias(ip, dentry)); | |
100 | } | |
ee154f01 BB |
101 | } |
102 | ||
ebe7e575 | 103 | void |
7b3e34ba | 104 | zpl_vap_init(vattr_t *vap, struct inode *dir, zpl_umode_t mode, cred_t *cr) |
9fd91dae BB |
105 | { |
106 | vap->va_mask = ATTR_MODE; | |
107 | vap->va_mode = mode; | |
9fd91dae BB |
108 | vap->va_uid = crgetfsuid(cr); |
109 | ||
110 | if (dir && dir->i_mode & S_ISGID) { | |
570d6edf | 111 | vap->va_gid = KGID_TO_SGID(dir->i_gid); |
9fd91dae BB |
112 | if (S_ISDIR(mode)) |
113 | vap->va_mode |= S_ISGID; | |
114 | } else { | |
115 | vap->va_gid = crgetfsgid(cr); | |
116 | } | |
117 | } | |
118 | ||
ee154f01 | 119 | static int |
558ef6d0 | 120 | #ifdef HAVE_CREATE_NAMEIDATA |
b39d3b9f | 121 | zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode, |
ee154f01 | 122 | struct nameidata *nd) |
558ef6d0 YS |
123 | #else |
124 | zpl_create(struct inode *dir, struct dentry *dentry, zpl_umode_t mode, | |
125 | bool flag) | |
126 | #endif | |
ee154f01 | 127 | { |
81e97e21 | 128 | cred_t *cr = CRED(); |
ee154f01 BB |
129 | struct inode *ip; |
130 | vattr_t *vap; | |
131 | int error; | |
40d06e3c | 132 | fstrans_cookie_t cookie; |
ee154f01 | 133 | |
81e97e21 | 134 | crhold(cr); |
d1d7e268 | 135 | vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); |
7b3e34ba | 136 | zpl_vap_init(vap, dir, mode, cr); |
ee154f01 | 137 | |
40d06e3c | 138 | cookie = spl_fstrans_mark(); |
e89260a1 BB |
139 | error = -zfs_create(dir, dname(dentry), vap, 0, mode, &ip, cr, 0, NULL); |
140 | if (error == 0) { | |
7b3e34ba | 141 | d_instantiate(dentry, ip); |
214806c7 DL |
142 | |
143 | error = zpl_xattr_security_init(ip, dir, &dentry->d_name); | |
144 | if (error == 0) | |
145 | error = zpl_init_acl(ip, dir); | |
146 | ||
147 | if (error) | |
148 | (void) zfs_remove(dir, dname(dentry), cr); | |
e89260a1 BB |
149 | } |
150 | ||
a438ff0e | 151 | spl_fstrans_unmark(cookie); |
d1d7e268 | 152 | kmem_free(vap, sizeof (vattr_t)); |
81e97e21 | 153 | crfree(cr); |
ee154f01 BB |
154 | ASSERT3S(error, <=, 0); |
155 | ||
156 | return (error); | |
157 | } | |
158 | ||
159 | static int | |
b39d3b9f BB |
160 | zpl_mknod(struct inode *dir, struct dentry *dentry, zpl_umode_t mode, |
161 | dev_t rdev) | |
ee154f01 | 162 | { |
81e97e21 | 163 | cred_t *cr = CRED(); |
ee154f01 BB |
164 | struct inode *ip; |
165 | vattr_t *vap; | |
166 | int error; | |
40d06e3c | 167 | fstrans_cookie_t cookie; |
ee154f01 | 168 | |
aa6d8c10 NB |
169 | /* |
170 | * We currently expect Linux to supply rdev=0 for all sockets | |
171 | * and fifos, but we want to know if this behavior ever changes. | |
172 | */ | |
173 | if (S_ISSOCK(mode) || S_ISFIFO(mode)) | |
174 | ASSERT(rdev == 0); | |
175 | ||
81e97e21 | 176 | crhold(cr); |
d1d7e268 | 177 | vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); |
7b3e34ba | 178 | zpl_vap_init(vap, dir, mode, cr); |
ee154f01 | 179 | vap->va_rdev = rdev; |
ee154f01 | 180 | |
40d06e3c | 181 | cookie = spl_fstrans_mark(); |
7b3e34ba | 182 | error = -zfs_create(dir, dname(dentry), vap, 0, mode, &ip, cr, 0, NULL); |
023699cd | 183 | if (error == 0) { |
7b3e34ba | 184 | d_instantiate(dentry, ip); |
214806c7 DL |
185 | |
186 | error = zpl_xattr_security_init(ip, dir, &dentry->d_name); | |
187 | if (error == 0) | |
188 | error = zpl_init_acl(ip, dir); | |
189 | ||
190 | if (error) | |
191 | (void) zfs_remove(dir, dname(dentry), cr); | |
023699cd | 192 | } |
7b3e34ba | 193 | |
a438ff0e | 194 | spl_fstrans_unmark(cookie); |
d1d7e268 | 195 | kmem_free(vap, sizeof (vattr_t)); |
81e97e21 | 196 | crfree(cr); |
ee154f01 BB |
197 | ASSERT3S(error, <=, 0); |
198 | ||
34d5a5fd | 199 | return (error); |
ee154f01 BB |
200 | } |
201 | ||
202 | static int | |
203 | zpl_unlink(struct inode *dir, struct dentry *dentry) | |
204 | { | |
81e97e21 | 205 | cred_t *cr = CRED(); |
ee154f01 | 206 | int error; |
40d06e3c | 207 | fstrans_cookie_t cookie; |
ee154f01 | 208 | |
81e97e21 | 209 | crhold(cr); |
40d06e3c | 210 | cookie = spl_fstrans_mark(); |
ee154f01 | 211 | error = -zfs_remove(dir, dname(dentry), cr); |
40d06e3c | 212 | spl_fstrans_unmark(cookie); |
81e97e21 | 213 | crfree(cr); |
ee154f01 BB |
214 | ASSERT3S(error, <=, 0); |
215 | ||
216 | return (error); | |
217 | } | |
218 | ||
219 | static int | |
b39d3b9f | 220 | zpl_mkdir(struct inode *dir, struct dentry *dentry, zpl_umode_t mode) |
ee154f01 | 221 | { |
81e97e21 | 222 | cred_t *cr = CRED(); |
ee154f01 BB |
223 | vattr_t *vap; |
224 | struct inode *ip; | |
225 | int error; | |
40d06e3c | 226 | fstrans_cookie_t cookie; |
ee154f01 | 227 | |
81e97e21 | 228 | crhold(cr); |
d1d7e268 | 229 | vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); |
7b3e34ba | 230 | zpl_vap_init(vap, dir, mode | S_IFDIR, cr); |
ee154f01 | 231 | |
40d06e3c | 232 | cookie = spl_fstrans_mark(); |
ee154f01 | 233 | error = -zfs_mkdir(dir, dname(dentry), vap, &ip, cr, 0, NULL); |
023699cd | 234 | if (error == 0) { |
7b3e34ba | 235 | d_instantiate(dentry, ip); |
214806c7 DL |
236 | |
237 | error = zpl_xattr_security_init(ip, dir, &dentry->d_name); | |
238 | if (error == 0) | |
239 | error = zpl_init_acl(ip, dir); | |
240 | ||
241 | if (error) | |
242 | (void) zfs_rmdir(dir, dname(dentry), NULL, cr, 0); | |
023699cd | 243 | } |
7b3e34ba | 244 | |
a438ff0e | 245 | spl_fstrans_unmark(cookie); |
d1d7e268 | 246 | kmem_free(vap, sizeof (vattr_t)); |
81e97e21 | 247 | crfree(cr); |
ee154f01 BB |
248 | ASSERT3S(error, <=, 0); |
249 | ||
250 | return (error); | |
251 | } | |
252 | ||
253 | static int | |
254 | zpl_rmdir(struct inode * dir, struct dentry *dentry) | |
255 | { | |
81e97e21 | 256 | cred_t *cr = CRED(); |
ee154f01 | 257 | int error; |
40d06e3c | 258 | fstrans_cookie_t cookie; |
ee154f01 | 259 | |
81e97e21 | 260 | crhold(cr); |
40d06e3c | 261 | cookie = spl_fstrans_mark(); |
ee154f01 | 262 | error = -zfs_rmdir(dir, dname(dentry), NULL, cr, 0); |
40d06e3c | 263 | spl_fstrans_unmark(cookie); |
81e97e21 | 264 | crfree(cr); |
ee154f01 BB |
265 | ASSERT3S(error, <=, 0); |
266 | ||
267 | return (error); | |
268 | } | |
269 | ||
270 | static int | |
271 | zpl_getattr(struct vfsmount *mnt, struct dentry *dentry, struct kstat *stat) | |
272 | { | |
ee154f01 | 273 | int error; |
40d06e3c | 274 | fstrans_cookie_t cookie; |
ee154f01 | 275 | |
40d06e3c | 276 | cookie = spl_fstrans_mark(); |
057e8eee | 277 | error = -zfs_getattr_fast(dentry->d_inode, stat); |
40d06e3c | 278 | spl_fstrans_unmark(cookie); |
ee154f01 BB |
279 | ASSERT3S(error, <=, 0); |
280 | ||
281 | return (error); | |
282 | } | |
283 | ||
284 | static int | |
5484965a | 285 | zpl_setattr(struct dentry *dentry, struct iattr *ia) |
ee154f01 | 286 | { |
023699cd | 287 | struct inode *ip = dentry->d_inode; |
81e97e21 | 288 | cred_t *cr = CRED(); |
5484965a | 289 | vattr_t *vap; |
ee154f01 | 290 | int error; |
40d06e3c | 291 | fstrans_cookie_t cookie; |
ee154f01 | 292 | |
023699cd | 293 | error = inode_change_ok(ip, ia); |
ee154f01 BB |
294 | if (error) |
295 | return (error); | |
296 | ||
81e97e21 | 297 | crhold(cr); |
d1d7e268 | 298 | vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); |
5484965a BB |
299 | vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK; |
300 | vap->va_mode = ia->ia_mode; | |
570d6edf RY |
301 | vap->va_uid = KUID_TO_SUID(ia->ia_uid); |
302 | vap->va_gid = KGID_TO_SGID(ia->ia_gid); | |
5484965a BB |
303 | vap->va_size = ia->ia_size; |
304 | vap->va_atime = ia->ia_atime; | |
305 | vap->va_mtime = ia->ia_mtime; | |
306 | vap->va_ctime = ia->ia_ctime; | |
307 | ||
40d06e3c | 308 | cookie = spl_fstrans_mark(); |
023699cd MM |
309 | error = -zfs_setattr(ip, vap, 0, cr); |
310 | if (!error && (ia->ia_valid & ATTR_MODE)) | |
311 | error = zpl_chmod_acl(ip); | |
5484965a | 312 | |
a438ff0e | 313 | spl_fstrans_unmark(cookie); |
d1d7e268 | 314 | kmem_free(vap, sizeof (vattr_t)); |
81e97e21 | 315 | crfree(cr); |
ee154f01 BB |
316 | ASSERT3S(error, <=, 0); |
317 | ||
5484965a | 318 | return (error); |
ee154f01 BB |
319 | } |
320 | ||
321 | static int | |
322 | zpl_rename(struct inode *sdip, struct dentry *sdentry, | |
323 | struct inode *tdip, struct dentry *tdentry) | |
324 | { | |
81e97e21 | 325 | cred_t *cr = CRED(); |
ee154f01 | 326 | int error; |
40d06e3c | 327 | fstrans_cookie_t cookie; |
ee154f01 | 328 | |
81e97e21 | 329 | crhold(cr); |
40d06e3c | 330 | cookie = spl_fstrans_mark(); |
ee154f01 | 331 | error = -zfs_rename(sdip, dname(sdentry), tdip, dname(tdentry), cr, 0); |
40d06e3c | 332 | spl_fstrans_unmark(cookie); |
81e97e21 | 333 | crfree(cr); |
ee154f01 BB |
334 | ASSERT3S(error, <=, 0); |
335 | ||
336 | return (error); | |
337 | } | |
338 | ||
339 | static int | |
340 | zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name) | |
341 | { | |
81e97e21 | 342 | cred_t *cr = CRED(); |
ee154f01 BB |
343 | vattr_t *vap; |
344 | struct inode *ip; | |
345 | int error; | |
40d06e3c | 346 | fstrans_cookie_t cookie; |
ee154f01 | 347 | |
81e97e21 | 348 | crhold(cr); |
d1d7e268 | 349 | vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP); |
7b3e34ba | 350 | zpl_vap_init(vap, dir, S_IFLNK | S_IRWXUGO, cr); |
ee154f01 | 351 | |
40d06e3c | 352 | cookie = spl_fstrans_mark(); |
ee154f01 | 353 | error = -zfs_symlink(dir, dname(dentry), vap, (char *)name, &ip, cr, 0); |
227bc969 | 354 | if (error == 0) { |
7b3e34ba | 355 | d_instantiate(dentry, ip); |
214806c7 DL |
356 | |
357 | error = zpl_xattr_security_init(ip, dir, &dentry->d_name); | |
358 | if (error) | |
359 | (void) zfs_remove(dir, dname(dentry), cr); | |
227bc969 | 360 | } |
7b3e34ba | 361 | |
a438ff0e | 362 | spl_fstrans_unmark(cookie); |
d1d7e268 | 363 | kmem_free(vap, sizeof (vattr_t)); |
81e97e21 | 364 | crfree(cr); |
ee154f01 BB |
365 | ASSERT3S(error, <=, 0); |
366 | ||
367 | return (error); | |
368 | } | |
369 | ||
beeed459 BB |
370 | #if defined(HAVE_PUT_LINK_COOKIE) |
371 | static void | |
372 | zpl_put_link(struct inode *unused, void *cookie) | |
373 | { | |
374 | kmem_free(cookie, MAXPATHLEN); | |
375 | } | |
376 | #elif defined(HAVE_PUT_LINK_NAMEIDATA) | |
377 | static void | |
378 | zpl_put_link(struct dentry *dentry, struct nameidata *nd, void *ptr) | |
379 | { | |
380 | const char *link = nd_get_link(nd); | |
381 | ||
382 | if (!IS_ERR(link)) | |
383 | kmem_free(link, MAXPATHLEN); | |
384 | } | |
385 | #elif defined(HAVE_PUT_LINK_DELAYED) | |
386 | static void | |
387 | zpl_put_link(void *ptr) | |
388 | { | |
389 | kmem_free(ptr, MAXPATHLEN); | |
390 | } | |
bd29109f | 391 | #endif |
beeed459 BB |
392 | |
393 | static int | |
394 | zpl_get_link_common(struct dentry *dentry, struct inode *ip, char **link) | |
ee154f01 | 395 | { |
beeed459 | 396 | fstrans_cookie_t cookie; |
81e97e21 | 397 | cred_t *cr = CRED(); |
8b4f9a2d BB |
398 | struct iovec iov; |
399 | uio_t uio; | |
8b4f9a2d BB |
400 | int error; |
401 | ||
81e97e21 | 402 | crhold(cr); |
beeed459 | 403 | *link = NULL; |
8b4f9a2d | 404 | iov.iov_len = MAXPATHLEN; |
beeed459 | 405 | iov.iov_base = kmem_zalloc(MAXPATHLEN, KM_SLEEP); |
8b4f9a2d BB |
406 | |
407 | uio.uio_iov = &iov; | |
408 | uio.uio_iovcnt = 1; | |
5475aada | 409 | uio.uio_skip = 0; |
8b4f9a2d BB |
410 | uio.uio_resid = (MAXPATHLEN - 1); |
411 | uio.uio_segflg = UIO_SYSSPACE; | |
412 | ||
40d06e3c | 413 | cookie = spl_fstrans_mark(); |
50950001 | 414 | error = -zfs_readlink(ip, &uio, cr); |
40d06e3c | 415 | spl_fstrans_unmark(cookie); |
bd29109f BB |
416 | crfree(cr); |
417 | ||
bd29109f | 418 | if (error) |
beeed459 | 419 | kmem_free(iov.iov_base, MAXPATHLEN); |
bd29109f | 420 | else |
beeed459 | 421 | *link = iov.iov_base; |
8b4f9a2d | 422 | |
beeed459 BB |
423 | return (error); |
424 | } | |
425 | ||
426 | #if defined(HAVE_GET_LINK_DELAYED) | |
427 | const char * | |
428 | zpl_get_link(struct dentry *dentry, struct inode *inode, | |
429 | struct delayed_call *done) | |
430 | { | |
431 | char *link = NULL; | |
432 | int error; | |
433 | ||
434 | if (!dentry) | |
435 | return (ERR_PTR(-ECHILD)); | |
436 | ||
437 | error = zpl_get_link_common(dentry, inode, &link); | |
bd29109f BB |
438 | if (error) |
439 | return (ERR_PTR(error)); | |
beeed459 BB |
440 | |
441 | set_delayed_call(done, zpl_put_link, link); | |
442 | ||
443 | return (link); | |
ee154f01 | 444 | } |
beeed459 BB |
445 | #elif defined(HAVE_GET_LINK_COOKIE) |
446 | const char * | |
447 | zpl_get_link(struct dentry *dentry, struct inode *inode, void **cookie) | |
448 | { | |
449 | char *link = NULL; | |
450 | int error; | |
ee154f01 | 451 | |
beeed459 BB |
452 | if (!dentry) |
453 | return (ERR_PTR(-ECHILD)); | |
454 | ||
455 | error = zpl_get_link_common(dentry, inode, &link); | |
456 | if (error) | |
457 | return (ERR_PTR(error)); | |
458 | ||
459 | return (*cookie = link); | |
460 | } | |
461 | #elif defined(HAVE_FOLLOW_LINK_COOKIE) | |
462 | const char * | |
463 | zpl_follow_link(struct dentry *dentry, void **cookie) | |
ee154f01 | 464 | { |
beeed459 BB |
465 | char *link = NULL; |
466 | int error; | |
ee154f01 | 467 | |
beeed459 BB |
468 | error = zpl_get_link_common(dentry, dentry->d_inode, &link); |
469 | if (error) | |
470 | return (ERR_PTR(error)); | |
471 | ||
472 | return (*cookie = link); | |
ee154f01 | 473 | } |
beeed459 BB |
474 | #elif defined(HAVE_FOLLOW_LINK_NAMEIDATA) |
475 | static void * | |
476 | zpl_follow_link(struct dentry *dentry, struct nameidata *nd) | |
bd29109f | 477 | { |
beeed459 BB |
478 | char *link = NULL; |
479 | int error; | |
480 | ||
481 | error = zpl_get_link_common(dentry, dentry->d_inode, &link); | |
482 | if (error) | |
483 | nd_set_link(nd, ERR_PTR(error)); | |
484 | else | |
485 | nd_set_link(nd, link); | |
486 | ||
487 | return (NULL); | |
bd29109f BB |
488 | } |
489 | #endif | |
ee154f01 BB |
490 | |
491 | static int | |
492 | zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry) | |
493 | { | |
81e97e21 | 494 | cred_t *cr = CRED(); |
ee154f01 | 495 | struct inode *ip = old_dentry->d_inode; |
ee154f01 | 496 | int error; |
40d06e3c | 497 | fstrans_cookie_t cookie; |
ee154f01 BB |
498 | |
499 | if (ip->i_nlink >= ZFS_LINK_MAX) | |
d1d7e268 | 500 | return (-EMLINK); |
ee154f01 | 501 | |
81e97e21 | 502 | crhold(cr); |
ee154f01 BB |
503 | ip->i_ctime = CURRENT_TIME_SEC; |
504 | igrab(ip); /* Use ihold() if available */ | |
505 | ||
40d06e3c | 506 | cookie = spl_fstrans_mark(); |
ee154f01 BB |
507 | error = -zfs_link(dir, ip, dname(dentry), cr); |
508 | if (error) { | |
509 | iput(ip); | |
510 | goto out; | |
511 | } | |
512 | ||
513 | d_instantiate(dentry, ip); | |
514 | out: | |
a438ff0e | 515 | spl_fstrans_unmark(cookie); |
81e97e21 | 516 | crfree(cr); |
ee154f01 BB |
517 | ASSERT3S(error, <=, 0); |
518 | ||
519 | return (error); | |
520 | } | |
521 | ||
ea1fdf46 | 522 | #ifdef HAVE_INODE_TRUNCATE_RANGE |
5cb63a57 | 523 | static void |
d1d7e268 | 524 | zpl_truncate_range(struct inode *ip, loff_t start, loff_t end) |
5cb63a57 ED |
525 | { |
526 | cred_t *cr = CRED(); | |
527 | flock64_t bf; | |
40d06e3c | 528 | fstrans_cookie_t cookie; |
5cb63a57 ED |
529 | |
530 | ASSERT3S(start, <=, end); | |
531 | ||
532 | /* | |
533 | * zfs_freesp() will interpret (len == 0) as meaning "truncate until | |
534 | * the end of the file". We don't want that. | |
535 | */ | |
536 | if (start == end) | |
537 | return; | |
538 | ||
539 | crhold(cr); | |
540 | ||
541 | bf.l_type = F_WRLCK; | |
542 | bf.l_whence = 0; | |
543 | bf.l_start = start; | |
544 | bf.l_len = end - start; | |
545 | bf.l_pid = 0; | |
40d06e3c | 546 | cookie = spl_fstrans_mark(); |
5cb63a57 | 547 | zfs_space(ip, F_FREESP, &bf, FWRITE, start, cr); |
40d06e3c | 548 | spl_fstrans_unmark(cookie); |
5cb63a57 ED |
549 | |
550 | crfree(cr); | |
551 | } | |
ea1fdf46 | 552 | #endif /* HAVE_INODE_TRUNCATE_RANGE */ |
5cb63a57 | 553 | |
cb2d1901 ED |
554 | #ifdef HAVE_INODE_FALLOCATE |
555 | static long | |
556 | zpl_fallocate(struct inode *ip, int mode, loff_t offset, loff_t len) | |
557 | { | |
d1d7e268 | 558 | return (zpl_fallocate_common(ip, mode, offset, len)); |
cb2d1901 ED |
559 | } |
560 | #endif /* HAVE_INODE_FALLOCATE */ | |
561 | ||
7b3e34ba BB |
562 | static int |
563 | #ifdef HAVE_D_REVALIDATE_NAMEIDATA | |
564 | zpl_revalidate(struct dentry *dentry, struct nameidata *nd) | |
565 | { | |
09a661e9 | 566 | unsigned int flags = (nd ? nd->flags : 0); |
7b3e34ba BB |
567 | #else |
568 | zpl_revalidate(struct dentry *dentry, unsigned int flags) | |
569 | { | |
570 | #endif /* HAVE_D_REVALIDATE_NAMEIDATA */ | |
571 | zfs_sb_t *zsb = dentry->d_sb->s_fs_info; | |
572 | int error; | |
573 | ||
574 | if (flags & LOOKUP_RCU) | |
575 | return (-ECHILD); | |
576 | ||
278bee93 BB |
577 | /* |
578 | * Automounted snapshots rely on periodic dentry revalidation | |
579 | * to defer snapshots from being automatically unmounted. | |
580 | */ | |
581 | if (zsb->z_issnap) { | |
582 | if (time_after(jiffies, zsb->z_snap_defer_time + | |
583 | MAX(zfs_expire_snapshot * HZ / 2, HZ))) { | |
584 | zsb->z_snap_defer_time = jiffies; | |
24ef51f6 | 585 | zfsctl_snapshot_unmount_delay(zsb->z_os->os_spa, |
278bee93 BB |
586 | dmu_objset_id(zsb->z_os), zfs_expire_snapshot); |
587 | } | |
588 | } | |
589 | ||
7b3e34ba BB |
590 | /* |
591 | * After a rollback negative dentries created before the rollback | |
592 | * time must be invalidated. Otherwise they can obscure files which | |
593 | * are only present in the rolled back dataset. | |
594 | */ | |
595 | if (dentry->d_inode == NULL) { | |
596 | spin_lock(&dentry->d_lock); | |
597 | error = time_before(dentry->d_time, zsb->z_rollback_time); | |
598 | spin_unlock(&dentry->d_lock); | |
599 | ||
600 | if (error) | |
601 | return (0); | |
602 | } | |
603 | ||
604 | /* | |
605 | * The dentry may reference a stale inode if a mounted file system | |
606 | * was rolled back to a point in time where the object didn't exist. | |
607 | */ | |
608 | if (dentry->d_inode && ITOZ(dentry->d_inode)->z_is_stale) | |
609 | return (0); | |
610 | ||
611 | return (1); | |
612 | } | |
cb2d1901 | 613 | |
ee154f01 | 614 | const struct inode_operations zpl_inode_operations = { |
ee154f01 BB |
615 | .create = zpl_create, |
616 | .link = zpl_link, | |
617 | .unlink = zpl_unlink, | |
618 | .symlink = zpl_symlink, | |
619 | .mkdir = zpl_mkdir, | |
620 | .rmdir = zpl_rmdir, | |
621 | .mknod = zpl_mknod, | |
622 | .rename = zpl_rename, | |
623 | .setattr = zpl_setattr, | |
624 | .getattr = zpl_getattr, | |
625 | .setxattr = generic_setxattr, | |
626 | .getxattr = generic_getxattr, | |
627 | .removexattr = generic_removexattr, | |
628 | .listxattr = zpl_xattr_list, | |
ea1fdf46 | 629 | #ifdef HAVE_INODE_TRUNCATE_RANGE |
5cb63a57 | 630 | .truncate_range = zpl_truncate_range, |
ea1fdf46 | 631 | #endif /* HAVE_INODE_TRUNCATE_RANGE */ |
cb2d1901 ED |
632 | #ifdef HAVE_INODE_FALLOCATE |
633 | .fallocate = zpl_fallocate, | |
634 | #endif /* HAVE_INODE_FALLOCATE */ | |
b695c34e | 635 | #if defined(CONFIG_FS_POSIX_ACL) |
023699cd MM |
636 | #if defined(HAVE_GET_ACL) |
637 | .get_acl = zpl_get_acl, | |
638 | #elif defined(HAVE_CHECK_ACL) | |
639 | .check_acl = zpl_check_acl, | |
640 | #elif defined(HAVE_PERMISSION) | |
641 | .permission = zpl_permission, | |
642 | #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */ | |
b695c34e | 643 | #endif /* CONFIG_FS_POSIX_ACL */ |
ee154f01 BB |
644 | }; |
645 | ||
646 | const struct inode_operations zpl_dir_inode_operations = { | |
ee154f01 BB |
647 | .create = zpl_create, |
648 | .lookup = zpl_lookup, | |
649 | .link = zpl_link, | |
650 | .unlink = zpl_unlink, | |
651 | .symlink = zpl_symlink, | |
652 | .mkdir = zpl_mkdir, | |
653 | .rmdir = zpl_rmdir, | |
654 | .mknod = zpl_mknod, | |
655 | .rename = zpl_rename, | |
656 | .setattr = zpl_setattr, | |
a6695d83 BB |
657 | .getattr = zpl_getattr, |
658 | .setxattr = generic_setxattr, | |
659 | .getxattr = generic_getxattr, | |
660 | .removexattr = generic_removexattr, | |
661 | .listxattr = zpl_xattr_list, | |
b695c34e | 662 | #if defined(CONFIG_FS_POSIX_ACL) |
023699cd MM |
663 | #if defined(HAVE_GET_ACL) |
664 | .get_acl = zpl_get_acl, | |
665 | #elif defined(HAVE_CHECK_ACL) | |
666 | .check_acl = zpl_check_acl, | |
667 | #elif defined(HAVE_PERMISSION) | |
668 | .permission = zpl_permission, | |
669 | #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */ | |
b695c34e | 670 | #endif /* CONFIG_FS_POSIX_ACL */ |
ee154f01 BB |
671 | }; |
672 | ||
673 | const struct inode_operations zpl_symlink_inode_operations = { | |
ee154f01 | 674 | .readlink = generic_readlink, |
beeed459 BB |
675 | #if defined(HAVE_GET_LINK_DELAYED) || defined(HAVE_GET_LINK_COOKIE) |
676 | .get_link = zpl_get_link, | |
677 | #elif defined(HAVE_FOLLOW_LINK_COOKIE) || defined(HAVE_FOLLOW_LINK_NAMEIDATA) | |
ee154f01 | 678 | .follow_link = zpl_follow_link, |
beeed459 BB |
679 | #endif |
680 | #if defined(HAVE_PUT_LINK_COOKIE) || defined(HAVE_PUT_LINK_NAMEIDATA) | |
ee154f01 | 681 | .put_link = zpl_put_link, |
beeed459 | 682 | #endif |
6f2255ba BB |
683 | .setattr = zpl_setattr, |
684 | .getattr = zpl_getattr, | |
f31b3ebe BB |
685 | .setxattr = generic_setxattr, |
686 | .getxattr = generic_getxattr, | |
687 | .removexattr = generic_removexattr, | |
688 | .listxattr = zpl_xattr_list, | |
ee154f01 BB |
689 | }; |
690 | ||
691 | const struct inode_operations zpl_special_inode_operations = { | |
a6695d83 BB |
692 | .setattr = zpl_setattr, |
693 | .getattr = zpl_getattr, | |
694 | .setxattr = generic_setxattr, | |
695 | .getxattr = generic_getxattr, | |
696 | .removexattr = generic_removexattr, | |
697 | .listxattr = zpl_xattr_list, | |
b695c34e | 698 | #if defined(CONFIG_FS_POSIX_ACL) |
023699cd MM |
699 | #if defined(HAVE_GET_ACL) |
700 | .get_acl = zpl_get_acl, | |
701 | #elif defined(HAVE_CHECK_ACL) | |
702 | .check_acl = zpl_check_acl, | |
703 | #elif defined(HAVE_PERMISSION) | |
704 | .permission = zpl_permission, | |
705 | #endif /* HAVE_GET_ACL | HAVE_CHECK_ACL | HAVE_PERMISSION */ | |
b695c34e | 706 | #endif /* CONFIG_FS_POSIX_ACL */ |
ee154f01 | 707 | }; |
7b3e34ba BB |
708 | |
709 | dentry_operations_t zpl_dentry_operations = { | |
710 | .d_revalidate = zpl_revalidate, | |
711 | }; |