Blame view

fs/hostfs/hostfs_user.c 7.09 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
16
  #include <sys/vfs.h>
  #include "hostfs.h"
84b3db04c   Jeff Dike   uml: fix hostfs s...
17
  #include "os.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
  #include "user.h"
84b3db04c   Jeff Dike   uml: fix hostfs s...
19
  #include <utime.h>
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
20

39b743c61   Al Viro   switch stat_file(...
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  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
42
43
  {
  	struct stat64 buf;
84b3db04c   Jeff Dike   uml: fix hostfs s...
44
  	if (fd >= 0) {
5822b7fac   Alberto Bertogli   uml: make hostfs_...
45
  		if (fstat64(fd, &buf) < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
46
  			return -errno;
84b3db04c   Jeff Dike   uml: fix hostfs s...
47
  	} else if (lstat64(path, &buf) < 0) {
f1adc05e7   Jeff Dike   uml: hostfs style...
48
  		return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
49
  	}
39b743c61   Al Viro   switch stat_file(...
50
  	stat64_to_hostfs(&buf, p);
f1adc05e7   Jeff Dike   uml: hostfs style...
51
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
54
55
  int access_file(char *path, int r, int w, int x)
  {
  	int mode = 0;
84b3db04c   Jeff Dike   uml: fix hostfs s...
56
57
58
59
60
61
62
  	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...
63
64
  		return -errno;
  	else return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
66
67
68
69
  }
  
  int open_file(char *path, int r, int w, int append)
  {
  	int mode = 0, fd;
84b3db04c   Jeff Dike   uml: fix hostfs s...
70
  	if (r && !w)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
71
  		mode = O_RDONLY;
84b3db04c   Jeff Dike   uml: fix hostfs s...
72
  	else if (!r && w)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
73
  		mode = O_WRONLY;
84b3db04c   Jeff Dike   uml: fix hostfs s...
74
  	else if (r && w)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
75
76
  		mode = O_RDWR;
  	else panic("Impossible mode in open_file");
84b3db04c   Jeff Dike   uml: fix hostfs s...
77
  	if (append)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
78
79
  		mode |= O_APPEND;
  	fd = open64(path, mode);
84b3db04c   Jeff Dike   uml: fix hostfs s...
80
  	if (fd < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
81
82
  		return -errno;
  	else return fd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
83
84
85
86
87
88
89
90
  }
  
  void *open_dir(char *path, int *err_out)
  {
  	DIR *dir;
  
  	dir = opendir(path);
  	*err_out = errno;
c5c6dd4e2   Richard Weinberger   hostfs: code clea...
91

f1adc05e7   Jeff Dike   uml: hostfs style...
92
  	return dir;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
93
94
95
96
97
98
99
100
101
102
  }
  
  char *read_dir(void *stream, unsigned long long *pos,
  	       unsigned long long *ino_out, int *len_out)
  {
  	DIR *dir = stream;
  	struct dirent *ent;
  
  	seekdir(dir, *pos);
  	ent = readdir(dir);
84b3db04c   Jeff Dike   uml: fix hostfs s...
103
  	if (ent == NULL)
f1adc05e7   Jeff Dike   uml: hostfs style...
104
  		return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
105
106
107
  	*len_out = strlen(ent->d_name);
  	*ino_out = ent->d_ino;
  	*pos = telldir(dir);
f1adc05e7   Jeff Dike   uml: hostfs style...
108
  	return ent->d_name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
109
110
111
112
113
114
115
  }
  
  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...
116
  	if (n < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
117
  		return -errno;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
  	*offset += n;
f1adc05e7   Jeff Dike   uml: hostfs style...
119
  	return n;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
120
121
122
123
124
125
126
  }
  
  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...
127
  	if (n < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
128
  		return -errno;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
129
  	*offset += n;
f1adc05e7   Jeff Dike   uml: hostfs style...
130
  	return n;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
131
132
133
134
135
136
137
  }
  
  int lseek_file(int fd, long long offset, int whence)
  {
  	int ret;
  
  	ret = lseek64(fd, offset, whence);
84b3db04c   Jeff Dike   uml: fix hostfs s...
138
  	if (ret < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
139
140
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
141
  }
a2d76bd8f   Paolo 'Blaisorblade' Giarrusso   [PATCH] uml: impl...
142
143
144
145
146
147
148
149
150
151
152
153
  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...
154
155
156
157
  int replace_file(int oldfd, int fd)
  {
  	return dup2(oldfd, fd);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  void close_file(void *stream)
  {
  	close(*((int *) stream));
  }
  
  void close_dir(void *stream)
  {
  	closedir(stream);
  }
  
  int file_create(char *name, int ur, int uw, int ux, int gr,
  		int gw, int gx, int or, int ow, int ox)
  {
  	int mode, fd;
  
  	mode = 0;
  	mode |= ur ? S_IRUSR : 0;
  	mode |= uw ? S_IWUSR : 0;
  	mode |= ux ? S_IXUSR : 0;
  	mode |= gr ? S_IRGRP : 0;
  	mode |= gw ? S_IWGRP : 0;
  	mode |= gx ? S_IXGRP : 0;
  	mode |= or ? S_IROTH : 0;
  	mode |= ow ? S_IWOTH : 0;
  	mode |= ox ? S_IXOTH : 0;
  	fd = open64(name, O_CREAT | O_RDWR, mode);
84b3db04c   Jeff Dike   uml: fix hostfs s...
184
  	if (fd < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
185
186
  		return -errno;
  	return fd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
187
  }
5822b7fac   Alberto Bertogli   uml: make hostfs_...
188
  int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
189
  {
39b743c61   Al Viro   switch stat_file(...
190
  	struct hostfs_stat st;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
191
  	struct timeval times[2];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
192
  	int err, ma;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
193
194
195
  	if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
  		if (fd >= 0) {
  			if (fchmod(fd, attrs->ia_mode) != 0)
c5c6dd4e2   Richard Weinberger   hostfs: code clea...
196
  				return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
197
  		} else if (chmod(file, attrs->ia_mode) != 0) {
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_UID) {
  		if (fd >= 0) {
  			if (fchown(fd, attrs->ia_uid, -1))
f1adc05e7   Jeff Dike   uml: hostfs style...
204
  				return -errno;
84b3db04c   Jeff Dike   uml: fix hostfs s...
205
  		} else if (chown(file, attrs->ia_uid, -1)) {
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_GID) {
  		if (fd >= 0) {
  			if (fchown(fd, -1, attrs->ia_gid))
f1adc05e7   Jeff Dike   uml: hostfs style...
212
  				return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
213
  		} else if (chown(file, -1, attrs->ia_gid)) {
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
218
219
  	if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
  		if (fd >= 0) {
  			if (ftruncate(fd, attrs->ia_size))
f1adc05e7   Jeff Dike   uml: hostfs style...
220
  				return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
221
  		} else if (truncate(file, attrs->ia_size)) {
f1adc05e7   Jeff Dike   uml: hostfs style...
222
  			return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
223
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
224
  	}
5822b7fac   Alberto Bertogli   uml: make hostfs_...
225

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

baabd156e   Jeff Dike   uml: remove unnee...
258
  	/* Note: ctime is not handled */
84b3db04c   Jeff Dike   uml: fix hostfs s...
259
  	if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) {
39b743c61   Al Viro   switch stat_file(...
260
261
262
  		err = stat_file(file, &st, fd);
  		attrs->ia_atime = st.atime;
  		attrs->ia_mtime = st.mtime;
84b3db04c   Jeff Dike   uml: fix hostfs s...
263
  		if (err != 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
264
  			return err;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
265
  	}
f1adc05e7   Jeff Dike   uml: hostfs style...
266
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
267
268
269
270
271
272
273
  }
  
  int make_symlink(const char *from, const char *to)
  {
  	int err;
  
  	err = symlink(to, from);
84b3db04c   Jeff Dike   uml: fix hostfs s...
274
  	if (err)
f1adc05e7   Jeff Dike   uml: hostfs style...
275
276
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
277
278
279
280
281
282
283
  }
  
  int unlink_file(const char *file)
  {
  	int err;
  
  	err = unlink(file);
84b3db04c   Jeff Dike   uml: fix hostfs s...
284
  	if (err)
f1adc05e7   Jeff Dike   uml: hostfs style...
285
286
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
287
288
289
290
291
292
293
  }
  
  int do_mkdir(const char *file, int mode)
  {
  	int err;
  
  	err = mkdir(file, mode);
84b3db04c   Jeff Dike   uml: fix hostfs s...
294
  	if (err)
f1adc05e7   Jeff Dike   uml: hostfs style...
295
296
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
297
298
299
300
301
302
303
  }
  
  int do_rmdir(const char *file)
  {
  	int err;
  
  	err = rmdir(file);
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
  }
88f6cd0c3   Johannes Stezenbach   [PATCH] uml: fix ...
308
  int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
309
310
  {
  	int err;
005a59ec7   Al Viro   Deal with missing...
311
  	err = mknod(file, mode, os_makedev(major, minor));
84b3db04c   Jeff Dike   uml: fix hostfs s...
312
  	if (err)
f1adc05e7   Jeff Dike   uml: hostfs style...
313
314
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
315
316
317
318
319
320
321
  }
  
  int link_file(const char *to, const char *from)
  {
  	int err;
  
  	err = link(to, from);
84b3db04c   Jeff Dike   uml: fix hostfs s...
322
  	if (err)
f1adc05e7   Jeff Dike   uml: hostfs style...
323
324
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
325
  }
ea7e743e4   WANG Cong   hostfs: fix a dup...
326
  int hostfs_do_readlink(char *file, char *buf, int size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
327
328
329
330
  {
  	int n;
  
  	n = readlink(file, buf, size);
84b3db04c   Jeff Dike   uml: fix hostfs s...
331
  	if (n < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
332
  		return -errno;
84b3db04c   Jeff Dike   uml: fix hostfs s...
333
  	if (n < size)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
334
  		buf[n] = '\0';
f1adc05e7   Jeff Dike   uml: hostfs style...
335
  	return n;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
336
337
338
339
340
341
342
  }
  
  int rename_file(char *from, char *to)
  {
  	int err;
  
  	err = rename(from, to);
84b3db04c   Jeff Dike   uml: fix hostfs s...
343
  	if (err < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
344
345
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
346
347
348
349
350
  }
  
  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...
351
  	      void *fsid_out, int fsid_size, long *namelen_out)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
352
353
354
355
356
  {
  	struct statfs64 buf;
  	int err;
  
  	err = statfs64(root, &buf);
84b3db04c   Jeff Dike   uml: fix hostfs s...
357
  	if (err < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
358
  		return -errno;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
359
360
361
362
363
364
365
366
367
368
  	*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...
369

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