summaryrefslogtreecommitdiff
path: root/tmpfs/dir.c
blob: 595e5437538e07e88d02c527b6ae0ca5ac68e8c5 (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
/* Directories for tmpfs.
   Copyright (C) 2000 Free Software Foundation, Inc.

This file is part of the GNU Hurd.

The GNU Hurd 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.

The GNU Hurd 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 the GNU Hurd; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

#include "tmpfs.h"
#include <stdlib.h>

error_t
diskfs_init_dir (struct node *dp, struct node *pdp, struct protid *cred)
{
  dp->dn->u.dir.dotdot = pdp->dn;
  dp->dn->u.dir.entries = 0;
  return 0;
}

error_t
diskfs_clear_directory (struct node *dp, struct node *pdp,
			struct protid *cred)
{
  if (dp->dn->u.dir.entries != 0)
    return ENOTEMPTY;
  assert (dp->dn_stat.st_size == 0);
  assert (dp->dn->u.dir.dotdot == pdp->dn);
  return 0;
}

int
diskfs_dirempty (struct node *dp, struct protid *cred)
{
  return dp->dn->u.dir.entries == 0;
}

error_t
diskfs_get_directs (struct node *dp, int entry, int n,
		    char **data, u_int *datacnt,
		    vm_size_t bufsiz, int *amt)
{
  struct tmpfs_dirent *d;
  struct dirent *entp;
  int i;

  assert (offsetof (struct tmpfs_dirent, name)
	  >= offsetof (struct dirent, d_name));

  if (bufsiz == 0)
    bufsiz = dp->dn_stat.st_size;
  if (bufsiz > *datacnt)
    {
      *data = mmap (0, bufsiz, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
      if (*data == MAP_FAILED)
	return ENOMEM;
    }

  entp = *data;
  entp->d_fileno = dp->dn_stat.st_ino;
  entp->d_type = DT_DIR;
  entp->d_namlen = 1;
  entp->d_name[0] = '.';
  entp->d_name[1] = '\0';
  entp->d_reclen = (&entp->d_name[2] - (char *) entp + 7) & ~7;
  entp = (void *) entp + entp->d_reclen;
  entp->d_fileno = (ino_t) dp->dn->u.dir.dotdot;
  entp->d_type = DT_DIR;
  entp->d_namlen = 2;
  entp->d_name[0] = '.';
  entp->d_name[1] = '.';
  entp->d_name[2] = '\0';
  entp->d_reclen = (&entp->d_name[3] - (char *) entp + 7) & ~7;
  entp = (void *) entp + entp->d_reclen;

  d = dp->dn->u.dir.entries;
  for (i = 2; i < entry && d != 0; ++i)
    d = d->next;

  for (i = 2; d != 0; d = d->next)
    {
      if ((char *) entp - *data >= bufsiz || (n >= 0 && ++i > n))
	break;
      entp->d_fileno = (ino_t) d->dn;
      entp->d_type = DT_UNKNOWN;
      entp->d_namlen = d->namelen;
      memcpy (entp->d_name, d->name, d->namelen + 1);
      entp->d_reclen = ((&entp->d_name[d->namelen + 1] - (char *) entp + 7)
			& ~7);
      entp = (void *) entp + entp->d_reclen;
    }

  *datacnt = (char *) entp - *data;
  *amt = i;

  return 0;
}


struct dirstat
{
  struct tmpfs_dirent **prevp;
};
const size_t diskfs_dirstat_size = sizeof (struct dirstat);

void
diskfs_null_dirstat (struct dirstat *ds)
{
  ds->prevp = 0;
}

error_t
diskfs_drop_dirstat (struct node *dp, struct dirstat *ds)
{
  /* No need to clear the pointers.  */
  return 0;
}

error_t
diskfs_lookup_hard (struct node *dp,
		    const char *name, enum lookup_type type,
		    struct node **np, struct dirstat *ds,
		    struct protid *cred)
{
  const size_t namelen = strlen (name);
  struct tmpfs_dirent *d, **prevp;

  if (namelen == 1 && name[0] == '.')
    {
      if (np != 0)
	{
	  *np = dp;
	  diskfs_nref (dp);
	}
      return 0;
    }
  if (namelen == 2 && name[0] == '.' && name[1] == '.')
    {
      struct disknode *ddnp = dp->dn->dir.dotdot;
      assert (np != 0);
      if (ddnp == 0)		/* root directory */
	return EAGAIN;
      switch (type)
	{
	case LOOKUP|SPEC_DOTDOT:
	  diskfs_nput (dp);
	default:
	  diskfs_nref (ddnp);
	  mutex_lock (&ddnp->lock);
	case REMOVE|SPEC_DOTDOT:
	case RENAME|SPEC_DOTDOT:
	  *np = ddnp;
	}
      return 0;
    }

  for (d = *(prevp = &dp->dn->u.dir.entries); d != 0;
       d = *(prevp = &d->next))
    if (d->namelen == namelen && !memcmp (d->name, name, namelen))
      {
	ds->prevp = prevp;
	return diskfs_cached_lookup ((ino_t) d->dn, np);
      }

  ds->prevp = prevp;
  return ENOENT;
}


error_t
diskfs_direnter_hard (struct node *dp, const char *name,
		      struct node *np, struct dirstat *ds,
		      struct protid *cred)
{
  const size_t namelen = strlen (name);
  const size_t entsize = offsetof (struct tmpfs_dirent, name) + namelen + 1;
  struct tmpfs_dirent *new;

  if (round_page (tmpfs_space_used + entsize) > tmpfs_page_limit)
    return ENOSPC;

  new = malloc (entsize);
  if (new == 0)
    return ENOSPC;

  new->next = 0;
  new->dn = np->dn;
  new->namelen = namelen;
  memcpy (new->name, name, namelen + 1);
  *ds->prevp = new;

  dp->dn_stat.st_size += entsize;
  adjust_used (entsize);

  dp->dn_stat.st_blocks = ((sizeof *dp->dn + dp->dn->translen
			    + dp->dn_stat.st_size + 511)
			   / 512);
  return 0;
}

error_t
diskfs_dirrewrite_hard (struct node *dp, struct node *np,
			struct dirstat *ds)
{
  (*ds->prevp)->dn = np->dn;
  return 0;
}

error_t
diskfs_dirremove_hard (struct node *dp, struct dirstat *ds)
{
  struct tmpfs_dirent *d = *ds->prevp;
  const size_t entsize = &d->name[d->namelen + 1] - (char *) d;

  *ds->prevp = d->next;

  if (dp->dirmod_reqs != 0)
    diskfs_notice_dirchange (dp, DIR_CHANGED_UNLINK, d->name);

  free (d);

  adjust_used (-entsize);
  dp->dn_stat.st_size -= entsize;
  dp->dn_stat.st_blocks = ((sizeof *dp->dn + dp->dn->translen
			    + dp->dn_stat.st_size + 511)
			   / 512);

  return 0;
}