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.
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.
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]
22 * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2017 by Delphix. All rights reserved.
26 #include <sys/types.h>
27 #include <sys/t_lock.h>
28 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysmacros.h>
32 #include <sys/resource.h>
33 #include <sys/signal.h>
39 #include <sys/vnode.h>
44 #include <sys/fcntl.h>
45 #include <sys/flock.h>
50 #include <sys/errno.h>
53 #include <sys/pathname.h>
54 #include <sys/debug.h>
55 #include <sys/vmsystm.h>
56 #include <sys/cmn_err.h>
57 #include <sys/dirent.h>
58 #include <sys/errno.h>
59 #include <sys/modctl.h>
60 #include <sys/statvfs.h>
61 #include <sys/mount.h>
62 #include <sys/sunddi.h>
63 #include <sys/bootconf.h>
64 #include <sys/policy.h>
71 #include <vm/seg_map.h>
72 #include <vm/seg_kmem.h>
73 #include <vm/seg_vn.h>
79 #include <sys/fs_subr.h>
82 #include <sys/fs/udf_volume.h>
83 #include <sys/fs/udf_inode.h>
85 extern const struct vnodeops udf_vnodeops
;
87 kmutex_t ud_sync_busy
;
89 * udf_vfs list manipulation routines
91 kmutex_t udf_vfs_mutex
;
92 struct udf_vfs
*udf_vfs_instances
;
93 _NOTE(MUTEX_PROTECTS_DATA(udf_vfs_mutex
, udf_vfs_instances
))
95 union ihead ud_ihead
[UD_HASH_SZ
];
96 kmutex_t ud_icache_lock
;
101 struct ud_inode
*udf_ifreeh
, *udf_ifreet
;
102 kmutex_t udf_ifree_lock
;
103 _NOTE(MUTEX_PROTECTS_DATA(udf_ifree_lock
, udf_ifreeh
))
104 _NOTE(MUTEX_PROTECTS_DATA(udf_ifree_lock
, udf_ifreet
))
106 kmutex_t ud_nino_lock
;
107 int32_t ud_max_inodes
= 512;
108 int32_t ud_cur_inodes
= 0;
109 _NOTE(MUTEX_PROTECTS_DATA(ud_nino_lock
, ud_cur_inodes
))
111 uid_t ud_default_uid
= 0;
112 gid_t ud_default_gid
= 3;
114 int32_t ud_updat_ext4(struct ud_inode
*, struct file_entry
*);
115 int32_t ud_updat_ext4096(struct ud_inode
*, struct file_entry
*);
116 void ud_make_sad(struct icb_ext
*, struct short_ad
*, int32_t);
117 void ud_make_lad(struct icb_ext
*, struct long_ad
*, int32_t);
118 void ud_trunc_ext4(struct ud_inode
*, uoff_t
);
119 void ud_trunc_ext4096(struct ud_inode
*, uoff_t
);
120 void ud_add_to_free_list(struct ud_inode
*, uint32_t);
121 void ud_remove_from_free_list(struct ud_inode
*, uint32_t);
126 ud_search_icache(struct vfs
*vfsp
, uint16_t prn
, uint32_t ploc
)
131 struct udf_vfs
*udf_vfsp
;
134 udf_vfsp
= (struct udf_vfs
*)vfsp
->vfs_data
;
135 loc
= ud_xlate_to_daddr(udf_vfsp
, prn
, ploc
, 1, &dummy
);
137 mutex_enter(&ud_icache_lock
);
138 hno
= UD_INOHASH(vfsp
->vfs_dev
, loc
);
140 for (ip
= ih
->ih_chain
[0];
141 ip
!= (struct ud_inode
*)ih
;
143 if ((prn
== ip
->i_icb_prn
) && (ploc
== ip
->i_icb_block
) &&
144 (vfsp
->vfs_dev
== ip
->i_dev
)) {
145 mutex_exit(&ud_icache_lock
);
149 mutex_exit(&ud_icache_lock
);
156 ud_iget(struct vfs
*vfsp
, uint16_t prn
, uint32_t ploc
, struct ud_inode
**ipp
,
157 struct buf
*pbp
, struct cred
*cred
)
159 int32_t hno
, nomem
= 0, icb_tag_flags
;
163 struct buf
*bp
= NULL
;
164 struct file_entry
*fe
;
165 struct udf_vfs
*udf_vfsp
;
166 struct ext_attr_hdr
*eah
;
168 int32_t ea_len
, ea_off
;
171 struct icb_ext
*iext
, *con
;
172 uint32_t length
, dummy
;
173 int32_t ndesc
, ftype
;
175 uint32_t old_block
, old_lbano
;
177 ud_printf("ud_iget\n");
178 udf_vfsp
= (struct udf_vfs
*)vfsp
->vfs_data
;
180 old_block
= old_lbano
= 0;
182 loc
= ud_xlate_to_daddr(udf_vfsp
, prn
, ploc
, 1, &dummy
);
184 mutex_enter(&ud_icache_lock
);
185 hno
= UD_INOHASH(vfsp
->vfs_dev
, loc
);
188 for (ip
= ih
->ih_chain
[0];
189 ip
!= (struct ud_inode
*)ih
;
192 if ((prn
== ip
->i_icb_prn
) &&
193 (ploc
== ip
->i_icb_block
) &&
194 (vfsp
->vfs_dev
== ip
->i_dev
)) {
198 mutex_exit(&ud_icache_lock
);
200 rw_enter(&ip
->i_contents
, RW_READER
);
201 mutex_enter(&ip
->i_tlock
);
202 if ((ip
->i_flag
& IREF
) == 0) {
203 mutex_enter(&udf_ifree_lock
);
204 ud_remove_from_free_list(ip
, UD_UNKN
);
205 mutex_exit(&udf_ifree_lock
);
208 mutex_exit(&ip
->i_tlock
);
209 rw_exit(&ip
->i_contents
);
222 * We don't have it in the cache
223 * Allocate a new entry
226 mutex_enter(&udf_ifree_lock
);
227 mutex_enter(&ud_nino_lock
);
228 if (ud_cur_inodes
> ud_max_inodes
) {
231 mutex_exit(&ud_nino_lock
);
232 while (udf_ifreeh
== NULL
||
233 vn_has_cached_data(ITOV(udf_ifreeh
))) {
235 * Try to put an inode on the freelist that's
236 * sitting in the dnlc.
238 mutex_exit(&udf_ifree_lock
);
239 purged
= dnlc_fs_purge1(&udf_vnodeops
);
240 mutex_enter(&udf_ifree_lock
);
245 mutex_enter(&ud_nino_lock
);
249 * If there's a free one available and it has no pages attached
250 * take it. If we're over the high water mark, take it even if
251 * it has attached pages. Otherwise, make a new one.
254 (nomem
|| !vn_has_cached_data(ITOV(udf_ifreeh
)) ||
255 ud_cur_inodes
>= ud_max_inodes
)) {
257 mutex_exit(&ud_nino_lock
);
261 ud_remove_from_free_list(ip
, UD_BEGIN
);
263 mutex_exit(&udf_ifree_lock
);
264 if (ip
->i_flag
& IREF
) {
265 cmn_err(CE_WARN
, "ud_iget: bad i_flag\n");
266 mutex_exit(&ud_icache_lock
);
272 rw_enter(&ip
->i_contents
, RW_WRITER
);
275 * We call udf_syncip() to synchronously destroy all pages
276 * associated with the vnode before re-using it. The pageout
277 * thread may have beat us to this page so our v_count can
278 * be > 0 at this point even though we are on the freelist.
280 mutex_enter(&ip
->i_tlock
);
281 ip
->i_flag
= (ip
->i_flag
& IMODTIME
) | IREF
;
282 mutex_exit(&ip
->i_tlock
);
285 if (ud_syncip(ip
, B_INVAL
, I_SYNC
) != 0) {
287 rw_exit(&ip
->i_contents
);
288 mutex_exit(&ud_icache_lock
);
292 mutex_enter(&ip
->i_tlock
);
293 ip
->i_flag
&= ~IMODTIME
;
294 mutex_exit(&ip
->i_tlock
);
298 sizeof (struct icb_ext
) * ip
->i_ext_count
);
300 ip
->i_ext_count
= ip
->i_ext_used
= 0;
305 sizeof (struct icb_ext
) * ip
->i_con_count
);
307 ip
->i_con_count
= ip
->i_con_used
= ip
->i_con_read
= 0;
311 * The pageout thread may not have had a chance to release
312 * its hold on the vnode (if it was active with this vp),
313 * but the pages should all be invalidated.
316 mutex_exit(&ud_nino_lock
);
317 mutex_exit(&udf_ifree_lock
);
319 * Try to get memory for this inode without blocking.
320 * If we can't and there is something on the freelist,
321 * go ahead and use it, otherwise block waiting for
322 * memory holding the hash_lock. We expose a potential
323 * deadlock if all users of memory have to do a ud_iget()
324 * before releasing memory.
326 ip
= kmem_zalloc(sizeof (struct ud_inode
),
328 vp
= vn_alloc(KM_NOSLEEP
);
329 if ((ip
== NULL
) || (vp
== NULL
)) {
330 mutex_enter(&udf_ifree_lock
);
332 mutex_exit(&udf_ifree_lock
);
334 kmem_free(ip
, sizeof (struct ud_inode
));
340 mutex_exit(&udf_ifree_lock
);
342 ip
= (struct ud_inode
*)
344 sizeof (struct ud_inode
),
347 vp
= vn_alloc(KM_SLEEP
);
352 ip
->i_marker1
= (uint32_t)0xAAAAAAAA;
353 ip
->i_marker2
= (uint32_t)0xBBBBBBBB;
354 ip
->i_marker3
= (uint32_t)0xCCCCCCCC;
356 rw_init(&ip
->i_rwlock
, NULL
, RW_DEFAULT
, NULL
);
357 rw_init(&ip
->i_contents
, NULL
, RW_DEFAULT
, NULL
);
358 mutex_init(&ip
->i_tlock
, NULL
, MUTEX_DEFAULT
, NULL
);
362 vp
->v_data
= (caddr_t
)ip
;
363 vn_setops(vp
, &udf_vnodeops
);
365 cv_init(&ip
->i_wrcv
, NULL
, CV_DRIVER
, NULL
);
366 mutex_enter(&ud_nino_lock
);
368 mutex_exit(&ud_nino_lock
);
370 rw_enter(&ip
->i_contents
, RW_WRITER
);
373 if (vp
->v_count
< 1) {
374 cmn_err(CE_WARN
, "ud_iget: v_count < 1\n");
375 mutex_exit(&ud_icache_lock
);
376 rw_exit(&ip
->i_contents
);
382 if (vn_has_cached_data(vp
)) {
383 cmn_err(CE_WARN
, "ud_iget: v_object list is not NULL\n");
384 mutex_exit(&ud_icache_lock
);
385 rw_exit(&ip
->i_contents
);
393 * Move the inode on the chain for its new (ino, dev) pair
400 ip
->i_dev
= vfsp
->vfs_dev
;
401 ip
->i_udf
= udf_vfsp
;
403 ip
->i_devvp
= ip
->i_udf
->udf_devvp
;
405 ip
->i_icb_block
= ploc
;
406 ip
->i_icb_lbano
= loc
;
409 mutex_exit(&ud_icache_lock
);
414 * assumption is that we will not
419 bp
= ud_bread(ip
->i_dev
,
420 ip
->i_icb_lbano
<< udf_vfsp
->udf_l2d_shift
,
421 udf_vfsp
->udf_lbsize
);
427 fe
= (struct file_entry
*)bp
->b_un
.b_addr
;
428 if ((bp
->b_flags
& B_ERROR
) ||
429 (ud_verify_tag_and_desc(&fe
->fe_tag
, UD_FILE_ENTRY
,
430 ip
->i_icb_block
, 1, udf_vfsp
->udf_lbsize
) != 0)) {
432 if (((bp
->b_flags
& B_ERROR
) == 0) &&
433 (ftype
== STRAT_TYPE4096
)) {
434 if (ud_check_te_unrec(udf_vfsp
,
435 bp
->b_un
.b_addr
, ip
->i_icb_block
) == 0) {
440 * restore old file entry location
442 ip
->i_icb_prn
= old_prn
;
443 ip
->i_icb_block
= old_block
;
444 ip
->i_icb_lbano
= old_lbano
;
447 * reread old file entry
449 bp
= ud_bread(ip
->i_dev
,
450 old_lbano
<< udf_vfsp
->udf_l2d_shift
,
451 udf_vfsp
->udf_lbsize
);
452 if ((bp
->b_flags
& B_ERROR
) == 0) {
453 fe
= (struct file_entry
*)
455 if (ud_verify_tag_and_desc(&fe
->fe_tag
,
456 UD_FILE_ENTRY
, ip
->i_icb_block
, 1,
457 udf_vfsp
->udf_lbsize
) == 0) {
466 * The inode may not contain anything useful. Mark it as
467 * having an error and let anyone else who was waiting for
468 * this know there was an error. Callers waiting for
469 * access to this inode in ud_iget will find
470 * the i_icb_lbano == 0, so there won't be a match.
471 * It remains in the cache. Put it back on the freelist.
473 mutex_enter(&vp
->v_lock
);
475 mutex_exit(&vp
->v_lock
);
479 * The folowing two lines make
480 * it impossible for any one do
481 * a VN_HOLD and then a VN_RELE
482 * so avoiding a ud_iinactive
484 ip
->i_icb_prn
= 0xffff;
488 * remove the bad inode from hash chains
489 * so that during unmount we will not
490 * go through this inode
492 mutex_enter(&ud_icache_lock
);
496 mutex_exit(&ud_icache_lock
);
498 /* Put the inode at the front of the freelist */
499 mutex_enter(&ip
->i_tlock
);
500 mutex_enter(&udf_ifree_lock
);
501 ud_add_to_free_list(ip
, UD_BEGIN
);
502 mutex_exit(&udf_ifree_lock
);
504 mutex_exit(&ip
->i_tlock
);
505 rw_exit(&ip
->i_contents
);
509 if (fe
->fe_icb_tag
.itag_strategy
== SWAP_16(STRAT_TYPE4096
)) {
510 struct buf
*ibp
= NULL
;
511 struct indirect_entry
*ie
;
514 * save old file_entry location
516 old_prn
= ip
->i_icb_prn
;
517 old_block
= ip
->i_icb_block
;
518 old_lbano
= ip
->i_icb_lbano
;
520 ftype
= STRAT_TYPE4096
;
523 * If astrat is 4096 different versions
524 * of the file exist on the media.
525 * we are supposed to get to the latest
526 * version of the file
530 * IE is supposed to be in the next block
533 ibp
= ud_bread(ip
->i_dev
,
534 (ip
->i_icb_lbano
+ 1) << udf_vfsp
->udf_l2d_shift
,
535 udf_vfsp
->udf_lbsize
);
536 if (ibp
->b_flags
& B_ERROR
) {
538 * Get rid of current ibp and
539 * then goto error on DE's bp
546 ie
= (struct indirect_entry
*)ibp
->b_un
.b_addr
;
547 if (ud_verify_tag_and_desc(&ie
->ie_tag
,
548 UD_INDIRECT_ENT
, ip
->i_icb_block
+ 1,
549 1, udf_vfsp
->udf_lbsize
) == 0) {
552 lad
= &ie
->ie_indirecticb
;
553 ip
->i_icb_prn
= SWAP_16(lad
->lad_ext_prn
);
554 ip
->i_icb_block
= SWAP_32(lad
->lad_ext_loc
);
555 ip
->i_icb_lbano
= ud_xlate_to_daddr(udf_vfsp
,
556 ip
->i_icb_prn
, ip
->i_icb_block
,
564 * If this block is TE or unrecorded we
565 * are at the last entry
567 if (ud_check_te_unrec(udf_vfsp
, ibp
->b_un
.b_addr
,
568 ip
->i_icb_block
+ 1) != 0) {
570 * This is not an unrecorded block
571 * Check if it a valid IE and
572 * get the address of DE that
578 * If ud_check_unrec returns "0"
579 * this is the last in the chain
587 ip
->i_uid
= SWAP_32(fe
->fe_uid
);
588 if (ip
->i_uid
== -1) {
589 ip
->i_uid
= ud_default_uid
;
591 ip
->i_gid
= SWAP_32(fe
->fe_gid
);
592 if (ip
->i_gid
== -1) {
593 ip
->i_gid
= ud_default_gid
;
595 ip
->i_perm
= SWAP_32(fe
->fe_perms
) & 0xFFFF;
596 if (fe
->fe_icb_tag
.itag_strategy
== SWAP_16(STRAT_TYPE4096
)) {
597 ip
->i_perm
&= ~(IWRITE
| (IWRITE
>> 5) | (IWRITE
>> 10));
600 ip
->i_nlink
= SWAP_16(fe
->fe_lcount
);
601 ip
->i_size
= SWAP_64(fe
->fe_info_len
);
602 ip
->i_lbr
= SWAP_64(fe
->fe_lbr
);
604 ud_dtime2utime(&ip
->i_atime
, &fe
->fe_acc_time
);
605 ud_dtime2utime(&ip
->i_mtime
, &fe
->fe_mod_time
);
606 ud_dtime2utime(&ip
->i_ctime
, &fe
->fe_attr_time
);
609 ip
->i_uniqid
= SWAP_64(fe
->fe_uniq_id
);
610 icb_tag_flags
= SWAP_16(fe
->fe_icb_tag
.itag_flags
);
612 if ((fe
->fe_icb_tag
.itag_ftype
== FTYPE_CHAR_DEV
) ||
613 (fe
->fe_icb_tag
.itag_ftype
== FTYPE_BLOCK_DEV
)) {
615 eah
= (struct ext_attr_hdr
*)fe
->fe_spec
;
616 ea_off
= GET_32(&eah
->eah_ial
);
617 ea_len
= GET_32(&fe
->fe_len_ear
);
618 if (ea_len
&& (ud_verify_tag_and_desc(&eah
->eah_tag
,
619 UD_EXT_ATTR_HDR
, ip
->i_icb_block
, 1,
620 sizeof (struct file_entry
) -
621 offsetof(struct file_entry
, fe_spec
)) == 0)) {
623 while (ea_off
< ea_len
) {
625 * We now check the validity of ea_off.
626 * (ea_len - ea_off) should be large enough to
627 * hold the attribute header atleast.
629 if ((ea_len
- ea_off
) <
630 sizeof (struct attr_hdr
)) {
632 "ea_len(0x%x) - ea_off(0x%x) is "
633 "too small to hold attr. info. "
635 ea_len
, ea_off
, ip
->i_icb_block
);
638 ah
= (struct attr_hdr
*)&fe
->fe_spec
[ea_off
];
641 * Device Specification EA
643 if ((GET_32(&ah
->ahdr_atype
) == 12) &&
644 (ah
->ahdr_astype
== 1)) {
645 struct dev_spec_ear
*ds
;
647 if ((ea_len
- ea_off
) <
648 sizeof (struct dev_spec_ear
)) {
651 "ea_off(0x%x) is too small "
652 "to hold dev_spec_ear."
658 ds
= (struct dev_spec_ear
*)ah
;
659 ip
->i_major
= GET_32(&ds
->ds_major_id
);
660 ip
->i_minor
= GET_32(&ds
->ds_minor_id
);
666 if ((GET_32(&ah
->ahdr_atype
) == 2048) &&
667 (ah
->ahdr_astype
== 1)) {
669 struct copy_mgt_info
*cmi
;
671 if ((ea_len
- ea_off
) <
672 sizeof (struct iu_ea
)) {
674 "ea_len(0x%x) - ea_off(0x%x) is too small to hold iu_ea. blockno 0x%x\n",
679 iuea
= (struct iu_ea
*)ah
;
680 if (strncmp(iuea
->iuea_ii
.reg_id
,
682 sizeof (iuea
->iuea_ii
.reg_id
))
686 } else if (strncmp(iuea
->iuea_ii
.reg_id
,
688 sizeof (iuea
->iuea_ii
.reg_id
))
690 cmi
= (struct copy_mgt_info
*)
696 if (GET_32(&ah
->ahdr_length
) == 0) {
699 ea_off
+= GET_32(&ah
->ahdr_length
);
706 ip
->i_maxent
= SWAP_16(fe
->fe_icb_tag
.itag_max_ent
);
707 ip
->i_astrat
= SWAP_16(fe
->fe_icb_tag
.itag_strategy
);
709 ip
->i_desc_type
= icb_tag_flags
& 0x7;
711 /* Strictly Paranoia */
713 ip
->i_ext_count
= ip
->i_ext_used
= 0;
715 ip
->i_con_count
= ip
->i_con_used
= ip
->i_con_read
= 0;
717 ip
->i_data_off
= 0xB0 + SWAP_32(fe
->fe_len_ear
);
718 ip
->i_max_emb
= udf_vfsp
->udf_lbsize
- ip
->i_data_off
;
719 if (ip
->i_desc_type
== ICB_FLAG_SHORT_AD
) {
720 /* Short allocation desc */
721 struct short_ad
*sad
;
724 ip
->i_ext_count
= ndesc
=
725 SWAP_32(fe
->fe_len_adesc
) / sizeof (struct short_ad
);
727 ((ip
->i_ext_count
/ EXT_PER_MALLOC
) + 1) * EXT_PER_MALLOC
;
728 ip
->i_ext
= kmem_zalloc(ip
->i_ext_count
*
729 sizeof (struct icb_ext
), KM_SLEEP
);
730 ip
->i_cur_max_ext
= ip
->i_max_emb
/ sizeof (struct short_ad
);
731 ip
->i_cur_max_ext
--;
733 if ((ip
->i_astrat
!= STRAT_TYPE4
) &&
734 (ip
->i_astrat
!= STRAT_TYPE4096
)) {
738 sad
= (struct short_ad
*)
739 (fe
->fe_spec
+ SWAP_32(fe
->fe_len_ear
));
742 length
= SWAP_32(sad
->sad_ext_len
);
743 if ((length
& 0x3FFFFFFF) == 0) {
746 if (((length
>> 30) & IB_MASK
) == IB_CON
) {
747 if (ip
->i_con
== NULL
) {
748 ip
->i_con_count
= EXT_PER_MALLOC
;
751 ip
->i_con
= kmem_zalloc(
753 sizeof (struct icb_ext
),
756 con
= &ip
->i_con
[ip
->i_con_used
];
758 con
->ib_block
= SWAP_32(sad
->sad_ext_loc
);
759 con
->ib_count
= length
& 0x3FFFFFFF;
760 con
->ib_flags
= (length
>> 30) & IB_MASK
;
766 iext
->ib_block
= SWAP_32(sad
->sad_ext_loc
);
767 length
= SWAP_32(sad
->sad_ext_len
);
768 iext
->ib_count
= length
& 0x3FFFFFFF;
769 iext
->ib_offset
= offset
;
770 iext
->ib_marker1
= (uint32_t)0xAAAAAAAA;
771 iext
->ib_marker2
= (uint32_t)0xBBBBBBBB;
772 offset
+= (iext
->ib_count
+ udf_vfsp
->udf_lbmask
) &
773 (~udf_vfsp
->udf_lbmask
);
775 iext
->ib_flags
= (length
>> 30) & IB_MASK
;
781 } else if (ip
->i_desc_type
== ICB_FLAG_LONG_AD
) {
782 /* Long allocation desc */
786 ip
->i_ext_count
= ndesc
=
787 SWAP_32(fe
->fe_len_adesc
) / sizeof (struct long_ad
);
789 ((ip
->i_ext_count
/ EXT_PER_MALLOC
) + 1) * EXT_PER_MALLOC
;
790 ip
->i_ext
= kmem_zalloc(ip
->i_ext_count
*
791 sizeof (struct icb_ext
), KM_SLEEP
);
793 ip
->i_cur_max_ext
= ip
->i_max_emb
/ sizeof (struct long_ad
);
794 ip
->i_cur_max_ext
--;
796 if ((ip
->i_astrat
!= STRAT_TYPE4
) &&
797 (ip
->i_astrat
!= STRAT_TYPE4096
)) {
801 lad
= (struct long_ad
*)
802 (fe
->fe_spec
+ SWAP_32(fe
->fe_len_ear
));
805 length
= SWAP_32(lad
->lad_ext_len
);
806 if ((length
& 0x3FFFFFFF) == 0) {
809 if (((length
>> 30) & IB_MASK
) == IB_CON
) {
810 if (ip
->i_con
== NULL
) {
811 ip
->i_con_count
= EXT_PER_MALLOC
;
814 ip
->i_con
= kmem_zalloc(
816 sizeof (struct icb_ext
),
819 con
= &ip
->i_con
[ip
->i_con_used
];
820 con
->ib_prn
= SWAP_16(lad
->lad_ext_prn
);
821 con
->ib_block
= SWAP_32(lad
->lad_ext_loc
);
822 con
->ib_count
= length
& 0x3FFFFFFF;
823 con
->ib_flags
= (length
>> 30) & IB_MASK
;
828 iext
->ib_prn
= SWAP_16(lad
->lad_ext_prn
);
829 iext
->ib_block
= SWAP_32(lad
->lad_ext_loc
);
830 iext
->ib_count
= length
& 0x3FFFFFFF;
831 iext
->ib_offset
= offset
;
832 iext
->ib_marker1
= (uint32_t)0xAAAAAAAA;
833 iext
->ib_marker2
= (uint32_t)0xBBBBBBBB;
834 offset
+= (iext
->ib_count
+ udf_vfsp
->udf_lbmask
) &
835 (~udf_vfsp
->udf_lbmask
);
837 iext
->ib_flags
= (length
>> 30) & IB_MASK
;
843 } else if (ip
->i_desc_type
== ICB_FLAG_ONE_AD
) {
844 ASSERT(SWAP_32(fe
->fe_len_ear
) < udf_vfsp
->udf_lbsize
);
846 if (SWAP_32(fe
->fe_len_ear
) > udf_vfsp
->udf_lbsize
) {
850 /* Not to be used in UDF 1.50 */
851 cmn_err(CE_NOTE
, "Invalid Allocation Descriptor type %x\n",
857 if (icb_tag_flags
& ICB_FLAG_SETUID
) {
862 if (icb_tag_flags
& ICB_FLAG_SETGID
) {
865 if (icb_tag_flags
& ICB_FLAG_STICKY
) {
868 switch (fe
->fe_icb_tag
.itag_ftype
) {
869 case FTYPE_DIRECTORY
:
875 case FTYPE_BLOCK_DEV
:
878 case FTYPE_CHAR_DEV
:
884 case FTYPE_C_ISSOCK
:
895 if (ip
->i_type
== VBLK
|| ip
->i_type
== VCHR
) {
896 ip
->i_rdev
= makedevice(ip
->i_major
, ip
->i_minor
);
900 * Fill in the rest. Don't bother with the vnode lock because nobody
901 * should be looking at this vnode. We have already invalidated the
902 * pages if it had any so pageout shouldn't be referencing this vnode
903 * and we are holding the write contents lock so a look up can't use
907 vp
->v_type
= ip
->i_type
;
908 vp
->v_rdev
= ip
->i_rdev
;
909 if (ip
->i_udf
->udf_root_blkno
== loc
) {
917 rw_exit(&ip
->i_contents
);
923 ud_iinactive(struct ud_inode
*ip
, struct cred
*cr
)
932 ud_printf("ud_iinactive\n");
935 * Get exclusive access to inode data.
937 rw_enter(&ip
->i_contents
, RW_WRITER
);
940 * Make sure no one reclaimed the inode before we put
941 * it on the freelist or destroy it. We keep our 'hold'
942 * on the vnode from vn_rele until we are ready to
943 * do something with the inode (freelist/destroy).
945 * Pageout may put a VN_HOLD/VN_RELE at anytime during this
946 * operation via an async putpage, so we must make sure
947 * we don't free/destroy the inode more than once. ud_iget
948 * may also put a VN_HOLD on the inode before it grabs
949 * the i_contents lock. This is done so we don't kmem_free
950 * an inode that a thread is waiting on.
954 mutex_enter(&vp
->v_lock
);
955 if (vp
->v_count
< 1) {
956 cmn_err(CE_WARN
, "ud_iinactive: v_count < 1\n");
959 if ((vp
->v_count
> 1) || ((ip
->i_flag
& IREF
) == 0)) {
961 mutex_exit(&vp
->v_lock
);
962 rw_exit(&ip
->i_contents
);
965 mutex_exit(&vp
->v_lock
);
968 * For forced umount case: if i_udf is NULL, the contents of
969 * the inode and all the pages have already been pushed back
970 * to disk. It can be safely destroyed.
972 if (ip
->i_udf
== NULL
) {
973 addr
= (caddr_t
)ip
->i_ext
;
974 size
= sizeof (struct icb_ext
) * ip
->i_ext_count
;
976 ip
->i_ext_count
= ip
->i_ext_used
= 0;
977 addr1
= (caddr_t
)ip
->i_con
;
978 size1
= sizeof (struct icb_ext
) * ip
->i_con_count
;
980 ip
->i_con_count
= ip
->i_con_used
= ip
->i_con_read
= 0;
981 rw_exit(&ip
->i_contents
);
984 mutex_enter(&ud_nino_lock
);
986 mutex_exit(&ud_nino_lock
);
988 cv_destroy(&ip
->i_wrcv
); /* throttling */
989 rw_destroy(&ip
->i_rwlock
);
990 rw_exit(&ip
->i_contents
);
991 rw_destroy(&ip
->i_contents
);
992 kmem_free(addr
, size
);
993 kmem_free(addr1
, size1
);
995 kmem_free(ip
, sizeof (struct ud_inode
));
999 if ((ip
->i_udf
->udf_flags
& UDF_FL_RDONLY
) == 0) {
1000 if (ip
->i_nlink
<= 0) {
1001 ip
->i_marker3
= (uint32_t)0xDDDD0000;
1002 ip
->i_nlink
= 1; /* prevent free-ing twice */
1003 (void) ud_itrunc(ip
, 0, 0, cr
);
1008 ip
->i_rdev
= 0; /* Zero in core version of rdev */
1009 mutex_enter(&ip
->i_tlock
);
1010 ip
->i_flag
|= IUPD
|ICHG
;
1011 mutex_exit(&ip
->i_tlock
);
1013 ip
->i_icb_prn
= 0xFFFF;
1014 } else if (!IS_SWAPVP(vp
)) {
1016 * Write the inode out if dirty. Pages are
1017 * written back and put on the freelist.
1019 (void) ud_syncip(ip
, B_FREE
| B_ASYNC
, 0);
1021 * Do nothing if inode is now busy -- inode may
1022 * have gone busy because ud_syncip
1023 * releases/reacquires the i_contents lock
1025 mutex_enter(&vp
->v_lock
);
1026 if (vp
->v_count
> 1) {
1028 mutex_exit(&vp
->v_lock
);
1029 rw_exit(&ip
->i_contents
);
1032 mutex_exit(&vp
->v_lock
);
1040 * Put the inode on the end of the free list.
1041 * Possibly in some cases it would be better to
1042 * put the inode at the head of the free list,
1043 * (e.g.: where i_perm == 0 || i_number == 0)
1044 * but I will think about that later.
1045 * (i_number is rarely 0 - only after an i/o error in ud_iget,
1046 * where i_perm == 0, the inode will probably be wanted
1047 * again soon for an ialloc, so possibly we should keep it)
1050 * If inode is invalid or there is no page associated with
1051 * this inode, put the inode in the front of the free list.
1052 * Since we have a VN_HOLD on the vnode, and checked that it
1053 * wasn't already on the freelist when we entered, we can safely
1054 * put it on the freelist even if another thread puts a VN_HOLD
1055 * on it (pageout/ud_iget).
1058 mutex_enter(&ud_nino_lock
);
1059 if (vn_has_cached_data(vp
)) {
1060 mutex_exit(&ud_nino_lock
);
1061 mutex_enter(&vp
->v_lock
);
1063 mutex_exit(&vp
->v_lock
);
1064 mutex_enter(&ip
->i_tlock
);
1065 mutex_enter(&udf_ifree_lock
);
1066 ud_add_to_free_list(ip
, UD_END
);
1067 mutex_exit(&udf_ifree_lock
);
1068 ip
->i_flag
&= IMODTIME
;
1069 mutex_exit(&ip
->i_tlock
);
1070 rw_exit(&ip
->i_contents
);
1071 } else if (busy
|| ud_cur_inodes
< ud_max_inodes
) {
1072 mutex_exit(&ud_nino_lock
);
1074 * We're not over our high water mark, or it's
1075 * not safe to kmem_free the inode, so put it
1078 mutex_enter(&vp
->v_lock
);
1079 if (vn_has_cached_data(vp
)) {
1080 cmn_err(CE_WARN
, "ud_iinactive: v_object list is "
1084 mutex_exit(&vp
->v_lock
);
1086 mutex_enter(&ip
->i_tlock
);
1087 mutex_enter(&udf_ifree_lock
);
1088 ud_add_to_free_list(ip
, UD_BEGIN
);
1089 mutex_exit(&udf_ifree_lock
);
1090 ip
->i_flag
&= IMODTIME
;
1091 mutex_exit(&ip
->i_tlock
);
1092 rw_exit(&ip
->i_contents
);
1094 mutex_exit(&ud_nino_lock
);
1095 if (vn_has_cached_data(vp
)) {
1096 cmn_err(CE_WARN
, "ud_iinactive: v_object list is "
1100 * Try to free the inode. We must make sure
1101 * it's o.k. to destroy this inode. We can't destroy
1102 * if a thread is waiting for this inode. If we can't get the
1103 * cache now, put it back on the freelist.
1105 if (!mutex_tryenter(&ud_icache_lock
)) {
1109 mutex_enter(&vp
->v_lock
);
1110 if (vp
->v_count
> 1) {
1111 /* inode is wanted in ud_iget */
1113 mutex_exit(&vp
->v_lock
);
1114 mutex_exit(&ud_icache_lock
);
1117 mutex_exit(&vp
->v_lock
);
1121 mutex_enter(&ud_nino_lock
);
1123 mutex_exit(&ud_nino_lock
);
1124 mutex_exit(&ud_icache_lock
);
1125 if (ip
->i_icb_prn
!= 0xFFFF) {
1128 addr
= (caddr_t
)ip
->i_ext
;
1129 size
= sizeof (struct icb_ext
) * ip
->i_ext_count
;
1131 ip
->i_ext_count
= ip
->i_ext_used
= 0;
1132 addr1
= (caddr_t
)ip
->i_con
;
1133 size1
= sizeof (struct icb_ext
) * ip
->i_con_count
;
1135 ip
->i_con_count
= ip
->i_con_used
= ip
->i_con_read
= 0;
1136 cv_destroy(&ip
->i_wrcv
); /* throttling */
1137 rw_destroy(&ip
->i_rwlock
);
1138 rw_exit(&ip
->i_contents
);
1139 rw_destroy(&ip
->i_contents
);
1140 kmem_free(addr
, size
);
1141 kmem_free(addr1
, size1
);
1142 ip
->i_marker3
= (uint32_t)0xDDDDDDDD;
1144 kmem_free(ip
, sizeof (struct ud_inode
));
1150 ud_iupdat(struct ud_inode
*ip
, int32_t waitfor
)
1152 uint16_t flag
, tag_flags
;
1155 struct udf_vfs
*udf_vfsp
;
1156 struct file_entry
*fe
;
1157 uint16_t crc_len
= 0;
1159 ASSERT(RW_WRITE_HELD(&ip
->i_contents
));
1161 ud_printf("ud_iupdat\n");
1163 * Return if file system has been forcibly umounted.
1165 if (ip
->i_udf
== NULL
) {
1169 udf_vfsp
= ip
->i_udf
;
1170 flag
= ip
->i_flag
; /* Atomic read */
1171 if ((flag
& (IUPD
|IACC
|ICHG
|IMOD
|IMODACC
)) != 0) {
1172 if (udf_vfsp
->udf_flags
& UDF_FL_RDONLY
) {
1173 ip
->i_flag
&= ~(IUPD
|IACC
|ICHG
|IMOD
|IMODACC
|IATTCHG
);
1177 bp
= ud_bread(ip
->i_dev
,
1178 ip
->i_icb_lbano
<< udf_vfsp
->udf_l2d_shift
,
1179 ip
->i_udf
->udf_lbsize
);
1180 if (bp
->b_flags
& B_ERROR
) {
1184 fe
= (struct file_entry
*)bp
->b_un
.b_addr
;
1185 if (ud_verify_tag_and_desc(&fe
->fe_tag
, UD_FILE_ENTRY
,
1187 1, ip
->i_udf
->udf_lbsize
) != 0) {
1192 mutex_enter(&ip
->i_tlock
);
1193 if (ip
->i_flag
& (IUPD
|IACC
|ICHG
)) {
1196 ip
->i_flag
&= ~(IUPD
|IACC
|ICHG
|IMOD
|IMODACC
);
1197 mutex_exit(&ip
->i_tlock
);
1199 fe
->fe_uid
= SWAP_32(ip
->i_uid
);
1200 fe
->fe_gid
= SWAP_32(ip
->i_gid
);
1202 fe
->fe_perms
= SWAP_32(ip
->i_perm
);
1204 fe
->fe_lcount
= SWAP_16(ip
->i_nlink
);
1205 fe
->fe_info_len
= SWAP_64(ip
->i_size
);
1206 fe
->fe_lbr
= SWAP_64(ip
->i_lbr
);
1208 ud_utime2dtime(&ip
->i_atime
, &fe
->fe_acc_time
);
1209 ud_utime2dtime(&ip
->i_mtime
, &fe
->fe_mod_time
);
1210 ud_utime2dtime(&ip
->i_ctime
, &fe
->fe_attr_time
);
1212 if (ip
->i_char
& ISUID
) {
1213 tag_flags
= ICB_FLAG_SETUID
;
1217 if (ip
->i_char
& ISGID
) {
1218 tag_flags
|= ICB_FLAG_SETGID
;
1220 if (ip
->i_char
& ISVTX
) {
1221 tag_flags
|= ICB_FLAG_STICKY
;
1223 tag_flags
|= ip
->i_desc_type
;
1226 * Remove the following it is no longer contig
1227 * if (ip->i_astrat == STRAT_TYPE4) {
1228 * tag_flags |= ICB_FLAG_CONTIG;
1232 fe
->fe_icb_tag
.itag_flags
&= ~SWAP_16((uint16_t)0x3C3);
1233 fe
->fe_icb_tag
.itag_strategy
= SWAP_16(ip
->i_astrat
);
1234 fe
->fe_icb_tag
.itag_flags
|= SWAP_16(tag_flags
);
1236 ud_update_regid(&fe
->fe_impl_id
);
1238 crc_len
= offsetof(struct file_entry
, fe_spec
) +
1239 SWAP_32(fe
->fe_len_ear
);
1240 if (ip
->i_desc_type
== ICB_FLAG_ONE_AD
) {
1241 crc_len
+= ip
->i_size
;
1242 fe
->fe_len_adesc
= SWAP_32(((uint32_t)ip
->i_size
));
1243 } else if ((ip
->i_size
!= 0) && (ip
->i_ext
!= NULL
) &&
1244 (ip
->i_ext_used
!= 0)) {
1246 if ((error
= ud_read_icb_till_off(ip
,
1247 ip
->i_size
)) == 0) {
1248 if (ip
->i_astrat
== STRAT_TYPE4
) {
1249 error
= ud_updat_ext4(ip
, fe
);
1250 } else if (ip
->i_astrat
== STRAT_TYPE4096
) {
1251 error
= ud_updat_ext4096(ip
, fe
);
1254 udf_vfsp
->udf_mark_bad
= 1;
1257 crc_len
+= SWAP_32(fe
->fe_len_adesc
);
1259 fe
->fe_len_adesc
= 0;
1263 * Zero out the rest of the block
1265 bzero(bp
->b_un
.b_addr
+ crc_len
,
1266 ip
->i_udf
->udf_lbsize
- crc_len
);
1268 ud_make_tag(ip
->i_udf
, &fe
->fe_tag
,
1269 UD_FILE_ENTRY
, ip
->i_icb_block
, crc_len
);
1276 * Synchronous write has guaranteed that inode
1277 * has been written on disk so clear the flag
1279 ip
->i_flag
&= ~(IBDWRITE
);
1284 * This write hasn't guaranteed that inode has been
1285 * written on the disk.
1286 * Since, all updat flags on indoe are cleared, we must
1287 * remember the condition in case inode is to be updated
1288 * synchronously later (e.g.- fsync()/fdatasync())
1289 * and inode has not been modified yet.
1291 ip
->i_flag
|= (IBDWRITE
);
1295 * In case previous inode update was done asynchronously
1296 * (IBDWRITE) and this inode update request wants guaranteed
1297 * (synchronous) disk update, flush the inode.
1299 if (waitfor
&& (flag
& IBDWRITE
)) {
1301 (daddr_t
)fsbtodb(udf_vfsp
, ip
->i_icb_lbano
));
1302 ip
->i_flag
&= ~(IBDWRITE
);
1308 ud_updat_ext4(struct ud_inode
*ip
, struct file_entry
*fe
)
1311 int32_t elen
, ndent
, index
, count
, con_index
;
1314 struct short_ad
*sad
;
1315 struct long_ad
*lad
;
1316 struct icb_ext
*iext
, *icon
;
1321 ASSERT((ip
->i_desc_type
== ICB_FLAG_SHORT_AD
) ||
1322 (ip
->i_desc_type
== ICB_FLAG_LONG_AD
));
1324 if (ip
->i_desc_type
== ICB_FLAG_SHORT_AD
) {
1325 elen
= sizeof (struct short_ad
);
1326 sad
= (struct short_ad
*)
1327 (fe
->fe_spec
+ SWAP_32(fe
->fe_len_ear
));
1328 } else if (ip
->i_desc_type
== ICB_FLAG_LONG_AD
) {
1329 elen
= sizeof (struct long_ad
);
1330 lad
= (struct long_ad
*)
1331 (fe
->fe_spec
+ SWAP_32(fe
->fe_len_ear
));
1333 /* This cannot happen return */
1337 ndent
= ip
->i_max_emb
/ elen
;
1339 if (ip
->i_ext_used
< ndent
) {
1341 if (ip
->i_desc_type
== ICB_FLAG_SHORT_AD
) {
1342 ud_make_sad(ip
->i_ext
, sad
, ip
->i_ext_used
);
1344 ud_make_lad(ip
->i_ext
, lad
, ip
->i_ext_used
);
1346 fe
->fe_len_adesc
= SWAP_32(ip
->i_ext_used
* elen
);
1350 con_index
= index
= 0;
1352 while (index
< ip
->i_ext_used
) {
1355 * bp is already read
1356 * First few extents will go
1357 * into the file_entry
1360 fe
->fe_len_adesc
= SWAP_32(ndent
* elen
);
1364 * Last entry to be cont ext
1366 icon
= &ip
->i_con
[con_index
];
1371 icon
= &ip
->i_con
[con_index
];
1373 bno
= ud_xlate_to_daddr(ip
->i_udf
,
1374 icon
->ib_prn
, icon
->ib_block
,
1375 icon
->ib_count
>> ip
->i_udf
->udf_l2d_shift
,
1377 bp
= ud_bread(ip
->i_dev
,
1378 bno
<< ip
->i_udf
->udf_l2d_shift
,
1379 ip
->i_udf
->udf_lbsize
);
1380 if (bp
->b_flags
& B_ERROR
) {
1386 * Figure out how many extents in
1389 count
= (bp
->b_bcount
-
1390 sizeof (struct alloc_ext_desc
)) / elen
;
1391 if (count
> (ip
->i_ext_used
- index
)) {
1392 count
= ip
->i_ext_used
- index
;
1397 if (con_index
>= ip
->i_con_used
) {
1400 icon
= &ip
->i_con
[con_index
];
1407 * convert to on disk form and
1410 iext
= &ip
->i_ext
[index
];
1411 if (ip
->i_desc_type
== ICB_FLAG_SHORT_AD
) {
1413 sad
= (struct short_ad
*)
1415 sizeof (struct alloc_ext_desc
));
1417 ud_make_sad(iext
, sad
, count
);
1420 ud_make_sad(icon
, sad
, 1);
1424 lad
= (struct long_ad
*)
1426 sizeof (struct alloc_ext_desc
));
1428 ud_make_lad(iext
, lad
, count
);
1431 ud_make_lad(icon
, lad
, 1);
1435 if (con_index
!= 0) {
1436 struct alloc_ext_desc
*aed
;
1438 struct icb_ext
*oicon
;
1440 oicon
= &ip
->i_con
[con_index
- 1];
1445 aed
= (struct alloc_ext_desc
*)bp
->b_un
.b_addr
;
1446 aed
->aed_len_aed
= SWAP_32(sz
);
1447 if (con_index
== 1) {
1449 SWAP_32(ip
->i_icb_block
);
1452 SWAP_32(oicon
->ib_block
);
1454 sz
+= sizeof (struct alloc_ext_desc
);
1455 ud_make_tag(ip
->i_udf
, &aed
->aed_tag
,
1456 UD_ALLOC_EXT_DESC
, oicon
->ib_block
, sz
);
1460 * Write back to disk
1470 if (con_index
!= ip
->i_con_used
) {
1471 int32_t lbmask
, l2b
, temp
;
1474 lbmask
= ip
->i_udf
->udf_lbmask
;
1475 l2b
= ip
->i_udf
->udf_l2b_shift
;
1477 * Free unused continuation extents
1479 for (; con_index
< ip
->i_con_used
; con_index
++) {
1480 icon
= &ip
->i_con
[con_index
];
1481 count
= (icon
->ib_count
+ lbmask
) >> l2b
;
1482 ud_free_space(ip
->i_udf
->udf_vfs
, icon
->ib_prn
,
1483 icon
->ib_block
, count
);
1484 count
= (count
<< l2b
) - sizeof (struct alloc_ext_desc
);
1485 ip
->i_cur_max_ext
-= (count
/ elen
) - 1;
1487 ip
->i_con_used
= temp
;
1494 ud_updat_ext4096(struct ud_inode
*ip
, struct file_entry
*fe
)
1500 ud_make_sad(struct icb_ext
*iext
, struct short_ad
*sad
, int32_t count
)
1502 int32_t index
= 0, scount
;
1509 while (index
< count
) {
1510 scount
= (iext
->ib_count
& 0x3FFFFFFF) |
1511 (iext
->ib_flags
<< 30);
1512 sad
->sad_ext_len
= SWAP_32(scount
);
1513 sad
->sad_ext_loc
= SWAP_32(iext
->ib_block
);
1522 ud_make_lad(struct icb_ext
*iext
, struct long_ad
*lad
, int32_t count
)
1524 int32_t index
= 0, scount
;
1532 while (index
< count
) {
1533 lad
->lad_ext_prn
= SWAP_16(iext
->ib_prn
);
1534 scount
= (iext
->ib_count
& 0x3FFFFFFF) |
1535 (iext
->ib_flags
<< 30);
1536 lad
->lad_ext_len
= SWAP_32(scount
);
1537 lad
->lad_ext_loc
= SWAP_32(iext
->ib_block
);
1546 * Truncate the inode ip to at most length size.
1547 * Free affected disk blocks -- the blocks of the
1548 * file are removed in reverse order.
1552 ud_itrunc(struct ud_inode
*oip
, uoff_t length
,
1553 int32_t flags
, struct cred
*cr
)
1555 int32_t error
, boff
;
1558 struct udf_vfs
*udf_vfsp
;
1560 ud_printf("ud_itrunc\n");
1562 ASSERT(RW_WRITE_HELD(&oip
->i_contents
));
1563 udf_vfsp
= oip
->i_udf
;
1564 bsize
= udf_vfsp
->udf_lbsize
;
1567 * We only allow truncation of regular files and directories
1568 * to arbritary lengths here. In addition, we allow symbolic
1569 * links to be truncated only to zero length. Other inode
1570 * types cannot have their length set here.
1573 if (mode
== VFIFO
) {
1576 if ((mode
!= VREG
) && (mode
!= VDIR
) &&
1577 (!(mode
== VLNK
&& length
== 0))) {
1580 if (length
== oip
->i_size
) {
1581 /* update ctime and mtime to please POSIX tests */
1582 mutex_enter(&oip
->i_tlock
);
1583 oip
->i_flag
|= ICHG
|IUPD
;
1584 mutex_exit(&oip
->i_tlock
);
1588 boff
= blkoff(udf_vfsp
, length
);
1590 if (length
> oip
->i_size
) {
1592 * Trunc up case.ud_bmap_write will insure that the right blocks
1593 * are allocated. This includes doing any work needed for
1594 * allocating the last block.
1597 error
= ud_bmap_write(oip
, length
- 1,
1600 error
= ud_bmap_write(oip
, length
- 1, boff
, 0, cr
);
1603 uoff_t osize
= oip
->i_size
;
1604 oip
->i_size
= length
;
1607 * Make sure we zero out the remaining bytes of
1608 * the page in case a mmap scribbled on it. We
1609 * can't prevent a mmap from writing beyond EOF
1610 * on the last page of a file.
1612 if ((boff
= blkoff(udf_vfsp
, osize
)) != 0) {
1613 pvn_vpzero(ITOV(oip
), osize
,
1614 (uint32_t)(bsize
- boff
));
1616 mutex_enter(&oip
->i_tlock
);
1617 oip
->i_flag
|= ICHG
;
1619 mutex_exit(&oip
->i_tlock
);
1625 * Update the pages of the file. If the file is not being
1626 * truncated to a block boundary, the contents of the
1627 * pages following the end of the file must be zero'ed
1628 * in case it ever become accessable again because
1629 * of subsequent file growth.
1632 (void) pvn_vplist_dirty(ITOV(oip
), length
,
1633 ud_putapage
, B_INVAL
| B_TRUNC
, CRED());
1636 * Make sure that the last block is properly allocated.
1637 * We only really have to do this if the last block is
1638 * actually allocated. Just to be sure, we do it now
1639 * independent of current allocation.
1641 error
= ud_bmap_write(oip
, length
- 1, boff
, 0, cr
);
1646 pvn_vpzero(ITOV(oip
), length
, (uint32_t)(bsize
- boff
));
1648 (void) pvn_vplist_dirty(ITOV(oip
), length
,
1649 ud_putapage
, B_INVAL
| B_TRUNC
, CRED());
1653 /* Free the blocks */
1654 if (oip
->i_desc_type
== ICB_FLAG_ONE_AD
) {
1655 if (length
> oip
->i_max_emb
) {
1658 oip
->i_size
= length
;
1659 mutex_enter(&oip
->i_tlock
);
1660 oip
->i_flag
|= ICHG
|IUPD
;
1661 mutex_exit(&oip
->i_tlock
);
1664 if ((error
= ud_read_icb_till_off(oip
, oip
->i_size
)) != 0) {
1668 if (oip
->i_astrat
== STRAT_TYPE4
) {
1669 ud_trunc_ext4(oip
, length
);
1670 } else if (oip
->i_astrat
== STRAT_TYPE4096
) {
1671 ud_trunc_ext4096(oip
, length
);
1680 ud_trunc_ext4(struct ud_inode
*ip
, uoff_t length
)
1682 int32_t index
, l2b
, count
, ecount
;
1683 int32_t elen
, ndent
, nient
;
1684 uoff_t ext_beg
, ext_end
;
1685 struct icb_ext
*iext
, *icon
;
1686 int32_t lbmask
, ext_used
;
1688 struct icb_ext text
;
1691 ASSERT((ip
->i_desc_type
== ICB_FLAG_SHORT_AD
) ||
1692 (ip
->i_desc_type
== ICB_FLAG_LONG_AD
));
1694 if (ip
->i_ext_used
== 0) {
1698 ext_used
= ip
->i_ext_used
;
1700 lbmask
= ip
->i_udf
->udf_lbmask
;
1701 l2b
= ip
->i_udf
->udf_l2b_shift
;
1706 for (index
= 0; index
< ext_used
; index
++) {
1707 iext
= &ip
->i_ext
[index
];
1710 * Find the begining and end
1713 ext_beg
= iext
->ib_offset
;
1714 ext_end
= iext
->ib_offset
+
1715 ((iext
->ib_count
+ lbmask
) & ~lbmask
);
1718 * This is the extent that has offset "length"
1719 * make a copy of this extent and
1720 * remember the index. We can use
1723 if ((length
<= ext_end
) && (length
>= ext_beg
)) {
1726 iext
->ib_count
= length
- ext_beg
;
1727 ip
->i_ext_used
= index
+ 1;
1730 if (iext
->ib_flags
!= IB_UN_RE_AL
) {
1731 ip
->i_lbr
+= iext
->ib_count
>> l2b
;
1734 if (ip
->i_ext_used
!= index
) {
1735 if (iext
->ib_flags
!= IB_UN_RE_AL
) {
1737 ((iext
->ib_count
+ lbmask
) & ~lbmask
) >> l2b
;
1741 ip
->i_size
= length
;
1742 mutex_enter(&ip
->i_tlock
);
1743 ip
->i_flag
|= ICHG
|IUPD
;
1744 mutex_exit(&ip
->i_tlock
);
1748 * Free the unused space
1750 if (text
.ib_flags
!= IB_UN_RE_AL
) {
1751 count
= (ext_end
- length
) >> l2b
;
1753 loc
= text
.ib_block
+
1754 (((length
- text
.ib_offset
) + lbmask
) >> l2b
);
1755 ud_free_space(ip
->i_udf
->udf_vfs
, text
.ib_prn
,
1759 for (index
= ip
->i_ext_used
; index
< ext_used
; index
++) {
1760 iext
= &ip
->i_ext
[index
];
1761 if (iext
->ib_flags
!= IB_UN_RE_AL
) {
1762 count
= (iext
->ib_count
+ lbmask
) >> l2b
;
1763 ud_free_space(ip
->i_udf
->udf_vfs
, iext
->ib_prn
,
1764 iext
->ib_block
, count
);
1766 bzero(iext
, sizeof (struct icb_ext
));
1771 * release any continuation blocks
1775 ASSERT(ip
->i_con_count
>= ip
->i_con_used
);
1778 * Find out how many indirect blocks
1779 * are required and release the rest
1781 if (ip
->i_desc_type
== ICB_FLAG_SHORT_AD
) {
1782 elen
= sizeof (struct short_ad
);
1783 } else if (ip
->i_desc_type
== ICB_FLAG_LONG_AD
) {
1784 elen
= sizeof (struct long_ad
);
1786 ndent
= ip
->i_max_emb
/ elen
;
1787 if (ip
->i_ext_used
> ndent
) {
1788 ecount
= ip
->i_ext_used
- ndent
;
1793 for (index
= 0; index
< ip
->i_con_used
; index
++) {
1794 icon
= &ip
->i_con
[index
];
1795 nient
= icon
->ib_count
-
1796 (sizeof (struct alloc_ext_desc
) + elen
);
1797 /* Header + 1 indirect extent */
1800 if (ecount
> nient
) {
1806 count
= ((icon
->ib_count
+ lbmask
) &
1808 ud_free_space(ip
->i_udf
->udf_vfs
,
1809 icon
->ib_prn
, icon
->ib_block
, count
);
1811 ip
->i_cur_max_ext
-= nient
;
1815 * set the continuation extents used(i_con_used)i to correct
1816 * value. It is possible for i_con_used to be zero,
1817 * if we free up all continuation extents. This happens
1818 * when ecount is 0 before entering the for loop above.
1820 ip
->i_con_used
-= con_freed
;
1821 if (ip
->i_con_read
> ip
->i_con_used
) {
1822 ip
->i_con_read
= ip
->i_con_used
;
1828 ud_trunc_ext4096(struct ud_inode
*ip
, uoff_t length
)
1831 * Truncate code is the same for
1832 * both file of type 4 and 4096
1834 ud_trunc_ext4(ip
, length
);
1838 * Remove any inodes in the inode cache belonging to dev
1840 * There should not be any active ones, return error if any are found but
1841 * still invalidate others (N.B.: this is a user error, not a system error).
1843 * Also, count the references to dev by block devices - this really
1844 * has nothing to do with the object of the procedure, but as we have
1845 * to scan the inode table here anyway, we might as well get the
1849 ud_iflush(struct vfs
*vfsp
)
1851 int32_t index
, busy
= 0;
1853 struct udf_vfs
*udf_vfsp
;
1855 struct vnode
*rvp
, *vp
;
1856 struct ud_inode
*ip
, *next
;
1858 ud_printf("ud_iflush\n");
1859 udf_vfsp
= (struct udf_vfs
*)vfsp
->vfs_data
;
1860 rvp
= udf_vfsp
->udf_root
;
1861 dev
= vfsp
->vfs_dev
;
1863 mutex_enter(&ud_icache_lock
);
1864 for (index
= 0; index
< UD_HASH_SZ
; index
++) {
1865 ih
= &ud_ihead
[index
];
1867 next
= ih
->ih_chain
[0];
1868 while (next
!= (struct ud_inode
*)ih
) {
1871 if (ip
->i_dev
!= dev
) {
1876 * root inode is processed by the caller
1879 if (vp
->v_count
> 1) {
1884 if (ip
->i_flag
& IREF
) {
1886 * Set error indicator for return value,
1887 * but continue invalidating other
1894 rw_enter(&ip
->i_contents
, RW_WRITER
);
1899 * Hold the vnode since its not done
1900 * in fop_putpage anymore.
1904 * XXX Synchronous write holding
1907 (void) ud_syncip(ip
, B_INVAL
, I_SYNC
);
1908 rw_exit(&ip
->i_contents
);
1912 mutex_exit(&ud_icache_lock
);
1919 * Check mode permission on inode. Mode is READ, WRITE or EXEC.
1920 * In the case of WRITE, the read-only status of the file system
1921 * is checked. The applicable mode bits are compared with the
1922 * requested form of access. If bits are missing, the secpolicy
1923 * function will check for privileges.
1926 ud_iaccess(struct ud_inode
*ip
, int32_t mode
, struct cred
*cr
, int dolock
)
1932 rw_enter(&ip
->i_contents
, RW_READER
);
1933 ASSERT(RW_LOCK_HELD(&ip
->i_contents
));
1935 ud_printf("ud_iaccess\n");
1936 if (mode
& IWRITE
) {
1938 * Disallow write attempts on read-only
1939 * file systems, unless the file is a block
1940 * or character device or a FIFO.
1942 if (ip
->i_udf
->udf_flags
& UDF_FL_RDONLY
) {
1943 if ((ip
->i_type
!= VCHR
) &&
1944 (ip
->i_type
!= VBLK
) &&
1945 (ip
->i_type
!= VFIFO
)) {
1953 * Access check is based on only
1954 * one of owner, group, public.
1955 * If not owner, then check group.
1956 * If not a member of the group, then
1957 * check public access.
1959 if (crgetuid(cr
) != ip
->i_uid
) {
1961 if (!groupmember((uid_t
)ip
->i_gid
, cr
))
1965 ret
= secpolicy_vnode_access2(cr
, ITOV(ip
), ip
->i_uid
,
1966 UD2VA_PERM(ip
->i_perm
<< shift
), UD2VA_PERM(mode
));
1970 rw_exit(&ip
->i_contents
);
1975 ud_imark(struct ud_inode
*ip
)
1980 ud_printf("ud_imark\n");
1981 if (ip
->i_flag
& IACC
) {
1982 ip
->i_atime
.tv_sec
= now
.tv_sec
;
1983 ip
->i_atime
.tv_nsec
= now
.tv_nsec
;
1985 if (ip
->i_flag
& IUPD
) {
1986 ip
->i_mtime
.tv_sec
= now
.tv_sec
;
1987 ip
->i_mtime
.tv_nsec
= now
.tv_nsec
;
1988 ip
->i_flag
|= IMODTIME
;
1990 if (ip
->i_flag
& ICHG
) {
1992 ip
->i_ctime
.tv_sec
= now
.tv_sec
;
1993 ip
->i_ctime
.tv_nsec
= now
.tv_nsec
;
1999 ud_itimes_nolock(struct ud_inode
*ip
)
2001 ud_printf("ud_itimes_nolock\n");
2003 if (ip
->i_flag
& (IUPD
|IACC
|ICHG
)) {
2004 if (ip
->i_flag
& ICHG
) {
2007 ip
->i_flag
|= IMODACC
;
2010 ip
->i_flag
&= ~(IACC
|IUPD
|ICHG
);
2015 ud_delcache(struct ud_inode
*ip
)
2017 ud_printf("ud_delcache\n");
2019 mutex_enter(&ud_icache_lock
);
2023 mutex_exit(&ud_icache_lock
);
2027 ud_idrop(struct ud_inode
*ip
)
2029 struct vnode
*vp
= ITOV(ip
);
2031 ASSERT(RW_WRITE_HELD(&ip
->i_contents
));
2033 ud_printf("ud_idrop\n");
2035 mutex_enter(&vp
->v_lock
);
2037 if (vp
->v_count
> 0) {
2038 mutex_exit(&vp
->v_lock
);
2041 mutex_exit(&vp
->v_lock
);
2044 * if inode is invalid or there is no page associated with
2045 * this inode, put the inode in the front of the free list
2047 mutex_enter(&ip
->i_tlock
);
2048 mutex_enter(&udf_ifree_lock
);
2049 if (!vn_has_cached_data(vp
) || ip
->i_perm
== 0) {
2050 ud_add_to_free_list(ip
, UD_BEGIN
);
2053 * Otherwise, put the inode back on the end of the free list.
2055 ud_add_to_free_list(ip
, UD_END
);
2057 mutex_exit(&udf_ifree_lock
);
2058 ip
->i_flag
&= IMODTIME
;
2059 mutex_exit(&ip
->i_tlock
);
2063 ud_add_to_free_list(struct ud_inode
*ip
, uint32_t at
)
2066 ASSERT(mutex_owned(&udf_ifree_lock
));
2069 /* Search if the element is already in the list */
2070 if (udf_ifreeh
!= NULL
) {
2071 struct ud_inode
*iq
;
2076 cmn_err(CE_WARN
, "Duplicate %p\n", (void *)ip
);
2085 if (udf_ifreeh
== NULL
) {
2087 * Nothing on the list just add it
2092 if (at
== UD_BEGIN
) {
2094 * Add at the begining of the list
2096 ip
->i_freef
= udf_ifreeh
;
2097 udf_ifreeh
->i_freeb
= ip
;
2101 * Add at the end of the list
2103 ip
->i_freeb
= udf_ifreet
;
2104 udf_ifreet
->i_freef
= ip
;
2111 ud_remove_from_free_list(struct ud_inode
*ip
, uint32_t at
)
2114 ASSERT(mutex_owned(&udf_ifree_lock
));
2118 struct ud_inode
*iq
;
2129 cmn_err(CE_WARN
, "ip %p is found %x times\n",
2135 if ((ip
->i_freef
== NULL
) && (ip
->i_freeb
== NULL
)) {
2136 if (ip
!= udf_ifreeh
) {
2141 if ((at
== UD_BEGIN
) || (ip
== udf_ifreeh
)) {
2142 udf_ifreeh
= ip
->i_freef
;
2143 if (ip
->i_freef
== NULL
) {
2146 udf_ifreeh
->i_freeb
= NULL
;
2149 ip
->i_freeb
->i_freef
= ip
->i_freef
;
2151 ip
->i_freef
->i_freeb
= ip
->i_freeb
;
2153 udf_ifreet
= ip
->i_freeb
;
2161 ud_init_inodes(void)
2163 union ihead
*ih
= ud_ihead
;
2166 _NOTE(NO_COMPETING_THREADS_NOW
);
2167 for (index
= 0; index
< UD_HASH_SZ
; index
++, ih
++) {
2168 ih
->ih_head
[0] = ih
;
2169 ih
->ih_head
[1] = ih
;
2171 mutex_init(&ud_icache_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2172 mutex_init(&ud_nino_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2176 mutex_init(&udf_ifree_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
2178 mutex_init(&ud_sync_busy
, NULL
, MUTEX_DEFAULT
, NULL
);
2179 udf_vfs_instances
= NULL
;
2180 mutex_init(&udf_vfs_mutex
, NULL
, MUTEX_DEFAULT
, NULL
);
2182 _NOTE(COMPETING_THREADS_NOW
);