1 | /* $Id$ */ |
---|
2 | |
---|
3 | /* NFS client implementation for RTEMS; hooks into the RTEMS filesystem */ |
---|
4 | |
---|
5 | /* Author: Till Straumann <strauman@slac.stanford.edu> 2002 */ |
---|
6 | |
---|
7 | /* |
---|
8 | * Authorship |
---|
9 | * ---------- |
---|
10 | * This software (NFS-2 client implementation for RTEMS) was created by |
---|
11 | * Till Straumann <strauman@slac.stanford.edu>, 2002-2007, |
---|
12 | * Stanford Linear Accelerator Center, Stanford University. |
---|
13 | * |
---|
14 | * Acknowledgement of sponsorship |
---|
15 | * ------------------------------ |
---|
16 | * The NFS-2 client implementation for RTEMS was produced by |
---|
17 | * the Stanford Linear Accelerator Center, Stanford University, |
---|
18 | * under Contract DE-AC03-76SFO0515 with the Department of Energy. |
---|
19 | * |
---|
20 | * Government disclaimer of liability |
---|
21 | * ---------------------------------- |
---|
22 | * Neither the United States nor the United States Department of Energy, |
---|
23 | * nor any of their employees, makes any warranty, express or implied, or |
---|
24 | * assumes any legal liability or responsibility for the accuracy, |
---|
25 | * completeness, or usefulness of any data, apparatus, product, or process |
---|
26 | * disclosed, or represents that its use would not infringe privately owned |
---|
27 | * rights. |
---|
28 | * |
---|
29 | * Stanford disclaimer of liability |
---|
30 | * -------------------------------- |
---|
31 | * Stanford University makes no representations or warranties, express or |
---|
32 | * implied, nor assumes any liability for the use of this software. |
---|
33 | * |
---|
34 | * Stanford disclaimer of copyright |
---|
35 | * -------------------------------- |
---|
36 | * Stanford University, owner of the copyright, hereby disclaims its |
---|
37 | * copyright and all other rights in this software. Hence, anyone may |
---|
38 | * freely use it for any purpose without restriction. |
---|
39 | * |
---|
40 | * Maintenance of notices |
---|
41 | * ---------------------- |
---|
42 | * In the interest of clarity regarding the origin and status of this |
---|
43 | * SLAC software, this and all the preceding Stanford University notices |
---|
44 | * are to remain affixed to any copy or derivative of this software made |
---|
45 | * or distributed by the recipient and are to be affixed to any copy of |
---|
46 | * software made or distributed by the recipient that contains a copy or |
---|
47 | * derivative of this software. |
---|
48 | * |
---|
49 | * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03 |
---|
50 | */ |
---|
51 | |
---|
52 | #ifdef HAVE_CONFIG_H |
---|
53 | #include <config.h> |
---|
54 | #endif |
---|
55 | |
---|
56 | #include <rtems.h> |
---|
57 | #include <rtems/libio.h> |
---|
58 | #include <rtems/libio_.h> |
---|
59 | #include <rtems/seterr.h> |
---|
60 | #include <string.h> |
---|
61 | #include <stdio.h> |
---|
62 | #include <stdlib.h> |
---|
63 | #include <assert.h> |
---|
64 | #include <sys/stat.h> |
---|
65 | #include <dirent.h> |
---|
66 | #include <netdb.h> |
---|
67 | #include <ctype.h> |
---|
68 | #include <netinet/in.h> |
---|
69 | #include <arpa/inet.h> |
---|
70 | |
---|
71 | #include <nfs_prot.h> |
---|
72 | #include <mount_prot.h> |
---|
73 | |
---|
74 | #include "rpcio.h" |
---|
75 | |
---|
76 | /* Configurable parameters */ |
---|
77 | |
---|
78 | /* Estimated average length of a filename (including terminating 0). |
---|
79 | * This was calculated by doing |
---|
80 | * |
---|
81 | * find <some root> -print -exec basename '{}' \; > feil |
---|
82 | * wc feil |
---|
83 | * |
---|
84 | * AVG_NAMLEN = (num_chars + num_lines)/num_lines |
---|
85 | */ |
---|
86 | #define CONFIG_AVG_NAMLEN 10 |
---|
87 | |
---|
88 | #define CONFIG_NFS_SMALL_XACT_SIZE 800 /* size of RPC arguments for non-write ops */ |
---|
89 | /* lifetime of NFS attributes in a NfsNode; |
---|
90 | * the time is in seconds and the lifetime is |
---|
91 | * infinite if the symbol is #undef |
---|
92 | */ |
---|
93 | #define CONFIG_ATTR_LIFETIME 10/*secs*/ |
---|
94 | |
---|
95 | /* |
---|
96 | * The 'st_blksize' (stat(2)) value this nfs |
---|
97 | * client should report. If set to zero then the server's fattr data |
---|
98 | * is passed throught which is not necessary optimal. |
---|
99 | * Newlib's stdio uses 'st_blksize' (if built with HAVE_BLKSIZE defined) |
---|
100 | * to size the default buffer. |
---|
101 | * Due to the overhead of NFS it is probably better to use the maximum |
---|
102 | * size of an NFS read request (8k) rather than the optimal block |
---|
103 | * size on the server. |
---|
104 | * This value can be overridden at run-time by setting the global |
---|
105 | * variable 'nfsStBlksize'. |
---|
106 | * Thanks to Steven Johnson <sjohnson@sakuraindustries.com> for helping |
---|
107 | * working on this issue. |
---|
108 | */ |
---|
109 | #define DEFAULT_NFS_ST_BLKSIZE NFS_MAXDATA |
---|
110 | |
---|
111 | /* dont change this without changing the maximal write size */ |
---|
112 | #define CONFIG_NFS_BIG_XACT_SIZE UDPMSGSIZE /* dont change this */ |
---|
113 | |
---|
114 | /* The real values for these are specified further down */ |
---|
115 | #define NFSCALL_TIMEOUT (&_nfscalltimeout) |
---|
116 | #define MNTCALL_TIMEOUT (&_nfscalltimeout) |
---|
117 | static struct timeval _nfscalltimeout = { 10, 0 }; /* {secs, us } */ |
---|
118 | |
---|
119 | /* More or less fixed constants; in particular, NFS3 is not supported */ |
---|
120 | #define DELIM '/' |
---|
121 | #define HOSTDELIM ':' |
---|
122 | #define UPDIR ".." |
---|
123 | #define UIDSEP '@' |
---|
124 | #define NFS_VERSION_2 NFS_VERSION |
---|
125 | |
---|
126 | /* we use a dynamically assigned major number */ |
---|
127 | #define NFS_MAJOR (nfsGlob.nfs_major) |
---|
128 | |
---|
129 | |
---|
130 | /* NOTE: RTEMS (ss-20020301) uses a 'short st_ino' type :-( but the |
---|
131 | * NFS fileid is 32 bit. [Later versions of RTEMS have fixed this; |
---|
132 | * nfsInit() issues a warning if you run a version with 'short st_ino'.] |
---|
133 | * |
---|
134 | * As a workarount, we merge the upper 16bits of the fileid into the |
---|
135 | * minor device no. Hence, it is still possible to uniquely identify |
---|
136 | * a file by looking at its device number (major = nfs, minor = part |
---|
137 | * of the fileid + our 'nfs-id' identifier). |
---|
138 | * |
---|
139 | * This has an impact on performance, as e.g. getcwd() stats() all |
---|
140 | * directory entries when it believes it has crossed a mount point |
---|
141 | * (a.st_dev != b.st_dev). |
---|
142 | * |
---|
143 | * OTOH, it also might cause node comparison failure! E.g. 'getcwd()' |
---|
144 | * assumes that two nodes residing in the same directory must be located |
---|
145 | * on the same device and hence compares 'st_ino' only. |
---|
146 | * If two files in the same directory have the same inode number |
---|
147 | * modulo 2^16, they will be considered equal (although their device |
---|
148 | * number doesn't match - getcwd doesn't look at it). |
---|
149 | * |
---|
150 | * Other software might or might not be affected. |
---|
151 | * |
---|
152 | * The only clean solution to this problem is bumping up the size of |
---|
153 | * 'ino_t' at least to 'long'. |
---|
154 | * Note that this requires _all_ software (libraries etc.) to be |
---|
155 | * recompiled. |
---|
156 | */ |
---|
157 | |
---|
158 | #define NFS_MAKE_DEV_T_INO_HACK(node) \ |
---|
159 | rtems_filesystem_make_dev_t( NFS_MAJOR, \ |
---|
160 | (((rtems_device_minor_number)((node)->nfs->id))<<16) | (((rtems_device_minor_number)SERP_ATTR((node)).fileid) >> 16) ) |
---|
161 | |
---|
162 | /* use our 'nfs id' and the server's fsid for the minor device number |
---|
163 | * this should be fairly unique |
---|
164 | */ |
---|
165 | #define NFS_MAKE_DEV_T(node) \ |
---|
166 | rtems_filesystem_make_dev_t( NFS_MAJOR, \ |
---|
167 | (((rtems_device_minor_number)((node)->nfs->id))<<16) | (SERP_ATTR((node)).fsid & (((rtems_device_minor_number)1<<16)-1)) ) |
---|
168 | |
---|
169 | #define DIRENT_HEADER_SIZE ( sizeof(struct dirent) - \ |
---|
170 | sizeof( ((struct dirent *)0)->d_name ) ) |
---|
171 | |
---|
172 | |
---|
173 | /* debugging flags */ |
---|
174 | #define DEBUG_COUNT_NODES (1<<0) |
---|
175 | #define DEBUG_TRACK_NODES (1<<1) |
---|
176 | #define DEBUG_EVALPATH (1<<2) |
---|
177 | #define DEBUG_READDIR (1<<3) |
---|
178 | #define DEBUG_SYSCALLS (1<<4) |
---|
179 | |
---|
180 | /* #define DEBUG ( DEBUG_SYSCALLS | DEBUG_COUNT_NODES ) */ |
---|
181 | |
---|
182 | #ifdef DEBUG |
---|
183 | #define STATIC |
---|
184 | #else |
---|
185 | #define STATIC static |
---|
186 | #endif |
---|
187 | |
---|
188 | #define MUTEX_ATTRIBUTES (RTEMS_LOCAL | \ |
---|
189 | RTEMS_PRIORITY | \ |
---|
190 | RTEMS_INHERIT_PRIORITY | \ |
---|
191 | RTEMS_BINARY_SEMAPHORE) |
---|
192 | |
---|
193 | #define LOCK(s) do { \ |
---|
194 | rtems_semaphore_obtain((s), \ |
---|
195 | RTEMS_WAIT, \ |
---|
196 | RTEMS_NO_TIMEOUT); \ |
---|
197 | } while (0) |
---|
198 | |
---|
199 | #define UNLOCK(s) do { rtems_semaphore_release((s)); \ |
---|
200 | } while (0) |
---|
201 | |
---|
202 | /***************************************** |
---|
203 | Types with Associated XDR Routines |
---|
204 | *****************************************/ |
---|
205 | |
---|
206 | /* a string buffer with a maximal length. |
---|
207 | * If the buffer pointer is NULL, it is updated |
---|
208 | * with an appropriately allocated area. |
---|
209 | */ |
---|
210 | typedef struct strbuf { |
---|
211 | char *buf; |
---|
212 | u_int max; |
---|
213 | } strbuf; |
---|
214 | |
---|
215 | static bool_t |
---|
216 | xdr_strbuf(XDR *xdrs, strbuf *obj) |
---|
217 | { |
---|
218 | return xdr_string(xdrs, &obj->buf, obj->max); |
---|
219 | } |
---|
220 | |
---|
221 | /* Read 'readlink' results into a 'strbuf'. |
---|
222 | * This is convenient as it avoids |
---|
223 | * one extra step of copying / lenght |
---|
224 | * checking. |
---|
225 | */ |
---|
226 | typedef struct readlinkres_strbuf { |
---|
227 | nfsstat status; |
---|
228 | strbuf strbuf; |
---|
229 | } readlinkres_strbuf; |
---|
230 | |
---|
231 | static bool_t |
---|
232 | xdr_readlinkres_strbuf(XDR *xdrs, readlinkres_strbuf *objp) |
---|
233 | { |
---|
234 | if ( !xdr_nfsstat(xdrs, &objp->status) ) |
---|
235 | return FALSE; |
---|
236 | |
---|
237 | if ( NFS_OK == objp->status ) { |
---|
238 | if ( !xdr_string(xdrs, &objp->strbuf.buf, objp->strbuf.max) ) |
---|
239 | return FALSE; |
---|
240 | } |
---|
241 | return TRUE; |
---|
242 | } |
---|
243 | |
---|
244 | |
---|
245 | /* DirInfoRec is used instead of dirresargs |
---|
246 | * to convert recursion into iteration. The |
---|
247 | * 'rpcgen'erated xdr_dirresargs ends up |
---|
248 | * doing nested calls when unpacking the |
---|
249 | * 'next' pointers. |
---|
250 | */ |
---|
251 | |
---|
252 | typedef struct DirInfoRec_ { |
---|
253 | readdirargs readdirargs; |
---|
254 | /* clone of the 'readdirres' fields; |
---|
255 | * the cookie is put into the readdirargs above |
---|
256 | */ |
---|
257 | nfsstat status; |
---|
258 | char *buf, *ptr; |
---|
259 | int len; |
---|
260 | bool_t eofreached; |
---|
261 | } DirInfoRec, *DirInfo; |
---|
262 | |
---|
263 | /* this deals with one entry / record */ |
---|
264 | static bool_t |
---|
265 | xdr_dir_info_entry(XDR *xdrs, DirInfo di) |
---|
266 | { |
---|
267 | union { |
---|
268 | char nambuf[NFS_MAXNAMLEN+1]; |
---|
269 | nfscookie cookie; |
---|
270 | } dummy; |
---|
271 | struct dirent *pde = (struct dirent *)di->ptr; |
---|
272 | u_int fileid; |
---|
273 | char *name; |
---|
274 | register int nlen = 0,len,naligned = 0; |
---|
275 | nfscookie *pcookie; |
---|
276 | |
---|
277 | len = di->len; |
---|
278 | |
---|
279 | if ( !xdr_u_int(xdrs, &fileid) ) |
---|
280 | return FALSE; |
---|
281 | |
---|
282 | /* we must pass the address of a char* */ |
---|
283 | name = (len > NFS_MAXNAMLEN) ? pde->d_name : dummy.nambuf; |
---|
284 | |
---|
285 | if ( !xdr_filename(xdrs, &name) ) { |
---|
286 | return FALSE; |
---|
287 | } |
---|
288 | |
---|
289 | if (len >= 0) { |
---|
290 | nlen = strlen(name); |
---|
291 | naligned = nlen + 1 /* string delimiter */ + 3 /* alignment */; |
---|
292 | naligned &= ~3; |
---|
293 | len -= naligned; |
---|
294 | } |
---|
295 | |
---|
296 | /* if the cookie goes into the DirInfo, we hope this doesn't fail |
---|
297 | * - the caller ends up with an invalid readdirargs cookie otherwise... |
---|
298 | */ |
---|
299 | pcookie = (len >= 0) ? &di->readdirargs.cookie : &dummy.cookie; |
---|
300 | if ( !xdr_nfscookie(xdrs, pcookie) ) { |
---|
301 | return FALSE; |
---|
302 | } |
---|
303 | |
---|
304 | di->len = len; |
---|
305 | /* adjust the buffer pointer */ |
---|
306 | if (len >= 0) { |
---|
307 | pde->d_ino = fileid; |
---|
308 | pde->d_namlen = nlen; |
---|
309 | pde->d_off = di->ptr - di->buf; |
---|
310 | if (name == dummy.nambuf) { |
---|
311 | memcpy(pde->d_name, dummy.nambuf, nlen + 1); |
---|
312 | } |
---|
313 | pde->d_reclen = DIRENT_HEADER_SIZE + naligned; |
---|
314 | di->ptr += pde->d_reclen; |
---|
315 | } |
---|
316 | |
---|
317 | return TRUE; |
---|
318 | } |
---|
319 | |
---|
320 | /* this routine loops over all entries */ |
---|
321 | static bool_t |
---|
322 | xdr_dir_info(XDR *xdrs, DirInfo di) |
---|
323 | { |
---|
324 | DirInfo dip; |
---|
325 | |
---|
326 | if ( !xdr_nfsstat(xdrs, &di->status) ) |
---|
327 | return FALSE; |
---|
328 | |
---|
329 | if ( NFS_OK != di->status ) |
---|
330 | return TRUE; |
---|
331 | |
---|
332 | dip = di; |
---|
333 | |
---|
334 | while (dip) { |
---|
335 | /* reserve space for the dirent 'header' - we assume it's word aligned! */ |
---|
336 | #ifdef DEBUG |
---|
337 | assert( DIRENT_HEADER_SIZE % 4 == 0 ); |
---|
338 | #endif |
---|
339 | dip->len -= DIRENT_HEADER_SIZE; |
---|
340 | |
---|
341 | /* we pass a 0 size - size is unused since |
---|
342 | * we always pass a non-NULL pointer |
---|
343 | */ |
---|
344 | if ( !xdr_pointer(xdrs, (void*)&dip, 0 /* size */, (xdrproc_t)xdr_dir_info_entry) ) |
---|
345 | return FALSE; |
---|
346 | } |
---|
347 | |
---|
348 | if ( ! xdr_bool(xdrs, &di->eofreached) ) |
---|
349 | return FALSE; |
---|
350 | |
---|
351 | /* if everything fits into the XDR buffer but not the user's buffer, |
---|
352 | * they must resume reading from where xdr_dir_info_entry() started |
---|
353 | * skipping and 'eofreached' needs to be adjusted |
---|
354 | */ |
---|
355 | if ( di->len < 0 && di->eofreached ) |
---|
356 | di->eofreached = FALSE; |
---|
357 | |
---|
358 | return TRUE; |
---|
359 | } |
---|
360 | |
---|
361 | |
---|
362 | /* a type better suited for node operations |
---|
363 | * than diropres. |
---|
364 | * fattr and fhs are swapped so parts of this |
---|
365 | * structure may be used as a diroparg which |
---|
366 | * is practical when looking up paths. |
---|
367 | */ |
---|
368 | |
---|
369 | /* Macro for accessing serporid fields |
---|
370 | */ |
---|
371 | #define SERP_ARGS(node) ((node)->serporid.serporid_u.serporid.arg_u) |
---|
372 | #define SERP_ATTR(node) ((node)->serporid.serporid_u.serporid.attributes) |
---|
373 | #define SERP_FILE(node) ((node)->serporid.serporid_u.serporid.file) |
---|
374 | |
---|
375 | |
---|
376 | typedef struct serporidok { |
---|
377 | fattr attributes; |
---|
378 | nfs_fh file; |
---|
379 | union { |
---|
380 | struct { |
---|
381 | filename name; |
---|
382 | } diroparg; |
---|
383 | struct { |
---|
384 | sattr attributes; |
---|
385 | } sattrarg; |
---|
386 | struct { |
---|
387 | uint32_t offset; |
---|
388 | uint32_t count; |
---|
389 | uint32_t totalcount; |
---|
390 | } readarg; |
---|
391 | struct { |
---|
392 | uint32_t beginoffset; |
---|
393 | uint32_t offset; |
---|
394 | uint32_t totalcount; |
---|
395 | struct { |
---|
396 | uint32_t data_len; |
---|
397 | char* data_val; |
---|
398 | } data; |
---|
399 | } writearg; |
---|
400 | struct { |
---|
401 | filename name; |
---|
402 | sattr attributes; |
---|
403 | } createarg; |
---|
404 | struct { |
---|
405 | filename name; |
---|
406 | diropargs to; |
---|
407 | } renamearg; |
---|
408 | struct { |
---|
409 | diropargs to; |
---|
410 | } linkarg; |
---|
411 | struct { |
---|
412 | filename name; |
---|
413 | nfspath to; |
---|
414 | sattr attributes; |
---|
415 | } symlinkarg; |
---|
416 | struct { |
---|
417 | nfscookie cookie; |
---|
418 | uint32_t count; |
---|
419 | } readdirarg; |
---|
420 | } arg_u; |
---|
421 | } serporidok; |
---|
422 | |
---|
423 | typedef struct serporid { |
---|
424 | nfsstat status; |
---|
425 | union { |
---|
426 | serporidok serporid; |
---|
427 | } serporid_u; |
---|
428 | } serporid; |
---|
429 | |
---|
430 | /* an XDR routine to encode/decode the inverted diropres |
---|
431 | * into an nfsnodestat; |
---|
432 | * |
---|
433 | * NOTE: this routine only acts on |
---|
434 | * - 'serporid.status' |
---|
435 | * - 'serporid.file' |
---|
436 | * - 'serporid.attributes' |
---|
437 | * and leaves the 'arg_u' alone. |
---|
438 | * |
---|
439 | * The idea is that a 'diropres' is read into 'serporid' |
---|
440 | * which can then be used as an argument to subsequent |
---|
441 | * NFS-RPCs (after filling in the node's arg_u). |
---|
442 | */ |
---|
443 | static bool_t |
---|
444 | xdr_serporidok(XDR *xdrs, serporidok *objp) |
---|
445 | { |
---|
446 | if (!xdr_nfs_fh (xdrs, &objp->file)) |
---|
447 | return FALSE; |
---|
448 | if (!xdr_fattr (xdrs, &objp->attributes)) |
---|
449 | return FALSE; |
---|
450 | return TRUE; |
---|
451 | } |
---|
452 | |
---|
453 | static bool_t |
---|
454 | xdr_serporid(XDR *xdrs, serporid *objp) |
---|
455 | { |
---|
456 | if (!xdr_nfsstat (xdrs, &objp->status)) |
---|
457 | return FALSE; |
---|
458 | switch (objp->status) { |
---|
459 | case NFS_OK: |
---|
460 | if (!xdr_serporidok(xdrs, &objp->serporid_u.serporid)) |
---|
461 | return FALSE; |
---|
462 | break; |
---|
463 | default: |
---|
464 | break; |
---|
465 | } |
---|
466 | return TRUE; |
---|
467 | } |
---|
468 | |
---|
469 | /***************************************** |
---|
470 | Data Structures and Types |
---|
471 | *****************************************/ |
---|
472 | |
---|
473 | /* 'time()' hack with less overhead; */ |
---|
474 | |
---|
475 | /* assume reading a long word is atomic */ |
---|
476 | #define READ_LONG_IS_ATOMIC |
---|
477 | |
---|
478 | typedef uint32_t TimeStamp; |
---|
479 | |
---|
480 | static inline TimeStamp |
---|
481 | nowSeconds(void) |
---|
482 | { |
---|
483 | rtems_interval rval; |
---|
484 | rtems_clock_get_seconds_since_epoch( &rval ); |
---|
485 | return rval; |
---|
486 | } |
---|
487 | |
---|
488 | |
---|
489 | /* Per mounted FS structure */ |
---|
490 | typedef struct NfsRec_ { |
---|
491 | /* the NFS server we're talking to. |
---|
492 | */ |
---|
493 | RpcUdpServer server; |
---|
494 | /* statistics; how many NfsNodes are |
---|
495 | * currently alive. |
---|
496 | */ |
---|
497 | volatile int nodesInUse; |
---|
498 | #if DEBUG & DEBUG_COUNT_NODES |
---|
499 | /* statistics; how many 'NfsNode.str' |
---|
500 | * strings are currently allocated. |
---|
501 | */ |
---|
502 | volatile int stringsInUse; |
---|
503 | #endif |
---|
504 | /* A small number who uniquely |
---|
505 | * identifies a mounted NFS within |
---|
506 | * this driver (i.e. this NfsRec). |
---|
507 | * Each time a NFS is mounted, the |
---|
508 | * global ID counter is incremented |
---|
509 | * and its value is assigned to the |
---|
510 | * newly created NfsRec. |
---|
511 | */ |
---|
512 | u_short id; |
---|
513 | /* Our RTEMS filesystem mt_entry |
---|
514 | */ |
---|
515 | rtems_filesystem_mount_table_entry_t *mt_entry; |
---|
516 | /* Next NfsRec on a linked list who |
---|
517 | * is anchored at nfsGlob |
---|
518 | */ |
---|
519 | struct NfsRec_ *next; |
---|
520 | /* Who we pretend we are |
---|
521 | */ |
---|
522 | u_long uid,gid; |
---|
523 | } NfsRec, *Nfs; |
---|
524 | |
---|
525 | typedef struct NfsNodeRec_ { |
---|
526 | /* This holds this node's attributes |
---|
527 | * (stats) and its nfs filehandle. |
---|
528 | * It also contains room for nfs rpc |
---|
529 | * arguments. |
---|
530 | */ |
---|
531 | serporid serporid; |
---|
532 | /* The arguments we used when doing |
---|
533 | * the 'lookup' call for this node. |
---|
534 | * We need this information (especially |
---|
535 | * the directory FH) for performing |
---|
536 | * certain operations on this |
---|
537 | * node (in particular: for unlinking |
---|
538 | * it from a parent directory) |
---|
539 | */ |
---|
540 | diropargs args; |
---|
541 | /* FS this node belongs to |
---|
542 | */ |
---|
543 | Nfs nfs; |
---|
544 | /* A buffer for the string the |
---|
545 | * args.name points to. |
---|
546 | * We need this because args.name might |
---|
547 | * temporarily point to strings on the |
---|
548 | * stack. Duplicates are allocated from |
---|
549 | * the heap and attached to 'str' so |
---|
550 | * they can be released as appropriate. |
---|
551 | */ |
---|
552 | char *str; |
---|
553 | /* A timestamp for the stats |
---|
554 | */ |
---|
555 | TimeStamp age; |
---|
556 | } NfsNodeRec, *NfsNode; |
---|
557 | |
---|
558 | /***************************************** |
---|
559 | Forward Declarations |
---|
560 | *****************************************/ |
---|
561 | |
---|
562 | static int nfs_readlink( |
---|
563 | rtems_filesystem_location_info_t *loc, /* IN */ |
---|
564 | char *buf, /* OUT */ |
---|
565 | size_t len |
---|
566 | ); |
---|
567 | |
---|
568 | static int updateAttr(NfsNode node, int force); |
---|
569 | |
---|
570 | /* Mask bits when setting attributes. |
---|
571 | * Only the 'arg' fields with their |
---|
572 | * corresponding bit set in the mask |
---|
573 | * will be used. The others are left |
---|
574 | * unchanged. |
---|
575 | * The 'TOUCH' bits instruct nfs_sattr() |
---|
576 | * to update the respective time |
---|
577 | * fields to the current time |
---|
578 | */ |
---|
579 | #define SATTR_MODE (1<<0) |
---|
580 | #define SATTR_UID (1<<1) |
---|
581 | #define SATTR_GID (1<<2) |
---|
582 | #define SATTR_SIZE (1<<3) |
---|
583 | #define SATTR_ATIME (1<<4) |
---|
584 | #define SATTR_TOUCHA (1<<5) |
---|
585 | #define SATTR_MTIME (1<<6) |
---|
586 | #define SATTR_TOUCHM (1<<7) |
---|
587 | #define SATTR_TOUCH (SATTR_TOUCHM | SATTR_TOUCHA) |
---|
588 | |
---|
589 | static int |
---|
590 | nfs_sattr(NfsNode node, sattr *arg, u_long mask); |
---|
591 | |
---|
592 | extern struct _rtems_filesystem_operations_table nfs_fs_ops; |
---|
593 | static struct _rtems_filesystem_file_handlers_r nfs_file_file_handlers; |
---|
594 | static struct _rtems_filesystem_file_handlers_r nfs_dir_file_handlers; |
---|
595 | static struct _rtems_filesystem_file_handlers_r nfs_link_file_handlers; |
---|
596 | static rtems_driver_address_table drvNfs; |
---|
597 | |
---|
598 | int |
---|
599 | nfsMountsShow(FILE*); |
---|
600 | |
---|
601 | rtems_status_code |
---|
602 | rtems_filesystem_resolve_location(char *buf, int len, rtems_filesystem_location_info_t *loc); |
---|
603 | |
---|
604 | |
---|
605 | /***************************************** |
---|
606 | Inline Routines |
---|
607 | *****************************************/ |
---|
608 | |
---|
609 | |
---|
610 | /* * * * * * * * * * * * * * * * * * |
---|
611 | Trivial Operations on a NfsNode |
---|
612 | * * * * * * * * * * * * * * * * * */ |
---|
613 | |
---|
614 | /* determine if a location 'l' is an NFS root node */ |
---|
615 | static inline int |
---|
616 | locIsRoot(rtems_filesystem_location_info_t *l) |
---|
617 | { |
---|
618 | NfsNode me = (NfsNode) l->node_access; |
---|
619 | NfsNode r; |
---|
620 | r = (NfsNode)l->mt_entry->mt_fs_root.node_access; |
---|
621 | return SERP_ATTR(r).fileid == SERP_ATTR(me).fileid && |
---|
622 | SERP_ATTR(r).fsid == SERP_ATTR(me).fsid; |
---|
623 | } |
---|
624 | |
---|
625 | /* determine if a location 'l' is an NFS node */ |
---|
626 | static inline int |
---|
627 | locIsNfs(rtems_filesystem_location_info_t *l) |
---|
628 | { |
---|
629 | return l->ops == &nfs_fs_ops; |
---|
630 | } |
---|
631 | |
---|
632 | /* determine if two locations refer to the |
---|
633 | * same entity. We know that 'nfsloc' is a |
---|
634 | * location inside nfs. However, we needn't |
---|
635 | * know anything about 'anyloc'. |
---|
636 | */ |
---|
637 | static inline int |
---|
638 | locAreEqual( |
---|
639 | rtems_filesystem_location_info_t *nfsloc, |
---|
640 | rtems_filesystem_location_info_t *anyloc |
---|
641 | ) |
---|
642 | { |
---|
643 | NfsNode na = (NfsNode) nfsloc->node_access; |
---|
644 | NfsNode nb; |
---|
645 | |
---|
646 | if (!locIsNfs(anyloc)) |
---|
647 | return 0; |
---|
648 | |
---|
649 | nb = (NfsNode) anyloc->node_access; |
---|
650 | |
---|
651 | if (na->nfs != nb->nfs) |
---|
652 | return 0; |
---|
653 | |
---|
654 | updateAttr(nb, 0); |
---|
655 | |
---|
656 | return SERP_ATTR(na).fileid == SERP_ATTR(nb).fileid && |
---|
657 | SERP_ATTR(na).fsid == SERP_ATTR(nb).fsid; |
---|
658 | } |
---|
659 | |
---|
660 | |
---|
661 | |
---|
662 | /***************************************** |
---|
663 | Global Variables |
---|
664 | *****************************************/ |
---|
665 | |
---|
666 | /* These are (except for MAXNAMLEN/MAXPATHLEN) copied from IMFS */ |
---|
667 | |
---|
668 | static rtems_filesystem_limits_and_options_t |
---|
669 | nfs_limits_and_options = { |
---|
670 | 5, /* link_max */ |
---|
671 | 6, /* max_canon */ |
---|
672 | 7, /* max_input */ |
---|
673 | NFS_MAXNAMLEN, /* name_max */ |
---|
674 | NFS_MAXPATHLEN, /* path_max */ |
---|
675 | 2, /* pipe_buf */ |
---|
676 | 1, /* posix_async_io */ |
---|
677 | 2, /* posix_chown_restrictions */ |
---|
678 | 3, /* posix_no_trunc */ |
---|
679 | 4, /* posix_prio_io */ |
---|
680 | 5, /* posix_sync_io */ |
---|
681 | 6 /* posix_vdisable */ |
---|
682 | }; |
---|
683 | |
---|
684 | /* size of an encoded 'entry' object */ |
---|
685 | static int dirres_entry_size; |
---|
686 | |
---|
687 | /* Global stuff and statistics */ |
---|
688 | static struct nfsstats { |
---|
689 | /* A lock for protecting the |
---|
690 | * linked ist of mounted NFS |
---|
691 | * and the num_mounted_fs field |
---|
692 | */ |
---|
693 | rtems_id llock; |
---|
694 | /* A lock for protecting misc |
---|
695 | * stuff within the driver. |
---|
696 | * The lock must only be held |
---|
697 | * for short periods of time. |
---|
698 | */ |
---|
699 | rtems_id lock; |
---|
700 | /* Our major number as assigned |
---|
701 | * by RTEMS |
---|
702 | */ |
---|
703 | rtems_device_major_number nfs_major; |
---|
704 | /* The number of currently |
---|
705 | * mounted NFS |
---|
706 | */ |
---|
707 | int num_mounted_fs; |
---|
708 | /* A list of the currently |
---|
709 | * mounted NFS |
---|
710 | */ |
---|
711 | struct NfsRec_ *mounted_fs; |
---|
712 | /* A counter for allocating |
---|
713 | * unique IDs to each mounted |
---|
714 | * NFS. |
---|
715 | * Assume we are not going to |
---|
716 | * do more than 16k mounts |
---|
717 | * during the system lifetime |
---|
718 | */ |
---|
719 | u_short fs_ids; |
---|
720 | } nfsGlob = {0, 0, 0, 0, 0, 0}; |
---|
721 | |
---|
722 | /* |
---|
723 | * Global variable to tune the 'st_blksize' (stat(2)) value this nfs |
---|
724 | * client should report. |
---|
725 | * size on the server. |
---|
726 | */ |
---|
727 | #ifndef DEFAULT_NFS_ST_BLKSIZE |
---|
728 | #define DEFAULT_NFS_ST_BLKSIZE NFS_MAXDATA |
---|
729 | #endif |
---|
730 | int nfsStBlksize = DEFAULT_NFS_ST_BLKSIZE; |
---|
731 | |
---|
732 | /* Two pools of RPC transactions; |
---|
733 | * One with small send buffers |
---|
734 | * the other with a big one. |
---|
735 | * The actual size of the small |
---|
736 | * buffer is configurable (see top). |
---|
737 | * |
---|
738 | * Note: The RX buffers are always |
---|
739 | * big |
---|
740 | */ |
---|
741 | static RpcUdpXactPool smallPool = 0; |
---|
742 | static RpcUdpXactPool bigPool = 0; |
---|
743 | |
---|
744 | |
---|
745 | /***************************************** |
---|
746 | Implementation |
---|
747 | *****************************************/ |
---|
748 | |
---|
749 | /* Create a Nfs object. This is |
---|
750 | * per-mounted NFS information. |
---|
751 | * |
---|
752 | * ARGS: The Nfs server handle. |
---|
753 | * |
---|
754 | * RETURNS: Nfs on success, |
---|
755 | * NULL on failure with |
---|
756 | * errno set |
---|
757 | * |
---|
758 | * NOTE: The submitted server |
---|
759 | * object is 'owned' by |
---|
760 | * this Nfs and will be |
---|
761 | * destroyed by nfsDestroy() |
---|
762 | */ |
---|
763 | static Nfs |
---|
764 | nfsCreate(RpcUdpServer server) |
---|
765 | { |
---|
766 | Nfs rval = calloc(1,sizeof(*rval)); |
---|
767 | |
---|
768 | if (rval) { |
---|
769 | rval->server = server; |
---|
770 | LOCK(nfsGlob.llock); |
---|
771 | rval->next = nfsGlob.mounted_fs; |
---|
772 | nfsGlob.mounted_fs = rval; |
---|
773 | UNLOCK(nfsGlob.llock); |
---|
774 | } else { |
---|
775 | errno = ENOMEM; |
---|
776 | } |
---|
777 | return rval; |
---|
778 | } |
---|
779 | |
---|
780 | /* Destroy an Nfs object and |
---|
781 | * its associated server |
---|
782 | */ |
---|
783 | static void |
---|
784 | nfsDestroy(Nfs nfs) |
---|
785 | { |
---|
786 | register Nfs prev; |
---|
787 | if (!nfs) |
---|
788 | return; |
---|
789 | |
---|
790 | LOCK(nfsGlob.llock); |
---|
791 | if (nfs == nfsGlob.mounted_fs) |
---|
792 | nfsGlob.mounted_fs = nfs->next; |
---|
793 | else { |
---|
794 | for (prev = nfsGlob.mounted_fs; |
---|
795 | prev && prev->next != nfs; |
---|
796 | prev = prev->next) |
---|
797 | /* nothing else to do */; |
---|
798 | assert( prev ); |
---|
799 | prev->next = nfs->next; |
---|
800 | } |
---|
801 | UNLOCK(nfsGlob.llock); |
---|
802 | |
---|
803 | nfs->next = 0; /* paranoia */ |
---|
804 | rpcUdpServerDestroy(nfs->server); |
---|
805 | free(nfs); |
---|
806 | } |
---|
807 | |
---|
808 | /* |
---|
809 | * Create a Node. The node will |
---|
810 | * be associated with a particular |
---|
811 | * mounted NFS identified by 'nfs' |
---|
812 | * Optionally, a NFS file handle |
---|
813 | * may be copied into this node. |
---|
814 | * |
---|
815 | * ARGS: nfs of the NFS this node |
---|
816 | * belongs to. |
---|
817 | * NFS file handle identifying |
---|
818 | * this node. |
---|
819 | * RETURNS: node on success, |
---|
820 | * NULL on failure with errno |
---|
821 | * set. |
---|
822 | * |
---|
823 | * NOTE: The caller of this routine |
---|
824 | * is responsible for copying |
---|
825 | * a NFS file handle if she |
---|
826 | * choses to pass a NULL fh. |
---|
827 | * |
---|
828 | * The driver code assumes the |
---|
829 | * a node always has a valid |
---|
830 | * NFS filehandle and file |
---|
831 | * attributes (unless the latter |
---|
832 | * are aged). |
---|
833 | */ |
---|
834 | static NfsNode |
---|
835 | nfsNodeCreate(Nfs nfs, fhandle *fh) |
---|
836 | { |
---|
837 | NfsNode rval = malloc(sizeof(*rval)); |
---|
838 | unsigned long flags; |
---|
839 | |
---|
840 | #if DEBUG & DEBUG_TRACK_NODES |
---|
841 | fprintf(stderr,"NFS: creating a node\n"); |
---|
842 | #endif |
---|
843 | |
---|
844 | if (rval) { |
---|
845 | if (fh) |
---|
846 | memcpy( &SERP_FILE(rval), fh, sizeof(*fh) ); |
---|
847 | rtems_interrupt_disable(flags); |
---|
848 | nfs->nodesInUse++; |
---|
849 | rtems_interrupt_enable(flags); |
---|
850 | rval->nfs = nfs; |
---|
851 | rval->str = 0; |
---|
852 | } else { |
---|
853 | errno = ENOMEM; |
---|
854 | } |
---|
855 | |
---|
856 | return rval; |
---|
857 | } |
---|
858 | |
---|
859 | /* destroy a node */ |
---|
860 | static void |
---|
861 | nfsNodeDestroy(NfsNode node) |
---|
862 | { |
---|
863 | unsigned long flags; |
---|
864 | |
---|
865 | #if DEBUG & DEBUG_TRACK_NODES |
---|
866 | fprintf(stderr,"NFS: destroying a node\n"); |
---|
867 | #endif |
---|
868 | #if 0 |
---|
869 | if (!node) |
---|
870 | return; |
---|
871 | /* this probably does nothing... */ |
---|
872 | xdr_free(xdr_serporid, &node->serporid); |
---|
873 | #endif |
---|
874 | |
---|
875 | rtems_interrupt_disable(flags); |
---|
876 | node->nfs->nodesInUse--; |
---|
877 | #if DEBUG & DEBUG_COUNT_NODES |
---|
878 | if (node->str) |
---|
879 | node->nfs->stringsInUse--; |
---|
880 | #endif |
---|
881 | rtems_interrupt_enable(flags); |
---|
882 | |
---|
883 | if (node->str) |
---|
884 | free(node->str); |
---|
885 | |
---|
886 | free(node); |
---|
887 | } |
---|
888 | |
---|
889 | /* Clone a given node (AKA copy constructor), |
---|
890 | * i.e. create an exact copy. |
---|
891 | * |
---|
892 | * ARGS: node to clone |
---|
893 | * RETURNS: new node on success |
---|
894 | * NULL on failure with errno set. |
---|
895 | * |
---|
896 | * NOTE: a string attached to 'str' |
---|
897 | * is cloned as well. Outdated |
---|
898 | * attributes (of the new copy |
---|
899 | * only) will be refreshed |
---|
900 | * (if unsuccessful, this could |
---|
901 | * be a reason for failure to |
---|
902 | * clone a node). |
---|
903 | */ |
---|
904 | static NfsNode |
---|
905 | nfsNodeClone(NfsNode node) |
---|
906 | { |
---|
907 | NfsNode rval = nfsNodeCreate(node->nfs, 0); |
---|
908 | |
---|
909 | if (rval) { |
---|
910 | *rval = *node; |
---|
911 | |
---|
912 | /* must clone the string also */ |
---|
913 | if (node->str) { |
---|
914 | rval->args.name = rval->str = strdup(node->str); |
---|
915 | if (!rval->str) { |
---|
916 | errno = ENOMEM; |
---|
917 | nfsNodeDestroy(rval); |
---|
918 | return 0; |
---|
919 | } |
---|
920 | #if DEBUG & DEBUG_COUNT_NODES |
---|
921 | { unsigned long flags; |
---|
922 | rtems_interrupt_disable(flags); |
---|
923 | node->nfs->stringsInUse++; |
---|
924 | rtems_interrupt_enable(flags); |
---|
925 | } |
---|
926 | #endif |
---|
927 | } |
---|
928 | |
---|
929 | /* possibly update the stats */ |
---|
930 | if (updateAttr(rval, 0 /* only if necessary */)) { |
---|
931 | nfsNodeDestroy(rval); |
---|
932 | return 0; |
---|
933 | } |
---|
934 | } |
---|
935 | return rval; |
---|
936 | } |
---|
937 | |
---|
938 | /* Initialize the driver. |
---|
939 | * |
---|
940 | * ARGS: depth of the small and big |
---|
941 | * transaction pools, i.e. how |
---|
942 | * many transactions (buffers) |
---|
943 | * should always be kept around. |
---|
944 | * |
---|
945 | * (If more transactions are needed, |
---|
946 | * they are created and destroyed |
---|
947 | * on the fly). |
---|
948 | */ |
---|
949 | void |
---|
950 | nfsInit(int smallPoolDepth, int bigPoolDepth) |
---|
951 | { |
---|
952 | static int initialised = 0; |
---|
953 | entry dummy; |
---|
954 | rtems_status_code status; |
---|
955 | |
---|
956 | if (initialised) |
---|
957 | return; |
---|
958 | |
---|
959 | initialised = 1; |
---|
960 | |
---|
961 | fprintf(stderr, |
---|
962 | "RTEMS-NFS $Release$, " \ |
---|
963 | "Till Straumann, Stanford/SLAC/SSRL 2002, " \ |
---|
964 | "See LICENSE file for licensing info.\n"); |
---|
965 | |
---|
966 | /* Get a major number */ |
---|
967 | |
---|
968 | if (RTEMS_SUCCESSFUL != rtems_io_register_driver(0, &drvNfs, &nfsGlob.nfs_major)) { |
---|
969 | fprintf(stderr,"Registering NFS driver failed - %s\n", strerror(errno)); |
---|
970 | return; |
---|
971 | } |
---|
972 | |
---|
973 | if (0==smallPoolDepth) |
---|
974 | smallPoolDepth = 20; |
---|
975 | if (0==bigPoolDepth) |
---|
976 | bigPoolDepth = 10; |
---|
977 | |
---|
978 | /* it's crucial to zero out the 'next' pointer |
---|
979 | * because it terminates the xdr_entry recursion |
---|
980 | * |
---|
981 | * we also must make the filename some non-zero |
---|
982 | * char pointer! |
---|
983 | */ |
---|
984 | |
---|
985 | memset(&dummy, 0, sizeof(dummy)); |
---|
986 | |
---|
987 | dummy.nextentry = 0; |
---|
988 | dummy.name = "somename"; /* guess average length of a filename */ |
---|
989 | dirres_entry_size = xdr_sizeof((xdrproc_t)xdr_entry, &dummy); |
---|
990 | |
---|
991 | smallPool = rpcUdpXactPoolCreate( |
---|
992 | NFS_PROGRAM, |
---|
993 | NFS_VERSION_2, |
---|
994 | CONFIG_NFS_SMALL_XACT_SIZE, |
---|
995 | smallPoolDepth); |
---|
996 | assert( smallPool ); |
---|
997 | |
---|
998 | bigPool = rpcUdpXactPoolCreate( |
---|
999 | NFS_PROGRAM, |
---|
1000 | NFS_VERSION_2, |
---|
1001 | CONFIG_NFS_BIG_XACT_SIZE, |
---|
1002 | bigPoolDepth); |
---|
1003 | assert( bigPool ); |
---|
1004 | |
---|
1005 | status = rtems_semaphore_create( |
---|
1006 | rtems_build_name('N','F','S','l'), |
---|
1007 | 1, |
---|
1008 | MUTEX_ATTRIBUTES, |
---|
1009 | 0, |
---|
1010 | &nfsGlob.llock); |
---|
1011 | assert( status == RTEMS_SUCCESSFUL ); |
---|
1012 | status = rtems_semaphore_create( |
---|
1013 | rtems_build_name('N','F','S','m'), |
---|
1014 | 1, |
---|
1015 | MUTEX_ATTRIBUTES, |
---|
1016 | 0, |
---|
1017 | &nfsGlob.lock); |
---|
1018 | assert( status == RTEMS_SUCCESSFUL ); |
---|
1019 | |
---|
1020 | if (sizeof(ino_t) < sizeof(u_int)) { |
---|
1021 | fprintf(stderr, |
---|
1022 | "WARNING: Using 'short st_ino' hits performance and may fail to access/find correct files\n"); |
---|
1023 | fprintf(stderr, |
---|
1024 | "you should fix newlib's sys/stat.h - for now I'll enable a hack...\n"); |
---|
1025 | |
---|
1026 | } |
---|
1027 | } |
---|
1028 | |
---|
1029 | /* Driver cleanup code |
---|
1030 | */ |
---|
1031 | int |
---|
1032 | nfsCleanup(void) |
---|
1033 | { |
---|
1034 | rtems_id l; |
---|
1035 | int refuse; |
---|
1036 | |
---|
1037 | if (!nfsGlob.llock) { |
---|
1038 | /* registering the driver failed - let them still cleanup */ |
---|
1039 | return 0; |
---|
1040 | } |
---|
1041 | |
---|
1042 | LOCK(nfsGlob.llock); |
---|
1043 | if ( (refuse = nfsGlob.num_mounted_fs) ) { |
---|
1044 | fprintf(stderr,"Refuse to unload NFS; %i filesystems still mounted.\n", |
---|
1045 | refuse); |
---|
1046 | nfsMountsShow(stderr); |
---|
1047 | /* yes, printing is slow - but since you try to unload the driver, |
---|
1048 | * you assume nobody is using NFS, so what if they have to wait? |
---|
1049 | */ |
---|
1050 | UNLOCK(nfsGlob.llock); |
---|
1051 | return -1; |
---|
1052 | } |
---|
1053 | |
---|
1054 | rtems_semaphore_delete(nfsGlob.lock); |
---|
1055 | nfsGlob.lock = 0; |
---|
1056 | |
---|
1057 | /* hold the lock while cleaning up... */ |
---|
1058 | |
---|
1059 | rpcUdpXactPoolDestroy(smallPool); |
---|
1060 | rpcUdpXactPoolDestroy(bigPool); |
---|
1061 | l = nfsGlob.llock; |
---|
1062 | rtems_io_unregister_driver(nfsGlob.nfs_major); |
---|
1063 | |
---|
1064 | rtems_semaphore_delete(l); |
---|
1065 | nfsGlob.llock = 0; |
---|
1066 | return 0; |
---|
1067 | } |
---|
1068 | |
---|
1069 | /* NFS RPC wrapper. |
---|
1070 | * |
---|
1071 | * ARGS: srvr the NFS server we want to call |
---|
1072 | * proc the NFSPROC_xx we want to invoke |
---|
1073 | * xargs xdr routine to wrap the arguments |
---|
1074 | * pargs pointer to the argument object |
---|
1075 | * xres xdr routine to unwrap the results |
---|
1076 | * pres pointer to the result object |
---|
1077 | * |
---|
1078 | * RETURNS: 0 on success, -1 on error with errno set. |
---|
1079 | * |
---|
1080 | * NOTE: the caller assumes that errno is set to |
---|
1081 | * a nonzero value if this routine returns |
---|
1082 | * an error (nonzero return value). |
---|
1083 | * |
---|
1084 | * This routine prints RPC error messages to |
---|
1085 | * stderr. |
---|
1086 | */ |
---|
1087 | STATIC int |
---|
1088 | nfscall( |
---|
1089 | RpcUdpServer srvr, |
---|
1090 | int proc, |
---|
1091 | xdrproc_t xargs, |
---|
1092 | void * pargs, |
---|
1093 | xdrproc_t xres, |
---|
1094 | void * pres) |
---|
1095 | { |
---|
1096 | RpcUdpXact xact; |
---|
1097 | enum clnt_stat stat; |
---|
1098 | RpcUdpXactPool pool; |
---|
1099 | int rval = -1; |
---|
1100 | |
---|
1101 | |
---|
1102 | switch (proc) { |
---|
1103 | case NFSPROC_SYMLINK: |
---|
1104 | case NFSPROC_WRITE: |
---|
1105 | pool = bigPool; break; |
---|
1106 | default: pool = smallPool; break; |
---|
1107 | } |
---|
1108 | |
---|
1109 | xact = rpcUdpXactPoolGet(pool, XactGetCreate); |
---|
1110 | |
---|
1111 | if ( !xact ) { |
---|
1112 | errno = ENOMEM; |
---|
1113 | return -1; |
---|
1114 | } |
---|
1115 | |
---|
1116 | if ( RPC_SUCCESS != (stat=rpcUdpSend( |
---|
1117 | xact, |
---|
1118 | srvr, |
---|
1119 | NFSCALL_TIMEOUT, |
---|
1120 | proc, |
---|
1121 | xres, |
---|
1122 | pres, |
---|
1123 | xargs, |
---|
1124 | pargs, |
---|
1125 | 0)) || |
---|
1126 | RPC_SUCCESS != (stat=rpcUdpRcv(xact)) ) { |
---|
1127 | |
---|
1128 | fprintf(stderr, |
---|
1129 | "NFS (proc %i) - %s\n", |
---|
1130 | proc, |
---|
1131 | clnt_sperrno(stat)); |
---|
1132 | |
---|
1133 | switch (stat) { |
---|
1134 | /* TODO: this is probably not complete and/or fully accurate */ |
---|
1135 | case RPC_CANTENCODEARGS : errno = EINVAL; break; |
---|
1136 | case RPC_AUTHERROR : errno = EPERM; break; |
---|
1137 | |
---|
1138 | case RPC_CANTSEND : |
---|
1139 | case RPC_CANTRECV : /* hope they have errno set */ |
---|
1140 | case RPC_SYSTEMERROR : break; |
---|
1141 | |
---|
1142 | default : errno = EIO; break; |
---|
1143 | } |
---|
1144 | } else { |
---|
1145 | rval = 0; |
---|
1146 | } |
---|
1147 | |
---|
1148 | /* release the transaction back into the pool */ |
---|
1149 | rpcUdpXactPoolPut(xact); |
---|
1150 | |
---|
1151 | if (rval && !errno) |
---|
1152 | errno = EIO; |
---|
1153 | |
---|
1154 | return rval; |
---|
1155 | } |
---|
1156 | |
---|
1157 | /* Check the 'age' of a node's stats |
---|
1158 | * and read the attributes from the server |
---|
1159 | * if necessary. |
---|
1160 | * |
---|
1161 | * ARGS: node node to update |
---|
1162 | * force enforce updating ignoring |
---|
1163 | * the timestamp/age |
---|
1164 | * |
---|
1165 | * RETURNS: 0 on success, |
---|
1166 | * -1 on failure with errno set |
---|
1167 | */ |
---|
1168 | |
---|
1169 | static int |
---|
1170 | updateAttr(NfsNode node, int force) |
---|
1171 | { |
---|
1172 | |
---|
1173 | if (force |
---|
1174 | #ifdef CONFIG_ATTR_LIFETIME |
---|
1175 | || (nowSeconds() - node->age > CONFIG_ATTR_LIFETIME) |
---|
1176 | #endif |
---|
1177 | ) { |
---|
1178 | if ( nfscall(node->nfs->server, |
---|
1179 | NFSPROC_GETATTR, |
---|
1180 | (xdrproc_t)xdr_nfs_fh, &SERP_FILE(node), |
---|
1181 | (xdrproc_t)xdr_attrstat, &node->serporid) ) |
---|
1182 | return -1; |
---|
1183 | |
---|
1184 | if ( NFS_OK != node->serporid.status ) { |
---|
1185 | errno = node->serporid.status; |
---|
1186 | return -1; |
---|
1187 | } |
---|
1188 | |
---|
1189 | node->age = nowSeconds(); |
---|
1190 | } |
---|
1191 | |
---|
1192 | return 0; |
---|
1193 | } |
---|
1194 | |
---|
1195 | /* |
---|
1196 | * IP address helper. Note that we avoid |
---|
1197 | * gethostbyname() since it's not reentrant. |
---|
1198 | * |
---|
1199 | * initialize a sockaddr_in from a |
---|
1200 | * [<uid>'.'<gid>'@']<host>':'<path>" string and let |
---|
1201 | * pPath point to the <path> part; retrieve the optional |
---|
1202 | * uid/gids |
---|
1203 | * |
---|
1204 | * ARGS: see description above |
---|
1205 | * |
---|
1206 | * RETURNS: 0 on success, |
---|
1207 | * -1 on failure with errno set |
---|
1208 | */ |
---|
1209 | static int |
---|
1210 | buildIpAddr(u_long *puid, u_long *pgid, |
---|
1211 | char **pHost, struct sockaddr_in *psa, |
---|
1212 | char **pPath) |
---|
1213 | { |
---|
1214 | char host[30]; |
---|
1215 | char *chpt = *pPath; |
---|
1216 | char *path; |
---|
1217 | int len; |
---|
1218 | |
---|
1219 | if ( !chpt ) { |
---|
1220 | errno = EINVAL; |
---|
1221 | return -1; |
---|
1222 | } |
---|
1223 | |
---|
1224 | /* look for the optional uid/gid */ |
---|
1225 | if ( (chpt = strchr(chpt, UIDSEP)) ) { |
---|
1226 | if ( 2 != sscanf(*pPath,"%li.%li",puid,pgid) ) { |
---|
1227 | errno = EINVAL; |
---|
1228 | return -1; |
---|
1229 | } |
---|
1230 | chpt++; |
---|
1231 | } else { |
---|
1232 | *puid = RPCIOD_DEFAULT_ID; |
---|
1233 | *pgid = RPCIOD_DEFAULT_ID; |
---|
1234 | chpt = *pPath; |
---|
1235 | } |
---|
1236 | if ( pHost ) |
---|
1237 | *pHost = chpt; |
---|
1238 | |
---|
1239 | /* split the device name which is in the form |
---|
1240 | * |
---|
1241 | * <host_ip> ':' <path> |
---|
1242 | * |
---|
1243 | * into its components using a local buffer |
---|
1244 | */ |
---|
1245 | |
---|
1246 | if ( !(path = strchr(chpt, HOSTDELIM)) || |
---|
1247 | (len = path - chpt) >= sizeof(host) - 1 ) { |
---|
1248 | errno = EINVAL; |
---|
1249 | return -1; |
---|
1250 | } |
---|
1251 | /* point to path beyond ':' */ |
---|
1252 | path++; |
---|
1253 | |
---|
1254 | strncpy(host, chpt, len); |
---|
1255 | host[len]=0; |
---|
1256 | |
---|
1257 | if ( ! inet_pton(AF_INET, host, &psa->sin_addr) ) { |
---|
1258 | errno = ENXIO; |
---|
1259 | return -1; |
---|
1260 | } |
---|
1261 | |
---|
1262 | psa->sin_family = AF_INET; |
---|
1263 | psa->sin_port = 0; |
---|
1264 | *pPath = path; |
---|
1265 | return 0; |
---|
1266 | } |
---|
1267 | |
---|
1268 | /* wrapper similar to nfscall. |
---|
1269 | * However, since it is not used |
---|
1270 | * very often, the simpler and less |
---|
1271 | * efficient rpcUdpCallRp API is used. |
---|
1272 | * |
---|
1273 | * ARGS: see 'nfscall()' above |
---|
1274 | * |
---|
1275 | * RETURNS: RPC status |
---|
1276 | */ |
---|
1277 | static enum clnt_stat |
---|
1278 | mntcall( |
---|
1279 | struct sockaddr_in *psrvr, |
---|
1280 | int proc, |
---|
1281 | xdrproc_t xargs, |
---|
1282 | void * pargs, |
---|
1283 | xdrproc_t xres, |
---|
1284 | void * pres, |
---|
1285 | u_long uid, |
---|
1286 | u_long gid) |
---|
1287 | { |
---|
1288 | #ifdef MOUNT_V1_PORT |
---|
1289 | int retry; |
---|
1290 | #endif |
---|
1291 | enum clnt_stat stat = RPC_FAILED; |
---|
1292 | |
---|
1293 | #ifdef MOUNT_V1_PORT |
---|
1294 | /* if the portmapper fails, retry a fixed port */ |
---|
1295 | for (retry = 1, psrvr->sin_port = 0, stat = RPC_FAILED; |
---|
1296 | retry >= 0 && stat; |
---|
1297 | stat && (psrvr->sin_port = htons(MOUNT_V1_PORT)), retry-- ) |
---|
1298 | #endif |
---|
1299 | stat = rpcUdpCallRp( |
---|
1300 | psrvr, |
---|
1301 | MOUNTPROG, |
---|
1302 | MOUNTVERS, |
---|
1303 | proc, |
---|
1304 | xargs, |
---|
1305 | pargs, |
---|
1306 | xres, |
---|
1307 | pres, |
---|
1308 | uid, |
---|
1309 | gid, |
---|
1310 | MNTCALL_TIMEOUT |
---|
1311 | ); |
---|
1312 | return stat; |
---|
1313 | } |
---|
1314 | |
---|
1315 | /***************************************** |
---|
1316 | RTEMS File System Operations for NFS |
---|
1317 | *****************************************/ |
---|
1318 | |
---|
1319 | #if 0 /* for reference */ |
---|
1320 | |
---|
1321 | struct rtems_filesystem_location_info_tt |
---|
1322 | { |
---|
1323 | void *node_access; |
---|
1324 | rtems_filesystem_file_handlers_r *handlers; |
---|
1325 | rtems_filesystem_operations_table *ops; |
---|
1326 | rtems_filesystem_mount_table_entry_t *mt_entry; |
---|
1327 | }; |
---|
1328 | |
---|
1329 | #endif |
---|
1330 | |
---|
1331 | /* |
---|
1332 | * Evaluate a path letting 'pathloc' travel along. |
---|
1333 | * |
---|
1334 | * The important semantics of this operation are: |
---|
1335 | * |
---|
1336 | * If this routine returns -1, the caller assumes |
---|
1337 | * pathloc to be _invalid_ and hence it will not |
---|
1338 | * invoke rtems_filesystem_freenode() on it. |
---|
1339 | * |
---|
1340 | * OTOH, if evalpath returns 0, |
---|
1341 | * rtems_filesystem_freenode() will eventually be |
---|
1342 | * called which results in our freeing the associated |
---|
1343 | * NfsNode attached to node_access. |
---|
1344 | * |
---|
1345 | * Therefore, this routine will _always_ allocate |
---|
1346 | * a NfsNode and pass it out to *pathloc (provided |
---|
1347 | * that the evaluation succeeds). |
---|
1348 | * |
---|
1349 | * However, if the evaluation finds that it has to |
---|
1350 | * step across FS boundaries (mount point or a symlink |
---|
1351 | * pointing outside), the NfsNode is destroyed |
---|
1352 | * before passing control to the new FS' evalpath_h() |
---|
1353 | * |
---|
1354 | */ |
---|
1355 | |
---|
1356 | union nfs_evalpath_arg { |
---|
1357 | int i; |
---|
1358 | const char **c; |
---|
1359 | }; |
---|
1360 | |
---|
1361 | STATIC int nfs_do_evalpath( |
---|
1362 | const char *pathname, /* IN */ |
---|
1363 | int pathnamelen, /* IN */ |
---|
1364 | union nfs_evalpath_arg *arg, |
---|
1365 | rtems_filesystem_location_info_t *pathloc, /* IN/OUT */ |
---|
1366 | int forMake |
---|
1367 | ) |
---|
1368 | { |
---|
1369 | char *del = 0, *part; |
---|
1370 | int e = 0; |
---|
1371 | NfsNode node = pathloc->node_access; |
---|
1372 | char *p = malloc(MAXPATHLEN+1); |
---|
1373 | Nfs nfs = (Nfs)pathloc->mt_entry->fs_info; |
---|
1374 | RpcUdpServer server = nfs->server; |
---|
1375 | unsigned long flags; |
---|
1376 | #if DEBUG & DEBUG_COUNT_NODES |
---|
1377 | unsigned long niu,siu; |
---|
1378 | #endif |
---|
1379 | |
---|
1380 | if ( !p ) { |
---|
1381 | e = ENOMEM; |
---|
1382 | goto cleanup; |
---|
1383 | } |
---|
1384 | memset(p, 0, MAXPATHLEN+1); |
---|
1385 | memcpy(p, pathname, pathnamelen); |
---|
1386 | |
---|
1387 | LOCK(nfsGlob.lock); |
---|
1388 | node = nfsNodeClone(node); |
---|
1389 | UNLOCK(nfsGlob.lock); |
---|
1390 | |
---|
1391 | /* from here on, the NFS is protected from being unmounted |
---|
1392 | * since the node refcount is > 1 |
---|
1393 | */ |
---|
1394 | |
---|
1395 | /* clone the node */ |
---|
1396 | if ( !node ) { |
---|
1397 | /* nodeClone sets errno */ |
---|
1398 | pathloc->node_access = 0; |
---|
1399 | if ( ! (e = errno) ) { |
---|
1400 | /* if we have no node, e must not be zero! */ |
---|
1401 | e = ENOMEM; |
---|
1402 | } |
---|
1403 | goto cleanup; |
---|
1404 | } |
---|
1405 | |
---|
1406 | pathloc->node_access = node; |
---|
1407 | |
---|
1408 | /* Special case: the RTEMS filesystem code |
---|
1409 | * may emit '..' on a regular file node to |
---|
1410 | * find the parent directory :-(. |
---|
1411 | * (eval.c: rtems_filesystem_evaluate_parent()) |
---|
1412 | * Try to catch this case here: |
---|
1413 | */ |
---|
1414 | if ( NFDIR != SERP_ATTR(node).type && '.'==*p && '.'==*(p+1) ) { |
---|
1415 | for ( part = p+2; '/'==*part; part++ ) |
---|
1416 | /* skip trailing '/' */; |
---|
1417 | if ( !*part ) { |
---|
1418 | /* this is it; back out dir and let them look up the dir itself... */ |
---|
1419 | memcpy( &SERP_FILE(node), |
---|
1420 | &node->args.dir, |
---|
1421 | sizeof(node->args.dir)); |
---|
1422 | *(p+1)=0; |
---|
1423 | } |
---|
1424 | } |
---|
1425 | |
---|
1426 | for (part=p; part && *part; part=del) { |
---|
1427 | |
---|
1428 | if ( NFLNK == SERP_ATTR(node).type ) { |
---|
1429 | /* follow midpath link */ |
---|
1430 | char *b = malloc(NFS_MAXPATHLEN+1); |
---|
1431 | int l; |
---|
1432 | |
---|
1433 | if (!b) { |
---|
1434 | e = ENOMEM; |
---|
1435 | goto cleanup; |
---|
1436 | } |
---|
1437 | if (nfs_readlink(pathloc, b, NFS_MAXPATHLEN+1)) { |
---|
1438 | free(b); |
---|
1439 | e = errno; |
---|
1440 | goto cleanup; |
---|
1441 | } |
---|
1442 | |
---|
1443 | /* prepend the link value to the rest of the path */ |
---|
1444 | if ( (l=strlen(b)) + strlen(part) + 1 > NFS_MAXPATHLEN ) { |
---|
1445 | free(b); |
---|
1446 | e = EINVAL; |
---|
1447 | goto cleanup; |
---|
1448 | } |
---|
1449 | /* swap string buffers and reset delimiter */ |
---|
1450 | b[l++] = DELIM; |
---|
1451 | strcpy(b+l,part); |
---|
1452 | part = b; |
---|
1453 | b = p; |
---|
1454 | p = del = part; |
---|
1455 | |
---|
1456 | free(b); |
---|
1457 | |
---|
1458 | /* back up the directory filehandle (only necessary |
---|
1459 | * if we don't back out to the root |
---|
1460 | */ |
---|
1461 | if (! (DELIM == *part) ) { |
---|
1462 | memcpy( &SERP_FILE(node), |
---|
1463 | &node->args.dir, |
---|
1464 | sizeof(node->args.dir)); |
---|
1465 | |
---|
1466 | if (updateAttr(node, 1 /* force */)) { |
---|
1467 | e = errno; |
---|
1468 | goto cleanup; |
---|
1469 | } |
---|
1470 | } |
---|
1471 | } |
---|
1472 | |
---|
1473 | /* find delimiter and eat /// sequences |
---|
1474 | * (only if we don't restart at the root) |
---|
1475 | */ |
---|
1476 | if ( DELIM != *part && (del = strchr(part, DELIM))) { |
---|
1477 | do { |
---|
1478 | *del++=0; |
---|
1479 | } while (DELIM==*del); |
---|
1480 | } |
---|
1481 | |
---|
1482 | /* refuse to backup over the root */ |
---|
1483 | if ( 0==strcmp(part,UPDIR) |
---|
1484 | && locAreEqual(pathloc, &rtems_filesystem_root) ) { |
---|
1485 | part++; |
---|
1486 | } |
---|
1487 | |
---|
1488 | /* cross mountpoint upwards */ |
---|
1489 | if ( (0==strcmp(part,UPDIR) && locIsRoot(pathloc)) /* cross mountpoint up */ |
---|
1490 | || DELIM == *part /* link starts at root */ |
---|
1491 | ) { |
---|
1492 | int rval; |
---|
1493 | |
---|
1494 | #if DEBUG & DEBUG_EVALPATH |
---|
1495 | fprintf(stderr, |
---|
1496 | "Crossing mountpoint upwards\n"); |
---|
1497 | #endif |
---|
1498 | |
---|
1499 | if (DELIM == *part) { |
---|
1500 | *pathloc = rtems_filesystem_root; |
---|
1501 | } else { |
---|
1502 | *pathloc = pathloc->mt_entry->mt_point_node; |
---|
1503 | /* re-append the rest of the path */ |
---|
1504 | if (del) |
---|
1505 | while ( 0 == *--del ) |
---|
1506 | *del = DELIM; |
---|
1507 | } |
---|
1508 | |
---|
1509 | nfsNodeDestroy(node); |
---|
1510 | |
---|
1511 | #if DEBUG & DEBUG_EVALPATH |
---|
1512 | fprintf(stderr, |
---|
1513 | "Re-evaluating '%s'\n", |
---|
1514 | part); |
---|
1515 | #endif |
---|
1516 | |
---|
1517 | if (forMake) |
---|
1518 | rval = pathloc->ops->evalformake_h(part, pathloc, arg->c); |
---|
1519 | else |
---|
1520 | rval = pathloc->ops->evalpath_h(part, strlen(part), arg->i, pathloc); |
---|
1521 | |
---|
1522 | free(p); |
---|
1523 | return rval; |
---|
1524 | } |
---|
1525 | |
---|
1526 | /* lookup one element */ |
---|
1527 | SERP_ARGS(node).diroparg.name = part; |
---|
1528 | |
---|
1529 | /* remember args / directory fh */ |
---|
1530 | memcpy( &node->args, &SERP_FILE(node), sizeof(node->args)); |
---|
1531 | |
---|
1532 | /* don't lookup the item we want to create */ |
---|
1533 | if ( forMake && (!del || !*del) ) |
---|
1534 | break; |
---|
1535 | |
---|
1536 | #if DEBUG & DEBUG_EVALPATH |
---|
1537 | fprintf(stderr,"Looking up '%s'\n",part); |
---|
1538 | #endif |
---|
1539 | |
---|
1540 | if ( nfscall(server, |
---|
1541 | NFSPROC_LOOKUP, |
---|
1542 | (xdrproc_t)xdr_diropargs, &SERP_FILE(node), |
---|
1543 | (xdrproc_t)xdr_serporid, &node->serporid) || |
---|
1544 | NFS_OK != (errno=node->serporid.status) ) { |
---|
1545 | e = errno; |
---|
1546 | goto cleanup; |
---|
1547 | } |
---|
1548 | node->age = nowSeconds(); |
---|
1549 | |
---|
1550 | #if DEBUG & DEBUG_EVALPATH |
---|
1551 | if (NFLNK == SERP_ATTR(node).type && del) { |
---|
1552 | fprintf(stderr, |
---|
1553 | "Following midpath link '%s'\n", |
---|
1554 | part); |
---|
1555 | } |
---|
1556 | #endif |
---|
1557 | |
---|
1558 | } |
---|
1559 | |
---|
1560 | if (forMake) { |
---|
1561 | /* remember the name - do this _before_ copying |
---|
1562 | * the name to local storage; the caller expects a |
---|
1563 | * pointer into pathloc |
---|
1564 | */ |
---|
1565 | assert( node->args.name ); |
---|
1566 | |
---|
1567 | *(const char**)arg = pathname + (node->args.name - p); |
---|
1568 | |
---|
1569 | #if 0 |
---|
1570 | /* restore the directory node */ |
---|
1571 | |
---|
1572 | memcpy( &SERP_FILE(node), |
---|
1573 | &node->args.dir, |
---|
1574 | sizeof(node->args.dir)); |
---|
1575 | |
---|
1576 | if ( (nfscall(nfs->server, |
---|
1577 | NFSPROC_GETATTR, |
---|
1578 | (xdrproc_t)xdr_nfs_fh, &SERP_FILE(node), |
---|
1579 | (xdrproc_t)xdr_attrstat, &node->serporid) && !errno && (errno = EIO)) || |
---|
1580 | (NFS_OK != (errno=node->serporid.status) ) ) { |
---|
1581 | goto cleanup; |
---|
1582 | } |
---|
1583 | #endif |
---|
1584 | } |
---|
1585 | |
---|
1586 | if (locIsRoot(pathloc)) { |
---|
1587 | |
---|
1588 | /* stupid filesystem code has no 'op' for comparing nodes |
---|
1589 | * but just compares the 'node_access' pointers. |
---|
1590 | * Luckily, this is only done for comparing the root nodes. |
---|
1591 | * Hence, we never give them a copy of the root but always |
---|
1592 | * the root itself. |
---|
1593 | */ |
---|
1594 | pathloc->node_access = pathloc->mt_entry->mt_fs_root.node_access; |
---|
1595 | /* increment the 'in use' counter since we return one more |
---|
1596 | * reference to the root node |
---|
1597 | */ |
---|
1598 | rtems_interrupt_disable(flags); |
---|
1599 | nfs->nodesInUse++; |
---|
1600 | rtems_interrupt_enable(flags); |
---|
1601 | nfsNodeDestroy(node); |
---|
1602 | |
---|
1603 | |
---|
1604 | } else { |
---|
1605 | switch (SERP_ATTR(node).type) { |
---|
1606 | case NFDIR: pathloc->handlers = &nfs_dir_file_handlers; break; |
---|
1607 | case NFREG: pathloc->handlers = &nfs_file_file_handlers; break; |
---|
1608 | case NFLNK: pathloc->handlers = &nfs_link_file_handlers; break; |
---|
1609 | default: pathloc->handlers = &rtems_filesystem_null_handlers; break; |
---|
1610 | } |
---|
1611 | pathloc->node_access = node; |
---|
1612 | |
---|
1613 | /* remember the name of this directory entry */ |
---|
1614 | |
---|
1615 | if (node->args.name) { |
---|
1616 | if (node->str) { |
---|
1617 | #if DEBUG & DEBUG_COUNT_NODES |
---|
1618 | rtems_interrupt_disable(flags); |
---|
1619 | nfs->stringsInUse--; |
---|
1620 | rtems_interrupt_enable(flags); |
---|
1621 | #endif |
---|
1622 | free(node->str); |
---|
1623 | } |
---|
1624 | node->args.name = node->str = strdup(node->args.name); |
---|
1625 | if (!node->str) { |
---|
1626 | e = ENOMEM; |
---|
1627 | goto cleanup; |
---|
1628 | } |
---|
1629 | |
---|
1630 | #if DEBUG & DEBUG_COUNT_NODES |
---|
1631 | rtems_interrupt_disable(flags); |
---|
1632 | nfs->stringsInUse++; |
---|
1633 | rtems_interrupt_enable(flags); |
---|
1634 | #endif |
---|
1635 | } |
---|
1636 | |
---|
1637 | } |
---|
1638 | node = 0; |
---|
1639 | |
---|
1640 | e = 0; |
---|
1641 | |
---|
1642 | cleanup: |
---|
1643 | free(p); |
---|
1644 | #if DEBUG & DEBUG_COUNT_NODES |
---|
1645 | /* cache counters; nfs may be unmounted by other thread after the last |
---|
1646 | * node is destroyed |
---|
1647 | */ |
---|
1648 | niu = nfs->nodesInUse; |
---|
1649 | siu = nfs->stringsInUse; |
---|
1650 | #endif |
---|
1651 | if (node) { |
---|
1652 | nfsNodeDestroy(node); |
---|
1653 | pathloc->node_access = 0; |
---|
1654 | } |
---|
1655 | #if DEBUG & DEBUG_COUNT_NODES |
---|
1656 | fprintf(stderr, |
---|
1657 | "leaving evalpath, in use count is %i nodes, %i strings\n", |
---|
1658 | niu,siu); |
---|
1659 | #endif |
---|
1660 | if (e) { |
---|
1661 | #if DEBUG & DEBUG_EVALPATH |
---|
1662 | perror("Evalpath"); |
---|
1663 | #endif |
---|
1664 | rtems_set_errno_and_return_minus_one(e); |
---|
1665 | } else { |
---|
1666 | return 0; |
---|
1667 | } |
---|
1668 | } |
---|
1669 | |
---|
1670 | /* MANDATORY; may set errno=ENOSYS and return -1 */ |
---|
1671 | static int nfs_evalformake( |
---|
1672 | const char *path, /* IN */ |
---|
1673 | rtems_filesystem_location_info_t *pathloc, /* IN/OUT */ |
---|
1674 | const char **pname /* OUT */ |
---|
1675 | ) |
---|
1676 | { |
---|
1677 | union nfs_evalpath_arg args; |
---|
1678 | args.c = pname; |
---|
1679 | |
---|
1680 | return nfs_do_evalpath(path, strlen(path), &args, pathloc, 1 /*forMake*/); |
---|
1681 | } |
---|
1682 | |
---|
1683 | static int nfs_evalpath( |
---|
1684 | const char *path, /* IN */ |
---|
1685 | size_t pathlen, /* IN */ |
---|
1686 | int flags, /* IN */ |
---|
1687 | rtems_filesystem_location_info_t *pathloc /* IN/OUT */ |
---|
1688 | ) |
---|
1689 | { |
---|
1690 | union nfs_evalpath_arg args; |
---|
1691 | args.i = flags; |
---|
1692 | return nfs_do_evalpath(path, pathlen, &args, pathloc, 0 /*not forMake*/); |
---|
1693 | } |
---|
1694 | |
---|
1695 | |
---|
1696 | /* create a hard link */ |
---|
1697 | |
---|
1698 | static int nfs_link( |
---|
1699 | rtems_filesystem_location_info_t *to_loc, /* IN */ |
---|
1700 | rtems_filesystem_location_info_t *parent_loc, /* IN */ |
---|
1701 | const char *name /* IN */ |
---|
1702 | ) |
---|
1703 | { |
---|
1704 | NfsNode pNode; |
---|
1705 | nfsstat status; |
---|
1706 | NfsNode tNode = to_loc->node_access; |
---|
1707 | |
---|
1708 | #if DEBUG & DEBUG_SYSCALLS |
---|
1709 | fprintf(stderr,"Creating link '%s'\n",name); |
---|
1710 | #endif |
---|
1711 | |
---|
1712 | if ( !locIsNfs(parent_loc) ) { |
---|
1713 | errno = EXDEV; |
---|
1714 | return -1; |
---|
1715 | } |
---|
1716 | |
---|
1717 | pNode = parent_loc->node_access; |
---|
1718 | if ( tNode->nfs != pNode->nfs ) { |
---|
1719 | errno = EXDEV; |
---|
1720 | return -1; |
---|
1721 | } |
---|
1722 | memcpy(&SERP_ARGS(tNode).linkarg.to.dir, |
---|
1723 | &SERP_FILE(pNode), |
---|
1724 | sizeof(SERP_FILE(pNode))); |
---|
1725 | |
---|
1726 | SERP_ARGS(tNode).linkarg.to.name = (filename)name; |
---|
1727 | |
---|
1728 | if ( nfscall(tNode->nfs->server, |
---|
1729 | NFSPROC_LINK, |
---|
1730 | (xdrproc_t)xdr_linkargs, &SERP_FILE(tNode), |
---|
1731 | (xdrproc_t)xdr_nfsstat, &status) |
---|
1732 | || (NFS_OK != (errno = status)) |
---|
1733 | ) { |
---|
1734 | #if DEBUG & DEBUG_SYSCALLS |
---|
1735 | perror("nfs_link"); |
---|
1736 | #endif |
---|
1737 | return -1; |
---|
1738 | } |
---|
1739 | |
---|
1740 | return 0; |
---|
1741 | |
---|
1742 | } |
---|
1743 | |
---|
1744 | static int nfs_do_unlink( |
---|
1745 | rtems_filesystem_location_info_t *parent_loc,/* IN */ |
---|
1746 | rtems_filesystem_location_info_t *loc, /* IN */ |
---|
1747 | int proc |
---|
1748 | ) |
---|
1749 | { |
---|
1750 | nfsstat status; |
---|
1751 | NfsNode node = loc->node_access; |
---|
1752 | Nfs nfs = node->nfs; |
---|
1753 | #if DEBUG & DEBUG_SYSCALLS |
---|
1754 | char *name = NFSPROC_REMOVE == proc ? |
---|
1755 | "nfs_unlink" : "nfs_rmdir"; |
---|
1756 | #endif |
---|
1757 | |
---|
1758 | /* The FS generics have determined that pathloc is _not_ |
---|
1759 | * a directory. Hence we may assume that the parent |
---|
1760 | * is in our NFS. |
---|
1761 | */ |
---|
1762 | |
---|
1763 | #if DEBUG & DEBUG_SYSCALLS |
---|
1764 | assert( node->args.name == node->str && node->str ); |
---|
1765 | |
---|
1766 | fprintf(stderr,"%s '%s'\n", name, node->args.name); |
---|
1767 | #endif |
---|
1768 | |
---|
1769 | if ( nfscall(nfs->server, |
---|
1770 | proc, |
---|
1771 | (xdrproc_t)xdr_diropargs, &node->args, |
---|
1772 | (xdrproc_t)xdr_nfsstat, &status) |
---|
1773 | || (NFS_OK != (errno = status)) |
---|
1774 | ) { |
---|
1775 | #if DEBUG & DEBUG_SYSCALLS |
---|
1776 | perror(name); |
---|
1777 | #endif |
---|
1778 | return -1; |
---|
1779 | } |
---|
1780 | |
---|
1781 | return 0; |
---|
1782 | } |
---|
1783 | |
---|
1784 | static int nfs_unlink( |
---|
1785 | rtems_filesystem_location_info_t *parent_loc, /* IN */ |
---|
1786 | rtems_filesystem_location_info_t *loc /* IN */ |
---|
1787 | ) |
---|
1788 | { |
---|
1789 | return nfs_do_unlink(parent_loc, loc, NFSPROC_REMOVE); |
---|
1790 | } |
---|
1791 | |
---|
1792 | static int nfs_chown( |
---|
1793 | rtems_filesystem_location_info_t *pathloc, /* IN */ |
---|
1794 | uid_t owner, /* IN */ |
---|
1795 | gid_t group /* IN */ |
---|
1796 | ) |
---|
1797 | { |
---|
1798 | sattr arg; |
---|
1799 | |
---|
1800 | arg.uid = owner; |
---|
1801 | arg.gid = group; |
---|
1802 | |
---|
1803 | return nfs_sattr(pathloc->node_access, &arg, SATTR_UID | SATTR_GID); |
---|
1804 | |
---|
1805 | } |
---|
1806 | |
---|
1807 | /* Cleanup the FS private info attached to pathloc->node_access */ |
---|
1808 | static int nfs_freenode( |
---|
1809 | rtems_filesystem_location_info_t *pathloc /* IN */ |
---|
1810 | ) |
---|
1811 | { |
---|
1812 | Nfs nfs = ((NfsNode)pathloc->node_access)->nfs; |
---|
1813 | |
---|
1814 | #if DEBUG & DEBUG_COUNT_NODES |
---|
1815 | /* print counts at entry where they are > 0 so 'nfs' is safe from being destroyed |
---|
1816 | * and there's no race condition |
---|
1817 | */ |
---|
1818 | fprintf(stderr, |
---|
1819 | "entering freenode, in use count is %i nodes, %i strings\n", |
---|
1820 | nfs->nodesInUse, |
---|
1821 | nfs->stringsInUse); |
---|
1822 | #endif |
---|
1823 | |
---|
1824 | /* never destroy the root node; it is released by the unmount |
---|
1825 | * code |
---|
1826 | */ |
---|
1827 | if (locIsRoot(pathloc)) { |
---|
1828 | unsigned long flags; |
---|
1829 | /* just adjust the references to the root node but |
---|
1830 | * don't really release it |
---|
1831 | */ |
---|
1832 | rtems_interrupt_disable(flags); |
---|
1833 | nfs->nodesInUse--; |
---|
1834 | rtems_interrupt_enable(flags); |
---|
1835 | } else { |
---|
1836 | nfsNodeDestroy(pathloc->node_access); |
---|
1837 | pathloc->node_access = 0; |
---|
1838 | } |
---|
1839 | return 0; |
---|
1840 | } |
---|
1841 | |
---|
1842 | /* NOTE/TODO: mounting on top of NFS is not currently supported |
---|
1843 | * |
---|
1844 | * Challenge: stateless protocol. It would be possible to |
---|
1845 | * delete mount points on the server. We would need some sort |
---|
1846 | * of a 'garbage collector' looking for dead/unreachable |
---|
1847 | * mount points and unmounting them. |
---|
1848 | * Also, the path evaluation routine would have to check |
---|
1849 | * for crossing mount points. Crossing over from one NFS |
---|
1850 | * into another NFS could probably handled iteratively |
---|
1851 | * rather than by recursion. |
---|
1852 | */ |
---|
1853 | |
---|
1854 | #ifdef DECLARE_BODY |
---|
1855 | /* This routine is called when they try to mount something |
---|
1856 | * on top of THIS filesystem, i.e. if one of our directories |
---|
1857 | * is used as a mount point |
---|
1858 | */ |
---|
1859 | static int nfs_mount( |
---|
1860 | rtems_filesystem_mount_table_entry_t *mt_entry /* in */ |
---|
1861 | )DECLARE_BODY |
---|
1862 | #else |
---|
1863 | #define nfs_mount 0 |
---|
1864 | #endif |
---|
1865 | |
---|
1866 | #ifdef DECLARE_BODY |
---|
1867 | /* This op is called when they try to unmount a FS |
---|
1868 | * from a mountpoint managed by THIS FS. |
---|
1869 | */ |
---|
1870 | static int nfs_unmount( |
---|
1871 | rtems_filesystem_mount_table_entry_t *mt_entry /* in */ |
---|
1872 | )DECLARE_BODY |
---|
1873 | #else |
---|
1874 | #define nfs_unmount 0 |
---|
1875 | #endif |
---|
1876 | |
---|
1877 | #if 0 |
---|
1878 | |
---|
1879 | /* for reference (libio.h) */ |
---|
1880 | |
---|
1881 | struct rtems_filesystem_mount_table_entry_tt { |
---|
1882 | rtems_chain_node Node; |
---|
1883 | rtems_filesystem_location_info_t mt_point_node; |
---|
1884 | rtems_filesystem_location_info_t mt_fs_root; |
---|
1885 | int options; |
---|
1886 | void *fs_info; |
---|
1887 | |
---|
1888 | rtems_filesystem_limits_and_options_t pathconf_limits_and_options; |
---|
1889 | |
---|
1890 | /* |
---|
1891 | * When someone adds a mounted filesystem on a real device, |
---|
1892 | * this will need to be used. |
---|
1893 | * |
---|
1894 | * The best option long term for this is probably an open file descriptor. |
---|
1895 | */ |
---|
1896 | char *dev; |
---|
1897 | }; |
---|
1898 | #endif |
---|
1899 | |
---|
1900 | |
---|
1901 | /* This op is called as the last step of mounting this FS */ |
---|
1902 | STATIC int nfs_fsmount_me( |
---|
1903 | rtems_filesystem_mount_table_entry_t *mt_entry |
---|
1904 | ) |
---|
1905 | { |
---|
1906 | char *host; |
---|
1907 | struct sockaddr_in saddr; |
---|
1908 | enum clnt_stat stat; |
---|
1909 | fhstatus fhstat; |
---|
1910 | u_long uid,gid; |
---|
1911 | #ifdef NFS_V2_PORT |
---|
1912 | int retry; |
---|
1913 | #endif |
---|
1914 | Nfs nfs = 0; |
---|
1915 | NfsNode rootNode = 0; |
---|
1916 | RpcUdpServer nfsServer = 0; |
---|
1917 | int e = -1; |
---|
1918 | char *path = mt_entry->dev; |
---|
1919 | |
---|
1920 | |
---|
1921 | if ( buildIpAddr(&uid, &gid, &host, &saddr, &path) ) |
---|
1922 | return -1; |
---|
1923 | |
---|
1924 | |
---|
1925 | #ifdef NFS_V2_PORT |
---|
1926 | /* if the portmapper fails, retry a fixed port */ |
---|
1927 | for (retry = 1, saddr.sin_port = 0, stat = RPC_FAILED; |
---|
1928 | retry >= 0 && stat; |
---|
1929 | stat && (saddr.sin_port = htons(NFS_V2_PORT)), retry-- ) |
---|
1930 | #endif |
---|
1931 | stat = rpcUdpServerCreate( |
---|
1932 | &saddr, |
---|
1933 | NFS_PROGRAM, |
---|
1934 | NFS_VERSION_2, |
---|
1935 | uid, |
---|
1936 | gid, |
---|
1937 | &nfsServer |
---|
1938 | ); |
---|
1939 | |
---|
1940 | if ( RPC_SUCCESS != stat ) { |
---|
1941 | fprintf(stderr, |
---|
1942 | "Unable to contact NFS server - invalid port? (%s)\n", |
---|
1943 | clnt_sperrno(stat)); |
---|
1944 | e = EPROTONOSUPPORT; |
---|
1945 | goto cleanup; |
---|
1946 | } |
---|
1947 | |
---|
1948 | |
---|
1949 | /* first, try to ping the NFS server by |
---|
1950 | * calling the NULL proc. |
---|
1951 | */ |
---|
1952 | if ( nfscall(nfsServer, |
---|
1953 | NFSPROC_NULL, |
---|
1954 | (xdrproc_t)xdr_void, 0, |
---|
1955 | (xdrproc_t)xdr_void, 0) ) { |
---|
1956 | |
---|
1957 | fputs("NFS Ping ",stderr); |
---|
1958 | fwrite(host, 1, path-host-1, stderr); |
---|
1959 | fprintf(stderr," failed: %s\n", strerror(errno)); |
---|
1960 | |
---|
1961 | e = errno ? errno : EIO; |
---|
1962 | goto cleanup; |
---|
1963 | } |
---|
1964 | |
---|
1965 | /* that seemed to work - we now try the |
---|
1966 | * actual mount |
---|
1967 | */ |
---|
1968 | |
---|
1969 | /* reuse server address but let the mntcall() |
---|
1970 | * search for the mountd's port |
---|
1971 | */ |
---|
1972 | saddr.sin_port = 0; |
---|
1973 | |
---|
1974 | stat = mntcall( &saddr, |
---|
1975 | MOUNTPROC_MNT, |
---|
1976 | (xdrproc_t)xdr_dirpath, |
---|
1977 | &path, |
---|
1978 | (xdrproc_t)xdr_fhstatus, |
---|
1979 | &fhstat, |
---|
1980 | uid, |
---|
1981 | gid ); |
---|
1982 | |
---|
1983 | if (stat) { |
---|
1984 | fprintf(stderr,"MOUNT -- %s\n",clnt_sperrno(stat)); |
---|
1985 | if ( e<=0 ) |
---|
1986 | e = EIO; |
---|
1987 | goto cleanup; |
---|
1988 | } else if (NFS_OK != (e=fhstat.fhs_status)) { |
---|
1989 | fprintf(stderr,"MOUNT: %s\n",strerror(e)); |
---|
1990 | goto cleanup; |
---|
1991 | } |
---|
1992 | |
---|
1993 | nfs = nfsCreate(nfsServer); |
---|
1994 | assert( nfs ); |
---|
1995 | nfsServer = 0; |
---|
1996 | |
---|
1997 | nfs->uid = uid; |
---|
1998 | nfs->gid = gid; |
---|
1999 | |
---|
2000 | /* that seemed to work - we now create the root node |
---|
2001 | * and we also must obtain the root node attributes |
---|
2002 | */ |
---|
2003 | rootNode = nfsNodeCreate(nfs, &fhstat.fhstatus_u.fhs_fhandle); |
---|
2004 | assert( rootNode ); |
---|
2005 | |
---|
2006 | if ( updateAttr(rootNode, 1 /* force */) ) { |
---|
2007 | e = errno; |
---|
2008 | goto cleanup; |
---|
2009 | } |
---|
2010 | |
---|
2011 | /* looks good so far */ |
---|
2012 | |
---|
2013 | mt_entry->mt_fs_root.node_access = rootNode; |
---|
2014 | |
---|
2015 | rootNode = 0; |
---|
2016 | |
---|
2017 | mt_entry->mt_fs_root.ops = &nfs_fs_ops; |
---|
2018 | mt_entry->mt_fs_root.handlers = &nfs_dir_file_handlers; |
---|
2019 | mt_entry->pathconf_limits_and_options = nfs_limits_and_options; |
---|
2020 | |
---|
2021 | LOCK(nfsGlob.llock); |
---|
2022 | nfsGlob.num_mounted_fs++; |
---|
2023 | /* allocate a new ID for this FS */ |
---|
2024 | nfs->id = nfsGlob.fs_ids++; |
---|
2025 | UNLOCK(nfsGlob.llock); |
---|
2026 | |
---|
2027 | mt_entry->fs_info = nfs; |
---|
2028 | nfs->mt_entry = mt_entry; |
---|
2029 | nfs = 0; |
---|
2030 | |
---|
2031 | e = 0; |
---|
2032 | |
---|
2033 | cleanup: |
---|
2034 | if (nfs) |
---|
2035 | nfsDestroy(nfs); |
---|
2036 | if (nfsServer) |
---|
2037 | rpcUdpServerDestroy(nfsServer); |
---|
2038 | if (rootNode) |
---|
2039 | nfsNodeDestroy(rootNode); |
---|
2040 | if (e) |
---|
2041 | rtems_set_errno_and_return_minus_one(e); |
---|
2042 | else |
---|
2043 | return 0; |
---|
2044 | } |
---|
2045 | |
---|
2046 | /* This op is called when they try to unmount THIS fs */ |
---|
2047 | STATIC int nfs_fsunmount_me( |
---|
2048 | rtems_filesystem_mount_table_entry_t *mt_entry /* in */ |
---|
2049 | ) |
---|
2050 | { |
---|
2051 | enum clnt_stat stat; |
---|
2052 | struct sockaddr_in saddr; |
---|
2053 | char *path = mt_entry->dev; |
---|
2054 | int nodesInUse; |
---|
2055 | u_long uid,gid; |
---|
2056 | int status; |
---|
2057 | |
---|
2058 | LOCK(nfsGlob.llock); |
---|
2059 | nodesInUse = ((Nfs)mt_entry->fs_info)->nodesInUse; |
---|
2060 | |
---|
2061 | if (nodesInUse > 1 /* one ref to the root node used by us */) { |
---|
2062 | UNLOCK(nfsGlob.llock); |
---|
2063 | fprintf(stderr, |
---|
2064 | "Refuse to unmount; there are still %i nodes in use (1 used by us)\n", |
---|
2065 | nodesInUse); |
---|
2066 | rtems_set_errno_and_return_minus_one(EBUSY); |
---|
2067 | } |
---|
2068 | |
---|
2069 | status = buildIpAddr(&uid, &gid, 0, &saddr, &path); |
---|
2070 | assert( !status ); |
---|
2071 | |
---|
2072 | stat = mntcall( &saddr, |
---|
2073 | MOUNTPROC_UMNT, |
---|
2074 | (xdrproc_t)xdr_dirpath, &path, |
---|
2075 | (xdrproc_t)xdr_void, 0, |
---|
2076 | uid, |
---|
2077 | gid |
---|
2078 | ); |
---|
2079 | |
---|
2080 | if (stat) { |
---|
2081 | UNLOCK(nfsGlob.llock); |
---|
2082 | fprintf(stderr,"NFS UMOUNT -- %s\n", clnt_sperrno(stat)); |
---|
2083 | errno = EIO; |
---|
2084 | return -1; |
---|
2085 | } |
---|
2086 | |
---|
2087 | nfsNodeDestroy(mt_entry->mt_fs_root.node_access); |
---|
2088 | mt_entry->mt_fs_root.node_access = 0; |
---|
2089 | |
---|
2090 | nfsDestroy(mt_entry->fs_info); |
---|
2091 | mt_entry->fs_info = 0; |
---|
2092 | |
---|
2093 | nfsGlob.num_mounted_fs--; |
---|
2094 | UNLOCK(nfsGlob.llock); |
---|
2095 | |
---|
2096 | return 0; |
---|
2097 | } |
---|
2098 | |
---|
2099 | /* OPTIONAL; may be NULL - BUT: CAUTION; mount() doesn't check |
---|
2100 | * for this handler to be present - a fs bug |
---|
2101 | * //NOTE: (10/25/2002) patch submitted and probably applied |
---|
2102 | */ |
---|
2103 | static rtems_filesystem_node_types_t nfs_node_type( |
---|
2104 | rtems_filesystem_location_info_t *pathloc /* in */ |
---|
2105 | ) |
---|
2106 | { |
---|
2107 | NfsNode node = pathloc->node_access; |
---|
2108 | |
---|
2109 | if (updateAttr(node, 0 /* only if old */)) |
---|
2110 | return -1; |
---|
2111 | |
---|
2112 | switch( SERP_ATTR(node).type ) { |
---|
2113 | default: |
---|
2114 | /* rtems has no value for 'unknown'; |
---|
2115 | */ |
---|
2116 | case NFNON: |
---|
2117 | case NFSOCK: |
---|
2118 | case NFBAD: |
---|
2119 | case NFFIFO: |
---|
2120 | break; |
---|
2121 | |
---|
2122 | |
---|
2123 | case NFREG: return RTEMS_FILESYSTEM_MEMORY_FILE; |
---|
2124 | case NFDIR: return RTEMS_FILESYSTEM_DIRECTORY; |
---|
2125 | |
---|
2126 | case NFBLK: |
---|
2127 | case NFCHR: return RTEMS_FILESYSTEM_DEVICE; |
---|
2128 | |
---|
2129 | case NFLNK: return RTEMS_FILESYSTEM_SYM_LINK; |
---|
2130 | } |
---|
2131 | return -1; |
---|
2132 | } |
---|
2133 | |
---|
2134 | static int nfs_mknod( |
---|
2135 | const char *path, /* IN */ |
---|
2136 | mode_t mode, /* IN */ |
---|
2137 | dev_t dev, /* IN */ |
---|
2138 | rtems_filesystem_location_info_t *pathloc /* IN/OUT */ |
---|
2139 | ) |
---|
2140 | { |
---|
2141 | |
---|
2142 | struct timeval now; |
---|
2143 | diropres res; |
---|
2144 | NfsNode node = pathloc->node_access; |
---|
2145 | mode_t type = S_IFMT & mode; |
---|
2146 | |
---|
2147 | if (type != S_IFDIR && type != S_IFREG) |
---|
2148 | rtems_set_errno_and_return_minus_one(ENOTSUP); |
---|
2149 | |
---|
2150 | #if DEBUG & DEBUG_SYSCALLS |
---|
2151 | fprintf(stderr,"nfs_mknod: creating %s\n", path); |
---|
2152 | #endif |
---|
2153 | |
---|
2154 | rtems_clock_get_tod_timeval(&now); |
---|
2155 | |
---|
2156 | SERP_ARGS(node).createarg.name = (filename)path; |
---|
2157 | SERP_ARGS(node).createarg.attributes.mode = mode; |
---|
2158 | /* TODO: either use our uid or use the Nfs credentials */ |
---|
2159 | SERP_ARGS(node).createarg.attributes.uid = 0; |
---|
2160 | SERP_ARGS(node).createarg.attributes.gid = 0; |
---|
2161 | SERP_ARGS(node).createarg.attributes.size = 0; |
---|
2162 | SERP_ARGS(node).createarg.attributes.atime.seconds = now.tv_sec; |
---|
2163 | SERP_ARGS(node).createarg.attributes.atime.useconds = now.tv_usec; |
---|
2164 | SERP_ARGS(node).createarg.attributes.mtime.seconds = now.tv_sec; |
---|
2165 | SERP_ARGS(node).createarg.attributes.mtime.useconds = now.tv_usec; |
---|
2166 | |
---|
2167 | if ( nfscall( node->nfs->server, |
---|
2168 | NFSPROC_CREATE, |
---|
2169 | (xdrproc_t)xdr_createargs, &SERP_FILE(node), |
---|
2170 | (xdrproc_t)xdr_diropres, &res) |
---|
2171 | || (NFS_OK != (errno = res.status)) ) { |
---|
2172 | #if DEBUG & DEBUG_SYSCALLS |
---|
2173 | perror("nfs_mknod"); |
---|
2174 | #endif |
---|
2175 | return -1; |
---|
2176 | } |
---|
2177 | |
---|
2178 | return 0; |
---|
2179 | } |
---|
2180 | |
---|
2181 | static int nfs_utime( |
---|
2182 | rtems_filesystem_location_info_t *pathloc, /* IN */ |
---|
2183 | time_t actime, /* IN */ |
---|
2184 | time_t modtime /* IN */ |
---|
2185 | ) |
---|
2186 | { |
---|
2187 | sattr arg; |
---|
2188 | |
---|
2189 | /* TODO: add rtems EPOCH - UNIX EPOCH seconds */ |
---|
2190 | arg.atime.seconds = actime; |
---|
2191 | arg.atime.useconds = 0; |
---|
2192 | arg.mtime.seconds = modtime; |
---|
2193 | arg.mtime.useconds = 0; |
---|
2194 | |
---|
2195 | return nfs_sattr(pathloc->node_access, &arg, SATTR_ATIME | SATTR_MTIME); |
---|
2196 | } |
---|
2197 | |
---|
2198 | static int nfs_symlink( |
---|
2199 | rtems_filesystem_location_info_t *loc, /* IN */ |
---|
2200 | const char *link_name, /* IN */ |
---|
2201 | const char *node_name |
---|
2202 | ) |
---|
2203 | { |
---|
2204 | struct timeval now; |
---|
2205 | nfsstat status; |
---|
2206 | NfsNode node = loc->node_access; |
---|
2207 | |
---|
2208 | |
---|
2209 | #if DEBUG & DEBUG_SYSCALLS |
---|
2210 | fprintf(stderr,"nfs_symlink: creating %s -> %s\n", link_name, node_name); |
---|
2211 | #endif |
---|
2212 | |
---|
2213 | rtems_clock_get_tod_timeval(&now); |
---|
2214 | |
---|
2215 | SERP_ARGS(node).symlinkarg.name = (filename)link_name; |
---|
2216 | SERP_ARGS(node).symlinkarg.to = (nfspath) node_name; |
---|
2217 | |
---|
2218 | SERP_ARGS(node).symlinkarg.attributes.mode = S_IFLNK | S_IRWXU | S_IRWXG | S_IRWXO; |
---|
2219 | /* TODO */ |
---|
2220 | SERP_ARGS(node).symlinkarg.attributes.uid = 0; |
---|
2221 | SERP_ARGS(node).symlinkarg.attributes.gid = 0; |
---|
2222 | SERP_ARGS(node).symlinkarg.attributes.size = 0; |
---|
2223 | SERP_ARGS(node).symlinkarg.attributes.atime.seconds = now.tv_sec; |
---|
2224 | SERP_ARGS(node).symlinkarg.attributes.atime.useconds = now.tv_usec; |
---|
2225 | SERP_ARGS(node).symlinkarg.attributes.mtime.seconds = now.tv_sec; |
---|
2226 | SERP_ARGS(node).symlinkarg.attributes.mtime.useconds = now.tv_usec; |
---|
2227 | |
---|
2228 | if ( nfscall( node->nfs->server, |
---|
2229 | NFSPROC_SYMLINK, |
---|
2230 | (xdrproc_t)xdr_symlinkargs, &SERP_FILE(node), |
---|
2231 | (xdrproc_t)xdr_nfsstat, &status) |
---|
2232 | || (NFS_OK != (errno = status)) ) { |
---|
2233 | #if DEBUG & DEBUG_SYSCALLS |
---|
2234 | perror("nfs_symlink"); |
---|
2235 | #endif |
---|
2236 | return -1; |
---|
2237 | } |
---|
2238 | |
---|
2239 | return 0; |
---|
2240 | } |
---|
2241 | |
---|
2242 | static int nfs_do_readlink( |
---|
2243 | rtems_filesystem_location_info_t *loc, /* IN */ |
---|
2244 | strbuf *psbuf /* IN/OUT */ |
---|
2245 | ) |
---|
2246 | { |
---|
2247 | NfsNode node = loc->node_access; |
---|
2248 | Nfs nfs = node->nfs; |
---|
2249 | readlinkres_strbuf rr; |
---|
2250 | int wasAlloced; |
---|
2251 | int rval; |
---|
2252 | |
---|
2253 | rr.strbuf = *psbuf; |
---|
2254 | |
---|
2255 | wasAlloced = (0 == psbuf->buf); |
---|
2256 | |
---|
2257 | if ( (rval = nfscall(nfs->server, |
---|
2258 | NFSPROC_READLINK, |
---|
2259 | (xdrproc_t)xdr_nfs_fh, &SERP_FILE(node), |
---|
2260 | (xdrproc_t)xdr_readlinkres_strbuf, &rr)) ) { |
---|
2261 | if (wasAlloced) |
---|
2262 | xdr_free( (xdrproc_t)xdr_strbuf, (caddr_t)&rr.strbuf ); |
---|
2263 | } |
---|
2264 | |
---|
2265 | |
---|
2266 | if (NFS_OK != rr.status) { |
---|
2267 | if (wasAlloced) |
---|
2268 | xdr_free( (xdrproc_t)xdr_strbuf, (caddr_t)&rr.strbuf ); |
---|
2269 | rtems_set_errno_and_return_minus_one(rr.status); |
---|
2270 | } |
---|
2271 | |
---|
2272 | *psbuf = rr.strbuf; |
---|
2273 | |
---|
2274 | return 0; |
---|
2275 | } |
---|
2276 | |
---|
2277 | static int nfs_readlink( |
---|
2278 | rtems_filesystem_location_info_t *loc, /* IN */ |
---|
2279 | char *buf, /* OUT */ |
---|
2280 | size_t len |
---|
2281 | ) |
---|
2282 | { |
---|
2283 | strbuf sbuf; |
---|
2284 | sbuf.buf = buf; |
---|
2285 | sbuf.max = len; |
---|
2286 | |
---|
2287 | return nfs_do_readlink(loc, &sbuf); |
---|
2288 | } |
---|
2289 | |
---|
2290 | /* The semantics of this routine are: |
---|
2291 | * |
---|
2292 | * The caller submits a valid pathloc, i.e. it has |
---|
2293 | * an NfsNode attached to node_access. |
---|
2294 | * On return, pathloc points to the target node which |
---|
2295 | * may or may not be an NFS node. |
---|
2296 | * Hence, the original NFS node is released in either |
---|
2297 | * case: |
---|
2298 | * - link evaluation fails; pathloc points to no valid node |
---|
2299 | * - link evaluation success; pathloc points to a new valid |
---|
2300 | * node. If it's an NFS node, a new NfsNode will be attached |
---|
2301 | * to node_access... |
---|
2302 | */ |
---|
2303 | |
---|
2304 | #define LINKVAL_BUFLEN (MAXPATHLEN+1) |
---|
2305 | #define RVAL_ERR_BUT_DONT_FREENODE (-1) |
---|
2306 | #define RVAL_ERR_AND_DO_FREENODE ( 1) |
---|
2307 | #define RVAL_OK ( 0) |
---|
2308 | |
---|
2309 | static int nfs_eval_link( |
---|
2310 | rtems_filesystem_location_info_t *pathloc, /* IN/OUT */ |
---|
2311 | int flags /* IN */ |
---|
2312 | ) |
---|
2313 | { |
---|
2314 | rtems_filesystem_node_types_t type; |
---|
2315 | char *buf = malloc(LINKVAL_BUFLEN); |
---|
2316 | int rval = RVAL_ERR_AND_DO_FREENODE; |
---|
2317 | |
---|
2318 | if (!buf) { |
---|
2319 | errno = ENOMEM; |
---|
2320 | goto cleanup; |
---|
2321 | } |
---|
2322 | |
---|
2323 | /* in this loop, we must not use NFS specific ops as we might |
---|
2324 | * step out of our FS during the process... |
---|
2325 | * This algorithm should actually be performed by the |
---|
2326 | * generic's evaluate_path routine :-( |
---|
2327 | * |
---|
2328 | * Unfortunately, there is no way of finding the |
---|
2329 | * directory node who contains 'pathloc', however :-( |
---|
2330 | */ |
---|
2331 | do { |
---|
2332 | /* assume the generics have verified 'pathloc' to be |
---|
2333 | * a link... |
---|
2334 | */ |
---|
2335 | if ( !pathloc->ops->readlink_h ) { |
---|
2336 | errno = ENOTSUP; |
---|
2337 | goto cleanup; |
---|
2338 | } |
---|
2339 | |
---|
2340 | if ( pathloc->ops->readlink_h(pathloc, buf, LINKVAL_BUFLEN) ) { |
---|
2341 | goto cleanup; |
---|
2342 | } |
---|
2343 | |
---|
2344 | #if DEBUG & DEBUG_EVALPATH |
---|
2345 | fprintf(stderr, "link value is '%s'\n", buf); |
---|
2346 | #endif |
---|
2347 | |
---|
2348 | /* is the link value an absolute path ? */ |
---|
2349 | if ( DELIM != *buf ) { |
---|
2350 | /* NO; a relative path */ |
---|
2351 | |
---|
2352 | /* we must backup to the link's directory - we |
---|
2353 | * know only how to do that for NFS, however. |
---|
2354 | * In this special case, we can avoid recursion. |
---|
2355 | * Otherwise (i.e. if the link is on another FS), |
---|
2356 | * we must step into its eval_link_h(). |
---|
2357 | */ |
---|
2358 | if (locIsNfs(pathloc)) { |
---|
2359 | NfsNode node = pathloc->node_access; |
---|
2360 | int err; |
---|
2361 | |
---|
2362 | memcpy( &SERP_FILE(node), |
---|
2363 | &node->args.dir, |
---|
2364 | sizeof(node->args.dir) ); |
---|
2365 | |
---|
2366 | if (updateAttr(node, 1 /* force */)) |
---|
2367 | goto cleanup; |
---|
2368 | |
---|
2369 | if (SERP_ATTR(node).type != NFDIR) { |
---|
2370 | errno = ENOTDIR; |
---|
2371 | goto cleanup; |
---|
2372 | } |
---|
2373 | |
---|
2374 | pathloc->handlers = &nfs_dir_file_handlers; |
---|
2375 | |
---|
2376 | err = nfs_evalpath(buf, strlen(buf), flags, pathloc); |
---|
2377 | |
---|
2378 | /* according to its semantics, |
---|
2379 | * nfs_evalpath cloned the node attached |
---|
2380 | * to pathloc. Hence we have to |
---|
2381 | * release the old one (referring to |
---|
2382 | * the link; the new clone has been |
---|
2383 | * updated and refers to the link _value_). |
---|
2384 | */ |
---|
2385 | nfsNodeDestroy(node); |
---|
2386 | |
---|
2387 | if (err) { |
---|
2388 | /* nfs_evalpath has set errno; |
---|
2389 | * pathloc->node_access has no |
---|
2390 | * valid node attached in this case |
---|
2391 | */ |
---|
2392 | rval = RVAL_ERR_BUT_DONT_FREENODE; |
---|
2393 | goto cleanup; |
---|
2394 | } |
---|
2395 | |
---|
2396 | } else { |
---|
2397 | if ( ! pathloc->ops->eval_link_h ) { |
---|
2398 | errno = ENOTSUP; |
---|
2399 | goto cleanup; |
---|
2400 | } |
---|
2401 | if (!pathloc->ops->eval_link_h(pathloc, flags)) { |
---|
2402 | /* FS is responsible for freeing its pathloc->node_access |
---|
2403 | * if necessary |
---|
2404 | */ |
---|
2405 | rval = RVAL_ERR_BUT_DONT_FREENODE; |
---|
2406 | goto cleanup; |
---|
2407 | } |
---|
2408 | } |
---|
2409 | } else { |
---|
2410 | /* link points to an absolute path '/xxx' */ |
---|
2411 | |
---|
2412 | /* release this node; filesystem_evaluate_path() will |
---|
2413 | * lookup a new one. |
---|
2414 | */ |
---|
2415 | rtems_filesystem_freenode(pathloc); |
---|
2416 | |
---|
2417 | if (rtems_filesystem_evaluate_path(buf, strlen(buf), flags, pathloc, 1)) { |
---|
2418 | /* If evalpath fails then there is no valid node |
---|
2419 | * attached to pathloc; hence we must not attempt |
---|
2420 | * to free the node |
---|
2421 | */ |
---|
2422 | rval = RVAL_ERR_BUT_DONT_FREENODE; |
---|
2423 | goto cleanup; |
---|
2424 | } |
---|
2425 | } |
---|
2426 | |
---|
2427 | if ( !pathloc->ops->node_type_h ) { |
---|
2428 | errno = ENOTSUP; |
---|
2429 | goto cleanup; |
---|
2430 | } |
---|
2431 | |
---|
2432 | type = pathloc->ops->node_type_h(pathloc); |
---|
2433 | |
---|
2434 | |
---|
2435 | /* I dont know what to do about hard links */ |
---|
2436 | } while ( RTEMS_FILESYSTEM_SYM_LINK == type ); |
---|
2437 | |
---|
2438 | rval = RVAL_OK; |
---|
2439 | |
---|
2440 | cleanup: |
---|
2441 | |
---|
2442 | free(buf); |
---|
2443 | |
---|
2444 | if (RVAL_ERR_AND_DO_FREENODE == rval) { |
---|
2445 | rtems_filesystem_freenode(pathloc); |
---|
2446 | return -1; |
---|
2447 | } |
---|
2448 | |
---|
2449 | return rval; |
---|
2450 | } |
---|
2451 | |
---|
2452 | |
---|
2453 | struct _rtems_filesystem_operations_table nfs_fs_ops = { |
---|
2454 | nfs_evalpath, /* MANDATORY */ |
---|
2455 | nfs_evalformake, /* MANDATORY; may set errno=ENOSYS and return -1 */ |
---|
2456 | nfs_link, /* OPTIONAL; may be NULL */ |
---|
2457 | nfs_unlink, /* OPTIONAL; may be NULL */ |
---|
2458 | nfs_node_type, /* OPTIONAL; may be NULL; BUG in mount - no test!! */ |
---|
2459 | nfs_mknod, /* OPTIONAL; may be NULL */ |
---|
2460 | nfs_chown, /* OPTIONAL; may be NULL */ |
---|
2461 | nfs_freenode, /* OPTIONAL; may be NULL; (release node_access) */ |
---|
2462 | nfs_mount, /* OPTIONAL; may be NULL */ |
---|
2463 | nfs_fsmount_me, /* OPTIONAL; may be NULL -- but this makes NO SENSE */ |
---|
2464 | nfs_unmount, /* OPTIONAL; may be NULL */ |
---|
2465 | nfs_fsunmount_me, /* OPTIONAL; may be NULL */ |
---|
2466 | nfs_utime, /* OPTIONAL; may be NULL */ |
---|
2467 | nfs_eval_link, /* OPTIONAL; may be NULL */ |
---|
2468 | nfs_symlink, /* OPTIONAL; may be NULL */ |
---|
2469 | nfs_readlink, /* OPTIONAL; may be NULL */ |
---|
2470 | NULL /* OPTIONAL; may be NULL */ |
---|
2471 | }; |
---|
2472 | |
---|
2473 | /***************************************** |
---|
2474 | File Handlers |
---|
2475 | |
---|
2476 | NOTE: the FS generics expect a FS' |
---|
2477 | evalpath_h() to switch the |
---|
2478 | pathloc->handlers according |
---|
2479 | to the pathloc/node's file |
---|
2480 | type. |
---|
2481 | We currently have 'file' and |
---|
2482 | 'directory' handlers and very |
---|
2483 | few 'symlink' handlers. |
---|
2484 | |
---|
2485 | The handlers for each type are |
---|
2486 | implemented or #defined ZERO |
---|
2487 | in a 'nfs_file_xxx', |
---|
2488 | 'nfs_dir_xxx', 'nfs_link_xxx' |
---|
2489 | sequence below this point. |
---|
2490 | |
---|
2491 | In some cases, a common handler, |
---|
2492 | can be used for all file types. |
---|
2493 | It is then simply called |
---|
2494 | 'nfs_xxx'. |
---|
2495 | *****************************************/ |
---|
2496 | |
---|
2497 | |
---|
2498 | #if 0 |
---|
2499 | /* from rtems/libio.h for convenience */ |
---|
2500 | struct rtems_libio_tt { |
---|
2501 | rtems_driver_name_t *driver; |
---|
2502 | off_t size; /* size of file */ |
---|
2503 | off_t offset; /* current offset into file */ |
---|
2504 | uint32_t flags; |
---|
2505 | rtems_filesystem_location_info_t pathinfo; |
---|
2506 | Objects_Id sem; |
---|
2507 | uint32_t data0; /* private to "driver" */ |
---|
2508 | void *data1; /* ... */ |
---|
2509 | void *file_info; /* used by file handlers */ |
---|
2510 | rtems_filesystem_file_handlers_r *handlers; /* type specific handlers */ |
---|
2511 | }; |
---|
2512 | #endif |
---|
2513 | |
---|
2514 | /* stateless NFS protocol makes this trivial */ |
---|
2515 | static int nfs_file_open( |
---|
2516 | rtems_libio_t *iop, |
---|
2517 | const char *pathname, |
---|
2518 | uint32_t flag, |
---|
2519 | uint32_t mode |
---|
2520 | ) |
---|
2521 | { |
---|
2522 | iop->file_info = 0; |
---|
2523 | return 0; |
---|
2524 | } |
---|
2525 | |
---|
2526 | /* reading directories is not stateless; we must |
---|
2527 | * remember the last 'read' position, i.e. |
---|
2528 | * the server 'cookie'. We do manage this information |
---|
2529 | * attached to the iop->file_info. |
---|
2530 | */ |
---|
2531 | static int nfs_dir_open( |
---|
2532 | rtems_libio_t *iop, |
---|
2533 | const char *pathname, |
---|
2534 | uint32_t flag, |
---|
2535 | uint32_t mode |
---|
2536 | ) |
---|
2537 | { |
---|
2538 | NfsNode node = iop->pathinfo.node_access; |
---|
2539 | DirInfo di; |
---|
2540 | |
---|
2541 | /* create a readdirargs object and copy the file handle; |
---|
2542 | * attach to the file_info. |
---|
2543 | */ |
---|
2544 | |
---|
2545 | di = (DirInfo) malloc(sizeof(*di)); |
---|
2546 | iop->file_info = di; |
---|
2547 | |
---|
2548 | if ( !di ) { |
---|
2549 | errno = ENOMEM; |
---|
2550 | return -1; |
---|
2551 | } |
---|
2552 | |
---|
2553 | memcpy( &di->readdirargs.dir, |
---|
2554 | &SERP_FILE(node), |
---|
2555 | sizeof(di->readdirargs.dir) ); |
---|
2556 | |
---|
2557 | /* rewind cookie */ |
---|
2558 | memset( &di->readdirargs.cookie, |
---|
2559 | 0, |
---|
2560 | sizeof(di->readdirargs.cookie) ); |
---|
2561 | |
---|
2562 | di->eofreached = FALSE; |
---|
2563 | |
---|
2564 | return 0; |
---|
2565 | } |
---|
2566 | |
---|
2567 | #define nfs_link_open 0 |
---|
2568 | |
---|
2569 | static int nfs_file_close( |
---|
2570 | rtems_libio_t *iop |
---|
2571 | ) |
---|
2572 | { |
---|
2573 | return 0; |
---|
2574 | } |
---|
2575 | |
---|
2576 | static int nfs_dir_close( |
---|
2577 | rtems_libio_t *iop |
---|
2578 | ) |
---|
2579 | { |
---|
2580 | free(iop->file_info); |
---|
2581 | iop->file_info = 0; |
---|
2582 | return 0; |
---|
2583 | } |
---|
2584 | |
---|
2585 | #define nfs_link_close 0 |
---|
2586 | |
---|
2587 | static ssize_t nfs_file_read( |
---|
2588 | rtems_libio_t *iop, |
---|
2589 | void *buffer, |
---|
2590 | size_t count |
---|
2591 | ) |
---|
2592 | { |
---|
2593 | readres rr; |
---|
2594 | NfsNode node = iop->pathinfo.node_access; |
---|
2595 | Nfs nfs = node->nfs; |
---|
2596 | |
---|
2597 | if (count > NFS_MAXDATA) |
---|
2598 | count = NFS_MAXDATA; |
---|
2599 | |
---|
2600 | SERP_ARGS(node).readarg.offset = iop->offset; |
---|
2601 | SERP_ARGS(node).readarg.count = count; |
---|
2602 | SERP_ARGS(node).readarg.totalcount = UINT32_C(0xdeadbeef); |
---|
2603 | |
---|
2604 | rr.readres_u.reply.data.data_val = buffer; |
---|
2605 | |
---|
2606 | if ( nfscall( nfs->server, |
---|
2607 | NFSPROC_READ, |
---|
2608 | (xdrproc_t)xdr_readargs, &SERP_FILE(node), |
---|
2609 | (xdrproc_t)xdr_readres, &rr) ) { |
---|
2610 | return -1; |
---|
2611 | } |
---|
2612 | |
---|
2613 | |
---|
2614 | if (NFS_OK != rr.status) { |
---|
2615 | rtems_set_errno_and_return_minus_one(rr.status); |
---|
2616 | } |
---|
2617 | |
---|
2618 | #if DEBUG & DEBUG_SYSCALLS |
---|
2619 | fprintf(stderr, |
---|
2620 | "Read %i (asked for %i) bytes from offset %i to 0x%08x\n", |
---|
2621 | rr.readres_u.reply.data.data_len, |
---|
2622 | count, |
---|
2623 | iop->offset, |
---|
2624 | rr.readres_u.reply.data.data_val); |
---|
2625 | #endif |
---|
2626 | |
---|
2627 | |
---|
2628 | return rr.readres_u.reply.data.data_len; |
---|
2629 | } |
---|
2630 | |
---|
2631 | /* this is called by readdir() / getdents() */ |
---|
2632 | static ssize_t nfs_dir_read( |
---|
2633 | rtems_libio_t *iop, |
---|
2634 | void *buffer, |
---|
2635 | size_t count |
---|
2636 | ) |
---|
2637 | { |
---|
2638 | DirInfo di = iop->file_info; |
---|
2639 | RpcUdpServer server = ((Nfs)iop->pathinfo.mt_entry->fs_info)->server; |
---|
2640 | |
---|
2641 | if ( di->eofreached ) |
---|
2642 | return 0; |
---|
2643 | |
---|
2644 | di->ptr = di->buf = buffer; |
---|
2645 | |
---|
2646 | /* align + round down the buffer */ |
---|
2647 | count &= ~ (DIRENT_HEADER_SIZE - 1); |
---|
2648 | di->len = count; |
---|
2649 | |
---|
2650 | #if 0 |
---|
2651 | /* now estimate the number of entries we should ask for */ |
---|
2652 | count /= DIRENT_HEADER_SIZE + CONFIG_AVG_NAMLEN; |
---|
2653 | |
---|
2654 | /* estimate the encoded size that might take up */ |
---|
2655 | count *= dirres_entry_size + CONFIG_AVG_NAMLEN; |
---|
2656 | #else |
---|
2657 | /* integer arithmetics are better done the other way round */ |
---|
2658 | count *= dirres_entry_size + CONFIG_AVG_NAMLEN; |
---|
2659 | count /= DIRENT_HEADER_SIZE + CONFIG_AVG_NAMLEN; |
---|
2660 | #endif |
---|
2661 | |
---|
2662 | if (count > NFS_MAXDATA) |
---|
2663 | count = NFS_MAXDATA; |
---|
2664 | |
---|
2665 | di->readdirargs.count = count; |
---|
2666 | |
---|
2667 | #if DEBUG & DEBUG_READDIR |
---|
2668 | fprintf(stderr, |
---|
2669 | "Readdir: asking for %i XDR bytes, buffer is %i\n", |
---|
2670 | count, di->len); |
---|
2671 | #endif |
---|
2672 | |
---|
2673 | if ( nfscall( |
---|
2674 | server, |
---|
2675 | NFSPROC_READDIR, |
---|
2676 | (xdrproc_t)xdr_readdirargs, &di->readdirargs, |
---|
2677 | (xdrproc_t)xdr_dir_info, di) ) { |
---|
2678 | return -1; |
---|
2679 | } |
---|
2680 | |
---|
2681 | |
---|
2682 | if (NFS_OK != di->status) { |
---|
2683 | rtems_set_errno_and_return_minus_one(di->status); |
---|
2684 | } |
---|
2685 | |
---|
2686 | return (char*)di->ptr - (char*)buffer; |
---|
2687 | } |
---|
2688 | |
---|
2689 | #define nfs_link_read 0 |
---|
2690 | |
---|
2691 | static ssize_t nfs_file_write( |
---|
2692 | rtems_libio_t *iop, |
---|
2693 | const void *buffer, |
---|
2694 | size_t count |
---|
2695 | ) |
---|
2696 | { |
---|
2697 | NfsNode node = iop->pathinfo.node_access; |
---|
2698 | Nfs nfs = node->nfs; |
---|
2699 | int e; |
---|
2700 | |
---|
2701 | if (count > NFS_MAXDATA) |
---|
2702 | count = NFS_MAXDATA; |
---|
2703 | |
---|
2704 | |
---|
2705 | SERP_ARGS(node).writearg.beginoffset = UINT32_C(0xdeadbeef); |
---|
2706 | if ( LIBIO_FLAGS_APPEND & iop->flags ) { |
---|
2707 | if ( updateAttr(node, 0) ) { |
---|
2708 | return -1; |
---|
2709 | } |
---|
2710 | SERP_ARGS(node).writearg.offset = SERP_ATTR(node).size; |
---|
2711 | } else { |
---|
2712 | SERP_ARGS(node).writearg.offset = iop->offset; |
---|
2713 | } |
---|
2714 | SERP_ARGS(node).writearg.totalcount = UINT32_C(0xdeadbeef); |
---|
2715 | SERP_ARGS(node).writearg.data.data_len = count; |
---|
2716 | SERP_ARGS(node).writearg.data.data_val = (void*)buffer; |
---|
2717 | |
---|
2718 | /* write XDR buffer size will be chosen by nfscall based |
---|
2719 | * on the PROC specifier |
---|
2720 | */ |
---|
2721 | |
---|
2722 | if ( nfscall( nfs->server, |
---|
2723 | NFSPROC_WRITE, |
---|
2724 | (xdrproc_t)xdr_writeargs, &SERP_FILE(node), |
---|
2725 | (xdrproc_t)xdr_attrstat, &node->serporid) ) { |
---|
2726 | return -1; |
---|
2727 | } |
---|
2728 | |
---|
2729 | |
---|
2730 | if (NFS_OK != (e=node->serporid.status) ) { |
---|
2731 | /* try at least to recover the current attributes */ |
---|
2732 | updateAttr(node, 1 /* force */); |
---|
2733 | rtems_set_errno_and_return_minus_one(e); |
---|
2734 | } |
---|
2735 | |
---|
2736 | node->age = nowSeconds(); |
---|
2737 | |
---|
2738 | return count; |
---|
2739 | } |
---|
2740 | |
---|
2741 | #define nfs_dir_write 0 |
---|
2742 | #define nfs_link_write 0 |
---|
2743 | |
---|
2744 | /* IOCTL is unneeded/unsupported */ |
---|
2745 | #ifdef DECLARE_BODY |
---|
2746 | static int nfs_file_ioctl( |
---|
2747 | rtems_libio_t *iop, |
---|
2748 | uint32_t command, |
---|
2749 | void *buffer |
---|
2750 | )DECLARE_BODY |
---|
2751 | #else |
---|
2752 | #define nfs_file_ioctl 0 |
---|
2753 | #define nfs_dir_ioctl 0 |
---|
2754 | #define nfs_link_ioctl 0 |
---|
2755 | #endif |
---|
2756 | |
---|
2757 | static rtems_off64_t nfs_file_lseek( |
---|
2758 | rtems_libio_t *iop, |
---|
2759 | rtems_off64_t length, |
---|
2760 | int whence |
---|
2761 | ) |
---|
2762 | { |
---|
2763 | #if DEBUG & DEBUG_SYSCALLS |
---|
2764 | fprintf(stderr, |
---|
2765 | "lseek to %i (length %i, whence %i)\n", |
---|
2766 | iop->offset, |
---|
2767 | length, |
---|
2768 | whence); |
---|
2769 | #endif |
---|
2770 | if ( SEEK_END == whence ) { |
---|
2771 | /* rtems (4.6.2) libcsupport code 'lseek' uses iop->size to |
---|
2772 | * compute the offset. We don't want to track the file size |
---|
2773 | * by updating 'iop->size' constantly. |
---|
2774 | * Since lseek is the only place using iop->size, we work |
---|
2775 | * around this by tweaking the offset here... |
---|
2776 | */ |
---|
2777 | NfsNode node = iop->pathinfo.node_access; |
---|
2778 | fattr *fa = &SERP_ATTR(node); |
---|
2779 | |
---|
2780 | if (updateAttr(node, 0 /* only if old */)) { |
---|
2781 | return -1; |
---|
2782 | } |
---|
2783 | iop->offset = fa->size; |
---|
2784 | } |
---|
2785 | |
---|
2786 | /* this is particularly easy :-) */ |
---|
2787 | return iop->offset; |
---|
2788 | } |
---|
2789 | |
---|
2790 | static rtems_off64_t nfs_dir_lseek( |
---|
2791 | rtems_libio_t *iop, |
---|
2792 | rtems_off64_t length, |
---|
2793 | int whence |
---|
2794 | ) |
---|
2795 | { |
---|
2796 | DirInfo di = iop->file_info; |
---|
2797 | |
---|
2798 | /* we don't support anything other than |
---|
2799 | * rewinding |
---|
2800 | */ |
---|
2801 | if (SEEK_SET != whence || 0 != length) { |
---|
2802 | errno = ENOTSUP; |
---|
2803 | return -1; |
---|
2804 | } |
---|
2805 | |
---|
2806 | /* rewind cookie */ |
---|
2807 | memset( &di->readdirargs.cookie, |
---|
2808 | 0, |
---|
2809 | sizeof(di->readdirargs.cookie) ); |
---|
2810 | |
---|
2811 | di->eofreached = FALSE; |
---|
2812 | |
---|
2813 | return iop->offset; |
---|
2814 | } |
---|
2815 | |
---|
2816 | #define nfs_link_lseek 0 |
---|
2817 | |
---|
2818 | #if 0 /* structure types for reference */ |
---|
2819 | struct fattr { |
---|
2820 | ftype type; |
---|
2821 | u_int mode; |
---|
2822 | u_int nlink; |
---|
2823 | u_int uid; |
---|
2824 | u_int gid; |
---|
2825 | u_int size; |
---|
2826 | u_int blocksize; |
---|
2827 | u_int rdev; |
---|
2828 | u_int blocks; |
---|
2829 | u_int fsid; |
---|
2830 | u_int fileid; |
---|
2831 | nfstime atime; |
---|
2832 | nfstime mtime; |
---|
2833 | nfstime ctime; |
---|
2834 | }; |
---|
2835 | |
---|
2836 | struct stat |
---|
2837 | { |
---|
2838 | dev_t st_dev; |
---|
2839 | ino_t st_ino; |
---|
2840 | mode_t st_mode; |
---|
2841 | nlink_t st_nlink; |
---|
2842 | uid_t st_uid; |
---|
2843 | gid_t st_gid; |
---|
2844 | dev_t st_rdev; |
---|
2845 | rtems_off64_t st_size; |
---|
2846 | /* SysV/sco doesn't have the rest... But Solaris, eabi does. */ |
---|
2847 | #if defined(__svr4__) && !defined(__PPC__) && !defined(__sun__) |
---|
2848 | time_t st_atime; |
---|
2849 | time_t st_mtime; |
---|
2850 | time_t st_ctime; |
---|
2851 | #else |
---|
2852 | time_t st_atime; |
---|
2853 | long st_spare1; |
---|
2854 | time_t st_mtime; |
---|
2855 | long st_spare2; |
---|
2856 | time_t st_ctime; |
---|
2857 | long st_spare3; |
---|
2858 | long st_blksize; |
---|
2859 | long st_blocks; |
---|
2860 | long st_spare4[2]; |
---|
2861 | #endif |
---|
2862 | }; |
---|
2863 | #endif |
---|
2864 | |
---|
2865 | /* common for file/dir/link */ |
---|
2866 | static int nfs_fstat( |
---|
2867 | rtems_filesystem_location_info_t *loc, |
---|
2868 | struct stat *buf |
---|
2869 | ) |
---|
2870 | { |
---|
2871 | NfsNode node = loc->node_access; |
---|
2872 | fattr *fa = &SERP_ATTR(node); |
---|
2873 | |
---|
2874 | if (updateAttr(node, 0 /* only if old */)) { |
---|
2875 | return -1; |
---|
2876 | } |
---|
2877 | |
---|
2878 | /* done by caller |
---|
2879 | memset(buf, 0, sizeof(*buf)); |
---|
2880 | */ |
---|
2881 | |
---|
2882 | /* translate */ |
---|
2883 | |
---|
2884 | /* one of the branches hopefully is optimized away */ |
---|
2885 | if (sizeof(ino_t) < sizeof(u_int)) { |
---|
2886 | buf->st_dev = NFS_MAKE_DEV_T_INO_HACK((NfsNode)loc->node_access); |
---|
2887 | } else { |
---|
2888 | buf->st_dev = NFS_MAKE_DEV_T((NfsNode)loc->node_access); |
---|
2889 | } |
---|
2890 | buf->st_mode = fa->mode; |
---|
2891 | buf->st_nlink = fa->nlink; |
---|
2892 | buf->st_uid = fa->uid; |
---|
2893 | buf->st_gid = fa->gid; |
---|
2894 | buf->st_size = fa->size; |
---|
2895 | /* Set to "preferred size" of this NFS client implementation */ |
---|
2896 | buf->st_blksize = nfsStBlksize ? nfsStBlksize : fa->blocksize; |
---|
2897 | buf->st_rdev = fa->rdev; |
---|
2898 | buf->st_blocks = fa->blocks; |
---|
2899 | buf->st_ino = fa->fileid; |
---|
2900 | buf->st_atime = fa->atime.seconds; |
---|
2901 | buf->st_mtime = fa->mtime.seconds; |
---|
2902 | buf->st_ctime = fa->ctime.seconds; |
---|
2903 | |
---|
2904 | #if 0 /* NFS should return the modes */ |
---|
2905 | switch(fa->type) { |
---|
2906 | default: |
---|
2907 | case NFNON: |
---|
2908 | case NFBAD: |
---|
2909 | break; |
---|
2910 | |
---|
2911 | case NFSOCK: buf->st_mode |= S_IFSOCK; break; |
---|
2912 | case NFFIFO: buf->st_mode |= S_IFIFO; break; |
---|
2913 | case NFREG : buf->st_mode |= S_IFREG; break; |
---|
2914 | case NFDIR : buf->st_mode |= S_IFDIR; break; |
---|
2915 | case NFBLK : buf->st_mode |= S_IFBLK; break; |
---|
2916 | case NFCHR : buf->st_mode |= S_IFCHR; break; |
---|
2917 | case NFLNK : buf->st_mode |= S_IFLNK; break; |
---|
2918 | } |
---|
2919 | #endif |
---|
2920 | |
---|
2921 | return 0; |
---|
2922 | } |
---|
2923 | |
---|
2924 | /* a helper which does the real work for |
---|
2925 | * a couple of handlers (such as chmod, |
---|
2926 | * ftruncate or utime) |
---|
2927 | */ |
---|
2928 | static int |
---|
2929 | nfs_sattr(NfsNode node, sattr *arg, u_long mask) |
---|
2930 | { |
---|
2931 | |
---|
2932 | struct timeval now; |
---|
2933 | nfstime nfsnow, t; |
---|
2934 | int e; |
---|
2935 | u_int mode; |
---|
2936 | |
---|
2937 | if (updateAttr(node, 0 /* only if old */)) |
---|
2938 | return -1; |
---|
2939 | |
---|
2940 | rtems_clock_get_tod_timeval(&now); |
---|
2941 | |
---|
2942 | /* TODO: add rtems EPOCH - UNIX EPOCH seconds */ |
---|
2943 | nfsnow.seconds = now.tv_sec; |
---|
2944 | nfsnow.useconds = now.tv_usec; |
---|
2945 | |
---|
2946 | /* merge permission bits into existing type bits */ |
---|
2947 | mode = SERP_ATTR(node).mode; |
---|
2948 | if (mask & SATTR_MODE) { |
---|
2949 | mode &= S_IFMT; |
---|
2950 | mode |= arg->mode & ~S_IFMT; |
---|
2951 | } else { |
---|
2952 | mode = -1; |
---|
2953 | } |
---|
2954 | SERP_ARGS(node).sattrarg.attributes.mode = mode; |
---|
2955 | |
---|
2956 | SERP_ARGS(node).sattrarg.attributes.uid = |
---|
2957 | (mask & SATTR_UID) ? arg->uid : -1; |
---|
2958 | |
---|
2959 | SERP_ARGS(node).sattrarg.attributes.gid = |
---|
2960 | (mask & SATTR_GID) ? arg->gid : -1; |
---|
2961 | |
---|
2962 | SERP_ARGS(node).sattrarg.attributes.size = |
---|
2963 | (mask & SATTR_SIZE) ? arg->size : -1; |
---|
2964 | |
---|
2965 | if (mask & SATTR_ATIME) |
---|
2966 | t = arg->atime; |
---|
2967 | else if (mask & SATTR_TOUCHA) |
---|
2968 | t = nfsnow; |
---|
2969 | else |
---|
2970 | t.seconds = t.useconds = -1; |
---|
2971 | SERP_ARGS(node).sattrarg.attributes.atime = t; |
---|
2972 | |
---|
2973 | if (mask & SATTR_ATIME) |
---|
2974 | t = arg->mtime; |
---|
2975 | else if (mask & SATTR_TOUCHA) |
---|
2976 | t = nfsnow; |
---|
2977 | else |
---|
2978 | t.seconds = t.useconds = -1; |
---|
2979 | SERP_ARGS(node).sattrarg.attributes.mtime = t; |
---|
2980 | |
---|
2981 | node->serporid.status = NFS_OK; |
---|
2982 | |
---|
2983 | if ( nfscall( node->nfs->server, |
---|
2984 | NFSPROC_SETATTR, |
---|
2985 | (xdrproc_t)xdr_sattrargs, &SERP_FILE(node), |
---|
2986 | (xdrproc_t)xdr_attrstat, &node->serporid) ) { |
---|
2987 | #if DEBUG & DEBUG_SYSCALLS |
---|
2988 | fprintf(stderr, |
---|
2989 | "nfs_sattr (mask 0x%08x): %s", |
---|
2990 | mask, |
---|
2991 | strerror(errno)); |
---|
2992 | #endif |
---|
2993 | return -1; |
---|
2994 | } |
---|
2995 | |
---|
2996 | if (NFS_OK != (e=node->serporid.status) ) { |
---|
2997 | #if DEBUG & DEBUG_SYSCALLS |
---|
2998 | fprintf(stderr,"nfs_sattr: %s\n",strerror(e)); |
---|
2999 | #endif |
---|
3000 | /* try at least to recover the current attributes */ |
---|
3001 | updateAttr(node, 1 /* force */); |
---|
3002 | rtems_set_errno_and_return_minus_one(e); |
---|
3003 | } |
---|
3004 | |
---|
3005 | node->age = nowSeconds(); |
---|
3006 | |
---|
3007 | return 0; |
---|
3008 | } |
---|
3009 | |
---|
3010 | |
---|
3011 | /* common for file/dir/link */ |
---|
3012 | static int nfs_fchmod( |
---|
3013 | rtems_filesystem_location_info_t *loc, |
---|
3014 | mode_t mode |
---|
3015 | ) |
---|
3016 | { |
---|
3017 | sattr arg; |
---|
3018 | |
---|
3019 | arg.mode = mode; |
---|
3020 | return nfs_sattr(loc->node_access, &arg, SATTR_MODE); |
---|
3021 | |
---|
3022 | } |
---|
3023 | |
---|
3024 | /* just set the size attribute to 'length' |
---|
3025 | * the server will take care of the rest :-) |
---|
3026 | */ |
---|
3027 | static int nfs_file_ftruncate( |
---|
3028 | rtems_libio_t *iop, |
---|
3029 | rtems_off64_t length |
---|
3030 | ) |
---|
3031 | { |
---|
3032 | sattr arg; |
---|
3033 | |
---|
3034 | arg.size = length; |
---|
3035 | /* must not modify any other attribute; if we are not the owner |
---|
3036 | * of the file or directory but only have write access changing |
---|
3037 | * any attribute besides 'size' will fail... |
---|
3038 | */ |
---|
3039 | return nfs_sattr(iop->pathinfo.node_access, |
---|
3040 | &arg, |
---|
3041 | SATTR_SIZE); |
---|
3042 | } |
---|
3043 | |
---|
3044 | #define nfs_dir_ftruncate 0 |
---|
3045 | #define nfs_link_ftruncate 0 |
---|
3046 | |
---|
3047 | /* not implemented */ |
---|
3048 | #ifdef DECLARE_BODY |
---|
3049 | static int nfs_file_fpathconf( |
---|
3050 | rtems_libio_t *iop, |
---|
3051 | int name |
---|
3052 | )DECLARE_BODY |
---|
3053 | #else |
---|
3054 | #define nfs_file_fpathconf 0 |
---|
3055 | #define nfs_dir_fpathconf 0 |
---|
3056 | #define nfs_link_fpathconf 0 |
---|
3057 | #endif |
---|
3058 | |
---|
3059 | /* unused */ |
---|
3060 | #ifdef DECLARE_BODY |
---|
3061 | static int nfs_file_fsync( |
---|
3062 | rtems_libio_t *iop |
---|
3063 | )DECLARE_BODY |
---|
3064 | #else |
---|
3065 | #define nfs_file_fsync 0 |
---|
3066 | #define nfs_dir_fsync 0 |
---|
3067 | #define nfs_link_fsync 0 |
---|
3068 | #endif |
---|
3069 | |
---|
3070 | /* unused */ |
---|
3071 | #ifdef DECLARE_BODY |
---|
3072 | static int nfs_file_fdatasync( |
---|
3073 | rtems_libio_t *iop |
---|
3074 | )DECLARE_BODY |
---|
3075 | #else |
---|
3076 | #define nfs_file_fdatasync 0 |
---|
3077 | #define nfs_dir_fdatasync 0 |
---|
3078 | #define nfs_link_fdatasync 0 |
---|
3079 | #endif |
---|
3080 | |
---|
3081 | /* unused */ |
---|
3082 | #ifdef DECLARE_BODY |
---|
3083 | static int nfs_file_fcntl( |
---|
3084 | int cmd, |
---|
3085 | rtems_libio_t *iop |
---|
3086 | )DECLARE_BODY |
---|
3087 | #else |
---|
3088 | #define nfs_file_fcntl 0 |
---|
3089 | #define nfs_dir_fcntl 0 |
---|
3090 | #define nfs_link_fcntl 0 |
---|
3091 | #endif |
---|
3092 | |
---|
3093 | /* files and symlinks are removed |
---|
3094 | * by the common nfs_unlink() routine. |
---|
3095 | * NFS has a different NFSPROC_RMDIR |
---|
3096 | * call, though... |
---|
3097 | */ |
---|
3098 | static int nfs_dir_rmnod( |
---|
3099 | rtems_filesystem_location_info_t *parentpathloc, /* IN */ |
---|
3100 | rtems_filesystem_location_info_t *pathloc /* IN */ |
---|
3101 | ) |
---|
3102 | { |
---|
3103 | return nfs_do_unlink(parentpathloc, pathloc, NFSPROC_RMDIR); |
---|
3104 | } |
---|
3105 | |
---|
3106 | /* the file handlers table */ |
---|
3107 | static |
---|
3108 | struct _rtems_filesystem_file_handlers_r nfs_file_file_handlers = { |
---|
3109 | nfs_file_open, /* OPTIONAL; may be NULL */ |
---|
3110 | nfs_file_close, /* OPTIONAL; may be NULL */ |
---|
3111 | nfs_file_read, /* OPTIONAL; may be NULL */ |
---|
3112 | nfs_file_write, /* OPTIONAL; may be NULL */ |
---|
3113 | nfs_file_ioctl, /* OPTIONAL; may be NULL */ |
---|
3114 | nfs_file_lseek, /* OPTIONAL; may be NULL */ |
---|
3115 | nfs_fstat, /* OPTIONAL; may be NULL */ |
---|
3116 | nfs_fchmod, /* OPTIONAL; may be NULL */ |
---|
3117 | nfs_file_ftruncate, /* OPTIONAL; may be NULL */ |
---|
3118 | nfs_file_fpathconf, /* OPTIONAL; may be NULL - UNUSED */ |
---|
3119 | nfs_file_fsync, /* OPTIONAL; may be NULL */ |
---|
3120 | nfs_file_fdatasync, /* OPTIONAL; may be NULL */ |
---|
3121 | nfs_file_fcntl, /* OPTIONAL; may be NULL */ |
---|
3122 | nfs_unlink, /* OPTIONAL; may be NULL */ |
---|
3123 | }; |
---|
3124 | |
---|
3125 | /* the directory handlers table */ |
---|
3126 | static |
---|
3127 | struct _rtems_filesystem_file_handlers_r nfs_dir_file_handlers = { |
---|
3128 | nfs_dir_open, /* OPTIONAL; may be NULL */ |
---|
3129 | nfs_dir_close, /* OPTIONAL; may be NULL */ |
---|
3130 | nfs_dir_read, /* OPTIONAL; may be NULL */ |
---|
3131 | nfs_dir_write, /* OPTIONAL; may be NULL */ |
---|
3132 | nfs_dir_ioctl, /* OPTIONAL; may be NULL */ |
---|
3133 | nfs_dir_lseek, /* OPTIONAL; may be NULL */ |
---|
3134 | nfs_fstat, /* OPTIONAL; may be NULL */ |
---|
3135 | nfs_fchmod, /* OPTIONAL; may be NULL */ |
---|
3136 | nfs_dir_ftruncate, /* OPTIONAL; may be NULL */ |
---|
3137 | nfs_dir_fpathconf, /* OPTIONAL; may be NULL - UNUSED */ |
---|
3138 | nfs_dir_fsync, /* OPTIONAL; may be NULL */ |
---|
3139 | nfs_dir_fdatasync, /* OPTIONAL; may be NULL */ |
---|
3140 | nfs_dir_fcntl, /* OPTIONAL; may be NULL */ |
---|
3141 | nfs_dir_rmnod, /* OPTIONAL; may be NULL */ |
---|
3142 | }; |
---|
3143 | |
---|
3144 | /* the link handlers table */ |
---|
3145 | static |
---|
3146 | struct _rtems_filesystem_file_handlers_r nfs_link_file_handlers = { |
---|
3147 | nfs_link_open, /* OPTIONAL; may be NULL */ |
---|
3148 | nfs_link_close, /* OPTIONAL; may be NULL */ |
---|
3149 | nfs_link_read, /* OPTIONAL; may be NULL */ |
---|
3150 | nfs_link_write, /* OPTIONAL; may be NULL */ |
---|
3151 | nfs_link_ioctl, /* OPTIONAL; may be NULL */ |
---|
3152 | nfs_link_lseek, /* OPTIONAL; may be NULL */ |
---|
3153 | nfs_fstat, /* OPTIONAL; may be NULL */ |
---|
3154 | nfs_fchmod, /* OPTIONAL; may be NULL */ |
---|
3155 | nfs_link_ftruncate, /* OPTIONAL; may be NULL */ |
---|
3156 | nfs_link_fpathconf, /* OPTIONAL; may be NULL - UNUSED */ |
---|
3157 | nfs_link_fsync, /* OPTIONAL; may be NULL */ |
---|
3158 | nfs_link_fdatasync, /* OPTIONAL; may be NULL */ |
---|
3159 | nfs_link_fcntl, /* OPTIONAL; may be NULL */ |
---|
3160 | nfs_unlink, /* OPTIONAL; may be NULL */ |
---|
3161 | }; |
---|
3162 | |
---|
3163 | /* we need a dummy driver entry table to get a |
---|
3164 | * major number from the system |
---|
3165 | */ |
---|
3166 | static |
---|
3167 | rtems_device_driver nfs_initialize( |
---|
3168 | rtems_device_major_number major, |
---|
3169 | rtems_device_minor_number minor, |
---|
3170 | void *arg |
---|
3171 | ) |
---|
3172 | { |
---|
3173 | /* we don't really use this routine because |
---|
3174 | * we cannot supply an argument (contrary |
---|
3175 | * to what the 'arg' parameter suggests - it |
---|
3176 | * is always set to 0 by the generics :-() |
---|
3177 | * and because we don't want the user to |
---|
3178 | * have to deal with the major number (which |
---|
3179 | * OTOH is something WE are interested in. The |
---|
3180 | * only reason for using this API was getting |
---|
3181 | * a major number, after all). |
---|
3182 | * |
---|
3183 | * Something must be present, however, to |
---|
3184 | * reserve a slot in the driver table. |
---|
3185 | */ |
---|
3186 | return RTEMS_SUCCESSFUL; |
---|
3187 | } |
---|
3188 | |
---|
3189 | static rtems_driver_address_table drvNfs = { |
---|
3190 | nfs_initialize, |
---|
3191 | 0, /* open */ |
---|
3192 | 0, /* close */ |
---|
3193 | 0, /* read */ |
---|
3194 | 0, /* write */ |
---|
3195 | 0 /* control */ |
---|
3196 | }; |
---|
3197 | |
---|
3198 | /* Dump a list of the currently mounted NFS to a file */ |
---|
3199 | int |
---|
3200 | nfsMountsShow(FILE *f) |
---|
3201 | { |
---|
3202 | char *mntpt = 0; |
---|
3203 | Nfs nfs; |
---|
3204 | |
---|
3205 | if (!f) |
---|
3206 | f = stdout; |
---|
3207 | |
---|
3208 | if ( !(mntpt=malloc(MAXPATHLEN)) ) { |
---|
3209 | fprintf(stderr,"nfsMountsShow(): no memory\n"); |
---|
3210 | return -1; |
---|
3211 | } |
---|
3212 | |
---|
3213 | fprintf(f,"Currently Mounted NFS:\n"); |
---|
3214 | |
---|
3215 | LOCK(nfsGlob.llock); |
---|
3216 | |
---|
3217 | for (nfs = nfsGlob.mounted_fs; nfs; nfs=nfs->next) { |
---|
3218 | fprintf(f,"%s on ", nfs->mt_entry->dev); |
---|
3219 | if (rtems_filesystem_resolve_location(mntpt, MAXPATHLEN, &nfs->mt_entry->mt_fs_root)) |
---|
3220 | fprintf(f,"<UNABLE TO LOOKUP MOUNTPOINT>\n"); |
---|
3221 | else |
---|
3222 | fprintf(f,"%s\n",mntpt); |
---|
3223 | } |
---|
3224 | |
---|
3225 | UNLOCK(nfsGlob.llock); |
---|
3226 | |
---|
3227 | free(mntpt); |
---|
3228 | return 0; |
---|
3229 | } |
---|
3230 | |
---|
3231 | /* convenience wrapper |
---|
3232 | * |
---|
3233 | * NOTE: this routine calls NON-REENTRANT |
---|
3234 | * gethostbyname() if the host is |
---|
3235 | * not in 'dot' notation. |
---|
3236 | */ |
---|
3237 | int |
---|
3238 | nfsMount(char *uidhost, char *path, char *mntpoint) |
---|
3239 | { |
---|
3240 | rtems_filesystem_mount_table_entry_t *mtab; |
---|
3241 | struct stat st; |
---|
3242 | int devl; |
---|
3243 | char *host; |
---|
3244 | int rval = -1; |
---|
3245 | char *dev = 0; |
---|
3246 | |
---|
3247 | if (!uidhost || !path || !mntpoint) { |
---|
3248 | fprintf(stderr,"usage: nfsMount(""[uid.gid@]host"",""path"",""mountpoint"")\n"); |
---|
3249 | nfsMountsShow(stderr); |
---|
3250 | return -1; |
---|
3251 | } |
---|
3252 | |
---|
3253 | if ( !(dev = malloc((devl=strlen(uidhost) + 20 + strlen(path)+1))) ) { |
---|
3254 | fprintf(stderr,"nfsMount: out of memory\n"); |
---|
3255 | return -1; |
---|
3256 | } |
---|
3257 | |
---|
3258 | /* Try to create the mount point if nonexistent */ |
---|
3259 | if (stat(mntpoint, &st)) { |
---|
3260 | if (ENOENT != errno) { |
---|
3261 | perror("nfsMount trying to create mount point - stat failed"); |
---|
3262 | goto cleanup; |
---|
3263 | } else if (mkdir(mntpoint,0777)) { |
---|
3264 | perror("nfsMount trying to create mount point"); |
---|
3265 | goto cleanup; |
---|
3266 | } |
---|
3267 | } |
---|
3268 | |
---|
3269 | if ( !(host=strchr(uidhost,UIDSEP)) ) { |
---|
3270 | host = uidhost; |
---|
3271 | } else { |
---|
3272 | host++; |
---|
3273 | } |
---|
3274 | |
---|
3275 | if (isdigit((unsigned char)*host)) { |
---|
3276 | /* avoid using gethostbyname */ |
---|
3277 | sprintf(dev,"%s:%s",uidhost,path); |
---|
3278 | } else { |
---|
3279 | struct hostent *h; |
---|
3280 | |
---|
3281 | /* copy the uid part (hostname will be |
---|
3282 | * overwritten) |
---|
3283 | */ |
---|
3284 | strcpy(dev, uidhost); |
---|
3285 | |
---|
3286 | /* NOTE NOTE NOTE: gethostbyname is NOT |
---|
3287 | * thread safe. This is UGLY |
---|
3288 | */ |
---|
3289 | |
---|
3290 | /* BEGIN OF NON-THREAD SAFE REGION */ |
---|
3291 | |
---|
3292 | h = gethostbyname(host); |
---|
3293 | |
---|
3294 | if ( !h || |
---|
3295 | !inet_ntop( AF_INET, |
---|
3296 | (struct in_addr*)h->h_addr_list[0], |
---|
3297 | dev + (host - uidhost), |
---|
3298 | devl - (host - uidhost) ) |
---|
3299 | ) { |
---|
3300 | fprintf(stderr,"nfsMount: host '%s' not found\n",host); |
---|
3301 | goto cleanup; |
---|
3302 | } |
---|
3303 | |
---|
3304 | /* END OF NON-THREAD SAFE REGION */ |
---|
3305 | |
---|
3306 | /* append ':<path>' */ |
---|
3307 | strcat(dev,":"); |
---|
3308 | strcat(dev,path); |
---|
3309 | } |
---|
3310 | |
---|
3311 | printf("Trying to mount %s on %s\n",dev,mntpoint); |
---|
3312 | |
---|
3313 | if (mount(&mtab, |
---|
3314 | &nfs_fs_ops, |
---|
3315 | RTEMS_FILESYSTEM_READ_WRITE, |
---|
3316 | dev, |
---|
3317 | mntpoint)) { |
---|
3318 | perror("nfsMount - mount"); |
---|
3319 | goto cleanup; |
---|
3320 | } |
---|
3321 | |
---|
3322 | rval = 0; |
---|
3323 | |
---|
3324 | cleanup: |
---|
3325 | free(dev); |
---|
3326 | return rval; |
---|
3327 | } |
---|
3328 | |
---|
3329 | /* HERE COMES A REALLY UGLY HACK */ |
---|
3330 | |
---|
3331 | /* This is stupid; it is _very_ hard to find the path |
---|
3332 | * leading to a rtems_filesystem_location_info_t node :-( |
---|
3333 | * The only easy way is making the location the current |
---|
3334 | * directory and issue a getcwd(). |
---|
3335 | * However, since we don't want to tamper with the |
---|
3336 | * current directory, we must create a separate |
---|
3337 | * task to do the job for us - sigh. |
---|
3338 | */ |
---|
3339 | |
---|
3340 | typedef struct ResolvePathArgRec_ { |
---|
3341 | rtems_filesystem_location_info_t *loc; /* IN: location to resolve */ |
---|
3342 | char *buf; /* IN/OUT: buffer where to put the path */ |
---|
3343 | int len; /* IN: buffer length */ |
---|
3344 | rtems_id sync; /* IN: synchronization */ |
---|
3345 | rtems_status_code status; /* OUT: result */ |
---|
3346 | } ResolvePathArgRec, *ResolvePathArg; |
---|
3347 | |
---|
3348 | static void |
---|
3349 | resolve_path(rtems_task_argument arg) |
---|
3350 | { |
---|
3351 | ResolvePathArg rpa = (ResolvePathArg)arg; |
---|
3352 | rtems_filesystem_location_info_t old; |
---|
3353 | |
---|
3354 | /* IMPORTANT: let the helper task have its own libio environment (i.e. cwd) */ |
---|
3355 | if (RTEMS_SUCCESSFUL == (rpa->status = rtems_libio_set_private_env())) { |
---|
3356 | |
---|
3357 | old = rtems_filesystem_current; |
---|
3358 | |
---|
3359 | rtems_filesystem_current = *(rpa->loc); |
---|
3360 | |
---|
3361 | if ( !getcwd(rpa->buf, rpa->len) ) |
---|
3362 | rpa->status = RTEMS_UNSATISFIED; |
---|
3363 | |
---|
3364 | /* must restore the cwd because 'freenode' will be called on it */ |
---|
3365 | rtems_filesystem_current = old; |
---|
3366 | } |
---|
3367 | rtems_semaphore_release(rpa->sync); |
---|
3368 | rtems_task_delete(RTEMS_SELF); |
---|
3369 | } |
---|
3370 | |
---|
3371 | |
---|
3372 | /* a utility routine to find the path leading to a |
---|
3373 | * rtems_filesystem_location_info_t node |
---|
3374 | * |
---|
3375 | * INPUT: 'loc' and a buffer 'buf' (length 'len') to hold the |
---|
3376 | * path. |
---|
3377 | * OUTPUT: path copied into 'buf' |
---|
3378 | * |
---|
3379 | * RETURNS: 0 on success, RTEMS error code on error. |
---|
3380 | */ |
---|
3381 | rtems_status_code |
---|
3382 | rtems_filesystem_resolve_location(char *buf, int len, rtems_filesystem_location_info_t *loc) |
---|
3383 | { |
---|
3384 | ResolvePathArgRec arg; |
---|
3385 | rtems_id tid = 0; |
---|
3386 | rtems_task_priority pri; |
---|
3387 | rtems_status_code status; |
---|
3388 | |
---|
3389 | arg.loc = loc; |
---|
3390 | arg.buf = buf; |
---|
3391 | arg.len = len; |
---|
3392 | arg.sync = 0; |
---|
3393 | |
---|
3394 | status = rtems_semaphore_create( |
---|
3395 | rtems_build_name('r','e','s','s'), |
---|
3396 | 0, |
---|
3397 | RTEMS_SIMPLE_BINARY_SEMAPHORE, |
---|
3398 | 0, |
---|
3399 | &arg.sync); |
---|
3400 | |
---|
3401 | if (RTEMS_SUCCESSFUL != status) |
---|
3402 | goto cleanup; |
---|
3403 | |
---|
3404 | rtems_task_set_priority(RTEMS_SELF, RTEMS_CURRENT_PRIORITY, &pri); |
---|
3405 | |
---|
3406 | status = rtems_task_create( |
---|
3407 | rtems_build_name('r','e','s','s'), |
---|
3408 | pri, |
---|
3409 | RTEMS_MINIMUM_STACK_SIZE + 50000, |
---|
3410 | RTEMS_DEFAULT_MODES, |
---|
3411 | RTEMS_DEFAULT_ATTRIBUTES, |
---|
3412 | &tid); |
---|
3413 | |
---|
3414 | if (RTEMS_SUCCESSFUL != status) |
---|
3415 | goto cleanup; |
---|
3416 | |
---|
3417 | status = rtems_task_start(tid, resolve_path, (rtems_task_argument)&arg); |
---|
3418 | |
---|
3419 | if (RTEMS_SUCCESSFUL != status) { |
---|
3420 | rtems_task_delete(tid); |
---|
3421 | goto cleanup; |
---|
3422 | } |
---|
3423 | |
---|
3424 | |
---|
3425 | /* synchronize with the helper task */ |
---|
3426 | rtems_semaphore_obtain(arg.sync, RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
3427 | |
---|
3428 | status = arg.status; |
---|
3429 | |
---|
3430 | cleanup: |
---|
3431 | if (arg.sync) |
---|
3432 | rtems_semaphore_delete(arg.sync); |
---|
3433 | |
---|
3434 | return status; |
---|
3435 | } |
---|
3436 | |
---|
3437 | int |
---|
3438 | nfsSetTimeout(uint32_t timeout_ms) |
---|
3439 | { |
---|
3440 | rtems_interrupt_level k; |
---|
3441 | uint32_t s,us; |
---|
3442 | |
---|
3443 | if ( timeout_ms > 100000 ) { |
---|
3444 | /* out of range */ |
---|
3445 | return -1; |
---|
3446 | } |
---|
3447 | |
---|
3448 | s = timeout_ms/1000; |
---|
3449 | us = (timeout_ms % 1000) * 1000; |
---|
3450 | |
---|
3451 | rtems_interrupt_disable(k); |
---|
3452 | _nfscalltimeout.tv_sec = s; |
---|
3453 | _nfscalltimeout.tv_usec = us; |
---|
3454 | rtems_interrupt_enable(k); |
---|
3455 | |
---|
3456 | return 0; |
---|
3457 | } |
---|
3458 | |
---|
3459 | uint32_t |
---|
3460 | nfsGetTimeout( void ) |
---|
3461 | { |
---|
3462 | rtems_interrupt_level k; |
---|
3463 | uint32_t s,us; |
---|
3464 | rtems_interrupt_disable(k); |
---|
3465 | s = _nfscalltimeout.tv_sec; |
---|
3466 | us = _nfscalltimeout.tv_usec; |
---|
3467 | rtems_interrupt_enable(k); |
---|
3468 | return s*1000 + us/1000; |
---|
3469 | } |
---|