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 https://opensource.org/licenses/CDDL-1.0.
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.
23 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
24 */
25
26
27 #include <sys/sysmacros.h>
28 #include <sys/zfs_ctldir.h>
29 #include <sys/zfs_vfsops.h>
30 #include <sys/zfs_vnops.h>
31 #include <sys/zfs_znode.h>
32 #include <sys/dmu_objset.h>
33 #include <sys/vfs.h>
34 #include <sys/zpl.h>
35 #include <sys/file.h>
36
37 static struct dentry *
zpl_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)38 zpl_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags)
39 {
40 cred_t *cr = CRED();
41 struct inode *ip;
42 znode_t *zp;
43 int error;
44 fstrans_cookie_t cookie;
45 pathname_t *ppn = NULL;
46 pathname_t pn;
47 int zfs_flags = 0;
48 zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info;
49
50 if (dlen(dentry) >= ZAP_MAXNAMELEN)
51 return (ERR_PTR(-ENAMETOOLONG));
52
53 crhold(cr);
54 cookie = spl_fstrans_mark();
55
56 /* If we are a case insensitive fs, we need the real name */
57 if (zfsvfs->z_case == ZFS_CASE_INSENSITIVE) {
58 zfs_flags = FIGNORECASE;
59 pn_alloc(&pn);
60 ppn = &pn;
61 }
62
63 error = -zfs_lookup(ITOZ(dir), dname(dentry), &zp,
64 zfs_flags, cr, NULL, ppn);
65 spl_fstrans_unmark(cookie);
66 ASSERT3S(error, <=, 0);
67 crfree(cr);
68
69 spin_lock(&dentry->d_lock);
70 dentry->d_time = jiffies;
71 spin_unlock(&dentry->d_lock);
72
73 if (error) {
74 /*
75 * If we have a case sensitive fs, we do not want to
76 * insert negative entries, so return NULL for ENOENT.
77 * Fall through if the error is not ENOENT. Also free memory.
78 */
79 if (ppn) {
80 pn_free(ppn);
81 if (error == -ENOENT)
82 return (NULL);
83 }
84
85 if (error == -ENOENT)
86 return (d_splice_alias(NULL, dentry));
87 else
88 return (ERR_PTR(error));
89 }
90 ip = ZTOI(zp);
91
92 /*
93 * If we are case insensitive, call the correct function
94 * to install the name.
95 */
96 if (ppn) {
97 struct dentry *new_dentry;
98 struct qstr ci_name;
99
100 if (strcmp(dname(dentry), pn.pn_buf) == 0) {
101 new_dentry = d_splice_alias(ip, dentry);
102 } else {
103 ci_name.name = pn.pn_buf;
104 ci_name.len = strlen(pn.pn_buf);
105 new_dentry = d_add_ci(dentry, ip, &ci_name);
106 }
107 pn_free(ppn);
108 return (new_dentry);
109 } else {
110 return (d_splice_alias(ip, dentry));
111 }
112 }
113
114 void
zpl_vap_init(vattr_t * vap,struct inode * dir,umode_t mode,cred_t * cr,zidmap_t * mnt_ns)115 zpl_vap_init(vattr_t *vap, struct inode *dir, umode_t mode, cred_t *cr,
116 zidmap_t *mnt_ns)
117 {
118 vap->va_mask = ATTR_MODE;
119 vap->va_mode = mode;
120
121 vap->va_uid = zfs_vfsuid_to_uid(mnt_ns,
122 zfs_i_user_ns(dir), crgetuid(cr));
123
124 if (dir->i_mode & S_ISGID) {
125 vap->va_gid = KGID_TO_SGID(dir->i_gid);
126 if (S_ISDIR(mode))
127 vap->va_mode |= S_ISGID;
128 } else {
129 vap->va_gid = zfs_vfsgid_to_gid(mnt_ns,
130 zfs_i_user_ns(dir), crgetgid(cr));
131 }
132 }
133
134 static int
135 #ifdef HAVE_IOPS_CREATE_USERNS
zpl_create(struct user_namespace * user_ns,struct inode * dir,struct dentry * dentry,umode_t mode,bool flag)136 zpl_create(struct user_namespace *user_ns, struct inode *dir,
137 struct dentry *dentry, umode_t mode, bool flag)
138 #elif defined(HAVE_IOPS_CREATE_IDMAP)
139 zpl_create(struct mnt_idmap *user_ns, struct inode *dir,
140 struct dentry *dentry, umode_t mode, bool flag)
141 #else
142 zpl_create(struct inode *dir, struct dentry *dentry, umode_t mode, bool flag)
143 #endif
144 {
145 cred_t *cr = CRED();
146 znode_t *zp;
147 vattr_t *vap;
148 int error;
149 fstrans_cookie_t cookie;
150 #if !(defined(HAVE_IOPS_CREATE_USERNS) || defined(HAVE_IOPS_CREATE_IDMAP))
151 zidmap_t *user_ns = kcred->user_ns;
152 #endif
153
154 crhold(cr);
155 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
156 zpl_vap_init(vap, dir, mode, cr, user_ns);
157
158 cookie = spl_fstrans_mark();
159 error = -zfs_create(ITOZ(dir), dname(dentry), vap, 0,
160 mode, &zp, cr, 0, NULL, user_ns);
161 if (error == 0) {
162 error = zpl_xattr_security_init(ZTOI(zp), dir, &dentry->d_name);
163 if (error == 0)
164 error = zpl_init_acl(ZTOI(zp), dir);
165
166 if (error) {
167 (void) zfs_remove(ITOZ(dir), dname(dentry), cr, 0);
168 remove_inode_hash(ZTOI(zp));
169 iput(ZTOI(zp));
170 } else {
171 d_instantiate(dentry, ZTOI(zp));
172 }
173 }
174
175 spl_fstrans_unmark(cookie);
176 kmem_free(vap, sizeof (vattr_t));
177 crfree(cr);
178 ASSERT3S(error, <=, 0);
179
180 return (error);
181 }
182
183 static int
184 #ifdef HAVE_IOPS_MKNOD_USERNS
zpl_mknod(struct user_namespace * user_ns,struct inode * dir,struct dentry * dentry,umode_t mode,dev_t rdev)185 zpl_mknod(struct user_namespace *user_ns, struct inode *dir,
186 struct dentry *dentry, umode_t mode,
187 #elif defined(HAVE_IOPS_MKNOD_IDMAP)
188 zpl_mknod(struct mnt_idmap *user_ns, struct inode *dir,
189 struct dentry *dentry, umode_t mode,
190 #else
191 zpl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
192 #endif
193 dev_t rdev)
194 {
195 cred_t *cr = CRED();
196 znode_t *zp;
197 vattr_t *vap;
198 int error;
199 fstrans_cookie_t cookie;
200 #if !(defined(HAVE_IOPS_MKNOD_USERNS) || defined(HAVE_IOPS_MKNOD_IDMAP))
201 zidmap_t *user_ns = kcred->user_ns;
202 #endif
203
204 /*
205 * We currently expect Linux to supply rdev=0 for all sockets
206 * and fifos, but we want to know if this behavior ever changes.
207 */
208 if (S_ISSOCK(mode) || S_ISFIFO(mode))
209 ASSERT(rdev == 0);
210
211 crhold(cr);
212 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
213 zpl_vap_init(vap, dir, mode, cr, user_ns);
214 vap->va_rdev = rdev;
215
216 cookie = spl_fstrans_mark();
217 error = -zfs_create(ITOZ(dir), dname(dentry), vap, 0,
218 mode, &zp, cr, 0, NULL, user_ns);
219 if (error == 0) {
220 error = zpl_xattr_security_init(ZTOI(zp), dir, &dentry->d_name);
221 if (error == 0)
222 error = zpl_init_acl(ZTOI(zp), dir);
223
224 if (error) {
225 (void) zfs_remove(ITOZ(dir), dname(dentry), cr, 0);
226 remove_inode_hash(ZTOI(zp));
227 iput(ZTOI(zp));
228 } else {
229 d_instantiate(dentry, ZTOI(zp));
230 }
231 }
232
233 spl_fstrans_unmark(cookie);
234 kmem_free(vap, sizeof (vattr_t));
235 crfree(cr);
236 ASSERT3S(error, <=, 0);
237
238 return (error);
239 }
240
241 static int
242 #ifdef HAVE_TMPFILE_IDMAP
zpl_tmpfile(struct mnt_idmap * userns,struct inode * dir,struct file * file,umode_t mode)243 zpl_tmpfile(struct mnt_idmap *userns, struct inode *dir,
244 struct file *file, umode_t mode)
245 #elif !defined(HAVE_TMPFILE_DENTRY)
246 zpl_tmpfile(struct user_namespace *userns, struct inode *dir,
247 struct file *file, umode_t mode)
248 #else
249 #ifdef HAVE_TMPFILE_USERNS
250 zpl_tmpfile(struct user_namespace *userns, struct inode *dir,
251 struct dentry *dentry, umode_t mode)
252 #else
253 zpl_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
254 #endif
255 #endif
256 {
257 cred_t *cr = CRED();
258 struct inode *ip;
259 vattr_t *vap;
260 int error;
261 fstrans_cookie_t cookie;
262 #if !(defined(HAVE_TMPFILE_USERNS) || defined(HAVE_TMPFILE_IDMAP))
263 zidmap_t *userns = kcred->user_ns;
264 #endif
265
266 crhold(cr);
267 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
268 /*
269 * The VFS does not apply the umask, therefore it is applied here
270 * when POSIX ACLs are not enabled.
271 */
272 if (!IS_POSIXACL(dir))
273 mode &= ~current_umask();
274 zpl_vap_init(vap, dir, mode, cr, userns);
275
276 cookie = spl_fstrans_mark();
277 error = -zfs_tmpfile(dir, vap, 0, mode, &ip, cr, 0, NULL, userns);
278 if (error == 0) {
279 /* d_tmpfile will do drop_nlink, so we should set it first */
280 set_nlink(ip, 1);
281 #ifndef HAVE_TMPFILE_DENTRY
282 d_tmpfile(file, ip);
283
284 error = zpl_xattr_security_init(ip, dir,
285 &file->f_path.dentry->d_name);
286 #else
287 d_tmpfile(dentry, ip);
288
289 error = zpl_xattr_security_init(ip, dir, &dentry->d_name);
290 #endif
291 if (error == 0)
292 error = zpl_init_acl(ip, dir);
293 #ifndef HAVE_TMPFILE_DENTRY
294 error = finish_open_simple(file, error);
295 #endif
296 /*
297 * don't need to handle error here, file is already in
298 * unlinked set.
299 */
300 }
301
302 spl_fstrans_unmark(cookie);
303 kmem_free(vap, sizeof (vattr_t));
304 crfree(cr);
305 ASSERT3S(error, <=, 0);
306
307 return (error);
308 }
309
310 static int
zpl_unlink(struct inode * dir,struct dentry * dentry)311 zpl_unlink(struct inode *dir, struct dentry *dentry)
312 {
313 cred_t *cr = CRED();
314 int error;
315 fstrans_cookie_t cookie;
316 zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info;
317
318 crhold(cr);
319 cookie = spl_fstrans_mark();
320 error = -zfs_remove(ITOZ(dir), dname(dentry), cr, 0);
321
322 /*
323 * For a CI FS we must invalidate the dentry to prevent the
324 * creation of negative entries.
325 */
326 if (error == 0 && zfsvfs->z_case == ZFS_CASE_INSENSITIVE)
327 d_invalidate(dentry);
328
329 spl_fstrans_unmark(cookie);
330 crfree(cr);
331 ASSERT3S(error, <=, 0);
332
333 return (error);
334 }
335
336 static int
337 #ifdef HAVE_IOPS_MKDIR_USERNS
zpl_mkdir(struct user_namespace * user_ns,struct inode * dir,struct dentry * dentry,umode_t mode)338 zpl_mkdir(struct user_namespace *user_ns, struct inode *dir,
339 struct dentry *dentry, umode_t mode)
340 #elif defined(HAVE_IOPS_MKDIR_IDMAP)
341 zpl_mkdir(struct mnt_idmap *user_ns, struct inode *dir,
342 struct dentry *dentry, umode_t mode)
343 #else
344 zpl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
345 #endif
346 {
347 cred_t *cr = CRED();
348 vattr_t *vap;
349 znode_t *zp;
350 int error;
351 fstrans_cookie_t cookie;
352 #if !(defined(HAVE_IOPS_MKDIR_USERNS) || defined(HAVE_IOPS_MKDIR_IDMAP))
353 zidmap_t *user_ns = kcred->user_ns;
354 #endif
355
356 crhold(cr);
357 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
358 zpl_vap_init(vap, dir, mode | S_IFDIR, cr, user_ns);
359
360 cookie = spl_fstrans_mark();
361 error = -zfs_mkdir(ITOZ(dir), dname(dentry), vap, &zp, cr, 0, NULL,
362 user_ns);
363 if (error == 0) {
364 error = zpl_xattr_security_init(ZTOI(zp), dir, &dentry->d_name);
365 if (error == 0)
366 error = zpl_init_acl(ZTOI(zp), dir);
367
368 if (error) {
369 (void) zfs_rmdir(ITOZ(dir), dname(dentry), NULL, cr, 0);
370 remove_inode_hash(ZTOI(zp));
371 iput(ZTOI(zp));
372 } else {
373 d_instantiate(dentry, ZTOI(zp));
374 }
375 }
376
377 spl_fstrans_unmark(cookie);
378 kmem_free(vap, sizeof (vattr_t));
379 crfree(cr);
380 ASSERT3S(error, <=, 0);
381
382 return (error);
383 }
384
385 static int
zpl_rmdir(struct inode * dir,struct dentry * dentry)386 zpl_rmdir(struct inode *dir, struct dentry *dentry)
387 {
388 cred_t *cr = CRED();
389 int error;
390 fstrans_cookie_t cookie;
391 zfsvfs_t *zfsvfs = dentry->d_sb->s_fs_info;
392
393 crhold(cr);
394 cookie = spl_fstrans_mark();
395 error = -zfs_rmdir(ITOZ(dir), dname(dentry), NULL, cr, 0);
396
397 /*
398 * For a CI FS we must invalidate the dentry to prevent the
399 * creation of negative entries.
400 */
401 if (error == 0 && zfsvfs->z_case == ZFS_CASE_INSENSITIVE)
402 d_invalidate(dentry);
403
404 spl_fstrans_unmark(cookie);
405 crfree(cr);
406 ASSERT3S(error, <=, 0);
407
408 return (error);
409 }
410
411 static int
412 #ifdef HAVE_USERNS_IOPS_GETATTR
zpl_getattr_impl(struct user_namespace * user_ns,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int query_flags)413 zpl_getattr_impl(struct user_namespace *user_ns,
414 const struct path *path, struct kstat *stat, u32 request_mask,
415 unsigned int query_flags)
416 #elif defined(HAVE_IDMAP_IOPS_GETATTR)
417 zpl_getattr_impl(struct mnt_idmap *user_ns,
418 const struct path *path, struct kstat *stat, u32 request_mask,
419 unsigned int query_flags)
420 #else
421 zpl_getattr_impl(const struct path *path, struct kstat *stat, u32 request_mask,
422 unsigned int query_flags)
423 #endif
424 {
425 int error;
426 fstrans_cookie_t cookie;
427 struct inode *ip = path->dentry->d_inode;
428 znode_t *zp __maybe_unused = ITOZ(ip);
429
430 cookie = spl_fstrans_mark();
431
432 /*
433 * XXX query_flags currently ignored.
434 */
435
436 #ifdef HAVE_GENERIC_FILLATTR_IDMAP_REQMASK
437 error = -zfs_getattr_fast(user_ns, request_mask, ip, stat);
438 #elif (defined(HAVE_USERNS_IOPS_GETATTR) || defined(HAVE_IDMAP_IOPS_GETATTR))
439 error = -zfs_getattr_fast(user_ns, ip, stat);
440 #else
441 error = -zfs_getattr_fast(kcred->user_ns, ip, stat);
442 #endif
443
444 #ifdef STATX_BTIME
445 if (request_mask & STATX_BTIME) {
446 stat->btime = zp->z_btime;
447 stat->result_mask |= STATX_BTIME;
448 }
449 #endif
450
451 #ifdef STATX_ATTR_IMMUTABLE
452 if (zp->z_pflags & ZFS_IMMUTABLE)
453 stat->attributes |= STATX_ATTR_IMMUTABLE;
454 stat->attributes_mask |= STATX_ATTR_IMMUTABLE;
455 #endif
456
457 #ifdef STATX_ATTR_APPEND
458 if (zp->z_pflags & ZFS_APPENDONLY)
459 stat->attributes |= STATX_ATTR_APPEND;
460 stat->attributes_mask |= STATX_ATTR_APPEND;
461 #endif
462
463 #ifdef STATX_ATTR_NODUMP
464 if (zp->z_pflags & ZFS_NODUMP)
465 stat->attributes |= STATX_ATTR_NODUMP;
466 stat->attributes_mask |= STATX_ATTR_NODUMP;
467 #endif
468
469 spl_fstrans_unmark(cookie);
470 ASSERT3S(error, <=, 0);
471
472 return (error);
473 }
474 ZPL_GETATTR_WRAPPER(zpl_getattr);
475
476 static int
477 #ifdef HAVE_USERNS_IOPS_SETATTR
zpl_setattr(struct user_namespace * user_ns,struct dentry * dentry,struct iattr * ia)478 zpl_setattr(struct user_namespace *user_ns, struct dentry *dentry,
479 struct iattr *ia)
480 #elif defined(HAVE_IDMAP_IOPS_SETATTR)
481 zpl_setattr(struct mnt_idmap *user_ns, struct dentry *dentry,
482 struct iattr *ia)
483 #else
484 zpl_setattr(struct dentry *dentry, struct iattr *ia)
485 #endif
486 {
487 struct inode *ip = dentry->d_inode;
488 cred_t *cr = CRED();
489 vattr_t *vap;
490 int error;
491 fstrans_cookie_t cookie;
492
493 #ifdef HAVE_SETATTR_PREPARE_USERNS
494 error = zpl_setattr_prepare(user_ns, dentry, ia);
495 #elif defined(HAVE_SETATTR_PREPARE_IDMAP)
496 error = zpl_setattr_prepare(user_ns, dentry, ia);
497 #else
498 error = zpl_setattr_prepare(zfs_init_idmap, dentry, ia);
499 #endif
500 if (error)
501 return (error);
502
503 crhold(cr);
504 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
505 vap->va_mask = ia->ia_valid & ATTR_IATTR_MASK;
506 vap->va_mode = ia->ia_mode;
507 if (ia->ia_valid & ATTR_UID)
508 #ifdef HAVE_IATTR_VFSID
509 vap->va_uid = zfs_vfsuid_to_uid(user_ns, zfs_i_user_ns(ip),
510 __vfsuid_val(ia->ia_vfsuid));
511 #else
512 vap->va_uid = KUID_TO_SUID(ia->ia_uid);
513 #endif
514 if (ia->ia_valid & ATTR_GID)
515 #ifdef HAVE_IATTR_VFSID
516 vap->va_gid = zfs_vfsgid_to_gid(user_ns, zfs_i_user_ns(ip),
517 __vfsgid_val(ia->ia_vfsgid));
518 #else
519 vap->va_gid = KGID_TO_SGID(ia->ia_gid);
520 #endif
521 vap->va_size = ia->ia_size;
522 vap->va_atime = ia->ia_atime;
523 vap->va_mtime = ia->ia_mtime;
524 vap->va_ctime = ia->ia_ctime;
525
526 if (vap->va_mask & ATTR_ATIME)
527 zpl_inode_set_atime_to_ts(ip,
528 zpl_inode_timestamp_truncate(ia->ia_atime, ip));
529
530 cookie = spl_fstrans_mark();
531 #ifdef HAVE_USERNS_IOPS_SETATTR
532 error = -zfs_setattr(ITOZ(ip), vap, 0, cr, user_ns);
533 #elif defined(HAVE_IDMAP_IOPS_SETATTR)
534 error = -zfs_setattr(ITOZ(ip), vap, 0, cr, user_ns);
535 #else
536 error = -zfs_setattr(ITOZ(ip), vap, 0, cr, zfs_init_idmap);
537 #endif
538 if (!error && (ia->ia_valid & ATTR_MODE))
539 error = zpl_chmod_acl(ip);
540
541 spl_fstrans_unmark(cookie);
542 kmem_free(vap, sizeof (vattr_t));
543 crfree(cr);
544 ASSERT3S(error, <=, 0);
545
546 return (error);
547 }
548
549 static int
550 #ifdef HAVE_IOPS_RENAME_USERNS
zpl_rename2(struct user_namespace * user_ns,struct inode * sdip,struct dentry * sdentry,struct inode * tdip,struct dentry * tdentry,unsigned int rflags)551 zpl_rename2(struct user_namespace *user_ns, struct inode *sdip,
552 struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry,
553 unsigned int rflags)
554 #elif defined(HAVE_IOPS_RENAME_IDMAP)
555 zpl_rename2(struct mnt_idmap *user_ns, struct inode *sdip,
556 struct dentry *sdentry, struct inode *tdip, struct dentry *tdentry,
557 unsigned int rflags)
558 #else
559 zpl_rename2(struct inode *sdip, struct dentry *sdentry,
560 struct inode *tdip, struct dentry *tdentry, unsigned int rflags)
561 #endif
562 {
563 cred_t *cr = CRED();
564 vattr_t *wo_vap = NULL;
565 int error;
566 fstrans_cookie_t cookie;
567 #if !(defined(HAVE_IOPS_RENAME_USERNS) || defined(HAVE_IOPS_RENAME_IDMAP))
568 zidmap_t *user_ns = kcred->user_ns;
569 #endif
570
571 crhold(cr);
572 if (rflags & RENAME_WHITEOUT) {
573 wo_vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
574 zpl_vap_init(wo_vap, sdip, S_IFCHR, cr, user_ns);
575 wo_vap->va_rdev = makedevice(0, 0);
576 }
577
578 cookie = spl_fstrans_mark();
579 error = -zfs_rename(ITOZ(sdip), dname(sdentry), ITOZ(tdip),
580 dname(tdentry), cr, 0, rflags, wo_vap, user_ns);
581 spl_fstrans_unmark(cookie);
582 if (wo_vap)
583 kmem_free(wo_vap, sizeof (vattr_t));
584 crfree(cr);
585 ASSERT3S(error, <=, 0);
586
587 return (error);
588 }
589
590 #if !defined(HAVE_IOPS_RENAME_USERNS) && \
591 !defined(HAVE_RENAME_WANTS_FLAGS) && \
592 !defined(HAVE_IOPS_RENAME_IDMAP)
593 static int
zpl_rename(struct inode * sdip,struct dentry * sdentry,struct inode * tdip,struct dentry * tdentry)594 zpl_rename(struct inode *sdip, struct dentry *sdentry,
595 struct inode *tdip, struct dentry *tdentry)
596 {
597 return (zpl_rename2(sdip, sdentry, tdip, tdentry, 0));
598 }
599 #endif
600
601 static int
602 #ifdef HAVE_IOPS_SYMLINK_USERNS
zpl_symlink(struct user_namespace * user_ns,struct inode * dir,struct dentry * dentry,const char * name)603 zpl_symlink(struct user_namespace *user_ns, struct inode *dir,
604 struct dentry *dentry, const char *name)
605 #elif defined(HAVE_IOPS_SYMLINK_IDMAP)
606 zpl_symlink(struct mnt_idmap *user_ns, struct inode *dir,
607 struct dentry *dentry, const char *name)
608 #else
609 zpl_symlink(struct inode *dir, struct dentry *dentry, const char *name)
610 #endif
611 {
612 cred_t *cr = CRED();
613 vattr_t *vap;
614 znode_t *zp;
615 int error;
616 fstrans_cookie_t cookie;
617 #if !(defined(HAVE_IOPS_SYMLINK_USERNS) || defined(HAVE_IOPS_SYMLINK_IDMAP))
618 zidmap_t *user_ns = kcred->user_ns;
619 #endif
620
621 crhold(cr);
622 vap = kmem_zalloc(sizeof (vattr_t), KM_SLEEP);
623 zpl_vap_init(vap, dir, S_IFLNK | S_IRWXUGO, cr, user_ns);
624
625 cookie = spl_fstrans_mark();
626 error = -zfs_symlink(ITOZ(dir), dname(dentry), vap,
627 (char *)name, &zp, cr, 0, user_ns);
628 if (error == 0) {
629 error = zpl_xattr_security_init(ZTOI(zp), dir, &dentry->d_name);
630 if (error) {
631 (void) zfs_remove(ITOZ(dir), dname(dentry), cr, 0);
632 remove_inode_hash(ZTOI(zp));
633 iput(ZTOI(zp));
634 } else {
635 d_instantiate(dentry, ZTOI(zp));
636 }
637 }
638
639 spl_fstrans_unmark(cookie);
640 kmem_free(vap, sizeof (vattr_t));
641 crfree(cr);
642 ASSERT3S(error, <=, 0);
643
644 return (error);
645 }
646
647 static void
zpl_put_link(void * ptr)648 zpl_put_link(void *ptr)
649 {
650 kmem_free(ptr, MAXPATHLEN);
651 }
652
653 static int
zpl_get_link_common(struct dentry * dentry,struct inode * ip,char ** link)654 zpl_get_link_common(struct dentry *dentry, struct inode *ip, char **link)
655 {
656 fstrans_cookie_t cookie;
657 cred_t *cr = CRED();
658 int error;
659
660 crhold(cr);
661 *link = NULL;
662
663 struct iovec iov;
664 iov.iov_len = MAXPATHLEN;
665 iov.iov_base = kmem_zalloc(MAXPATHLEN, KM_SLEEP);
666
667 zfs_uio_t uio;
668 zfs_uio_iovec_init(&uio, &iov, 1, 0, UIO_SYSSPACE, MAXPATHLEN - 1, 0);
669
670 cookie = spl_fstrans_mark();
671 error = -zfs_readlink(ip, &uio, cr);
672 spl_fstrans_unmark(cookie);
673 crfree(cr);
674
675 if (error)
676 kmem_free(iov.iov_base, MAXPATHLEN);
677 else
678 *link = iov.iov_base;
679
680 return (error);
681 }
682
683 static const char *
zpl_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * done)684 zpl_get_link(struct dentry *dentry, struct inode *inode,
685 struct delayed_call *done)
686 {
687 char *link = NULL;
688 int error;
689
690 if (!dentry)
691 return (ERR_PTR(-ECHILD));
692
693 error = zpl_get_link_common(dentry, inode, &link);
694 if (error)
695 return (ERR_PTR(error));
696
697 set_delayed_call(done, zpl_put_link, link);
698
699 return (link);
700 }
701
702 static int
zpl_link(struct dentry * old_dentry,struct inode * dir,struct dentry * dentry)703 zpl_link(struct dentry *old_dentry, struct inode *dir, struct dentry *dentry)
704 {
705 cred_t *cr = CRED();
706 struct inode *ip = old_dentry->d_inode;
707 int error;
708 fstrans_cookie_t cookie;
709
710 if (ip->i_nlink >= ZFS_LINK_MAX)
711 return (-EMLINK);
712
713 crhold(cr);
714 zpl_inode_set_ctime_to_ts(ip, current_time(ip));
715 /* Must have an existing ref, so igrab() cannot return NULL */
716 VERIFY3P(igrab(ip), !=, NULL);
717
718 cookie = spl_fstrans_mark();
719 error = -zfs_link(ITOZ(dir), ITOZ(ip), dname(dentry), cr, 0);
720 if (error) {
721 iput(ip);
722 goto out;
723 }
724
725 d_instantiate(dentry, ip);
726 out:
727 spl_fstrans_unmark(cookie);
728 crfree(cr);
729 ASSERT3S(error, <=, 0);
730
731 return (error);
732 }
733
734 const struct inode_operations zpl_inode_operations = {
735 .setattr = zpl_setattr,
736 .getattr = zpl_getattr,
737 .listxattr = zpl_xattr_list,
738 #if defined(CONFIG_FS_POSIX_ACL)
739 .set_acl = zpl_set_acl,
740 #if defined(HAVE_GET_INODE_ACL)
741 .get_inode_acl = zpl_get_acl,
742 #else
743 .get_acl = zpl_get_acl,
744 #endif /* HAVE_GET_INODE_ACL */
745 #endif /* CONFIG_FS_POSIX_ACL */
746 };
747
748 const struct inode_operations zpl_dir_inode_operations = {
749 .create = zpl_create,
750 .lookup = zpl_lookup,
751 .link = zpl_link,
752 .unlink = zpl_unlink,
753 .symlink = zpl_symlink,
754 .mkdir = zpl_mkdir,
755 .rmdir = zpl_rmdir,
756 .mknod = zpl_mknod,
757 #if defined(HAVE_RENAME_WANTS_FLAGS) || defined(HAVE_IOPS_RENAME_USERNS)
758 .rename = zpl_rename2,
759 #elif defined(HAVE_IOPS_RENAME_IDMAP)
760 .rename = zpl_rename2,
761 #else
762 .rename = zpl_rename,
763 #endif
764 .tmpfile = zpl_tmpfile,
765 .setattr = zpl_setattr,
766 .getattr = zpl_getattr,
767 .listxattr = zpl_xattr_list,
768 #if defined(CONFIG_FS_POSIX_ACL)
769 .set_acl = zpl_set_acl,
770 #if defined(HAVE_GET_INODE_ACL)
771 .get_inode_acl = zpl_get_acl,
772 #else
773 .get_acl = zpl_get_acl,
774 #endif /* HAVE_GET_INODE_ACL */
775 #endif /* CONFIG_FS_POSIX_ACL */
776 };
777
778 const struct inode_operations zpl_symlink_inode_operations = {
779 .get_link = zpl_get_link,
780 .setattr = zpl_setattr,
781 .getattr = zpl_getattr,
782 .listxattr = zpl_xattr_list,
783 };
784
785 const struct inode_operations zpl_special_inode_operations = {
786 .setattr = zpl_setattr,
787 .getattr = zpl_getattr,
788 .listxattr = zpl_xattr_list,
789 #if defined(CONFIG_FS_POSIX_ACL)
790 .set_acl = zpl_set_acl,
791 #if defined(HAVE_GET_INODE_ACL)
792 .get_inode_acl = zpl_get_acl,
793 #else
794 .get_acl = zpl_get_acl,
795 #endif /* HAVE_GET_INODE_ACL */
796 #endif /* CONFIG_FS_POSIX_ACL */
797 };
798