1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /** |
---|
4 | * @file |
---|
5 | * |
---|
6 | * @ingroup IMFS |
---|
7 | * |
---|
8 | * @brief IMFS Create a New Link Node |
---|
9 | */ |
---|
10 | |
---|
11 | /* |
---|
12 | * COPYRIGHT (c) 1989-2010. |
---|
13 | * On-Line Applications Research Corporation (OAR). |
---|
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 COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
25 | * AND 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 COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
28 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
29 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
30 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
31 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
32 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
33 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
34 | * POSSIBILITY OF SUCH DAMAGE. |
---|
35 | */ |
---|
36 | |
---|
37 | #ifdef HAVE_CONFIG_H |
---|
38 | #include "config.h" |
---|
39 | #endif |
---|
40 | |
---|
41 | #include <rtems/imfsimpl.h> |
---|
42 | |
---|
43 | static const IMFS_node_control IMFS_node_control_hard_link; |
---|
44 | |
---|
45 | int IMFS_link( |
---|
46 | const rtems_filesystem_location_info_t *parentloc, |
---|
47 | const rtems_filesystem_location_info_t *targetloc, |
---|
48 | const char *name, |
---|
49 | size_t namelen |
---|
50 | ) |
---|
51 | { |
---|
52 | IMFS_jnode_t *new_node; |
---|
53 | IMFS_jnode_t *target; |
---|
54 | |
---|
55 | target = targetloc->node_access; |
---|
56 | |
---|
57 | /* |
---|
58 | * Verify this node can be linked to. |
---|
59 | */ |
---|
60 | if ( target->st_nlink >= LINK_MAX ) |
---|
61 | rtems_set_errno_and_return_minus_one( EMLINK ); |
---|
62 | |
---|
63 | /* |
---|
64 | * Create a new link node. |
---|
65 | */ |
---|
66 | new_node = IMFS_create_node( |
---|
67 | parentloc, |
---|
68 | &IMFS_node_control_hard_link, |
---|
69 | sizeof( IMFS_link_t ), |
---|
70 | name, |
---|
71 | namelen, |
---|
72 | IMFS_STAT_FMT_HARD_LINK | ( S_IRWXU | S_IRWXG | S_IRWXO ), |
---|
73 | target |
---|
74 | ); |
---|
75 | |
---|
76 | if ( !new_node ) |
---|
77 | rtems_set_errno_and_return_minus_one( ENOMEM ); |
---|
78 | |
---|
79 | /* |
---|
80 | * Increment the link count of the node being pointed to. |
---|
81 | */ |
---|
82 | target->reference_count++; |
---|
83 | target->st_nlink++; |
---|
84 | IMFS_update_ctime( target ); |
---|
85 | |
---|
86 | return 0; |
---|
87 | } |
---|
88 | |
---|
89 | static int IMFS_stat_hard_link( |
---|
90 | const rtems_filesystem_location_info_t *loc, |
---|
91 | struct stat *buf |
---|
92 | ) |
---|
93 | { |
---|
94 | const IMFS_link_t *hard_link = loc->node_access; |
---|
95 | rtems_filesystem_location_info_t targetloc = *loc; |
---|
96 | |
---|
97 | targetloc.node_access = hard_link->link_node; |
---|
98 | IMFS_Set_handlers( &targetloc ); |
---|
99 | |
---|
100 | return (targetloc.handlers->fstat_h)( &targetloc, buf ); |
---|
101 | } |
---|
102 | |
---|
103 | static const rtems_filesystem_file_handlers_r IMFS_link_handlers = { |
---|
104 | .open_h = rtems_filesystem_default_open, |
---|
105 | .close_h = rtems_filesystem_default_close, |
---|
106 | .read_h = rtems_filesystem_default_read, |
---|
107 | .write_h = rtems_filesystem_default_write, |
---|
108 | .ioctl_h = rtems_filesystem_default_ioctl, |
---|
109 | .lseek_h = rtems_filesystem_default_lseek, |
---|
110 | .fstat_h = IMFS_stat_hard_link, |
---|
111 | .ftruncate_h = rtems_filesystem_default_ftruncate, |
---|
112 | .fsync_h = rtems_filesystem_default_fsync_or_fdatasync, |
---|
113 | .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync, |
---|
114 | .fcntl_h = rtems_filesystem_default_fcntl, |
---|
115 | .kqfilter_h = rtems_filesystem_default_kqfilter, |
---|
116 | .mmap_h = rtems_filesystem_default_mmap, |
---|
117 | .poll_h = rtems_filesystem_default_poll, |
---|
118 | .readv_h = rtems_filesystem_default_readv, |
---|
119 | .writev_h = rtems_filesystem_default_writev |
---|
120 | }; |
---|
121 | |
---|
122 | static IMFS_jnode_t *IMFS_node_initialize_hard_link( |
---|
123 | IMFS_jnode_t *node, |
---|
124 | void *arg |
---|
125 | ) |
---|
126 | { |
---|
127 | IMFS_link_t *hard_link = (IMFS_link_t *) node; |
---|
128 | |
---|
129 | hard_link->link_node = arg; |
---|
130 | |
---|
131 | return node; |
---|
132 | } |
---|
133 | |
---|
134 | static IMFS_jnode_t *IMFS_node_remove_hard_link( |
---|
135 | IMFS_jnode_t *node |
---|
136 | ) |
---|
137 | { |
---|
138 | IMFS_link_t *hard_link = (IMFS_link_t *) node; |
---|
139 | IMFS_jnode_t *target = hard_link->link_node; |
---|
140 | |
---|
141 | _Assert( target != NULL ); |
---|
142 | |
---|
143 | if ( target->st_nlink == 1 ) { |
---|
144 | target = (*target->control->node_remove)( target ); |
---|
145 | if ( target == NULL ) { |
---|
146 | node = NULL; |
---|
147 | } |
---|
148 | } else { |
---|
149 | --target->st_nlink; |
---|
150 | IMFS_update_ctime( target ); |
---|
151 | } |
---|
152 | |
---|
153 | if ( target != NULL ) { |
---|
154 | --target->reference_count; |
---|
155 | |
---|
156 | if ( target->reference_count == 0 ) { |
---|
157 | IMFS_node_destroy( target ); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | return node; |
---|
162 | } |
---|
163 | |
---|
164 | static const IMFS_node_control IMFS_node_control_hard_link = { |
---|
165 | .handlers = &IMFS_link_handlers, |
---|
166 | .node_initialize = IMFS_node_initialize_hard_link, |
---|
167 | .node_remove = IMFS_node_remove_hard_link, |
---|
168 | .node_destroy = IMFS_node_destroy_default |
---|
169 | }; |
---|