blob: 409ce6a7cca4ccd2e53f46693a5e4301bef87245 [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;
141
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800142 /* Doesn't hurt to "reset" the validity timeout */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800143 fuse_invalidate_entry_cache(entry);
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800144
145 /* For negative dentries, always do a fresh lookup */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800146 if (!inode)
147 return 0;
148
149 fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700150 req = fuse_get_req(fc);
151 if (IS_ERR(req))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700152 return 0;
153
154 fuse_lookup_init(req, entry->d_parent->d_inode, entry, &outarg);
Miklos Szeredi7c352bd2005-09-09 13:10:39 -0700155 request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700156 err = req->out.h.error;
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800157 /* Zero nodeid is same as -ENOENT */
158 if (!err && !outarg.nodeid)
159 err = -ENOENT;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700160 if (!err) {
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800161 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700162 if (outarg.nodeid != get_node_id(inode)) {
163 fuse_send_forget(fc, req, outarg.nodeid, 1);
164 return 0;
165 }
166 fi->nlookup ++;
167 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700168 fuse_put_request(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700169 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700170 return 0;
171
172 fuse_change_attributes(inode, &outarg.attr);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800173 fuse_change_timeout(entry, &outarg);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700174 }
175 return 1;
176}
177
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800178/*
179 * Check if there's already a hashed alias of this directory inode.
180 * If yes, then lookup and mkdir must not create a new alias.
181 */
Miklos Szeredif007d5c2005-11-28 13:44:16 -0800182static int dir_alias(struct inode *inode)
183{
184 if (S_ISDIR(inode->i_mode)) {
Miklos Szeredif007d5c2005-11-28 13:44:16 -0800185 struct dentry *alias = d_find_alias(inode);
186 if (alias) {
187 dput(alias);
188 return 1;
189 }
190 }
191 return 0;
192}
193
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800194static int invalid_nodeid(u64 nodeid)
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800195{
196 return !nodeid || nodeid == FUSE_ROOT_ID;
197}
198
Miklos Szeredie5e55582005-09-09 13:10:28 -0700199static struct dentry_operations fuse_dentry_operations = {
200 .d_revalidate = fuse_dentry_revalidate,
201};
202
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800203static int valid_mode(int m)
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800204{
205 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
206 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
207}
208
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800209static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
210 struct nameidata *nd)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700211{
212 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700213 struct fuse_entry_out outarg;
214 struct inode *inode = NULL;
215 struct fuse_conn *fc = get_fuse_conn(dir);
216 struct fuse_req *req;
217
218 if (entry->d_name.len > FUSE_NAME_MAX)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800219 return ERR_PTR(-ENAMETOOLONG);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700220
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700221 req = fuse_get_req(fc);
222 if (IS_ERR(req))
223 return ERR_PTR(PTR_ERR(req));
Miklos Szeredie5e55582005-09-09 13:10:28 -0700224
225 fuse_lookup_init(req, dir, entry, &outarg);
226 request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700227 err = req->out.h.error;
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800228 /* Zero nodeid is same as -ENOENT, but with valid timeout */
229 if (!err && outarg.nodeid &&
230 (invalid_nodeid(outarg.nodeid) || !valid_mode(outarg.attr.mode)))
Miklos Szerediee4e5272005-09-27 21:45:21 -0700231 err = -EIO;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800232 if (!err && outarg.nodeid) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700233 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700234 &outarg.attr);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700235 if (!inode) {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700236 fuse_send_forget(fc, req, outarg.nodeid, 1);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800237 return ERR_PTR(-ENOMEM);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700238 }
239 }
240 fuse_put_request(fc, req);
241 if (err && err != -ENOENT)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800242 return ERR_PTR(err);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700243
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800244 if (inode && dir_alias(inode)) {
245 iput(inode);
246 return ERR_PTR(-EIO);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700247 }
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800248 d_add(entry, inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700249 entry->d_op = &fuse_dentry_operations;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800250 if (!err)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800251 fuse_change_timeout(entry, &outarg);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800252 else
253 fuse_invalidate_entry_cache(entry);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800254 return NULL;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700255}
256
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800257/*
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700258 * Synchronous release for the case when something goes wrong in CREATE_OPEN
259 */
260static void fuse_sync_release(struct fuse_conn *fc, struct fuse_file *ff,
261 u64 nodeid, int flags)
262{
263 struct fuse_req *req;
264
265 req = fuse_release_fill(ff, nodeid, flags, FUSE_RELEASE);
266 req->force = 1;
267 request_send(fc, req);
268 fuse_put_request(fc, req);
269}
270
271/*
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800272 * Atomic create+open operation
273 *
274 * If the filesystem doesn't support this, then fall back to separate
275 * 'mknod' + 'open' requests.
276 */
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800277static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
278 struct nameidata *nd)
279{
280 int err;
281 struct inode *inode;
282 struct fuse_conn *fc = get_fuse_conn(dir);
283 struct fuse_req *req;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700284 struct fuse_req *forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800285 struct fuse_open_in inarg;
286 struct fuse_open_out outopen;
287 struct fuse_entry_out outentry;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800288 struct fuse_file *ff;
289 struct file *file;
290 int flags = nd->intent.open.flags - 1;
291
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800292 if (fc->no_create)
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700293 return -ENOSYS;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800294
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700295 forget_req = fuse_get_req(fc);
296 if (IS_ERR(forget_req))
297 return PTR_ERR(forget_req);
298
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700299 req = fuse_get_req(fc);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700300 err = PTR_ERR(req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700301 if (IS_ERR(req))
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700302 goto out_put_forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800303
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700304 err = -ENOMEM;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800305 ff = fuse_file_alloc();
306 if (!ff)
307 goto out_put_request;
308
309 flags &= ~O_NOCTTY;
310 memset(&inarg, 0, sizeof(inarg));
311 inarg.flags = flags;
312 inarg.mode = mode;
313 req->in.h.opcode = FUSE_CREATE;
314 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800315 req->in.numargs = 2;
316 req->in.args[0].size = sizeof(inarg);
317 req->in.args[0].value = &inarg;
318 req->in.args[1].size = entry->d_name.len + 1;
319 req->in.args[1].value = entry->d_name.name;
320 req->out.numargs = 2;
321 req->out.args[0].size = sizeof(outentry);
322 req->out.args[0].value = &outentry;
323 req->out.args[1].size = sizeof(outopen);
324 req->out.args[1].value = &outopen;
325 request_send(fc, req);
326 err = req->out.h.error;
327 if (err) {
328 if (err == -ENOSYS)
329 fc->no_create = 1;
330 goto out_free_ff;
331 }
332
333 err = -EIO;
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800334 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800335 goto out_free_ff;
336
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700337 fuse_put_request(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800338 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
339 &outentry.attr);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800340 if (!inode) {
341 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
342 ff->fh = outopen.fh;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700343 fuse_sync_release(fc, ff, outentry.nodeid, flags);
344 fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
345 return -ENOMEM;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800346 }
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700347 fuse_put_request(fc, forget_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800348 d_instantiate(entry, inode);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800349 fuse_change_timeout(entry, &outentry);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800350 file = lookup_instantiate_filp(nd, entry, generic_file_open);
351 if (IS_ERR(file)) {
352 ff->fh = outopen.fh;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700353 fuse_sync_release(fc, ff, outentry.nodeid, flags);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800354 return PTR_ERR(file);
355 }
356 fuse_finish_open(inode, file, ff, &outopen);
357 return 0;
358
359 out_free_ff:
360 fuse_file_free(ff);
361 out_put_request:
362 fuse_put_request(fc, req);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700363 out_put_forget_req:
364 fuse_put_request(fc, forget_req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800365 return err;
366}
367
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800368/*
369 * Code shared between mknod, mkdir, symlink and link
370 */
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700371static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
372 struct inode *dir, struct dentry *entry,
373 int mode)
374{
375 struct fuse_entry_out outarg;
376 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700377 int err;
378
379 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700380 req->out.numargs = 1;
381 req->out.args[0].size = sizeof(outarg);
382 req->out.args[0].value = &outarg;
383 request_send(fc, req);
384 err = req->out.h.error;
385 if (err) {
386 fuse_put_request(fc, req);
387 return err;
388 }
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800389 err = -EIO;
390 if (invalid_nodeid(outarg.nodeid))
391 goto out_put_request;
392
393 if ((outarg.attr.mode ^ mode) & S_IFMT)
394 goto out_put_request;
395
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700396 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
397 &outarg.attr);
398 if (!inode) {
399 fuse_send_forget(fc, req, outarg.nodeid, 1);
400 return -ENOMEM;
401 }
402 fuse_put_request(fc, req);
403
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800404 if (dir_alias(inode)) {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700405 iput(inode);
406 return -EIO;
407 }
408
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700409 d_instantiate(entry, inode);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800410 fuse_change_timeout(entry, &outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700411 fuse_invalidate_attr(dir);
412 return 0;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800413
414 out_put_request:
415 fuse_put_request(fc, req);
416 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700417}
418
419static int fuse_mknod(struct inode *dir, struct dentry *entry, int mode,
420 dev_t rdev)
421{
422 struct fuse_mknod_in inarg;
423 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700424 struct fuse_req *req = fuse_get_req(fc);
425 if (IS_ERR(req))
426 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700427
428 memset(&inarg, 0, sizeof(inarg));
429 inarg.mode = mode;
430 inarg.rdev = new_encode_dev(rdev);
431 req->in.h.opcode = FUSE_MKNOD;
432 req->in.numargs = 2;
433 req->in.args[0].size = sizeof(inarg);
434 req->in.args[0].value = &inarg;
435 req->in.args[1].size = entry->d_name.len + 1;
436 req->in.args[1].value = entry->d_name.name;
437 return create_new_entry(fc, req, dir, entry, mode);
438}
439
440static int fuse_create(struct inode *dir, struct dentry *entry, int mode,
441 struct nameidata *nd)
442{
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800443 if (nd && (nd->flags & LOOKUP_CREATE)) {
444 int err = fuse_create_open(dir, entry, mode, nd);
445 if (err != -ENOSYS)
446 return err;
447 /* Fall back on mknod */
448 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700449 return fuse_mknod(dir, entry, mode, 0);
450}
451
452static int fuse_mkdir(struct inode *dir, struct dentry *entry, int mode)
453{
454 struct fuse_mkdir_in inarg;
455 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700456 struct fuse_req *req = fuse_get_req(fc);
457 if (IS_ERR(req))
458 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700459
460 memset(&inarg, 0, sizeof(inarg));
461 inarg.mode = mode;
462 req->in.h.opcode = FUSE_MKDIR;
463 req->in.numargs = 2;
464 req->in.args[0].size = sizeof(inarg);
465 req->in.args[0].value = &inarg;
466 req->in.args[1].size = entry->d_name.len + 1;
467 req->in.args[1].value = entry->d_name.name;
468 return create_new_entry(fc, req, dir, entry, S_IFDIR);
469}
470
471static int fuse_symlink(struct inode *dir, struct dentry *entry,
472 const char *link)
473{
474 struct fuse_conn *fc = get_fuse_conn(dir);
475 unsigned len = strlen(link) + 1;
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700476 struct fuse_req *req = fuse_get_req(fc);
477 if (IS_ERR(req))
478 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700479
480 req->in.h.opcode = FUSE_SYMLINK;
481 req->in.numargs = 2;
482 req->in.args[0].size = entry->d_name.len + 1;
483 req->in.args[0].value = entry->d_name.name;
484 req->in.args[1].size = len;
485 req->in.args[1].value = link;
486 return create_new_entry(fc, req, dir, entry, S_IFLNK);
487}
488
489static int fuse_unlink(struct inode *dir, struct dentry *entry)
490{
491 int err;
492 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700493 struct fuse_req *req = fuse_get_req(fc);
494 if (IS_ERR(req))
495 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700496
497 req->in.h.opcode = FUSE_UNLINK;
498 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700499 req->in.numargs = 1;
500 req->in.args[0].size = entry->d_name.len + 1;
501 req->in.args[0].value = entry->d_name.name;
502 request_send(fc, req);
503 err = req->out.h.error;
504 fuse_put_request(fc, req);
505 if (!err) {
506 struct inode *inode = entry->d_inode;
507
508 /* Set nlink to zero so the inode can be cleared, if
509 the inode does have more links this will be
510 discovered at the next lookup/getattr */
511 inode->i_nlink = 0;
512 fuse_invalidate_attr(inode);
513 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800514 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700515 } else if (err == -EINTR)
516 fuse_invalidate_entry(entry);
517 return err;
518}
519
520static int fuse_rmdir(struct inode *dir, struct dentry *entry)
521{
522 int err;
523 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700524 struct fuse_req *req = fuse_get_req(fc);
525 if (IS_ERR(req))
526 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700527
528 req->in.h.opcode = FUSE_RMDIR;
529 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700530 req->in.numargs = 1;
531 req->in.args[0].size = entry->d_name.len + 1;
532 req->in.args[0].value = entry->d_name.name;
533 request_send(fc, req);
534 err = req->out.h.error;
535 fuse_put_request(fc, req);
536 if (!err) {
537 entry->d_inode->i_nlink = 0;
538 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800539 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700540 } else if (err == -EINTR)
541 fuse_invalidate_entry(entry);
542 return err;
543}
544
545static int fuse_rename(struct inode *olddir, struct dentry *oldent,
546 struct inode *newdir, struct dentry *newent)
547{
548 int err;
549 struct fuse_rename_in inarg;
550 struct fuse_conn *fc = get_fuse_conn(olddir);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700551 struct fuse_req *req = fuse_get_req(fc);
552 if (IS_ERR(req))
553 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700554
555 memset(&inarg, 0, sizeof(inarg));
556 inarg.newdir = get_node_id(newdir);
557 req->in.h.opcode = FUSE_RENAME;
558 req->in.h.nodeid = get_node_id(olddir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700559 req->in.numargs = 3;
560 req->in.args[0].size = sizeof(inarg);
561 req->in.args[0].value = &inarg;
562 req->in.args[1].size = oldent->d_name.len + 1;
563 req->in.args[1].value = oldent->d_name.name;
564 req->in.args[2].size = newent->d_name.len + 1;
565 req->in.args[2].value = newent->d_name.name;
566 request_send(fc, req);
567 err = req->out.h.error;
568 fuse_put_request(fc, req);
569 if (!err) {
570 fuse_invalidate_attr(olddir);
571 if (olddir != newdir)
572 fuse_invalidate_attr(newdir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800573
574 /* newent will end up negative */
575 if (newent->d_inode)
576 fuse_invalidate_entry_cache(newent);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700577 } else if (err == -EINTR) {
578 /* If request was interrupted, DEITY only knows if the
579 rename actually took place. If the invalidation
580 fails (e.g. some process has CWD under the renamed
581 directory), then there can be inconsistency between
582 the dcache and the real filesystem. Tough luck. */
583 fuse_invalidate_entry(oldent);
584 if (newent->d_inode)
585 fuse_invalidate_entry(newent);
586 }
587
588 return err;
589}
590
591static int fuse_link(struct dentry *entry, struct inode *newdir,
592 struct dentry *newent)
593{
594 int err;
595 struct fuse_link_in inarg;
596 struct inode *inode = entry->d_inode;
597 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700598 struct fuse_req *req = fuse_get_req(fc);
599 if (IS_ERR(req))
600 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700601
602 memset(&inarg, 0, sizeof(inarg));
603 inarg.oldnodeid = get_node_id(inode);
604 req->in.h.opcode = FUSE_LINK;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700605 req->in.numargs = 2;
606 req->in.args[0].size = sizeof(inarg);
607 req->in.args[0].value = &inarg;
608 req->in.args[1].size = newent->d_name.len + 1;
609 req->in.args[1].value = newent->d_name.name;
610 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
611 /* Contrary to "normal" filesystems it can happen that link
612 makes two "logical" inodes point to the same "physical"
613 inode. We invalidate the attributes of the old one, so it
614 will reflect changes in the backing inode (link count,
615 etc.)
616 */
617 if (!err || err == -EINTR)
618 fuse_invalidate_attr(inode);
619 return err;
620}
621
Miklos Szeredie5e55582005-09-09 13:10:28 -0700622int fuse_do_getattr(struct inode *inode)
623{
624 int err;
625 struct fuse_attr_out arg;
626 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700627 struct fuse_req *req = fuse_get_req(fc);
628 if (IS_ERR(req))
629 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700630
631 req->in.h.opcode = FUSE_GETATTR;
632 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700633 req->out.numargs = 1;
634 req->out.args[0].size = sizeof(arg);
635 req->out.args[0].value = &arg;
636 request_send(fc, req);
637 err = req->out.h.error;
638 fuse_put_request(fc, req);
639 if (!err) {
640 if ((inode->i_mode ^ arg.attr.mode) & S_IFMT) {
641 make_bad_inode(inode);
642 err = -EIO;
643 } else {
644 struct fuse_inode *fi = get_fuse_inode(inode);
645 fuse_change_attributes(inode, &arg.attr);
646 fi->i_time = time_to_jiffies(arg.attr_valid,
647 arg.attr_valid_nsec);
648 }
649 }
650 return err;
651}
652
Miklos Szeredi87729a52005-09-09 13:10:34 -0700653/*
654 * Calling into a user-controlled filesystem gives the filesystem
655 * daemon ptrace-like capabilities over the requester process. This
656 * means, that the filesystem daemon is able to record the exact
657 * filesystem operations performed, and can also control the behavior
658 * of the requester process in otherwise impossible ways. For example
659 * it can delay the operation for arbitrary length of time allowing
660 * DoS against the requester.
661 *
662 * For this reason only those processes can call into the filesystem,
663 * for which the owner of the mount has ptrace privilege. This
664 * excludes processes started by other users, suid or sgid processes.
665 */
666static int fuse_allow_task(struct fuse_conn *fc, struct task_struct *task)
667{
668 if (fc->flags & FUSE_ALLOW_OTHER)
669 return 1;
670
671 if (task->euid == fc->user_id &&
672 task->suid == fc->user_id &&
673 task->uid == fc->user_id &&
674 task->egid == fc->group_id &&
675 task->sgid == fc->group_id &&
676 task->gid == fc->group_id)
677 return 1;
678
679 return 0;
680}
681
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800682/*
683 * Check whether the inode attributes are still valid
684 *
685 * If the attribute validity timeout has expired, then fetch the fresh
686 * attributes with a 'getattr' request
687 *
688 * I'm not sure why cached attributes are never returned for the root
689 * inode, this is probably being too cautious.
690 */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700691static int fuse_revalidate(struct dentry *entry)
692{
693 struct inode *inode = entry->d_inode;
694 struct fuse_inode *fi = get_fuse_inode(inode);
695 struct fuse_conn *fc = get_fuse_conn(inode);
696
Miklos Szeredi87729a52005-09-09 13:10:34 -0700697 if (!fuse_allow_task(fc, current))
698 return -EACCES;
699 if (get_node_id(inode) != FUSE_ROOT_ID &&
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700700 fi->i_time >= get_jiffies_64())
Miklos Szeredie5e55582005-09-09 13:10:28 -0700701 return 0;
702
703 return fuse_do_getattr(inode);
704}
705
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800706static int fuse_access(struct inode *inode, int mask)
707{
708 struct fuse_conn *fc = get_fuse_conn(inode);
709 struct fuse_req *req;
710 struct fuse_access_in inarg;
711 int err;
712
713 if (fc->no_access)
714 return 0;
715
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700716 req = fuse_get_req(fc);
717 if (IS_ERR(req))
718 return PTR_ERR(req);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800719
720 memset(&inarg, 0, sizeof(inarg));
721 inarg.mask = mask;
722 req->in.h.opcode = FUSE_ACCESS;
723 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800724 req->in.numargs = 1;
725 req->in.args[0].size = sizeof(inarg);
726 req->in.args[0].value = &inarg;
727 request_send(fc, req);
728 err = req->out.h.error;
729 fuse_put_request(fc, req);
730 if (err == -ENOSYS) {
731 fc->no_access = 1;
732 err = 0;
733 }
734 return err;
735}
736
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800737/*
738 * Check permission. The two basic access models of FUSE are:
739 *
740 * 1) Local access checking ('default_permissions' mount option) based
741 * on file mode. This is the plain old disk filesystem permission
742 * modell.
743 *
744 * 2) "Remote" access checking, where server is responsible for
745 * checking permission in each inode operation. An exception to this
746 * is if ->permission() was invoked from sys_access() in which case an
747 * access request is sent. Execute permission is still checked
748 * locally based on file mode.
749 */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700750static int fuse_permission(struct inode *inode, int mask, struct nameidata *nd)
751{
752 struct fuse_conn *fc = get_fuse_conn(inode);
753
Miklos Szeredi87729a52005-09-09 13:10:34 -0700754 if (!fuse_allow_task(fc, current))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700755 return -EACCES;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700756 else if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
757 int err = generic_permission(inode, mask, NULL);
758
759 /* If permission is denied, try to refresh file
760 attributes. This is also needed, because the root
761 node will at first have no permissions */
762 if (err == -EACCES) {
763 err = fuse_do_getattr(inode);
764 if (!err)
765 err = generic_permission(inode, mask, NULL);
766 }
767
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800768 /* Note: the opposite of the above test does not
769 exist. So if permissions are revoked this won't be
770 noticed immediately, only after the attribute
771 timeout has expired */
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700772
773 return err;
774 } else {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700775 int mode = inode->i_mode;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700776 if ((mask & MAY_EXEC) && !S_ISDIR(mode) && !(mode & S_IXUGO))
777 return -EACCES;
Miklos Szeredi31d40d72005-11-07 00:59:50 -0800778
779 if (nd && (nd->flags & LOOKUP_ACCESS))
780 return fuse_access(inode, mask);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700781 return 0;
782 }
783}
784
785static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
786 void *dstbuf, filldir_t filldir)
787{
788 while (nbytes >= FUSE_NAME_OFFSET) {
789 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
790 size_t reclen = FUSE_DIRENT_SIZE(dirent);
791 int over;
792 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
793 return -EIO;
794 if (reclen > nbytes)
795 break;
796
797 over = filldir(dstbuf, dirent->name, dirent->namelen,
798 file->f_pos, dirent->ino, dirent->type);
799 if (over)
800 break;
801
802 buf += reclen;
803 nbytes -= reclen;
804 file->f_pos = dirent->off;
805 }
806
807 return 0;
808}
809
Miklos Szeredie5e55582005-09-09 13:10:28 -0700810static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
811{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700812 int err;
813 size_t nbytes;
814 struct page *page;
815 struct inode *inode = file->f_dentry->d_inode;
816 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -0800817 struct fuse_req *req;
818
819 if (is_bad_inode(inode))
820 return -EIO;
821
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700822 req = fuse_get_req(fc);
823 if (IS_ERR(req))
824 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700825
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700826 page = alloc_page(GFP_KERNEL);
827 if (!page) {
828 fuse_put_request(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700829 return -ENOMEM;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700830 }
831 req->num_pages = 1;
832 req->pages[0] = page;
Miklos Szeredi361b1eb2006-01-16 22:14:45 -0800833 fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
834 request_send(fc, req);
835 nbytes = req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700836 err = req->out.h.error;
837 fuse_put_request(fc, req);
838 if (!err)
839 err = parse_dirfile(page_address(page), nbytes, file, dstbuf,
840 filldir);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700841
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700842 __free_page(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700843 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700844 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700845}
846
847static char *read_link(struct dentry *dentry)
848{
849 struct inode *inode = dentry->d_inode;
850 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700851 struct fuse_req *req = fuse_get_req(fc);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700852 char *link;
853
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700854 if (IS_ERR(req))
855 return ERR_PTR(PTR_ERR(req));
Miklos Szeredie5e55582005-09-09 13:10:28 -0700856
857 link = (char *) __get_free_page(GFP_KERNEL);
858 if (!link) {
859 link = ERR_PTR(-ENOMEM);
860 goto out;
861 }
862 req->in.h.opcode = FUSE_READLINK;
863 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700864 req->out.argvar = 1;
865 req->out.numargs = 1;
866 req->out.args[0].size = PAGE_SIZE - 1;
867 req->out.args[0].value = link;
868 request_send(fc, req);
869 if (req->out.h.error) {
870 free_page((unsigned long) link);
871 link = ERR_PTR(req->out.h.error);
872 } else
873 link[req->out.args[0].size] = '\0';
874 out:
875 fuse_put_request(fc, req);
Miklos Szeredib36c31b2005-09-09 13:10:38 -0700876 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredie5e55582005-09-09 13:10:28 -0700877 return link;
878}
879
880static void free_link(char *link)
881{
882 if (!IS_ERR(link))
883 free_page((unsigned long) link);
884}
885
886static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
887{
888 nd_set_link(nd, read_link(dentry));
889 return NULL;
890}
891
892static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
893{
894 free_link(nd_get_link(nd));
895}
896
897static int fuse_dir_open(struct inode *inode, struct file *file)
898{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700899 return fuse_open_common(inode, file, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700900}
901
902static int fuse_dir_release(struct inode *inode, struct file *file)
903{
Miklos Szeredi04730fe2005-09-09 13:10:36 -0700904 return fuse_release_common(inode, file, 1);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700905}
906
Miklos Szeredi82547982005-09-09 13:10:38 -0700907static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
908{
909 /* nfsd can call this with no file */
910 return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
911}
912
Miklos Szeredibefc6492005-11-07 00:59:52 -0800913static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700914{
915 unsigned ivalid = iattr->ia_valid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700916
917 if (ivalid & ATTR_MODE)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800918 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700919 if (ivalid & ATTR_UID)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800920 arg->valid |= FATTR_UID, arg->uid = iattr->ia_uid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700921 if (ivalid & ATTR_GID)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800922 arg->valid |= FATTR_GID, arg->gid = iattr->ia_gid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700923 if (ivalid & ATTR_SIZE)
Miklos Szeredibefc6492005-11-07 00:59:52 -0800924 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700925 /* You can only _set_ these together (they may change by themselves) */
926 if ((ivalid & (ATTR_ATIME | ATTR_MTIME)) == (ATTR_ATIME | ATTR_MTIME)) {
Miklos Szeredibefc6492005-11-07 00:59:52 -0800927 arg->valid |= FATTR_ATIME | FATTR_MTIME;
928 arg->atime = iattr->ia_atime.tv_sec;
929 arg->mtime = iattr->ia_mtime.tv_sec;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700930 }
Miklos Szeredibefc6492005-11-07 00:59:52 -0800931 if (ivalid & ATTR_FILE) {
932 struct fuse_file *ff = iattr->ia_file->private_data;
933 arg->valid |= FATTR_FH;
934 arg->fh = ff->fh;
935 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700936}
937
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800938/*
939 * Set attributes, and at the same time refresh them.
940 *
941 * Truncation is slightly complicated, because the 'truncate' request
942 * may fail, in which case we don't want to touch the mapping.
943 * vmtruncate() doesn't allow for this case. So do the rlimit
944 * checking by hand and call vmtruncate() only after the file has
945 * actually been truncated.
946 */
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700947static int fuse_setattr(struct dentry *entry, struct iattr *attr)
948{
949 struct inode *inode = entry->d_inode;
950 struct fuse_conn *fc = get_fuse_conn(inode);
951 struct fuse_inode *fi = get_fuse_inode(inode);
952 struct fuse_req *req;
953 struct fuse_setattr_in inarg;
954 struct fuse_attr_out outarg;
955 int err;
956 int is_truncate = 0;
957
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -0700958 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
959 err = inode_change_ok(inode, attr);
960 if (err)
961 return err;
962 }
963
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700964 if (attr->ia_valid & ATTR_SIZE) {
965 unsigned long limit;
966 is_truncate = 1;
967 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
968 if (limit != RLIM_INFINITY && attr->ia_size > (loff_t) limit) {
969 send_sig(SIGXFSZ, current, 0);
970 return -EFBIG;
971 }
972 }
973
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700974 req = fuse_get_req(fc);
975 if (IS_ERR(req))
976 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700977
978 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredibefc6492005-11-07 00:59:52 -0800979 iattr_to_fattr(attr, &inarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700980 req->in.h.opcode = FUSE_SETATTR;
981 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700982 req->in.numargs = 1;
983 req->in.args[0].size = sizeof(inarg);
984 req->in.args[0].value = &inarg;
985 req->out.numargs = 1;
986 req->out.args[0].size = sizeof(outarg);
987 req->out.args[0].value = &outarg;
988 request_send(fc, req);
989 err = req->out.h.error;
990 fuse_put_request(fc, req);
991 if (!err) {
992 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
993 make_bad_inode(inode);
994 err = -EIO;
995 } else {
996 if (is_truncate) {
997 loff_t origsize = i_size_read(inode);
998 i_size_write(inode, outarg.attr.size);
999 if (origsize > outarg.attr.size)
1000 vmtruncate(inode, outarg.attr.size);
1001 }
1002 fuse_change_attributes(inode, &outarg.attr);
1003 fi->i_time = time_to_jiffies(outarg.attr_valid,
1004 outarg.attr_valid_nsec);
1005 }
1006 } else if (err == -EINTR)
1007 fuse_invalidate_attr(inode);
1008
1009 return err;
1010}
1011
Miklos Szeredie5e55582005-09-09 13:10:28 -07001012static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1013 struct kstat *stat)
1014{
1015 struct inode *inode = entry->d_inode;
1016 int err = fuse_revalidate(entry);
1017 if (!err)
1018 generic_fillattr(inode, stat);
1019
1020 return err;
1021}
1022
Miklos Szeredi92a87802005-09-09 13:10:31 -07001023static int fuse_setxattr(struct dentry *entry, const char *name,
1024 const void *value, size_t size, int flags)
1025{
1026 struct inode *inode = entry->d_inode;
1027 struct fuse_conn *fc = get_fuse_conn(inode);
1028 struct fuse_req *req;
1029 struct fuse_setxattr_in inarg;
1030 int err;
1031
Miklos Szeredi92a87802005-09-09 13:10:31 -07001032 if (fc->no_setxattr)
1033 return -EOPNOTSUPP;
1034
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001035 req = fuse_get_req(fc);
1036 if (IS_ERR(req))
1037 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001038
1039 memset(&inarg, 0, sizeof(inarg));
1040 inarg.size = size;
1041 inarg.flags = flags;
1042 req->in.h.opcode = FUSE_SETXATTR;
1043 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001044 req->in.numargs = 3;
1045 req->in.args[0].size = sizeof(inarg);
1046 req->in.args[0].value = &inarg;
1047 req->in.args[1].size = strlen(name) + 1;
1048 req->in.args[1].value = name;
1049 req->in.args[2].size = size;
1050 req->in.args[2].value = value;
1051 request_send(fc, req);
1052 err = req->out.h.error;
1053 fuse_put_request(fc, req);
1054 if (err == -ENOSYS) {
1055 fc->no_setxattr = 1;
1056 err = -EOPNOTSUPP;
1057 }
1058 return err;
1059}
1060
1061static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1062 void *value, size_t size)
1063{
1064 struct inode *inode = entry->d_inode;
1065 struct fuse_conn *fc = get_fuse_conn(inode);
1066 struct fuse_req *req;
1067 struct fuse_getxattr_in inarg;
1068 struct fuse_getxattr_out outarg;
1069 ssize_t ret;
1070
1071 if (fc->no_getxattr)
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 req->in.h.opcode = FUSE_GETXATTR;
1081 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001082 req->in.numargs = 2;
1083 req->in.args[0].size = sizeof(inarg);
1084 req->in.args[0].value = &inarg;
1085 req->in.args[1].size = strlen(name) + 1;
1086 req->in.args[1].value = name;
1087 /* This is really two different operations rolled into one */
1088 req->out.numargs = 1;
1089 if (size) {
1090 req->out.argvar = 1;
1091 req->out.args[0].size = size;
1092 req->out.args[0].value = value;
1093 } else {
1094 req->out.args[0].size = sizeof(outarg);
1095 req->out.args[0].value = &outarg;
1096 }
1097 request_send(fc, req);
1098 ret = req->out.h.error;
1099 if (!ret)
1100 ret = size ? req->out.args[0].size : outarg.size;
1101 else {
1102 if (ret == -ENOSYS) {
1103 fc->no_getxattr = 1;
1104 ret = -EOPNOTSUPP;
1105 }
1106 }
1107 fuse_put_request(fc, req);
1108 return ret;
1109}
1110
1111static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1112{
1113 struct inode *inode = entry->d_inode;
1114 struct fuse_conn *fc = get_fuse_conn(inode);
1115 struct fuse_req *req;
1116 struct fuse_getxattr_in inarg;
1117 struct fuse_getxattr_out outarg;
1118 ssize_t ret;
1119
1120 if (fc->no_listxattr)
1121 return -EOPNOTSUPP;
1122
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001123 req = fuse_get_req(fc);
1124 if (IS_ERR(req))
1125 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001126
1127 memset(&inarg, 0, sizeof(inarg));
1128 inarg.size = size;
1129 req->in.h.opcode = FUSE_LISTXATTR;
1130 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001131 req->in.numargs = 1;
1132 req->in.args[0].size = sizeof(inarg);
1133 req->in.args[0].value = &inarg;
1134 /* This is really two different operations rolled into one */
1135 req->out.numargs = 1;
1136 if (size) {
1137 req->out.argvar = 1;
1138 req->out.args[0].size = size;
1139 req->out.args[0].value = list;
1140 } else {
1141 req->out.args[0].size = sizeof(outarg);
1142 req->out.args[0].value = &outarg;
1143 }
1144 request_send(fc, req);
1145 ret = req->out.h.error;
1146 if (!ret)
1147 ret = size ? req->out.args[0].size : outarg.size;
1148 else {
1149 if (ret == -ENOSYS) {
1150 fc->no_listxattr = 1;
1151 ret = -EOPNOTSUPP;
1152 }
1153 }
1154 fuse_put_request(fc, req);
1155 return ret;
1156}
1157
1158static int fuse_removexattr(struct dentry *entry, const char *name)
1159{
1160 struct inode *inode = entry->d_inode;
1161 struct fuse_conn *fc = get_fuse_conn(inode);
1162 struct fuse_req *req;
1163 int err;
1164
1165 if (fc->no_removexattr)
1166 return -EOPNOTSUPP;
1167
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001168 req = fuse_get_req(fc);
1169 if (IS_ERR(req))
1170 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001171
1172 req->in.h.opcode = FUSE_REMOVEXATTR;
1173 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001174 req->in.numargs = 1;
1175 req->in.args[0].size = strlen(name) + 1;
1176 req->in.args[0].value = name;
1177 request_send(fc, req);
1178 err = req->out.h.error;
1179 fuse_put_request(fc, req);
1180 if (err == -ENOSYS) {
1181 fc->no_removexattr = 1;
1182 err = -EOPNOTSUPP;
1183 }
1184 return err;
1185}
1186
Miklos Szeredie5e55582005-09-09 13:10:28 -07001187static struct inode_operations fuse_dir_inode_operations = {
1188 .lookup = fuse_lookup,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001189 .mkdir = fuse_mkdir,
1190 .symlink = fuse_symlink,
1191 .unlink = fuse_unlink,
1192 .rmdir = fuse_rmdir,
1193 .rename = fuse_rename,
1194 .link = fuse_link,
1195 .setattr = fuse_setattr,
1196 .create = fuse_create,
1197 .mknod = fuse_mknod,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001198 .permission = fuse_permission,
1199 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001200 .setxattr = fuse_setxattr,
1201 .getxattr = fuse_getxattr,
1202 .listxattr = fuse_listxattr,
1203 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001204};
1205
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001206static const struct file_operations fuse_dir_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001207 .llseek = generic_file_llseek,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001208 .read = generic_read_dir,
1209 .readdir = fuse_readdir,
1210 .open = fuse_dir_open,
1211 .release = fuse_dir_release,
Miklos Szeredi82547982005-09-09 13:10:38 -07001212 .fsync = fuse_dir_fsync,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001213};
1214
1215static struct inode_operations fuse_common_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001216 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001217 .permission = fuse_permission,
1218 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001219 .setxattr = fuse_setxattr,
1220 .getxattr = fuse_getxattr,
1221 .listxattr = fuse_listxattr,
1222 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001223};
1224
1225static struct inode_operations fuse_symlink_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001226 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001227 .follow_link = fuse_follow_link,
1228 .put_link = fuse_put_link,
1229 .readlink = generic_readlink,
1230 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001231 .setxattr = fuse_setxattr,
1232 .getxattr = fuse_getxattr,
1233 .listxattr = fuse_listxattr,
1234 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001235};
1236
1237void fuse_init_common(struct inode *inode)
1238{
1239 inode->i_op = &fuse_common_inode_operations;
1240}
1241
1242void fuse_init_dir(struct inode *inode)
1243{
1244 inode->i_op = &fuse_dir_inode_operations;
1245 inode->i_fop = &fuse_dir_operations;
1246}
1247
1248void fuse_init_symlink(struct inode *inode)
1249{
1250 inode->i_op = &fuse_symlink_inode_operations;
1251}