1 .\" $NetBSD: fileassoc.9,v 1.21 2008/05/28 00:31:10 snj Exp $
3 .\" Copyright (c) 2006 Elad Efrat <elad@NetBSD.org>
4 .\" All rights reserved.
6 .\" Redistribution and use in source and binary forms, with or without
7 .\" modification, are permitted provided that the following conditions
9 .\" 1. Redistributions of source code must retain the above copyright
10 .\" notice, this list of conditions and the following disclaimer.
11 .\" 2. Redistributions in binary form must reproduce the above copyright
12 .\" notice, this list of conditions and the following disclaimer in the
13 .\" documentation and/or other materials provided with the distribution.
14 .\" 3. The name of the author may not be used to endorse or promote products
15 .\" derived from this software without specific prior written permission.
17 .\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 .\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 .Nd in-kernel, file-system independent, file-meta data association
39 KPI allows association of meta-data with files independent of file-system
40 support for such elaborate meta-data.
42 When plugging a new fileassoc to the system, a developer can specify private
43 data to be associated with every file, as well as (potentially different)
44 private data to be associated with every file-system mount.
46 For example, a developer might choose to associate a custom ACL with every
47 file, and a count of total files with ACLs with the mount.
48 .Ss Kernel Programming Interface
49 Designed with simplicity in mind, the
51 KPI usually accepts four different types of parameters to the most commonly
53 .Bl -tag -width "123456"
54 .It Ft struct mount * Ar mp
55 Describing a mount on which to take action.
56 .It Ft struct vnode * Ar vp
57 Describing a file on which to take action.
58 .It Ft fileassoc_t Ar id
59 Describing an id, as returned from a successful call to
60 .Fn fileassoc_register .
62 Describing a custom private data block, attached to either a file or a mount.
67 KPI it is important to keep in mind that the interface provides memory
71 Any additional memory stored in the tables (such as private data-structures
72 used by custom fileassocs) should be allocated and freed by the developer.
75 provides the ability to specify a
78 .Fn fileassoc_register
80 to be called whenever an entry for a file or a mount is deleted.
81 .Ss Fileassoc Registration and Deregistration Routines
82 These routines allow a developer to allocate a
84 slot to be used for private data.
85 .Bl -tag -width "123456"
86 .It Ft int Fn fileassoc_register "const char *name" \
87 "fileassoc_cleanup_cb_t cleanup_cb" "fileassoc_t *result"
88 Registers a new fileassoc as
94 to be used as identifier in subsequent calls to the
98 .Fn fileassoc_register
99 returns zero on success.
100 Otherwise, an error number will be returned.
106 it will be called during delete/clear operations (see routines below) with
107 indication whether the passed data is file- or mount-specific.
110 should be a function receiving a
116 section for illustration.
118 .It Ft int Fn fileassoc_deregister "fileassoc_t id"
125 .Fn fileassoc_deregister
126 only frees the associated slot in the
129 It is up to the developer to take care of garbage collection.
132 These routines allow lookup of
134 mounts, files, and private data attached to them.
135 .Bl -tag -width "123456"
136 .It Ft void * Fn fileassoc_lookup "struct vnode *vp" "fileassoc_t id"
137 Returns the private data for the file/id combination
142 .Ss Mount-wide Routines
143 .Bl -tag -width "123456"
144 .It Ft int Fn fileassoc_table_delete "struct mount *mp"
145 Deletes a fileassoc table for
148 .It Ft int Fn fileassoc_table_clear "struct mount *mp" "fileassoc_t id"
149 Clear all table entries for
154 If specified, the fileassoc's
156 will be called with a pointer to the private data-structure.
158 .It Ft int Fn fileassoc_table_run "struct mount *mp" "fileassoc_t id" \
159 "fileassoc_cb_t cb" "void *cookie"
164 with the entry being the first argument, and
166 being the second argument.
169 is a function returning
175 .Ss File-specific Routines
176 .Bl -tag -width "123456"
177 .It Ft int Fn fileassoc_file_delete "struct vnode *vp"
178 Delete the fileassoc entries for
183 of all fileassoc types added will be called with a pointer to the corresponding
184 private data structure and indication of
185 .Dv FILEASSOC_CLEANUP_FILE .
187 .Ss Fileassoc-specific Routines
188 .Bl -tag -width "123456"
189 .It Ft int Fn fileassoc_add "struct vnode *vp" "fileassoc_t id" "void *data"
194 for the fileassoc specified by
197 If a table for the mount-point
199 is on doesn't exist, one will be created automatically.
201 manages internally the optimal table sizes as tables are modified.
202 .It Ft int Fn fileassoc_clear "struct vnode *vp" "fileassoc_t id"
203 Clear the private data for
205 for the fileassoc specified by
208 If specified, the fileassoc's
210 will be called with a pointer to the private data-structure and indication of
211 .Dv FILEASSOC_CLEANUP_FILE .
214 The following code examples should give you a clue on using
218 First, we'll begin with registering a new id.
219 We need to do that to save a slot for private data storage with each mount
221 .Bd -literal -offset indent
222 fileassoc_t myhook_id;
225 error = fileassoc_register("my_hook", myhook_cleanup, \*[Am]myhook_id);
230 In the above example we pass a
233 It could look something like this:
234 .Bd -literal -offset indent
236 myhook_cleanup(void *data)
239 printf("Myhook: Removing entry for file.\en");
240 ...handle file entry removal...
245 Another useful thing would be to add our private data to a file.
246 For example, let's assume we keep a custom ACL with each file:
247 .Bd -literal -offset indent
249 myhook_acl_add(struct vnode *vp, struct myhook_acl *acl)
253 error = fileassoc_add(vp, myhook_id, acl);
255 printf("Myhook: Could not add ACL.\en");
259 printf("Myhook: Added ACL.\en");
265 Adding an entry will override any entry that previously exists.
267 Whatever your plug is, eventually you'll want to access the private data you
268 store with each file.
269 To do that you can use the following:
270 .Bd -literal -offset indent
272 myhook_acl_access(struct vnode *vp, int access_flags)
274 struct myhook_acl *acl;
276 acl = fileassoc_lookup(vp, myhook_id);
280 error = myhook_acl_eval(acl, access_flags);
282 printf("Myhook: Denying access based on ACL decision.\en");
290 And, in some cases, it may be desired to remove private data associated with
292 .Bd -literal -offset indent
295 error = fileassoc_clear(vp, myhook_id);
297 printf("Myhook: Error occurred during fileassoc removal.\en");
302 As mentioned previously, the call to
304 will result in a call to the
306 specified in the initial call to
307 .Fn fileassoc_register .
309 The above should be enough to get you started.
313 see the Veriexec code.
315 .Pa src/sys/kern/kern_fileassoc.c
319 KPI first appeared in
322 .An Elad Efrat Aq elad@NetBSD.org
323 .An Brett Lymn Aq blymn@NetBSD.org