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