blob: 29fef75f23605398e2aad21574d7d15bc084a563 [file] [log] [blame]
Miklos Szeredie5e55582005-09-09 13:10:28 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi51eb01e2006-06-25 05:48:50 -07003 Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredie5e55582005-09-09 13:10:28 -07004
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
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070017#if BITS_PER_LONG >= 64
18static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
19{
20 entry->d_time = time;
21}
22
23static 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 */
31static 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
37static 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
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080044/*
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 */
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070053static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
Miklos Szeredie5e55582005-09-09 13:10:28 -070054{
Miklos Szeredi685d16d2006-07-30 03:04:08 -070055 if (sec || nsec) {
56 struct timespec ts = {sec, nsec};
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070057 return get_jiffies_64() + timespec_to_jiffies(&ts);
Miklos Szeredi685d16d2006-07-30 03:04:08 -070058 } else
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070059 return 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -070060}
61
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080062/*
63 * Set dentry and possibly attribute timeouts from the lookup/mk*
64 * replies
65 */
Miklos Szeredi0aa7c692006-01-06 00:19:34 -080066static void fuse_change_timeout(struct dentry *entry, struct fuse_entry_out *o)
67{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070068 fuse_dentry_settime(entry,
69 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -080070 if (entry->d_inode)
71 get_fuse_inode(entry->d_inode)->i_time =
72 time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
73}
74
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080075/*
76 * Mark the attributes as stale, so that at the next call to
77 * ->getattr() they will be fetched from userspace
78 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -080079void fuse_invalidate_attr(struct inode *inode)
80{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070081 get_fuse_inode(inode)->i_time = 0;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -080082}
83
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080084/*
85 * Just mark the entry as stale, so that a next attempt to look it up
86 * will result in a new lookup call to userspace
87 *
88 * This is called when a dentry is about to become negative and the
89 * timeout is unknown (unlink, rmdir, rename and in some cases
90 * lookup)
91 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -080092static void fuse_invalidate_entry_cache(struct dentry *entry)
93{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070094 fuse_dentry_settime(entry, 0);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -080095}
96
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080097/*
98 * Same as fuse_invalidate_entry_cache(), but also try to remove the
99 * dentry from the hash
100 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800101static void fuse_invalidate_entry(struct dentry *entry)
102{
103 d_invalidate(entry);
104 fuse_invalidate_entry_cache(entry);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800105}
106
Miklos Szeredie5e55582005-09-09 13:10:28 -0700107static void fuse_lookup_init(struct fuse_req *req, struct inode *dir,
108 struct dentry *entry,
109 struct fuse_entry_out *outarg)
110{
111 req->in.h.opcode = FUSE_LOOKUP;
112 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700113 req->in.numargs = 1;
114 req->in.args[0].size = entry->d_name.len + 1;
115 req->in.args[0].value = entry->d_name.name;
116 req->out.numargs = 1;
117 req->out.args[0].size = sizeof(struct fuse_entry_out);
118 req->out.args[0].value = outarg;
119}
120
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800121/*
122 * Check whether the dentry is still valid
123 *
124 * If the entry validity timeout has expired and the dentry is
125 * positive, try to redo the lookup. If the lookup results in a
126 * different inode, then let the VFS invalidate the dentry and redo
127 * the lookup once more. If the lookup results in the same inode,
128 * then refresh the attributes, timeouts and mark the dentry valid.
129 */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700130static int fuse_dentry_revalidate(struct dentry *entry, struct nameidata *nd)
131{
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800132 struct inode *inode = entry->d_inode;
133
134 if (inode && is_bad_inode(inode))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700135 return 0;
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700136 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700137 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700138 struct fuse_entry_out outarg;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800139 struct fuse_conn *fc;
140 struct fuse_req *req;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800141 struct fuse_req *forget_req;
Miklos Szeredie956edd2006-10-17 00:10:12 -0700142 struct dentry *parent;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800143
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800144 /* For negative dentries, always do a fresh lookup */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800145 if (!inode)
146 return 0;
147
148 fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700149 req = fuse_get_req(fc);
150 if (IS_ERR(req))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700151 return 0;
152
Miklos Szeredi2d510132006-11-25 11:09:20 -0800153 forget_req = fuse_get_req(fc);
154 if (IS_ERR(forget_req)) {
155 fuse_put_request(fc, req);
156 return 0;
157 }
158
Miklos Szeredie956edd2006-10-17 00:10:12 -0700159 parent = dget_parent(entry);
160 fuse_lookup_init(req, parent->d_inode, entry, &outarg);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700161 request_send(fc, req);
Miklos Szeredie956edd2006-10-17 00:10:12 -0700162 dput(parent);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700163 err = req->out.h.error;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800164 fuse_put_request(fc, req);
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800165 /* Zero nodeid is same as -ENOENT */
166 if (!err && !outarg.nodeid)
167 err = -ENOENT;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700168 if (!err) {
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800169 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700170 if (outarg.nodeid != get_node_id(inode)) {
Miklos Szeredi2d510132006-11-25 11:09:20 -0800171 fuse_send_forget(fc, forget_req,
172 outarg.nodeid, 1);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700173 return 0;
174 }
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700175 spin_lock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700176 fi->nlookup ++;
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700177 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700178 }
Miklos Szeredi2d510132006-11-25 11:09:20 -0800179 fuse_put_request(fc, forget_req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700180 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700181 return 0;
182
183 fuse_change_attributes(inode, &outarg.attr);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800184 fuse_change_timeout(entry, &outarg);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700185 }
186 return 1;
187}
188
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800189static int invalid_nodeid(u64 nodeid)
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800190{
191 return !nodeid || nodeid == FUSE_ROOT_ID;
192}
193
Miklos Szeredie5e55582005-09-09 13:10:28 -0700194static struct dentry_operations fuse_dentry_operations = {
195 .d_revalidate = fuse_dentry_revalidate,
196};
197
Timo Savolaa5bfffac2007-04-08 16:04:00 -0700198int fuse_valid_type(int m)
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800199{
200 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
201 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
202}
203
Miklos Szeredid2a85162006-10-17 00:10:11 -0700204/*
205 * Add a directory inode to a dentry, ensuring that no other dentry
206 * refers to this inode. Called with fc->inst_mutex.
207 */
208static int fuse_d_add_directory(struct dentry *entry, struct inode *inode)
209{
210 struct dentry *alias = d_find_alias(inode);
211 if (alias) {
212 /* This tries to shrink the subtree below alias */
213 fuse_invalidate_entry(alias);
214 dput(alias);
215 if (!list_empty(&inode->i_dentry))
216 return -EBUSY;
217 }
218 d_add(entry, inode);
219 return 0;
220}
221
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800222static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
223 struct nameidata *nd)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700224{
225 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700226 struct fuse_entry_out outarg;
227 struct inode *inode = NULL;
228 struct fuse_conn *fc = get_fuse_conn(dir);
229 struct fuse_req *req;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800230 struct fuse_req *forget_req;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700231
232 if (entry->d_name.len > FUSE_NAME_MAX)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800233 return ERR_PTR(-ENAMETOOLONG);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700234
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700235 req = fuse_get_req(fc);
236 if (IS_ERR(req))
237 return ERR_PTR(PTR_ERR(req));
Miklos Szeredie5e55582005-09-09 13:10:28 -0700238
Miklos Szeredi2d510132006-11-25 11:09:20 -0800239 forget_req = fuse_get_req(fc);
240 if (IS_ERR(forget_req)) {
241 fuse_put_request(fc, req);
242 return ERR_PTR(PTR_ERR(forget_req));
243 }
244
Miklos Szeredie5e55582005-09-09 13:10:28 -0700245 fuse_lookup_init(req, dir, entry, &outarg);
246 request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700247 err = req->out.h.error;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800248 fuse_put_request(fc, req);
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800249 /* Zero nodeid is same as -ENOENT, but with valid timeout */
250 if (!err && outarg.nodeid &&
Timo Savolaa5bfffac2007-04-08 16:04:00 -0700251 (invalid_nodeid(outarg.nodeid) ||
252 !fuse_valid_type(outarg.attr.mode)))
Miklos Szerediee4e5272005-09-27 21:45:21 -0700253 err = -EIO;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800254 if (!err && outarg.nodeid) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700255 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700256 &outarg.attr);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700257 if (!inode) {
Miklos Szeredi2d510132006-11-25 11:09:20 -0800258 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800259 return ERR_PTR(-ENOMEM);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700260 }
261 }
Miklos Szeredi2d510132006-11-25 11:09:20 -0800262 fuse_put_request(fc, forget_req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700263 if (err && err != -ENOENT)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800264 return ERR_PTR(err);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700265
Miklos Szeredid2a85162006-10-17 00:10:11 -0700266 if (inode && S_ISDIR(inode->i_mode)) {
267 mutex_lock(&fc->inst_mutex);
268 err = fuse_d_add_directory(entry, inode);
269 mutex_unlock(&fc->inst_mutex);
270 if (err) {
271 iput(inode);
272 return ERR_PTR(err);
273 }
274 } else
275 d_add(entry, inode);
276
Miklos Szeredie5e55582005-09-09 13:10:28 -0700277 entry->d_op = &fuse_dentry_operations;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800278 if (!err)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800279 fuse_change_timeout(entry, &outarg);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800280 else
281 fuse_invalidate_entry_cache(entry);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800282 return NULL;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700283}
284
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800285/*
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700286 * Synchronous release for the case when something goes wrong in CREATE_OPEN
287 */
288static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
289 u64 nodeid, int flags)
290{
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700291 fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
292 ff->reserved_req->force = 1;
293 request_send(fc, ff->reserved_req);
294 fuse_put_request(fc, ff->reserved_req);
295 kfree(ff);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700296}
297
298/*
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800299 * Atomic create+open operation
300 *
301 * If the filesystem doesn't support this, then fall back to separate
302 * 'mknod' + 'open' requests.
303 */
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800304static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
305 struct nameidata *nd)
306{
307 int err;
308 struct inode *inode;
309 struct fuse_conn *fc = get_fuse_conn(dir);
310 struct fuse_req *req;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700311 struct fuse_req *forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800312 struct fuse_open_in inarg;
313 struct fuse_open_out outopen;
314 struct fuse_entry_out outentry;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800315 struct fuse_file *ff;
316 struct file *file;
317 int flags = nd->intent.open.flags - 1;
318
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800319 if (fc->no_create)
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700320 return -ENOSYS;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800321
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700322 forget_req = fuse_get_req(fc);
323 if (IS_ERR(forget_req))
324 return PTR_ERR(forget_req);
325
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700326 req = fuse_get_req(fc);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700327 err = PTR_ERR(req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700328 if (IS_ERR(req))
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700329 goto out_put_forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800330
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700331 err = -ENOMEM;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800332 ff = fuse_file_alloc();
333 if (!ff)
334 goto out_put_request;
335
336 flags &= ~O_NOCTTY;
337 memset(&inarg, 0, sizeof(inarg));
338 inarg.flags = flags;
339 inarg.mode = mode;
340 req->in.h.opcode = FUSE_CREATE;
341 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800342 req->in.numargs = 2;
343 req->in.args[0].size = sizeof(inarg);
344 req->in.args[0].value = &inarg;
345 req->in.args[1].size = entry->d_name.len + 1;
346 req->in.args[1].value = entry->d_name.name;
347 req->out.numargs = 2;
348 req->out.args[0].size = sizeof(outentry);
349 req->out.args[0].value = &outentry;
350 req->out.args[1].size = sizeof(outopen);
351 req->out.args[1].value = &outopen;
352 request_send(fc, req);
353 err = req->out.h.error;
354 if (err) {
355 if (err == -ENOSYS)
356 fc->no_create = 1;
357 goto out_free_ff;
358 }
359
360 err = -EIO;
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800361 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800362 goto out_free_ff;
363
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700364 fuse_put_request(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800365 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
366 &outentry.attr);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800367 if (!inode) {
368 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
369 ff->fh = outopen.fh;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700370 fuse_sync_release(fc, ff, outentry.nodeid, flags);
371 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
372 return -ENOMEM;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800373 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700374 fuse_put_request(fc, forget_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800375 d_instantiate(entry, inode);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800376 fuse_change_timeout(entry, &outentry);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800377 file = lookup_instantiate_filp(nd, entry, generic_file_open);
378 if (IS_ERR(file)) {
379 ff->fh = outopen.fh;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700380 fuse_sync_release(fc, ff, outentry.nodeid, flags);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800381 return PTR_ERR(file);
382 }
383 fuse_finish_open(inode, file, ff, &outopen);
384 return 0;
385
386 out_free_ff:
387 fuse_file_free(ff);
388 out_put_request:
389 fuse_put_request(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700390 out_put_forget_req:
391 fuse_put_request(fc, forget_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800392 return err;
393}
394
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800395/*
396 * Code shared between mknod, mkdir, symlink and link
397 */
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700398static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
399 struct inode *dir, struct dentry *entry,
400 int mode)
401{
402 struct fuse_entry_out outarg;
403 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700404 int err;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800405 struct fuse_req *forget_req;
406
407 forget_req = fuse_get_req(fc);
408 if (IS_ERR(forget_req)) {
409 fuse_put_request(fc, req);
410 return PTR_ERR(forget_req);
411 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700412
413 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700414 req->out.numargs = 1;
415 req->out.args[0].size = sizeof(outarg);
416 req->out.args[0].value = &outarg;
417 request_send(fc, req);
418 err = req->out.h.error;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800419 fuse_put_request(fc, req);
420 if (err)
421 goto out_put_forget_req;
422
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800423 err = -EIO;
424 if (invalid_nodeid(outarg.nodeid))
Miklos Szeredi2d510132006-11-25 11:09:20 -0800425 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800426
427 if ((outarg.attr.mode ^ mode) & S_IFMT)
Miklos Szeredi2d510132006-11-25 11:09:20 -0800428 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800429
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700430 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
431 &outarg.attr);
432 if (!inode) {
Miklos Szeredi2d510132006-11-25 11:09:20 -0800433 fuse_send_forget(fc, forget_req, outarg.nodeid, 1);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700434 return -ENOMEM;
435 }
Miklos Szeredi2d510132006-11-25 11:09:20 -0800436 fuse_put_request(fc, forget_req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700437
Miklos Szeredid2a85162006-10-17 00:10:11 -0700438 if (S_ISDIR(inode->i_mode)) {
439 struct dentry *alias;
440 mutex_lock(&fc->inst_mutex);
441 alias = d_find_alias(inode);
442 if (alias) {
443 /* New directory must have moved since mkdir */
444 mutex_unlock(&fc->inst_mutex);
445 dput(alias);
446 iput(inode);
447 return -EBUSY;
448 }
449 d_instantiate(entry, inode);
450 mutex_unlock(&fc->inst_mutex);
451 } else
452 d_instantiate(entry, inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700453
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800454 fuse_change_timeout(entry, &outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700455 fuse_invalidate_attr(dir);
456 return 0;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800457
Miklos Szeredi2d510132006-11-25 11:09:20 -0800458 out_put_forget_req:
459 fuse_put_request(fc, forget_req);
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800460 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700461}
462
463static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
464 dev_t rdev)
465{
466 struct fuse_mknod_in inarg;
467 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700468 struct fuse_req *req = fuse_get_req(fc);
469 if (IS_ERR(req))
470 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700471
472 memset(&inarg, 0, sizeof(inarg));
473 inarg.mode = mode;
474 inarg.rdev = new_encode_dev(rdev);
475 req->in.h.opcode = FUSE_MKNOD;
476 req->in.numargs = 2;
477 req->in.args[0].size = sizeof(inarg);
478 req->in.args[0].value = &inarg;
479 req->in.args[1].size = entry->d_name.len + 1;
480 req->in.args[1].value = entry->d_name.name;
481 return create_new_entry(fc, req, dir, entry, mode);
482}
483
484static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
485 struct nameidata *nd)
486{
Miklos Szeredib9ba3472007-05-23 13:57:32 -0700487 if (nd && (nd->flags & LOOKUP_OPEN)) {
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800488 int err = fuse_create_open(dir, entry, mode, nd);
489 if (err != -ENOSYS)
490 return err;
491 /* Fall back on mknod */
492 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700493 return fuse_mknod(dir, entry, mode, 0);
494}
495
496static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
497{
498 struct fuse_mkdir_in inarg;
499 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700500 struct fuse_req *req = fuse_get_req(fc);
501 if (IS_ERR(req))
502 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700503
504 memset(&inarg, 0, sizeof(inarg));
505 inarg.mode = mode;
506 req->in.h.opcode = FUSE_MKDIR;
507 req->in.numargs = 2;
508 req->in.args[0].size = sizeof(inarg);
509 req->in.args[0].value = &inarg;
510 req->in.args[1].size = entry->d_name.len + 1;
511 req->in.args[1].value = entry->d_name.name;
512 return create_new_entry(fc, req, dir, entry, S_IFDIR);
513}
514
515static int fuse_symlink(struct inode *dir, struct dentry *entry,
516 const char *link)
517{
518 struct fuse_conn *fc = get_fuse_conn(dir);
519 unsigned len = strlen(link) + 1;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700520 struct fuse_req *req = fuse_get_req(fc);
521 if (IS_ERR(req))
522 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700523
524 req->in.h.opcode = FUSE_SYMLINK;
525 req->in.numargs = 2;
526 req->in.args[0].size = entry->d_name.len + 1;
527 req->in.args[0].value = entry->d_name.name;
528 req->in.args[1].size = len;
529 req->in.args[1].value = link;
530 return create_new_entry(fc, req, dir, entry, S_IFLNK);
531}
532
533static int fuse_unlink(struct inode *dir, struct dentry *entry)
534{
535 int err;
536 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700537 struct fuse_req *req = fuse_get_req(fc);
538 if (IS_ERR(req))
539 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700540
541 req->in.h.opcode = FUSE_UNLINK;
542 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700543 req->in.numargs = 1;
544 req->in.args[0].size = entry->d_name.len + 1;
545 req->in.args[0].value = entry->d_name.name;
546 request_send(fc, req);
547 err = req->out.h.error;
548 fuse_put_request(fc, req);
549 if (!err) {
550 struct inode *inode = entry->d_inode;
551
552 /* Set nlink to zero so the inode can be cleared, if
553 the inode does have more links this will be
554 discovered at the next lookup/getattr */
Dave Hansence71ec32006-09-30 23:29:06 -0700555 clear_nlink(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700556 fuse_invalidate_attr(inode);
557 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800558 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700559 } else if (err == -EINTR)
560 fuse_invalidate_entry(entry);
561 return err;
562}
563
564static int fuse_rmdir(struct inode *dir, struct dentry *entry)
565{
566 int err;
567 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700568 struct fuse_req *req = fuse_get_req(fc);
569 if (IS_ERR(req))
570 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700571
572 req->in.h.opcode = FUSE_RMDIR;
573 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700574 req->in.numargs = 1;
575 req->in.args[0].size = entry->d_name.len + 1;
576 req->in.args[0].value = entry->d_name.name;
577 request_send(fc, req);
578 err = req->out.h.error;
579 fuse_put_request(fc, req);
580 if (!err) {
Dave Hansence71ec32006-09-30 23:29:06 -0700581 clear_nlink(entry->d_inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700582 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800583 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700584 } else if (err == -EINTR)
585 fuse_invalidate_entry(entry);
586 return err;
587}
588
589static int fuse_rename(struct inode *olddir, struct dentry *oldent,
590 struct inode *newdir, struct dentry *newent)
591{
592 int err;
593 struct fuse_rename_in inarg;
594 struct fuse_conn *fc = get_fuse_conn(olddir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700595 struct fuse_req *req = fuse_get_req(fc);
596 if (IS_ERR(req))
597 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700598
599 memset(&inarg, 0, sizeof(inarg));
600 inarg.newdir = get_node_id(newdir);
601 req->in.h.opcode = FUSE_RENAME;
602 req->in.h.nodeid = get_node_id(olddir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700603 req->in.numargs = 3;
604 req->in.args[0].size = sizeof(inarg);
605 req->in.args[0].value = &inarg;
606 req->in.args[1].size = oldent->d_name.len + 1;
607 req->in.args[1].value = oldent->d_name.name;
608 req->in.args[2].size = newent->d_name.len + 1;
609 req->in.args[2].value = newent->d_name.name;
610 request_send(fc, req);
611 err = req->out.h.error;
612 fuse_put_request(fc, req);
613 if (!err) {
614 fuse_invalidate_attr(olddir);
615 if (olddir != newdir)
616 fuse_invalidate_attr(newdir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800617
618 /* newent will end up negative */
619 if (newent->d_inode)
620 fuse_invalidate_entry_cache(newent);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700621 } else if (err == -EINTR) {
622 /* If request was interrupted, DEITY only knows if the
623 rename actually took place. If the invalidation
624 fails (e.g. some process has CWD under the renamed
625 directory), then there can be inconsistency between
626 the dcache and the real filesystem. Tough luck. */
627 fuse_invalidate_entry(oldent);
628 if (newent->d_inode)
629 fuse_invalidate_entry(newent);
630 }
631
632 return err;
633}
634
635static int fuse_link(struct dentry *entry, struct inode *newdir,
636 struct dentry *newent)
637{
638 int err;
639 struct fuse_link_in inarg;
640 struct inode *inode = entry->d_inode;
641 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700642 struct fuse_req *req = fuse_get_req(fc);
643 if (IS_ERR(req))
644 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700645
646 memset(&inarg, 0, sizeof(inarg));
647 inarg.oldnodeid = get_node_id(inode);
648 req->in.h.opcode = FUSE_LINK;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700649 req->in.numargs = 2;
650 req->in.args[0].size = sizeof(inarg);
651 req->in.args[0].value = &inarg;
652 req->in.args[1].size = newent->d_name.len + 1;
653 req->in.args[1].value = newent->d_name.name;
654 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
655 /* Contrary to "normal" filesystems it can happen that link
656 makes two "logical" inodes point to the same "physical"
657 inode. We invalidate the attributes of the old one, so it
658 will reflect changes in the backing inode (link count,
659 etc.)
660 */
661 if (!err || err == -EINTR)
662 fuse_invalidate_attr(inode);
663 return err;
664}
665
Miklos Szeredie5e55582005-09-09 13:10:28 -0700666int fuse_do_getattr(struct inode *inode)
667{
668 int err;
669 struct fuse_attr_out arg;
670 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700671 struct fuse_req *req = fuse_get_req(fc);
672 if (IS_ERR(req))
673 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700674
675 req->in.h.opcode = FUSE_GETATTR;
676 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700677 req->out.numargs = 1;
678 req->out.args[0].size = sizeof(arg);
679 req->out.args[0].value = &arg;
680 request_send(fc, req);
681 err = req->out.h.error;
682 fuse_put_request(fc, req);
683 if (!err) {
684 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
685 make_bad_inode(inode);
686 err = -EIO;
687 } else {
688 struct fuse_inode *fi = get_fuse_inode(inode);
689 fuse_change_attributes(inode, &arg.attr);
690 fi->i_time = time_to_jiffies(arg.attr_valid,
691 arg.attr_valid_nsec);
692 }
693 }
694 return err;
695}
696
Miklos Szeredi87729a52005-09-09 13:10:34 -0700697/*
698 * Calling into a user-controlled filesystem gives the filesystem
699 * daemon ptrace-like capabilities over the requester process. This
700 * means, that the filesystem daemon is able to record the exact
701 * filesystem operations performed, and can also control the behavior
702 * of the requester process in otherwise impossible ways. For example
703 * it can delay the operation for arbitrary length of time allowing
704 * DoS against the requester.
705 *
706 * For this reason only those processes can call into the filesystem,
707 * for which the owner of the mount has ptrace privilege. This
708 * excludes processes started by other users, suid or sgid processes.
709 */
710static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
711{
712 if (fc->flags & FUSE_ALLOW_OTHER)
713 return 1;
714
715 if (task->euid == fc->user_id &&
716 task->suid == fc->user_id &&
717 task->uid == fc->user_id &&
718 task->egid == fc->group_id &&
719 task->sgid == fc->group_id &&
720 task->gid == fc->group_id)
721 return 1;
722
723 return 0;
724}
725
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800726/*
727 * Check whether the inode attributes are still valid
728 *
729 * If the attribute validity timeout has expired, then fetch the fresh
730 * attributes with a 'getattr' request
731 *
732 * I'm not sure why cached attributes are never returned for the root
733 * inode, this is probably being too cautious.
734 */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700735static int fuse_revalidate(struct dentry *entry)
736{
737 struct inode *inode = entry->d_inode;
738 struct fuse_inode *fi = get_fuse_inode(inode);
739 struct fuse_conn *fc = get_fuse_conn(inode);
740
Miklos Szeredi87729a52005-09-09 13:10:34 -0700741 if (!fuse_allow_task(fc, current))
742 return -EACCES;
743 if (get_node_id(inode) != FUSE_ROOT_ID &&
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700744 fi->i_time >= get_jiffies_64())
Miklos Szeredie5e55582005-09-09 13:10:28 -0700745 return 0;
746
747 return fuse_do_getattr(inode);
748}
749
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800750static int fuse_access(struct inode *inode, int mask)
751{
752 struct fuse_conn *fc = get_fuse_conn(inode);
753 struct fuse_req *req;
754 struct fuse_access_in inarg;
755 int err;
756
757 if (fc->no_access)
758 return 0;
759
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700760 req = fuse_get_req(fc);
761 if (IS_ERR(req))
762 return PTR_ERR(req);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800763
764 memset(&inarg, 0, sizeof(inarg));
765 inarg.mask = mask;
766 req->in.h.opcode = FUSE_ACCESS;
767 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800768 req->in.numargs = 1;
769 req->in.args[0].size = sizeof(inarg);
770 req->in.args[0].value = &inarg;
771 request_send(fc, req);
772 err = req->out.h.error;
773 fuse_put_request(fc, req);
774 if (err == -ENOSYS) {
775 fc->no_access = 1;
776 err = 0;
777 }
778 return err;
779}
780
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800781/*
782 * Check permission. The two basic access models of FUSE are:
783 *
784 * 1) Local access checking ('default_permissions' mount option) based
785 * on file mode. This is the plain old disk filesystem permission
786 * modell.
787 *
788 * 2) "Remote" access checking, where server is responsible for
789 * checking permission in each inode operation. An exception to this
790 * is if ->permission() was invoked from sys_access() in which case an
791 * access request is sent. Execute permission is still checked
792 * locally based on file mode.
793 */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700794static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
795{
796 struct fuse_conn *fc = get_fuse_conn(inode);
797
Miklos Szeredi87729a52005-09-09 13:10:34 -0700798 if (!fuse_allow_task(fc, current))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700799 return -EACCES;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700800 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
801 int err = generic_permission(inode, mask, NULL);
802
803 /* If permission is denied, try to refresh file
804 attributes. This is also needed, because the root
805 node will at first have no permissions */
806 if (err == -EACCES) {
807 err = fuse_do_getattr(inode);
808 if (!err)
809 err = generic_permission(inode, mask, NULL);
810 }
811
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800812 /* Note: the opposite of the above test does not
813 exist. So if permissions are revoked this won't be
814 noticed immediately, only after the attribute
815 timeout has expired */
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700816
817 return err;
818 } else {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700819 int mode = inode->i_mode;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700820 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
821 return -EACCES;
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800822
Miklos Szeredi650a8982006-09-29 01:59:35 -0700823 if (nd && (nd->flags & (LOOKUP_ACCESS | LOOKUP_CHDIR)))
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800824 return fuse_access(inode, mask);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700825 return 0;
826 }
827}
828
829static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
830 void *dstbuf, filldir_t filldir)
831{
832 while (nbytes >= FUSE_NAME_OFFSET) {
833 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
834 size_t reclen = FUSE_DIRENT_SIZE(dirent);
835 int over;
836 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
837 return -EIO;
838 if (reclen > nbytes)
839 break;
840
841 over = filldir(dstbuf, dirent->name, dirent->namelen,
842 file->f_pos, dirent->ino, dirent->type);
843 if (over)
844 break;
845
846 buf += reclen;
847 nbytes -= reclen;
848 file->f_pos = dirent->off;
849 }
850
851 return 0;
852}
853
Miklos Szeredie5e55582005-09-09 13:10:28 -0700854static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
855{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700856 int err;
857 size_t nbytes;
858 struct page *page;
Josef Sipek7706a9d2006-12-08 02:37:02 -0800859 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700860 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700861 struct fuse_file *ff = file->private_data;
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800862 struct fuse_req *req;
863
864 if (is_bad_inode(inode))
865 return -EIO;
866
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700867 req = fuse_get_req(fc);
868 if (IS_ERR(req))
869 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700870
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700871 page = alloc_page(GFP_KERNEL);
872 if (!page) {
873 fuse_put_request(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700874 return -ENOMEM;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700875 }
876 req->num_pages = 1;
877 req->pages[0] = page;
Miklos Szeredic756e0a2007-10-16 23:31:00 -0700878 fuse_read_fill(req, ff, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -0800879 request_send(fc, req);
880 nbytes = req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700881 err = req->out.h.error;
882 fuse_put_request(fc, req);
883 if (!err)
884 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
885 filldir);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700886
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700887 __free_page(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700888 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700889 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700890}
891
892static char *read_link(struct dentry *dentry)
893{
894 struct inode *inode = dentry->d_inode;
895 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700896 struct fuse_req *req = fuse_get_req(fc);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700897 char *link;
898
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700899 if (IS_ERR(req))
900 return ERR_PTR(PTR_ERR(req));
Miklos Szeredie5e55582005-09-09 13:10:28 -0700901
902 link = (char *) __get_free_page(GFP_KERNEL);
903 if (!link) {
904 link = ERR_PTR(-ENOMEM);
905 goto out;
906 }
907 req->in.h.opcode = FUSE_READLINK;
908 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700909 req->out.argvar = 1;
910 req->out.numargs = 1;
911 req->out.args[0].size = PAGE_SIZE - 1;
912 req->out.args[0].value = link;
913 request_send(fc, req);
914 if (req->out.h.error) {
915 free_page((unsigned long) link);
916 link = ERR_PTR(req->out.h.error);
917 } else
918 link[req->out.args[0].size] = '\0';
919 out:
920 fuse_put_request(fc, req);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700921 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700922 return link;
923}
924
925static void free_link(char *link)
926{
927 if (!IS_ERR(link))
928 free_page((unsigned long) link);
929}
930
931static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
932{
933 nd_set_link(nd, read_link(dentry));
934 return NULL;
935}
936
937static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
938{
939 free_link(nd_get_link(nd));
940}
941
942static int fuse_dir_open(struct inode *inode, struct file *file)
943{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700944 return fuse_open_common(inode, file, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700945}
946
947static int fuse_dir_release(struct inode *inode, struct file *file)
948{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700949 return fuse_release_common(inode, file, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700950}
951
Miklos Szeredi82547982005-09-09 13:10:38 -0700952static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
953{
954 /* nfsd can call this with no file */
955 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
956}
957
Miklos Szeredibefc6492005-11-07 00:59:52 -0800958static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700959{
960 unsigned ivalid = iattr->ia_valid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700961
962 if (ivalid & ATTR_MODE)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800963 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700964 if (ivalid & ATTR_UID)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800965 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700966 if (ivalid & ATTR_GID)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800967 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700968 if (ivalid & ATTR_SIZE)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800969 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700970 /* You can only _set_ these together (they may change by themselves) */
971 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
Miklos Szeredibefc6492005-11-07 00:59:52 -0800972 arg->valid |= FATTR_ATIME | FATTR_MTIME;
973 arg->atime = iattr->ia_atime.tv_sec;
974 arg->mtime = iattr->ia_mtime.tv_sec;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700975 }
Miklos Szeredibefc6492005-11-07 00:59:52 -0800976 if (ivalid & ATTR_FILE) {
977 struct fuse_file *ff = iattr->ia_file->private_data;
978 arg->valid |= FATTR_FH;
979 arg->fh = ff->fh;
980 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700981}
982
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800983/*
984 * Set attributes, and at the same time refresh them.
985 *
986 * Truncation is slightly complicated, because the 'truncate' request
987 * may fail, in which case we don't want to touch the mapping.
Miklos Szeredi9ffbb912006-10-17 00:10:06 -0700988 * vmtruncate() doesn't allow for this case, so do the rlimit checking
989 * and the actual truncation by hand.
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800990 */
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700991static int fuse_setattr(struct dentry *entry, struct iattr *attr)
992{
993 struct inode *inode = entry->d_inode;
994 struct fuse_conn *fc = get_fuse_conn(inode);
995 struct fuse_inode *fi = get_fuse_inode(inode);
996 struct fuse_req *req;
997 struct fuse_setattr_in inarg;
998 struct fuse_attr_out outarg;
999 int err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001000
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001001 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1002 err = inode_change_ok(inode, attr);
1003 if (err)
1004 return err;
1005 }
1006
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001007 if (attr->ia_valid & ATTR_SIZE) {
1008 unsigned long limit;
Miklos Szeredib2d22722006-12-06 20:35:51 -08001009 if (IS_SWAPFILE(inode))
1010 return -ETXTBSY;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001011 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
1012 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
1013 send_sig(SIGXFSZ, current, 0);
1014 return -EFBIG;
1015 }
1016 }
1017
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001018 req = fuse_get_req(fc);
1019 if (IS_ERR(req))
1020 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001021
1022 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredibefc6492005-11-07 00:59:52 -08001023 iattr_to_fattr(attr, &inarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001024 req->in.h.opcode = FUSE_SETATTR;
1025 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001026 req->in.numargs = 1;
1027 req->in.args[0].size = sizeof(inarg);
1028 req->in.args[0].value = &inarg;
1029 req->out.numargs = 1;
1030 req->out.args[0].size = sizeof(outarg);
1031 req->out.args[0].value = &outarg;
1032 request_send(fc, req);
1033 err = req->out.h.error;
1034 fuse_put_request(fc, req);
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001035 if (err) {
1036 if (err == -EINTR)
1037 fuse_invalidate_attr(inode);
1038 return err;
1039 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001040
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001041 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1042 make_bad_inode(inode);
1043 return -EIO;
1044 }
1045
1046 fuse_change_attributes(inode, &outarg.attr);
1047 fi->i_time = time_to_jiffies(outarg.attr_valid, outarg.attr_valid_nsec);
1048 return 0;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001049}
1050
Miklos Szeredie5e55582005-09-09 13:10:28 -07001051static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1052 struct kstat *stat)
1053{
1054 struct inode *inode = entry->d_inode;
1055 int err = fuse_revalidate(entry);
1056 if (!err)
1057 generic_fillattr(inode, stat);
1058
1059 return err;
1060}
1061
Miklos Szeredi92a87802005-09-09 13:10:31 -07001062static int fuse_setxattr(struct dentry *entry, const char *name,
1063 const void *value, size_t size, int flags)
1064{
1065 struct inode *inode = entry->d_inode;
1066 struct fuse_conn *fc = get_fuse_conn(inode);
1067 struct fuse_req *req;
1068 struct fuse_setxattr_in inarg;
1069 int err;
1070
Miklos Szeredi92a87802005-09-09 13:10:31 -07001071 if (fc->no_setxattr)
1072 return -EOPNOTSUPP;
1073
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001074 req = fuse_get_req(fc);
1075 if (IS_ERR(req))
1076 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001077
1078 memset(&inarg, 0, sizeof(inarg));
1079 inarg.size = size;
1080 inarg.flags = flags;
1081 req->in.h.opcode = FUSE_SETXATTR;
1082 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001083 req->in.numargs = 3;
1084 req->in.args[0].size = sizeof(inarg);
1085 req->in.args[0].value = &inarg;
1086 req->in.args[1].size = strlen(name) + 1;
1087 req->in.args[1].value = name;
1088 req->in.args[2].size = size;
1089 req->in.args[2].value = value;
1090 request_send(fc, req);
1091 err = req->out.h.error;
1092 fuse_put_request(fc, req);
1093 if (err == -ENOSYS) {
1094 fc->no_setxattr = 1;
1095 err = -EOPNOTSUPP;
1096 }
1097 return err;
1098}
1099
1100static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1101 void *value, size_t size)
1102{
1103 struct inode *inode = entry->d_inode;
1104 struct fuse_conn *fc = get_fuse_conn(inode);
1105 struct fuse_req *req;
1106 struct fuse_getxattr_in inarg;
1107 struct fuse_getxattr_out outarg;
1108 ssize_t ret;
1109
1110 if (fc->no_getxattr)
1111 return -EOPNOTSUPP;
1112
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001113 req = fuse_get_req(fc);
1114 if (IS_ERR(req))
1115 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001116
1117 memset(&inarg, 0, sizeof(inarg));
1118 inarg.size = size;
1119 req->in.h.opcode = FUSE_GETXATTR;
1120 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001121 req->in.numargs = 2;
1122 req->in.args[0].size = sizeof(inarg);
1123 req->in.args[0].value = &inarg;
1124 req->in.args[1].size = strlen(name) + 1;
1125 req->in.args[1].value = name;
1126 /* This is really two different operations rolled into one */
1127 req->out.numargs = 1;
1128 if (size) {
1129 req->out.argvar = 1;
1130 req->out.args[0].size = size;
1131 req->out.args[0].value = value;
1132 } else {
1133 req->out.args[0].size = sizeof(outarg);
1134 req->out.args[0].value = &outarg;
1135 }
1136 request_send(fc, req);
1137 ret = req->out.h.error;
1138 if (!ret)
1139 ret = size ? req->out.args[0].size : outarg.size;
1140 else {
1141 if (ret == -ENOSYS) {
1142 fc->no_getxattr = 1;
1143 ret = -EOPNOTSUPP;
1144 }
1145 }
1146 fuse_put_request(fc, req);
1147 return ret;
1148}
1149
1150static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1151{
1152 struct inode *inode = entry->d_inode;
1153 struct fuse_conn *fc = get_fuse_conn(inode);
1154 struct fuse_req *req;
1155 struct fuse_getxattr_in inarg;
1156 struct fuse_getxattr_out outarg;
1157 ssize_t ret;
1158
1159 if (fc->no_listxattr)
1160 return -EOPNOTSUPP;
1161
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001162 req = fuse_get_req(fc);
1163 if (IS_ERR(req))
1164 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001165
1166 memset(&inarg, 0, sizeof(inarg));
1167 inarg.size = size;
1168 req->in.h.opcode = FUSE_LISTXATTR;
1169 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001170 req->in.numargs = 1;
1171 req->in.args[0].size = sizeof(inarg);
1172 req->in.args[0].value = &inarg;
1173 /* This is really two different operations rolled into one */
1174 req->out.numargs = 1;
1175 if (size) {
1176 req->out.argvar = 1;
1177 req->out.args[0].size = size;
1178 req->out.args[0].value = list;
1179 } else {
1180 req->out.args[0].size = sizeof(outarg);
1181 req->out.args[0].value = &outarg;
1182 }
1183 request_send(fc, req);
1184 ret = req->out.h.error;
1185 if (!ret)
1186 ret = size ? req->out.args[0].size : outarg.size;
1187 else {
1188 if (ret == -ENOSYS) {
1189 fc->no_listxattr = 1;
1190 ret = -EOPNOTSUPP;
1191 }
1192 }
1193 fuse_put_request(fc, req);
1194 return ret;
1195}
1196
1197static int fuse_removexattr(struct dentry *entry, const char *name)
1198{
1199 struct inode *inode = entry->d_inode;
1200 struct fuse_conn *fc = get_fuse_conn(inode);
1201 struct fuse_req *req;
1202 int err;
1203
1204 if (fc->no_removexattr)
1205 return -EOPNOTSUPP;
1206
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001207 req = fuse_get_req(fc);
1208 if (IS_ERR(req))
1209 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001210
1211 req->in.h.opcode = FUSE_REMOVEXATTR;
1212 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001213 req->in.numargs = 1;
1214 req->in.args[0].size = strlen(name) + 1;
1215 req->in.args[0].value = name;
1216 request_send(fc, req);
1217 err = req->out.h.error;
1218 fuse_put_request(fc, req);
1219 if (err == -ENOSYS) {
1220 fc->no_removexattr = 1;
1221 err = -EOPNOTSUPP;
1222 }
1223 return err;
1224}
1225
Arjan van de Ven754661f2007-02-12 00:55:38 -08001226static const struct inode_operations fuse_dir_inode_operations = {
Miklos Szeredie5e55582005-09-09 13:10:28 -07001227 .lookup = fuse_lookup,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001228 .mkdir = fuse_mkdir,
1229 .symlink = fuse_symlink,
1230 .unlink = fuse_unlink,
1231 .rmdir = fuse_rmdir,
1232 .rename = fuse_rename,
1233 .link = fuse_link,
1234 .setattr = fuse_setattr,
1235 .create = fuse_create,
1236 .mknod = fuse_mknod,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001237 .permission = fuse_permission,
1238 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001239 .setxattr = fuse_setxattr,
1240 .getxattr = fuse_getxattr,
1241 .listxattr = fuse_listxattr,
1242 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001243};
1244
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001245static const struct file_operations fuse_dir_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001246 .llseek = generic_file_llseek,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001247 .read = generic_read_dir,
1248 .readdir = fuse_readdir,
1249 .open = fuse_dir_open,
1250 .release = fuse_dir_release,
Miklos Szeredi82547982005-09-09 13:10:38 -07001251 .fsync = fuse_dir_fsync,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001252};
1253
Arjan van de Ven754661f2007-02-12 00:55:38 -08001254static const struct inode_operations fuse_common_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001255 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001256 .permission = fuse_permission,
1257 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001258 .setxattr = fuse_setxattr,
1259 .getxattr = fuse_getxattr,
1260 .listxattr = fuse_listxattr,
1261 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001262};
1263
Arjan van de Ven754661f2007-02-12 00:55:38 -08001264static const struct inode_operations fuse_symlink_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001265 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001266 .follow_link = fuse_follow_link,
1267 .put_link = fuse_put_link,
1268 .readlink = generic_readlink,
1269 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001270 .setxattr = fuse_setxattr,
1271 .getxattr = fuse_getxattr,
1272 .listxattr = fuse_listxattr,
1273 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001274};
1275
1276void fuse_init_common(struct inode *inode)
1277{
1278 inode->i_op = &fuse_common_inode_operations;
1279}
1280
1281void fuse_init_dir(struct inode *inode)
1282{
1283 inode->i_op = &fuse_dir_inode_operations;
1284 inode->i_fop = &fuse_dir_operations;
1285}
1286
1287void fuse_init_symlink(struct inode *inode)
1288{
1289 inode->i_op = &fuse_symlink_inode_operations;
1290}