summaryrefslogtreecommitdiff
path: root/sysutils/hal/files/patch-hald_hf-storage.c
blob: a699ca827e57b6a1a61314858cb4a2d7ddf82d9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
--- hald/freebsd/hf-storage.c.orig	2009-05-12 08:24:28.000000000 -0400
+++ hald/freebsd/hf-storage.c	2009-10-24 02:30:22.000000000 -0400
@@ -30,6 +30,8 @@
 #include <limits.h>
 #include <inttypes.h>
 #include <string.h>
+#include <unistd.h>
+#include <sys/param.h>
 #include <sys/types.h>
 #include <sys/disklabel.h>
 
@@ -38,6 +40,7 @@
 
 #include "hf-storage.h"
 #include "hf-block.h"
+#include "hf-devd.h"
 #include "hf-devtree.h"
 #include "hf-volume.h"
 #include "hf-util.h"
@@ -64,7 +67,7 @@ typedef struct
 static GNode *hf_storage_geom_tree = NULL;
 static GHashTable *hf_storage_geom_hash = NULL;
 
-static void hf_storage_init_geom (void);
+static void hf_storage_init_geom (gboolean force);
 static gboolean hf_storage_device_has_addon (HalDevice *device);
 
 static void
@@ -104,6 +107,8 @@ hf_storage_class_is_partitionable (const
 {
   return (! strcmp(geom_class, "MBR") ||
           ! strcmp(geom_class, "MBREXT") ||
+	  ! strcmp(geom_class, "PART") ||
+	  ! strcmp(geom_class, "JOURNAL") ||
 	  ! strcmp(geom_class, "GPT") ||
           ! strcmp(geom_class, "APPLE") || ! strcmp(geom_class, "SUN"));
 }
@@ -117,6 +122,7 @@ hf_storage_geom_has_partitions (const Ge
   if (g_node_n_children(node) > 0)
     return TRUE;
 
+  /*
   if (hf_storage_class_is_partitionable(geom_obj->class) &&
       g_node_next_sibling(node) != NULL)
     {
@@ -135,6 +141,7 @@ hf_storage_geom_has_partitions (const Ge
             return TRUE;
         }
     }
+    */
 
   return FALSE;
 }
@@ -294,7 +301,7 @@ hf_storage_device_probe (HalDevice *devi
 {
   g_return_if_fail(HAL_IS_DEVICE(device));
 
-  hf_storage_init_geom();
+  hf_storage_init_geom(TRUE);
 
   if (hf_runner_run_sync(device, 0, "hald-probe-storage",
 			 "HF_HAS_CHILDREN", HF_BOOL_TO_STRING(hf_storage_device_has_partitions(device)),
@@ -403,13 +410,49 @@ hf_storage_parse_conftxt (const char *co
           continue;
 	}
 
+      depth = atoi(fields[0]);
+      hash = g_str_hash(fields[2]);
+      if (g_hash_table_lookup(table, GUINT_TO_POINTER(hash)) != NULL)
+        {
+          g_strfreev(fields);
+	  curr_depth = depth;
+	  continue;
+	}
+
+      /* XXX This is a hack, but we need to ignore dynamic labels like
+       * ufsids which are created and destroyed based on whether or not
+       * the actual device is mounted or not.  If we don't then strange
+       * things happen in applications like nautilus.
+       */
+      if ((! strcmp(fields[1], "LABEL") ||
+          ! strcmp(fields[1], "BSD")) &&
+          ! strncmp(fields[2], "ufsid/", strlen("ufsid/")))
+        {
+          g_strfreev(fields);
+	  continue;
+	}
+
       geom_obj = g_new0(Geom_Object, 1);
 
-      depth = atoi(fields[0]);
       geom_obj->class = g_strdup(fields[1]);
       geom_obj->dev = g_strdup(fields[2]);
+      /* Allow for spaces in label names. */
+      if (! strcmp(fields[1], "LABEL"))
+        {
+          int j;
+
+	  for (j = 3; g_strv_length(fields) > (j + 2) &&
+               strcmp(fields[j + 2], "i"); j++)
+            {
+              char *tmp;
+
+	      tmp = g_strdup_printf("%s %s", geom_obj->dev, fields[j]);
+	      g_free(geom_obj->dev);
+	      geom_obj->dev = tmp;
+	    }
+	}
+
       geom_obj->type = -1;	/* We use -1 here to denote a missing type. */
-      hash = g_str_hash(geom_obj->dev);
       geom_obj->hash = hash;
 
       if (g_strv_length(fields) >= 5)
@@ -433,6 +476,30 @@ hf_storage_parse_conftxt (const char *co
               if (! strcmp (geom_obj->class, "GPT") ||
                   ! strcmp (geom_obj->class, "APPLE"))
                 geom_obj->str_type = g_strdup(fields[10]);
+	      else if (! strcmp (geom_obj->class, "PART"))
+                {
+		  geom_obj->str_type = g_strdup(fields[10]);
+                  if (g_strv_length(fields) >= 15)
+                    {
+                      if (! strcmp(fields[13], "xt"))
+                        {
+                          geom_obj->type = atoi(fields[14]);
+			  if (! strcmp(fields[11], "xs"))
+                            {
+                              g_free(geom_obj->class);
+			      geom_obj->class = g_strdup(fields[12]);
+			    }
+			}
+		    }
+		}
+	      else if (fields[10][0] == '!')
+                {
+                  char *nottype;
+
+		  nottype = fields[10];
+		  nottype++;
+                  geom_obj->type = atoi(nottype);
+		}
 	      else
                 geom_obj->type = atoi(fields[10]);
 	    }
@@ -541,17 +608,27 @@ hf_storage_device_rescan_real (HalDevice
 }
 
 static gboolean
-hf_storage_conftxt_timeout_cb (gpointer data)
+hf_storage_devd_notify (const char *system,
+		        const char *subsystem,
+			const char *type,
+			const char *data)
 {
   static GSList *disks = NULL;
   static gboolean first = TRUE;
+  gboolean handled = FALSE;
   char *conftxt;
   GSList *new_disks;
 
-  if (hf_is_waiting)
-    return TRUE;
+  if (! data || strcmp(system, "DEVFS") || strcmp(subsystem, "CDEV") ||
+      (strcmp(type, "CREATE") && strcmp(type, "DESTROY")))
+    return FALSE;
+
+  if (! strcmp(type, "DESTROY"))
+    g_usleep(G_USEC_PER_SEC/2);
 
   conftxt = hf_get_string_sysctl(NULL, "kern.geom.conftxt");
+  if (! conftxt)
+    return FALSE;
   new_disks = hf_storage_parse_conftxt(conftxt);
   g_free(conftxt);
 
@@ -572,6 +649,7 @@ hf_storage_conftxt_timeout_cb (gpointer 
 	  if (! hf_storage_find_disk(disks, disk->name))
 	    {
 	      osspec_probe();	/* catch new disk(s) */
+	      handled = TRUE;
 	      break;
 	    }
 	}
@@ -593,7 +671,10 @@ hf_storage_conftxt_timeout_cb (gpointer 
 		  device = hf_devtree_find_from_name(hald_get_gdl(), disk->name);
 		  if (device && hal_device_has_capability(device, "storage") &&
 		      ! hf_storage_device_has_addon(device))
-		    hf_storage_device_rescan_real(device);
+		    {
+		      hf_storage_device_rescan_real(device);
+		      handled = TRUE;
+		    }
 		}
 	    }
 	  else
@@ -601,7 +682,10 @@ hf_storage_conftxt_timeout_cb (gpointer 
 	      /* disk removed */
 	      device = hf_devtree_find_from_name(hald_get_gdl(), disk->name);
 	      if (device && hal_device_has_capability(device, "storage"))
-	        hf_device_remove_tree(device);
+		{
+	          hf_device_remove_tree(device);
+		  handled = TRUE;
+		}
 	    }
 	}
     }
@@ -610,17 +694,30 @@ hf_storage_conftxt_timeout_cb (gpointer 
   g_slist_free(disks);
   disks = new_disks;
 
+  return handled;
+}
+
+#if __FreeBSD_version < 700110
+static gboolean
+hf_storage_conftxt_timeout_cb (gpointer data)
+{
+  if (hf_is_waiting)
+    return TRUE;
+
+  hf_storage_devd_notify("DEVFS", "CDEV", "CREATE", "");
+
   return TRUE;
 }
+#endif
 
 static void
-hf_storage_init_geom (void)
+hf_storage_init_geom (gboolean force)
 {
   char *conftxt;
   static gboolean inited = FALSE;
   GSList *disks;
 
-  if (inited)
+  if (inited && ! force)
     return;
 
   conftxt = hf_get_string_sysctl(NULL, "kern.geom.conftxt");
@@ -636,8 +733,10 @@ hf_storage_init_geom (void)
 static void
 hf_storage_init (void)
 {
-  hf_storage_init_geom();
+  hf_storage_init_geom(FALSE);
+#if __FreeBSD_version < 700110
   g_timeout_add(3000, hf_storage_conftxt_timeout_cb, NULL);
+#endif
 }
 
 void
@@ -719,8 +818,6 @@ hf_storage_device_add (HalDevice *device
 {
   g_return_if_fail(HAL_IS_DEVICE(device));
 
-  hf_storage_init_geom();
-
   if (hf_device_preprobe(device))
     {
       hf_storage_device_probe(device, FALSE);
@@ -738,7 +835,7 @@ hf_storage_get_geoms (const char *devnam
 
   g_return_val_if_fail(devname != NULL, NULL);
 
-  hf_storage_init_geom();
+  hf_storage_init_geom(FALSE);
 
   hash = g_str_hash(devname);
   node = g_node_find(hf_storage_geom_tree, G_PRE_ORDER, G_TRAVERSE_ALL,
@@ -801,3 +898,7 @@ HFHandler hf_storage_handler = {
   .probe =		hf_storage_probe,
   .device_rescan =	hf_storage_device_rescan
 };
+
+HFDevdHandler hf_storage_devd_handler = {
+  .notify =	hf_storage_devd_notify
+};