ffs_extattr.c revision 1.5 1 /* $NetBSD: ffs_extattr.c,v 1.5 2020/05/16 18:31:53 christos Exp $ */
2
3 /*-
4 * SPDX-License-Identifier: (BSD-2-Clause-FreeBSD AND BSD-3-Clause)
5 *
6 * Copyright (c) 2002, 2003 Networks Associates Technology, Inc.
7 * All rights reserved.
8 *
9 * This software was developed for the FreeBSD Project by Marshall
10 * Kirk McKusick and Network Associates Laboratories, the Security
11 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
12 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
13 * research program
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * Copyright (c) 1982, 1986, 1989, 1993
37 * The Regents of the University of California. All rights reserved.
38 *
39 * Redistribution and use in source and binary forms, with or without
40 * modification, are permitted provided that the following conditions
41 * are met:
42 * 1. Redistributions of source code must retain the above copyright
43 * notice, this list of conditions and the following disclaimer.
44 * 2. Redistributions in binary form must reproduce the above copyright
45 * notice, this list of conditions and the following disclaimer in the
46 * documentation and/or other materials provided with the distribution.
47 * 3. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * from: @(#)ufs_readwrite.c 8.11 (Berkeley) 5/8/95
64 * from: $FreeBSD: .../ufs/ufs_readwrite.c,v 1.96 2002/08/12 09:22:11 phk ...
65 * @(#)ffs_vnops.c 8.15 (Berkeley) 5/14/95
66 */
67
68 #include <sys/cdefs.h>
69 __KERNEL_RCSID(0, "$NetBSD: ffs_extattr.c,v 1.5 2020/05/16 18:31:53 christos Exp $");
70
71 #if defined(_KERNEL_OPT)
72 #include "opt_ffs.h"
73 #include "opt_wapbl.h"
74 #endif
75
76 #include <sys/param.h>
77 #include <sys/systm.h>
78 #include <sys/resourcevar.h>
79 #include <sys/kernel.h>
80 #include <sys/file.h>
81 #include <sys/stat.h>
82 #include <sys/buf.h>
83 #include <sys/event.h>
84 #include <sys/extattr.h>
85 #include <sys/kauth.h>
86 #include <sys/proc.h>
87 #include <sys/mount.h>
88 #include <sys/vnode.h>
89 #include <sys/malloc.h>
90 #include <sys/pool.h>
91 #include <sys/signalvar.h>
92 #include <sys/kauth.h>
93 #include <sys/wapbl.h>
94
95 #include <miscfs/fifofs/fifo.h>
96 #include <miscfs/genfs/genfs.h>
97 #include <miscfs/specfs/specdev.h>
98
99 #include <ufs/ufs/inode.h>
100 #include <ufs/ufs/dir.h>
101 #include <ufs/ufs/ufs_extern.h>
102 #include <ufs/ufs/ufsmount.h>
103 #include <ufs/ufs/ufs_wapbl.h>
104
105 #include <ufs/ffs/fs.h>
106 #include <ufs/ffs/ffs_extern.h>
107
108 #include <uvm/uvm.h>
109
110 #define ALIGNED_TO(ptr, s) \
111 (((uintptr_t)(ptr) & (_Alignof(s) - 1)) == 0)
112 #define uoff_t uintmax_t
113 #define ITOFS(ip) (ip)->i_fs
114 #define i_din2 i_din.ffs2_din
115 #define VI_LOCK(vp) mutex_enter((vp)->v_interlock)
116 #define VI_UNLOCK(vp) mutex_exit((vp)->v_interlock)
117 #define UFS_INODE_SET_FLAG(ip, f) ((ip)->i_flag |= (f))
118 #define ASSERT_VOP_ELOCKED(vp, m) KASSERT(VOP_ISLOCKED(vp))
119 #define I_IS_UFS2(ip) (ITOFS(ip)->fs_magic == FS_UFS2_MAGIC)
120 #define lblktosize(fs, o) ffs_lblktosize(fs, o)
121 #define lblkno(fs, o) ffs_lblkno(fs, o)
122 #define blkoff(fs, o) ffs_blkoff(fs, o)
123 #define sblksize(fs, o, lbn) ffs_sblksize(fs, o, lbn)
124 typedef mode_t accmode_t; /* so that it breaks soon */
125 typedef daddr_t ufs_lbn_t;
126 #define msleep(chan, mtx, pri, wmesg, timeo) \
127 mtsleep((chan), (pri), (wmesg), (timeo), *(mtx))
128 #define vm_page_count_severe() 0
129 #define buf_dirty_count_severe() 0
130 #define BA_CLRBUF B_CLRBUF
131 #define IO_ASYNC 0
132 #define vfs_bio_brelse(bp, ioflag) brelse(bp, 0)
133 #define vfs_bio_clrbuf(bp) clrbuf(bp)
134 #define vfs_bio_set_flags(bp, ioflag) __nothing
135
136 /*
137 * Extended attribute area reading.
138 */
139 static int
140 ffs_extread(struct vnode *vp, struct uio *uio, int ioflag)
141 {
142 struct inode *ip;
143 struct ufs2_dinode *dp;
144 struct fs *fs;
145 struct buf *bp;
146 ufs_lbn_t lbn, nextlbn;
147 off_t bytesinfile;
148 long size, xfersize, blkoffset;
149 ssize_t orig_resid;
150 int error;
151
152 ip = VTOI(vp);
153 fs = ITOFS(ip);
154 dp = ip->i_din2;
155
156 #ifdef INVARIANTS
157 if (uio->uio_rw != UIO_READ || fs->fs_magic != FS_UFS2_MAGIC)
158 panic("ffs_extread: mode");
159
160 #endif
161 orig_resid = uio->uio_resid;
162 KASSERT(orig_resid >= 0);
163 if (orig_resid == 0)
164 return (0);
165 KASSERT(uio->uio_offset >= 0);
166
167 for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
168 if ((bytesinfile = dp->di_extsize - uio->uio_offset) <= 0)
169 break;
170 lbn = lblkno(fs, uio->uio_offset);
171 nextlbn = lbn + 1;
172
173 /*
174 * size of buffer. The buffer representing the
175 * end of the file is rounded up to the size of
176 * the block type ( fragment or full block,
177 * depending ).
178 */
179 size = sblksize(fs, dp->di_extsize, lbn);
180 blkoffset = blkoff(fs, uio->uio_offset);
181
182 /*
183 * The amount we want to transfer in this iteration is
184 * one FS block less the amount of the data before
185 * our startpoint (duh!)
186 */
187 xfersize = fs->fs_bsize - blkoffset;
188
189 /*
190 * But if we actually want less than the block,
191 * or the file doesn't have a whole block more of data,
192 * then use the lesser number.
193 */
194 if (uio->uio_resid < xfersize)
195 xfersize = uio->uio_resid;
196 if (bytesinfile < xfersize)
197 xfersize = bytesinfile;
198
199 if (lblktosize(fs, nextlbn) >= dp->di_extsize) {
200 /*
201 * Don't do readahead if this is the end of the info.
202 */
203 error = bread(vp, -1 - lbn, size, 0, &bp);
204 } else {
205 /*
206 * If we have a second block, then
207 * fire off a request for a readahead
208 * as well as a read. Note that the 4th and 5th
209 * arguments point to arrays of the size specified in
210 * the 6th argument.
211 */
212 u_int nextsize = sblksize(fs, dp->di_extsize, nextlbn);
213
214 nextlbn = -1 - nextlbn;
215 error = breadn(vp, -1 - lbn,
216 size, &nextlbn, &nextsize, 1, 0, &bp);
217 }
218 if (error) {
219 brelse(bp, 0);
220 bp = NULL;
221 break;
222 }
223
224 /*
225 * We should only get non-zero b_resid when an I/O error
226 * has occurred, which should cause us to break above.
227 * However, if the short read did not cause an error,
228 * then we want to ensure that we do not uiomove bad
229 * or uninitialized data.
230 */
231 size -= bp->b_resid;
232 if (size < xfersize) {
233 if (size == 0)
234 break;
235 xfersize = size;
236 }
237
238 error = uiomove((char *)bp->b_data + blkoffset,
239 (int)xfersize, uio);
240 if (error)
241 break;
242 vfs_bio_brelse(bp, ioflag);
243 }
244
245 /*
246 * This can only happen in the case of an error
247 * because the loop above resets bp to NULL on each iteration
248 * and on normal completion has not set a new value into it.
249 * so it must have come from a 'break' statement
250 */
251 if (bp != NULL)
252 vfs_bio_brelse(bp, ioflag);
253 return (error);
254 }
255 /*
256 * Extended attribute area writing.
257 */
258 static int
259 ffs_extwrite(struct vnode *vp, struct uio *uio, int ioflag, kauth_cred_t ucred)
260 {
261 struct inode *ip;
262 struct ufs2_dinode *dp;
263 struct fs *fs;
264 struct buf *bp;
265 ufs_lbn_t lbn;
266 off_t osize;
267 ssize_t resid;
268 int blkoffset, error, flags, size, xfersize;
269
270 ip = VTOI(vp);
271 fs = ITOFS(ip);
272 dp = ip->i_din2;
273
274 #ifdef INVARIANTS
275 if (uio->uio_rw != UIO_WRITE || fs->fs_magic != FS_UFS2_MAGIC)
276 panic("ffs_extwrite: mode");
277 #endif
278
279 if (ioflag & IO_APPEND)
280 uio->uio_offset = dp->di_extsize;
281 KASSERT(uio->uio_offset >= 0);
282 if ((uoff_t)uio->uio_offset + uio->uio_resid >
283 UFS_NXADDR * fs->fs_bsize)
284 return (EFBIG);
285
286 resid = uio->uio_resid;
287 osize = dp->di_extsize;
288 flags = IO_EXT;
289 if (ioflag & IO_SYNC)
290 flags |= IO_SYNC;
291
292 if ((error = UFS_WAPBL_BEGIN(vp->v_mount)) != 0)
293 return error;
294
295 for (error = 0; uio->uio_resid > 0;) {
296 lbn = lblkno(fs, uio->uio_offset);
297 blkoffset = blkoff(fs, uio->uio_offset);
298 xfersize = fs->fs_bsize - blkoffset;
299 if (uio->uio_resid < xfersize)
300 xfersize = uio->uio_resid;
301
302 /*
303 * We must perform a read-before-write if the transfer size
304 * does not cover the entire buffer.
305 */
306 if (fs->fs_bsize > xfersize)
307 flags |= BA_CLRBUF;
308 else
309 flags &= ~BA_CLRBUF;
310 error = UFS_BALLOC(vp, uio->uio_offset, xfersize,
311 ucred, flags, &bp);
312 if (error != 0)
313 break;
314 /*
315 * If the buffer is not valid we have to clear out any
316 * garbage data from the pages instantiated for the buffer.
317 * If we do not, a failed uiomove() during a write can leave
318 * the prior contents of the pages exposed to a userland
319 * mmap(). XXX deal with uiomove() errors a better way.
320 */
321 if ((bp->b_flags & BC_NOCACHE) && fs->fs_bsize <= xfersize)
322 vfs_bio_clrbuf(bp);
323
324 if (uio->uio_offset + xfersize > dp->di_extsize)
325 dp->di_extsize = uio->uio_offset + xfersize;
326
327 size = sblksize(fs, dp->di_extsize, lbn) - bp->b_resid;
328 if (size < xfersize)
329 xfersize = size;
330
331 error =
332 uiomove((char *)bp->b_data + blkoffset, (int)xfersize, uio);
333
334 vfs_bio_set_flags(bp, ioflag);
335
336 /*
337 * If IO_SYNC each buffer is written synchronously. Otherwise
338 * if we have a severe page deficiency write the buffer
339 * asynchronously. Otherwise try to cluster, and if that
340 * doesn't do it then either do an async write (if O_DIRECT),
341 * or a delayed write (if not).
342 */
343 if (ioflag & IO_SYNC) {
344 (void)bwrite(bp);
345 } else if (vm_page_count_severe() ||
346 buf_dirty_count_severe() ||
347 xfersize + blkoffset == fs->fs_bsize ||
348 (ioflag & (IO_ASYNC | IO_DIRECT)))
349 bawrite(bp);
350 else
351 bdwrite(bp);
352 if (error || xfersize == 0)
353 break;
354 UFS_INODE_SET_FLAG(ip, IN_CHANGE);
355 }
356 /*
357 * If we successfully wrote any data, and we are not the superuser
358 * we clear the setuid and setgid bits as a precaution against
359 * tampering.
360 */
361 if ((ip->i_mode & (ISUID | ISGID)) && resid > uio->uio_resid && ucred) {
362 ip->i_mode &= ~(ISUID | ISGID);
363 dp->di_mode = ip->i_mode;
364 }
365 if (error) {
366 if (ioflag & IO_UNIT) {
367 (void)ffs_truncate(vp, osize,
368 IO_EXT | (ioflag&IO_SYNC), ucred);
369 uio->uio_offset -= resid - uio->uio_resid;
370 uio->uio_resid = resid;
371 }
372 } else if (resid > uio->uio_resid && (ioflag & IO_SYNC))
373 error = ffs_update(vp, NULL, NULL, UPDATE_WAIT);
374 UFS_WAPBL_END(vp->v_mount);
375 return (error);
376 }
377
378 /*
379 * Vnode operating to retrieve a named extended attribute.
380 *
381 * Locate a particular EA (nspace:name) in the area (ptr:length), and return
382 * the length of the EA, and possibly the pointer to the entry and to the data.
383 */
384 static int
385 ffs_findextattr(u_char *ptr, u_int length, int nspace, const char *name,
386 struct extattr **eapp, u_char **eac)
387 {
388 struct extattr *eap, *eaend;
389 size_t nlen;
390
391 nlen = strlen(name);
392 KASSERT(ALIGNED_TO(ptr, struct extattr));
393 eap = (struct extattr *)ptr;
394 eaend = (struct extattr *)(ptr + length);
395 for (; eap < eaend; eap = EXTATTR_NEXT(eap)) {
396 /* make sure this entry is complete */
397 if (EXTATTR_NEXT(eap) > eaend)
398 break;
399 if (eap->ea_namespace != nspace || eap->ea_namelength != nlen
400 || memcmp(eap->ea_name, name, nlen) != 0)
401 continue;
402 if (eapp != NULL)
403 *eapp = eap;
404 if (eac != NULL)
405 *eac = EXTATTR_CONTENT(eap);
406 return (EXTATTR_CONTENT_SIZE(eap));
407 }
408 return (-1);
409 }
410
411 static int
412 ffs_rdextattr(u_char **p, struct vnode *vp, int extra)
413 {
414 struct inode *ip;
415 struct ufs2_dinode *dp;
416 struct fs *fs;
417 struct uio luio;
418 struct iovec liovec;
419 u_int easize;
420 int error;
421 u_char *eae;
422
423 ip = VTOI(vp);
424 fs = ITOFS(ip);
425 dp = ip->i_din2;
426 easize = dp->di_extsize;
427 if ((uoff_t)easize + extra > UFS_NXADDR * fs->fs_bsize)
428 return (EFBIG);
429
430 eae = malloc(easize + extra, M_TEMP, M_WAITOK);
431
432 liovec.iov_base = eae;
433 liovec.iov_len = easize;
434 luio.uio_iov = &liovec;
435 luio.uio_iovcnt = 1;
436 luio.uio_offset = 0;
437 luio.uio_resid = easize;
438 luio.uio_vmspace = vmspace_kernel();
439 luio.uio_rw = UIO_READ;
440
441 error = ffs_extread(vp, &luio, IO_EXT | IO_SYNC);
442 if (error) {
443 free(eae, M_TEMP);
444 return(error);
445 }
446 *p = eae;
447 return (0);
448 }
449
450 static void
451 ffs_lock_ea(struct vnode *vp)
452 {
453 genfs_node_wrlock(vp);
454 }
455
456 static void
457 ffs_unlock_ea(struct vnode *vp)
458 {
459 genfs_node_unlock(vp);
460 }
461
462 static int
463 ffs_open_ea(struct vnode *vp, kauth_cred_t cred)
464 {
465 struct inode *ip;
466 struct ufs2_dinode *dp;
467 int error;
468
469 ip = VTOI(vp);
470
471 ffs_lock_ea(vp);
472 if (ip->i_ea_area != NULL) {
473 ip->i_ea_refs++;
474 ffs_unlock_ea(vp);
475 return (0);
476 }
477 dp = ip->i_din2;
478 error = ffs_rdextattr(&ip->i_ea_area, vp, 0);
479 if (error) {
480 ffs_unlock_ea(vp);
481 return (error);
482 }
483 ip->i_ea_len = dp->di_extsize;
484 ip->i_ea_error = 0;
485 ip->i_ea_refs++;
486 ffs_unlock_ea(vp);
487 return (0);
488 }
489
490 /*
491 * Vnode extattr transaction commit/abort
492 */
493 static int
494 ffs_close_ea(struct vnode *vp, int commit, kauth_cred_t cred)
495 {
496 struct inode *ip;
497 struct uio luio;
498 struct iovec liovec;
499 int error;
500 struct ufs2_dinode *dp;
501
502 ip = VTOI(vp);
503
504 if (commit)
505 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
506 else
507 KASSERT(VOP_ISLOCKED(vp));
508 ffs_lock_ea(vp);
509 if (ip->i_ea_area == NULL) {
510 ffs_unlock_ea(vp);
511 return (EINVAL);
512 }
513 dp = ip->i_din2;
514 error = ip->i_ea_error;
515 if (commit && error == 0) {
516 ASSERT_VOP_ELOCKED(vp, "ffs_close_ea commit");
517 if (cred == NOCRED)
518 cred = lwp0.l_cred;
519 liovec.iov_base = ip->i_ea_area;
520 liovec.iov_len = ip->i_ea_len;
521 luio.uio_iov = &liovec;
522 luio.uio_iovcnt = 1;
523 luio.uio_offset = 0;
524 luio.uio_resid = ip->i_ea_len;
525 luio.uio_vmspace = vmspace_kernel();
526 luio.uio_rw = UIO_WRITE;
527
528 /* XXX: I'm not happy about truncating to zero size */
529 if (ip->i_ea_len < dp->di_extsize) {
530 if ((error = UFS_WAPBL_BEGIN(vp->v_mount)) != 0) {
531 ffs_unlock_ea(vp);
532 return error;
533 }
534 error = ffs_truncate(vp, 0, IO_EXT, cred);
535 UFS_WAPBL_END(vp->v_mount);
536 }
537 error = ffs_extwrite(vp, &luio, IO_EXT | IO_SYNC, cred);
538 }
539 if (--ip->i_ea_refs == 0) {
540 free(ip->i_ea_area, M_TEMP);
541 ip->i_ea_area = NULL;
542 ip->i_ea_len = 0;
543 ip->i_ea_error = 0;
544 }
545 ffs_unlock_ea(vp);
546 return (error);
547 }
548
549 /*
550 * Vnode extattr strategy routine for fifos.
551 *
552 * We need to check for a read or write of the external attributes.
553 * Otherwise we just fall through and do the usual thing.
554 */
555 int
556 ffsext_strategy(void *v)
557 {
558 struct vop_strategy_args /* {
559 struct vnodeop_desc *a_desc;
560 struct vnode *a_vp;
561 struct buf *a_bp;
562 } */ *ap = v;
563 struct vnode *vp;
564 daddr_t lbn;
565
566 vp = ap->a_vp;
567 lbn = ap->a_bp->b_lblkno;
568 if (I_IS_UFS2(VTOI(vp)) && lbn < 0 && lbn >= -UFS_NXADDR)
569 return ufs_strategy(ap);
570 if (vp->v_type == VFIFO)
571 return vn_fifo_bypass(ap);
572 panic("spec nodes went here");
573 }
574
575 /*
576 * Vnode extattr transaction commit/abort
577 */
578 int
579 ffs_openextattr(void *v)
580 {
581 struct vop_openextattr_args /* {
582 struct vnode *a_vp;
583 kauth_cred_t a_cred;
584 struct proc *a_p;
585 } */ *ap = v;
586 struct inode *ip = VTOI(ap->a_vp);
587 struct fs *fs = ip->i_fs;
588
589 /* Not supported for UFS1 file systems. */
590 if (fs->fs_magic == FS_UFS1_MAGIC)
591 return (EOPNOTSUPP);
592
593 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
594 return (EOPNOTSUPP);
595
596 return (ffs_open_ea(ap->a_vp, ap->a_cred));
597 }
598
599 /*
600 * Vnode extattr transaction commit/abort
601 */
602 int
603 ffs_closeextattr(void *v)
604 {
605 struct vop_closeextattr_args /* {
606 struct vnode *a_vp;
607 int a_commit;
608 kauth_cred_t a_cred;
609 struct proc *a_p;
610 } */ *ap = v;
611 struct inode *ip = VTOI(ap->a_vp);
612 struct fs *fs = ip->i_fs;
613
614 /* Not supported for UFS1 file systems. */
615 if (fs->fs_magic == FS_UFS1_MAGIC)
616 return (EOPNOTSUPP);
617
618 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
619 return (EOPNOTSUPP);
620
621 if (ap->a_commit && (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY))
622 return (EROFS);
623
624 return (ffs_close_ea(ap->a_vp, ap->a_commit, ap->a_cred));
625 }
626
627 /*
628 * Vnode operation to retrieve a named extended attribute.
629 */
630 int
631 ffs_getextattr(void *v)
632 {
633 struct vop_getextattr_args /* {
634 struct vnode *a_vp;
635 int a_attrnamespace;
636 const char *a_name;
637 struct uio *a_uio;
638 size_t *a_size;
639 kauth_cred_t a_cred;
640 struct proc *a_p;
641 } */ *ap = v;
642 struct vnode *vp = ap->a_vp;
643 struct inode *ip = VTOI(vp);
644 struct fs *fs = ip->i_fs;
645
646 KASSERT(VOP_ISLOCKED(vp));
647 if (fs->fs_magic == FS_UFS1_MAGIC) {
648 return ufs_getextattr(ap);
649 }
650
651 u_char *eae, *p;
652 unsigned easize;
653 int error, ealen;
654
655 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
656 return (EOPNOTSUPP);
657
658 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
659 ap->a_cred, VREAD);
660 if (error)
661 return (error);
662
663 error = ffs_open_ea(ap->a_vp, ap->a_cred);
664 if (error)
665 return (error);
666
667 eae = ip->i_ea_area;
668 easize = ip->i_ea_len;
669
670 ealen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
671 NULL, &p);
672 if (ealen >= 0) {
673 error = 0;
674 if (ap->a_size != NULL)
675 *ap->a_size = ealen;
676 else if (ap->a_uio != NULL)
677 error = uiomove(p, ealen, ap->a_uio);
678 } else
679 error = ENOATTR;
680
681 ffs_close_ea(ap->a_vp, 0, ap->a_cred);
682 return (error);
683 }
684
685 /*
686 * Vnode operation to set a named attribute.
687 */
688 int
689 ffs_setextattr(void *v)
690 {
691 struct vop_setextattr_args /* {
692 struct vnode *a_vp;
693 int a_attrnamespace;
694 const char *a_name;
695 struct uio *a_uio;
696 kauth_cred_t a_cred;
697 struct proc *a_p;
698 } */ *ap = v;
699 struct vnode *vp = ap->a_vp;
700 struct inode *ip = VTOI(vp);
701 struct fs *fs = ip->i_fs;
702
703 KASSERT(VOP_ISLOCKED(vp) == LK_EXCLUSIVE);
704 if (fs->fs_magic == FS_UFS1_MAGIC) {
705 return ufs_setextattr(ap);
706 }
707
708 struct extattr *eap;
709 uint32_t ealength, ul;
710 ssize_t ealen;
711 int olen, eapad1, eapad2, error, i, easize;
712 u_char *eae;
713 void *tmp;
714
715 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
716 return (EOPNOTSUPP);
717
718 if (strlen(ap->a_name) == 0)
719 return (EINVAL);
720
721 /* XXX Now unsupported API to delete EAs using NULL uio. */
722 if (ap->a_uio == NULL)
723 return (EOPNOTSUPP);
724
725 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
726 return (EROFS);
727
728 ealen = ap->a_uio->uio_resid;
729 if (ealen < 0 || ealen > lblktosize(fs, UFS_NXADDR))
730 return (EINVAL);
731
732 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
733 ap->a_cred, VWRITE);
734 if (error) {
735
736 /*
737 * ffs_lock_ea is not needed there, because the vnode
738 * must be exclusively locked.
739 */
740 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
741 ip->i_ea_error = error;
742 return (error);
743 }
744
745 error = ffs_open_ea(ap->a_vp, ap->a_cred);
746 if (error)
747 return (error);
748
749 ealength = sizeof(uint32_t) + 3 + strlen(ap->a_name);
750 eapad1 = roundup2(ealength, 8) - ealength;
751 eapad2 = roundup2(ealen, 8) - ealen;
752 ealength += eapad1 + ealen + eapad2;
753
754 /*
755 * CEM: rewrites of the same size or smaller could be done in-place
756 * instead. (We don't acquire any fine-grained locks in here either,
757 * so we could also do bigger writes in-place.)
758 */
759 eae = malloc(ip->i_ea_len + ealength, M_TEMP, M_WAITOK);
760 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
761 easize = ip->i_ea_len;
762
763 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
764 &eap, NULL);
765 if (olen == -1) {
766 /* new, append at end */
767 KASSERT(ALIGNED_TO(eae + easize, struct extattr));
768 eap = (struct extattr *)(eae + easize);
769 easize += ealength;
770 } else {
771 ul = eap->ea_length;
772 i = (u_char *)EXTATTR_NEXT(eap) - eae;
773 if (ul != ealength) {
774 bcopy(EXTATTR_NEXT(eap), (u_char *)eap + ealength,
775 easize - i);
776 easize += (ealength - ul);
777 }
778 }
779 if (easize > lblktosize(fs, UFS_NXADDR)) {
780 free(eae, M_TEMP);
781 ffs_close_ea(ap->a_vp, 0, ap->a_cred);
782 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
783 ip->i_ea_error = ENOSPC;
784 return (ENOSPC);
785 }
786 eap->ea_length = ealength;
787 eap->ea_namespace = ap->a_attrnamespace;
788 eap->ea_contentpadlen = eapad2;
789 eap->ea_namelength = strlen(ap->a_name);
790 memcpy(eap->ea_name, ap->a_name, strlen(ap->a_name));
791 bzero(&eap->ea_name[strlen(ap->a_name)], eapad1);
792 error = uiomove(EXTATTR_CONTENT(eap), ealen, ap->a_uio);
793 if (error) {
794 free(eae, M_TEMP);
795 ffs_close_ea(ap->a_vp, 0, ap->a_cred);
796 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
797 ip->i_ea_error = error;
798 return (error);
799 }
800 bzero((u_char *)EXTATTR_CONTENT(eap) + ealen, eapad2);
801
802 tmp = ip->i_ea_area;
803 ip->i_ea_area = eae;
804 ip->i_ea_len = easize;
805 free(tmp, M_TEMP);
806 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred);
807 return (error);
808 }
809
810 /*
811 * Vnode operation to retrieve extended attributes on a vnode.
812 */
813 int
814 ffs_listextattr(void *v)
815 {
816 struct vop_listextattr_args /* {
817 struct vnode *a_vp;
818 int a_attrnamespace;
819 struct uio *a_uio;
820 size_t *a_size;
821 kauth_cred_t a_cred;
822 struct proc *a_p;
823 } */ *ap = v;
824 struct inode *ip = VTOI(ap->a_vp);
825 struct fs *fs = ip->i_fs;
826
827 if (fs->fs_magic == FS_UFS1_MAGIC) {
828 return ufs_listextattr(ap);
829 }
830
831 struct extattr *eap, *eaend;
832 int error, ealen;
833
834 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
835 return (EOPNOTSUPP);
836
837 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
838 ap->a_cred, VREAD);
839 if (error)
840 return (error);
841
842 error = ffs_open_ea(ap->a_vp, ap->a_cred);
843 if (error)
844 return (error);
845
846 error = 0;
847 if (ap->a_size != NULL)
848 *ap->a_size = 0;
849
850 KASSERT(ALIGNED_TO(ip->i_ea_area, struct extattr));
851 eap = (struct extattr *)ip->i_ea_area;
852 eaend = (struct extattr *)(ip->i_ea_area + ip->i_ea_len);
853 for (; error == 0 && eap < eaend; eap = EXTATTR_NEXT(eap)) {
854 /* make sure this entry is complete */
855 if (EXTATTR_NEXT(eap) > eaend)
856 break;
857 if (eap->ea_namespace != ap->a_attrnamespace)
858 continue;
859
860 ealen = eap->ea_namelength;
861 if (ap->a_size != NULL)
862 *ap->a_size += ealen + 1;
863 else if (ap->a_uio != NULL)
864 error = uiomove(&eap->ea_namelength, ealen + 1,
865 ap->a_uio);
866 }
867
868 ffs_close_ea(ap->a_vp, 0, ap->a_cred);
869 return (error);
870 }
871
872 /*
873 * Vnode operation to remove a named attribute.
874 */
875 int
876 ffs_deleteextattr(void *v)
877 {
878 struct vop_deleteextattr_args /* {
879 struct vnode *a_vp;
880 int a_attrnamespace;
881 kauth_cred_t a_cred;
882 struct proc *a_p;
883 } */ *ap = v;
884 struct vnode *vp = ap->a_vp;
885 struct inode *ip = VTOI(vp);
886 struct fs *fs = ip->i_fs;
887
888 if (fs->fs_magic == FS_UFS1_MAGIC) {
889 return ufs_deleteextattr(ap);
890 }
891
892 struct extattr *eap;
893 uint32_t ul;
894 int olen, error, i, easize;
895 u_char *eae;
896 void *tmp;
897
898 if (ap->a_vp->v_type == VCHR || ap->a_vp->v_type == VBLK)
899 return (EOPNOTSUPP);
900
901 if (strlen(ap->a_name) == 0)
902 return (EINVAL);
903
904 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
905 return (EROFS);
906
907 error = extattr_check_cred(ap->a_vp, ap->a_attrnamespace,
908 ap->a_cred, VWRITE);
909 if (error) {
910 /*
911 * ffs_lock_ea is not needed there, because the vnode
912 * must be exclusively locked.
913 */
914 if (ip->i_ea_area != NULL && ip->i_ea_error == 0)
915 ip->i_ea_error = error;
916 return (error);
917 }
918
919 error = ffs_open_ea(ap->a_vp, ap->a_cred);
920 if (error)
921 return (error);
922
923 /* CEM: delete could be done in-place instead */
924 eae = malloc(ip->i_ea_len, M_TEMP, M_WAITOK);
925 bcopy(ip->i_ea_area, eae, ip->i_ea_len);
926 easize = ip->i_ea_len;
927
928 olen = ffs_findextattr(eae, easize, ap->a_attrnamespace, ap->a_name,
929 &eap, NULL);
930 if (olen == -1) {
931 /* delete but nonexistent */
932 free(eae, M_TEMP);
933 ffs_close_ea(ap->a_vp, 0, ap->a_cred);
934 return (ENOATTR);
935 }
936 ul = eap->ea_length;
937 i = (u_char *)EXTATTR_NEXT(eap) - eae;
938 bcopy(EXTATTR_NEXT(eap), eap, easize - i);
939 easize -= ul;
940
941 tmp = ip->i_ea_area;
942 ip->i_ea_area = eae;
943 ip->i_ea_len = easize;
944 free(tmp, M_TEMP);
945 error = ffs_close_ea(ap->a_vp, 1, ap->a_cred);
946 return error;
947 }
948