Commit | Line | Data |
---|---|---|
e5e5558e MS |
1 | /* |
2 | FUSE: Filesystem in Userspace | |
51eb01e7 | 3 | Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu> |
e5e5558e MS |
4 | |
5 | This program can be distributed under the terms of the GNU GPL. | |
6 | See the file COPYING. | |
7 | */ | |
8 | ||
9 | #include "fuse_i.h" | |
10 | ||
11 | #include <linux/pagemap.h> | |
12 | #include <linux/file.h> | |
13 | #include <linux/gfp.h> | |
14 | #include <linux/sched.h> | |
15 | #include <linux/namei.h> | |
16 | ||
0a0898cf MS |
17 | #if BITS_PER_LONG >= 64 |
18 | static inline void fuse_dentry_settime(struct dentry *entry, u64 time) | |
19 | { | |
20 | entry->d_time = time; | |
21 | } | |
22 | ||
23 | static inline u64 fuse_dentry_time(struct dentry *entry) | |
24 | { | |
25 | return entry->d_time; | |
26 | } | |
27 | #else | |
28 | /* | |
29 | * On 32 bit archs store the high 32 bits of time in d_fsdata | |
30 | */ | |
31 | static void fuse_dentry_settime(struct dentry *entry, u64 time) | |
32 | { | |
33 | entry->d_time = time; | |
34 | entry->d_fsdata = (void *) (unsigned long) (time >> 32); | |
35 | } | |
36 | ||
37 | static u64 fuse_dentry_time(struct dentry *entry) | |
38 | { | |
39 | return (u64) entry->d_time + | |
40 | ((u64) (unsigned long) entry->d_fsdata << 32); | |
41 | } | |
42 | #endif | |
43 | ||
6f9f1180 MS |
44 | /* |
45 | * FUSE caches dentries and attributes with separate timeout. The | |
46 | * time in jiffies until the dentry/attributes are valid is stored in | |
47 | * dentry->d_time and fuse_inode->i_time respectively. | |
48 | */ | |
49 | ||
50 | /* | |
51 | * Calculate the time in jiffies until a dentry/attributes are valid | |
52 | */ | |
0a0898cf | 53 | static u64 time_to_jiffies(unsigned long sec, unsigned long nsec) |
e5e5558e | 54 | { |
685d16dd MS |
55 | if (sec || nsec) { |
56 | struct timespec ts = {sec, nsec}; | |
0a0898cf | 57 | return get_jiffies_64() + timespec_to_jiffies(&ts); |
685d16dd | 58 | } else |
0a0898cf | 59 | return 0; |
e5e5558e MS |
60 | } |
61 | ||
6f9f1180 MS |
62 | /* |
63 | * Set dentry and possibly attribute timeouts from the lookup/mk* | |
64 | * replies | |
65 | */ | |
1fb69e78 MS |
66 | static void fuse_change_entry_timeout(struct dentry *entry, |
67 | struct fuse_entry_out *o) | |
0aa7c699 | 68 | { |
0a0898cf MS |
69 | fuse_dentry_settime(entry, |
70 | time_to_jiffies(o->entry_valid, o->entry_valid_nsec)); | |
1fb69e78 MS |
71 | } |
72 | ||
73 | static u64 attr_timeout(struct fuse_attr_out *o) | |
74 | { | |
75 | return time_to_jiffies(o->attr_valid, o->attr_valid_nsec); | |
76 | } | |
77 | ||
78 | static u64 entry_attr_timeout(struct fuse_entry_out *o) | |
79 | { | |
80 | return time_to_jiffies(o->attr_valid, o->attr_valid_nsec); | |
8cbdf1e6 MS |
81 | } |
82 | ||
6f9f1180 MS |
83 | /* |
84 | * Mark the attributes as stale, so that at the next call to | |
85 | * ->getattr() they will be fetched from userspace | |
86 | */ | |
8cbdf1e6 MS |
87 | void fuse_invalidate_attr(struct inode *inode) |
88 | { | |
0a0898cf | 89 | get_fuse_inode(inode)->i_time = 0; |
8cbdf1e6 MS |
90 | } |
91 | ||
6f9f1180 MS |
92 | /* |
93 | * Just mark the entry as stale, so that a next attempt to look it up | |
94 | * will result in a new lookup call to userspace | |
95 | * | |
96 | * This is called when a dentry is about to become negative and the | |
97 | * timeout is unknown (unlink, rmdir, rename and in some cases | |
98 | * lookup) | |
99 | */ | |
8cbdf1e6 MS |
100 | static void fuse_invalidate_entry_cache(struct dentry *entry) |
101 | { | |
0a0898cf | 102 | fuse_dentry_settime(entry, 0); |
8cbdf1e6 MS |
103 | } |
104 | ||
6f9f1180 MS |
105 | /* |
106 | * Same as fuse_invalidate_entry_cache(), but also try to remove the | |
107 | * dentry from the hash | |
108 | */ | |
8cbdf1e6 MS |
109 | static void fuse_invalidate_entry(struct dentry *entry) |
110 | { | |
111 | d_invalidate(entry); | |
112 | fuse_invalidate_entry_cache(entry); | |
0aa7c699 MS |
113 | } |
114 | ||
e5e5558e MS |
115 | static void fuse_lookup_init(struct fuse_req *req, struct inode *dir, |
116 | struct dentry *entry, | |
117 | struct fuse_entry_out *outarg) | |
118 | { | |
0e9663ee MS |
119 | struct fuse_conn *fc = get_fuse_conn(dir); |
120 | ||
121 | memset(outarg, 0, sizeof(struct fuse_entry_out)); | |
e5e5558e MS |
122 | req->in.h.opcode = FUSE_LOOKUP; |
123 | req->in.h.nodeid = get_node_id(dir); | |
e5e5558e MS |
124 | req->in.numargs = 1; |
125 | req->in.args[0].size = entry->d_name.len + 1; | |
126 | req->in.args[0].value = entry->d_name.name; | |
127 | req->out.numargs = 1; | |
0e9663ee MS |
128 | if (fc->minor < 9) |
129 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; | |
130 | else | |
131 | req->out.args[0].size = sizeof(struct fuse_entry_out); | |
e5e5558e MS |
132 | req->out.args[0].value = outarg; |
133 | } | |
134 | ||
6f9f1180 MS |
135 | /* |
136 | * Check whether the dentry is still valid | |
137 | * | |
138 | * If the entry validity timeout has expired and the dentry is | |
139 | * positive, try to redo the lookup. If the lookup results in a | |
140 | * different inode, then let the VFS invalidate the dentry and redo | |
141 | * the lookup once more. If the lookup results in the same inode, | |
142 | * then refresh the attributes, timeouts and mark the dentry valid. | |
143 | */ | |
e5e5558e MS |
144 | static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd) |
145 | { | |
8cbdf1e6 MS |
146 | struct inode *inode = entry->d_inode; |
147 | ||
148 | if (inode && is_bad_inode(inode)) | |
e5e5558e | 149 | return 0; |
0a0898cf | 150 | else if (fuse_dentry_time(entry) < get_jiffies_64()) { |
e5e5558e | 151 | int err; |
e5e5558e | 152 | struct fuse_entry_out outarg; |
8cbdf1e6 MS |
153 | struct fuse_conn *fc; |
154 | struct fuse_req *req; | |
2d51013e | 155 | struct fuse_req *forget_req; |
e956edd0 | 156 | struct dentry *parent; |
1fb69e78 | 157 | u64 attr_version; |
8cbdf1e6 | 158 | |
50322fe7 | 159 | /* For negative dentries, always do a fresh lookup */ |
8cbdf1e6 MS |
160 | if (!inode) |
161 | return 0; | |
162 | ||
163 | fc = get_fuse_conn(inode); | |
ce1d5a49 MS |
164 | req = fuse_get_req(fc); |
165 | if (IS_ERR(req)) | |
e5e5558e MS |
166 | return 0; |
167 | ||
2d51013e MS |
168 | forget_req = fuse_get_req(fc); |
169 | if (IS_ERR(forget_req)) { | |
170 | fuse_put_request(fc, req); | |
171 | return 0; | |
172 | } | |
173 | ||
1fb69e78 MS |
174 | spin_lock(&fc->lock); |
175 | attr_version = fc->attr_version; | |
176 | spin_unlock(&fc->lock); | |
177 | ||
e956edd0 MS |
178 | parent = dget_parent(entry); |
179 | fuse_lookup_init(req, parent->d_inode, entry, &outarg); | |
7c352bdf | 180 | request_send(fc, req); |
e956edd0 | 181 | dput(parent); |
e5e5558e | 182 | err = req->out.h.error; |
2d51013e | 183 | fuse_put_request(fc, req); |
50322fe7 MS |
184 | /* Zero nodeid is same as -ENOENT */ |
185 | if (!err && !outarg.nodeid) | |
186 | err = -ENOENT; | |
9e6268db | 187 | if (!err) { |
8cbdf1e6 | 188 | struct fuse_inode *fi = get_fuse_inode(inode); |
9e6268db | 189 | if (outarg.nodeid != get_node_id(inode)) { |
2d51013e MS |
190 | fuse_send_forget(fc, forget_req, |
191 | outarg.nodeid, 1); | |
9e6268db MS |
192 | return 0; |
193 | } | |
8da5ff23 | 194 | spin_lock(&fc->lock); |
9e6268db | 195 | fi->nlookup ++; |
8da5ff23 | 196 | spin_unlock(&fc->lock); |
9e6268db | 197 | } |
2d51013e | 198 | fuse_put_request(fc, forget_req); |
9e6268db | 199 | if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT) |
e5e5558e MS |
200 | return 0; |
201 | ||
1fb69e78 MS |
202 | fuse_change_attributes(inode, &outarg.attr, |
203 | entry_attr_timeout(&outarg), | |
204 | attr_version); | |
205 | fuse_change_entry_timeout(entry, &outarg); | |
e5e5558e MS |
206 | } |
207 | return 1; | |
208 | } | |
209 | ||
8bfc016d | 210 | static int invalid_nodeid(u64 nodeid) |
2827d0b2 MS |
211 | { |
212 | return !nodeid || nodeid == FUSE_ROOT_ID; | |
213 | } | |
214 | ||
e5e5558e MS |
215 | static struct dentry_operations fuse_dentry_operations = { |
216 | .d_revalidate = fuse_dentry_revalidate, | |
217 | }; | |
218 | ||
a5bfffac | 219 | int fuse_valid_type(int m) |
39ee059a MS |
220 | { |
221 | return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) || | |
222 | S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m); | |
223 | } | |
224 | ||
d2a85164 MS |
225 | /* |
226 | * Add a directory inode to a dentry, ensuring that no other dentry | |
227 | * refers to this inode. Called with fc->inst_mutex. | |
228 | */ | |
229 | static int fuse_d_add_directory(struct dentry *entry, struct inode *inode) | |
230 | { | |
231 | struct dentry *alias = d_find_alias(inode); | |
232 | if (alias) { | |
233 | /* This tries to shrink the subtree below alias */ | |
234 | fuse_invalidate_entry(alias); | |
235 | dput(alias); | |
236 | if (!list_empty(&inode->i_dentry)) | |
237 | return -EBUSY; | |
238 | } | |
239 | d_add(entry, inode); | |
240 | return 0; | |
241 | } | |
242 | ||
0aa7c699 MS |
243 | static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, |
244 | struct nameidata *nd) | |
e5e5558e MS |
245 | { |
246 | int err; | |
e5e5558e MS |
247 | struct fuse_entry_out outarg; |
248 | struct inode *inode = NULL; | |
249 | struct fuse_conn *fc = get_fuse_conn(dir); | |
250 | struct fuse_req *req; | |
2d51013e | 251 | struct fuse_req *forget_req; |
1fb69e78 | 252 | u64 attr_version; |
e5e5558e MS |
253 | |
254 | if (entry->d_name.len > FUSE_NAME_MAX) | |
0aa7c699 | 255 | return ERR_PTR(-ENAMETOOLONG); |
e5e5558e | 256 | |
ce1d5a49 MS |
257 | req = fuse_get_req(fc); |
258 | if (IS_ERR(req)) | |
259 | return ERR_PTR(PTR_ERR(req)); | |
e5e5558e | 260 | |
2d51013e MS |
261 | forget_req = fuse_get_req(fc); |
262 | if (IS_ERR(forget_req)) { | |
263 | fuse_put_request(fc, req); | |
264 | return ERR_PTR(PTR_ERR(forget_req)); | |
265 | } | |
266 | ||
1fb69e78 MS |
267 | spin_lock(&fc->lock); |
268 | attr_version = fc->attr_version; | |
269 | spin_unlock(&fc->lock); | |
270 | ||
e5e5558e MS |
271 | fuse_lookup_init(req, dir, entry, &outarg); |
272 | request_send(fc, req); | |
e5e5558e | 273 | err = req->out.h.error; |
2d51013e | 274 | fuse_put_request(fc, req); |
50322fe7 MS |
275 | /* Zero nodeid is same as -ENOENT, but with valid timeout */ |
276 | if (!err && outarg.nodeid && | |
a5bfffac TS |
277 | (invalid_nodeid(outarg.nodeid) || |
278 | !fuse_valid_type(outarg.attr.mode))) | |
ee4e5271 | 279 | err = -EIO; |
8cbdf1e6 | 280 | if (!err && outarg.nodeid) { |
e5e5558e | 281 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, |
1fb69e78 MS |
282 | &outarg.attr, entry_attr_timeout(&outarg), |
283 | attr_version); | |
e5e5558e | 284 | if (!inode) { |
2d51013e | 285 | fuse_send_forget(fc, forget_req, outarg.nodeid, 1); |
0aa7c699 | 286 | return ERR_PTR(-ENOMEM); |
e5e5558e MS |
287 | } |
288 | } | |
2d51013e | 289 | fuse_put_request(fc, forget_req); |
e5e5558e | 290 | if (err && err != -ENOENT) |
0aa7c699 | 291 | return ERR_PTR(err); |
e5e5558e | 292 | |
d2a85164 MS |
293 | if (inode && S_ISDIR(inode->i_mode)) { |
294 | mutex_lock(&fc->inst_mutex); | |
295 | err = fuse_d_add_directory(entry, inode); | |
296 | mutex_unlock(&fc->inst_mutex); | |
297 | if (err) { | |
298 | iput(inode); | |
299 | return ERR_PTR(err); | |
300 | } | |
301 | } else | |
302 | d_add(entry, inode); | |
303 | ||
e5e5558e | 304 | entry->d_op = &fuse_dentry_operations; |
8cbdf1e6 | 305 | if (!err) |
1fb69e78 | 306 | fuse_change_entry_timeout(entry, &outarg); |
8cbdf1e6 MS |
307 | else |
308 | fuse_invalidate_entry_cache(entry); | |
0aa7c699 | 309 | return NULL; |
e5e5558e MS |
310 | } |
311 | ||
51eb01e7 MS |
312 | /* |
313 | * Synchronous release for the case when something goes wrong in CREATE_OPEN | |
314 | */ | |
315 | static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff, | |
316 | u64 nodeid, int flags) | |
317 | { | |
c756e0a4 MS |
318 | fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE); |
319 | ff->reserved_req->force = 1; | |
320 | request_send(fc, ff->reserved_req); | |
321 | fuse_put_request(fc, ff->reserved_req); | |
322 | kfree(ff); | |
51eb01e7 MS |
323 | } |
324 | ||
6f9f1180 MS |
325 | /* |
326 | * Atomic create+open operation | |
327 | * | |
328 | * If the filesystem doesn't support this, then fall back to separate | |
329 | * 'mknod' + 'open' requests. | |
330 | */ | |
fd72faac MS |
331 | static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode, |
332 | struct nameidata *nd) | |
333 | { | |
334 | int err; | |
335 | struct inode *inode; | |
336 | struct fuse_conn *fc = get_fuse_conn(dir); | |
337 | struct fuse_req *req; | |
51eb01e7 | 338 | struct fuse_req *forget_req; |
fd72faac MS |
339 | struct fuse_open_in inarg; |
340 | struct fuse_open_out outopen; | |
341 | struct fuse_entry_out outentry; | |
fd72faac MS |
342 | struct fuse_file *ff; |
343 | struct file *file; | |
344 | int flags = nd->intent.open.flags - 1; | |
345 | ||
fd72faac | 346 | if (fc->no_create) |
ce1d5a49 | 347 | return -ENOSYS; |
fd72faac | 348 | |
51eb01e7 MS |
349 | forget_req = fuse_get_req(fc); |
350 | if (IS_ERR(forget_req)) | |
351 | return PTR_ERR(forget_req); | |
352 | ||
ce1d5a49 | 353 | req = fuse_get_req(fc); |
51eb01e7 | 354 | err = PTR_ERR(req); |
ce1d5a49 | 355 | if (IS_ERR(req)) |
51eb01e7 | 356 | goto out_put_forget_req; |
fd72faac | 357 | |
ce1d5a49 | 358 | err = -ENOMEM; |
fd72faac MS |
359 | ff = fuse_file_alloc(); |
360 | if (!ff) | |
361 | goto out_put_request; | |
362 | ||
363 | flags &= ~O_NOCTTY; | |
364 | memset(&inarg, 0, sizeof(inarg)); | |
0e9663ee | 365 | memset(&outentry, 0, sizeof(outentry)); |
fd72faac MS |
366 | inarg.flags = flags; |
367 | inarg.mode = mode; | |
368 | req->in.h.opcode = FUSE_CREATE; | |
369 | req->in.h.nodeid = get_node_id(dir); | |
fd72faac MS |
370 | req->in.numargs = 2; |
371 | req->in.args[0].size = sizeof(inarg); | |
372 | req->in.args[0].value = &inarg; | |
373 | req->in.args[1].size = entry->d_name.len + 1; | |
374 | req->in.args[1].value = entry->d_name.name; | |
375 | req->out.numargs = 2; | |
0e9663ee MS |
376 | if (fc->minor < 9) |
377 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; | |
378 | else | |
379 | req->out.args[0].size = sizeof(outentry); | |
fd72faac MS |
380 | req->out.args[0].value = &outentry; |
381 | req->out.args[1].size = sizeof(outopen); | |
382 | req->out.args[1].value = &outopen; | |
383 | request_send(fc, req); | |
384 | err = req->out.h.error; | |
385 | if (err) { | |
386 | if (err == -ENOSYS) | |
387 | fc->no_create = 1; | |
388 | goto out_free_ff; | |
389 | } | |
390 | ||
391 | err = -EIO; | |
2827d0b2 | 392 | if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid)) |
fd72faac MS |
393 | goto out_free_ff; |
394 | ||
51eb01e7 | 395 | fuse_put_request(fc, req); |
fd72faac | 396 | inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation, |
1fb69e78 | 397 | &outentry.attr, entry_attr_timeout(&outentry), 0); |
fd72faac MS |
398 | if (!inode) { |
399 | flags &= ~(O_CREAT | O_EXCL | O_TRUNC); | |
400 | ff->fh = outopen.fh; | |
51eb01e7 MS |
401 | fuse_sync_release(fc, ff, outentry.nodeid, flags); |
402 | fuse_send_forget(fc, forget_req, outentry.nodeid, 1); | |
403 | return -ENOMEM; | |
fd72faac | 404 | } |
51eb01e7 | 405 | fuse_put_request(fc, forget_req); |
fd72faac | 406 | d_instantiate(entry, inode); |
1fb69e78 | 407 | fuse_change_entry_timeout(entry, &outentry); |
fd72faac MS |
408 | file = lookup_instantiate_filp(nd, entry, generic_file_open); |
409 | if (IS_ERR(file)) { | |
410 | ff->fh = outopen.fh; | |
51eb01e7 | 411 | fuse_sync_release(fc, ff, outentry.nodeid, flags); |
fd72faac MS |
412 | return PTR_ERR(file); |
413 | } | |
414 | fuse_finish_open(inode, file, ff, &outopen); | |
415 | return 0; | |
416 | ||
417 | out_free_ff: | |
418 | fuse_file_free(ff); | |
419 | out_put_request: | |
420 | fuse_put_request(fc, req); | |
51eb01e7 MS |
421 | out_put_forget_req: |
422 | fuse_put_request(fc, forget_req); | |
fd72faac MS |
423 | return err; |
424 | } | |
425 | ||
6f9f1180 MS |
426 | /* |
427 | * Code shared between mknod, mkdir, symlink and link | |
428 | */ | |
9e6268db MS |
429 | static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req, |
430 | struct inode *dir, struct dentry *entry, | |
431 | int mode) | |
432 | { | |
433 | struct fuse_entry_out outarg; | |
434 | struct inode *inode; | |
9e6268db | 435 | int err; |
2d51013e MS |
436 | struct fuse_req *forget_req; |
437 | ||
438 | forget_req = fuse_get_req(fc); | |
439 | if (IS_ERR(forget_req)) { | |
440 | fuse_put_request(fc, req); | |
441 | return PTR_ERR(forget_req); | |
442 | } | |
9e6268db | 443 | |
0e9663ee | 444 | memset(&outarg, 0, sizeof(outarg)); |
9e6268db | 445 | req->in.h.nodeid = get_node_id(dir); |
9e6268db | 446 | req->out.numargs = 1; |
0e9663ee MS |
447 | if (fc->minor < 9) |
448 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; | |
449 | else | |
450 | req->out.args[0].size = sizeof(outarg); | |
9e6268db MS |
451 | req->out.args[0].value = &outarg; |
452 | request_send(fc, req); | |
453 | err = req->out.h.error; | |
2d51013e MS |
454 | fuse_put_request(fc, req); |
455 | if (err) | |
456 | goto out_put_forget_req; | |
457 | ||
39ee059a MS |
458 | err = -EIO; |
459 | if (invalid_nodeid(outarg.nodeid)) | |
2d51013e | 460 | goto out_put_forget_req; |
39ee059a MS |
461 | |
462 | if ((outarg.attr.mode ^ mode) & S_IFMT) | |
2d51013e | 463 | goto out_put_forget_req; |
39ee059a | 464 | |
9e6268db | 465 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, |
1fb69e78 | 466 | &outarg.attr, entry_attr_timeout(&outarg), 0); |
9e6268db | 467 | if (!inode) { |
2d51013e | 468 | fuse_send_forget(fc, forget_req, outarg.nodeid, 1); |
9e6268db MS |
469 | return -ENOMEM; |
470 | } | |
2d51013e | 471 | fuse_put_request(fc, forget_req); |
9e6268db | 472 | |
d2a85164 MS |
473 | if (S_ISDIR(inode->i_mode)) { |
474 | struct dentry *alias; | |
475 | mutex_lock(&fc->inst_mutex); | |
476 | alias = d_find_alias(inode); | |
477 | if (alias) { | |
478 | /* New directory must have moved since mkdir */ | |
479 | mutex_unlock(&fc->inst_mutex); | |
480 | dput(alias); | |
481 | iput(inode); | |
482 | return -EBUSY; | |
483 | } | |
484 | d_instantiate(entry, inode); | |
485 | mutex_unlock(&fc->inst_mutex); | |
486 | } else | |
487 | d_instantiate(entry, inode); | |
9e6268db | 488 | |
1fb69e78 | 489 | fuse_change_entry_timeout(entry, &outarg); |
9e6268db MS |
490 | fuse_invalidate_attr(dir); |
491 | return 0; | |
39ee059a | 492 | |
2d51013e MS |
493 | out_put_forget_req: |
494 | fuse_put_request(fc, forget_req); | |
39ee059a | 495 | return err; |
9e6268db MS |
496 | } |
497 | ||
498 | static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode, | |
499 | dev_t rdev) | |
500 | { | |
501 | struct fuse_mknod_in inarg; | |
502 | struct fuse_conn *fc = get_fuse_conn(dir); | |
ce1d5a49 MS |
503 | struct fuse_req *req = fuse_get_req(fc); |
504 | if (IS_ERR(req)) | |
505 | return PTR_ERR(req); | |
9e6268db MS |
506 | |
507 | memset(&inarg, 0, sizeof(inarg)); | |
508 | inarg.mode = mode; | |
509 | inarg.rdev = new_encode_dev(rdev); | |
510 | req->in.h.opcode = FUSE_MKNOD; | |
511 | req->in.numargs = 2; | |
512 | req->in.args[0].size = sizeof(inarg); | |
513 | req->in.args[0].value = &inarg; | |
514 | req->in.args[1].size = entry->d_name.len + 1; | |
515 | req->in.args[1].value = entry->d_name.name; | |
516 | return create_new_entry(fc, req, dir, entry, mode); | |
517 | } | |
518 | ||
519 | static int fuse_create(struct inode *dir, struct dentry *entry, int mode, | |
520 | struct nameidata *nd) | |
521 | { | |
b9ba347f | 522 | if (nd && (nd->flags & LOOKUP_OPEN)) { |
fd72faac MS |
523 | int err = fuse_create_open(dir, entry, mode, nd); |
524 | if (err != -ENOSYS) | |
525 | return err; | |
526 | /* Fall back on mknod */ | |
527 | } | |
9e6268db MS |
528 | return fuse_mknod(dir, entry, mode, 0); |
529 | } | |
530 | ||
531 | static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode) | |
532 | { | |
533 | struct fuse_mkdir_in inarg; | |
534 | struct fuse_conn *fc = get_fuse_conn(dir); | |
ce1d5a49 MS |
535 | struct fuse_req *req = fuse_get_req(fc); |
536 | if (IS_ERR(req)) | |
537 | return PTR_ERR(req); | |
9e6268db MS |
538 | |
539 | memset(&inarg, 0, sizeof(inarg)); | |
540 | inarg.mode = mode; | |
541 | req->in.h.opcode = FUSE_MKDIR; | |
542 | req->in.numargs = 2; | |
543 | req->in.args[0].size = sizeof(inarg); | |
544 | req->in.args[0].value = &inarg; | |
545 | req->in.args[1].size = entry->d_name.len + 1; | |
546 | req->in.args[1].value = entry->d_name.name; | |
547 | return create_new_entry(fc, req, dir, entry, S_IFDIR); | |
548 | } | |
549 | ||
550 | static int fuse_symlink(struct inode *dir, struct dentry *entry, | |
551 | const char *link) | |
552 | { | |
553 | struct fuse_conn *fc = get_fuse_conn(dir); | |
554 | unsigned len = strlen(link) + 1; | |
ce1d5a49 MS |
555 | struct fuse_req *req = fuse_get_req(fc); |
556 | if (IS_ERR(req)) | |
557 | return PTR_ERR(req); | |
9e6268db MS |
558 | |
559 | req->in.h.opcode = FUSE_SYMLINK; | |
560 | req->in.numargs = 2; | |
561 | req->in.args[0].size = entry->d_name.len + 1; | |
562 | req->in.args[0].value = entry->d_name.name; | |
563 | req->in.args[1].size = len; | |
564 | req->in.args[1].value = link; | |
565 | return create_new_entry(fc, req, dir, entry, S_IFLNK); | |
566 | } | |
567 | ||
568 | static int fuse_unlink(struct inode *dir, struct dentry *entry) | |
569 | { | |
570 | int err; | |
571 | struct fuse_conn *fc = get_fuse_conn(dir); | |
ce1d5a49 MS |
572 | struct fuse_req *req = fuse_get_req(fc); |
573 | if (IS_ERR(req)) | |
574 | return PTR_ERR(req); | |
9e6268db MS |
575 | |
576 | req->in.h.opcode = FUSE_UNLINK; | |
577 | req->in.h.nodeid = get_node_id(dir); | |
9e6268db MS |
578 | req->in.numargs = 1; |
579 | req->in.args[0].size = entry->d_name.len + 1; | |
580 | req->in.args[0].value = entry->d_name.name; | |
581 | request_send(fc, req); | |
582 | err = req->out.h.error; | |
583 | fuse_put_request(fc, req); | |
584 | if (!err) { | |
585 | struct inode *inode = entry->d_inode; | |
586 | ||
587 | /* Set nlink to zero so the inode can be cleared, if | |
588 | the inode does have more links this will be | |
589 | discovered at the next lookup/getattr */ | |
ce71ec36 | 590 | clear_nlink(inode); |
9e6268db MS |
591 | fuse_invalidate_attr(inode); |
592 | fuse_invalidate_attr(dir); | |
8cbdf1e6 | 593 | fuse_invalidate_entry_cache(entry); |
9e6268db MS |
594 | } else if (err == -EINTR) |
595 | fuse_invalidate_entry(entry); | |
596 | return err; | |
597 | } | |
598 | ||
599 | static int fuse_rmdir(struct inode *dir, struct dentry *entry) | |
600 | { | |
601 | int err; | |
602 | struct fuse_conn *fc = get_fuse_conn(dir); | |
ce1d5a49 MS |
603 | struct fuse_req *req = fuse_get_req(fc); |
604 | if (IS_ERR(req)) | |
605 | return PTR_ERR(req); | |
9e6268db MS |
606 | |
607 | req->in.h.opcode = FUSE_RMDIR; | |
608 | req->in.h.nodeid = get_node_id(dir); | |
9e6268db MS |
609 | req->in.numargs = 1; |
610 | req->in.args[0].size = entry->d_name.len + 1; | |
611 | req->in.args[0].value = entry->d_name.name; | |
612 | request_send(fc, req); | |
613 | err = req->out.h.error; | |
614 | fuse_put_request(fc, req); | |
615 | if (!err) { | |
ce71ec36 | 616 | clear_nlink(entry->d_inode); |
9e6268db | 617 | fuse_invalidate_attr(dir); |
8cbdf1e6 | 618 | fuse_invalidate_entry_cache(entry); |
9e6268db MS |
619 | } else if (err == -EINTR) |
620 | fuse_invalidate_entry(entry); | |
621 | return err; | |
622 | } | |
623 | ||
624 | static int fuse_rename(struct inode *olddir, struct dentry *oldent, | |
625 | struct inode *newdir, struct dentry *newent) | |
626 | { | |
627 | int err; | |
628 | struct fuse_rename_in inarg; | |
629 | struct fuse_conn *fc = get_fuse_conn(olddir); | |
ce1d5a49 MS |
630 | struct fuse_req *req = fuse_get_req(fc); |
631 | if (IS_ERR(req)) | |
632 | return PTR_ERR(req); | |
9e6268db MS |
633 | |
634 | memset(&inarg, 0, sizeof(inarg)); | |
635 | inarg.newdir = get_node_id(newdir); | |
636 | req->in.h.opcode = FUSE_RENAME; | |
637 | req->in.h.nodeid = get_node_id(olddir); | |
9e6268db MS |
638 | req->in.numargs = 3; |
639 | req->in.args[0].size = sizeof(inarg); | |
640 | req->in.args[0].value = &inarg; | |
641 | req->in.args[1].size = oldent->d_name.len + 1; | |
642 | req->in.args[1].value = oldent->d_name.name; | |
643 | req->in.args[2].size = newent->d_name.len + 1; | |
644 | req->in.args[2].value = newent->d_name.name; | |
645 | request_send(fc, req); | |
646 | err = req->out.h.error; | |
647 | fuse_put_request(fc, req); | |
648 | if (!err) { | |
649 | fuse_invalidate_attr(olddir); | |
650 | if (olddir != newdir) | |
651 | fuse_invalidate_attr(newdir); | |
8cbdf1e6 MS |
652 | |
653 | /* newent will end up negative */ | |
654 | if (newent->d_inode) | |
655 | fuse_invalidate_entry_cache(newent); | |
9e6268db MS |
656 | } else if (err == -EINTR) { |
657 | /* If request was interrupted, DEITY only knows if the | |
658 | rename actually took place. If the invalidation | |
659 | fails (e.g. some process has CWD under the renamed | |
660 | directory), then there can be inconsistency between | |
661 | the dcache and the real filesystem. Tough luck. */ | |
662 | fuse_invalidate_entry(oldent); | |
663 | if (newent->d_inode) | |
664 | fuse_invalidate_entry(newent); | |
665 | } | |
666 | ||
667 | return err; | |
668 | } | |
669 | ||
670 | static int fuse_link(struct dentry *entry, struct inode *newdir, | |
671 | struct dentry *newent) | |
672 | { | |
673 | int err; | |
674 | struct fuse_link_in inarg; | |
675 | struct inode *inode = entry->d_inode; | |
676 | struct fuse_conn *fc = get_fuse_conn(inode); | |
ce1d5a49 MS |
677 | struct fuse_req *req = fuse_get_req(fc); |
678 | if (IS_ERR(req)) | |
679 | return PTR_ERR(req); | |
9e6268db MS |
680 | |
681 | memset(&inarg, 0, sizeof(inarg)); | |
682 | inarg.oldnodeid = get_node_id(inode); | |
683 | req->in.h.opcode = FUSE_LINK; | |
9e6268db MS |
684 | req->in.numargs = 2; |
685 | req->in.args[0].size = sizeof(inarg); | |
686 | req->in.args[0].value = &inarg; | |
687 | req->in.args[1].size = newent->d_name.len + 1; | |
688 | req->in.args[1].value = newent->d_name.name; | |
689 | err = create_new_entry(fc, req, newdir, newent, inode->i_mode); | |
690 | /* Contrary to "normal" filesystems it can happen that link | |
691 | makes two "logical" inodes point to the same "physical" | |
692 | inode. We invalidate the attributes of the old one, so it | |
693 | will reflect changes in the backing inode (link count, | |
694 | etc.) | |
695 | */ | |
696 | if (!err || err == -EINTR) | |
697 | fuse_invalidate_attr(inode); | |
698 | return err; | |
699 | } | |
700 | ||
1fb69e78 MS |
701 | static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr, |
702 | struct kstat *stat) | |
703 | { | |
704 | stat->dev = inode->i_sb->s_dev; | |
705 | stat->ino = attr->ino; | |
706 | stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777); | |
707 | stat->nlink = attr->nlink; | |
708 | stat->uid = attr->uid; | |
709 | stat->gid = attr->gid; | |
710 | stat->rdev = inode->i_rdev; | |
711 | stat->atime.tv_sec = attr->atime; | |
712 | stat->atime.tv_nsec = attr->atimensec; | |
713 | stat->mtime.tv_sec = attr->mtime; | |
714 | stat->mtime.tv_nsec = attr->mtimensec; | |
715 | stat->ctime.tv_sec = attr->ctime; | |
716 | stat->ctime.tv_nsec = attr->ctimensec; | |
717 | stat->size = attr->size; | |
718 | stat->blocks = attr->blocks; | |
719 | stat->blksize = (1 << inode->i_blkbits); | |
720 | } | |
721 | ||
c79e322f MS |
722 | static int fuse_do_getattr(struct inode *inode, struct kstat *stat, |
723 | struct file *file) | |
e5e5558e MS |
724 | { |
725 | int err; | |
c79e322f MS |
726 | struct fuse_getattr_in inarg; |
727 | struct fuse_attr_out outarg; | |
e5e5558e | 728 | struct fuse_conn *fc = get_fuse_conn(inode); |
1fb69e78 MS |
729 | struct fuse_req *req; |
730 | u64 attr_version; | |
731 | ||
732 | req = fuse_get_req(fc); | |
ce1d5a49 MS |
733 | if (IS_ERR(req)) |
734 | return PTR_ERR(req); | |
e5e5558e | 735 | |
1fb69e78 MS |
736 | spin_lock(&fc->lock); |
737 | attr_version = fc->attr_version; | |
738 | spin_unlock(&fc->lock); | |
739 | ||
c79e322f | 740 | memset(&inarg, 0, sizeof(inarg)); |
0e9663ee | 741 | memset(&outarg, 0, sizeof(outarg)); |
c79e322f MS |
742 | /* Directories have separate file-handle space */ |
743 | if (file && S_ISREG(inode->i_mode)) { | |
744 | struct fuse_file *ff = file->private_data; | |
745 | ||
746 | inarg.getattr_flags |= FUSE_GETATTR_FH; | |
747 | inarg.fh = ff->fh; | |
748 | } | |
e5e5558e MS |
749 | req->in.h.opcode = FUSE_GETATTR; |
750 | req->in.h.nodeid = get_node_id(inode); | |
c79e322f MS |
751 | req->in.numargs = 1; |
752 | req->in.args[0].size = sizeof(inarg); | |
753 | req->in.args[0].value = &inarg; | |
e5e5558e | 754 | req->out.numargs = 1; |
0e9663ee MS |
755 | if (fc->minor < 9) |
756 | req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; | |
757 | else | |
758 | req->out.args[0].size = sizeof(outarg); | |
c79e322f | 759 | req->out.args[0].value = &outarg; |
e5e5558e MS |
760 | request_send(fc, req); |
761 | err = req->out.h.error; | |
762 | fuse_put_request(fc, req); | |
763 | if (!err) { | |
c79e322f | 764 | if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { |
e5e5558e MS |
765 | make_bad_inode(inode); |
766 | err = -EIO; | |
767 | } else { | |
c79e322f MS |
768 | fuse_change_attributes(inode, &outarg.attr, |
769 | attr_timeout(&outarg), | |
1fb69e78 MS |
770 | attr_version); |
771 | if (stat) | |
c79e322f | 772 | fuse_fillattr(inode, &outarg.attr, stat); |
e5e5558e MS |
773 | } |
774 | } | |
775 | return err; | |
776 | } | |
777 | ||
bcb4be80 MS |
778 | int fuse_update_attributes(struct inode *inode, struct kstat *stat, |
779 | struct file *file, bool *refreshed) | |
780 | { | |
781 | struct fuse_inode *fi = get_fuse_inode(inode); | |
782 | int err; | |
783 | bool r; | |
784 | ||
785 | if (fi->i_time < get_jiffies_64()) { | |
786 | r = true; | |
787 | err = fuse_do_getattr(inode, stat, file); | |
788 | } else { | |
789 | r = false; | |
790 | err = 0; | |
791 | if (stat) { | |
792 | generic_fillattr(inode, stat); | |
793 | stat->mode = fi->orig_i_mode; | |
794 | } | |
795 | } | |
796 | ||
797 | if (refreshed != NULL) | |
798 | *refreshed = r; | |
799 | ||
800 | return err; | |
801 | } | |
802 | ||
87729a55 MS |
803 | /* |
804 | * Calling into a user-controlled filesystem gives the filesystem | |
805 | * daemon ptrace-like capabilities over the requester process. This | |
806 | * means, that the filesystem daemon is able to record the exact | |
807 | * filesystem operations performed, and can also control the behavior | |
808 | * of the requester process in otherwise impossible ways. For example | |
809 | * it can delay the operation for arbitrary length of time allowing | |
810 | * DoS against the requester. | |
811 | * | |
812 | * For this reason only those processes can call into the filesystem, | |
813 | * for which the owner of the mount has ptrace privilege. This | |
814 | * excludes processes started by other users, suid or sgid processes. | |
815 | */ | |
e57ac683 | 816 | int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task) |
87729a55 MS |
817 | { |
818 | if (fc->flags & FUSE_ALLOW_OTHER) | |
819 | return 1; | |
820 | ||
821 | if (task->euid == fc->user_id && | |
822 | task->suid == fc->user_id && | |
823 | task->uid == fc->user_id && | |
824 | task->egid == fc->group_id && | |
825 | task->sgid == fc->group_id && | |
826 | task->gid == fc->group_id) | |
827 | return 1; | |
828 | ||
829 | return 0; | |
830 | } | |
831 | ||
31d40d74 MS |
832 | static int fuse_access(struct inode *inode, int mask) |
833 | { | |
834 | struct fuse_conn *fc = get_fuse_conn(inode); | |
835 | struct fuse_req *req; | |
836 | struct fuse_access_in inarg; | |
837 | int err; | |
838 | ||
839 | if (fc->no_access) | |
840 | return 0; | |
841 | ||
ce1d5a49 MS |
842 | req = fuse_get_req(fc); |
843 | if (IS_ERR(req)) | |
844 | return PTR_ERR(req); | |
31d40d74 MS |
845 | |
846 | memset(&inarg, 0, sizeof(inarg)); | |
847 | inarg.mask = mask; | |
848 | req->in.h.opcode = FUSE_ACCESS; | |
849 | req->in.h.nodeid = get_node_id(inode); | |
31d40d74 MS |
850 | req->in.numargs = 1; |
851 | req->in.args[0].size = sizeof(inarg); | |
852 | req->in.args[0].value = &inarg; | |
853 | request_send(fc, req); | |
854 | err = req->out.h.error; | |
855 | fuse_put_request(fc, req); | |
856 | if (err == -ENOSYS) { | |
857 | fc->no_access = 1; | |
858 | err = 0; | |
859 | } | |
860 | return err; | |
861 | } | |
862 | ||
6f9f1180 MS |
863 | /* |
864 | * Check permission. The two basic access models of FUSE are: | |
865 | * | |
866 | * 1) Local access checking ('default_permissions' mount option) based | |
867 | * on file mode. This is the plain old disk filesystem permission | |
868 | * modell. | |
869 | * | |
870 | * 2) "Remote" access checking, where server is responsible for | |
871 | * checking permission in each inode operation. An exception to this | |
872 | * is if ->permission() was invoked from sys_access() in which case an | |
873 | * access request is sent. Execute permission is still checked | |
874 | * locally based on file mode. | |
875 | */ | |
e5e5558e MS |
876 | static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd) |
877 | { | |
878 | struct fuse_conn *fc = get_fuse_conn(inode); | |
244f6385 MS |
879 | bool refreshed = false; |
880 | int err = 0; | |
e5e5558e | 881 | |
87729a55 | 882 | if (!fuse_allow_task(fc, current)) |
e5e5558e | 883 | return -EACCES; |
244f6385 MS |
884 | |
885 | /* | |
e8e96157 | 886 | * If attributes are needed, refresh them before proceeding |
244f6385 | 887 | */ |
e8e96157 MS |
888 | if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) || |
889 | ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) { | |
bcb4be80 MS |
890 | err = fuse_update_attributes(inode, NULL, NULL, &refreshed); |
891 | if (err) | |
892 | return err; | |
244f6385 MS |
893 | } |
894 | ||
895 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { | |
1e9a4ed9 MS |
896 | int err = generic_permission(inode, mask, NULL); |
897 | ||
898 | /* If permission is denied, try to refresh file | |
899 | attributes. This is also needed, because the root | |
900 | node will at first have no permissions */ | |
244f6385 | 901 | if (err == -EACCES && !refreshed) { |
c79e322f | 902 | err = fuse_do_getattr(inode, NULL, NULL); |
1e9a4ed9 MS |
903 | if (!err) |
904 | err = generic_permission(inode, mask, NULL); | |
905 | } | |
906 | ||
6f9f1180 MS |
907 | /* Note: the opposite of the above test does not |
908 | exist. So if permissions are revoked this won't be | |
909 | noticed immediately, only after the attribute | |
910 | timeout has expired */ | |
e8e96157 MS |
911 | } else if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR))) { |
912 | err = fuse_access(inode, mask); | |
913 | } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) { | |
914 | if (!(inode->i_mode & S_IXUGO)) { | |
915 | if (refreshed) | |
916 | return -EACCES; | |
917 | ||
c79e322f | 918 | err = fuse_do_getattr(inode, NULL, NULL); |
e8e96157 MS |
919 | if (!err && !(inode->i_mode & S_IXUGO)) |
920 | return -EACCES; | |
921 | } | |
e5e5558e | 922 | } |
244f6385 | 923 | return err; |
e5e5558e MS |
924 | } |
925 | ||
926 | static int parse_dirfile(char *buf, size_t nbytes, struct file *file, | |
927 | void *dstbuf, filldir_t filldir) | |
928 | { | |
929 | while (nbytes >= FUSE_NAME_OFFSET) { | |
930 | struct fuse_dirent *dirent = (struct fuse_dirent *) buf; | |
931 | size_t reclen = FUSE_DIRENT_SIZE(dirent); | |
932 | int over; | |
933 | if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX) | |
934 | return -EIO; | |
935 | if (reclen > nbytes) | |
936 | break; | |
937 | ||
938 | over = filldir(dstbuf, dirent->name, dirent->namelen, | |
939 | file->f_pos, dirent->ino, dirent->type); | |
940 | if (over) | |
941 | break; | |
942 | ||
943 | buf += reclen; | |
944 | nbytes -= reclen; | |
945 | file->f_pos = dirent->off; | |
946 | } | |
947 | ||
948 | return 0; | |
949 | } | |
950 | ||
04730fef | 951 | static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir) |
e5e5558e | 952 | { |
04730fef MS |
953 | int err; |
954 | size_t nbytes; | |
955 | struct page *page; | |
7706a9d6 | 956 | struct inode *inode = file->f_path.dentry->d_inode; |
e5e5558e | 957 | struct fuse_conn *fc = get_fuse_conn(inode); |
c756e0a4 | 958 | struct fuse_file *ff = file->private_data; |
248d86e8 MS |
959 | struct fuse_req *req; |
960 | ||
961 | if (is_bad_inode(inode)) | |
962 | return -EIO; | |
963 | ||
ce1d5a49 MS |
964 | req = fuse_get_req(fc); |
965 | if (IS_ERR(req)) | |
966 | return PTR_ERR(req); | |
e5e5558e | 967 | |
04730fef MS |
968 | page = alloc_page(GFP_KERNEL); |
969 | if (!page) { | |
970 | fuse_put_request(fc, req); | |
971 | return -ENOMEM; | |
972 | } | |
973 | req->num_pages = 1; | |
974 | req->pages[0] = page; | |
c756e0a4 | 975 | fuse_read_fill(req, ff, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR); |
361b1eb5 MS |
976 | request_send(fc, req); |
977 | nbytes = req->out.args[0].size; | |
e5e5558e MS |
978 | err = req->out.h.error; |
979 | fuse_put_request(fc, req); | |
980 | if (!err) | |
04730fef MS |
981 | err = parse_dirfile(page_address(page), nbytes, file, dstbuf, |
982 | filldir); | |
e5e5558e | 983 | |
04730fef | 984 | __free_page(page); |
b36c31ba | 985 | fuse_invalidate_attr(inode); /* atime changed */ |
04730fef | 986 | return err; |
e5e5558e MS |
987 | } |
988 | ||
989 | static char *read_link(struct dentry *dentry) | |
990 | { | |
991 | struct inode *inode = dentry->d_inode; | |
992 | struct fuse_conn *fc = get_fuse_conn(inode); | |
ce1d5a49 | 993 | struct fuse_req *req = fuse_get_req(fc); |
e5e5558e MS |
994 | char *link; |
995 | ||
ce1d5a49 MS |
996 | if (IS_ERR(req)) |
997 | return ERR_PTR(PTR_ERR(req)); | |
e5e5558e MS |
998 | |
999 | link = (char *) __get_free_page(GFP_KERNEL); | |
1000 | if (!link) { | |
1001 | link = ERR_PTR(-ENOMEM); | |
1002 | goto out; | |
1003 | } | |
1004 | req->in.h.opcode = FUSE_READLINK; | |
1005 | req->in.h.nodeid = get_node_id(inode); | |
e5e5558e MS |
1006 | req->out.argvar = 1; |
1007 | req->out.numargs = 1; | |
1008 | req->out.args[0].size = PAGE_SIZE - 1; | |
1009 | req->out.args[0].value = link; | |
1010 | request_send(fc, req); | |
1011 | if (req->out.h.error) { | |
1012 | free_page((unsigned long) link); | |
1013 | link = ERR_PTR(req->out.h.error); | |
1014 | } else | |
1015 | link[req->out.args[0].size] = '\0'; | |
1016 | out: | |
1017 | fuse_put_request(fc, req); | |
b36c31ba | 1018 | fuse_invalidate_attr(inode); /* atime changed */ |
e5e5558e MS |
1019 | return link; |
1020 | } | |
1021 | ||
1022 | static void free_link(char *link) | |
1023 | { | |
1024 | if (!IS_ERR(link)) | |
1025 | free_page((unsigned long) link); | |
1026 | } | |
1027 | ||
1028 | static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd) | |
1029 | { | |
1030 | nd_set_link(nd, read_link(dentry)); | |
1031 | return NULL; | |
1032 | } | |
1033 | ||
1034 | static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c) | |
1035 | { | |
1036 | free_link(nd_get_link(nd)); | |
1037 | } | |
1038 | ||
1039 | static int fuse_dir_open(struct inode *inode, struct file *file) | |
1040 | { | |
04730fef | 1041 | return fuse_open_common(inode, file, 1); |
e5e5558e MS |
1042 | } |
1043 | ||
1044 | static int fuse_dir_release(struct inode *inode, struct file *file) | |
1045 | { | |
04730fef | 1046 | return fuse_release_common(inode, file, 1); |
e5e5558e MS |
1047 | } |
1048 | ||
82547981 MS |
1049 | static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync) |
1050 | { | |
1051 | /* nfsd can call this with no file */ | |
1052 | return file ? fuse_fsync_common(file, de, datasync, 1) : 0; | |
1053 | } | |
1054 | ||
17637cba MS |
1055 | static bool update_mtime(unsigned ivalid) |
1056 | { | |
1057 | /* Always update if mtime is explicitly set */ | |
1058 | if (ivalid & ATTR_MTIME_SET) | |
1059 | return true; | |
1060 | ||
1061 | /* If it's an open(O_TRUNC) or an ftruncate(), don't update */ | |
1062 | if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE))) | |
1063 | return false; | |
1064 | ||
1065 | /* In all other cases update */ | |
1066 | return true; | |
1067 | } | |
1068 | ||
befc649c | 1069 | static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg) |
9e6268db MS |
1070 | { |
1071 | unsigned ivalid = iattr->ia_valid; | |
9e6268db MS |
1072 | |
1073 | if (ivalid & ATTR_MODE) | |
befc649c | 1074 | arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode; |
9e6268db | 1075 | if (ivalid & ATTR_UID) |
befc649c | 1076 | arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid; |
9e6268db | 1077 | if (ivalid & ATTR_GID) |
befc649c | 1078 | arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid; |
9e6268db | 1079 | if (ivalid & ATTR_SIZE) |
befc649c | 1080 | arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size; |
17637cba MS |
1081 | if (ivalid & ATTR_ATIME) { |
1082 | arg->valid |= FATTR_ATIME; | |
befc649c | 1083 | arg->atime = iattr->ia_atime.tv_sec; |
17637cba MS |
1084 | arg->atimensec = iattr->ia_atime.tv_nsec; |
1085 | if (!(ivalid & ATTR_ATIME_SET)) | |
1086 | arg->valid |= FATTR_ATIME_NOW; | |
1087 | } | |
1088 | if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) { | |
1089 | arg->valid |= FATTR_MTIME; | |
befc649c | 1090 | arg->mtime = iattr->ia_mtime.tv_sec; |
17637cba MS |
1091 | arg->mtimensec = iattr->ia_mtime.tv_nsec; |
1092 | if (!(ivalid & ATTR_MTIME_SET)) | |
1093 | arg->valid |= FATTR_MTIME_NOW; | |
befc649c | 1094 | } |
9e6268db MS |
1095 | } |
1096 | ||
6f9f1180 MS |
1097 | /* |
1098 | * Set attributes, and at the same time refresh them. | |
1099 | * | |
1100 | * Truncation is slightly complicated, because the 'truncate' request | |
1101 | * may fail, in which case we don't want to touch the mapping. | |
9ffbb916 MS |
1102 | * vmtruncate() doesn't allow for this case, so do the rlimit checking |
1103 | * and the actual truncation by hand. | |
6f9f1180 | 1104 | */ |
49d4914f MS |
1105 | static int fuse_do_setattr(struct dentry *entry, struct iattr *attr, |
1106 | struct file *file) | |
9e6268db MS |
1107 | { |
1108 | struct inode *inode = entry->d_inode; | |
1109 | struct fuse_conn *fc = get_fuse_conn(inode); | |
9e6268db MS |
1110 | struct fuse_req *req; |
1111 | struct fuse_setattr_in inarg; | |
1112 | struct fuse_attr_out outarg; | |
1113 | int err; | |
9e6268db | 1114 | |
e57ac683 MS |
1115 | if (!fuse_allow_task(fc, current)) |
1116 | return -EACCES; | |
1117 | ||
1e9a4ed9 MS |
1118 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { |
1119 | err = inode_change_ok(inode, attr); | |
1120 | if (err) | |
1121 | return err; | |
1122 | } | |
1123 | ||
6ff958ed MS |
1124 | if ((attr->ia_valid & ATTR_OPEN) && fc->atomic_o_trunc) |
1125 | return 0; | |
1126 | ||
9e6268db MS |
1127 | if (attr->ia_valid & ATTR_SIZE) { |
1128 | unsigned long limit; | |
b2d2272f MS |
1129 | if (IS_SWAPFILE(inode)) |
1130 | return -ETXTBSY; | |
9e6268db MS |
1131 | limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur; |
1132 | if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) { | |
1133 | send_sig(SIGXFSZ, current, 0); | |
1134 | return -EFBIG; | |
1135 | } | |
1136 | } | |
1137 | ||
ce1d5a49 MS |
1138 | req = fuse_get_req(fc); |
1139 | if (IS_ERR(req)) | |
1140 | return PTR_ERR(req); | |
9e6268db MS |
1141 | |
1142 | memset(&inarg, 0, sizeof(inarg)); | |
0e9663ee | 1143 | memset(&outarg, 0, sizeof(outarg)); |
befc649c | 1144 | iattr_to_fattr(attr, &inarg); |
49d4914f MS |
1145 | if (file) { |
1146 | struct fuse_file *ff = file->private_data; | |
1147 | inarg.valid |= FATTR_FH; | |
1148 | inarg.fh = ff->fh; | |
1149 | } | |
f3332114 MS |
1150 | if (attr->ia_valid & ATTR_SIZE) { |
1151 | /* For mandatory locking in truncate */ | |
1152 | inarg.valid |= FATTR_LOCKOWNER; | |
1153 | inarg.lock_owner = fuse_lock_owner_id(fc, current->files); | |
1154 | } | |
9e6268db MS |
1155 | req->in.h.opcode = FUSE_SETATTR; |
1156 | req->in.h.nodeid = get_node_id(inode); | |
9e6268db MS |
1157 | req->in.numargs = 1; |
1158 | req->in.args[0].size = sizeof(inarg); | |
1159 | req->in.args[0].value = &inarg; | |
1160 | req->out.numargs = 1; | |
0e9663ee MS |
1161 | if (fc->minor < 9) |
1162 | req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; | |
1163 | else | |
1164 | req->out.args[0].size = sizeof(outarg); | |
9e6268db MS |
1165 | req->out.args[0].value = &outarg; |
1166 | request_send(fc, req); | |
1167 | err = req->out.h.error; | |
1168 | fuse_put_request(fc, req); | |
e00d2c2d MS |
1169 | if (err) { |
1170 | if (err == -EINTR) | |
1171 | fuse_invalidate_attr(inode); | |
1172 | return err; | |
1173 | } | |
9e6268db | 1174 | |
e00d2c2d MS |
1175 | if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { |
1176 | make_bad_inode(inode); | |
1177 | return -EIO; | |
1178 | } | |
1179 | ||
1fb69e78 | 1180 | fuse_change_attributes(inode, &outarg.attr, attr_timeout(&outarg), 0); |
e00d2c2d | 1181 | return 0; |
9e6268db MS |
1182 | } |
1183 | ||
49d4914f MS |
1184 | static int fuse_setattr(struct dentry *entry, struct iattr *attr) |
1185 | { | |
1186 | if (attr->ia_valid & ATTR_FILE) | |
1187 | return fuse_do_setattr(entry, attr, attr->ia_file); | |
1188 | else | |
1189 | return fuse_do_setattr(entry, attr, NULL); | |
1190 | } | |
1191 | ||
e5e5558e MS |
1192 | static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry, |
1193 | struct kstat *stat) | |
1194 | { | |
1195 | struct inode *inode = entry->d_inode; | |
244f6385 | 1196 | struct fuse_conn *fc = get_fuse_conn(inode); |
244f6385 MS |
1197 | |
1198 | if (!fuse_allow_task(fc, current)) | |
1199 | return -EACCES; | |
1200 | ||
bcb4be80 | 1201 | return fuse_update_attributes(inode, stat, NULL, NULL); |
e5e5558e MS |
1202 | } |
1203 | ||
92a8780e MS |
1204 | static int fuse_setxattr(struct dentry *entry, const char *name, |
1205 | const void *value, size_t size, int flags) | |
1206 | { | |
1207 | struct inode *inode = entry->d_inode; | |
1208 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1209 | struct fuse_req *req; | |
1210 | struct fuse_setxattr_in inarg; | |
1211 | int err; | |
1212 | ||
92a8780e MS |
1213 | if (fc->no_setxattr) |
1214 | return -EOPNOTSUPP; | |
1215 | ||
ce1d5a49 MS |
1216 | req = fuse_get_req(fc); |
1217 | if (IS_ERR(req)) | |
1218 | return PTR_ERR(req); | |
92a8780e MS |
1219 | |
1220 | memset(&inarg, 0, sizeof(inarg)); | |
1221 | inarg.size = size; | |
1222 | inarg.flags = flags; | |
1223 | req->in.h.opcode = FUSE_SETXATTR; | |
1224 | req->in.h.nodeid = get_node_id(inode); | |
92a8780e MS |
1225 | req->in.numargs = 3; |
1226 | req->in.args[0].size = sizeof(inarg); | |
1227 | req->in.args[0].value = &inarg; | |
1228 | req->in.args[1].size = strlen(name) + 1; | |
1229 | req->in.args[1].value = name; | |
1230 | req->in.args[2].size = size; | |
1231 | req->in.args[2].value = value; | |
1232 | request_send(fc, req); | |
1233 | err = req->out.h.error; | |
1234 | fuse_put_request(fc, req); | |
1235 | if (err == -ENOSYS) { | |
1236 | fc->no_setxattr = 1; | |
1237 | err = -EOPNOTSUPP; | |
1238 | } | |
1239 | return err; | |
1240 | } | |
1241 | ||
1242 | static ssize_t fuse_getxattr(struct dentry *entry, const char *name, | |
1243 | void *value, size_t size) | |
1244 | { | |
1245 | struct inode *inode = entry->d_inode; | |
1246 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1247 | struct fuse_req *req; | |
1248 | struct fuse_getxattr_in inarg; | |
1249 | struct fuse_getxattr_out outarg; | |
1250 | ssize_t ret; | |
1251 | ||
1252 | if (fc->no_getxattr) | |
1253 | return -EOPNOTSUPP; | |
1254 | ||
ce1d5a49 MS |
1255 | req = fuse_get_req(fc); |
1256 | if (IS_ERR(req)) | |
1257 | return PTR_ERR(req); | |
92a8780e MS |
1258 | |
1259 | memset(&inarg, 0, sizeof(inarg)); | |
1260 | inarg.size = size; | |
1261 | req->in.h.opcode = FUSE_GETXATTR; | |
1262 | req->in.h.nodeid = get_node_id(inode); | |
92a8780e MS |
1263 | req->in.numargs = 2; |
1264 | req->in.args[0].size = sizeof(inarg); | |
1265 | req->in.args[0].value = &inarg; | |
1266 | req->in.args[1].size = strlen(name) + 1; | |
1267 | req->in.args[1].value = name; | |
1268 | /* This is really two different operations rolled into one */ | |
1269 | req->out.numargs = 1; | |
1270 | if (size) { | |
1271 | req->out.argvar = 1; | |
1272 | req->out.args[0].size = size; | |
1273 | req->out.args[0].value = value; | |
1274 | } else { | |
1275 | req->out.args[0].size = sizeof(outarg); | |
1276 | req->out.args[0].value = &outarg; | |
1277 | } | |
1278 | request_send(fc, req); | |
1279 | ret = req->out.h.error; | |
1280 | if (!ret) | |
1281 | ret = size ? req->out.args[0].size : outarg.size; | |
1282 | else { | |
1283 | if (ret == -ENOSYS) { | |
1284 | fc->no_getxattr = 1; | |
1285 | ret = -EOPNOTSUPP; | |
1286 | } | |
1287 | } | |
1288 | fuse_put_request(fc, req); | |
1289 | return ret; | |
1290 | } | |
1291 | ||
1292 | static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size) | |
1293 | { | |
1294 | struct inode *inode = entry->d_inode; | |
1295 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1296 | struct fuse_req *req; | |
1297 | struct fuse_getxattr_in inarg; | |
1298 | struct fuse_getxattr_out outarg; | |
1299 | ssize_t ret; | |
1300 | ||
e57ac683 MS |
1301 | if (!fuse_allow_task(fc, current)) |
1302 | return -EACCES; | |
1303 | ||
92a8780e MS |
1304 | if (fc->no_listxattr) |
1305 | return -EOPNOTSUPP; | |
1306 | ||
ce1d5a49 MS |
1307 | req = fuse_get_req(fc); |
1308 | if (IS_ERR(req)) | |
1309 | return PTR_ERR(req); | |
92a8780e MS |
1310 | |
1311 | memset(&inarg, 0, sizeof(inarg)); | |
1312 | inarg.size = size; | |
1313 | req->in.h.opcode = FUSE_LISTXATTR; | |
1314 | req->in.h.nodeid = get_node_id(inode); | |
92a8780e MS |
1315 | req->in.numargs = 1; |
1316 | req->in.args[0].size = sizeof(inarg); | |
1317 | req->in.args[0].value = &inarg; | |
1318 | /* This is really two different operations rolled into one */ | |
1319 | req->out.numargs = 1; | |
1320 | if (size) { | |
1321 | req->out.argvar = 1; | |
1322 | req->out.args[0].size = size; | |
1323 | req->out.args[0].value = list; | |
1324 | } else { | |
1325 | req->out.args[0].size = sizeof(outarg); | |
1326 | req->out.args[0].value = &outarg; | |
1327 | } | |
1328 | request_send(fc, req); | |
1329 | ret = req->out.h.error; | |
1330 | if (!ret) | |
1331 | ret = size ? req->out.args[0].size : outarg.size; | |
1332 | else { | |
1333 | if (ret == -ENOSYS) { | |
1334 | fc->no_listxattr = 1; | |
1335 | ret = -EOPNOTSUPP; | |
1336 | } | |
1337 | } | |
1338 | fuse_put_request(fc, req); | |
1339 | return ret; | |
1340 | } | |
1341 | ||
1342 | static int fuse_removexattr(struct dentry *entry, const char *name) | |
1343 | { | |
1344 | struct inode *inode = entry->d_inode; | |
1345 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1346 | struct fuse_req *req; | |
1347 | int err; | |
1348 | ||
1349 | if (fc->no_removexattr) | |
1350 | return -EOPNOTSUPP; | |
1351 | ||
ce1d5a49 MS |
1352 | req = fuse_get_req(fc); |
1353 | if (IS_ERR(req)) | |
1354 | return PTR_ERR(req); | |
92a8780e MS |
1355 | |
1356 | req->in.h.opcode = FUSE_REMOVEXATTR; | |
1357 | req->in.h.nodeid = get_node_id(inode); | |
92a8780e MS |
1358 | req->in.numargs = 1; |
1359 | req->in.args[0].size = strlen(name) + 1; | |
1360 | req->in.args[0].value = name; | |
1361 | request_send(fc, req); | |
1362 | err = req->out.h.error; | |
1363 | fuse_put_request(fc, req); | |
1364 | if (err == -ENOSYS) { | |
1365 | fc->no_removexattr = 1; | |
1366 | err = -EOPNOTSUPP; | |
1367 | } | |
1368 | return err; | |
1369 | } | |
1370 | ||
754661f1 | 1371 | static const struct inode_operations fuse_dir_inode_operations = { |
e5e5558e | 1372 | .lookup = fuse_lookup, |
9e6268db MS |
1373 | .mkdir = fuse_mkdir, |
1374 | .symlink = fuse_symlink, | |
1375 | .unlink = fuse_unlink, | |
1376 | .rmdir = fuse_rmdir, | |
1377 | .rename = fuse_rename, | |
1378 | .link = fuse_link, | |
1379 | .setattr = fuse_setattr, | |
1380 | .create = fuse_create, | |
1381 | .mknod = fuse_mknod, | |
e5e5558e MS |
1382 | .permission = fuse_permission, |
1383 | .getattr = fuse_getattr, | |
92a8780e MS |
1384 | .setxattr = fuse_setxattr, |
1385 | .getxattr = fuse_getxattr, | |
1386 | .listxattr = fuse_listxattr, | |
1387 | .removexattr = fuse_removexattr, | |
e5e5558e MS |
1388 | }; |
1389 | ||
4b6f5d20 | 1390 | static const struct file_operations fuse_dir_operations = { |
b6aeaded | 1391 | .llseek = generic_file_llseek, |
e5e5558e MS |
1392 | .read = generic_read_dir, |
1393 | .readdir = fuse_readdir, | |
1394 | .open = fuse_dir_open, | |
1395 | .release = fuse_dir_release, | |
82547981 | 1396 | .fsync = fuse_dir_fsync, |
e5e5558e MS |
1397 | }; |
1398 | ||
754661f1 | 1399 | static const struct inode_operations fuse_common_inode_operations = { |
9e6268db | 1400 | .setattr = fuse_setattr, |
e5e5558e MS |
1401 | .permission = fuse_permission, |
1402 | .getattr = fuse_getattr, | |
92a8780e MS |
1403 | .setxattr = fuse_setxattr, |
1404 | .getxattr = fuse_getxattr, | |
1405 | .listxattr = fuse_listxattr, | |
1406 | .removexattr = fuse_removexattr, | |
e5e5558e MS |
1407 | }; |
1408 | ||
754661f1 | 1409 | static const struct inode_operations fuse_symlink_inode_operations = { |
9e6268db | 1410 | .setattr = fuse_setattr, |
e5e5558e MS |
1411 | .follow_link = fuse_follow_link, |
1412 | .put_link = fuse_put_link, | |
1413 | .readlink = generic_readlink, | |
1414 | .getattr = fuse_getattr, | |
92a8780e MS |
1415 | .setxattr = fuse_setxattr, |
1416 | .getxattr = fuse_getxattr, | |
1417 | .listxattr = fuse_listxattr, | |
1418 | .removexattr = fuse_removexattr, | |
e5e5558e MS |
1419 | }; |
1420 | ||
1421 | void fuse_init_common(struct inode *inode) | |
1422 | { | |
1423 | inode->i_op = &fuse_common_inode_operations; | |
1424 | } | |
1425 | ||
1426 | void fuse_init_dir(struct inode *inode) | |
1427 | { | |
1428 | inode->i_op = &fuse_dir_inode_operations; | |
1429 | inode->i_fop = &fuse_dir_operations; | |
1430 | } | |
1431 | ||
1432 | void fuse_init_symlink(struct inode *inode) | |
1433 | { | |
1434 | inode->i_op = &fuse_symlink_inode_operations; | |
1435 | } |