summaryrefslogtreecommitdiff
path: root/libdiskfs/dir-lookup.c
blob: cee5f614c32d8e62864fd9385c3518fdf0d1bf38 (plain)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/* libdiskfs implementation of fs.defs:dir_lookup
   Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2, or (at
   your option) any later version.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */

#include "priv.h"
#include "fs_S.h"
#include <fcntl.h>
#include <string.h>
#include <hurd/fsys.h>

/* Implement dir_lookup as described in <hurd/fs.defs>. */
kern_return_t
diskfs_S_dir_lookup (struct protid *dircred,
		     char *path,
		     int flags,
		     mode_t mode,
		     enum retry_type *retry,
		     char *retryname,
		     file_t *returned_port,
		     mach_msg_type_name_t *returned_port_poly)
{
  struct node *dnp;
  struct node *np;
  int nsymlink = 0;
  char *nextname;
  int nextnamelen;
  error_t error = 0;
  char *pathbuf = 0;
  int pathbuflen = 0;
  int newnamelen;
  int create, excl;
  int lastcomp = 0;
  int newnode = 0;
  struct dirstat *ds = 0;
  int mustbedir = 0;
  int amt;
  int type;

  if (!dircred)
    return EOPNOTSUPP;

  flags &= O_HURD;
  
  create = (flags & O_CREAT);
  excl = (flags & O_EXCL);

  /* Skip leading slashes */
  while (path[0] == '/')
    path++;

  *returned_port_poly = MACH_MSG_TYPE_MAKE_SEND;
  *retry = FS_RETRY_NORMAL;
  retryname[0] = '\0';

  if (path[0] == '\0')
    {
      mustbedir = 1;

      /* Set things up in the state expected by the code from gotit: on. */
      dnp = 0;
      np = dircred->po->np;
      mutex_lock (&np->lock);
      diskfs_nref (np);
      goto gotit;
    }

  dnp = dircred->po->np;
  mutex_lock (&dnp->lock);
  np = 0;

  diskfs_nref (dnp);		/* acquire a reference for later diskfs_nput */

  do
    {
      assert (!lastcomp);
      
      /* Find the name of the next pathname component */
      nextname = index (path, '/');

      if (nextname)
	{
	  *nextname++ = '\0';
	  while (*nextname == '/')
	    nextname++;
	  if (*nextname == '\0')
	    {
	      /* These are the rules for filenames ending in /. */
	      nextname = 0;
	      lastcomp = 1;
	      mustbedir = 1;
	      create = 0;
	    }
	  else
	    lastcomp = 0;
	}
      else
	lastcomp = 1;
	  
      np = 0;

      /* diskfs_lookup the next pathname component */
      if (lastcomp && create)
	{
	  assert (!ds);
	  ds = alloca (diskfs_dirstat_size);
	  error = diskfs_lookup (dnp, path, CREATE, &np, ds, dircred);
	}
      else
	error = diskfs_lookup (dnp, path, LOOKUP, &np, 0, dircred);

      if (lastcomp && create && excl && (!error || error == EAGAIN))
	error = EEXIST;

      /* If we get an error we're done */
      if (error == EAGAIN)
	{
	  if (dircred->po->dotdotport != MACH_PORT_NULL)
	    {
	      *retry = FS_RETRY_REAUTH;
	      *returned_port = dircred->po->dotdotport;
	      *returned_port_poly = MACH_MSG_TYPE_COPY_SEND;
	      if (!lastcomp)
		strcpy (retryname, nextname);
	      error = 0;
	      goto out;
	    }
	  else
	    {
	      error = 0;
	      np = dnp;
	      diskfs_nref (np);
	    }
	}

      /* Create the new node if necessary */
      if (lastcomp && create)
	{
	  if (error == ENOENT)
	    {
	      mode &= ~(S_IFMT | S_ISPARE | S_ISVTX);
	      mode |= S_IFREG;
	      error = diskfs_create_node (dnp, path, mode, &np, dircred, ds);
	      if (diskfs_synchronous)
		{
		  diskfs_update_file (dnp, 1);
		  diskfs_update_file (np, 1);
		}
	      newnode = 1;
	    }
	  else
	    diskfs_drop_dirstat (dnp, ds);
	}
      
      if (error)
	goto out;

      /* If this is translated, start the translator (if necessary)
	 and return.  */
      /* The check for `np != dnp' simplifies this code a great
	 deal.  Such a translator should already have been started,
	 so there's no lossage in doing it this way. */
      if ((!lastcomp || !(flags & O_NOTRANS))
	  && np != dnp)
	{
	  mach_port_t control;
	  uid_t *uids = 0, *gids;
	  int nuids, ngids;
	  file_t dirfile = MACH_PORT_NULL;

	  /* Be very careful not to hold an inode lock while fetching
	     a translator lock and vice versa.  */

	  mutex_unlock (&np->lock);
	  mutex_unlock (&dnp->lock);

	repeat_trans:
	  mutex_lock (&np->translator.lock);
	  if (np->translator.control != MACH_PORT_NULL)
	    {
	      control = np->translator.control;
	      mach_port_mod_refs (mach_task_self (), control, 
				  MACH_PORT_RIGHT_SEND, 1);
	      mutex_unlock (&np->translator.lock);
	      
	      /* Now we have a copy of the translator port that isn't
		 dependent on the translator lock itself.  Relock
		 the directory, make a port from it, and then call
		 fsys_getroot. */

	      if (dirfile == MACH_PORT_NULL)
		{
		  mutex_lock (&dnp->lock);
		  dirfile = (ports_get_right
			     (diskfs_make_protid
			      (diskfs_make_peropen (dnp, 0, 
						    dircred->po->dotdotport),
			       0, 0, 0, 0)));
		  mach_port_insert_right (mach_task_self (), dirfile, dirfile,
					  MACH_MSG_TYPE_MAKE_SEND);
		  mutex_unlock (&dnp->lock);
		}

	      if (!uids)
		{
		  uids = dircred->uids;
		  gids = dircred->gids;
		  nuids = dircred->nuids;
		  ngids = dircred->ngids;
		}
	      
	      error = fsys_getroot (control, dirfile, MACH_MSG_TYPE_COPY_SEND,
				    uids, nuids, gids, ngids,
				    lastcomp ? flags : 0,
				    retry, retryname, returned_port);
	      
	      /* If we got MACH_SEND_INVALID_DEST or MIG_SERVER_DIED, then
		 the server is dead.  Zero out the old control port and try
		 everything again.  */

	      if (error == MACH_SEND_INVALID_DEST || error == MIG_SERVER_DIED)
		{
		  mutex_lock (&np->translator.lock);

		  /* Only zero it if it hasn't changed. */

		  if (np->translator.control == control)
		    fshelp_translator_drop (&np->translator);
		  mutex_unlock (&np->translator.lock);
	      
		  /* And we're done with this port. */
		  mach_port_deallocate (mach_task_self (), control);

		  goto repeat_trans;
		}
	      
	      /* Otherwise, we're done; return to the user.  If there
		 are more components after this name, be sure to append
		 them to the user's retry path. */
	      if (!error && !lastcomp)
		{
		  strcat (retryname, "/");
		  strcat (retryname, nextname);
		}
	      *returned_port_poly = MACH_MSG_TYPE_MOVE_SEND;
	      diskfs_nrele (dnp);
	      diskfs_nrele (np);
	      mach_port_deallocate (mach_task_self (), dirfile);
	      return error;
	    }
	  else
	    {
	      /* If we get here, then we have no active control port.
		 Check to see if there is a passive translator, and if so
		 repeat the translator check. */
	      mutex_unlock (&np->translator.lock);
	      
	      mutex_lock (&np->lock);
	      if (np->istranslated)
		{
		  /* Start the translator. */
		  if (dirfile == MACH_PORT_NULL)
		    {
		      /* This code is the same as above. */
		      mutex_unlock (&np->lock);
		      mutex_lock (&dnp->lock);
		      dirfile = (ports_get_right
				 (diskfs_make_protid
				  (diskfs_make_peropen (dnp, 0, 
							dircred->po
							->dotdotport),
				   0, 0, 0, 0)));
		      mach_port_insert_right (mach_task_self (), dirfile,
					      dirfile,
					      MACH_MSG_TYPE_MAKE_SEND);
		      mutex_unlock (&dnp->lock);
		      mutex_lock (&np->lock);
		    }
		  error = diskfs_start_translator (np, dirfile, dircred);
		  if (error)
		    {
		      mach_port_deallocate (mach_task_self (), dirfile);
		      diskfs_nrele (dnp);
		      diskfs_nput (np);
		      return error;
		    }
		  goto repeat_trans;
		}
	    }
	  
	  /* We're here if we tried the translator check, and it
	     failed.   Lock everything back, and make sure we do it
	     in the right order. */
	  if (strcmp (path, ".."))
	    {
	      mutex_unlock (&np->lock);
	      mutex_lock (&dnp->lock);
	      mutex_lock (&np->lock);
	    }
	  else
	    mutex_lock (&dnp->lock);
	}
      
      if (S_ISLNK (np->dn_stat.st_mode)
	  && !(lastcomp && (flags & (O_NOLINK|O_NOTRANS))))
	{
	  /* Handle symlink interpretation */

	  if (nsymlink++ > diskfs_maxsymlinks)
	    {
	      error = ELOOP;
	      goto out;
	    }
	      
	  nextnamelen = nextname ? strlen (nextname) + 1 : 0;
	  newnamelen = nextnamelen + np->dn_stat.st_size + 1;
	  if (pathbuflen < newnamelen)
	    {
	      pathbuf = alloca (newnamelen);
	      pathbuflen = newnamelen;
	    }
	      
	  if (diskfs_read_symlink_hook)
	    error = (*diskfs_read_symlink_hook)(np, pathbuf);
	  if (!diskfs_read_symlink_hook || error == EINVAL)
	    error = diskfs_node_rdwr (np, pathbuf, 
				      0, np->dn_stat.st_size, 0, 
				      dircred, &amt);
	  if (error)
	    goto out;
	      
	  if (nextname)
	    {
	      pathbuf[np->dn_stat.st_size] = '/';
	      bcopy (nextname, pathbuf + np->dn_stat.st_size + 1, 
		     nextnamelen - 1);
	    }
	  pathbuf[nextnamelen + np->dn_stat.st_size] = '\0';

	  if (pathbuf[0] == '/')
	    {
	      /* Punt to the caller.  */
	      *retry = FS_RETRY_MAGICAL;
	      *returned_port = MACH_PORT_NULL;
	      strcpy (retryname, pathbuf);
	      goto out;
	    }
	  
	  path = pathbuf;
	  if (lastcomp)
	    {
	      lastcomp = 0;
	      /* Symlinks to nonexistent files aren't allowed to cause
		 creation, so clear the flag here. */
	      create = 0;
	    }
	  diskfs_nput (np);
	  np = 0;
	}
      else
	{
	  /* Handle normal nodes */
	  path = nextname;
	  if (np == dnp)
	    diskfs_nrele (dnp);
	  else
	    diskfs_nput (dnp);
	  if (!lastcomp)
	    {
	      dnp = np;
	      np = 0;
	    }
	  else
	    dnp = 0;
	}
    } while (path && *path);
  
  /* At this point, np is the node to return.  If newnode is set, then
     we just created this node.  */

 gotit:
  type = np->dn_stat.st_mode & S_IFMT;

  if (mustbedir && type != S_IFDIR)
    {
      error = ENOTDIR;
      goto out;
    }
  
  if (!newnode)
    /* Check permissions on existing nodes, but not new ones. */
    {
      if ((type == S_IFSOCK || type == S_IFBLK || type == S_IFLNK
	   || type == S_IFCHR || type == S_IFIFO)
	  && (flags & (O_READ|O_WRITE|O_EXEC)))
	error = EOPNOTSUPP;

      if (!error && (flags & O_READ))
	error = diskfs_access (np, S_IREAD, dircred);

      if (!error && (flags & O_EXEC))
	error = diskfs_access (np, S_IEXEC, dircred);

      if (!error && (flags & O_WRITE))
	{
	  if (type == S_IFDIR)
	    error = EISDIR;
	  else if (diskfs_readonly)
	    error = EROFS;
	  else
	    error = diskfs_access (np, S_IWRITE, dircred);
	}

      if (error)
	goto out;
    }
    
  if ((flags & O_NOATIME) && (diskfs_isowner (np, dircred) == EPERM))
    flags &= ~O_NOATIME;

  flags &= ~OPENONLY_STATE_MODES;
      
  *returned_port = (ports_get_right
		    (diskfs_make_protid 
		     (diskfs_make_peropen (np, flags, dircred->po->dotdotport),
		      dircred->uids, dircred->nuids,
		      dircred->gids, dircred->ngids)));

  
 out:
  if (np)
    {
      if (dnp == np)
	diskfs_nrele (np);
      else
	diskfs_nput (np);
    }
  if (dnp)
    diskfs_nput (dnp);
  return error;
}