8b2c8e993c5fee24243aff954b0c13b01b5d6631
[openafs.git] / src / vol / devname.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <sys/param.h>
11 #include <afs/param.h>
12 #include <rx/xdr.h>
13 #include <afs/afsint.h>
14 #include <ctype.h>
15 #if !defined(AFS_SGI_ENV)
16 #ifdef  AFS_OSF_ENV
17 #include <ufs/fs.h>
18 #else   /* AFS_OSF_ENV */
19 #ifdef AFS_VFSINCL_ENV
20 #define VFS
21 #ifdef  AFS_SUN5_ENV
22 #include <sys/fs/ufs_fs.h>
23 #else
24 #include <ufs/fs.h>
25 #endif
26 #else /* AFS_VFSINCL_ENV */
27 #if !defined(AFS_AIX_ENV) && !defined(AFS_LINUX22_ENV)
28 #include <sys/fs.h>
29 #endif
30 #endif /* AFS_VFSINCL_ENV */
31 #endif  /* AFS_OSF_ENV */
32 #endif /* AFS_SGI_ENV */
33 #include <sys/errno.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <stdio.h>
37 #include <sys/file.h>
38 #ifdef  AFS_AIX_ENV
39 #include <sys/vfs.h>
40 #include <fcntl.h>
41 #else
42 #ifdef  AFS_HPUX_ENV
43 #include <fcntl.h>
44 #include <mntent.h>
45 #else
46 #if     defined(AFS_SUN_ENV) || defined(AFS_SUN5_ENV)
47 #ifdef  AFS_SUN5_ENV
48 #include <sys/mnttab.h>
49 #include <sys/mntent.h>
50 #else
51 #include <mntent.h>
52 #endif
53 #else
54 #if defined(AFS_SGI_ENV)
55 #include <fcntl.h>
56 #include <mntent.h>
57 #include "../sgiefs/efs.h"
58 #define ROOTINO EFS_ROOTINO
59 #else
60 #ifdef AFS_LINUX22_ENV
61 #include <mntent.h>
62 #else
63 #include <fstab.h>
64 #endif
65 #endif
66 #endif /* AFS_SGI_ENV */
67 #endif /* AFS_HPUX_ENV */
68 #endif
69 #include <netdb.h>
70 #include <netinet/in.h>
71 #include <sys/wait.h>
72 #include <setjmp.h>
73 #ifndef ITIMER_REAL
74 #include <sys/time.h>
75 #endif /* ITIMER_REAL */
76 #include "partition.h"
77 #ifdef AFS_LINUX22_ENV
78 #include <asm/types.h>
79 #include <linux/ext2_fs.h>
80 #define ROOTINO EXT2_ROOT_INO /* Assuming we do this on ext2, of course. */
81 #endif
82
83 /* ensure that we don't have a "/" instead of a "/dev/rxd0a" type of device.
84  * returns pointer to static storage; copy it out quickly!
85  */
86 char *vol_DevName(adev, wpath)
87 char *wpath;
88 dev_t adev; {
89     static char pbuffer[128];
90     char pbuf[128], *ptr;
91     int code, i;
92 #ifdef  AFS_SUN5_ENV
93     struct mnttab mnt;
94     FILE *mntfile;
95 #else
96 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_LINUX22_ENV)
97     struct mntent *mntent;
98     FILE *mfd;
99 #else
100     struct fstab *fsent;
101 #endif
102 #endif
103 #ifdef  AFS_AIX_ENV
104     int nmounts;
105     struct vmount *vmountp;
106 #endif
107
108 #ifdef  AFS_AIX_ENV
109     if ((nmounts = getmount(&vmountp)) <= 0)    {   
110         return (char *)0;
111     }
112     for (; nmounts; nmounts--, vmountp = (struct vmount *)((int)vmountp + vmountp->vmt_length)) {
113         char *part = vmt2dataptr(vmountp, VMT_STUB);
114 #else
115 #ifdef  AFS_SUN5_ENV
116     if (!(mntfile = fopen(MNTTAB, "r"))) {
117         return (char *)0;
118     }
119     while (!getmntent(mntfile, &mnt)) {
120         char *part = mnt.mnt_mountp;
121 #else
122 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_LINUX22_ENV)
123 #ifdef AFS_LINUX22_ENV
124     if ((mfd = setmntent("/proc/mounts", "r")) == NULL) {
125         if ((mfd = setmntent("/etc/mtab", "r")) == NULL) {
126             return (char *)0;
127         }
128     }
129 #else
130     if ((mfd = setmntent(MOUNTED/*MNTTAB*/, "r")) == NULL) {
131         return (char *)0;
132     }
133 #endif
134     while (mntent = getmntent(mfd)) {
135         char *part = mntent->mnt_dir;
136 #else
137     setfsent();
138     while (fsent = getfsent()) {
139         char *part = fsent->fs_file;
140 #endif
141 #endif /* AFS_SGI_ENV */
142 #endif
143         struct stat status;
144 #ifdef  AFS_AIX_ENV
145         if (vmountp->vmt_flags & (MNT_READONLY|MNT_REMOVABLE|MNT_REMOTE)) continue; /* Ignore any "special" partitions */
146 #else
147 #ifdef  AFS_SUN5_ENV
148         /* Ignore non ufs or non read/write partitions */
149         if ((strcmp(mnt.mnt_fstype, "ufs") !=0) ||
150             (strncmp(mnt.mnt_mntopts, "ro,ignore",9) ==0)) 
151             continue; 
152 #else
153 #if defined(AFS_LINUX22_ENV)
154         if (strcmp(mntent->mnt_type, "ext2"))
155             continue;
156 #else
157 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV)
158         if (!hasmntopt(mntent, MNTOPT_RW)) continue;
159 #else
160         if (strcmp(fsent->fs_type, "rw") != 0) continue; /* Ignore non read/write partitions */
161 #endif /* AFS_LINUX22_ENV */
162 #endif /* AFS_SGI_ENV */
163 #endif
164 #endif
165         /* Only keep track of "/vicepx" partitions since it can get hairy when NFS mounts are involved.. */
166         if (strncmp(part, VICE_PARTITION_PREFIX, VICE_PREFIX_SIZE)) {
167             continue;           /* Non /vicepx; ignore */
168         }
169         if (stat(part, &status) == -1) {
170             continue;
171         }
172 #ifndef AFS_SGI_XFS_IOPS_ENV
173         if ((status.st_ino != ROOTINO) /*|| ((status.st_mode & S_IFMT) != S_IFBLK)*/) {
174             continue;
175         }
176 #endif
177         if (status.st_dev == adev) {
178 #ifdef  AFS_AIX_ENV
179             strcpy(pbuffer, vmt2dataptr(vmountp, VMT_OBJECT));
180 #else
181 #ifdef  AFS_SUN5_ENV
182             strcpy(pbuffer, mnt.mnt_special);
183 #else
184 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_LINUX22_ENV)
185             strcpy(pbuffer, mntent->mnt_fsname);
186 #else
187             strcpy(pbuffer, fsent->fs_spec);
188 #endif
189 #endif  /* AFS_SGI_ENV */
190 #endif
191             if (wpath) {
192                 strcpy(pbuf, pbuffer);
193                 ptr = (char *)rindex(pbuf, '/');
194                 if (ptr) {
195                     *ptr = '\0';
196                     strcpy(wpath, pbuf);
197                 } else
198                     return (char *)0;
199             }
200             ptr = (char *)rindex(pbuffer, '/');     
201             if (ptr) {
202                 strcpy(pbuffer, ptr+1);
203                 return pbuffer;
204             } else
205                 return (char *)0;
206         }
207     }
208 #ifndef AFS_AIX_ENV
209 #ifdef  AFS_SUN5_ENV
210    (void) fclose(mntfile);
211 #else
212 #if defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_HPUX_ENV) || defined(AFS_LINUX22_ENV)
213     endmntent(mfd);
214 #else
215     endfsent();
216 #endif
217 #endif /* AFS_SGI_ENV */
218 #endif
219     return (char *)0;
220 }
221
222
223 /* Search for the raw device name. Will put an "r" in front of each
224  * directory and file entry of the pathname until we find a character
225  * device.
226  */
227 char *afs_rawname(devfile)
228   char *devfile;
229 {
230   static char rawname[100];
231   struct stat statbuf;
232   char *p;
233   int code, i;
234
235   i = strlen(devfile);
236   while (i >= 0) {
237      strcpy(rawname, devfile);
238      if (devfile[i] == '/') {
239         rawname[i+1] = 'r';
240         rawname[i+2] = 0;
241         strcat(rawname, &devfile[i+1]);
242      }
243
244      code = stat(rawname, &statbuf);
245      if (!code && S_ISCHR(statbuf.st_mode))
246         return rawname;
247   
248      while((--i>=0) && (devfile[i] != '/'));
249   }
250
251   return (char *)0;
252 }
253