Blame view

fs/hostfs/hostfs_user.c 7.33 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
f1adc05e7   Jeff Dike   uml: hostfs style...
2
   * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
3
4
   * Licensed under the GPL
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
5
  #include <stdio.h>
84b3db04c   Jeff Dike   uml: fix hostfs s...
6
7
  #include <stddef.h>
  #include <unistd.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
8
9
  #include <dirent.h>
  #include <errno.h>
84b3db04c   Jeff Dike   uml: fix hostfs s...
10
  #include <fcntl.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
11
12
13
  #include <string.h>
  #include <sys/stat.h>
  #include <sys/time.h>
84b3db04c   Jeff Dike   uml: fix hostfs s...
14
  #include <sys/types.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
15
  #include <sys/vfs.h>
9a423bb6e   Miklos Szeredi   hostfs: support r...
16
  #include <sys/syscall.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
  #include "hostfs.h"
84b3db04c   Jeff Dike   uml: fix hostfs s...
18
  #include <utime.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
19

39b743c61   Al Viro   switch stat_file(...
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  static void stat64_to_hostfs(const struct stat64 *buf, struct hostfs_stat *p)
  {
  	p->ino = buf->st_ino;
  	p->mode = buf->st_mode;
  	p->nlink = buf->st_nlink;
  	p->uid = buf->st_uid;
  	p->gid = buf->st_gid;
  	p->size = buf->st_size;
  	p->atime.tv_sec = buf->st_atime;
  	p->atime.tv_nsec = 0;
  	p->ctime.tv_sec = buf->st_ctime;
  	p->ctime.tv_nsec = 0;
  	p->mtime.tv_sec = buf->st_mtime;
  	p->mtime.tv_nsec = 0;
  	p->blksize = buf->st_blksize;
  	p->blocks = buf->st_blocks;
  	p->maj = os_major(buf->st_rdev);
  	p->min = os_minor(buf->st_rdev);
  }
  
  int stat_file(const char *path, struct hostfs_stat *p, int fd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
42
  {
  	struct stat64 buf;
84b3db04c   Jeff Dike   uml: fix hostfs s...
43
  	if (fd >= 0) {
5822b7fac   Alberto Bertogli   uml: make hostfs_...
44
  		if (fstat64(fd, &buf) < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
45
  			return -errno;
84b3db04c   Jeff Dike   uml: fix hostfs s...
46
  	} else if (lstat64(path, &buf) < 0) {
f1adc05e7   Jeff Dike   uml: hostfs style...
47
  		return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
48
  	}
39b743c61   Al Viro   switch stat_file(...
49
  	stat64_to_hostfs(&buf, p);
f1adc05e7   Jeff Dike   uml: hostfs style...
50
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
53
54
  int access_file(char *path, int r, int w, int x)
  {
  	int mode = 0;
84b3db04c   Jeff Dike   uml: fix hostfs s...
55
56
57
58
59
60
61
  	if (r)
  		mode = R_OK;
  	if (w)
  		mode |= W_OK;
  	if (x)
  		mode |= X_OK;
  	if (access(path, mode) != 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
62
63
  		return -errno;
  	else return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
65
66
67
68
  }
  
  int open_file(char *path, int r, int w, int append)
  {
  	int mode = 0, fd;
84b3db04c   Jeff Dike   uml: fix hostfs s...
69
  	if (r && !w)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
70
  		mode = O_RDONLY;
84b3db04c   Jeff Dike   uml: fix hostfs s...
71
  	else if (!r && w)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
72
  		mode = O_WRONLY;
84b3db04c   Jeff Dike   uml: fix hostfs s...
73
  	else if (r && w)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
74
75
  		mode = O_RDWR;
  	else panic("Impossible mode in open_file");
84b3db04c   Jeff Dike   uml: fix hostfs s...
76
  	if (append)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
77
78
  		mode |= O_APPEND;
  	fd = open64(path, mode);
84b3db04c   Jeff Dike   uml: fix hostfs s...
79
  	if (fd < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
80
81
  		return -errno;
  	else return fd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
84
85
86
87
88
89
  }
  
  void *open_dir(char *path, int *err_out)
  {
  	DIR *dir;
  
  	dir = opendir(path);
  	*err_out = errno;
c5c6dd4e2   Richard Weinberger   hostfs: code clea...
90

f1adc05e7   Jeff Dike   uml: hostfs style...
91
  	return dir;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
  }
0c9bd6365   Richard Weinberger   um: hostfs: Reduc...
93
94
95
96
97
98
99
100
  void seek_dir(void *stream, unsigned long long pos)
  {
  	DIR *dir = stream;
  
  	seekdir(dir, pos);
  }
  
  char *read_dir(void *stream, unsigned long long *pos_out,
3ee6bd8e8   Geert Uytterhoeven   uml/hostfs: Propa...
101
102
  	       unsigned long long *ino_out, int *len_out,
  	       unsigned int *type_out)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
103
104
105
  {
  	DIR *dir = stream;
  	struct dirent *ent;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
106
  	ent = readdir(dir);
84b3db04c   Jeff Dike   uml: fix hostfs s...
107
  	if (ent == NULL)
f1adc05e7   Jeff Dike   uml: hostfs style...
108
  		return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
  	*len_out = strlen(ent->d_name);
  	*ino_out = ent->d_ino;
3ee6bd8e8   Geert Uytterhoeven   uml/hostfs: Propa...
111
  	*type_out = ent->d_type;
0c9bd6365   Richard Weinberger   um: hostfs: Reduc...
112
  	*pos_out = ent->d_off;
f1adc05e7   Jeff Dike   uml: hostfs style...
113
  	return ent->d_name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
114
115
116
117
118
119
120
  }
  
  int read_file(int fd, unsigned long long *offset, char *buf, int len)
  {
  	int n;
  
  	n = pread64(fd, buf, len, *offset);
84b3db04c   Jeff Dike   uml: fix hostfs s...
121
  	if (n < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
122
  		return -errno;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
123
  	*offset += n;
f1adc05e7   Jeff Dike   uml: hostfs style...
124
  	return n;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
125
126
127
128
129
130
131
  }
  
  int write_file(int fd, unsigned long long *offset, const char *buf, int len)
  {
  	int n;
  
  	n = pwrite64(fd, buf, len, *offset);
84b3db04c   Jeff Dike   uml: fix hostfs s...
132
  	if (n < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
133
  		return -errno;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134
  	*offset += n;
f1adc05e7   Jeff Dike   uml: hostfs style...
135
  	return n;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
136
137
138
139
140
141
142
  }
  
  int lseek_file(int fd, long long offset, int whence)
  {
  	int ret;
  
  	ret = lseek64(fd, offset, whence);
84b3db04c   Jeff Dike   uml: fix hostfs s...
143
  	if (ret < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
144
145
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
146
  }
a2d76bd8f   Paolo 'Blaisorblade' Giarrusso   [PATCH] uml: impl...
147
148
149
150
151
152
153
154
155
156
157
158
  int fsync_file(int fd, int datasync)
  {
  	int ret;
  	if (datasync)
  		ret = fdatasync(fd);
  	else
  		ret = fsync(fd);
  
  	if (ret < 0)
  		return -errno;
  	return 0;
  }
f8ad850f1   Al Viro   try to get rid of...
159
160
161
162
  int replace_file(int oldfd, int fd)
  {
  	return dup2(oldfd, fd);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
163
164
165
166
167
168
169
170
171
  void close_file(void *stream)
  {
  	close(*((int *) stream));
  }
  
  void close_dir(void *stream)
  {
  	closedir(stream);
  }
b98b91029   Richard Weinberger   hostfs: No need t...
172
  int file_create(char *name, int mode)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
173
  {
b98b91029   Richard Weinberger   hostfs: No need t...
174
  	int fd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
175
  	fd = open64(name, O_CREAT | O_RDWR, mode);
84b3db04c   Jeff Dike   uml: fix hostfs s...
176
  	if (fd < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
177
178
  		return -errno;
  	return fd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
179
  }
5822b7fac   Alberto Bertogli   uml: make hostfs_...
180
  int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
181
  {
39b743c61   Al Viro   switch stat_file(...
182
  	struct hostfs_stat st;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
183
  	struct timeval times[2];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
184
  	int err, ma;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
185
186
187
  	if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
  		if (fd >= 0) {
  			if (fchmod(fd, attrs->ia_mode) != 0)
c5c6dd4e2   Richard Weinberger   hostfs: code clea...
188
  				return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
189
  		} else if (chmod(file, attrs->ia_mode) != 0) {
f1adc05e7   Jeff Dike   uml: hostfs style...
190
  			return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
191
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192
  	}
5822b7fac   Alberto Bertogli   uml: make hostfs_...
193
194
195
  	if (attrs->ia_valid & HOSTFS_ATTR_UID) {
  		if (fd >= 0) {
  			if (fchown(fd, attrs->ia_uid, -1))
f1adc05e7   Jeff Dike   uml: hostfs style...
196
  				return -errno;
84b3db04c   Jeff Dike   uml: fix hostfs s...
197
  		} else if (chown(file, attrs->ia_uid, -1)) {
f1adc05e7   Jeff Dike   uml: hostfs style...
198
  			return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
199
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
200
  	}
5822b7fac   Alberto Bertogli   uml: make hostfs_...
201
202
203
  	if (attrs->ia_valid & HOSTFS_ATTR_GID) {
  		if (fd >= 0) {
  			if (fchown(fd, -1, attrs->ia_gid))
f1adc05e7   Jeff Dike   uml: hostfs style...
204
  				return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
205
  		} else if (chown(file, -1, attrs->ia_gid)) {
f1adc05e7   Jeff Dike   uml: hostfs style...
206
  			return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
207
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
208
  	}
5822b7fac   Alberto Bertogli   uml: make hostfs_...
209
210
211
  	if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
  		if (fd >= 0) {
  			if (ftruncate(fd, attrs->ia_size))
f1adc05e7   Jeff Dike   uml: hostfs style...
212
  				return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
213
  		} else if (truncate(file, attrs->ia_size)) {
f1adc05e7   Jeff Dike   uml: hostfs style...
214
  			return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
215
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
  	}
5822b7fac   Alberto Bertogli   uml: make hostfs_...
217

84b3db04c   Jeff Dike   uml: fix hostfs s...
218
219
  	/*
  	 * Update accessed and/or modified time, in two parts: first set
5822b7fac   Alberto Bertogli   uml: make hostfs_...
220
  	 * times according to the changes to perform, and then call futimes()
84b3db04c   Jeff Dike   uml: fix hostfs s...
221
222
  	 * or utimes() to apply them.
  	 */
5822b7fac   Alberto Bertogli   uml: make hostfs_...
223
224
  	ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET);
  	if (attrs->ia_valid & ma) {
39b743c61   Al Viro   switch stat_file(...
225
  		err = stat_file(file, &st, fd);
5822b7fac   Alberto Bertogli   uml: make hostfs_...
226
227
  		if (err != 0)
  			return err;
39b743c61   Al Viro   switch stat_file(...
228
229
230
231
  		times[0].tv_sec = st.atime.tv_sec;
  		times[0].tv_usec = st.atime.tv_nsec / 1000;
  		times[1].tv_sec = st.mtime.tv_sec;
  		times[1].tv_usec = st.mtime.tv_nsec / 1000;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
232
233
234
  
  		if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) {
  			times[0].tv_sec = attrs->ia_atime.tv_sec;
d7b88513c   Dominique Quatravaux   uml: fix hostfs t...
235
  			times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
236
  		}
5822b7fac   Alberto Bertogli   uml: make hostfs_...
237
238
  		if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) {
  			times[1].tv_sec = attrs->ia_mtime.tv_sec;
d7b88513c   Dominique Quatravaux   uml: fix hostfs t...
239
  			times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
240
241
242
243
  		}
  
  		if (fd >= 0) {
  			if (futimes(fd, times) != 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
244
  				return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
245
  		} else if (utimes(file, times) != 0) {
f1adc05e7   Jeff Dike   uml: hostfs style...
246
  			return -errno;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
247
248
  		}
  	}
5822b7fac   Alberto Bertogli   uml: make hostfs_...
249

baabd156e   Jeff Dike   uml: remove unnee...
250
  	/* Note: ctime is not handled */
84b3db04c   Jeff Dike   uml: fix hostfs s...
251
  	if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) {
39b743c61   Al Viro   switch stat_file(...
252
253
254
  		err = stat_file(file, &st, fd);
  		attrs->ia_atime = st.atime;
  		attrs->ia_mtime = st.mtime;
84b3db04c   Jeff Dike   uml: fix hostfs s...
255
  		if (err != 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
256
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
257
  	}
f1adc05e7   Jeff Dike   uml: hostfs style...
258
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
259
260
261
262
263
264
265
  }
  
  int make_symlink(const char *from, const char *to)
  {
  	int err;
  
  	err = symlink(to, from);
84b3db04c   Jeff Dike   uml: fix hostfs s...
266
  	if (err)
f1adc05e7   Jeff Dike   uml: hostfs style...
267
268
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
269
270
271
272
273
274
275
  }
  
  int unlink_file(const char *file)
  {
  	int err;
  
  	err = unlink(file);
84b3db04c   Jeff Dike   uml: fix hostfs s...
276
  	if (err)
f1adc05e7   Jeff Dike   uml: hostfs style...
277
278
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
279
280
281
282
283
284
285
  }
  
  int do_mkdir(const char *file, int mode)
  {
  	int err;
  
  	err = mkdir(file, mode);
84b3db04c   Jeff Dike   uml: fix hostfs s...
286
  	if (err)
f1adc05e7   Jeff Dike   uml: hostfs style...
287
288
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
289
290
291
292
293
294
295
  }
  
  int do_rmdir(const char *file)
  {
  	int err;
  
  	err = rmdir(file);
84b3db04c   Jeff Dike   uml: fix hostfs s...
296
  	if (err)
f1adc05e7   Jeff Dike   uml: hostfs style...
297
298
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
299
  }
88f6cd0c3   Johannes Stezenbach   [PATCH] uml: fix ...
300
  int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
301
302
  {
  	int err;
005a59ec7   Al Viro   Deal with missing...
303
  	err = mknod(file, mode, os_makedev(major, minor));
84b3db04c   Jeff Dike   uml: fix hostfs s...
304
  	if (err)
f1adc05e7   Jeff Dike   uml: hostfs style...
305
306
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
307
308
309
310
311
312
313
  }
  
  int link_file(const char *to, const char *from)
  {
  	int err;
  
  	err = link(to, from);
84b3db04c   Jeff Dike   uml: fix hostfs s...
314
  	if (err)
f1adc05e7   Jeff Dike   uml: hostfs style...
315
316
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
317
  }
ea7e743e4   WANG Cong   hostfs: fix a dup...
318
  int hostfs_do_readlink(char *file, char *buf, int size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
319
320
321
322
  {
  	int n;
  
  	n = readlink(file, buf, size);
84b3db04c   Jeff Dike   uml: fix hostfs s...
323
  	if (n < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
324
  		return -errno;
84b3db04c   Jeff Dike   uml: fix hostfs s...
325
  	if (n < size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326
  		buf[n] = '\0';
f1adc05e7   Jeff Dike   uml: hostfs style...
327
  	return n;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
329
330
331
332
333
334
  }
  
  int rename_file(char *from, char *to)
  {
  	int err;
  
  	err = rename(from, to);
84b3db04c   Jeff Dike   uml: fix hostfs s...
335
  	if (err < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
336
337
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
338
  }
9a423bb6e   Miklos Szeredi   hostfs: support r...
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
  int rename2_file(char *from, char *to, unsigned int flags)
  {
  	int err;
  
  #ifndef SYS_renameat2
  #  ifdef __x86_64__
  #    define SYS_renameat2 316
  #  endif
  #  ifdef __i386__
  #    define SYS_renameat2 353
  #  endif
  #endif
  
  #ifdef SYS_renameat2
  	err = syscall(SYS_renameat2, AT_FDCWD, from, AT_FDCWD, to, flags);
  	if (err < 0) {
  		if (errno != ENOSYS)
  			return -errno;
  		else
  			return -EINVAL;
  	}
  	return 0;
  #else
  	return -EINVAL;
  #endif
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
365
366
367
  int do_statfs(char *root, long *bsize_out, long long *blocks_out,
  	      long long *bfree_out, long long *bavail_out,
  	      long long *files_out, long long *ffree_out,
1b627d577   Richard Weinberger   hostfs: fix UML c...
368
  	      void *fsid_out, int fsid_size, long *namelen_out)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
369
370
371
372
373
  {
  	struct statfs64 buf;
  	int err;
  
  	err = statfs64(root, &buf);
84b3db04c   Jeff Dike   uml: fix hostfs s...
374
  	if (err < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
375
  		return -errno;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
376
377
378
379
380
381
382
383
384
385
  	*bsize_out = buf.f_bsize;
  	*blocks_out = buf.f_blocks;
  	*bfree_out = buf.f_bfree;
  	*bavail_out = buf.f_bavail;
  	*files_out = buf.f_files;
  	*ffree_out = buf.f_ffree;
  	memcpy(fsid_out, &buf.f_fsid,
  	       sizeof(buf.f_fsid) > fsid_size ? fsid_size :
  	       sizeof(buf.f_fsid));
  	*namelen_out = buf.f_namelen;
1b627d577   Richard Weinberger   hostfs: fix UML c...
386

f1adc05e7   Jeff Dike   uml: hostfs style...
387
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
388
  }