Commit | Line | Data |
---|---|---|
e5e5558e MS |
1 | /* |
2 | FUSE: Filesystem in Userspace | |
1729a16c | 3 | Copyright (C) 2001-2008 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> | |
e5e5558e MS |
13 | #include <linux/sched.h> |
14 | #include <linux/namei.h> | |
07e77dca | 15 | #include <linux/slab.h> |
e5e5558e | 16 | |
4582a4ab FS |
17 | static bool fuse_use_readdirplus(struct inode *dir, struct file *filp) |
18 | { | |
19 | struct fuse_conn *fc = get_fuse_conn(dir); | |
20 | struct fuse_inode *fi = get_fuse_inode(dir); | |
21 | ||
22 | if (!fc->do_readdirplus) | |
23 | return false; | |
24 | if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state)) | |
25 | return true; | |
26 | if (filp->f_pos == 0) | |
27 | return true; | |
28 | return false; | |
29 | } | |
30 | ||
31 | static void fuse_advise_use_readdirplus(struct inode *dir) | |
32 | { | |
33 | struct fuse_inode *fi = get_fuse_inode(dir); | |
34 | ||
35 | set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state); | |
36 | } | |
37 | ||
0a0898cf MS |
38 | #if BITS_PER_LONG >= 64 |
39 | static inline void fuse_dentry_settime(struct dentry *entry, u64 time) | |
40 | { | |
41 | entry->d_time = time; | |
42 | } | |
43 | ||
44 | static inline u64 fuse_dentry_time(struct dentry *entry) | |
45 | { | |
46 | return entry->d_time; | |
47 | } | |
48 | #else | |
49 | /* | |
50 | * On 32 bit archs store the high 32 bits of time in d_fsdata | |
51 | */ | |
52 | static void fuse_dentry_settime(struct dentry *entry, u64 time) | |
53 | { | |
54 | entry->d_time = time; | |
55 | entry->d_fsdata = (void *) (unsigned long) (time >> 32); | |
56 | } | |
57 | ||
58 | static u64 fuse_dentry_time(struct dentry *entry) | |
59 | { | |
60 | return (u64) entry->d_time + | |
61 | ((u64) (unsigned long) entry->d_fsdata << 32); | |
62 | } | |
63 | #endif | |
64 | ||
6f9f1180 MS |
65 | /* |
66 | * FUSE caches dentries and attributes with separate timeout. The | |
67 | * time in jiffies until the dentry/attributes are valid is stored in | |
68 | * dentry->d_time and fuse_inode->i_time respectively. | |
69 | */ | |
70 | ||
71 | /* | |
72 | * Calculate the time in jiffies until a dentry/attributes are valid | |
73 | */ | |
0a0898cf | 74 | static u64 time_to_jiffies(unsigned long sec, unsigned long nsec) |
e5e5558e | 75 | { |
685d16dd MS |
76 | if (sec || nsec) { |
77 | struct timespec ts = {sec, nsec}; | |
0a0898cf | 78 | return get_jiffies_64() + timespec_to_jiffies(&ts); |
685d16dd | 79 | } else |
0a0898cf | 80 | return 0; |
e5e5558e MS |
81 | } |
82 | ||
6f9f1180 MS |
83 | /* |
84 | * Set dentry and possibly attribute timeouts from the lookup/mk* | |
85 | * replies | |
86 | */ | |
1fb69e78 MS |
87 | static void fuse_change_entry_timeout(struct dentry *entry, |
88 | struct fuse_entry_out *o) | |
0aa7c699 | 89 | { |
0a0898cf MS |
90 | fuse_dentry_settime(entry, |
91 | time_to_jiffies(o->entry_valid, o->entry_valid_nsec)); | |
1fb69e78 MS |
92 | } |
93 | ||
94 | static u64 attr_timeout(struct fuse_attr_out *o) | |
95 | { | |
96 | return time_to_jiffies(o->attr_valid, o->attr_valid_nsec); | |
97 | } | |
98 | ||
99 | static u64 entry_attr_timeout(struct fuse_entry_out *o) | |
100 | { | |
101 | return time_to_jiffies(o->attr_valid, o->attr_valid_nsec); | |
8cbdf1e6 MS |
102 | } |
103 | ||
6f9f1180 MS |
104 | /* |
105 | * Mark the attributes as stale, so that at the next call to | |
106 | * ->getattr() they will be fetched from userspace | |
107 | */ | |
8cbdf1e6 MS |
108 | void fuse_invalidate_attr(struct inode *inode) |
109 | { | |
0a0898cf | 110 | get_fuse_inode(inode)->i_time = 0; |
8cbdf1e6 MS |
111 | } |
112 | ||
6f9f1180 MS |
113 | /* |
114 | * Just mark the entry as stale, so that a next attempt to look it up | |
115 | * will result in a new lookup call to userspace | |
116 | * | |
117 | * This is called when a dentry is about to become negative and the | |
118 | * timeout is unknown (unlink, rmdir, rename and in some cases | |
119 | * lookup) | |
120 | */ | |
dbd561d2 | 121 | void fuse_invalidate_entry_cache(struct dentry *entry) |
8cbdf1e6 | 122 | { |
0a0898cf | 123 | fuse_dentry_settime(entry, 0); |
8cbdf1e6 MS |
124 | } |
125 | ||
6f9f1180 MS |
126 | /* |
127 | * Same as fuse_invalidate_entry_cache(), but also try to remove the | |
128 | * dentry from the hash | |
129 | */ | |
8cbdf1e6 MS |
130 | static void fuse_invalidate_entry(struct dentry *entry) |
131 | { | |
132 | d_invalidate(entry); | |
133 | fuse_invalidate_entry_cache(entry); | |
0aa7c699 MS |
134 | } |
135 | ||
c180eebe MS |
136 | static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_req *req, |
137 | u64 nodeid, struct qstr *name, | |
e5e5558e MS |
138 | struct fuse_entry_out *outarg) |
139 | { | |
0e9663ee | 140 | memset(outarg, 0, sizeof(struct fuse_entry_out)); |
e5e5558e | 141 | req->in.h.opcode = FUSE_LOOKUP; |
c180eebe | 142 | req->in.h.nodeid = nodeid; |
e5e5558e | 143 | req->in.numargs = 1; |
c180eebe MS |
144 | req->in.args[0].size = name->len + 1; |
145 | req->in.args[0].value = name->name; | |
e5e5558e | 146 | req->out.numargs = 1; |
0e9663ee MS |
147 | if (fc->minor < 9) |
148 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; | |
149 | else | |
150 | req->out.args[0].size = sizeof(struct fuse_entry_out); | |
e5e5558e MS |
151 | req->out.args[0].value = outarg; |
152 | } | |
153 | ||
5c5c5e51 | 154 | u64 fuse_get_attr_version(struct fuse_conn *fc) |
7dca9fd3 MS |
155 | { |
156 | u64 curr_version; | |
157 | ||
158 | /* | |
159 | * The spin lock isn't actually needed on 64bit archs, but we | |
160 | * don't yet care too much about such optimizations. | |
161 | */ | |
162 | spin_lock(&fc->lock); | |
163 | curr_version = fc->attr_version; | |
164 | spin_unlock(&fc->lock); | |
165 | ||
166 | return curr_version; | |
167 | } | |
168 | ||
6f9f1180 MS |
169 | /* |
170 | * Check whether the dentry is still valid | |
171 | * | |
172 | * If the entry validity timeout has expired and the dentry is | |
173 | * positive, try to redo the lookup. If the lookup results in a | |
174 | * different inode, then let the VFS invalidate the dentry and redo | |
175 | * the lookup once more. If the lookup results in the same inode, | |
176 | * then refresh the attributes, timeouts and mark the dentry valid. | |
177 | */ | |
0b728e19 | 178 | static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags) |
e5e5558e | 179 | { |
34286d66 | 180 | struct inode *inode; |
8cbdf1e6 | 181 | |
e7c0a167 | 182 | inode = ACCESS_ONCE(entry->d_inode); |
8cbdf1e6 | 183 | if (inode && is_bad_inode(inode)) |
e5e5558e | 184 | return 0; |
0a0898cf | 185 | else if (fuse_dentry_time(entry) < get_jiffies_64()) { |
e5e5558e | 186 | int err; |
e5e5558e | 187 | struct fuse_entry_out outarg; |
8cbdf1e6 MS |
188 | struct fuse_conn *fc; |
189 | struct fuse_req *req; | |
07e77dca | 190 | struct fuse_forget_link *forget; |
e956edd0 | 191 | struct dentry *parent; |
1fb69e78 | 192 | u64 attr_version; |
8cbdf1e6 | 193 | |
50322fe7 | 194 | /* For negative dentries, always do a fresh lookup */ |
8cbdf1e6 MS |
195 | if (!inode) |
196 | return 0; | |
197 | ||
0b728e19 | 198 | if (flags & LOOKUP_RCU) |
e7c0a167 MS |
199 | return -ECHILD; |
200 | ||
8cbdf1e6 | 201 | fc = get_fuse_conn(inode); |
b111c8c0 | 202 | req = fuse_get_req_nopages(fc); |
ce1d5a49 | 203 | if (IS_ERR(req)) |
e5e5558e MS |
204 | return 0; |
205 | ||
07e77dca MS |
206 | forget = fuse_alloc_forget(); |
207 | if (!forget) { | |
2d51013e MS |
208 | fuse_put_request(fc, req); |
209 | return 0; | |
210 | } | |
211 | ||
7dca9fd3 | 212 | attr_version = fuse_get_attr_version(fc); |
1fb69e78 | 213 | |
e956edd0 | 214 | parent = dget_parent(entry); |
c180eebe MS |
215 | fuse_lookup_init(fc, req, get_node_id(parent->d_inode), |
216 | &entry->d_name, &outarg); | |
b93f858a | 217 | fuse_request_send(fc, req); |
e956edd0 | 218 | dput(parent); |
e5e5558e | 219 | err = req->out.h.error; |
2d51013e | 220 | fuse_put_request(fc, req); |
50322fe7 MS |
221 | /* Zero nodeid is same as -ENOENT */ |
222 | if (!err && !outarg.nodeid) | |
223 | err = -ENOENT; | |
9e6268db | 224 | if (!err) { |
8cbdf1e6 | 225 | struct fuse_inode *fi = get_fuse_inode(inode); |
9e6268db | 226 | if (outarg.nodeid != get_node_id(inode)) { |
07e77dca | 227 | fuse_queue_forget(fc, forget, outarg.nodeid, 1); |
9e6268db MS |
228 | return 0; |
229 | } | |
8da5ff23 | 230 | spin_lock(&fc->lock); |
1729a16c | 231 | fi->nlookup++; |
8da5ff23 | 232 | spin_unlock(&fc->lock); |
9e6268db | 233 | } |
07e77dca | 234 | kfree(forget); |
9e6268db | 235 | if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT) |
e5e5558e MS |
236 | return 0; |
237 | ||
1fb69e78 MS |
238 | fuse_change_attributes(inode, &outarg.attr, |
239 | entry_attr_timeout(&outarg), | |
240 | attr_version); | |
241 | fuse_change_entry_timeout(entry, &outarg); | |
e5e5558e | 242 | } |
4582a4ab | 243 | fuse_advise_use_readdirplus(inode); |
e5e5558e MS |
244 | return 1; |
245 | } | |
246 | ||
8bfc016d | 247 | static int invalid_nodeid(u64 nodeid) |
2827d0b2 MS |
248 | { |
249 | return !nodeid || nodeid == FUSE_ROOT_ID; | |
250 | } | |
251 | ||
4269590a | 252 | const struct dentry_operations fuse_dentry_operations = { |
e5e5558e MS |
253 | .d_revalidate = fuse_dentry_revalidate, |
254 | }; | |
255 | ||
a5bfffac | 256 | int fuse_valid_type(int m) |
39ee059a MS |
257 | { |
258 | return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) || | |
259 | S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m); | |
260 | } | |
261 | ||
d2a85164 MS |
262 | /* |
263 | * Add a directory inode to a dentry, ensuring that no other dentry | |
264 | * refers to this inode. Called with fc->inst_mutex. | |
265 | */ | |
0de6256d MS |
266 | static struct dentry *fuse_d_add_directory(struct dentry *entry, |
267 | struct inode *inode) | |
d2a85164 MS |
268 | { |
269 | struct dentry *alias = d_find_alias(inode); | |
0de6256d | 270 | if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) { |
d2a85164 MS |
271 | /* This tries to shrink the subtree below alias */ |
272 | fuse_invalidate_entry(alias); | |
273 | dput(alias); | |
b3d9b7a3 | 274 | if (!hlist_empty(&inode->i_dentry)) |
0de6256d MS |
275 | return ERR_PTR(-EBUSY); |
276 | } else { | |
277 | dput(alias); | |
d2a85164 | 278 | } |
0de6256d | 279 | return d_splice_alias(inode, entry); |
d2a85164 MS |
280 | } |
281 | ||
c180eebe MS |
282 | int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name, |
283 | struct fuse_entry_out *outarg, struct inode **inode) | |
e5e5558e | 284 | { |
c180eebe | 285 | struct fuse_conn *fc = get_fuse_conn_super(sb); |
e5e5558e | 286 | struct fuse_req *req; |
07e77dca | 287 | struct fuse_forget_link *forget; |
1fb69e78 | 288 | u64 attr_version; |
c180eebe | 289 | int err; |
e5e5558e | 290 | |
c180eebe MS |
291 | *inode = NULL; |
292 | err = -ENAMETOOLONG; | |
293 | if (name->len > FUSE_NAME_MAX) | |
294 | goto out; | |
e5e5558e | 295 | |
b111c8c0 | 296 | req = fuse_get_req_nopages(fc); |
c180eebe | 297 | err = PTR_ERR(req); |
ce1d5a49 | 298 | if (IS_ERR(req)) |
c180eebe | 299 | goto out; |
e5e5558e | 300 | |
07e77dca MS |
301 | forget = fuse_alloc_forget(); |
302 | err = -ENOMEM; | |
303 | if (!forget) { | |
2d51013e | 304 | fuse_put_request(fc, req); |
c180eebe | 305 | goto out; |
2d51013e MS |
306 | } |
307 | ||
7dca9fd3 | 308 | attr_version = fuse_get_attr_version(fc); |
1fb69e78 | 309 | |
c180eebe | 310 | fuse_lookup_init(fc, req, nodeid, name, outarg); |
b93f858a | 311 | fuse_request_send(fc, req); |
e5e5558e | 312 | err = req->out.h.error; |
2d51013e | 313 | fuse_put_request(fc, req); |
50322fe7 | 314 | /* Zero nodeid is same as -ENOENT, but with valid timeout */ |
c180eebe MS |
315 | if (err || !outarg->nodeid) |
316 | goto out_put_forget; | |
317 | ||
318 | err = -EIO; | |
319 | if (!outarg->nodeid) | |
320 | goto out_put_forget; | |
321 | if (!fuse_valid_type(outarg->attr.mode)) | |
322 | goto out_put_forget; | |
323 | ||
324 | *inode = fuse_iget(sb, outarg->nodeid, outarg->generation, | |
325 | &outarg->attr, entry_attr_timeout(outarg), | |
326 | attr_version); | |
327 | err = -ENOMEM; | |
328 | if (!*inode) { | |
07e77dca | 329 | fuse_queue_forget(fc, forget, outarg->nodeid, 1); |
c180eebe | 330 | goto out; |
e5e5558e | 331 | } |
c180eebe MS |
332 | err = 0; |
333 | ||
334 | out_put_forget: | |
07e77dca | 335 | kfree(forget); |
c180eebe MS |
336 | out: |
337 | return err; | |
338 | } | |
339 | ||
340 | static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry, | |
00cd8dd3 | 341 | unsigned int flags) |
c180eebe MS |
342 | { |
343 | int err; | |
344 | struct fuse_entry_out outarg; | |
345 | struct inode *inode; | |
346 | struct dentry *newent; | |
347 | struct fuse_conn *fc = get_fuse_conn(dir); | |
348 | bool outarg_valid = true; | |
349 | ||
350 | err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name, | |
351 | &outarg, &inode); | |
352 | if (err == -ENOENT) { | |
353 | outarg_valid = false; | |
354 | err = 0; | |
355 | } | |
356 | if (err) | |
357 | goto out_err; | |
358 | ||
359 | err = -EIO; | |
360 | if (inode && get_node_id(inode) == FUSE_ROOT_ID) | |
361 | goto out_iput; | |
e5e5558e | 362 | |
d2a85164 MS |
363 | if (inode && S_ISDIR(inode->i_mode)) { |
364 | mutex_lock(&fc->inst_mutex); | |
0de6256d | 365 | newent = fuse_d_add_directory(entry, inode); |
d2a85164 | 366 | mutex_unlock(&fc->inst_mutex); |
c180eebe MS |
367 | err = PTR_ERR(newent); |
368 | if (IS_ERR(newent)) | |
369 | goto out_iput; | |
370 | } else { | |
0de6256d | 371 | newent = d_splice_alias(inode, entry); |
c180eebe | 372 | } |
d2a85164 | 373 | |
0de6256d | 374 | entry = newent ? newent : entry; |
c180eebe | 375 | if (outarg_valid) |
1fb69e78 | 376 | fuse_change_entry_timeout(entry, &outarg); |
8cbdf1e6 MS |
377 | else |
378 | fuse_invalidate_entry_cache(entry); | |
c180eebe | 379 | |
4582a4ab | 380 | fuse_advise_use_readdirplus(dir); |
0de6256d | 381 | return newent; |
c180eebe MS |
382 | |
383 | out_iput: | |
384 | iput(inode); | |
385 | out_err: | |
386 | return ERR_PTR(err); | |
e5e5558e MS |
387 | } |
388 | ||
6f9f1180 MS |
389 | /* |
390 | * Atomic create+open operation | |
391 | * | |
392 | * If the filesystem doesn't support this, then fall back to separate | |
393 | * 'mknod' + 'open' requests. | |
394 | */ | |
d9585277 | 395 | static int fuse_create_open(struct inode *dir, struct dentry *entry, |
30d90494 | 396 | struct file *file, unsigned flags, |
d9585277 | 397 | umode_t mode, int *opened) |
fd72faac MS |
398 | { |
399 | int err; | |
400 | struct inode *inode; | |
401 | struct fuse_conn *fc = get_fuse_conn(dir); | |
402 | struct fuse_req *req; | |
07e77dca | 403 | struct fuse_forget_link *forget; |
e0a43ddc | 404 | struct fuse_create_in inarg; |
fd72faac MS |
405 | struct fuse_open_out outopen; |
406 | struct fuse_entry_out outentry; | |
fd72faac | 407 | struct fuse_file *ff; |
fd72faac | 408 | |
af109bca MS |
409 | /* Userspace expects S_IFREG in create mode */ |
410 | BUG_ON((mode & S_IFMT) != S_IFREG); | |
411 | ||
07e77dca | 412 | forget = fuse_alloc_forget(); |
c8ccbe03 | 413 | err = -ENOMEM; |
07e77dca | 414 | if (!forget) |
c8ccbe03 | 415 | goto out_err; |
51eb01e7 | 416 | |
b111c8c0 | 417 | req = fuse_get_req_nopages(fc); |
51eb01e7 | 418 | err = PTR_ERR(req); |
ce1d5a49 | 419 | if (IS_ERR(req)) |
51eb01e7 | 420 | goto out_put_forget_req; |
fd72faac | 421 | |
ce1d5a49 | 422 | err = -ENOMEM; |
acf99433 | 423 | ff = fuse_file_alloc(fc); |
fd72faac MS |
424 | if (!ff) |
425 | goto out_put_request; | |
426 | ||
e0a43ddc MS |
427 | if (!fc->dont_mask) |
428 | mode &= ~current_umask(); | |
429 | ||
fd72faac MS |
430 | flags &= ~O_NOCTTY; |
431 | memset(&inarg, 0, sizeof(inarg)); | |
0e9663ee | 432 | memset(&outentry, 0, sizeof(outentry)); |
fd72faac MS |
433 | inarg.flags = flags; |
434 | inarg.mode = mode; | |
e0a43ddc | 435 | inarg.umask = current_umask(); |
fd72faac MS |
436 | req->in.h.opcode = FUSE_CREATE; |
437 | req->in.h.nodeid = get_node_id(dir); | |
fd72faac | 438 | req->in.numargs = 2; |
e0a43ddc MS |
439 | req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) : |
440 | sizeof(inarg); | |
fd72faac MS |
441 | req->in.args[0].value = &inarg; |
442 | req->in.args[1].size = entry->d_name.len + 1; | |
443 | req->in.args[1].value = entry->d_name.name; | |
444 | req->out.numargs = 2; | |
0e9663ee MS |
445 | if (fc->minor < 9) |
446 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; | |
447 | else | |
448 | req->out.args[0].size = sizeof(outentry); | |
fd72faac MS |
449 | req->out.args[0].value = &outentry; |
450 | req->out.args[1].size = sizeof(outopen); | |
451 | req->out.args[1].value = &outopen; | |
b93f858a | 452 | fuse_request_send(fc, req); |
fd72faac | 453 | err = req->out.h.error; |
c8ccbe03 | 454 | if (err) |
fd72faac | 455 | goto out_free_ff; |
fd72faac MS |
456 | |
457 | err = -EIO; | |
2827d0b2 | 458 | if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid)) |
fd72faac MS |
459 | goto out_free_ff; |
460 | ||
51eb01e7 | 461 | fuse_put_request(fc, req); |
c7b7143c MS |
462 | ff->fh = outopen.fh; |
463 | ff->nodeid = outentry.nodeid; | |
464 | ff->open_flags = outopen.open_flags; | |
fd72faac | 465 | inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation, |
1fb69e78 | 466 | &outentry.attr, entry_attr_timeout(&outentry), 0); |
fd72faac MS |
467 | if (!inode) { |
468 | flags &= ~(O_CREAT | O_EXCL | O_TRUNC); | |
8b0797a4 | 469 | fuse_sync_release(ff, flags); |
07e77dca | 470 | fuse_queue_forget(fc, forget, outentry.nodeid, 1); |
c8ccbe03 MS |
471 | err = -ENOMEM; |
472 | goto out_err; | |
fd72faac | 473 | } |
07e77dca | 474 | kfree(forget); |
fd72faac | 475 | d_instantiate(entry, inode); |
1fb69e78 | 476 | fuse_change_entry_timeout(entry, &outentry); |
0952b2a4 | 477 | fuse_invalidate_attr(dir); |
30d90494 AV |
478 | err = finish_open(file, entry, generic_file_open, opened); |
479 | if (err) { | |
8b0797a4 | 480 | fuse_sync_release(ff, flags); |
c8ccbe03 MS |
481 | } else { |
482 | file->private_data = fuse_file_get(ff); | |
483 | fuse_finish_open(inode, file); | |
fd72faac | 484 | } |
d9585277 | 485 | return err; |
fd72faac | 486 | |
c8ccbe03 | 487 | out_free_ff: |
fd72faac | 488 | fuse_file_free(ff); |
c8ccbe03 | 489 | out_put_request: |
fd72faac | 490 | fuse_put_request(fc, req); |
c8ccbe03 | 491 | out_put_forget_req: |
07e77dca | 492 | kfree(forget); |
c8ccbe03 | 493 | out_err: |
d9585277 | 494 | return err; |
c8ccbe03 MS |
495 | } |
496 | ||
497 | static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t); | |
d9585277 | 498 | static int fuse_atomic_open(struct inode *dir, struct dentry *entry, |
30d90494 | 499 | struct file *file, unsigned flags, |
d9585277 | 500 | umode_t mode, int *opened) |
c8ccbe03 MS |
501 | { |
502 | int err; | |
503 | struct fuse_conn *fc = get_fuse_conn(dir); | |
c8ccbe03 MS |
504 | struct dentry *res = NULL; |
505 | ||
506 | if (d_unhashed(entry)) { | |
00cd8dd3 | 507 | res = fuse_lookup(dir, entry, 0); |
c8ccbe03 | 508 | if (IS_ERR(res)) |
d9585277 | 509 | return PTR_ERR(res); |
c8ccbe03 MS |
510 | |
511 | if (res) | |
512 | entry = res; | |
513 | } | |
514 | ||
515 | if (!(flags & O_CREAT) || entry->d_inode) | |
516 | goto no_open; | |
517 | ||
518 | /* Only creates */ | |
47237687 | 519 | *opened |= FILE_CREATED; |
c8ccbe03 MS |
520 | |
521 | if (fc->no_create) | |
522 | goto mknod; | |
523 | ||
30d90494 | 524 | err = fuse_create_open(dir, entry, file, flags, mode, opened); |
d9585277 | 525 | if (err == -ENOSYS) { |
c8ccbe03 MS |
526 | fc->no_create = 1; |
527 | goto mknod; | |
528 | } | |
529 | out_dput: | |
530 | dput(res); | |
d9585277 | 531 | return err; |
c8ccbe03 MS |
532 | |
533 | mknod: | |
534 | err = fuse_mknod(dir, entry, mode, 0); | |
d9585277 | 535 | if (err) |
c8ccbe03 | 536 | goto out_dput; |
c8ccbe03 | 537 | no_open: |
e45198a6 | 538 | return finish_no_open(file, res); |
fd72faac MS |
539 | } |
540 | ||
6f9f1180 MS |
541 | /* |
542 | * Code shared between mknod, mkdir, symlink and link | |
543 | */ | |
9e6268db MS |
544 | static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req, |
545 | struct inode *dir, struct dentry *entry, | |
541af6a0 | 546 | umode_t mode) |
9e6268db MS |
547 | { |
548 | struct fuse_entry_out outarg; | |
549 | struct inode *inode; | |
9e6268db | 550 | int err; |
07e77dca | 551 | struct fuse_forget_link *forget; |
2d51013e | 552 | |
07e77dca MS |
553 | forget = fuse_alloc_forget(); |
554 | if (!forget) { | |
2d51013e | 555 | fuse_put_request(fc, req); |
07e77dca | 556 | return -ENOMEM; |
2d51013e | 557 | } |
9e6268db | 558 | |
0e9663ee | 559 | memset(&outarg, 0, sizeof(outarg)); |
9e6268db | 560 | req->in.h.nodeid = get_node_id(dir); |
9e6268db | 561 | req->out.numargs = 1; |
0e9663ee MS |
562 | if (fc->minor < 9) |
563 | req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE; | |
564 | else | |
565 | req->out.args[0].size = sizeof(outarg); | |
9e6268db | 566 | req->out.args[0].value = &outarg; |
b93f858a | 567 | fuse_request_send(fc, req); |
9e6268db | 568 | err = req->out.h.error; |
2d51013e MS |
569 | fuse_put_request(fc, req); |
570 | if (err) | |
571 | goto out_put_forget_req; | |
572 | ||
39ee059a MS |
573 | err = -EIO; |
574 | if (invalid_nodeid(outarg.nodeid)) | |
2d51013e | 575 | goto out_put_forget_req; |
39ee059a MS |
576 | |
577 | if ((outarg.attr.mode ^ mode) & S_IFMT) | |
2d51013e | 578 | goto out_put_forget_req; |
39ee059a | 579 | |
9e6268db | 580 | inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation, |
1fb69e78 | 581 | &outarg.attr, entry_attr_timeout(&outarg), 0); |
9e6268db | 582 | if (!inode) { |
07e77dca | 583 | fuse_queue_forget(fc, forget, outarg.nodeid, 1); |
9e6268db MS |
584 | return -ENOMEM; |
585 | } | |
07e77dca | 586 | kfree(forget); |
9e6268db | 587 | |
d2a85164 MS |
588 | if (S_ISDIR(inode->i_mode)) { |
589 | struct dentry *alias; | |
590 | mutex_lock(&fc->inst_mutex); | |
591 | alias = d_find_alias(inode); | |
592 | if (alias) { | |
593 | /* New directory must have moved since mkdir */ | |
594 | mutex_unlock(&fc->inst_mutex); | |
595 | dput(alias); | |
596 | iput(inode); | |
597 | return -EBUSY; | |
598 | } | |
599 | d_instantiate(entry, inode); | |
600 | mutex_unlock(&fc->inst_mutex); | |
601 | } else | |
602 | d_instantiate(entry, inode); | |
9e6268db | 603 | |
1fb69e78 | 604 | fuse_change_entry_timeout(entry, &outarg); |
9e6268db MS |
605 | fuse_invalidate_attr(dir); |
606 | return 0; | |
39ee059a | 607 | |
2d51013e | 608 | out_put_forget_req: |
07e77dca | 609 | kfree(forget); |
39ee059a | 610 | return err; |
9e6268db MS |
611 | } |
612 | ||
1a67aafb | 613 | static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode, |
9e6268db MS |
614 | dev_t rdev) |
615 | { | |
616 | struct fuse_mknod_in inarg; | |
617 | struct fuse_conn *fc = get_fuse_conn(dir); | |
b111c8c0 | 618 | struct fuse_req *req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
619 | if (IS_ERR(req)) |
620 | return PTR_ERR(req); | |
9e6268db | 621 | |
e0a43ddc MS |
622 | if (!fc->dont_mask) |
623 | mode &= ~current_umask(); | |
624 | ||
9e6268db MS |
625 | memset(&inarg, 0, sizeof(inarg)); |
626 | inarg.mode = mode; | |
627 | inarg.rdev = new_encode_dev(rdev); | |
e0a43ddc | 628 | inarg.umask = current_umask(); |
9e6268db MS |
629 | req->in.h.opcode = FUSE_MKNOD; |
630 | req->in.numargs = 2; | |
e0a43ddc MS |
631 | req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE : |
632 | sizeof(inarg); | |
9e6268db MS |
633 | req->in.args[0].value = &inarg; |
634 | req->in.args[1].size = entry->d_name.len + 1; | |
635 | req->in.args[1].value = entry->d_name.name; | |
636 | return create_new_entry(fc, req, dir, entry, mode); | |
637 | } | |
638 | ||
4acdaf27 | 639 | static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode, |
ebfc3b49 | 640 | bool excl) |
9e6268db MS |
641 | { |
642 | return fuse_mknod(dir, entry, mode, 0); | |
643 | } | |
644 | ||
18bb1db3 | 645 | static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode) |
9e6268db MS |
646 | { |
647 | struct fuse_mkdir_in inarg; | |
648 | struct fuse_conn *fc = get_fuse_conn(dir); | |
b111c8c0 | 649 | struct fuse_req *req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
650 | if (IS_ERR(req)) |
651 | return PTR_ERR(req); | |
9e6268db | 652 | |
e0a43ddc MS |
653 | if (!fc->dont_mask) |
654 | mode &= ~current_umask(); | |
655 | ||
9e6268db MS |
656 | memset(&inarg, 0, sizeof(inarg)); |
657 | inarg.mode = mode; | |
e0a43ddc | 658 | inarg.umask = current_umask(); |
9e6268db MS |
659 | req->in.h.opcode = FUSE_MKDIR; |
660 | req->in.numargs = 2; | |
661 | req->in.args[0].size = sizeof(inarg); | |
662 | req->in.args[0].value = &inarg; | |
663 | req->in.args[1].size = entry->d_name.len + 1; | |
664 | req->in.args[1].value = entry->d_name.name; | |
665 | return create_new_entry(fc, req, dir, entry, S_IFDIR); | |
666 | } | |
667 | ||
668 | static int fuse_symlink(struct inode *dir, struct dentry *entry, | |
669 | const char *link) | |
670 | { | |
671 | struct fuse_conn *fc = get_fuse_conn(dir); | |
672 | unsigned len = strlen(link) + 1; | |
b111c8c0 | 673 | struct fuse_req *req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
674 | if (IS_ERR(req)) |
675 | return PTR_ERR(req); | |
9e6268db MS |
676 | |
677 | req->in.h.opcode = FUSE_SYMLINK; | |
678 | req->in.numargs = 2; | |
679 | req->in.args[0].size = entry->d_name.len + 1; | |
680 | req->in.args[0].value = entry->d_name.name; | |
681 | req->in.args[1].size = len; | |
682 | req->in.args[1].value = link; | |
683 | return create_new_entry(fc, req, dir, entry, S_IFLNK); | |
684 | } | |
685 | ||
686 | static int fuse_unlink(struct inode *dir, struct dentry *entry) | |
687 | { | |
688 | int err; | |
689 | struct fuse_conn *fc = get_fuse_conn(dir); | |
b111c8c0 | 690 | struct fuse_req *req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
691 | if (IS_ERR(req)) |
692 | return PTR_ERR(req); | |
9e6268db MS |
693 | |
694 | req->in.h.opcode = FUSE_UNLINK; | |
695 | req->in.h.nodeid = get_node_id(dir); | |
9e6268db MS |
696 | req->in.numargs = 1; |
697 | req->in.args[0].size = entry->d_name.len + 1; | |
698 | req->in.args[0].value = entry->d_name.name; | |
b93f858a | 699 | fuse_request_send(fc, req); |
9e6268db MS |
700 | err = req->out.h.error; |
701 | fuse_put_request(fc, req); | |
702 | if (!err) { | |
703 | struct inode *inode = entry->d_inode; | |
ac45d613 | 704 | struct fuse_inode *fi = get_fuse_inode(inode); |
9e6268db | 705 | |
ac45d613 MS |
706 | spin_lock(&fc->lock); |
707 | fi->attr_version = ++fc->attr_version; | |
708 | drop_nlink(inode); | |
709 | spin_unlock(&fc->lock); | |
9e6268db MS |
710 | fuse_invalidate_attr(inode); |
711 | fuse_invalidate_attr(dir); | |
8cbdf1e6 | 712 | fuse_invalidate_entry_cache(entry); |
9e6268db MS |
713 | } else if (err == -EINTR) |
714 | fuse_invalidate_entry(entry); | |
715 | return err; | |
716 | } | |
717 | ||
718 | static int fuse_rmdir(struct inode *dir, struct dentry *entry) | |
719 | { | |
720 | int err; | |
721 | struct fuse_conn *fc = get_fuse_conn(dir); | |
b111c8c0 | 722 | struct fuse_req *req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
723 | if (IS_ERR(req)) |
724 | return PTR_ERR(req); | |
9e6268db MS |
725 | |
726 | req->in.h.opcode = FUSE_RMDIR; | |
727 | req->in.h.nodeid = get_node_id(dir); | |
9e6268db MS |
728 | req->in.numargs = 1; |
729 | req->in.args[0].size = entry->d_name.len + 1; | |
730 | req->in.args[0].value = entry->d_name.name; | |
b93f858a | 731 | fuse_request_send(fc, req); |
9e6268db MS |
732 | err = req->out.h.error; |
733 | fuse_put_request(fc, req); | |
734 | if (!err) { | |
ce71ec36 | 735 | clear_nlink(entry->d_inode); |
9e6268db | 736 | fuse_invalidate_attr(dir); |
8cbdf1e6 | 737 | fuse_invalidate_entry_cache(entry); |
9e6268db MS |
738 | } else if (err == -EINTR) |
739 | fuse_invalidate_entry(entry); | |
740 | return err; | |
741 | } | |
742 | ||
743 | static int fuse_rename(struct inode *olddir, struct dentry *oldent, | |
744 | struct inode *newdir, struct dentry *newent) | |
745 | { | |
746 | int err; | |
747 | struct fuse_rename_in inarg; | |
748 | struct fuse_conn *fc = get_fuse_conn(olddir); | |
b111c8c0 | 749 | struct fuse_req *req = fuse_get_req_nopages(fc); |
e4eaac06 | 750 | |
ce1d5a49 MS |
751 | if (IS_ERR(req)) |
752 | return PTR_ERR(req); | |
9e6268db MS |
753 | |
754 | memset(&inarg, 0, sizeof(inarg)); | |
755 | inarg.newdir = get_node_id(newdir); | |
756 | req->in.h.opcode = FUSE_RENAME; | |
757 | req->in.h.nodeid = get_node_id(olddir); | |
9e6268db MS |
758 | req->in.numargs = 3; |
759 | req->in.args[0].size = sizeof(inarg); | |
760 | req->in.args[0].value = &inarg; | |
761 | req->in.args[1].size = oldent->d_name.len + 1; | |
762 | req->in.args[1].value = oldent->d_name.name; | |
763 | req->in.args[2].size = newent->d_name.len + 1; | |
764 | req->in.args[2].value = newent->d_name.name; | |
b93f858a | 765 | fuse_request_send(fc, req); |
9e6268db MS |
766 | err = req->out.h.error; |
767 | fuse_put_request(fc, req); | |
768 | if (!err) { | |
08b63307 MS |
769 | /* ctime changes */ |
770 | fuse_invalidate_attr(oldent->d_inode); | |
771 | ||
9e6268db MS |
772 | fuse_invalidate_attr(olddir); |
773 | if (olddir != newdir) | |
774 | fuse_invalidate_attr(newdir); | |
8cbdf1e6 MS |
775 | |
776 | /* newent will end up negative */ | |
5219f346 MS |
777 | if (newent->d_inode) { |
778 | fuse_invalidate_attr(newent->d_inode); | |
8cbdf1e6 | 779 | fuse_invalidate_entry_cache(newent); |
5219f346 | 780 | } |
9e6268db MS |
781 | } else if (err == -EINTR) { |
782 | /* If request was interrupted, DEITY only knows if the | |
783 | rename actually took place. If the invalidation | |
784 | fails (e.g. some process has CWD under the renamed | |
785 | directory), then there can be inconsistency between | |
786 | the dcache and the real filesystem. Tough luck. */ | |
787 | fuse_invalidate_entry(oldent); | |
788 | if (newent->d_inode) | |
789 | fuse_invalidate_entry(newent); | |
790 | } | |
791 | ||
792 | return err; | |
793 | } | |
794 | ||
795 | static int fuse_link(struct dentry *entry, struct inode *newdir, | |
796 | struct dentry *newent) | |
797 | { | |
798 | int err; | |
799 | struct fuse_link_in inarg; | |
800 | struct inode *inode = entry->d_inode; | |
801 | struct fuse_conn *fc = get_fuse_conn(inode); | |
b111c8c0 | 802 | struct fuse_req *req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
803 | if (IS_ERR(req)) |
804 | return PTR_ERR(req); | |
9e6268db MS |
805 | |
806 | memset(&inarg, 0, sizeof(inarg)); | |
807 | inarg.oldnodeid = get_node_id(inode); | |
808 | req->in.h.opcode = FUSE_LINK; | |
9e6268db MS |
809 | req->in.numargs = 2; |
810 | req->in.args[0].size = sizeof(inarg); | |
811 | req->in.args[0].value = &inarg; | |
812 | req->in.args[1].size = newent->d_name.len + 1; | |
813 | req->in.args[1].value = newent->d_name.name; | |
814 | err = create_new_entry(fc, req, newdir, newent, inode->i_mode); | |
815 | /* Contrary to "normal" filesystems it can happen that link | |
816 | makes two "logical" inodes point to the same "physical" | |
817 | inode. We invalidate the attributes of the old one, so it | |
818 | will reflect changes in the backing inode (link count, | |
819 | etc.) | |
820 | */ | |
ac45d613 MS |
821 | if (!err) { |
822 | struct fuse_inode *fi = get_fuse_inode(inode); | |
823 | ||
824 | spin_lock(&fc->lock); | |
825 | fi->attr_version = ++fc->attr_version; | |
826 | inc_nlink(inode); | |
827 | spin_unlock(&fc->lock); | |
9e6268db | 828 | fuse_invalidate_attr(inode); |
ac45d613 MS |
829 | } else if (err == -EINTR) { |
830 | fuse_invalidate_attr(inode); | |
831 | } | |
9e6268db MS |
832 | return err; |
833 | } | |
834 | ||
1fb69e78 MS |
835 | static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr, |
836 | struct kstat *stat) | |
837 | { | |
203627bb MS |
838 | unsigned int blkbits; |
839 | ||
1fb69e78 MS |
840 | stat->dev = inode->i_sb->s_dev; |
841 | stat->ino = attr->ino; | |
842 | stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777); | |
843 | stat->nlink = attr->nlink; | |
499dcf20 EB |
844 | stat->uid = make_kuid(&init_user_ns, attr->uid); |
845 | stat->gid = make_kgid(&init_user_ns, attr->gid); | |
1fb69e78 MS |
846 | stat->rdev = inode->i_rdev; |
847 | stat->atime.tv_sec = attr->atime; | |
848 | stat->atime.tv_nsec = attr->atimensec; | |
849 | stat->mtime.tv_sec = attr->mtime; | |
850 | stat->mtime.tv_nsec = attr->mtimensec; | |
851 | stat->ctime.tv_sec = attr->ctime; | |
852 | stat->ctime.tv_nsec = attr->ctimensec; | |
853 | stat->size = attr->size; | |
854 | stat->blocks = attr->blocks; | |
203627bb MS |
855 | |
856 | if (attr->blksize != 0) | |
857 | blkbits = ilog2(attr->blksize); | |
858 | else | |
859 | blkbits = inode->i_sb->s_blocksize_bits; | |
860 | ||
861 | stat->blksize = 1 << blkbits; | |
1fb69e78 MS |
862 | } |
863 | ||
c79e322f MS |
864 | static int fuse_do_getattr(struct inode *inode, struct kstat *stat, |
865 | struct file *file) | |
e5e5558e MS |
866 | { |
867 | int err; | |
c79e322f MS |
868 | struct fuse_getattr_in inarg; |
869 | struct fuse_attr_out outarg; | |
e5e5558e | 870 | struct fuse_conn *fc = get_fuse_conn(inode); |
1fb69e78 MS |
871 | struct fuse_req *req; |
872 | u64 attr_version; | |
873 | ||
b111c8c0 | 874 | req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
875 | if (IS_ERR(req)) |
876 | return PTR_ERR(req); | |
e5e5558e | 877 | |
7dca9fd3 | 878 | attr_version = fuse_get_attr_version(fc); |
1fb69e78 | 879 | |
c79e322f | 880 | memset(&inarg, 0, sizeof(inarg)); |
0e9663ee | 881 | memset(&outarg, 0, sizeof(outarg)); |
c79e322f MS |
882 | /* Directories have separate file-handle space */ |
883 | if (file && S_ISREG(inode->i_mode)) { | |
884 | struct fuse_file *ff = file->private_data; | |
885 | ||
886 | inarg.getattr_flags |= FUSE_GETATTR_FH; | |
887 | inarg.fh = ff->fh; | |
888 | } | |
e5e5558e MS |
889 | req->in.h.opcode = FUSE_GETATTR; |
890 | req->in.h.nodeid = get_node_id(inode); | |
c79e322f MS |
891 | req->in.numargs = 1; |
892 | req->in.args[0].size = sizeof(inarg); | |
893 | req->in.args[0].value = &inarg; | |
e5e5558e | 894 | req->out.numargs = 1; |
0e9663ee MS |
895 | if (fc->minor < 9) |
896 | req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; | |
897 | else | |
898 | req->out.args[0].size = sizeof(outarg); | |
c79e322f | 899 | req->out.args[0].value = &outarg; |
b93f858a | 900 | fuse_request_send(fc, req); |
e5e5558e MS |
901 | err = req->out.h.error; |
902 | fuse_put_request(fc, req); | |
903 | if (!err) { | |
c79e322f | 904 | if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { |
e5e5558e MS |
905 | make_bad_inode(inode); |
906 | err = -EIO; | |
907 | } else { | |
c79e322f MS |
908 | fuse_change_attributes(inode, &outarg.attr, |
909 | attr_timeout(&outarg), | |
1fb69e78 MS |
910 | attr_version); |
911 | if (stat) | |
c79e322f | 912 | fuse_fillattr(inode, &outarg.attr, stat); |
e5e5558e MS |
913 | } |
914 | } | |
915 | return err; | |
916 | } | |
917 | ||
bcb4be80 MS |
918 | int fuse_update_attributes(struct inode *inode, struct kstat *stat, |
919 | struct file *file, bool *refreshed) | |
920 | { | |
921 | struct fuse_inode *fi = get_fuse_inode(inode); | |
922 | int err; | |
923 | bool r; | |
924 | ||
925 | if (fi->i_time < get_jiffies_64()) { | |
926 | r = true; | |
927 | err = fuse_do_getattr(inode, stat, file); | |
928 | } else { | |
929 | r = false; | |
930 | err = 0; | |
931 | if (stat) { | |
932 | generic_fillattr(inode, stat); | |
933 | stat->mode = fi->orig_i_mode; | |
45c72cd7 | 934 | stat->ino = fi->orig_ino; |
bcb4be80 MS |
935 | } |
936 | } | |
937 | ||
938 | if (refreshed != NULL) | |
939 | *refreshed = r; | |
940 | ||
941 | return err; | |
942 | } | |
943 | ||
3b463ae0 | 944 | int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid, |
451d0f59 | 945 | u64 child_nodeid, struct qstr *name) |
3b463ae0 JM |
946 | { |
947 | int err = -ENOTDIR; | |
948 | struct inode *parent; | |
949 | struct dentry *dir; | |
950 | struct dentry *entry; | |
951 | ||
952 | parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid); | |
953 | if (!parent) | |
954 | return -ENOENT; | |
955 | ||
956 | mutex_lock(&parent->i_mutex); | |
957 | if (!S_ISDIR(parent->i_mode)) | |
958 | goto unlock; | |
959 | ||
960 | err = -ENOENT; | |
961 | dir = d_find_alias(parent); | |
962 | if (!dir) | |
963 | goto unlock; | |
964 | ||
965 | entry = d_lookup(dir, name); | |
966 | dput(dir); | |
967 | if (!entry) | |
968 | goto unlock; | |
969 | ||
970 | fuse_invalidate_attr(parent); | |
971 | fuse_invalidate_entry(entry); | |
451d0f59 JM |
972 | |
973 | if (child_nodeid != 0 && entry->d_inode) { | |
974 | mutex_lock(&entry->d_inode->i_mutex); | |
975 | if (get_node_id(entry->d_inode) != child_nodeid) { | |
976 | err = -ENOENT; | |
977 | goto badentry; | |
978 | } | |
979 | if (d_mountpoint(entry)) { | |
980 | err = -EBUSY; | |
981 | goto badentry; | |
982 | } | |
983 | if (S_ISDIR(entry->d_inode->i_mode)) { | |
984 | shrink_dcache_parent(entry); | |
985 | if (!simple_empty(entry)) { | |
986 | err = -ENOTEMPTY; | |
987 | goto badentry; | |
988 | } | |
989 | entry->d_inode->i_flags |= S_DEAD; | |
990 | } | |
991 | dont_mount(entry); | |
992 | clear_nlink(entry->d_inode); | |
993 | err = 0; | |
994 | badentry: | |
995 | mutex_unlock(&entry->d_inode->i_mutex); | |
996 | if (!err) | |
997 | d_delete(entry); | |
998 | } else { | |
999 | err = 0; | |
1000 | } | |
3b463ae0 | 1001 | dput(entry); |
3b463ae0 JM |
1002 | |
1003 | unlock: | |
1004 | mutex_unlock(&parent->i_mutex); | |
1005 | iput(parent); | |
1006 | return err; | |
1007 | } | |
1008 | ||
87729a55 MS |
1009 | /* |
1010 | * Calling into a user-controlled filesystem gives the filesystem | |
c2132c1b | 1011 | * daemon ptrace-like capabilities over the current process. This |
87729a55 MS |
1012 | * means, that the filesystem daemon is able to record the exact |
1013 | * filesystem operations performed, and can also control the behavior | |
1014 | * of the requester process in otherwise impossible ways. For example | |
1015 | * it can delay the operation for arbitrary length of time allowing | |
1016 | * DoS against the requester. | |
1017 | * | |
1018 | * For this reason only those processes can call into the filesystem, | |
1019 | * for which the owner of the mount has ptrace privilege. This | |
1020 | * excludes processes started by other users, suid or sgid processes. | |
1021 | */ | |
c2132c1b | 1022 | int fuse_allow_current_process(struct fuse_conn *fc) |
87729a55 | 1023 | { |
c69e8d9c | 1024 | const struct cred *cred; |
87729a55 | 1025 | |
c69e8d9c | 1026 | if (fc->flags & FUSE_ALLOW_OTHER) |
87729a55 MS |
1027 | return 1; |
1028 | ||
c2132c1b | 1029 | cred = current_cred(); |
499dcf20 EB |
1030 | if (uid_eq(cred->euid, fc->user_id) && |
1031 | uid_eq(cred->suid, fc->user_id) && | |
1032 | uid_eq(cred->uid, fc->user_id) && | |
1033 | gid_eq(cred->egid, fc->group_id) && | |
1034 | gid_eq(cred->sgid, fc->group_id) && | |
1035 | gid_eq(cred->gid, fc->group_id)) | |
c2132c1b | 1036 | return 1; |
c69e8d9c | 1037 | |
c2132c1b | 1038 | return 0; |
87729a55 MS |
1039 | } |
1040 | ||
31d40d74 MS |
1041 | static int fuse_access(struct inode *inode, int mask) |
1042 | { | |
1043 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1044 | struct fuse_req *req; | |
1045 | struct fuse_access_in inarg; | |
1046 | int err; | |
1047 | ||
1048 | if (fc->no_access) | |
1049 | return 0; | |
1050 | ||
b111c8c0 | 1051 | req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
1052 | if (IS_ERR(req)) |
1053 | return PTR_ERR(req); | |
31d40d74 MS |
1054 | |
1055 | memset(&inarg, 0, sizeof(inarg)); | |
e6305c43 | 1056 | inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC); |
31d40d74 MS |
1057 | req->in.h.opcode = FUSE_ACCESS; |
1058 | req->in.h.nodeid = get_node_id(inode); | |
31d40d74 MS |
1059 | req->in.numargs = 1; |
1060 | req->in.args[0].size = sizeof(inarg); | |
1061 | req->in.args[0].value = &inarg; | |
b93f858a | 1062 | fuse_request_send(fc, req); |
31d40d74 MS |
1063 | err = req->out.h.error; |
1064 | fuse_put_request(fc, req); | |
1065 | if (err == -ENOSYS) { | |
1066 | fc->no_access = 1; | |
1067 | err = 0; | |
1068 | } | |
1069 | return err; | |
1070 | } | |
1071 | ||
10556cb2 | 1072 | static int fuse_perm_getattr(struct inode *inode, int mask) |
19690ddb | 1073 | { |
10556cb2 | 1074 | if (mask & MAY_NOT_BLOCK) |
19690ddb MS |
1075 | return -ECHILD; |
1076 | ||
1077 | return fuse_do_getattr(inode, NULL, NULL); | |
1078 | } | |
1079 | ||
6f9f1180 MS |
1080 | /* |
1081 | * Check permission. The two basic access models of FUSE are: | |
1082 | * | |
1083 | * 1) Local access checking ('default_permissions' mount option) based | |
1084 | * on file mode. This is the plain old disk filesystem permission | |
1085 | * modell. | |
1086 | * | |
1087 | * 2) "Remote" access checking, where server is responsible for | |
1088 | * checking permission in each inode operation. An exception to this | |
1089 | * is if ->permission() was invoked from sys_access() in which case an | |
1090 | * access request is sent. Execute permission is still checked | |
1091 | * locally based on file mode. | |
1092 | */ | |
10556cb2 | 1093 | static int fuse_permission(struct inode *inode, int mask) |
e5e5558e MS |
1094 | { |
1095 | struct fuse_conn *fc = get_fuse_conn(inode); | |
244f6385 MS |
1096 | bool refreshed = false; |
1097 | int err = 0; | |
e5e5558e | 1098 | |
c2132c1b | 1099 | if (!fuse_allow_current_process(fc)) |
e5e5558e | 1100 | return -EACCES; |
244f6385 MS |
1101 | |
1102 | /* | |
e8e96157 | 1103 | * If attributes are needed, refresh them before proceeding |
244f6385 | 1104 | */ |
e8e96157 MS |
1105 | if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) || |
1106 | ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) { | |
19690ddb MS |
1107 | struct fuse_inode *fi = get_fuse_inode(inode); |
1108 | ||
1109 | if (fi->i_time < get_jiffies_64()) { | |
1110 | refreshed = true; | |
1111 | ||
10556cb2 | 1112 | err = fuse_perm_getattr(inode, mask); |
19690ddb MS |
1113 | if (err) |
1114 | return err; | |
1115 | } | |
244f6385 MS |
1116 | } |
1117 | ||
1118 | if (fc->flags & FUSE_DEFAULT_PERMISSIONS) { | |
2830ba7f | 1119 | err = generic_permission(inode, mask); |
1e9a4ed9 MS |
1120 | |
1121 | /* If permission is denied, try to refresh file | |
1122 | attributes. This is also needed, because the root | |
1123 | node will at first have no permissions */ | |
244f6385 | 1124 | if (err == -EACCES && !refreshed) { |
10556cb2 | 1125 | err = fuse_perm_getattr(inode, mask); |
1e9a4ed9 | 1126 | if (!err) |
2830ba7f | 1127 | err = generic_permission(inode, mask); |
1e9a4ed9 MS |
1128 | } |
1129 | ||
6f9f1180 MS |
1130 | /* Note: the opposite of the above test does not |
1131 | exist. So if permissions are revoked this won't be | |
1132 | noticed immediately, only after the attribute | |
1133 | timeout has expired */ | |
9cfcac81 | 1134 | } else if (mask & (MAY_ACCESS | MAY_CHDIR)) { |
10556cb2 | 1135 | if (mask & MAY_NOT_BLOCK) |
19690ddb MS |
1136 | return -ECHILD; |
1137 | ||
e8e96157 MS |
1138 | err = fuse_access(inode, mask); |
1139 | } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) { | |
1140 | if (!(inode->i_mode & S_IXUGO)) { | |
1141 | if (refreshed) | |
1142 | return -EACCES; | |
1143 | ||
10556cb2 | 1144 | err = fuse_perm_getattr(inode, mask); |
e8e96157 MS |
1145 | if (!err && !(inode->i_mode & S_IXUGO)) |
1146 | return -EACCES; | |
1147 | } | |
e5e5558e | 1148 | } |
244f6385 | 1149 | return err; |
e5e5558e MS |
1150 | } |
1151 | ||
1152 | static int parse_dirfile(char *buf, size_t nbytes, struct file *file, | |
1153 | void *dstbuf, filldir_t filldir) | |
1154 | { | |
1155 | while (nbytes >= FUSE_NAME_OFFSET) { | |
1156 | struct fuse_dirent *dirent = (struct fuse_dirent *) buf; | |
1157 | size_t reclen = FUSE_DIRENT_SIZE(dirent); | |
1158 | int over; | |
1159 | if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX) | |
1160 | return -EIO; | |
1161 | if (reclen > nbytes) | |
1162 | break; | |
1163 | ||
1164 | over = filldir(dstbuf, dirent->name, dirent->namelen, | |
1165 | file->f_pos, dirent->ino, dirent->type); | |
1166 | if (over) | |
1167 | break; | |
1168 | ||
1169 | buf += reclen; | |
1170 | nbytes -= reclen; | |
1171 | file->f_pos = dirent->off; | |
1172 | } | |
1173 | ||
1174 | return 0; | |
1175 | } | |
1176 | ||
0b05b183 AA |
1177 | static int fuse_direntplus_link(struct file *file, |
1178 | struct fuse_direntplus *direntplus, | |
1179 | u64 attr_version) | |
1180 | { | |
1181 | int err; | |
1182 | struct fuse_entry_out *o = &direntplus->entry_out; | |
1183 | struct fuse_dirent *dirent = &direntplus->dirent; | |
1184 | struct dentry *parent = file->f_path.dentry; | |
1185 | struct qstr name = QSTR_INIT(dirent->name, dirent->namelen); | |
1186 | struct dentry *dentry; | |
1187 | struct dentry *alias; | |
1188 | struct inode *dir = parent->d_inode; | |
1189 | struct fuse_conn *fc; | |
1190 | struct inode *inode; | |
1191 | ||
1192 | if (!o->nodeid) { | |
1193 | /* | |
1194 | * Unlike in the case of fuse_lookup, zero nodeid does not mean | |
1195 | * ENOENT. Instead, it only means the userspace filesystem did | |
1196 | * not want to return attributes/handle for this entry. | |
1197 | * | |
1198 | * So do nothing. | |
1199 | */ | |
1200 | return 0; | |
1201 | } | |
1202 | ||
1203 | if (name.name[0] == '.') { | |
1204 | /* | |
1205 | * We could potentially refresh the attributes of the directory | |
1206 | * and its parent? | |
1207 | */ | |
1208 | if (name.len == 1) | |
1209 | return 0; | |
1210 | if (name.name[1] == '.' && name.len == 2) | |
1211 | return 0; | |
1212 | } | |
1213 | fc = get_fuse_conn(dir); | |
1214 | ||
1215 | name.hash = full_name_hash(name.name, name.len); | |
1216 | dentry = d_lookup(parent, &name); | |
1217 | if (dentry && dentry->d_inode) { | |
1218 | inode = dentry->d_inode; | |
1219 | if (get_node_id(inode) == o->nodeid) { | |
1220 | struct fuse_inode *fi; | |
1221 | fi = get_fuse_inode(inode); | |
1222 | spin_lock(&fc->lock); | |
1223 | fi->nlookup++; | |
1224 | spin_unlock(&fc->lock); | |
1225 | ||
1226 | /* | |
1227 | * The other branch to 'found' comes via fuse_iget() | |
1228 | * which bumps nlookup inside | |
1229 | */ | |
1230 | goto found; | |
1231 | } | |
1232 | err = d_invalidate(dentry); | |
1233 | if (err) | |
1234 | goto out; | |
1235 | dput(dentry); | |
1236 | dentry = NULL; | |
1237 | } | |
1238 | ||
1239 | dentry = d_alloc(parent, &name); | |
1240 | err = -ENOMEM; | |
1241 | if (!dentry) | |
1242 | goto out; | |
1243 | ||
1244 | inode = fuse_iget(dir->i_sb, o->nodeid, o->generation, | |
1245 | &o->attr, entry_attr_timeout(o), attr_version); | |
1246 | if (!inode) | |
1247 | goto out; | |
1248 | ||
1249 | alias = d_materialise_unique(dentry, inode); | |
1250 | err = PTR_ERR(alias); | |
1251 | if (IS_ERR(alias)) | |
1252 | goto out; | |
1253 | if (alias) { | |
1254 | dput(dentry); | |
1255 | dentry = alias; | |
1256 | } | |
1257 | ||
1258 | found: | |
1259 | fuse_change_attributes(inode, &o->attr, entry_attr_timeout(o), | |
1260 | attr_version); | |
1261 | ||
1262 | fuse_change_entry_timeout(dentry, o); | |
1263 | ||
1264 | err = 0; | |
1265 | out: | |
1266 | if (dentry) | |
1267 | dput(dentry); | |
1268 | return err; | |
1269 | } | |
1270 | ||
1271 | static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file, | |
1272 | void *dstbuf, filldir_t filldir, u64 attr_version) | |
1273 | { | |
1274 | struct fuse_direntplus *direntplus; | |
1275 | struct fuse_dirent *dirent; | |
1276 | size_t reclen; | |
1277 | int over = 0; | |
1278 | int ret; | |
1279 | ||
1280 | while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) { | |
1281 | direntplus = (struct fuse_direntplus *) buf; | |
1282 | dirent = &direntplus->dirent; | |
1283 | reclen = FUSE_DIRENTPLUS_SIZE(direntplus); | |
1284 | ||
1285 | if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX) | |
1286 | return -EIO; | |
1287 | if (reclen > nbytes) | |
1288 | break; | |
1289 | ||
1290 | if (!over) { | |
1291 | /* We fill entries into dstbuf only as much as | |
1292 | it can hold. But we still continue iterating | |
1293 | over remaining entries to link them. If not, | |
1294 | we need to send a FORGET for each of those | |
1295 | which we did not link. | |
1296 | */ | |
1297 | over = filldir(dstbuf, dirent->name, dirent->namelen, | |
1298 | file->f_pos, dirent->ino, | |
1299 | dirent->type); | |
1300 | file->f_pos = dirent->off; | |
1301 | } | |
1302 | ||
1303 | buf += reclen; | |
1304 | nbytes -= reclen; | |
1305 | ||
1306 | ret = fuse_direntplus_link(file, direntplus, attr_version); | |
1307 | if (ret) | |
1308 | fuse_force_forget(file, direntplus->entry_out.nodeid); | |
1309 | } | |
1310 | ||
1311 | return 0; | |
1312 | } | |
1313 | ||
04730fef | 1314 | static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir) |
e5e5558e | 1315 | { |
4582a4ab | 1316 | int plus, err; |
04730fef MS |
1317 | size_t nbytes; |
1318 | struct page *page; | |
7706a9d6 | 1319 | struct inode *inode = file->f_path.dentry->d_inode; |
e5e5558e | 1320 | struct fuse_conn *fc = get_fuse_conn(inode); |
248d86e8 | 1321 | struct fuse_req *req; |
0b05b183 | 1322 | u64 attr_version = 0; |
248d86e8 MS |
1323 | |
1324 | if (is_bad_inode(inode)) | |
1325 | return -EIO; | |
1326 | ||
b111c8c0 | 1327 | req = fuse_get_req(fc, 1); |
ce1d5a49 MS |
1328 | if (IS_ERR(req)) |
1329 | return PTR_ERR(req); | |
e5e5558e | 1330 | |
04730fef MS |
1331 | page = alloc_page(GFP_KERNEL); |
1332 | if (!page) { | |
1333 | fuse_put_request(fc, req); | |
1334 | return -ENOMEM; | |
1335 | } | |
4582a4ab FS |
1336 | |
1337 | plus = fuse_use_readdirplus(inode, file); | |
f4975c67 | 1338 | req->out.argpages = 1; |
04730fef MS |
1339 | req->num_pages = 1; |
1340 | req->pages[0] = page; | |
85f40aec | 1341 | req->page_descs[0].length = PAGE_SIZE; |
4582a4ab | 1342 | if (plus) { |
0b05b183 AA |
1343 | attr_version = fuse_get_attr_version(fc); |
1344 | fuse_read_fill(req, file, file->f_pos, PAGE_SIZE, | |
1345 | FUSE_READDIRPLUS); | |
1346 | } else { | |
1347 | fuse_read_fill(req, file, file->f_pos, PAGE_SIZE, | |
1348 | FUSE_READDIR); | |
1349 | } | |
b93f858a | 1350 | fuse_request_send(fc, req); |
361b1eb5 | 1351 | nbytes = req->out.args[0].size; |
e5e5558e MS |
1352 | err = req->out.h.error; |
1353 | fuse_put_request(fc, req); | |
0b05b183 | 1354 | if (!err) { |
4582a4ab | 1355 | if (plus) { |
0b05b183 AA |
1356 | err = parse_dirplusfile(page_address(page), nbytes, |
1357 | file, dstbuf, filldir, | |
1358 | attr_version); | |
1359 | } else { | |
1360 | err = parse_dirfile(page_address(page), nbytes, file, | |
1361 | dstbuf, filldir); | |
1362 | } | |
1363 | } | |
e5e5558e | 1364 | |
04730fef | 1365 | __free_page(page); |
b36c31ba | 1366 | fuse_invalidate_attr(inode); /* atime changed */ |
04730fef | 1367 | return err; |
e5e5558e MS |
1368 | } |
1369 | ||
1370 | static char *read_link(struct dentry *dentry) | |
1371 | { | |
1372 | struct inode *inode = dentry->d_inode; | |
1373 | struct fuse_conn *fc = get_fuse_conn(inode); | |
b111c8c0 | 1374 | struct fuse_req *req = fuse_get_req_nopages(fc); |
e5e5558e MS |
1375 | char *link; |
1376 | ||
ce1d5a49 | 1377 | if (IS_ERR(req)) |
e231c2ee | 1378 | return ERR_CAST(req); |
e5e5558e MS |
1379 | |
1380 | link = (char *) __get_free_page(GFP_KERNEL); | |
1381 | if (!link) { | |
1382 | link = ERR_PTR(-ENOMEM); | |
1383 | goto out; | |
1384 | } | |
1385 | req->in.h.opcode = FUSE_READLINK; | |
1386 | req->in.h.nodeid = get_node_id(inode); | |
e5e5558e MS |
1387 | req->out.argvar = 1; |
1388 | req->out.numargs = 1; | |
1389 | req->out.args[0].size = PAGE_SIZE - 1; | |
1390 | req->out.args[0].value = link; | |
b93f858a | 1391 | fuse_request_send(fc, req); |
e5e5558e MS |
1392 | if (req->out.h.error) { |
1393 | free_page((unsigned long) link); | |
1394 | link = ERR_PTR(req->out.h.error); | |
1395 | } else | |
1396 | link[req->out.args[0].size] = '\0'; | |
1397 | out: | |
1398 | fuse_put_request(fc, req); | |
b36c31ba | 1399 | fuse_invalidate_attr(inode); /* atime changed */ |
e5e5558e MS |
1400 | return link; |
1401 | } | |
1402 | ||
1403 | static void free_link(char *link) | |
1404 | { | |
1405 | if (!IS_ERR(link)) | |
1406 | free_page((unsigned long) link); | |
1407 | } | |
1408 | ||
1409 | static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd) | |
1410 | { | |
1411 | nd_set_link(nd, read_link(dentry)); | |
1412 | return NULL; | |
1413 | } | |
1414 | ||
1415 | static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c) | |
1416 | { | |
1417 | free_link(nd_get_link(nd)); | |
1418 | } | |
1419 | ||
1420 | static int fuse_dir_open(struct inode *inode, struct file *file) | |
1421 | { | |
91fe96b4 | 1422 | return fuse_open_common(inode, file, true); |
e5e5558e MS |
1423 | } |
1424 | ||
1425 | static int fuse_dir_release(struct inode *inode, struct file *file) | |
1426 | { | |
8b0797a4 MS |
1427 | fuse_release_common(file, FUSE_RELEASEDIR); |
1428 | ||
1429 | return 0; | |
e5e5558e MS |
1430 | } |
1431 | ||
02c24a82 JB |
1432 | static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end, |
1433 | int datasync) | |
82547981 | 1434 | { |
02c24a82 | 1435 | return fuse_fsync_common(file, start, end, datasync, 1); |
82547981 MS |
1436 | } |
1437 | ||
b18da0c5 MS |
1438 | static long fuse_dir_ioctl(struct file *file, unsigned int cmd, |
1439 | unsigned long arg) | |
1440 | { | |
1441 | struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host); | |
1442 | ||
1443 | /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */ | |
1444 | if (fc->minor < 18) | |
1445 | return -ENOTTY; | |
1446 | ||
1447 | return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR); | |
1448 | } | |
1449 | ||
1450 | static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd, | |
1451 | unsigned long arg) | |
1452 | { | |
1453 | struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host); | |
1454 | ||
1455 | if (fc->minor < 18) | |
1456 | return -ENOTTY; | |
1457 | ||
1458 | return fuse_ioctl_common(file, cmd, arg, | |
1459 | FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR); | |
1460 | } | |
1461 | ||
17637cba MS |
1462 | static bool update_mtime(unsigned ivalid) |
1463 | { | |
1464 | /* Always update if mtime is explicitly set */ | |
1465 | if (ivalid & ATTR_MTIME_SET) | |
1466 | return true; | |
1467 | ||
1468 | /* If it's an open(O_TRUNC) or an ftruncate(), don't update */ | |
1469 | if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE))) | |
1470 | return false; | |
1471 | ||
1472 | /* In all other cases update */ | |
1473 | return true; | |
1474 | } | |
1475 | ||
befc649c | 1476 | static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg) |
9e6268db MS |
1477 | { |
1478 | unsigned ivalid = iattr->ia_valid; | |
9e6268db MS |
1479 | |
1480 | if (ivalid & ATTR_MODE) | |
befc649c | 1481 | arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode; |
9e6268db | 1482 | if (ivalid & ATTR_UID) |
499dcf20 | 1483 | arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid); |
9e6268db | 1484 | if (ivalid & ATTR_GID) |
499dcf20 | 1485 | arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid); |
9e6268db | 1486 | if (ivalid & ATTR_SIZE) |
befc649c | 1487 | arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size; |
17637cba MS |
1488 | if (ivalid & ATTR_ATIME) { |
1489 | arg->valid |= FATTR_ATIME; | |
befc649c | 1490 | arg->atime = iattr->ia_atime.tv_sec; |
17637cba MS |
1491 | arg->atimensec = iattr->ia_atime.tv_nsec; |
1492 | if (!(ivalid & ATTR_ATIME_SET)) | |
1493 | arg->valid |= FATTR_ATIME_NOW; | |
1494 | } | |
1495 | if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) { | |
1496 | arg->valid |= FATTR_MTIME; | |
befc649c | 1497 | arg->mtime = iattr->ia_mtime.tv_sec; |
17637cba MS |
1498 | arg->mtimensec = iattr->ia_mtime.tv_nsec; |
1499 | if (!(ivalid & ATTR_MTIME_SET)) | |
1500 | arg->valid |= FATTR_MTIME_NOW; | |
befc649c | 1501 | } |
9e6268db MS |
1502 | } |
1503 | ||
3be5a52b MS |
1504 | /* |
1505 | * Prevent concurrent writepages on inode | |
1506 | * | |
1507 | * This is done by adding a negative bias to the inode write counter | |
1508 | * and waiting for all pending writes to finish. | |
1509 | */ | |
1510 | void fuse_set_nowrite(struct inode *inode) | |
1511 | { | |
1512 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1513 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1514 | ||
1515 | BUG_ON(!mutex_is_locked(&inode->i_mutex)); | |
1516 | ||
1517 | spin_lock(&fc->lock); | |
1518 | BUG_ON(fi->writectr < 0); | |
1519 | fi->writectr += FUSE_NOWRITE; | |
1520 | spin_unlock(&fc->lock); | |
1521 | wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE); | |
1522 | } | |
1523 | ||
1524 | /* | |
1525 | * Allow writepages on inode | |
1526 | * | |
1527 | * Remove the bias from the writecounter and send any queued | |
1528 | * writepages. | |
1529 | */ | |
1530 | static void __fuse_release_nowrite(struct inode *inode) | |
1531 | { | |
1532 | struct fuse_inode *fi = get_fuse_inode(inode); | |
1533 | ||
1534 | BUG_ON(fi->writectr != FUSE_NOWRITE); | |
1535 | fi->writectr = 0; | |
1536 | fuse_flush_writepages(inode); | |
1537 | } | |
1538 | ||
1539 | void fuse_release_nowrite(struct inode *inode) | |
1540 | { | |
1541 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1542 | ||
1543 | spin_lock(&fc->lock); | |
1544 | __fuse_release_nowrite(inode); | |
1545 | spin_unlock(&fc->lock); | |
1546 | } | |
1547 | ||
6f9f1180 MS |
1548 | /* |
1549 | * Set attributes, and at the same time refresh them. | |
1550 | * | |
1551 | * Truncation is slightly complicated, because the 'truncate' request | |
1552 | * may fail, in which case we don't want to touch the mapping. | |
9ffbb916 MS |
1553 | * vmtruncate() doesn't allow for this case, so do the rlimit checking |
1554 | * and the actual truncation by hand. | |
6f9f1180 | 1555 | */ |
49d4914f MS |
1556 | static int fuse_do_setattr(struct dentry *entry, struct iattr *attr, |
1557 | struct file *file) | |
9e6268db MS |
1558 | { |
1559 | struct inode *inode = entry->d_inode; | |
1560 | struct fuse_conn *fc = get_fuse_conn(inode); | |
9e6268db MS |
1561 | struct fuse_req *req; |
1562 | struct fuse_setattr_in inarg; | |
1563 | struct fuse_attr_out outarg; | |
3be5a52b MS |
1564 | bool is_truncate = false; |
1565 | loff_t oldsize; | |
9e6268db | 1566 | int err; |
9e6268db | 1567 | |
c2132c1b | 1568 | if (!fuse_allow_current_process(fc)) |
e57ac683 MS |
1569 | return -EACCES; |
1570 | ||
db78b877 CH |
1571 | if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS)) |
1572 | attr->ia_valid |= ATTR_FORCE; | |
1573 | ||
1574 | err = inode_change_ok(inode, attr); | |
1575 | if (err) | |
1576 | return err; | |
1e9a4ed9 | 1577 | |
8d56addd MS |
1578 | if (attr->ia_valid & ATTR_OPEN) { |
1579 | if (fc->atomic_o_trunc) | |
1580 | return 0; | |
1581 | file = NULL; | |
1582 | } | |
6ff958ed | 1583 | |
2c27c65e | 1584 | if (attr->ia_valid & ATTR_SIZE) |
3be5a52b | 1585 | is_truncate = true; |
9e6268db | 1586 | |
b111c8c0 | 1587 | req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
1588 | if (IS_ERR(req)) |
1589 | return PTR_ERR(req); | |
9e6268db | 1590 | |
3be5a52b MS |
1591 | if (is_truncate) |
1592 | fuse_set_nowrite(inode); | |
1593 | ||
9e6268db | 1594 | memset(&inarg, 0, sizeof(inarg)); |
0e9663ee | 1595 | memset(&outarg, 0, sizeof(outarg)); |
befc649c | 1596 | iattr_to_fattr(attr, &inarg); |
49d4914f MS |
1597 | if (file) { |
1598 | struct fuse_file *ff = file->private_data; | |
1599 | inarg.valid |= FATTR_FH; | |
1600 | inarg.fh = ff->fh; | |
1601 | } | |
f3332114 MS |
1602 | if (attr->ia_valid & ATTR_SIZE) { |
1603 | /* For mandatory locking in truncate */ | |
1604 | inarg.valid |= FATTR_LOCKOWNER; | |
1605 | inarg.lock_owner = fuse_lock_owner_id(fc, current->files); | |
1606 | } | |
9e6268db MS |
1607 | req->in.h.opcode = FUSE_SETATTR; |
1608 | req->in.h.nodeid = get_node_id(inode); | |
9e6268db MS |
1609 | req->in.numargs = 1; |
1610 | req->in.args[0].size = sizeof(inarg); | |
1611 | req->in.args[0].value = &inarg; | |
1612 | req->out.numargs = 1; | |
0e9663ee MS |
1613 | if (fc->minor < 9) |
1614 | req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE; | |
1615 | else | |
1616 | req->out.args[0].size = sizeof(outarg); | |
9e6268db | 1617 | req->out.args[0].value = &outarg; |
b93f858a | 1618 | fuse_request_send(fc, req); |
9e6268db MS |
1619 | err = req->out.h.error; |
1620 | fuse_put_request(fc, req); | |
e00d2c2d MS |
1621 | if (err) { |
1622 | if (err == -EINTR) | |
1623 | fuse_invalidate_attr(inode); | |
3be5a52b | 1624 | goto error; |
e00d2c2d | 1625 | } |
9e6268db | 1626 | |
e00d2c2d MS |
1627 | if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) { |
1628 | make_bad_inode(inode); | |
3be5a52b MS |
1629 | err = -EIO; |
1630 | goto error; | |
1631 | } | |
1632 | ||
1633 | spin_lock(&fc->lock); | |
1634 | fuse_change_attributes_common(inode, &outarg.attr, | |
1635 | attr_timeout(&outarg)); | |
1636 | oldsize = inode->i_size; | |
1637 | i_size_write(inode, outarg.attr.size); | |
1638 | ||
1639 | if (is_truncate) { | |
1640 | /* NOTE: this may release/reacquire fc->lock */ | |
1641 | __fuse_release_nowrite(inode); | |
1642 | } | |
1643 | spin_unlock(&fc->lock); | |
1644 | ||
1645 | /* | |
1646 | * Only call invalidate_inode_pages2() after removing | |
1647 | * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock. | |
1648 | */ | |
1649 | if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) { | |
c08d3b0e | 1650 | truncate_pagecache(inode, oldsize, outarg.attr.size); |
3be5a52b | 1651 | invalidate_inode_pages2(inode->i_mapping); |
e00d2c2d MS |
1652 | } |
1653 | ||
e00d2c2d | 1654 | return 0; |
3be5a52b MS |
1655 | |
1656 | error: | |
1657 | if (is_truncate) | |
1658 | fuse_release_nowrite(inode); | |
1659 | ||
1660 | return err; | |
9e6268db MS |
1661 | } |
1662 | ||
49d4914f MS |
1663 | static int fuse_setattr(struct dentry *entry, struct iattr *attr) |
1664 | { | |
1665 | if (attr->ia_valid & ATTR_FILE) | |
1666 | return fuse_do_setattr(entry, attr, attr->ia_file); | |
1667 | else | |
1668 | return fuse_do_setattr(entry, attr, NULL); | |
1669 | } | |
1670 | ||
e5e5558e MS |
1671 | static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry, |
1672 | struct kstat *stat) | |
1673 | { | |
1674 | struct inode *inode = entry->d_inode; | |
244f6385 | 1675 | struct fuse_conn *fc = get_fuse_conn(inode); |
244f6385 | 1676 | |
c2132c1b | 1677 | if (!fuse_allow_current_process(fc)) |
244f6385 MS |
1678 | return -EACCES; |
1679 | ||
bcb4be80 | 1680 | return fuse_update_attributes(inode, stat, NULL, NULL); |
e5e5558e MS |
1681 | } |
1682 | ||
92a8780e MS |
1683 | static int fuse_setxattr(struct dentry *entry, const char *name, |
1684 | const void *value, size_t size, int flags) | |
1685 | { | |
1686 | struct inode *inode = entry->d_inode; | |
1687 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1688 | struct fuse_req *req; | |
1689 | struct fuse_setxattr_in inarg; | |
1690 | int err; | |
1691 | ||
92a8780e MS |
1692 | if (fc->no_setxattr) |
1693 | return -EOPNOTSUPP; | |
1694 | ||
b111c8c0 | 1695 | req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
1696 | if (IS_ERR(req)) |
1697 | return PTR_ERR(req); | |
92a8780e MS |
1698 | |
1699 | memset(&inarg, 0, sizeof(inarg)); | |
1700 | inarg.size = size; | |
1701 | inarg.flags = flags; | |
1702 | req->in.h.opcode = FUSE_SETXATTR; | |
1703 | req->in.h.nodeid = get_node_id(inode); | |
92a8780e MS |
1704 | req->in.numargs = 3; |
1705 | req->in.args[0].size = sizeof(inarg); | |
1706 | req->in.args[0].value = &inarg; | |
1707 | req->in.args[1].size = strlen(name) + 1; | |
1708 | req->in.args[1].value = name; | |
1709 | req->in.args[2].size = size; | |
1710 | req->in.args[2].value = value; | |
b93f858a | 1711 | fuse_request_send(fc, req); |
92a8780e MS |
1712 | err = req->out.h.error; |
1713 | fuse_put_request(fc, req); | |
1714 | if (err == -ENOSYS) { | |
1715 | fc->no_setxattr = 1; | |
1716 | err = -EOPNOTSUPP; | |
1717 | } | |
1718 | return err; | |
1719 | } | |
1720 | ||
1721 | static ssize_t fuse_getxattr(struct dentry *entry, const char *name, | |
1722 | void *value, size_t size) | |
1723 | { | |
1724 | struct inode *inode = entry->d_inode; | |
1725 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1726 | struct fuse_req *req; | |
1727 | struct fuse_getxattr_in inarg; | |
1728 | struct fuse_getxattr_out outarg; | |
1729 | ssize_t ret; | |
1730 | ||
1731 | if (fc->no_getxattr) | |
1732 | return -EOPNOTSUPP; | |
1733 | ||
b111c8c0 | 1734 | req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
1735 | if (IS_ERR(req)) |
1736 | return PTR_ERR(req); | |
92a8780e MS |
1737 | |
1738 | memset(&inarg, 0, sizeof(inarg)); | |
1739 | inarg.size = size; | |
1740 | req->in.h.opcode = FUSE_GETXATTR; | |
1741 | req->in.h.nodeid = get_node_id(inode); | |
92a8780e MS |
1742 | req->in.numargs = 2; |
1743 | req->in.args[0].size = sizeof(inarg); | |
1744 | req->in.args[0].value = &inarg; | |
1745 | req->in.args[1].size = strlen(name) + 1; | |
1746 | req->in.args[1].value = name; | |
1747 | /* This is really two different operations rolled into one */ | |
1748 | req->out.numargs = 1; | |
1749 | if (size) { | |
1750 | req->out.argvar = 1; | |
1751 | req->out.args[0].size = size; | |
1752 | req->out.args[0].value = value; | |
1753 | } else { | |
1754 | req->out.args[0].size = sizeof(outarg); | |
1755 | req->out.args[0].value = &outarg; | |
1756 | } | |
b93f858a | 1757 | fuse_request_send(fc, req); |
92a8780e MS |
1758 | ret = req->out.h.error; |
1759 | if (!ret) | |
1760 | ret = size ? req->out.args[0].size : outarg.size; | |
1761 | else { | |
1762 | if (ret == -ENOSYS) { | |
1763 | fc->no_getxattr = 1; | |
1764 | ret = -EOPNOTSUPP; | |
1765 | } | |
1766 | } | |
1767 | fuse_put_request(fc, req); | |
1768 | return ret; | |
1769 | } | |
1770 | ||
1771 | static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size) | |
1772 | { | |
1773 | struct inode *inode = entry->d_inode; | |
1774 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1775 | struct fuse_req *req; | |
1776 | struct fuse_getxattr_in inarg; | |
1777 | struct fuse_getxattr_out outarg; | |
1778 | ssize_t ret; | |
1779 | ||
c2132c1b | 1780 | if (!fuse_allow_current_process(fc)) |
e57ac683 MS |
1781 | return -EACCES; |
1782 | ||
92a8780e MS |
1783 | if (fc->no_listxattr) |
1784 | return -EOPNOTSUPP; | |
1785 | ||
b111c8c0 | 1786 | req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
1787 | if (IS_ERR(req)) |
1788 | return PTR_ERR(req); | |
92a8780e MS |
1789 | |
1790 | memset(&inarg, 0, sizeof(inarg)); | |
1791 | inarg.size = size; | |
1792 | req->in.h.opcode = FUSE_LISTXATTR; | |
1793 | req->in.h.nodeid = get_node_id(inode); | |
92a8780e MS |
1794 | req->in.numargs = 1; |
1795 | req->in.args[0].size = sizeof(inarg); | |
1796 | req->in.args[0].value = &inarg; | |
1797 | /* This is really two different operations rolled into one */ | |
1798 | req->out.numargs = 1; | |
1799 | if (size) { | |
1800 | req->out.argvar = 1; | |
1801 | req->out.args[0].size = size; | |
1802 | req->out.args[0].value = list; | |
1803 | } else { | |
1804 | req->out.args[0].size = sizeof(outarg); | |
1805 | req->out.args[0].value = &outarg; | |
1806 | } | |
b93f858a | 1807 | fuse_request_send(fc, req); |
92a8780e MS |
1808 | ret = req->out.h.error; |
1809 | if (!ret) | |
1810 | ret = size ? req->out.args[0].size : outarg.size; | |
1811 | else { | |
1812 | if (ret == -ENOSYS) { | |
1813 | fc->no_listxattr = 1; | |
1814 | ret = -EOPNOTSUPP; | |
1815 | } | |
1816 | } | |
1817 | fuse_put_request(fc, req); | |
1818 | return ret; | |
1819 | } | |
1820 | ||
1821 | static int fuse_removexattr(struct dentry *entry, const char *name) | |
1822 | { | |
1823 | struct inode *inode = entry->d_inode; | |
1824 | struct fuse_conn *fc = get_fuse_conn(inode); | |
1825 | struct fuse_req *req; | |
1826 | int err; | |
1827 | ||
1828 | if (fc->no_removexattr) | |
1829 | return -EOPNOTSUPP; | |
1830 | ||
b111c8c0 | 1831 | req = fuse_get_req_nopages(fc); |
ce1d5a49 MS |
1832 | if (IS_ERR(req)) |
1833 | return PTR_ERR(req); | |
92a8780e MS |
1834 | |
1835 | req->in.h.opcode = FUSE_REMOVEXATTR; | |
1836 | req->in.h.nodeid = get_node_id(inode); | |
92a8780e MS |
1837 | req->in.numargs = 1; |
1838 | req->in.args[0].size = strlen(name) + 1; | |
1839 | req->in.args[0].value = name; | |
b93f858a | 1840 | fuse_request_send(fc, req); |
92a8780e MS |
1841 | err = req->out.h.error; |
1842 | fuse_put_request(fc, req); | |
1843 | if (err == -ENOSYS) { | |
1844 | fc->no_removexattr = 1; | |
1845 | err = -EOPNOTSUPP; | |
1846 | } | |
1847 | return err; | |
1848 | } | |
1849 | ||
754661f1 | 1850 | static const struct inode_operations fuse_dir_inode_operations = { |
e5e5558e | 1851 | .lookup = fuse_lookup, |
9e6268db MS |
1852 | .mkdir = fuse_mkdir, |
1853 | .symlink = fuse_symlink, | |
1854 | .unlink = fuse_unlink, | |
1855 | .rmdir = fuse_rmdir, | |
1856 | .rename = fuse_rename, | |
1857 | .link = fuse_link, | |
1858 | .setattr = fuse_setattr, | |
1859 | .create = fuse_create, | |
c8ccbe03 | 1860 | .atomic_open = fuse_atomic_open, |
9e6268db | 1861 | .mknod = fuse_mknod, |
e5e5558e MS |
1862 | .permission = fuse_permission, |
1863 | .getattr = fuse_getattr, | |
92a8780e MS |
1864 | .setxattr = fuse_setxattr, |
1865 | .getxattr = fuse_getxattr, | |
1866 | .listxattr = fuse_listxattr, | |
1867 | .removexattr = fuse_removexattr, | |
e5e5558e MS |
1868 | }; |
1869 | ||
4b6f5d20 | 1870 | static const struct file_operations fuse_dir_operations = { |
b6aeaded | 1871 | .llseek = generic_file_llseek, |
e5e5558e MS |
1872 | .read = generic_read_dir, |
1873 | .readdir = fuse_readdir, | |
1874 | .open = fuse_dir_open, | |
1875 | .release = fuse_dir_release, | |
82547981 | 1876 | .fsync = fuse_dir_fsync, |
b18da0c5 MS |
1877 | .unlocked_ioctl = fuse_dir_ioctl, |
1878 | .compat_ioctl = fuse_dir_compat_ioctl, | |
e5e5558e MS |
1879 | }; |
1880 | ||
754661f1 | 1881 | static const struct inode_operations fuse_common_inode_operations = { |
9e6268db | 1882 | .setattr = fuse_setattr, |
e5e5558e MS |
1883 | .permission = fuse_permission, |
1884 | .getattr = fuse_getattr, | |
92a8780e MS |
1885 | .setxattr = fuse_setxattr, |
1886 | .getxattr = fuse_getxattr, | |
1887 | .listxattr = fuse_listxattr, | |
1888 | .removexattr = fuse_removexattr, | |
e5e5558e MS |
1889 | }; |
1890 | ||
754661f1 | 1891 | static const struct inode_operations fuse_symlink_inode_operations = { |
9e6268db | 1892 | .setattr = fuse_setattr, |
e5e5558e MS |
1893 | .follow_link = fuse_follow_link, |
1894 | .put_link = fuse_put_link, | |
1895 | .readlink = generic_readlink, | |
1896 | .getattr = fuse_getattr, | |
92a8780e MS |
1897 | .setxattr = fuse_setxattr, |
1898 | .getxattr = fuse_getxattr, | |
1899 | .listxattr = fuse_listxattr, | |
1900 | .removexattr = fuse_removexattr, | |
e5e5558e MS |
1901 | }; |
1902 | ||
1903 | void fuse_init_common(struct inode *inode) | |
1904 | { | |
1905 | inode->i_op = &fuse_common_inode_operations; | |
1906 | } | |
1907 | ||
1908 | void fuse_init_dir(struct inode *inode) | |
1909 | { | |
1910 | inode->i_op = &fuse_dir_inode_operations; | |
1911 | inode->i_fop = &fuse_dir_operations; | |
1912 | } | |
1913 | ||
1914 | void fuse_init_symlink(struct inode *inode) | |
1915 | { | |
1916 | inode->i_op = &fuse_symlink_inode_operations; | |
1917 | } |