fs: icache RCU free inodes
[deliverable/linux.git] / drivers / staging / smbfs / dir.c
CommitLineData
1da177e4
LT
1/*
2 * dir.c
3 *
4 * Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
5 * Copyright (C) 1997 by Volker Lendecke
6 *
7 * Please add a note about your changes to smbfs in the ChangeLog file.
8 */
9
10#include <linux/time.h>
11#include <linux/errno.h>
12#include <linux/kernel.h>
13#include <linux/smp_lock.h>
14#include <linux/ctype.h>
15#include <linux/net.h>
e8edc6e0 16#include <linux/sched.h>
1da177e4 17
2116b7a4
AB
18#include "smb_fs.h"
19#include "smb_mount.h"
20#include "smbno.h"
1da177e4
LT
21
22#include "smb_debug.h"
23#include "proto.h"
24
25static int smb_readdir(struct file *, void *, filldir_t);
26static int smb_dir_open(struct inode *, struct file *);
27
28static struct dentry *smb_lookup(struct inode *, struct dentry *, struct nameidata *);
29static int smb_create(struct inode *, struct dentry *, int, struct nameidata *);
30static int smb_mkdir(struct inode *, struct dentry *, int);
31static int smb_rmdir(struct inode *, struct dentry *);
32static int smb_unlink(struct inode *, struct dentry *);
33static int smb_rename(struct inode *, struct dentry *,
34 struct inode *, struct dentry *);
35static int smb_make_node(struct inode *,struct dentry *,int,dev_t);
36static int smb_link(struct dentry *, struct inode *, struct dentry *);
37
4b6f5d20 38const struct file_operations smb_dir_operations =
1da177e4 39{
ca572727 40 .llseek = generic_file_llseek,
1da177e4
LT
41 .read = generic_read_dir,
42 .readdir = smb_readdir,
ce8273a5 43 .unlocked_ioctl = smb_ioctl,
1da177e4
LT
44 .open = smb_dir_open,
45};
46
c5ef1c42 47const struct inode_operations smb_dir_inode_operations =
1da177e4
LT
48{
49 .create = smb_create,
50 .lookup = smb_lookup,
51 .unlink = smb_unlink,
52 .mkdir = smb_mkdir,
53 .rmdir = smb_rmdir,
54 .rename = smb_rename,
55 .getattr = smb_getattr,
56 .setattr = smb_notify_change,
57};
58
c5ef1c42 59const struct inode_operations smb_dir_inode_operations_unix =
1da177e4
LT
60{
61 .create = smb_create,
62 .lookup = smb_lookup,
63 .unlink = smb_unlink,
64 .mkdir = smb_mkdir,
65 .rmdir = smb_rmdir,
66 .rename = smb_rename,
67 .getattr = smb_getattr,
68 .setattr = smb_notify_change,
69 .symlink = smb_symlink,
70 .mknod = smb_make_node,
71 .link = smb_link,
72};
73
74/*
75 * Read a directory, using filldir to fill the dirent memory.
76 * smb_proc_readdir does the actual reading from the smb server.
77 *
78 * The cache code is almost directly taken from ncpfs
79 */
80static int
81smb_readdir(struct file *filp, void *dirent, filldir_t filldir)
82{
17b75e69 83 struct dentry *dentry = filp->f_path.dentry;
1da177e4
LT
84 struct inode *dir = dentry->d_inode;
85 struct smb_sb_info *server = server_from_dentry(dentry);
86 union smb_dir_cache *cache = NULL;
87 struct smb_cache_control ctl;
88 struct page *page = NULL;
89 int result;
90
91 ctl.page = NULL;
92 ctl.cache = NULL;
93
94 VERBOSE("reading %s/%s, f_pos=%d\n",
95 DENTRY_PATH(dentry), (int) filp->f_pos);
96
97 result = 0;
98
99 lock_kernel();
100
101 switch ((unsigned int) filp->f_pos) {
102 case 0:
103 if (filldir(dirent, ".", 1, 0, dir->i_ino, DT_DIR) < 0)
104 goto out;
105 filp->f_pos = 1;
106 /* fallthrough */
107 case 1:
108 if (filldir(dirent, "..", 2, 1, parent_ino(dentry), DT_DIR) < 0)
109 goto out;
110 filp->f_pos = 2;
111 }
112
113 /*
114 * Make sure our inode is up-to-date.
115 */
116 result = smb_revalidate_inode(dentry);
117 if (result)
118 goto out;
119
120
121 page = grab_cache_page(&dir->i_data, 0);
122 if (!page)
123 goto read_really;
124
125 ctl.cache = cache = kmap(page);
126 ctl.head = cache->head;
127
128 if (!PageUptodate(page) || !ctl.head.eof) {
129 VERBOSE("%s/%s, page uptodate=%d, eof=%d\n",
130 DENTRY_PATH(dentry), PageUptodate(page),ctl.head.eof);
131 goto init_cache;
132 }
133
134 if (filp->f_pos == 2) {
135 if (jiffies - ctl.head.time >= SMB_MAX_AGE(server))
136 goto init_cache;
137
138 /*
139 * N.B. ncpfs checks mtime of dentry too here, we don't.
140 * 1. common smb servers do not update mtime on dir changes
141 * 2. it requires an extra smb request
142 * (revalidate has the same timeout as ctl.head.time)
143 *
144 * Instead smbfs invalidates its own cache on local changes
145 * and remote changes are not seen until timeout.
146 */
147 }
148
149 if (filp->f_pos > ctl.head.end)
150 goto finished;
151
152 ctl.fpos = filp->f_pos + (SMB_DIRCACHE_START - 2);
153 ctl.ofs = ctl.fpos / SMB_DIRCACHE_SIZE;
154 ctl.idx = ctl.fpos % SMB_DIRCACHE_SIZE;
155
156 for (;;) {
157 if (ctl.ofs != 0) {
158 ctl.page = find_lock_page(&dir->i_data, ctl.ofs);
159 if (!ctl.page)
160 goto invalid_cache;
161 ctl.cache = kmap(ctl.page);
162 if (!PageUptodate(ctl.page))
163 goto invalid_cache;
164 }
165 while (ctl.idx < SMB_DIRCACHE_SIZE) {
166 struct dentry *dent;
167 int res;
168
169 dent = smb_dget_fpos(ctl.cache->dentry[ctl.idx],
170 dentry, filp->f_pos);
171 if (!dent)
172 goto invalid_cache;
173
174 res = filldir(dirent, dent->d_name.name,
175 dent->d_name.len, filp->f_pos,
176 dent->d_inode->i_ino, DT_UNKNOWN);
177 dput(dent);
178 if (res)
179 goto finished;
180 filp->f_pos += 1;
181 ctl.idx += 1;
182 if (filp->f_pos > ctl.head.end)
183 goto finished;
184 }
185 if (ctl.page) {
186 kunmap(ctl.page);
187 SetPageUptodate(ctl.page);
188 unlock_page(ctl.page);
189 page_cache_release(ctl.page);
190 ctl.page = NULL;
191 }
192 ctl.idx = 0;
193 ctl.ofs += 1;
194 }
195invalid_cache:
196 if (ctl.page) {
197 kunmap(ctl.page);
198 unlock_page(ctl.page);
199 page_cache_release(ctl.page);
200 ctl.page = NULL;
201 }
202 ctl.cache = cache;
203init_cache:
204 smb_invalidate_dircache_entries(dentry);
205 ctl.head.time = jiffies;
206 ctl.head.eof = 0;
207 ctl.fpos = 2;
208 ctl.ofs = 0;
209 ctl.idx = SMB_DIRCACHE_START;
210 ctl.filled = 0;
211 ctl.valid = 1;
212read_really:
213 result = server->ops->readdir(filp, dirent, filldir, &ctl);
caf73608
AM
214 if (result == -ERESTARTSYS && page)
215 ClearPageUptodate(page);
1da177e4
LT
216 if (ctl.idx == -1)
217 goto invalid_cache; /* retry */
218 ctl.head.end = ctl.fpos - 1;
219 ctl.head.eof = ctl.valid;
220finished:
221 if (page) {
222 cache->head = ctl.head;
223 kunmap(page);
caf73608
AM
224 if (result != -ERESTARTSYS)
225 SetPageUptodate(page);
1da177e4
LT
226 unlock_page(page);
227 page_cache_release(page);
228 }
229 if (ctl.page) {
230 kunmap(ctl.page);
231 SetPageUptodate(ctl.page);
232 unlock_page(ctl.page);
233 page_cache_release(ctl.page);
234 }
235out:
236 unlock_kernel();
237 return result;
238}
239
240static int
241smb_dir_open(struct inode *dir, struct file *file)
242{
17b75e69 243 struct dentry *dentry = file->f_path.dentry;
1da177e4
LT
244 struct smb_sb_info *server;
245 int error = 0;
246
247 VERBOSE("(%s/%s)\n", dentry->d_parent->d_name.name,
17b75e69 248 file->f_path.dentry->d_name.name);
1da177e4
LT
249
250 /*
251 * Directory timestamps in the core protocol aren't updated
252 * when a file is added, so we give them a very short TTL.
253 */
254 lock_kernel();
255 server = server_from_dentry(dentry);
256 if (server->opt.protocol < SMB_PROTOCOL_LANMAN2) {
257 unsigned long age = jiffies - SMB_I(dir)->oldmtime;
258 if (age > 2*HZ)
259 smb_invalid_dir_cache(dir);
260 }
261
262 /*
263 * Note: in order to allow the smbmount process to open the
264 * mount point, we only revalidate if the connection is valid or
265 * if the process is trying to access something other than the root.
266 */
267 if (server->state == CONN_VALID || !IS_ROOT(dentry))
268 error = smb_revalidate_inode(dentry);
269 unlock_kernel();
270 return error;
271}
272
273/*
274 * Dentry operations routines
275 */
276static int smb_lookup_validate(struct dentry *, struct nameidata *);
b1e6a015
NP
277static int smb_hash_dentry(const struct dentry *, const struct inode *,
278 struct qstr *);
621e155a
NP
279static int smb_compare_dentry(const struct dentry *,
280 const struct inode *,
281 const struct dentry *, const struct inode *,
282 unsigned int, const char *, const struct qstr *);
fe15ce44 283static int smb_delete_dentry(const struct dentry *);
1da177e4 284
e16404ed 285static const struct dentry_operations smbfs_dentry_operations =
1da177e4
LT
286{
287 .d_revalidate = smb_lookup_validate,
288 .d_hash = smb_hash_dentry,
289 .d_compare = smb_compare_dentry,
290 .d_delete = smb_delete_dentry,
291};
292
e16404ed 293static const struct dentry_operations smbfs_dentry_operations_case =
1da177e4
LT
294{
295 .d_revalidate = smb_lookup_validate,
296 .d_delete = smb_delete_dentry,
297};
298
299
300/*
301 * This is the callback when the dcache has a lookup hit.
302 */
303static int
304smb_lookup_validate(struct dentry * dentry, struct nameidata *nd)
305{
306 struct smb_sb_info *server = server_from_dentry(dentry);
307 struct inode * inode = dentry->d_inode;
308 unsigned long age = jiffies - dentry->d_time;
309 int valid;
310
311 /*
312 * The default validation is based on dentry age:
313 * we believe in dentries for a few seconds. (But each
314 * successful server lookup renews the timestamp.)
315 */
316 valid = (age <= SMB_MAX_AGE(server));
317#ifdef SMBFS_DEBUG_VERBOSE
318 if (!valid)
319 VERBOSE("%s/%s not valid, age=%lu\n",
320 DENTRY_PATH(dentry), age);
321#endif
322
323 if (inode) {
324 lock_kernel();
325 if (is_bad_inode(inode)) {
326 PARANOIA("%s/%s has dud inode\n", DENTRY_PATH(dentry));
327 valid = 0;
328 } else if (!valid)
329 valid = (smb_revalidate_inode(dentry) == 0);
330 unlock_kernel();
331 } else {
332 /*
333 * What should we do for negative dentries?
334 */
335 }
336 return valid;
337}
338
339static int
b1e6a015
NP
340smb_hash_dentry(const struct dentry *dir, const struct inode *inode,
341 struct qstr *this)
1da177e4
LT
342{
343 unsigned long hash;
344 int i;
345
346 hash = init_name_hash();
347 for (i=0; i < this->len ; i++)
348 hash = partial_name_hash(tolower(this->name[i]), hash);
349 this->hash = end_name_hash(hash);
350
351 return 0;
352}
353
354static int
621e155a
NP
355smb_compare_dentry(const struct dentry *parent,
356 const struct inode *pinode,
357 const struct dentry *dentry, const struct inode *inode,
358 unsigned int len, const char *str, const struct qstr *name)
1da177e4
LT
359{
360 int i, result = 1;
361
621e155a 362 if (len != name->len)
1da177e4 363 goto out;
621e155a
NP
364 for (i=0; i < len; i++) {
365 if (tolower(str[i]) != tolower(name->name[i]))
1da177e4
LT
366 goto out;
367 }
368 result = 0;
369out:
370 return result;
371}
372
373/*
374 * This is the callback from dput() when d_count is going to 0.
375 * We use this to unhash dentries with bad inodes.
376 */
377static int
fe15ce44 378smb_delete_dentry(const struct dentry *dentry)
1da177e4
LT
379{
380 if (dentry->d_inode) {
381 if (is_bad_inode(dentry->d_inode)) {
382 PARANOIA("bad inode, unhashing %s/%s\n",
383 DENTRY_PATH(dentry));
384 return 1;
385 }
386 } else {
387 /* N.B. Unhash negative dentries? */
388 }
389 return 0;
390}
391
392/*
393 * Initialize a new dentry
394 */
395void
396smb_new_dentry(struct dentry *dentry)
397{
398 struct smb_sb_info *server = server_from_dentry(dentry);
399
400 if (server->mnt->flags & SMB_MOUNT_CASE)
401 dentry->d_op = &smbfs_dentry_operations_case;
402 else
403 dentry->d_op = &smbfs_dentry_operations;
404 dentry->d_time = jiffies;
405}
406
407
408/*
409 * Whenever a lookup succeeds, we know the parent directories
410 * are all valid, so we want to update the dentry timestamps.
411 * N.B. Move this to dcache?
412 */
413void
414smb_renew_times(struct dentry * dentry)
415{
416 dget(dentry);
be9eee2e 417 dentry->d_time = jiffies;
1da177e4 418
be9eee2e
CH
419 while (!IS_ROOT(dentry)) {
420 struct dentry *parent = dget_parent(dentry);
1da177e4
LT
421 dput(dentry);
422 dentry = parent;
be9eee2e
CH
423
424 dentry->d_time = jiffies;
1da177e4 425 }
1da177e4
LT
426 dput(dentry);
427}
428
429static struct dentry *
430smb_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
431{
432 struct smb_fattr finfo;
433 struct inode *inode;
434 int error;
435 struct smb_sb_info *server;
436
437 error = -ENAMETOOLONG;
438 if (dentry->d_name.len > SMB_MAXNAMELEN)
439 goto out;
440
3b7c8108
OK
441 /* Do not allow lookup of names with backslashes in */
442 error = -EINVAL;
443 if (memchr(dentry->d_name.name, '\\', dentry->d_name.len))
444 goto out;
445
1da177e4
LT
446 lock_kernel();
447 error = smb_proc_getattr(dentry, &finfo);
448#ifdef SMBFS_PARANOIA
449 if (error && error != -ENOENT)
450 PARANOIA("find %s/%s failed, error=%d\n",
451 DENTRY_PATH(dentry), error);
452#endif
453
454 inode = NULL;
455 if (error == -ENOENT)
456 goto add_entry;
457 if (!error) {
458 error = -EACCES;
459 finfo.f_ino = iunique(dentry->d_sb, 2);
460 inode = smb_iget(dir->i_sb, &finfo);
461 if (inode) {
462 add_entry:
463 server = server_from_dentry(dentry);
464 if (server->mnt->flags & SMB_MOUNT_CASE)
465 dentry->d_op = &smbfs_dentry_operations_case;
466 else
467 dentry->d_op = &smbfs_dentry_operations;
468
469 d_add(dentry, inode);
470 smb_renew_times(dentry);
471 error = 0;
472 }
473 }
474 unlock_kernel();
475out:
476 return ERR_PTR(error);
477}
478
479/*
480 * This code is common to all routines creating a new inode.
481 */
482static int
483smb_instantiate(struct dentry *dentry, __u16 fileid, int have_id)
484{
485 struct smb_sb_info *server = server_from_dentry(dentry);
486 struct inode *inode;
487 int error;
488 struct smb_fattr fattr;
489
490 VERBOSE("file %s/%s, fileid=%u\n", DENTRY_PATH(dentry), fileid);
491
492 error = smb_proc_getattr(dentry, &fattr);
493 if (error)
494 goto out_close;
495
496 smb_renew_times(dentry);
497 fattr.f_ino = iunique(dentry->d_sb, 2);
498 inode = smb_iget(dentry->d_sb, &fattr);
499 if (!inode)
500 goto out_no_inode;
501
502 if (have_id) {
503 struct smb_inode_info *ei = SMB_I(inode);
504 ei->fileid = fileid;
505 ei->access = SMB_O_RDWR;
506 ei->open = server->generation;
507 }
508 d_instantiate(dentry, inode);
509out:
510 return error;
511
512out_no_inode:
513 error = -EACCES;
514out_close:
515 if (have_id) {
516 PARANOIA("%s/%s failed, error=%d, closing %u\n",
517 DENTRY_PATH(dentry), error, fileid);
518 smb_close_fileid(dentry, fileid);
519 }
520 goto out;
521}
522
523/* N.B. How should the mode argument be used? */
524static int
525smb_create(struct inode *dir, struct dentry *dentry, int mode,
526 struct nameidata *nd)
527{
528 struct smb_sb_info *server = server_from_dentry(dentry);
529 __u16 fileid;
530 int error;
531 struct iattr attr;
532
533 VERBOSE("creating %s/%s, mode=%d\n", DENTRY_PATH(dentry), mode);
534
535 lock_kernel();
536 smb_invalid_dir_cache(dir);
537 error = smb_proc_create(dentry, 0, get_seconds(), &fileid);
538 if (!error) {
539 if (server->opt.capabilities & SMB_CAP_UNIX) {
540 /* Set attributes for new file */
541 attr.ia_valid = ATTR_MODE;
542 attr.ia_mode = mode;
543 error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
544 }
545 error = smb_instantiate(dentry, fileid, 1);
546 } else {
547 PARANOIA("%s/%s failed, error=%d\n",
548 DENTRY_PATH(dentry), error);
549 }
550 unlock_kernel();
551 return error;
552}
553
554/* N.B. How should the mode argument be used? */
555static int
556smb_mkdir(struct inode *dir, struct dentry *dentry, int mode)
557{
558 struct smb_sb_info *server = server_from_dentry(dentry);
559 int error;
560 struct iattr attr;
561
562 lock_kernel();
563 smb_invalid_dir_cache(dir);
564 error = smb_proc_mkdir(dentry);
565 if (!error) {
566 if (server->opt.capabilities & SMB_CAP_UNIX) {
567 /* Set attributes for new directory */
568 attr.ia_valid = ATTR_MODE;
569 attr.ia_mode = mode;
570 error = smb_proc_setattr_unix(dentry, &attr, 0, 0);
571 }
572 error = smb_instantiate(dentry, 0, 0);
573 }
574 unlock_kernel();
575 return error;
576}
577
578static int
579smb_rmdir(struct inode *dir, struct dentry *dentry)
580{
581 struct inode *inode = dentry->d_inode;
582 int error;
583
584 /*
585 * Close the directory if it's open.
586 */
587 lock_kernel();
588 smb_close(inode);
589
590 /*
591 * Check that nobody else is using the directory..
592 */
593 error = -EBUSY;
594 if (!d_unhashed(dentry))
595 goto out;
596
597 smb_invalid_dir_cache(dir);
598 error = smb_proc_rmdir(dentry);
599
600out:
601 unlock_kernel();
602 return error;
603}
604
605static int
606smb_unlink(struct inode *dir, struct dentry *dentry)
607{
608 int error;
609
610 /*
611 * Close the file if it's open.
612 */
613 lock_kernel();
614 smb_close(dentry->d_inode);
615
616 smb_invalid_dir_cache(dir);
617 error = smb_proc_unlink(dentry);
618 if (!error)
619 smb_renew_times(dentry);
620 unlock_kernel();
621 return error;
622}
623
624static int
625smb_rename(struct inode *old_dir, struct dentry *old_dentry,
626 struct inode *new_dir, struct dentry *new_dentry)
627{
628 int error;
629
630 /*
631 * Close any open files, and check whether to delete the
632 * target before attempting the rename.
633 */
634 lock_kernel();
635 if (old_dentry->d_inode)
636 smb_close(old_dentry->d_inode);
637 if (new_dentry->d_inode) {
638 smb_close(new_dentry->d_inode);
639 error = smb_proc_unlink(new_dentry);
640 if (error) {
641 VERBOSE("unlink %s/%s, error=%d\n",
642 DENTRY_PATH(new_dentry), error);
643 goto out;
644 }
645 /* FIXME */
646 d_delete(new_dentry);
647 }
648
649 smb_invalid_dir_cache(old_dir);
650 smb_invalid_dir_cache(new_dir);
651 error = smb_proc_mv(old_dentry, new_dentry);
652 if (!error) {
653 smb_renew_times(old_dentry);
654 smb_renew_times(new_dentry);
655 }
656out:
657 unlock_kernel();
658 return error;
659}
660
661/*
662 * FIXME: samba servers won't let you create device nodes unless uid/gid
663 * matches the connection credentials (and we don't know which those are ...)
664 */
665static int
666smb_make_node(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
667{
668 int error;
669 struct iattr attr;
670
671 attr.ia_valid = ATTR_MODE | ATTR_UID | ATTR_GID;
672 attr.ia_mode = mode;
86a264ab 673 current_euid_egid(&attr.ia_uid, &attr.ia_gid);
1da177e4
LT
674
675 if (!new_valid_dev(dev))
676 return -EINVAL;
677
678 smb_invalid_dir_cache(dir);
679 error = smb_proc_setattr_unix(dentry, &attr, MAJOR(dev), MINOR(dev));
680 if (!error) {
681 error = smb_instantiate(dentry, 0, 0);
682 }
683 return error;
684}
685
686/*
687 * dentry = existing file
688 * new_dentry = new file
689 */
690static int
691smb_link(struct dentry *dentry, struct inode *dir, struct dentry *new_dentry)
692{
693 int error;
694
695 DEBUG1("smb_link old=%s/%s new=%s/%s\n",
696 DENTRY_PATH(dentry), DENTRY_PATH(new_dentry));
697 smb_invalid_dir_cache(dir);
698 error = smb_proc_link(server_from_dentry(dentry), dentry, new_dentry);
699 if (!error) {
700 smb_renew_times(dentry);
701 error = smb_instantiate(new_dentry, 0, 0);
702 }
703 return error;
704}
This page took 0.532942 seconds and 5 git commands to generate.