Blame view

fs/hostfs/hostfs_user.c 7.07 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"
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
93
94
95
96
97
98
99
100
101
  }
  
  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...
102
  	if (ent == NULL)
f1adc05e7   Jeff Dike   uml: hostfs style...
103
  		return NULL;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
105
106
  	*len_out = strlen(ent->d_name);
  	*ino_out = ent->d_ino;
  	*pos = telldir(dir);
f1adc05e7   Jeff Dike   uml: hostfs style...
107
  	return ent->d_name;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
108
109
110
111
112
113
114
  }
  
  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...
115
  	if (n < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
116
  		return -errno;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
117
  	*offset += n;
f1adc05e7   Jeff Dike   uml: hostfs style...
118
  	return n;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
120
121
122
123
124
125
  }
  
  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...
126
  	if (n < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
127
  		return -errno;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
  	*offset += n;
f1adc05e7   Jeff Dike   uml: hostfs style...
129
  	return n;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
130
131
132
133
134
135
136
  }
  
  int lseek_file(int fd, long long offset, int whence)
  {
  	int ret;
  
  	ret = lseek64(fd, offset, whence);
84b3db04c   Jeff Dike   uml: fix hostfs s...
137
  	if (ret < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
138
139
  		return -errno;
  	return 0;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
140
  }
a2d76bd8f   Paolo 'Blaisorblade' Giarrusso   [PATCH] uml: impl...
141
142
143
144
145
146
147
148
149
150
151
152
  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...
153
154
155
156
  int replace_file(int oldfd, int fd)
  {
  	return dup2(oldfd, fd);
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
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
  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...
183
  	if (fd < 0)
f1adc05e7   Jeff Dike   uml: hostfs style...
184
185
  		return -errno;
  	return fd;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
186
  }
5822b7fac   Alberto Bertogli   uml: make hostfs_...
187
  int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
188
  {
39b743c61   Al Viro   switch stat_file(...
189
  	struct hostfs_stat st;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
190
  	struct timeval times[2];
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
  	int err, ma;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
192
193
194
  	if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
  		if (fd >= 0) {
  			if (fchmod(fd, attrs->ia_mode) != 0)
c5c6dd4e2   Richard Weinberger   hostfs: code clea...
195
  				return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
196
  		} else if (chmod(file, attrs->ia_mode) != 0) {
f1adc05e7   Jeff Dike   uml: hostfs style...
197
  			return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
198
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
199
  	}
5822b7fac   Alberto Bertogli   uml: make hostfs_...
200
201
202
  	if (attrs->ia_valid & HOSTFS_ATTR_UID) {
  		if (fd >= 0) {
  			if (fchown(fd, attrs->ia_uid, -1))
f1adc05e7   Jeff Dike   uml: hostfs style...
203
  				return -errno;
84b3db04c   Jeff Dike   uml: fix hostfs s...
204
  		} else if (chown(file, attrs->ia_uid, -1)) {
f1adc05e7   Jeff Dike   uml: hostfs style...
205
  			return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
206
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
  	}
5822b7fac   Alberto Bertogli   uml: make hostfs_...
208
209
210
  	if (attrs->ia_valid & HOSTFS_ATTR_GID) {
  		if (fd >= 0) {
  			if (fchown(fd, -1, attrs->ia_gid))
f1adc05e7   Jeff Dike   uml: hostfs style...
211
  				return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
212
  		} else if (chown(file, -1, attrs->ia_gid)) {
f1adc05e7   Jeff Dike   uml: hostfs style...
213
  			return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
214
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215
  	}
5822b7fac   Alberto Bertogli   uml: make hostfs_...
216
217
218
  	if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
  		if (fd >= 0) {
  			if (ftruncate(fd, attrs->ia_size))
f1adc05e7   Jeff Dike   uml: hostfs style...
219
  				return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
220
  		} else if (truncate(file, attrs->ia_size)) {
f1adc05e7   Jeff Dike   uml: hostfs style...
221
  			return -errno;
5822b7fac   Alberto Bertogli   uml: make hostfs_...
222
  		}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
223
  	}
5822b7fac   Alberto Bertogli   uml: make hostfs_...
224

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

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

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