1 | /** |
---|
2 | * @file |
---|
3 | * |
---|
4 | * @brief IMFS Create a New Link Node |
---|
5 | * @ingroup IMFS |
---|
6 | */ |
---|
7 | |
---|
8 | /* |
---|
9 | * COPYRIGHT (c) 1989-2010. |
---|
10 | * On-Line Applications Research Corporation (OAR). |
---|
11 | * |
---|
12 | * The license and distribution terms for this file may be |
---|
13 | * found in the file LICENSE in this distribution or at |
---|
14 | * http://www.rtems.org/license/LICENSE. |
---|
15 | */ |
---|
16 | |
---|
17 | #if HAVE_CONFIG_H |
---|
18 | #include "config.h" |
---|
19 | #endif |
---|
20 | |
---|
21 | #include "imfs.h" |
---|
22 | |
---|
23 | static const IMFS_node_control IMFS_node_control_hard_link; |
---|
24 | |
---|
25 | int IMFS_link( |
---|
26 | const rtems_filesystem_location_info_t *parentloc, |
---|
27 | const rtems_filesystem_location_info_t *targetloc, |
---|
28 | const char *name, |
---|
29 | size_t namelen |
---|
30 | ) |
---|
31 | { |
---|
32 | IMFS_jnode_t *new_node; |
---|
33 | IMFS_jnode_t *target; |
---|
34 | |
---|
35 | target = targetloc->node_access; |
---|
36 | |
---|
37 | /* |
---|
38 | * Verify this node can be linked to. |
---|
39 | */ |
---|
40 | if ( target->st_nlink >= LINK_MAX ) |
---|
41 | rtems_set_errno_and_return_minus_one( EMLINK ); |
---|
42 | |
---|
43 | /* |
---|
44 | * Create a new link node. |
---|
45 | */ |
---|
46 | new_node = IMFS_create_node( |
---|
47 | parentloc, |
---|
48 | &IMFS_node_control_hard_link, |
---|
49 | sizeof( IMFS_link_t ), |
---|
50 | name, |
---|
51 | namelen, |
---|
52 | IMFS_STAT_FMT_HARD_LINK | ( S_IRWXU | S_IRWXG | S_IRWXO ), |
---|
53 | target |
---|
54 | ); |
---|
55 | |
---|
56 | if ( !new_node ) |
---|
57 | rtems_set_errno_and_return_minus_one( ENOMEM ); |
---|
58 | |
---|
59 | /* |
---|
60 | * Increment the link count of the node being pointed to. |
---|
61 | */ |
---|
62 | target->reference_count++; |
---|
63 | target->st_nlink++; |
---|
64 | IMFS_update_ctime( target ); |
---|
65 | |
---|
66 | return 0; |
---|
67 | } |
---|
68 | |
---|
69 | static int IMFS_stat_hard_link( |
---|
70 | const rtems_filesystem_location_info_t *loc, |
---|
71 | struct stat *buf |
---|
72 | ) |
---|
73 | { |
---|
74 | const IMFS_link_t *hard_link = loc->node_access; |
---|
75 | rtems_filesystem_location_info_t targetloc = *loc; |
---|
76 | |
---|
77 | targetloc.node_access = hard_link->link_node; |
---|
78 | IMFS_Set_handlers( &targetloc ); |
---|
79 | |
---|
80 | return (targetloc.handlers->fstat_h)( &targetloc, buf ); |
---|
81 | } |
---|
82 | |
---|
83 | static const rtems_filesystem_file_handlers_r IMFS_link_handlers = { |
---|
84 | .open_h = rtems_filesystem_default_open, |
---|
85 | .close_h = rtems_filesystem_default_close, |
---|
86 | .read_h = rtems_filesystem_default_read, |
---|
87 | .write_h = rtems_filesystem_default_write, |
---|
88 | .ioctl_h = rtems_filesystem_default_ioctl, |
---|
89 | .lseek_h = rtems_filesystem_default_lseek, |
---|
90 | .fstat_h = IMFS_stat_hard_link, |
---|
91 | .ftruncate_h = rtems_filesystem_default_ftruncate, |
---|
92 | .fsync_h = rtems_filesystem_default_fsync_or_fdatasync, |
---|
93 | .fdatasync_h = rtems_filesystem_default_fsync_or_fdatasync, |
---|
94 | .fcntl_h = rtems_filesystem_default_fcntl, |
---|
95 | .kqfilter_h = rtems_filesystem_default_kqfilter, |
---|
96 | .poll_h = rtems_filesystem_default_poll, |
---|
97 | .readv_h = rtems_filesystem_default_readv, |
---|
98 | .writev_h = rtems_filesystem_default_writev |
---|
99 | }; |
---|
100 | |
---|
101 | static IMFS_jnode_t *IMFS_node_initialize_hard_link( |
---|
102 | IMFS_jnode_t *node, |
---|
103 | void *arg |
---|
104 | ) |
---|
105 | { |
---|
106 | IMFS_link_t *hard_link = (IMFS_link_t *) node; |
---|
107 | |
---|
108 | hard_link->link_node = arg; |
---|
109 | |
---|
110 | return node; |
---|
111 | } |
---|
112 | |
---|
113 | static IMFS_jnode_t *IMFS_node_remove_hard_link( |
---|
114 | IMFS_jnode_t *node |
---|
115 | ) |
---|
116 | { |
---|
117 | IMFS_link_t *hard_link = (IMFS_link_t *) node; |
---|
118 | IMFS_jnode_t *target = hard_link->link_node; |
---|
119 | |
---|
120 | _Assert( target != NULL ); |
---|
121 | |
---|
122 | if ( target->st_nlink == 1 ) { |
---|
123 | target = (*target->control->node_remove)( target ); |
---|
124 | if ( target == NULL ) { |
---|
125 | node = NULL; |
---|
126 | } |
---|
127 | } else { |
---|
128 | --target->st_nlink; |
---|
129 | IMFS_update_ctime( target ); |
---|
130 | } |
---|
131 | |
---|
132 | if ( target != NULL ) { |
---|
133 | --target->reference_count; |
---|
134 | |
---|
135 | if ( target->reference_count == 0 ) { |
---|
136 | IMFS_node_destroy( target ); |
---|
137 | } |
---|
138 | } |
---|
139 | |
---|
140 | return node; |
---|
141 | } |
---|
142 | |
---|
143 | static const IMFS_node_control IMFS_node_control_hard_link = { |
---|
144 | .handlers = &IMFS_link_handlers, |
---|
145 | .node_initialize = IMFS_node_initialize_hard_link, |
---|
146 | .node_remove = IMFS_node_remove_hard_link, |
---|
147 | .node_destroy = IMFS_node_destroy_default |
---|
148 | }; |
---|