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
|
/* This file contains directory entry related file system call handlers.
*
* The entry points into this file are:
* do_create perform the CREATE file system call
* do_mkdir perform the MKDIR file system call
* do_unlink perform the UNLINK file system call
* do_rmdir perform the RMDIR file system call
* do_rename perform the RENAME file system call
*
* Created:
* April 2009 (D.C. van Moolenbroek)
*/
#include "inc.h"
#include <fcntl.h>
/*===========================================================================*
* do_create *
*===========================================================================*/
int do_create(ino_t dir_nr, char *name, mode_t mode, uid_t uid, gid_t gid,
struct fsdriver_node *node)
{
/* Create a new file.
*/
char path[PATH_MAX];
struct inode *parent, *ino;
struct sffs_attr attr;
sffs_file_t handle;
int r;
/* We cannot create files on a read-only file system. */
if (read_only)
return EROFS;
if ((parent = find_inode(dir_nr)) == NULL)
return EINVAL;
if ((r = verify_dentry(parent, name, path, &ino)) != OK)
return r;
/* Are we going to need a new inode upon success?
* Then make sure there is one available before trying anything.
*/
if (ino == NULL || ino->i_ref > 1 || HAS_CHILDREN(ino)) {
if (!have_free_inode()) {
if (ino != NULL)
put_inode(ino);
return ENFILE;
}
}
/* Perform the actual create call. */
r = sffs_table->t_open(path, O_CREAT | O_EXCL | O_RDWR, mode, &handle);
if (r != OK) {
/* Let's not try to be too clever with error codes here. If something
* is wrong with the directory, we'll find out later anyway.
*/
if (ino != NULL)
put_inode(ino);
return r;
}
/* Get the created file's attributes. */
attr.a_mask = SFFS_ATTR_MODE | SFFS_ATTR_SIZE;
r = sffs_table->t_getattr(path, &attr);
/* If this fails, or returns a directory, we have a problem. This
* scenario is in fact possible with race conditions.
* Simulate a close and return a somewhat appropriate error.
*/
if (r != OK || S_ISDIR(attr.a_mode)) {
printf("%s: lost file after creation!\n", sffs_name);
sffs_table->t_close(handle);
if (ino != NULL) {
del_dentry(ino);
put_inode(ino);
}
return (r == OK) ? EEXIST : r;
}
/* We do assume that the underlying open(O_CREAT|O_EXCL) call did its job.
* If we previousy found an inode, get rid of it now. It's old.
*/
if (ino != NULL) {
del_dentry(ino);
put_inode(ino);
}
/* Associate the open file handle with an inode, and reply with its details.
*/
ino = get_free_inode();
assert(ino != NULL); /* we checked before whether we had a free one */
ino->i_file = handle;
ino->i_flags = I_HANDLE;
add_dentry(parent, name, ino);
node->fn_ino_nr = INODE_NR(ino);
node->fn_mode = get_mode(ino, attr.a_mode);
node->fn_size = attr.a_size;
node->fn_uid = sffs_params->p_uid;
node->fn_gid = sffs_params->p_gid;
node->fn_dev = NO_DEV;
return OK;
}
/*===========================================================================*
* do_mkdir *
*===========================================================================*/
int do_mkdir(ino_t dir_nr, char *name, mode_t mode, uid_t uid, gid_t gid)
{
/* Make a new directory.
*/
char path[PATH_MAX];
struct inode *parent, *ino;
int r;
/* We cannot create directories on a read-only file system. */
if (read_only)
return EROFS;
if ((parent = find_inode(dir_nr)) == NULL)
return EINVAL;
if ((r = verify_dentry(parent, name, path, &ino)) != OK)
return r;
/* Perform the actual mkdir call. */
r = sffs_table->t_mkdir(path, mode);
if (r != OK) {
if (ino != NULL)
put_inode(ino);
return r;
}
/* If we thought the new dentry already existed, it was apparently gone
* already. Delete it.
*/
if (ino != NULL) {
del_dentry(ino);
put_inode(ino);
}
return OK;
}
/*===========================================================================*
* force_remove *
*===========================================================================*/
static int force_remove(
char *path, /* path to file or directory */
int dir /* TRUE iff directory */
)
{
/* Remove a file or directory. Wrapper around unlink and rmdir that makes the
* target temporarily writable if the operation fails with an access denied
* error. On Windows hosts, read-only files or directories cannot be removed
* (even though they can be renamed). In general, the SFFS library follows the
* behavior of the host file system, but this case just confuses the hell out
* of the MINIX userland..
*/
struct sffs_attr attr;
int r, r2;
/* First try to remove the target. */
if (dir)
r = sffs_table->t_rmdir(path);
else
r = sffs_table->t_unlink(path);
if (r != EACCES) return r;
/* If this fails with an access error, retrieve the target's mode. */
attr.a_mask = SFFS_ATTR_MODE;
r2 = sffs_table->t_getattr(path, &attr);
if (r2 != OK || (attr.a_mode & S_IWUSR)) return r;
/* If the target is not writable, temporarily set it to writable. */
attr.a_mode |= S_IWUSR;
r2 = sffs_table->t_setattr(path, &attr);
if (r2 != OK) return r;
/* Then try the original operation again. */
if (dir)
r = sffs_table->t_rmdir(path);
else
r = sffs_table->t_unlink(path);
if (r == OK) return r;
/* If the operation still fails, unset the writable bit again. */
attr.a_mode &= ~S_IWUSR;
sffs_table->t_setattr(path, &attr);
return r;
}
/*===========================================================================*
* do_unlink *
*===========================================================================*/
int do_unlink(ino_t dir_nr, char *name, int call)
{
/* Delete a file.
*/
char path[PATH_MAX];
struct inode *parent, *ino;
int r;
/* We cannot delete files on a read-only file system. */
if (read_only)
return EROFS;
if ((parent = find_inode(dir_nr)) == NULL)
return EINVAL;
if ((r = verify_dentry(parent, name, path, &ino)) != OK)
return r;
/* Perform the unlink call. */
r = force_remove(path, FALSE /*dir*/);
if (r != OK) {
if (ino != NULL)
put_inode(ino);
return r;
}
/* If a dentry existed for this name, it is gone now. */
if (ino != NULL) {
del_dentry(ino);
put_inode(ino);
}
return OK;
}
/*===========================================================================*
* do_rmdir *
*===========================================================================*/
int do_rmdir(ino_t dir_nr, char *name, int call)
{
/* Remove an empty directory.
*/
char path[PATH_MAX];
struct inode *parent, *ino;
int r;
/* We cannot remove directories on a read-only file system. */
if (read_only)
return EROFS;
if ((parent = find_inode(dir_nr)) == NULL)
return EINVAL;
if ((r = verify_dentry(parent, name, path, &ino)) != OK)
return r;
/* Perform the rmdir call. */
r = force_remove(path, TRUE /*dir*/);
if (r != OK) {
if (ino != NULL)
put_inode(ino);
return r;
}
/* If a dentry existed for this name, it is gone now. */
if (ino != NULL) {
del_dentry(ino);
put_inode(ino);
}
return OK;
}
/*===========================================================================*
* do_rename *
*===========================================================================*/
int do_rename(ino_t old_dir_nr, char *old_name, ino_t new_dir_nr,
char *new_name)
{
/* Rename a file or directory.
*/
char old_path[PATH_MAX], new_path[PATH_MAX];
struct inode *old_parent, *new_parent;
struct inode *old_ino, *new_ino;
int r;
/* We cannot do rename on a read-only file system. */
if (read_only)
return EROFS;
/* Get possibly preexisting inodes for the old and new paths. */
if ((old_parent = find_inode(old_dir_nr)) == NULL ||
(new_parent = find_inode(new_dir_nr)) == NULL)
return EINVAL;
if ((r = verify_dentry(old_parent, old_name, old_path, &old_ino)) != OK)
return r;
if ((r = verify_dentry(new_parent, new_name, new_path, &new_ino)) != OK) {
if (old_ino != NULL)
put_inode(old_ino);
return r;
}
/* Perform the actual rename call. */
r = sffs_table->t_rename(old_path, new_path);
/* If we failed, or if we have nothing further to do: both inodes are
* NULL, or they both refer to the same file.
*/
if (r != OK || old_ino == new_ino) {
if (old_ino != NULL) put_inode(old_ino);
if (new_ino != NULL) put_inode(new_ino);
return r;
}
/* If the new dentry already existed, it has now been overwritten.
* Delete the associated inode if we had found one.
*/
if (new_ino != NULL) {
del_dentry(new_ino);
put_inode(new_ino);
}
/* If the old dentry existed, rename it accordingly. */
if (old_ino != NULL) {
del_dentry(old_ino);
add_dentry(new_parent, new_name, old_ino);
put_inode(old_ino);
}
return OK;
}
|